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