?? bind.c
字號:
/* bind.c -- key binding and startup file support for the readline library. *//* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc. This file is part of the GNU Readline Library, a library for reading lines of text with interactive input and history editing. The GNU Readline Library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. The GNU Readline Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The GNU General Public License is often shipped with GNU software, and is generally kept in a file called COPYING or LICENSE. If you do not have a copy of the license, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */#define READLINE_LIBRARY#include <stdio.h>#include <sys/types.h>#include <fcntl.h>#if !defined (NO_SYS_FILE)# include <sys/file.h>#endif /* !NO_SYS_FILE */#include <signal.h>#if defined (HAVE_UNISTD_H)# include <unistd.h>#endif /* HAVE_UNISTD_H */#if defined (HAVE_STDLIB_H)# include <stdlib.h>#else# include "ansi_stdlib.h"#endif /* HAVE_STDLIB_H */#include <errno.h>/* Not all systems declare ERRNO in errno.h... and some systems #define it! */#if !defined (errno)extern int errno;#endif /* !errno */#include "posixstat.h"/* System-specific feature definitions and include files. */#include "rldefs.h"/* Some standard library routines. */#include "readline.h"#include "history.h"#if !defined (strchr) && !defined (__STDC__)extern char *strchr (), *strrchr ();#endif /* !strchr && !__STDC__ */extern int _rl_horizontal_scroll_mode;extern int _rl_mark_modified_lines;extern int _rl_bell_preference;extern int _rl_meta_flag;extern int _rl_convert_meta_chars_to_ascii;extern int _rl_output_meta_chars;extern int _rl_complete_show_all;#if defined (PAREN_MATCHING)extern int rl_blink_matching_paren;#endif /* PAREN_MATCHING */#if defined (VISIBLE_STATS)extern int rl_visible_stats;#endif /* VISIBLE_STATS */extern int rl_complete_with_tilde_expansion;extern int rl_completion_query_items;#if defined (VI_MODE)extern char *rl_vi_comment_begin;#endifextern int rl_explicit_arg;extern int rl_editing_mode;extern unsigned short _rl_parsing_conditionalized_out;extern Keymap _rl_keymap;extern char *possible_control_prefixes[], *possible_meta_prefixes[];extern char **rl_funmap_names ();/* Forward declarations */void rl_set_keymap_from_edit_mode ();static int glean_key_from_name ();#if defined (HAVE_STRCASECMP)#define stricmp strcasecmp#define strnicmp strncasecmp#elsestatic int stricmp (), strnicmp ();#endif#if defined (STATIC_MALLOC)static char *xmalloc (), *xrealloc ();#elseextern char *xmalloc (), *xrealloc ();#endif /* STATIC_MALLOC *//* **************************************************************** *//* *//* Binding keys *//* *//* **************************************************************** *//* rl_add_defun (char *name, Function *function, int key) Add NAME to the list of named functions. Make FUNCTION be the function that gets called. If KEY is not -1, then bind it. */rl_add_defun (name, function, key) char *name; Function *function; int key;{ if (key != -1) rl_bind_key (key, function); rl_add_funmap_entry (name, function); return 0;}/* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */intrl_bind_key (key, function) int key; Function *function;{ if (key < 0) return (key); if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii) { if (_rl_keymap[ESC].type == ISKMAP) { Keymap escmap; escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC); key = UNMETA (key); escmap[key].type = ISFUNC; escmap[key].function = function; return (0); } return (key); } _rl_keymap[key].type = ISFUNC; _rl_keymap[key].function = function; return (0);}/* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid KEY. */intrl_bind_key_in_map (key, function, map) int key; Function *function; Keymap map;{ int result; Keymap oldmap = _rl_keymap; _rl_keymap = map; result = rl_bind_key (key, function); _rl_keymap = oldmap; return (result);}/* Make KEY do nothing in the currently selected keymap. Returns non-zero in case of error. */intrl_unbind_key (key) int key;{ return (rl_bind_key (key, (Function *)NULL));}/* Make KEY do nothing in MAP. Returns non-zero in case of error. */intrl_unbind_key_in_map (key, map) int key; Keymap map;{ return (rl_bind_key_in_map (key, (Function *)NULL, map));}/* Bind the key sequence represented by the string KEYSEQ to FUNCTION. This makes new keymaps as necessary. The initial place to do bindings is in MAP. */rl_set_key (keyseq, function, map) char *keyseq; Function *function; Keymap map;{ return (rl_generic_bind (ISFUNC, keyseq, function, map));}/* Bind the key sequence represented by the string KEYSEQ to the string of characters MACRO. This makes new keymaps as necessary. The initial place to do bindings is in MAP. */rl_macro_bind (keyseq, macro, map) char *keyseq, *macro; Keymap map;{ char *macro_keys; int macro_keys_len; macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1); if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len)) { free (macro_keys); return -1; } rl_generic_bind (ISMACR, keyseq, macro_keys, map); return 0;}/* Bind the key sequence represented by the string KEYSEQ to the arbitrary pointer DATA. TYPE says what kind of data is pointed to by DATA, right now this can be a function (ISFUNC), a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps as necessary. The initial place to do bindings is in MAP. */rl_generic_bind (type, keyseq, data, map) int type; char *keyseq, *data; Keymap map;{ char *keys; int keys_len; register int i; /* If no keys to bind to, exit right away. */ if (!keyseq || !*keyseq) { if (type == ISMACR) free (data); return -1; } keys = xmalloc (1 + (2 * strlen (keyseq))); /* Translate the ASCII representation of KEYSEQ into an array of characters. Stuff the characters into KEYS, and the length of KEYS into KEYS_LEN. */ if (rl_translate_keyseq (keyseq, keys, &keys_len)) { free (keys); return -1; } /* Bind keys, making new keymaps as necessary. */ for (i = 0; i < keys_len; i++) { int ic = (int) ((unsigned char)keys[i]); if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic)) { ic = UNMETA (ic); if (map[ESC].type == ISKMAP) map = FUNCTION_TO_KEYMAP (map, ESC); } if ((i + 1) < keys_len) { if (map[ic].type != ISKMAP) { if (map[ic].type == ISMACR) free ((char *)map[ic].function); map[ic].type = ISKMAP; map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap()); } map = FUNCTION_TO_KEYMAP (map, ic); } else { if (map[ic].type == ISMACR) free ((char *)map[ic].function); map[ic].function = KEYMAP_TO_FUNCTION (data); map[ic].type = type; } } free (keys); return 0;}/* Translate the ASCII representation of SEQ, stuffing the values into ARRAY, an array of characters. LEN gets the final length of ARRAY. Return non-zero if there was an error parsing SEQ. */rl_translate_keyseq (seq, array, len) char *seq, *array; int *len;{ register int i, c, l = 0; for (i = 0; c = seq[i]; i++) { if (c == '\\') { c = seq[++i]; if (!c) break; if (((c == 'C' || c == 'M') && seq[i + 1] == '-') || (c == 'e')) { /* Handle special case of backwards define. */ if (strncmp (&seq[i], "C-\\M-", 5) == 0) { array[l++] = ESC; i += 5; array[l++] = CTRL (to_upper (seq[i])); if (!seq[i]) i--; continue; } switch (c) { case 'M': i++; array[l++] = ESC; break; case 'C': i += 2; /* Special hack for C-?... */ if (seq[i] == '?') array[l++] = RUBOUT; else array[l++] = CTRL (to_upper (seq[i])); break; case 'e': array[l++] = ESC; } continue; } } array[l++] = c; } *len = l; array[l] = '\0'; return (0);}/* Return a pointer to the function that STRING represents. If STRING doesn't have a matching function, then a NULL pointer is returned. */Function *rl_named_function (string) char *string;{ register int i; rl_initialize_funmap (); for (i = 0; funmap[i]; i++) if (stricmp (funmap[i]->name, string) == 0) return (funmap[i]->function); return ((Function *)NULL);}/* Return the function (or macro) definition which would be invoked via KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is used. TYPE, if non-NULL, is a pointer to an int which will receive the type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap), or ISMACR (macro). */Function *rl_function_of_keyseq (keyseq, map, type) char *keyseq; Keymap map; int *type;{ register int i; if (!map) map = _rl_keymap; for (i = 0; keyseq && keyseq[i]; i++) { int ic = keyseq[i]; if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii) { if (map[ESC].type != ISKMAP) { if (type) *type = map[ESC].type; return (map[ESC].function); } else { map = FUNCTION_TO_KEYMAP (map, ESC); ic = UNMETA (ic); } } if (map[ic].type == ISKMAP) { /* If this is the last key in the key sequence, return the map. */ if (!keyseq[i + 1]) { if (type) *type = ISKMAP; return (map[ic].function); } else map = FUNCTION_TO_KEYMAP (map, ic); } else { if (type) *type = map[ic].type; return (map[ic].function); } } return ((Function *) NULL);}/* The last key bindings file read. */static char *last_readline_init_file = (char *)NULL;/* Re-read the current keybindings file. */rl_re_read_init_file (count, ignore) int count, ignore;{ int r; r = rl_read_init_file ((char *)NULL); rl_set_keymap_from_edit_mode (); return r;}/* Do key bindings from a file. If FILENAME is NULL it defaults to the first non-null filename from this list: 1. the filename used for the previous call 2. the value of the shell variable `INPUTRC' 3. ~/.inputrc If the file existed and could be opened and read, 0 is returned, otherwise errno is returned. */intrl_read_init_file (filename) char *filename;{ register int i; char *buffer, *openname, *line, *end; struct stat finfo; int file; /* Default the filename. */ if (!filename) { filename = last_readline_init_file; if (!filename) filename = getenv ("INPUTRC"); if (!filename) filename = DEFAULT_INPUTRC; } if (!*filename) filename = DEFAULT_INPUTRC; openname = tilde_expand (filename); if ((stat (openname, &finfo) < 0) || (file = open (openname, O_RDONLY, 0666)) < 0) { free (openname); return (errno); } else free (openname); if (filename != last_readline_init_file) { if (last_readline_init_file) free (last_readline_init_file); last_readline_init_file = savestring (filename); } /* Read the file into BUFFER. */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -