Annotation of src/usr.bin/mg/funmap.c, Revision 1.21
1.21 ! kjell 1: /* $OpenBSD: funmap.c,v 1.20 2005/11/20 04:17:25 kjell Exp $ */
1.3 art 2: /*
3: * Copyright (c) 2001 Artur Grabowski <art@openbsd.org>. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: *
14: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24: */
1.1 art 25: #include "def.h"
26: #include "kbd.h"
27: #include "funmap.h"
28:
29: /*
30: * If the function is NULL, it must be listed with the
31: * same name in the map_table.
32: */
33:
34: struct funmap {
1.6 vincent 35: PF fn_funct;
36: const char *fn_name;
1.1 art 37: struct funmap *fn_next;
38: };
39:
40: static struct funmap *funs;
41:
42: static struct funmap functnames[] = {
43: #ifndef NO_HELP
44: {apropos_command, "apropos",},
45: #endif /* !NO_HELP */
1.11 db 46: {auto_execute, "auto-execute", },
1.1 art 47: {fillmode, "auto-fill-mode",},
48: {indentmode, "auto-indent-mode",},
49: {backchar, "backward-char",},
50: {delbword, "backward-kill-word",},
51: {gotobop, "backward-paragraph",},
52: {backword, "backward-word",},
53: {gotobob, "beginning-of-buffer",},
54: {gotobol, "beginning-of-line",},
55: {blinkparen, "blink-matching-paren",},
56: {showmatch, "blink-matching-paren-hack",},
57: #ifdef BSMAP
58: {bsmap, "bsmap-mode",},
59: #endif /* BSMAP */
60: {NULL, "c-x 4 prefix",},
61: {NULL, "c-x prefix",},
62: #ifndef NO_MACRO
63: {executemacro, "call-last-kbd-macro",},
64: #endif /* !NO_MACRO */
65: {capword, "capitalize-word",},
66: {changedir, "cd",},
67: {copyregion, "copy-region-as-kill",},
68: #ifdef REGEX
69: {cntmatchlines, "count-matches",},
70: {cntnonmatchlines, "count-non-matches",},
71: #endif /* REGEX */
72: {define_key, "define-key",},
73: {backdel, "delete-backward-char",},
74: {deblank, "delete-blank-lines",},
75: {forwdel, "delete-char",},
76: {delwhite, "delete-horizontal-space",},
77: #ifdef REGEX
78: {delmatchlines, "delete-matching-lines",},
79: {delnonmatchlines, "delete-non-matching-lines",},
80: #endif /* REGEX */
81: {onlywind, "delete-other-windows",},
82: {delwind, "delete-window",},
83: #ifndef NO_HELP
84: {wallchart, "describe-bindings",},
85: {desckey, "describe-key-briefly",},
86: #endif /* !NO_HELP */
87: {digit_argument, "digit-argument",},
88: {lowerregion, "downcase-region",},
89: {lowerword, "downcase-word",},
90: {showversion, "emacs-version",},
91: #ifndef NO_MACRO
92: {finishmacro, "end-kbd-macro",},
93: #endif /* !NO_MACRO */
94: {gotoeob, "end-of-buffer",},
95: {gotoeol, "end-of-line",},
96: {enlargewind, "enlarge-window",},
97: {NULL, "esc prefix",},
98: #ifndef NO_STARTUP
99: {evalbuffer, "eval-current-buffer",},
100: {evalexpr, "eval-expression",},
101: #endif /* !NO_STARTUP */
102: {swapmark, "exchange-point-and-mark",},
103: {extend, "execute-extended-command",},
104: {fillpara, "fill-paragraph",},
105: {filevisit, "find-file",},
1.8 vincent 106: {filevisitro, "find-file-read-only",},
1.14 jason 107: {filevisitalt, "find-alternate-file",},
1.1 art 108: {poptofile, "find-file-other-window",},
109: {forwchar, "forward-char",},
110: {gotoeop, "forward-paragraph",},
111: {forwword, "forward-word",},
112: {bindtokey, "global-set-key",},
113: {unbindtokey, "global-unset-key",},
114: {gotoline, "goto-line",},
115: #ifndef NO_HELP
116: {help_help, "help-help",},
117: #endif /* !NO_HELP */
118: {insert, "insert",},
119: {bufferinsert, "insert-buffer",},
120: {fileinsert, "insert-file",},
121: {fillword, "insert-with-wrap",},
122: {backisearch, "isearch-backward",},
123: {forwisearch, "isearch-forward",},
124: {justone, "just-one-space",},
125: {ctrlg, "keyboard-quit",},
1.10 jfb 126: {killbuffer_cmd, "kill-buffer",},
1.1 art 127: {killline, "kill-line",},
128: {killpara, "kill-paragraph",},
129: {killregion, "kill-region",},
130: {delfword, "kill-word",},
131: {listbuffers, "list-buffers",},
132: #ifndef NO_STARTUP
133: {evalfile, "load",},
134: #endif /* !NO_STARTUP */
135: {localbind, "local-set-key",},
136: {localunbind, "local-unset-key",},
137: {makebkfile, "make-backup-files",},
138: {do_meta, "meta-key-mode",}, /* better name, anyone? */
139: {negative_argument, "negative-argument",},
140: {newline, "newline",},
141: {indent, "newline-and-indent",},
142: {forwline, "next-line",},
143: #ifdef NOTAB
144: {notabmode, "no-tab-mode",},
145: #endif /* NOTAB */
146: {notmodified, "not-modified",},
147: {openline, "open-line",},
148: {nextwind, "other-window",},
149: {overwrite, "overwrite-mode",},
150: #ifdef PREFIXREGION
151: {prefixregion, "prefix-region",},
152: #endif /* PREFIXREGION */
153: {backline, "previous-line",},
154: {prevwind, "previous-window",},
155: {spawncli, "push-shell",},
156: {showcwdir, "pwd",},
157: {queryrepl, "query-replace",},
1.15 deraadt 158: #ifdef REGEX
1.13 cloder 159: {replstr, "replace-string",},
1.1 art 160: {re_queryrepl, "query-replace-regexp",},
161: #endif /* REGEX */
162: {quote, "quoted-insert",},
163: #ifdef REGEX
164: {re_searchagain, "re-search-again",},
165: {re_backsearch, "re-search-backward",},
166: {re_forwsearch, "re-search-forward",},
167: #endif /* REGEX */
168: {reposition, "recenter",},
169: {refresh, "redraw-display",},
170: {filesave, "save-buffer",},
171: {quit, "save-buffers-kill-emacs",},
172: {savebuffers, "save-some-buffers",},
173: {backpage, "scroll-down",},
174: {back1page, "scroll-one-line-down",},
175: {forw1page, "scroll-one-line-up",},
176: {pagenext, "scroll-other-window",},
177: {forwpage, "scroll-up",},
178: {searchagain, "search-again",},
179: {backsearch, "search-backward",},
180: {forwsearch, "search-forward",},
181: {selfinsert, "self-insert-command",},
182: #ifdef REGEX
183: {setcasefold, "set-case-fold-search",},
184: #endif /* REGEX */
185: {set_default_mode, "set-default-mode",},
186: {setfillcol, "set-fill-column",},
187: {setmark, "set-mark-command",},
188: #ifdef PREFIXREGION
189: {setprefix, "set-prefix-string",},
190: #endif /* PREFIXREGION */
191: {shrinkwind, "shrink-window",},
192: #ifdef NOTAB
193: {space_to_tabstop, "space-to-tabstop",},
194: #endif /* NOTAB */
195: {splitwind, "split-window-vertically",},
196: #ifndef NO_MACRO
197: {definemacro, "start-kbd-macro",},
198: #endif /* !NO_MACRO */
199: {spawncli, "suspend-emacs",},
200: {usebuffer, "switch-to-buffer",},
201: {poptobuffer, "switch-to-buffer-other-window",},
1.8 vincent 202: {togglereadonly, "toggle-read-only" },
1.1 art 203: {twiddle, "transpose-chars",},
1.11 db 204: {undo, "undo", },
205: {undo_dump, "undo-list", },
1.1 art 206: {universal_argument, "universal-argument",},
207: {upperregion, "upcase-region",},
208: {upperword, "upcase-word",},
209: {showcpos, "what-cursor-position",},
210: {filewrite, "write-file",},
211: {yank, "yank",},
1.11 db 212: {NULL, NULL,}
1.1 art 213: };
214:
215: void
216: funmap_init(void)
217: {
218: struct funmap *fn;
219:
220: for (fn = functnames; fn->fn_name != NULL; fn++) {
221: fn->fn_next = funs;
222: funs = fn;
223: }
224: }
225:
226: int
1.6 vincent 227: funmap_add(PF fun, const char *fname)
1.1 art 228: {
229: struct funmap *fn;
230:
231: if ((fn = malloc(sizeof(*fn))) == NULL)
1.11 db 232: return (FALSE);
1.1 art 233:
234: fn->fn_funct = fun;
235: fn->fn_name = fname;
236: fn->fn_next = funs;
237:
238: funs = fn;
1.11 db 239: return (TRUE);
1.1 art 240: }
241:
242: /*
243: * Translate from function name to function pointer.
244: */
245: PF
1.6 vincent 246: name_function(const char *fname)
1.1 art 247: {
248: struct funmap *fn;
249:
250: for (fn = funs; fn != NULL; fn = fn->fn_next) {
251: if (strcmp(fn->fn_name, fname) == 0)
1.11 db 252: return (fn->fn_funct);
1.1 art 253: }
1.11 db 254: return (NULL);
1.1 art 255: }
256:
1.6 vincent 257: const char *
1.1 art 258: function_name(PF fun)
259: {
260: struct funmap *fn;
261:
262: for (fn = funs; fn != NULL; fn = fn->fn_next) {
263: if (fn->fn_funct == fun)
1.11 db 264: return (fn->fn_name);
1.1 art 265: }
1.11 db 266: return (NULL);
1.1 art 267: }
268:
269: /*
1.11 db 270: * List possible function name completions.
1.1 art 271: */
1.17 deraadt 272: struct list *
1.12 otto 273: complete_function_list(const char *fname)
1.1 art 274: {
1.11 db 275: struct funmap *fn;
1.17 deraadt 276: struct list *head, *el;
1.11 db 277: int len;
1.1 art 278:
279: len = strlen(fname);
280: head = NULL;
281: for (fn = funs; fn != NULL; fn = fn->fn_next) {
282: if (memcmp(fname, fn->fn_name, len) == 0) {
283: if ((el = malloc(sizeof(*el))) == NULL) {
284: free_file_list(head);
1.11 db 285: return (NULL);
1.1 art 286: }
287: el->l_name = fn->fn_name;
288: el->l_next = head;
289: head = el;
290: }
291: }
1.11 db 292: return (head);
1.1 art 293: }