📄 editoptions.c
字号:
/* return -1 on error, 0 on success */int save_options_section (const char *file, const char *section, const char *text){ char *t, *p, *result; t = loadfile (file, 0); if (!t) /* file does not exist, savefile() will create it */ t = (char *) strdup ("\n\n"); p = find_section (t, section); if (p) { *p++ = 0; p = strstr (p, "\n["); result = catstrs (t, section, "\n", text, p, 0); } else { result = catstrs (section, "\n", text, "\n", t, 0); } free (t); return savefile (file, result, strlen (result), 0600);}/* return -1 on error */int load_user_defined_keys (struct key_list k_list[], const char *file){ char kname[128]; struct key_list kl; char *s, *p; p = s = get_options_section (file, "[Key Defines]"); if (!s) return -1; for (;;) { int i; *kname = 0; kl.state0 = 0; kl.state1 = 0; kl.state2 = 0; kl.keycode0 = 0; kl.keycode1 = 0; kl.keycode2 = 0; i = sscanf (p, "%s %x %x %x %x %x %x", kname, &kl.state0, &kl.keycode0, &kl.state1, &kl.keycode1, &kl.state2, &kl.keycode2); if (i >= 3) for (i = 0; k_list[i].key_name[0]; i++) { if (!strcasecmp (kname, k_list[i].key_name)) { k_list[i].state0 = kl.state0; k_list[i].state1 = kl.state1; k_list[i].state2 = kl.state2; k_list[i].keycode0 = kl.keycode0; k_list[i].keycode1 = kl.keycode1; k_list[i].keycode2 = kl.keycode2; break; } } p = strchr (p, '\n'); if (!p) break; p++; i++; } free (s); return 0;}int load_keys (const char *file){ return load_user_defined_keys (klist, file);}/* saves the klist key list into the options file in the section [Key Defines]. saves only those keys that have at least one define. Returns -1 on error. */int save_user_defined_keys (struct key_list k_list[], const char *file){ char *s, *p; int n, i; p = s = malloc (MAX_KEY_TEXT_SIZE); if (!s) return -1; for (i = 0; k_list[i].key_name[0]; i++) { if (k_list[i].keycode2) { sprintf (p, "%s\t%x %x %x %x %x %x\n%n", k_list[i].key_name, k_list[i].state0, k_list[i].keycode0, k_list[i].state1, k_list[i].keycode1, k_list[i].state2, k_list[i].keycode2, &n); p += n; } else if (k_list[i].keycode1) { sprintf (p, "%s\t%x %x %x %x\n%n", k_list[i].key_name, k_list[i].state0, k_list[i].keycode0, k_list[i].state1, k_list[i].keycode1, &n); p += n; } else if (k_list[i].keycode0) { sprintf (p, "%s\t%x %x\n%n", k_list[i].key_name, k_list[i].state0, k_list[i].keycode0, &n); p += n; } } *p = 0; n = save_options_section (file, "[Key Defines]", s); free (s); return n;}/* This converts the klist list into a text block with each line containing a key define. The returned text is for display in a text box widget. Return 0 on error. Result must be free'd. */char **get_key_text (void *data, int line, int *num_fields, int *tagged){ struct key_list *get_klist; static char key_0[16]; static char key_1[16]; static char key_2[16]; static char key_s[4] = ""; static char *result[5] = {0, 0, 0, 0, 0}; static int i = 0; get_klist = (struct key_list *) data; if (!get_klist[line].key_name[0]) return 0; if (!key_s[0]) sprintf (key_s, "\f%c", (unsigned char) CImageStringWidth ("99999")); if (!i) for (i = 0; get_klist[i].key_name[0]; i++) { char *p; strcpy (get_klist[i].key_name, _(get_klist[i].key_name)); for (p = get_klist[i].key_name; *p; p++) *p = *p == '.' ? '\b' : *p; } result[0] = get_klist[line].key_name; *num_fields = 4; *tagged = 0; if (get_klist[line].keycode0) { sprintf (key_0, "\t%d\t", get_klist[line].keycode0); result[1] = key_0; *tagged = 1; } else result[1] = key_s; if (get_klist[line].keycode1) { sprintf (key_1, "\t%d\t", get_klist[line].keycode1); result[2] = key_1; *tagged = 1; } else result[2] = key_s; if (get_klist[line].keycode2) { sprintf (key_2, "\t%d\t", get_klist[line].keycode2); result[3] = key_2; *tagged = 1; } else result[3] = key_s; return result;}static void move_down (struct key_list k_list[], CWidget * w){ int i, j; CTextboxCursorMove (w, CK_Down); for (j = 0; j < 6; j++) { i = w->cursor; if (k_list[i].key_name[0]) if (*(k_list[i].key_name) == '\t') CTextboxCursorMove (w, CK_Down); }}int cb_learnkeys (CWidget * w, XEvent * xe, CEvent * ce){ int i; KeySym x_key;/* we must ignore control, alt, etc. */ x_key = CKeySym (xe); if (mod_type_key (x_key)) return 0; i = w->cursor; if (*(klist[i].key_name) != '\t') { if (xe->type == KeyPress) { if (!klist[i].keycode0) { klist[i].keycode0 = xe->xkey.keycode; klist[i].state0 = xe->xkey.state; } else if (!klist[i].keycode1) { klist[i].keycode1 = xe->xkey.keycode; klist[i].state1 = xe->xkey.state; } else if (!klist[i].keycode2) { klist[i].keycode2 = xe->xkey.keycode; klist[i].state2 = xe->xkey.state; } move_down (klist, w); CExpose ("_learnkeysbox"); } } else { move_down (klist, w); CExpose ("_learnkeysbox"); } return 1; /* always handled. This will stop the tab key from being seen */}int cb_save (CWidget * w, XEvent * xe, CEvent * ce){ if (save_user_defined_keys (klist, editor_options_file)) CErrorDialog (main_window, 20, 20, _(" Save keys "), get_sys_error (_(" Error trying to save file "))); return 0;}int cb_clear (CWidget * w, XEvent * xe, CEvent * ce){ int i; for (i = 0; klist[i].key_name[0]; i++) { klist[i].state0 = 0; klist[i].state1 = 0; klist[i].state2 = 0; klist[i].keycode0 = 0; klist[i].keycode1 = 0; klist[i].keycode2 = 0; } CFocus (CIdent ("_learnkeysbox")); CExpose ("_learnkeysbox"); return 0;}int cb_clearline (CWidget * w, XEvent * xe, CEvent * ce){ int i; w = CIdent ("_learnkeysbox"); i = w->cursor; klist[i].state0 = 0; klist[i].state1 = 0; klist[i].state2 = 0; klist[i].keycode0 = 0; klist[i].keycode1 = 0; klist[i].keycode2 = 0; move_down (klist, w); CFocus (w); CExpose ("_learnkeysbox"); return 0;}/* only allowed to draw one of these */CWidget *Cdrawlearnkeys (Window parent, int x, int y, int columns, int lines){ CWidget *w; CPushFont ("editor", 0); w = CDrawFieldedTextbox ("_learnkeysbox", parent, x, y, AUTO_WIDTH, lines * FONT_PIX_PER_LINE + 6, 0, 0, get_key_text, TEXTBOX_NO_KEYS, (void *) klist); CPopFont ();/* Tool hint */ CSetToolHint ("_learnkeysbox", _("Click on an editing action and the press the key to bind it to")); CAddCallback ("_learnkeysbox", cb_learnkeys); CGetHintPos (0, &y); (CDrawButton ("_learnkeysbox.save", parent, x, y, AUTO_WIDTH, AUTO_HEIGHT, " Save "))->takes_focus = 0;/* Tool hint */ CSetToolHint ("_learnkeysbox.save", _("Save key defines to your initialisation file")); CAddCallback ("_learnkeysbox.save", cb_save); CGetHintPos (&x, 0); (CDrawButton ("_learnkeysbox.clear", parent, x, y, AUTO_WIDTH, AUTO_HEIGHT, _(" Clear all ")))->takes_focus = 0;/* Tool hint */ CSetToolHint ("_learnkeysbox.clear", _("Erase all user key definitions")); CAddCallback ("_learnkeysbox.clear", cb_clear); CGetHintPos (&x, 0); (CDrawButton ("_learnkeysbox.clearline", parent, x, y, AUTO_WIDTH, AUTO_HEIGHT, _(" Clear line ")))->takes_focus = 0;/* Tool hint */ CSetToolHint ("_learnkeysbox.clearline", _("Erase key definition on this line")); CAddCallback ("_learnkeysbox.clearline", cb_clearline); return w;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -