]> git.lyx.org Git - lyx.git/blob - src/LyXAction.C
clean code to export between different flavours, output different code for sgml to...
[lyx.git] / src / LyXAction.C
1 /**
2  * \file LyXAction.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author John Levon
9  * \author André Pönitz
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "LyXAction.h"
17
18 #include "debug.h"
19 #include "funcrequest.h"
20
21 #include "support/lstrings.h"
22
23 #include <boost/assert.hpp>
24
25 using lyx::support::split;
26 using lyx::support::trim;
27
28 using std::endl;
29 using std::string;
30 using std::ostream;
31
32
33 /*
34      NAMING RULES FOR USER-COMMANDS
35      Here's the set of rules to apply when a new command name is introduced:
36
37      1) Use the object.event order. That is, use `word-forward'
38         instead of `forward-word'.
39      2) Don't introduce an alias for an already named object. Same for events.
40      3) Forward movement or focus is called `forward' (not `right').
41      4) Backward movement or focus is called `backward' (not `left').
42      5) Upward movement of focus is called `up'.
43      6) Downward movement is called `down'.
44      7) The begin of an object is called `begin' (not `start').
45      8) The end of an object is called `end'.
46
47      (May 19 1996, 12:04, RvdK)
48 */
49
50 LyXAction lyxaction;
51
52
53 void LyXAction::newFunc(kb_action action, string const & name,
54                         unsigned int attrib)
55 {
56         lyx_func_map[name] = action;
57         func_info tmpinfo;
58         tmpinfo.name = name;
59         tmpinfo.attrib = attrib;
60         lyx_info_map[action] = tmpinfo;
61 }
62
63
64 void LyXAction::init()
65 {
66         // This function was changed to use the array below in initalization
67         // instead of calling newFunc numerous times because of compilation
68         // times. Since the array is not static we get back the memory it
69         // occupies after the init is completed. It compiles several
70         // magnitudes faster.
71
72         static bool init;
73         if (init) return;
74
75         struct ev_item {
76                 kb_action action;
77                 char const * name;
78                 unsigned int attrib;
79         };
80
81         ev_item const items[] = {
82                 { LFUN_ACUTE, "accent-acute", Noop },
83                 { LFUN_BREVE, "accent-breve", Noop },
84                 { LFUN_CARON, "accent-caron", Noop },
85                 { LFUN_CEDILLA, "accent-cedilla", Noop },
86                 { LFUN_CIRCLE, "accent-circle", Noop },
87                 { LFUN_CIRCUMFLEX, "accent-circumflex", Noop },
88                 { LFUN_DOT, "accent-dot", Noop },
89                 { LFUN_GRAVE, "accent-grave", Noop },
90                 { LFUN_HUNG_UMLAUT, "accent-hungarian-umlaut", Noop },
91                 { LFUN_MACRON, "accent-macron", Noop },
92                 { LFUN_OGONEK, "accent-ogonek", Noop },
93                 { LFUN_SPECIAL_CARON, "accent-special-caron", Noop },
94                 { LFUN_TIE, "accent-tie", Noop },
95                 { LFUN_TILDE, "accent-tilde", Noop },
96                 { LFUN_UMLAUT, "accent-umlaut", Noop },
97                 { LFUN_UNDERBAR, "accent-underbar", Noop },
98                 { LFUN_UNDERDOT, "accent-underdot", Noop },
99                 { LFUN_APPENDIX, "appendix", Noop },
100                 { LFUN_LEFTSEL, "backward-select", ReadOnly },
101                 { LFUN_BOOKMARK_GOTO, "bookmark-goto", ReadOnly },
102                 { LFUN_BOOKMARK_SAVE, "bookmark-save", ReadOnly },
103                 { LFUN_BREAKLINE, "break-line", Noop },
104                 { LFUN_BREAKPARAGRAPH, "break-paragraph", Noop },
105                 { LFUN_BREAKPARAGRAPHKEEPLAYOUT, "break-paragraph-keep-layout", Noop },
106                 { LFUN_BREAKPARAGRAPH_SKIP, "break-paragraph-skip", Noop },
107                 { LFUN_BUILDPROG, "build-program", ReadOnly },
108                 { LFUN_AUTOSAVE, "buffer-auto-save", Noop },
109                 { LFUN_BEGINNINGBUF, "buffer-begin", ReadOnly },
110                 { LFUN_BEGINNINGBUFSEL, "buffer-begin-select", ReadOnly },
111                 { LFUN_CHILDOPEN, "buffer-child-open", ReadOnly },
112                 { LFUN_RUNCHKTEX, "buffer-chktex", ReadOnly },
113                 { LFUN_CLOSEBUFFER, "buffer-close", ReadOnly },
114                 { LFUN_ENDBUF, "buffer-end", ReadOnly },
115                 { LFUN_ENDBUFSEL, "buffer-end-select", ReadOnly },
116                 { LFUN_EXPORT, "buffer-export", ReadOnly },
117                 { LFUN_EXPORT_CUSTOM, "buffer-export-custom", ReadOnly },
118                 { LFUN_PRINT, "buffer-print", ReadOnly },
119                 { LFUN_IMPORT, "buffer-import", NoBuffer },
120                 { LFUN_MENUNEW, "buffer-new", NoBuffer },
121                 { LFUN_MENUNEWTMPLT,"buffer-new-template", NoBuffer },
122                 { LFUN_MENURELOAD, "buffer-reload", ReadOnly },
123                 { LFUN_SWITCHBUFFER, "buffer-switch", ReadOnly },
124                 { LFUN_READ_ONLY_TOGGLE, "buffer-toggle-read-only", ReadOnly },
125                 { LFUN_UPDATE, "buffer-update", ReadOnly },
126                 { LFUN_PREVIEW, "buffer-view", ReadOnly },
127                 { LFUN_MENUWRITE, "buffer-write", ReadOnly },
128                 { LFUN_WRITEAS, "buffer-write-as", ReadOnly },
129                 { LFUN_CANCEL, "cancel", NoBuffer },
130                 { LFUN_INSET_CAPTION, "caption-insert", Noop },
131                 { LFUN_LEFT, "char-backward", ReadOnly },
132                 { LFUN_RIGHT, "char-forward", ReadOnly },
133                 { LFUN_EXEC_COMMAND, "command-execute", NoBuffer },
134                 { LFUN_PREFIX, "command-prefix", NoBuffer },
135                 { LFUN_SEQUENCE, "command-sequence", NoBuffer },
136                 { LFUN_COPY, "copy", ReadOnly },
137                 { LFUN_CUT, "cut", Noop },
138                 { LFUN_DATE_INSERT, "date-insert", Noop },
139                 { LFUN_BACKSPACE, "delete-backward", Noop },
140                 { LFUN_BACKSPACE_SKIP, "delete-backward-skip", Noop },
141                 { LFUN_DELETE, "delete-forward", Noop },
142                 { LFUN_DELETE_SKIP, "delete-forward-skip", Noop },
143                 { LFUN_DEPTH_MIN, "depth-decrement", Noop },
144                 { LFUN_DEPTH_PLUS, "depth-increment", Noop },
145                 { LFUN_LDOTS, "dots-insert", Noop },
146                 { LFUN_DOWN, "down", ReadOnly },
147                 { LFUN_DOWNSEL, "down-select", ReadOnly },
148                 { LFUN_DROP_LAYOUTS_CHOICE, "drop-layouts-choice", ReadOnly },
149                 { LFUN_END_OF_SENTENCE, "end-of-sentence-period-insert", Noop },
150                 { LFUN_ENVIRONMENT_INSERT, "environment-insert", Noop },
151                 { LFUN_GOTOERROR, "error-next", ReadOnly },
152                 { LFUN_INSET_ERT, "ert-insert", Noop },
153                 { LFUN_FILE_INSERT, "file-insert", Noop },
154                 { LFUN_FILE_INSERT_ASCII, "file-insert-ascii", Noop },
155                 { LFUN_FILE_INSERT_ASCII_PARA, "file-insert-ascii-para", Noop },
156                 { LFUN_FILE_NEW, "file-new", NoBuffer },
157                 { LFUN_FILE_OPEN, "file-open", NoBuffer },
158                 { LFUN_INSET_FLOAT, "float-insert", Noop },
159                 { LFUN_INSET_WIDE_FLOAT, "float-wide-insert", Noop },
160                 { LFUN_INSET_WRAP, "wrap-insert", Noop },
161                 { LFUN_BOLD, "font-bold", Noop },
162                 { LFUN_CODE, "font-code", Noop },
163                 { LFUN_DEFAULT, "font-default", Noop },
164                 { LFUN_EMPH, "font-emph", Noop },
165                 { LFUN_FREEFONT_APPLY, "font-free-apply", Noop },
166                 { LFUN_FREEFONT_UPDATE, "font-free-update", Noop },
167                 { LFUN_NOUN, "font-noun", Noop },
168                 { LFUN_ROMAN, "font-roman", Noop },
169                 { LFUN_SANS, "font-sans", Noop },
170                 { LFUN_FRAK, "font-frak", Noop },
171                 { LFUN_ITAL, "font-ital", Noop },
172                 { LFUN_FONT_SIZE, "font-size", Noop },
173                 { LFUN_FONT_STATE, "font-state", ReadOnly },
174                 { LFUN_UNDERLINE, "font-underline", Noop },
175                 { LFUN_INSET_FOOTNOTE, "footnote-insert", Noop },
176                 { LFUN_RIGHTSEL, "forward-select", ReadOnly },
177                 { LFUN_HFILL, "hfill-insert", Noop },
178                 { LFUN_HELP_OPEN, "help-open", NoBuffer | Argument},
179                 { LFUN_HTMLURL, "html-insert", Noop },
180                 { LFUN_HYPHENATION, "hyphenation-point-insert", Noop },
181                 { LFUN_LIGATURE_BREAK, "ligature-break-insert", Noop },
182                 { LFUN_INDEX_INSERT, "index-insert", Noop },
183                 { LFUN_INDEX_PRINT, "index-print", Noop },
184                 { LFUN_KMAP_OFF, "keymap-off", ReadOnly },
185                 { LFUN_KMAP_PRIM, "keymap-primary", ReadOnly },
186                 { LFUN_KMAP_SEC, "keymap-secondary", ReadOnly },
187                 { LFUN_KMAP_TOGGLE, "keymap-toggle", ReadOnly },
188                 { LFUN_INSERT_LABEL, "label-insert", Noop },
189                 { LFUN_INSET_OPTARG, "optional-insert", Noop },
190                 { LFUN_INSERT_BIBITEM, "bibitem-insert", Noop },
191                 { LFUN_INSERT_LINE, "line-insert", Noop },
192                 { LFUN_INSERT_PAGEBREAK, "pagebreak-insert", Noop },
193                 { LFUN_LANGUAGE, "language", Noop },
194                 { LFUN_LAYOUT, "layout", Noop },
195                 { LFUN_LAYOUT_PARAGRAPH, "layout-paragraph", ReadOnly },
196                 { LFUN_LAYOUT_TABULAR, "layout-tabular", Noop },
197                 { LFUN_HOME, "line-begin", ReadOnly },
198                 { LFUN_HOMESEL, "line-begin-select", ReadOnly },
199                 { LFUN_DELETE_LINE_FORWARD, "line-delete-forward", Noop },
200                 { LFUN_END, "line-end", ReadOnly },
201                 { LFUN_ENDSEL, "line-end-select", ReadOnly },
202 #if 0
203                 { LFUN_INSET_LIST, "list-insert", Noop },
204 #endif
205                 { LFUN_QUIT, "lyx-quit", NoBuffer },
206                 { LFUN_INSET_MARGINAL, "marginalnote-insert", Noop },
207                 { LFUN_MARK_OFF, "mark-off", ReadOnly },
208                 { LFUN_MARK_ON, "mark-on", ReadOnly },
209                 { LFUN_SETMARK, "mark-toggle", ReadOnly },
210                 { LFUN_MATH_DELIM, "math-delim", Noop },
211                 { LFUN_MATH_DISPLAY, "math-display", Noop },
212                 { LFUN_INSERT_MATH, "math-insert", Noop },
213                 { LFUN_SUBSCRIPT, "math-subscript", Noop },
214                 { LFUN_SUPERSCRIPT, "math-superscript", Noop },
215                 { LFUN_MATH_LIMITS, "math-limits", Noop },
216                 { LFUN_MATH_MACRO, "math-macro", Noop },
217                 { LFUN_MATH_MUTATE, "math-mutate", Noop },
218                 { LFUN_MATH_SPACE, "math-space", Noop },
219                 { LFUN_MATH_IMPORT_SELECTION, "math-import-selection", Noop },
220                 { LFUN_INSERT_MATRIX, "math-matrix", Noop },
221                 { LFUN_MATH_MODE, "math-mode", Noop },
222                 { LFUN_MATH_NONUMBER, "math-nonumber", Noop },
223                 { LFUN_MATH_NUMBER, "math-number", Noop },
224                 { LFUN_MATH_EXTERN, "math-extern", Noop },
225                 { LFUN_MATH_SIZE, "math-size", Noop },
226                 { LFUN_MENU_OPEN_BY_NAME, "menu-open", NoBuffer },
227                 { LFUN_MENU_SEPARATOR, "menu-separator-insert", Noop },
228                 { LFUN_META_FAKE, "meta-prefix", NoBuffer },
229                 { LFUN_INSERT_BRANCH, "branch-insert", Noop },
230                 { LFUN_INSERT_CHARSTYLE, "charstyle-insert", Noop },
231                 { LFUN_INSERT_NOTE, "note-insert", Noop },
232                 { LFUN_INSERT_BOX, "box-insert", Noop },
233                 { LFUN_GOTONOTE, "note-next", ReadOnly },
234                 { LFUN_INSET_TOGGLE, "", ReadOnly },
235                 { LFUN_NEXT_INSET_TOGGLE, "next-inset-toggle", ReadOnly },
236                 { LFUN_ALL_INSETS_TOGGLE, "all-insets-toggle", ReadOnly },
237                 { LFUN_DOWN_PARAGRAPH, "paragraph-down", ReadOnly },
238                 { LFUN_DOWN_PARAGRAPHSEL, "paragraph-down-select", ReadOnly },
239                 { LFUN_GOTO_PARAGRAPH, "paragraph-goto", ReadOnly },
240                 { LFUN_PARAGRAPH_SPACING, "paragraph-spacing", Noop },
241                 { LFUN_UP_PARAGRAPH, "paragraph-up", ReadOnly },
242                 { LFUN_UP_PARAGRAPHSEL, "paragraph-up-select", ReadOnly },
243                 { LFUN_PASTE, "paste", Noop },
244                 { LFUN_SAVEPREFERENCES, "preferences-save", NoBuffer },
245                 { LFUN_PASTESELECTION, "primary-selection-paste", Noop },
246                 { LFUN_QUOTE, "quote-insert", Noop },
247                 { LFUN_RECONFIGURE, "reconfigure", NoBuffer },
248                 { LFUN_REDO, "redo", Noop },
249                 { LFUN_REF_GOTO, "reference-goto", ReadOnly },
250                 { LFUN_REFERENCE_GOTO, "reference-next", ReadOnly },
251                 { LFUN_NEXT, "screen-down", ReadOnly },
252                 { LFUN_NEXTSEL, "screen-down-select", ReadOnly },
253                 { LFUN_SCREEN_FONT_UPDATE, "screen-font-update", NoBuffer },
254                 { LFUN_CENTER, "screen-recenter", ReadOnly },
255                 { LFUN_PRIOR, "screen-up", ReadOnly },
256                 { LFUN_PRIORSEL, "screen-up-select", ReadOnly },
257                 { LFUN_SCROLL_INSET, "inset-scroll", ReadOnly },
258                 { LFUN_SELFINSERT, "self-insert", Noop },
259                 { LFUN_SPACE_INSERT, "space-insert", Noop },
260                 { LFUN_CHARATCURSOR, "server-char-after", ReadOnly },
261                 { LFUN_GETFONT, "server-get-font", ReadOnly },
262                 { LFUN_GETLAYOUT, "server-get-layout", ReadOnly },
263                 { LFUN_GETNAME, "server-get-name", ReadOnly },
264                 { LFUN_GETXY, "server-get-xy", ReadOnly },
265                 { LFUN_GOTOFILEROW, "server-goto-file-row", Noop },
266                 { LFUN_NOTIFY, "server-notify", ReadOnly },
267                 { LFUN_SETXY, "server-set-xy", ReadOnly },
268                 { LFUN_SET_COLOR, "set-color", ReadOnly | NoBuffer },
269                 { LFUN_CELL_BACKWARD, "cell-backward", Noop },
270                 { LFUN_CELL_FORWARD, "cell-forward", Noop },
271                 { LFUN_CELL_SPLIT, "cell-split", Noop },
272                 { LFUN_TABULAR_INSERT, "tabular-insert", Noop },
273                 { LFUN_TABULAR_FEATURE, "tabular-feature", Noop },
274 #if 0
275                 { LFUN_INSET_THEOREM, "theorem-insert", Noop },
276 #endif
277                 { LFUN_THESAURUS_ENTRY, "thesaurus-entry", ReadOnly },
278                 { LFUN_TOC_INSERT, "toc-insert", Noop },
279                 { LFUN_TOCVIEW, "toc-view", ReadOnly },
280                 { LFUN_TOGGLECURSORFOLLOW, "toggle-cursor-follows-scrollbar", ReadOnly },
281                 { LFUN_UNDO, "undo", Noop },
282                 { LFUN_UP, "up", ReadOnly },
283                 { LFUN_UPSEL, "up-select", ReadOnly },
284                 { LFUN_URL, "url-insert", Noop },
285                 { LFUN_VC_CHECKIN, "vc-check-in", ReadOnly },
286                 { LFUN_VC_CHECKOUT, "vc-check-out", ReadOnly },
287                 { LFUN_VC_REGISTER, "vc-register", ReadOnly },
288                 { LFUN_VC_REVERT, "vc-revert", ReadOnly },
289                 { LFUN_VC_UNDO, "vc-undo-last", ReadOnly },
290                 { LFUN_WORDLEFT, "word-backward", ReadOnly },
291                 { LFUN_WORDLEFTSEL, "word-backward-select", ReadOnly },
292                 { LFUN_CAPITALIZE_WORD, "word-capitalize", Noop },
293                 { LFUN_DELETE_WORD_BACKWARD, "word-delete-backward", Noop },
294                 { LFUN_DELETE_WORD_FORWARD, "word-delete-forward", Noop },
295                 { LFUN_WORDFINDBACKWARD, "word-find-backward", ReadOnly },
296                 { LFUN_WORDFINDFORWARD, "word-find-forward", ReadOnly },
297                 { LFUN_WORDRIGHT, "word-forward", ReadOnly },
298                 { LFUN_WORDRIGHTSEL, "word-forward-select", ReadOnly },
299                 { LFUN_LOWCASE_WORD, "word-lowcase", Noop },
300                 { LFUN_WORDSEL, "word-select", ReadOnly },
301                 { LFUN_UPCASE_WORD, "word-upcase", Noop },
302                 { LFUN_MESSAGE, "message", NoBuffer },
303                 { LFUN_TRANSPOSE_CHARS, "chars-transpose", Noop },
304                 { LFUN_FLOAT_LIST, "float-list", Noop },
305                 { LFUN_ESCAPE, "escape", ReadOnly },
306                 { LFUN_TOOLTIPS_TOGGLE, "toggle-tooltips", NoBuffer },
307                 { LFUN_TRACK_CHANGES, "track-changes", Noop },
308                 { LFUN_MERGE_CHANGES, "merge-changes", Noop },
309                 { LFUN_ACCEPT_CHANGE, "accept-change", Noop },
310                 { LFUN_REJECT_CHANGE, "reject-change", Noop },
311                 { LFUN_ACCEPT_ALL_CHANGES, "accept-all-changes", Noop },
312                 { LFUN_REJECT_ALL_CHANGES, "reject-all-changes", Noop },
313                 { LFUN_DIALOG_SHOW, "dialog-show", NoBuffer },
314                 { LFUN_DIALOG_SHOW_NEW_INSET, "dialog-show-new-inset", Noop },
315                 { LFUN_DIALOG_SHOW_NEXT_INSET, "dialog-show-next-inset", Noop },
316                 { LFUN_DIALOG_UPDATE, "dialog-update", NoBuffer },
317                 { LFUN_DIALOG_HIDE, "dialog-hide", Noop },
318                 { LFUN_DIALOG_DISCONNECT_INSET, "dialog-disconnect-inset", Noop },
319                 { LFUN_INSET_APPLY, "inset-apply", Noop },
320                 { LFUN_INSET_INSERT, "inset-insert", Noop },
321                 { LFUN_INSET_MODIFY, "", Noop },
322                 { LFUN_INSET_DIALOG_UPDATE, "", Noop },
323                 { LFUN_INSET_SETTINGS, "inset-settings", ReadOnly },
324                 { LFUN_INSET_DIALOG_SHOW, "inset-dialog-show", Noop },
325                 { LFUN_PARAGRAPH_APPLY, "paragraph-params-apply", Noop },
326                 { LFUN_PARAGRAPH_UPDATE, "", Noop },
327                 { LFUN_EXTERNAL_EDIT, "external-edit", Noop },
328                 { LFUN_GRAPHICS_EDIT, "graphics-edit", Noop },
329                 { LFUN_REPEAT, "repeat", NoBuffer },
330                 { LFUN_WORD_FIND, "word-find", Noop },
331                 { LFUN_WORD_REPLACE, "word-replace", Noop },
332                 { LFUN_KEYMAP_TOGGLE, "keymap-toggle", Noop },
333                 { LFUN_LANGUAGE_BUFFER, "buffer-language", Noop },
334                 { LFUN_TEXTCLASS_APPLY, "textclass-apply", Noop },
335                 { LFUN_TEXTCLASS_LOAD, "textclass-load", Noop },
336                 { LFUN_SAVE_AS_DEFAULT, "buffer-save-as-default", Noop },
337                 { LFUN_BUFFERPARAMS_APPLY, "buffer-params-apply", Noop },
338                 { LFUN_LYXRC_APPLY, "lyxrc-apply", NoBuffer },
339                 { LFUN_NOACTION, "", Noop }
340         };
341
342         for (int i = 0; items[i].action != LFUN_NOACTION; ++i) {
343                 newFunc(items[i].action, items[i].name, items[i].attrib);
344         }
345
346         init = true;
347 }
348
349
350 LyXAction::LyXAction()
351 {
352         init();
353 }
354
355
356 // Returns an action tag from a string.
357 FuncRequest LyXAction::lookupFunc(string const & func) const
358 {
359         string const func2 = trim(func);
360
361         if (func2.empty()) {
362                 return FuncRequest(LFUN_NOACTION);
363         }
364
365         string cmd;
366         string const arg = split(func2, cmd, ' ');
367
368         func_map::const_iterator fit = lyx_func_map.find(cmd);
369
370         return fit != lyx_func_map.end() ? FuncRequest(fit->second, arg) : FuncRequest(LFUN_UNKNOWN_ACTION);
371 }
372
373
374 string const LyXAction::getActionName(kb_action action) const
375 {
376         info_map::const_iterator const it = lyx_info_map.find(action);
377         return it != lyx_info_map.end() ? it->second.name : string();
378 }
379
380
381 bool LyXAction::funcHasFlag(kb_action action,
382                             LyXAction::func_attrib flag) const
383 {
384         info_map::const_iterator ici = lyx_info_map.find(action);
385
386         if (ici == lyx_info_map.end()) {
387                 lyxerr << "action: " << action << " is not known." << endl;
388                 BOOST_ASSERT(false);
389         }
390
391         return ici->second.attrib & flag;
392 }
393
394
395 LyXAction::const_func_iterator LyXAction::func_begin() const
396 {
397         return lyx_func_map.begin();
398 }
399
400
401 LyXAction::const_func_iterator LyXAction::func_end() const
402 {
403         return lyx_func_map.end();
404 }
405
406
407 ostream & operator<<(ostream & o, kb_action action)
408 {
409         return o << int(action);
410 }