📄 editcmd.c
字号:
if (r[0] && r[1]) if (*--q == '/') *q = '\0'; return r;}#endif /* GTK */void edit_split_filename (WEdit * edit, char *longname){ char *exp, *p;#if defined(MIDNIGHT) || defined(GTK) exp = canonicalize_pathname (longname);#else exp = pathdup (longname); /* this ensures a full path */#endif if (edit->filename) free (edit->filename); if (edit->dir) free (edit->dir); p = strrchr (exp, '/'); edit->filename = (char *) strdup (++p); *p = 0; edit->dir = (char *) strdup (exp); free (exp);}#endif /* ! MIDNIGHT *//* here we want to warn the user of overwriting an existing file, but only if they have made a change to the filename *//* returns 1 on success */int edit_save_as_cmd (WEdit * edit){/* This heads the 'Save As' dialog box */ char *exp = 0; int different_filename = 0; exp = edit_get_save_file (edit->dir, edit->filename, _(" Save As ")); edit_push_action (edit, KEY_PRESS + edit->start_display); if (exp) { if (!*exp) { free (exp); edit->force |= REDRAW_COMPLETELY; return 0; } else { if (strcmp(catstrs (edit->dir, edit->filename, 0), exp)) { int file; different_filename = 1; if ((file = open ((char *) exp, O_RDONLY)) != -1) { /* the file exists */ close (file); if (edit_query_dialog2 (_(" Warning "), _(" A file already exists with this name. "), /* Push buttons to over-write the current file, or cancel the operation */ _("Overwrite"), _("Cancel"))) { edit->force |= REDRAW_COMPLETELY; return 0; } } } if (edit_save_file (edit, exp)) { edit_split_filename (edit, exp); free (exp); edit->modified = 0;#if defined(MIDNIGHT) || defined(GTK) edit->delete_file = 0;#endif if (different_filename && !edit->explicit_syntax) edit_load_syntax (edit, 0, 0); edit->force |= REDRAW_COMPLETELY; return 1; } else { free (exp); edit_error_dialog (_(" Save as "), get_sys_error (_(" Error trying to save file. "))); edit->force |= REDRAW_COMPLETELY; return 0; } } } edit->force |= REDRAW_COMPLETELY; return 0;}/* {{{ Macro stuff starts here */#ifdef MIDNIGHTint raw_callback (struct Dlg_head *h, int key, int Msg){ switch (Msg) { case DLG_DRAW: attrset (REVERSE_COLOR); dlg_erase (h); draw_box (h, 1, 1, h->lines - 2, h->cols - 2); attrset (COLOR_HOT_NORMAL); dlg_move (h, 1, 2); printw (h->title); break; case DLG_KEY: h->running = 0; h->ret_value = key; return 1; } return 0;}/* gets a raw key from the keyboard. Passing cancel = 1 draws a cancel button thus allowing c-c etc.. Alternatively, cancel = 0 will return the next key pressed */int edit_raw_key_query (char *heading, char *query, int cancel){ int w = strlen (query) + 7; struct Dlg_head *raw_dlg = create_dlg (0, 0, 7, w, dialog_colors,/* NLS ? */ raw_callback, "[Raw Key Query]", "raw_key_input", DLG_CENTER | DLG_TRYUP); x_set_dialog_title (raw_dlg, heading); raw_dlg->raw = 1; /* to return even a tab key */ if (cancel) add_widget (raw_dlg, button_new (4, w / 2 - 5, B_CANCEL, NORMAL_BUTTON, _("Cancel"), 0, 0, 0)); add_widget (raw_dlg, label_new (3 - cancel, 2, query, 0)); add_widget (raw_dlg, input_new (3 - cancel, w - 5, INPUT_COLOR, 2, "", 0)); run_dlg (raw_dlg); w = raw_dlg->ret_value; destroy_dlg (raw_dlg); if (cancel) if (w == XCTRL ('g') || w == XCTRL ('c') || w == ESC_CHAR || w == B_CANCEL) return 0;/* hence ctrl-a (=B_CANCEL), ctrl-g, ctrl-c, and Esc are cannot returned */ return w;}#elseint edit_raw_key_query (char *heading, char *query, int cancel){#ifdef GTK /* *** */ return 0;#else return CKeySymMod (CRawkeyQuery (0, 0, 0, heading, query));#endif}#endif/* creates a macro file if it doesn't exist */static FILE *edit_open_macro_file (const char *r){ char *filename; int file; filename = catstrs (home_dir, MACRO_FILE, 0); if ((file = open (filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) return 0; close (file); return fopen (filename, r);}#define MAX_MACROS 1024static int saved_macro[MAX_MACROS + 1] ={0, 0};static int saved_macros_loaded = 0;/* This is just to stop the macro file be loaded over and over for keys that aren't defined to anything. On slow systems this could be annoying. */int macro_exists (int k){ int i; for (i = 0; i < MAX_MACROS && saved_macro[i]; i++) if (saved_macro[i] == k) return i; return -1;}/* returns 1 on error */int edit_delete_macro (WEdit * edit, int k){ struct macro macro[MAX_MACRO_LENGTH]; FILE *f, *g; int s, i, n, j = 0; if (saved_macros_loaded) if ((j = macro_exists (k)) < 0) return 0; g = fopen (catstrs (home_dir, TEMP_FILE, 0), "w"); if (!g) {/* This heads the delete macro error dialog box */ edit_error_dialog (_(" Delete macro "),/* 'Open' = load temp file */ get_sys_error (_(" Error trying to open temp file "))); return 1; } f = edit_open_macro_file ("r"); if (!f) {/* This heads the delete macro error dialog box */ edit_error_dialog (_(" Delete macro "),/* 'Open' = load temp file */ get_sys_error (_(" Error trying to open macro file "))); fclose (g); return 1; } for (;;) { n = fscanf (f, _("key '%d 0': "), &s); if (!n || n == EOF) break; n = 0; while (fscanf (f, "%d %ld, ", ¯o[n].command, ¯o[n].ch)) n++; fscanf (f, ";\n"); if (s != k) { fprintf (g, _("key '%d 0': "), s); for (i = 0; i < n; i++) fprintf (g, "%d %ld, ", macro[i].command, macro[i].ch); fprintf (g, ";\n"); } }; fclose (f); fclose (g); if (rename (catstrs (home_dir, TEMP_FILE, 0), catstrs (home_dir, MACRO_FILE, 0)) == -1) {/* This heads the delete macro error dialog box */ edit_error_dialog (_(" Delete macro "), get_sys_error (_(" Error trying to overwrite macro file "))); return 1; } if (saved_macros_loaded) memmove (saved_macro + j, saved_macro + j + 1, sizeof (int) * (MAX_MACROS - j - 1)); return 0;}/* returns 0 on error */int edit_save_macro_cmd (WEdit * edit, struct macro macro[], int n){ FILE *f; int s, i; edit_push_action (edit, KEY_PRESS + edit->start_display);/* This heads the 'Macro' dialog box */ s = edit_raw_key_query (_(" Macro "),/* Input line for a single key press follows the ':' */ _(" Press the macro's new hotkey: "), 1); edit->force |= REDRAW_COMPLETELY; if (s) { if (edit_delete_macro (edit, s)) return 0; f = edit_open_macro_file ("a+"); if (f) { fprintf (f, _("key '%d 0': "), s); for (i = 0; i < n; i++) fprintf (f, "%d %ld, ", macro[i].command, macro[i].ch); fprintf (f, ";\n"); fclose (f); if (saved_macros_loaded) { for (i = 0; i < MAX_MACROS && saved_macro[i]; i++); saved_macro[i] = s; } return 1; } else/* This heads the 'Save Macro' dialog box */ edit_error_dialog (_(" Save macro "), get_sys_error (_(" Error trying to open macro file "))); } return 0;}void edit_delete_macro_cmd (WEdit * edit){ int command;#ifdef MIDNIGHT command = CK_Macro (edit_raw_key_query (_ (" Delete Macro "), _ (" Press macro hotkey: "), 1));#else/* This heads the 'Delete Macro' dialog box */#ifdef GTK/* *** */ command = 0;#else command = CKeySymMod (CRawkeyQuery (0, 0, 0, _ (" Delete Macro "),/* Input line for a single key press follows the ':' */ _ (" Press macro hotkey: ")));#endif#endif if (!command) return; edit_delete_macro (edit, command);}/* return 0 on error */int edit_load_macro_cmd (WEdit * edit, struct macro macro[], int *n, int k){ FILE *f; int s, i = 0, found = 0; if (saved_macros_loaded) if (macro_exists (k) < 0) return 0; if ((f = edit_open_macro_file ("r"))) { struct macro dummy; do { int u; u = fscanf (f, _("key '%d 0': "), &s); if (!u || u == EOF) break; if (!saved_macros_loaded) saved_macro[i++] = s; if (!found) { *n = 0; while (*n < MAX_MACRO_LENGTH && 2 == fscanf (f, "%d %ld, ", ¯o[*n].command, ¯o[*n].ch)) (*n)++; } else { while (2 == fscanf (f, "%d %ld, ", &dummy.command, &dummy.ch)); } fscanf (f, ";\n"); if (s == k) found = 1; } while (!found || !saved_macros_loaded); if (!saved_macros_loaded) { saved_macro[i] = 0; saved_macros_loaded = 1; } fclose (f); return found; } else/* This heads the 'Load Macro' dialog box */ edit_error_dialog (_(" Load macro "), get_sys_error (_(" Error trying to open macro file "))); return 0;}/* }}} Macro stuff starts here *//* returns 1 on success */int edit_save_confirm_cmd (WEdit * edit){ char *f; if (edit_confirm_save) {#ifdef MIDNIGHT f = catstrs (_(" Confirm save file? : "), edit->filename, " ", 0);#else f = catstrs (_(" Confirm save file? : "), edit->dir, edit->filename, " ", 0);#endif/* Buttons to 'Confirm save file' query */ if (edit_query_dialog2 (_(" Save file "), f, _("Save"), _("Cancel"))) return 0; } return edit_save_cmd (edit);}/* returns 1 on success */int edit_save_cmd (WEdit * edit){ if (!edit_save_file (edit, catstrs (edit->dir, edit->filename, 0))) return edit_save_as_cmd (edit); edit->force |= REDRAW_COMPLETELY; edit->modified = 0;#if defined(MIDNIGHT) || defined(GTK) edit->delete_file = 0;#endif return 1;}/* returns 1 on success */int edit_new_cmd (WEdit * edit){ if (edit->modified) { if (edit_query_dialog2 (_ (" Warning "), _ (" Current text was modified without a file save. \n Continue discards these changes. "), _ ("Continue"), _ ("Cancel"))) { edit->force |= REDRAW_COMPLETELY; return 0; } } edit->force |= REDRAW_COMPLETELY; edit->modified = 0; return edit_renew (edit); /* if this gives an error, something has really screwed up */}/* returns 1 on error */int edit_load_file_from_filename (WEdit * edit, char *exp){ if (!edit_reload (edit, exp, 0, "", 0)) return 1; edit_split_filename (edit, exp); edit->modified = 0; return 0;}int edit_load_cmd (WEdit * edit){ char *exp; if (edit->modified) { if (edit_query_dialog2 (_ (" Warning "), _ (" Current text was modified without a file save. \n Continue discards these changes. "), _ ("Continue"), _ ("Cancel"))) { edit->force |= REDRAW_COMPLETELY; return 0; } } exp = edit_get_load_file (edit->dir, edit->filename, _ (" Load ")); if (exp) { if (*exp) edit_load_file_from_filename (edit, exp); free (exp); } edit->force |= REDRAW_COMPLETELY; return 0;}/* if mark2 is -1 then marking is from mark1 to the cursor. Otherwise its between the markers. This handles this. Returns 1 if no text is marked. */int eval_marks (WEdit * edit, long *start_mark, long *end_mark){ if (edit->mark1 != edit->mark2) { if (edit->mark2 >= 0) { *start_mark = min (edit->mark1, edit->mark2); *end_mark = max (edit->mark1, edit->mark2); } else { *start_mark = min (edit->mark1, edit->curs1); *end_mark = max (edit->mark1, edit->curs1); edit->column2 = edit->curs_col; } return 0; } else { *start_mark = *end_mark = 0; edit->column2 = edit->column1 = 0; return 1; }}/*Block copy, move and delete commands */extern int column_highlighting;#ifdef MIDNIGHT#define space_width 1#elseextern int space_width;#endifvoid edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width){ long cursor; int i, col; cursor = edit->curs1; col = edit_get_col (edit); for (i = 0; i < size; i++) { if (data[i] == '\n') { /* fill in and move to next line */ int l; long p; if (edit_get_byte (edit, edit->curs1) != '\n') { l = width - (edit_get_col (edit) - col); while (l > 0) { edit_insert (edit, ' '); l -= space_width; } } for (p = edit->curs1;; p++) { if (p == edit->last_byte) edit_insert_ahead (edit, '\n'); if (edit_get_byte (edit, p) == '\n') { p++; break; } } edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1); l = col - edit_get_col (edit); while (l >= space_width) { edit_insert (edit, ' '); l -= space_width; } continue; } edit_insert (edit, data[i]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -