summaryrefslogtreecommitdiff
path: root/commands.c
diff options
context:
space:
mode:
Diffstat (limited to 'commands.c')
-rw-r--r--commands.c255
1 files changed, 130 insertions, 125 deletions
diff --git a/commands.c b/commands.c
index e7f23b1..bb6d98f 100644
--- a/commands.c
+++ b/commands.c
@@ -67,6 +67,11 @@ static void AddDoubleClickCallback(Widget, Bool);
static Bool ReallyDoLoad(char*, char*);
static char *makeBackupName(String, String, unsigned);
+/*
+ * External
+ */
+extern void _XawTextShowPosition(TextWidget);
+
extern Widget scratch, texts[3], labels[3];
static Boolean double_click = FALSE;
@@ -209,7 +214,16 @@ int rename (from, to)
void
SaveFile(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
- DoSave(w, NULL, NULL);
+ if (line_edit) {
+ /* Don't try to save buffer with regex string.
+ * Call CancelFindFile() to leave line_edit mode.
+ */
+ XeditPrintf("Save: Leaving line edit mode -- nothing saved.\n");
+ CancelFindFile(w, event, params, num_params);
+ Feep();
+ }
+ else
+ DoSave(w, NULL, NULL);
}
/*ARGSUSED*/
@@ -218,7 +232,6 @@ DoSave(Widget w, XtPointer client_data, XtPointer call_data)
{
String name = GetString(filenamewindow);
String filename = ResolveName(name);
- char buf[BUFSIZ];
FileAccess file_access;
xedit_flist_item *item;
Boolean exists;
@@ -238,10 +251,8 @@ DoSave(Widget w, XtPointer client_data, XtPointer call_data)
struct stat st;
if (stat(filename, &st) == 0 && !S_ISREG(st.st_mode)) {
- XmuSnprintf(buf, sizeof(buf),
- "Save: file %s is not a regular file -- nothing saved.\n",
- name);
- XeditPrintf(buf);
+ XeditPrintf("Save: file %s is not a regular file "
+ "-- nothing saved.\n", name);
Feep();
return;
}
@@ -250,11 +261,9 @@ DoSave(Widget w, XtPointer client_data, XtPointer call_data)
item = FindTextSource(NULL, filename);
if (item != NULL && item->source != source) {
if (!double_click || (dc_state && dc_state != DC_LOADED)) {
- XmuSnprintf(buf, sizeof(buf),
- "Save: file %s is already loaded, "
+ XeditPrintf("Save: file %s is already loaded, "
"Save again to unload it -- nothing saved.\n",
name);
- XeditPrintf(buf);
Feep();
double_click = TRUE;
dc_state = DC_LOADED;
@@ -284,11 +293,9 @@ DoSave(Widget w, XtPointer client_data, XtPointer call_data)
if (!item || strcmp(item->filename, filename)) {
if (file_access == WRITE_OK && exists) {
if (!double_click || (dc_state && dc_state != DC_CLOBBER)) {
- XmuSnprintf(buf, sizeof(buf),
- "Save: file %s already exists, "
+ XeditPrintf("Save: file %s already exists, "
"Save again to overwrite it -- nothing saved.\n",
name);
- XeditPrintf(buf);
Feep();
double_click = TRUE;
dc_state = DC_CLOBBER;
@@ -301,95 +308,87 @@ DoSave(Widget w, XtPointer client_data, XtPointer call_data)
if (!item)
item = FindTextSource(source, NULL);
}
-
- if (app_resources.enableBackups && exists) {
- char backup_file[BUFSIZ];
-
- if (makeBackupName(backup_file, filename, sizeof(backup_file)) == NULL
- || rename(filename, backup_file) != 0) {
- XmuSnprintf(buf, sizeof(buf),"error backing up file: %s\n",
- filename);
- XeditPrintf(buf);
+
+ if (app_resources.enableBackups && exists) {
+ char backup_file[BUFSIZ];
+
+ if (makeBackupName(backup_file, filename, sizeof(backup_file)) == NULL
+ || rename(filename, backup_file) != 0) {
+ XeditPrintf("Error backing up file: %s\n", filename);
+ }
}
- }
-
- switch( file_access = MaybeCreateFile(filename)) {
- case NO_READ:
- case READ_OK:
- XmuSnprintf(buf, sizeof(buf),
- "File %s could not be opened for writing.\n", name);
- Feep();
- break;
- case WRITE_OK:
- if ( XawAsciiSaveAsFile(source, filename) ) {
- int i;
- Arg args[1];
- char label_buf[BUFSIZ];
-
- /* Keep file protection mode */
- if (item && item->mode)
- chmod(filename, item->mode);
-
- XmuSnprintf(label_buf, sizeof(label_buf),
- "%s Read - Write", name);
- XtSetArg(args[0], XtNlabel, label_buf);
- for (i = 0; i < 3; i++)
- if (XawTextGetSource(texts[i]) == source)
- XtSetValues(labels[i], args, 1);
-
- XmuSnprintf(buf, sizeof(buf), "Saved file: %s\n", name);
-
- if (item && item->source != scratch) {
- XtSetArg(args[0], XtNlabel, filename);
- XtSetValues(item->sme, args, 1);
-
- XtSetArg(args[0], XtNeditType, XawtextEdit);
- XtSetValues(item->source, args, 1);
-
- XtFree(item->name);
- XtFree(item->filename);
- item->name = XtNewString(name);
- item->filename = XtNewString(filename);
- item->flags = EXISTS_BIT;
- }
- else {
- item = flist.itens[0];
- XtRemoveCallback(scratch, XtNcallback, SourceChanged,
- (XtPointer)item);
- item->source = scratch =
- XtVaCreateWidget("textSource", international ?
- multiSrcObjectClass :
- asciiSrcObjectClass,
- topwindow,
- XtNtype, XawAsciiFile,
- XtNeditType, XawtextEdit,
- NULL, NULL);
- ResetSourceChanged(item);
- XtAddCallback(scratch, XtNcallback, SourceChanged,
- (XtPointer)item);
-
- item = AddTextSource(source, name, filename, EXISTS_BIT,
- file_access);
- XtAddCallback(item->source, XtNcallback, SourceChanged,
- (XtPointer)item);
- }
- item->flags |= EXISTS_BIT;
- ResetSourceChanged(item);
- }
- else {
- XmuSnprintf(buf, sizeof(buf), "Error saving file: %s\n", name);
- Feep();
- }
- break;
- default:
- XmuSnprintf(buf, sizeof(buf), "%s %s",
- "Internal function MaybeCreateFile()",
- "returned unexpected value.\n");
- Feep();
- break;
- }
- XeditPrintf(buf);
+ switch (file_access = MaybeCreateFile(filename)) {
+ case NO_READ:
+ case READ_OK:
+ XeditPrintf("File %s could not be opened for writing.\n", name);
+ Feep();
+ break;
+ case WRITE_OK:
+ if (XawAsciiSaveAsFile(source, filename)) {
+ int i;
+ Arg args[1];
+ char label_buf[BUFSIZ];
+
+ /* Keep file protection mode */
+ if (item && item->mode)
+ chmod(filename, item->mode);
+
+ XmuSnprintf(label_buf, sizeof(label_buf),
+ "%s Read - Write", name);
+ XtSetArg(args[0], XtNlabel, label_buf);
+ for (i = 0; i < 3; i++)
+ if (XawTextGetSource(texts[i]) == source)
+ XtSetValues(labels[i], args, 1);
+
+ XeditPrintf("Saved file: %s\n", name);
+
+ if (item && item->source != scratch) {
+ XtSetArg(args[0], XtNlabel, filename);
+ XtSetValues(item->sme, args, 1);
+
+ XtSetArg(args[0], XtNeditType, XawtextEdit);
+ XtSetValues(item->source, args, 1);
+
+ XtFree(item->name);
+ XtFree(item->filename);
+ item->name = XtNewString(name);
+ item->filename = XtNewString(filename);
+ item->flags = EXISTS_BIT;
+ }
+ else {
+ item = flist.itens[0];
+ XtRemoveCallback(scratch, XtNcallback, SourceChanged,
+ (XtPointer)item);
+ item->source = scratch =
+ XtVaCreateWidget("textSource", international ?
+ multiSrcObjectClass :
+ asciiSrcObjectClass,
+ topwindow,
+ XtNtype, XawAsciiFile,
+ XtNeditType, XawtextEdit,
+ NULL, NULL);
+ ResetSourceChanged(item);
+ XtAddCallback(scratch, XtNcallback, SourceChanged,
+ (XtPointer)item);
+
+ item = AddTextSource(source, name, filename, EXISTS_BIT,
+ file_access);
+ XtAddCallback(item->source, XtNcallback, SourceChanged,
+ (XtPointer)item);
+ }
+ item->flags |= EXISTS_BIT;
+ ResetSourceChanged(item);
+ }
+ else {
+ XeditPrintf("Error saving file: %s\n", name);
+ Feep();
+ }
+ break;
+ default:
+ Feep();
+ break;
+ }
}
/*ARGSUSED*/
@@ -402,12 +401,17 @@ DoLoad(Widget w, XtPointer client_data, XtPointer call_data)
}
}
+Bool
+LoadFileInTextwindow(char *name, char *resolved_name)
+{
+ return (ReallyDoLoad(name, resolved_name));
+}
+
static Bool
ReallyDoLoad(char *name, char *filename)
{
Arg args[5];
Cardinal num_args = 0;
- char buf[BUFSIZ];
xedit_flist_item *item;
Widget source = XawTextGetSource(textwindow);
@@ -456,37 +460,28 @@ ReallyDoLoad(char *name, char *filename)
switch( file_access = CheckFilePermissions(filename, &exists) ) {
case NO_READ:
if (exists)
- XmuSnprintf(buf, sizeof(buf), "File %s, %s", name,
- "exists, and could not be opened for reading.\n");
+ XeditPrintf("File %s, %s", name,
+ "exists, and could not be opened for reading.\n");
else
- XmuSnprintf(buf, sizeof(buf), "File %s %s %s", name,
+ XeditPrintf("File %s %s %s", name,
"does not exist, and",
- "the directory could not be opened for writing.\n");
+ "the directory could not be opened for writing.\n");
- XeditPrintf(buf);
Feep();
return (False);
case READ_OK:
XtSetArg(args[num_args], XtNeditType, XawtextRead); num_args++;
- XmuSnprintf(buf, sizeof(buf), "File %s opened READ ONLY.\n",
- name);
+ XeditPrintf("File %s opened READ ONLY.\n", name);
break;
case WRITE_OK:
XtSetArg(args[num_args], XtNeditType, XawtextEdit); num_args++;
- XmuSnprintf(buf, sizeof(buf), "File %s opened read - write.\n",
- name);
+ XeditPrintf("File %s opened read - write.\n", name);
break;
default:
- XmuSnprintf(buf, sizeof(buf), "%s %s",
- "Internal function MaybeCreateFile()",
- "returned unexpected value.\n");
- XeditPrintf(buf);
Feep();
return (False);
}
- XeditPrintf(buf);
-
if (exists) {
flags = EXISTS_BIT;
XtSetArg(args[num_args], XtNstring, filename); num_args++;
@@ -511,7 +506,7 @@ ReallyDoLoad(char *name, char *filename)
if (exists && file_access == WRITE_OK) {
struct stat st;
- if (stat(filename, &st) == 0)
+ if (stat(item->filename, &st) == 0)
item->mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
}
SwitchTextSource(item);
@@ -729,14 +724,23 @@ KillFile(Widget w, XEvent *event, String *params, Cardinal *num_params)
void
FindFile(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
- char *string = GetString(filenamewindow);
- char *slash = NULL;
+ char *string;
+ char *slash;
XawTextBlock block;
XawTextPosition end = XawTextSourceScan(XawTextGetSource(filenamewindow),
0, XawstAll, XawsdRight, 1, True);
- if (string)
- slash = strrchr(string, '/');
+ if (!line_edit) {
+ string = GetString(filenamewindow);
+ if (string)
+ slash = strrchr(string, '/');
+ }
+ else {
+ string = "";
+ slash = NULL;
+ line_edit = False;
+ }
+
block.firstPos = 0;
block.format = FMT8BIT;
block.ptr = string;
@@ -746,7 +750,6 @@ FindFile(Widget w, XEvent *event, String *params, Cardinal *num_params)
XawTextReplace(filenamewindow, 0, end, &block);
XawTextSetInsertionPoint(filenamewindow, end);
XtSetKeyboardFocus(topwindow, filenamewindow);
- line_edit = False;
}
/*ARGSUSED*/
@@ -778,6 +781,9 @@ CancelFindFile(Widget w, XEvent *event, String *params, Cardinal *num_params)
XtSetArg(args[0], XtNstring, NULL);
XtSetValues(filenamewindow, args, 1);
+ /* XXX This probably should be done by the TextWidget, i.e. notice
+ * if the cursor became inivisible due to an horizontal scroll */
+ _XawTextShowPosition((TextWidget)filenamewindow);
if (XtIsManaged(XtParent(dirwindow)))
SwitchDirWindow(False);
@@ -893,7 +899,6 @@ FileCompletion(Widget w, XEvent *event, String *params, Cardinal *num_params)
strncpy(fname, slash2 + 1, sizeof(fname) - 1);
fname[sizeof(fname) - 1] = '\0';
save = XtRealloc(save, slen - diff + hlen + 2);
- (void)memmove(&save[hlen], slash2, slen - diff + 1);
(void)memmove(save, home, hlen);
save[hlen] = '/';
strcpy(&save[hlen + 1], fname);
@@ -937,11 +942,11 @@ FileCompletion(Widget w, XEvent *event, String *params, Cardinal *num_params)
mlen = 0;
match[0] = '\0';
- (void)readdir(dir); /* "." */
- (void)readdir(dir); /* ".." */
while ((ent = readdir(dir)) != NULL) {
unsigned d_namlen = strlen(ent->d_name);
+ if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
+ continue;
if (d_namlen >= len && strncmp(ent->d_name, file_name, len) == 0) {
char *tmp = &(ent->d_name[len]), *mat = match;
struct stat st;
@@ -996,7 +1001,7 @@ FileCompletion(Widget w, XEvent *event, String *params, Cardinal *num_params)
closedir(dir);
changed = mlen != 0;
- if (n_matches) {
+ if (first || n_matches) {
Bool free_matches = True, add_slash = n_matches == 1 && isdir && !slash;
if (mlen && has_dot && match[mlen - 1] == '.')