xref: /openbsd-src/usr.bin/mg/funmap.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: funmap.c,v 1.47 2013/05/31 18:03:44 lum Exp $	*/
2 
3 /* This file is in the public domain */
4 
5 #include "def.h"
6 #include "kbd.h"
7 #include "funmap.h"
8 
9 /*
10  * If the function is NULL, it must be listed with the
11  * same name in the map_table.
12  */
13 
14 struct funmap {
15 	PF		 fn_funct;
16 	const		 char *fn_name;
17 	struct funmap	*fn_next;
18 };
19 
20 static struct funmap *funs;
21 
22 static struct funmap functnames[] = {
23 	{apropos_command, "apropos",},
24 	{toggleaudiblebell, "audible-bell",},
25 	{auto_execute, "auto-execute",},
26 	{fillmode, "auto-fill-mode",},
27 	{indentmode, "auto-indent-mode",},
28 	{backtoindent, "back-to-indentation",},
29 	{backuptohomedir, "backup-to-home-directory",},
30 	{backchar, "backward-char",},
31 	{delbword, "backward-kill-word",},
32 	{gotobop, "backward-paragraph",},
33 	{backword, "backward-word",},
34 	{gotobob, "beginning-of-buffer",},
35 	{gotobol, "beginning-of-line",},
36 	{showmatch, "blink-and-insert",},
37 	{bsmap, "bsmap-mode",},
38 	{NULL, "c-x 4 prefix",},
39 	{NULL, "c-x prefix",},
40 	{executemacro, "call-last-kbd-macro",},
41 	{capword, "capitalize-word",},
42 	{changedir, "cd",},
43 	{clearmark, "clear-mark",},
44 	{colnotoggle, "column-number-mode",},
45 	{copyregion, "copy-region-as-kill",},
46 #ifdef	REGEX
47 	{cntmatchlines, "count-matches",},
48 	{cntnonmatchlines, "count-non-matches",},
49 #endif /* REGEX */
50 	{cscreatelist, "cscope-create-list-of-files-to-index",},
51 	{csfuncalled, "cscope-find-called-functions",},
52 	{csegrep, "cscope-find-egrep-pattern",},
53 	{csfindinc, "cscope-find-files-including-file",},
54 	{cscallerfuncs, "cscope-find-functions-calling-this-function",},
55 	{csdefinition, "cscope-find-global-definition",},
56 	{csfindfile, "cscope-find-this-file",},
57 	{cssymbol, "cscope-find-this-symbol",},
58 	{csfindtext, "cscope-find-this-text-string",},
59 	{csnextfile, "cscope-next-file",},
60 	{csnextmatch, "cscope-next-symbol",},
61 	{csprevfile, "cscope-prev-file",},
62 	{csprevmatch, "cscope-prev-symbol",},
63 	{redefine_key, "define-key",},
64 	{backdel, "delete-backward-char",},
65 	{deblank, "delete-blank-lines",},
66 	{forwdel, "delete-char",},
67 	{delwhite, "delete-horizontal-space",},
68 	{delleadwhite, "delete-leading-space",},
69 #ifdef	REGEX
70 	{delmatchlines, "delete-matching-lines",},
71 	{delnonmatchlines, "delete-non-matching-lines",},
72 #endif /* REGEX */
73 	{onlywind, "delete-other-windows",},
74 	{deltrailwhite, "delete-trailing-space",},
75 	{delwind, "delete-window",},
76 	{wallchart, "describe-bindings",},
77 	{desckey, "describe-key-briefly",},
78 	{diffbuffer, "diff-buffer-with-file",},
79 	{digit_argument, "digit-argument",},
80 	{lowerregion, "downcase-region",},
81 	{lowerword, "downcase-word",},
82 	{showversion, "emacs-version",},
83 	{finishmacro, "end-kbd-macro",},
84 	{gotoeob, "end-of-buffer",},
85 	{gotoeol, "end-of-line",},
86 	{enlargewind, "enlarge-window",},
87 	{NULL, "esc prefix",},
88 	{evalbuffer, "eval-current-buffer",},
89 	{evalexpr, "eval-expression",},
90 	{swapmark, "exchange-point-and-mark",},
91 	{extend, "execute-extended-command",},
92 	{fillpara, "fill-paragraph",},
93 	{filevisitalt, "find-alternate-file",},
94 	{filevisit, "find-file",},
95 	{poptofile, "find-file-other-window",},
96 	{filevisitro, "find-file-read-only",},
97 	{findtag, "find-tag",},
98 	{forwchar, "forward-char",},
99 	{gotoeop, "forward-paragraph",},
100 	{forwword, "forward-word",},
101 	{bindtokey, "global-set-key",},
102 	{unbindtokey, "global-unset-key",},
103 	{globalwdtoggle, "global-wd-mode",},
104 	{gotoline, "goto-line",},
105 	{help_help, "help-help",},
106 	{indent, "indent-current-line",},
107 	{insert, "insert",},
108 	{bufferinsert, "insert-buffer",},
109 	{fileinsert, "insert-file",},
110 	{fillword, "insert-with-wrap",},
111 	{backisearch, "isearch-backward",},
112 	{forwisearch, "isearch-forward",},
113 	{joinline, "join-line",},
114 	{justone, "just-one-space",},
115 	{ctrlg, "keyboard-quit",},
116 	{killbuffer_cmd, "kill-buffer",},
117 	{killline, "kill-line",},
118 	{killpara, "kill-paragraph",},
119 	{killregion, "kill-region",},
120 	{delfword, "kill-word",},
121 	{toggleleavetmp, "leave-tmpdir-backups",},
122 	{linenotoggle, "line-number-mode",},
123 	{listbuffers, "list-buffers",},
124 	{evalfile, "load",},
125 	{localbind, "local-set-key",},
126 	{localunbind, "local-unset-key",},
127 	{makebkfile, "make-backup-files",},
128 	{makedir, "make-directory",},
129 	{markbuffer, "mark-whole-buffer",},
130 	{do_meta, "meta-key-mode",},	/* better name, anyone? */
131 	{negative_argument, "negative-argument",},
132 	{newline, "newline",},
133 	{lfindent, "newline-and-indent",},
134 	{forwline, "next-line",},
135 #ifdef NOTAB
136 	{notabmode, "no-tab-mode",},
137 #endif /* NOTAB */
138 	{notmodified, "not-modified",},
139 	{openline, "open-line",},
140 	{nextwind, "other-window",},
141 	{overwrite_mode, "overwrite-mode",},
142 	{poptag, "pop-tag-mark",},
143 	{prefixregion, "prefix-region",},
144 	{backline, "previous-line",},
145 	{prevwind, "previous-window",},
146 	{spawncli, "push-shell",},
147 	{showcwdir, "pwd",},
148 	{queryrepl, "query-replace",},
149 #ifdef REGEX
150 	{re_queryrepl, "query-replace-regexp",},
151 #endif /* REGEX */
152 	{quote, "quoted-insert",},
153 #ifdef REGEX
154 	{re_searchagain, "re-search-again",},
155 	{re_backsearch, "re-search-backward",},
156 	{re_forwsearch, "re-search-forward",},
157 #endif /* REGEX */
158 	{reposition, "recenter",},
159 	{redraw, "redraw-display",},
160 #ifdef REGEX
161 	{replstr, "replace-string",},
162 #endif /* REGEX */
163 	{revertbuffer, "revert-buffer",},
164 	{filesave, "save-buffer",},
165 	{quit, "save-buffers-kill-emacs",},
166 	{savebuffers, "save-some-buffers",},
167 	{backpage, "scroll-down",},
168 	{back1page, "scroll-one-line-down",},
169 	{forw1page, "scroll-one-line-up",},
170 	{pagenext, "scroll-other-window",},
171 	{forwpage, "scroll-up",},
172 	{searchagain, "search-again",},
173 	{backsearch, "search-backward",},
174 	{forwsearch, "search-forward",},
175 	{selfinsert, "self-insert-command",},
176 #ifdef REGEX
177 	{setcasefold, "set-case-fold-search",},
178 #endif /* REGEX */
179 	{set_default_mode, "set-default-mode",},
180 	{setfillcol, "set-fill-column",},
181 	{setmark, "set-mark-command",},
182 	{setprefix, "set-prefix-string",},
183 	{shellcommand, "shell-command",},
184 	{piperegion, "shell-command-on-region",},
185 	{shrinkwind, "shrink-window",},
186 #ifdef NOTAB
187 	{space_to_tabstop, "space-to-tabstop",},
188 #endif /* NOTAB */
189 	{splitwind, "split-window-vertically",},
190 	{definemacro, "start-kbd-macro",},
191 	{spawncli, "suspend-emacs",},
192 	{usebuffer, "switch-to-buffer",},
193 	{poptobuffer, "switch-to-buffer-other-window",},
194 	{togglereadonly, "toggle-read-only" },
195 	{twiddle, "transpose-chars",},
196 	{undo, "undo",},
197 	{undo_add_boundary, "undo-boundary",},
198 	{undo_boundary_enable, "undo-boundary-toggle",},
199 	{undo_enable, "undo-enable",},
200 	{undo_dump, "undo-list",},
201 	{universal_argument, "universal-argument",},
202 	{upperregion, "upcase-region",},
203 	{upperword, "upcase-word",},
204 	{togglevisiblebell, "visible-bell",},
205 	{tagsvisit, "visit-tags-table",},
206 	{showcpos, "what-cursor-position",},
207 	{filewrite, "write-file",},
208 	{yank, "yank",},
209 	{NULL, NULL,}
210 };
211 
212 void
213 funmap_init(void)
214 {
215 	struct funmap *fn;
216 
217 	for (fn = functnames; fn->fn_name != NULL; fn++) {
218 		fn->fn_next = funs;
219 		funs = fn;
220 	}
221 }
222 
223 int
224 funmap_add(PF fun, const char *fname)
225 {
226 	struct funmap *fn;
227 
228 	if ((fn = malloc(sizeof(*fn))) == NULL)
229 		return (FALSE);
230 
231 	fn->fn_funct = fun;
232 	fn->fn_name = fname;
233 	fn->fn_next = funs;
234 
235 	funs = fn;
236 	return (TRUE);
237 }
238 
239 /*
240  * Translate from function name to function pointer.
241  */
242 PF
243 name_function(const char *fname)
244 {
245 	struct funmap *fn;
246 
247 	for (fn = funs; fn != NULL; fn = fn->fn_next) {
248 		if (strcmp(fn->fn_name, fname) == 0)
249 			return (fn->fn_funct);
250 	}
251 	return (NULL);
252 }
253 
254 const char *
255 function_name(PF fun)
256 {
257 	struct funmap *fn;
258 
259 	for (fn = funs; fn != NULL; fn = fn->fn_next) {
260 		if (fn->fn_funct == fun)
261 			return (fn->fn_name);
262 	}
263 	return (NULL);
264 }
265 
266 /*
267  * List possible function name completions.
268  */
269 struct list *
270 complete_function_list(const char *fname)
271 {
272 	struct funmap	*fn;
273 	struct list	*head, *el;
274 	int		 len;
275 
276 	len = strlen(fname);
277 	head = NULL;
278 	for (fn = funs; fn != NULL; fn = fn->fn_next) {
279 		if (memcmp(fname, fn->fn_name, len) == 0) {
280 			if ((el = malloc(sizeof(*el))) == NULL) {
281 				free_file_list(head);
282 				return (NULL);
283 			}
284 			el->l_name = strdup(fn->fn_name);
285 			el->l_next = head;
286 			head = el;
287 		}
288 	}
289 	return (head);
290 }
291