📄 bind.c
字号:
A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */intrl_parse_and_bind (string) char *string;{ char *funname, *kname; register int c, i; int key, equivalency; while (string && whitespace (*string)) string++; if (!string || !*string || *string == '#') return 0; /* If this is a parser directive, act on it. */ if (*string == '$') { handle_parser_directive (&string[1]); return 0; } /* If we aren't supposed to be parsing right now, then we're done. */ if (_rl_parsing_conditionalized_out) return 0; i = 0; /* If this keyname is a complex key expression surrounded by quotes, advance to after the matching close quote. This code allows the backslash to quote characters in the key expression. */ if (*string == '"') { int passc = 0; for (i = 1; c = string[i]; i++) { if (passc) { passc = 0; continue; } if (c == '\\') { passc++; continue; } if (c == '"') break; } /* If we didn't find a closing quote, abort the line. */ if (string[i] == '\0') { _rl_init_file_error ("no closing `\"' in key binding"); return 1; } } /* Advance to the colon (:) or whitespace which separates the two objects. */ for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ ); equivalency = (c == ':' && string[i + 1] == '='); /* Mark the end of the command (or keyname). */ if (string[i]) string[i++] = '\0'; /* If doing assignment, skip the '=' sign as well. */ if (equivalency) string[i++] = '\0'; /* If this is a command to set a variable, then do that. */ if (_rl_stricmp (string, "set") == 0) { char *var = string + i; char *value; /* Make VAR point to start of variable name. */ while (*var && whitespace (*var)) var++; /* Make VALUE point to start of value string. */ value = var; while (*value && !whitespace (*value)) value++; if (*value) *value++ = '\0'; while (*value && whitespace (*value)) value++; rl_variable_bind (var, value); return 0; } /* Skip any whitespace between keyname and funname. */ for (; string[i] && whitespace (string[i]); i++); funname = &string[i]; /* Now isolate funname. For straight function names just look for whitespace, since that will signify the end of the string. But this could be a macro definition. In that case, the string is quoted, so skip to the matching delimiter. We allow the backslash to quote the delimiter characters in the macro body. */ /* This code exists to allow whitespace in macro expansions, which would otherwise be gobbled up by the next `for' loop.*/ /* XXX - it may be desirable to allow backslash quoting only if " is the quoted string delimiter, like the shell. */ if (*funname == '\'' || *funname == '"') { int delimiter = string[i++], passc; for (passc = 0; c = string[i]; i++) { if (passc) { passc = 0; continue; } if (c == '\\') { passc = 1; continue; } if (c == delimiter) break; } if (c) i++; } /* Advance to the end of the string. */ for (; string[i] && !whitespace (string[i]); i++); /* No extra whitespace at the end of the string. */ string[i] = '\0'; /* Handle equivalency bindings here. Make the left-hand side be exactly whatever the right-hand evaluates to, including keymaps. */ if (equivalency) { return 0; } /* If this is a new-style key-binding, then do the binding with rl_bind_keyseq (). Otherwise, let the older code deal with it. */ if (*string == '"') { char *seq; register int j, k, passc; seq = (char *)xmalloc (1 + strlen (string)); for (j = 1, k = passc = 0; string[j]; j++) { /* Allow backslash to quote characters, but leave them in place. This allows a string to end with a backslash quoting another backslash, or with a backslash quoting a double quote. The backslashes are left in place for rl_translate_keyseq (). */ if (passc || (string[j] == '\\')) { seq[k++] = string[j]; passc = !passc; continue; } if (string[j] == '"') break; seq[k++] = string[j]; } seq[k] = '\0'; /* Binding macro? */ if (*funname == '\'' || *funname == '"') { j = strlen (funname); /* Remove the delimiting quotes from each end of FUNNAME. */ if (j && funname[j - 1] == *funname) funname[j - 1] = '\0'; rl_macro_bind (seq, &funname[1], _rl_keymap); } else rl_bind_keyseq (seq, rl_named_function (funname)); free (seq); return 0; } /* Get the actual character we want to deal with. */ kname = strrchr (string, '-'); if (!kname) kname = string; else kname++; key = glean_key_from_name (kname); /* Add in control and meta bits. */ if (substring_member_of_array (string, _rl_possible_control_prefixes)) key = CTRL (_rl_to_upper (key)); if (substring_member_of_array (string, _rl_possible_meta_prefixes)) key = META (key); /* Temporary. Handle old-style keyname with macro-binding. */ if (*funname == '\'' || *funname == '"') { char useq[2]; int fl = strlen (funname); useq[0] = key; useq[1] = '\0'; if (fl && funname[fl - 1] == *funname) funname[fl - 1] = '\0'; rl_macro_bind (useq, &funname[1], _rl_keymap); }#if defined (PREFIX_META_HACK) /* Ugly, but working hack to keep prefix-meta around. */ else if (_rl_stricmp (funname, "prefix-meta") == 0) { char seq[2]; seq[0] = key; seq[1] = '\0'; rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap); }#endif /* PREFIX_META_HACK */ else rl_bind_key (key, rl_named_function (funname)); return 0;}/* Simple structure for boolean readline variables (i.e., those that can have one of two values; either "On" or 1 for truth, or "Off" or 0 for false. */#define V_SPECIAL 0x1static struct { const char *name; int *value; int flags;} boolean_varlist [] = { { "blink-matching-paren", &rl_blink_matching_paren, V_SPECIAL }, { "byte-oriented", &rl_byte_oriented, 0 }, { "completion-ignore-case", &_rl_completion_case_fold, 0 }, { "convert-meta", &_rl_convert_meta_chars_to_ascii, 0 }, { "disable-completion", &rl_inhibit_completion, 0 }, { "enable-keypad", &_rl_enable_keypad, 0 }, { "expand-tilde", &rl_complete_with_tilde_expansion, 0 }, { "history-preserve-point", &_rl_history_preserve_point, 0 }, { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode, 0 }, { "input-meta", &_rl_meta_flag, 0 }, { "mark-directories", &_rl_complete_mark_directories, 0 }, { "mark-modified-lines", &_rl_mark_modified_lines, 0 }, { "mark-symlinked-directories", &_rl_complete_mark_symlink_dirs, 0 }, { "match-hidden-files", &_rl_match_hidden_files, 0 }, { "meta-flag", &_rl_meta_flag, 0 }, { "output-meta", &_rl_output_meta_chars, 0 }, { "page-completions", &_rl_page_completions, 0 }, { "prefer-visible-bell", &_rl_prefer_visible_bell, V_SPECIAL }, { "print-completions-horizontally", &_rl_print_completions_horizontally, 0 }, { "show-all-if-ambiguous", &_rl_complete_show_all, 0 }, { "show-all-if-unmodified", &_rl_complete_show_unmodified, 0 },#if defined (VISIBLE_STATS) { "visible-stats", &rl_visible_stats, 0 },#endif /* VISIBLE_STATS */ { (char *)NULL, (int *)NULL }};static intfind_boolean_var (name) const char *name;{ register int i; for (i = 0; boolean_varlist[i].name; i++) if (_rl_stricmp (name, boolean_varlist[i].name) == 0) return i; return -1;}/* Hooks for handling special boolean variables, where a function needs to be called or another variable needs to be changed when they're changed. */static voidhack_special_boolean_var (i) int i;{ const char *name; name = boolean_varlist[i].name; if (_rl_stricmp (name, "blink-matching-paren") == 0) _rl_enable_paren_matching (rl_blink_matching_paren); else if (_rl_stricmp (name, "prefer-visible-bell") == 0) { if (_rl_prefer_visible_bell) _rl_bell_preference = VISIBLE_BELL; else _rl_bell_preference = AUDIBLE_BELL; }}typedef int _rl_sv_func_t PARAMS((const char *));/* These *must* correspond to the array indices for the appropriate string variable. (Though they're not used right now.) */#define V_BELLSTYLE 0#define V_COMBEGIN 1#define V_EDITMODE 2#define V_ISRCHTERM 3#define V_KEYMAP 4#define V_STRING 1#define V_INT 2/* Forward declarations */static int sv_bell_style PARAMS((const char *));static int sv_combegin PARAMS((const char *));static int sv_compquery PARAMS((const char *));static int sv_editmode PARAMS((const char *));static int sv_isrchterm PARAMS((const char *));static int sv_keymap PARAMS((const char *));static struct { const char *name; int flags; _rl_sv_func_t *set_func;} string_varlist[] = { { "bell-style", V_STRING, sv_bell_style }, { "comment-begin", V_STRING, sv_combegin }, { "completion-query-items", V_INT, sv_compquery }, { "editing-mode", V_STRING, sv_editmode }, { "isearch-terminators", V_STRING, sv_isrchterm }, { "keymap", V_STRING, sv_keymap }, { (char *)NULL, 0 }};static intfind_string_var (name) const char *name;{ register int i; for (i = 0; string_varlist[i].name; i++) if (_rl_stricmp (name, string_varlist[i].name) == 0) return i; return -1;}/* A boolean value that can appear in a `set variable' command is true if the value is null or empty, `on' (case-insenstive), or "1". Any other values result in 0 (false). */static intbool_to_int (value) char *value;{ return (value == 0 || *value == '\0' || (_rl_stricmp (value, "on") == 0) || (value[0] == '1' && value[1] == '\0'));}intrl_variable_bind (name, value) const char *name, *value;{ register int i; int v; /* Check for simple variables first. */ i = find_boolean_var (name); if (i >= 0) { *boolean_varlist[i].value = bool_to_int (value); if (boolean_varlist[i].flags & V_SPECIAL) hack_special_boolean_var (i); return 0; } i = find_string_var (name); /* For the time being, unknown variable names or string names without a handler function are simply ignored. */ if (i < 0 || string_varlist[i].set_func == 0) return 0; v = (*string_varlist[i].set_func) (value); return v;}static intsv_editmode (value) const char *value;{ if (_rl_strnicmp (value, "vi", 2) == 0) {#if defined (VI_MODE) _rl_keymap = vi_insertion_keymap; rl_editing_mode = vi_mode;#endif /* VI_MODE */ return 0; } else if (_rl_strnicmp (value, "emacs", 5) == 0) { _rl_keymap = emacs_standard_keymap; rl_editing_mode = emacs_mode; return 0; } return 1;}static intsv_combegin (value) const char *value;{ if (value && *value) { FREE (_rl_comment_begin); _rl_comment_begin = savestring (value); return 0; } return 1;}static intsv_compquery (value) const char *value;{ int nval = 100; if (value && *value) { nval = atoi (value); if (nval < 0) nval = 0; } rl_completion_query_items = nval; return 0;}static intsv_keymap (value) const char *value;{ Keymap kmap; kmap = rl_get_keymap_by_name (value); if (kmap) { rl_set_keymap (kmap); return 0; } return 1;}static intsv_bell_style (value) const char *value;{ if (value == 0 || *value == '\0') _rl_bell_preference = AUDIBLE_BELL; else if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0) _rl_bell_preference = NO_BELL; else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0) _rl_bell_preference = AUDIBLE_BELL; else if (_rl_stricmp (value, "visible") == 0) _rl_bell_preference = VISIBLE_BELL; else return 1; return 0;}static intsv_isrchterm (value) const char *value;{ int beg, end, delim; char *v; if (value == 0) return 1; /* Isolate the value and translate it into a character string. */ v = savestring (value); FREE (_rl_isearch_terminators); if (v[0] == '"' || v[0] == '\'') { delim = v[0]; for (beg = end = 1; v[end] && v[end] != delim; end++) ; } else { for (beg = end = 0; whitespace (v[end]) == 0; end++) ; } v[end] = '\0'; /* The value starts at v + beg. Translate it into a character string. */ _rl_isearch_terminators = (char *)xmalloc (2 * strlen (v) + 1); rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end); _rl_isearch_terminators[end] = '\0'; free (v); return 0;} /* Return the character which matches NAME. For example, `Space' returns ' '. */typedef struct { const char *name; int value;} assoc_list;static assoc_list name_key_alist[] = { { "DEL", 0x7f }, { "ESC", '\033' }, { "Escape", '\033' }, { "LFD", '\n' }, { "Newline", '\n' }, { "RET", '\r' }, { "Return", '\r' }, { "Rubout", 0x7f }, { "SPC", ' ' }, { "Space", ' ' }, { "Tab", 0x09 }, { (char *)0x0, 0 }};static intglean_key_from_name (name) char *name;{ register int i; for (i = 0; name_key_alist[i].name; i++) if (_rl_stricmp (name, name_key_alist[i].name) == 0) return (name_key_alist[i].value); return (*(unsigned char *)name); /* XXX was return (*name) */}/* Auxiliary functions to manage keymaps. */static struct { const char *name; Keymap map;} keymap_names[] = { { "emacs", emacs_standard_keymap }, { "emacs-standard", emacs_standard_keymap }, { "emacs-meta", emacs_meta_keymap }, { "emacs-ctlx", emacs_ctlx_keymap },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -