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