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