1
/* edit.c - Editing the file names.
3
* Copyright (C) 2001, 2002, 2004, 2005 Oskar Liljeblad
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Library General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#include <string.h> /* C89 */
24
#include <stdlib.h> /* C89 */
25
#include <stdio.h> /* C89 */
26
#include <stdbool.h> /* Gnulib (POSIX) */
27
#include <quotearg.h> /* Gnulib */
28
#include <gettext.h> /* Gnulib (gettext) */
29
#define _(s) gettext(s)
32
#include "xvasprintf.h"
33
#include "common/string-utils.h"
34
#include "common/io-utils.h"
35
#include "common/error.h"
36
#include "common/llist.h"
39
LList *edit_file_list;
40
static bool accept_reedit = false;
41
static EditFormat *formats[] = {
42
&destination_only_format,
47
/* Find an edit format by its long or short name.
48
* Return NULL if no format was found.
51
find_edit_format_by_name(const char *name)
55
for (c = 0; c < sizeof(formats)/sizeof(*formats); c++) {
56
if (strcmp(name, formats[c]->name) == 0
57
|| strcmp(name, formats[c]->short_name) == 0)
64
/* A readline completion generator for the various edit formats.
67
edit_format_generator(const char *text, int state)
74
while (c < sizeof(formats)/sizeof(*formats)) {
75
char *name = formats[c]->name;
77
if (starts_with(name, text))
85
edit_files(bool all, bool force)
92
if (!accept_reedit || force) {
93
accept_reedit = false;
95
/* Determine what files we want to display to user */
96
llist_clear(edit_file_list);
98
if (plan != NULL && llist_size(plan->error) != 0)
99
llist_add_all(edit_file_list, plan->error);
101
llist_add_all(edit_file_list, work_files);
104
if (llist_is_empty(edit_file_list)) {
105
warn(_("There are no files to edit."));
109
/* Prepare the edit file */
110
file = fopen(edit_filename, "w");
112
warn(_("cannot create `%s': %s"), quotearg(edit_filename), errstr);
113
llist_clear(edit_file_list);
117
/* Write file names to the output file */
118
format->output(file, edit_file_list);
119
if (fclose(file) != 0) {
120
warn_errno(_("cannot close `%s'"), quotearg(edit_filename));
121
llist_clear(edit_file_list);
125
accept_reedit = true;
128
/* Start the editor */
129
cmd = xasprintf("%s %s", editor_program, edit_filename);
130
/* Fortunately, edit_filename usually does not contain
131
* special characters and does not start with a dash. */
135
warn(_("cannot start editor - %s"), quotearg(editor_program), errstr);
139
warn(_("editor exited with status %d"), quotearg(editor_program), rc);
144
file = fopen(edit_filename, "r");
146
warn(_("cannot open `%s' for reading: %s"), quotearg(edit_filename), errstr);
151
if (!format->input(file, edit_file_list))
153
if (fclose(file) != 0)
154
warn(_("cannot close `%s': %s"), quotearg(edit_filename), errstr);
155
accept_reedit = false;
157
/* Reset status of all files. */
158
for (llist_iterator(edit_file_list, &it); it.has_next(&it); ) {
159
FileSpec *spec = it.next(&it);
160
spec->status = STATUS_UNCHECKED;
162
llist_clear(edit_file_list);