]> git.lyx.org Git - lyx.git/blob - src/LyXAction.C
750312ff2803c2b9379235b0d19086e7c5399e76
[lyx.git] / src / LyXAction.C
1 /* This file is part of
2 * ======================================================
3
4 *           LyX, The Document Processor
5 *        
6 *           Copyright (C) 1995 Matthias Ettrich
7 *           Copyright (C) 1995-1998 The LyX Team.
8 *
9 *======================================================*/
10
11 #include <config.h>
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <string.h>
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "LyXAction.h"
23 #include "error.h"
24 #include "gettext.h"
25
26  /*  
27      NAMING RULES FOR USER-COMMANDS
28      Here's the set of rules to apply when a new command name is introduced:
29  
30      1) Use the object.event order. That is, use `word-forward'
31         instead of `forward-word'.
32      2) Don't introduce an alias for an already named object. Same for events.
33      3) Forward movement or focus is called `forward' (not `right').
34      4) Backward movement or focus is called `backward' (not `left').
35      5) Upward movement of focus is called `up'.
36      6) Downward movement is called `down'.
37      7) The begin of an object is called `begin' (not `start').
38      8) The end of an object is called `end'.
39  
40      (May 19 1996, 12:04, RvdK)
41  */
42  
43
44 // These are globals. 
45 LyXAction lyxaction;
46
47
48 /* This table is sorted alphabetically [asierra 14Jan96] */
49 /* This table MUST be sorted alphabetically, incidentally! */ 
50 kb_func_table const lyx_func_table[] = {
51         { "accent-acute",                         LFUN_ACUTE },
52         { "accent-breve",                         LFUN_BREVE },
53         { "accent-caron",                         LFUN_CARON },
54         { "accent-cedilla",                       LFUN_CEDILLA },
55         { "accent-circle",                        LFUN_CIRCLE },
56         { "accent-circumflex",                    LFUN_CIRCUMFLEX },
57         { "accent-dot",                           LFUN_DOT },
58         { "accent-grave",                         LFUN_GRAVE },
59         { "accent-hungarian-umlaut",              LFUN_HUNG_UMLAUT },
60         { "accent-macron",                        LFUN_MACRON },
61         { "accent-ogonek",                        LFUN_OGONEK },
62         { "accent-special-caron",                 LFUN_SPECIAL_CARON },
63         { "accent-tie",                           LFUN_TIE },
64         { "accent-tilde",                         LFUN_TILDE },
65         { "accent-umlaut",                        LFUN_UMLAUT },
66         { "accent-underbar",                      LFUN_UNDERBAR },
67         { "accent-underdot",                      LFUN_UNDERDOT },
68         { "accent-vector",                        LFUN_VECTOR }, 
69         { "appendix",                         LFUN_APPENDIX }, 
70         { "apropos",                              LFUN_APROPOS },
71         { "backward-select",                      LFUN_LEFTSEL },
72         { "bibtex-database-add",                  LFUN_BIBDB_ADD },
73         { "bibtex-database-del",                  LFUN_BIBDB_DEL },
74         { "bibtex-insert",                        LFUN_INSERT_BIBTEX },
75         { "bibtex-style",                         LFUN_BIBTEX_STYLE },
76         { "break-line",                           LFUN_BREAKLINE },
77         { "break-paragraph",                      LFUN_BREAKPARAGRAPH },
78         { "break-paragraph-keep-layout",          LFUN_BREAKPARAGRAPHKEEPLAYOUT },
79         { "break-paragraph-skip",                 LFUN_BREAKPARAGRAPH_SKIP },
80         { "buffer-auto-save",                     LFUN_AUTOSAVE },
81         { "buffer-begin",                         LFUN_BEGINNINGBUF },
82         { "buffer-begin-select",                  LFUN_BEGINNINGBUFSEL },
83         { "buffer-child-insert",                  LFUN_CHILDINSERT },  // ale970521
84         { "buffer-child-open",                    LFUN_CHILDOPEN },  // ale970528
85         { "buffer-chktex",                        LFUN_RUNCHKTEX }, // Asger 971030
86         { "buffer-close",                         LFUN_CLOSEBUFFER },         
87         { "buffer-end",                           LFUN_ENDBUF },
88         { "buffer-end-select",                    LFUN_ENDBUFSEL },
89         { "buffer-export",                        LFUN_EXPORT },
90         { "buffer-fax",                           LFUN_FAX },
91         { "buffer-float-insert",                  LFUN_INSERTFOOTNOTE },
92         { "buffer-import",                        LFUN_IMPORT },
93         { "buffer-itemize-bullets-select",        LFUN_BUFFERBULLETSSELECT },
94         { "buffer-new",                           LFUN_MENUNEW },     
95         { "buffer-new-template",                  LFUN_MENUNEWTMPLT },
96         { "buffer-open",                          LFUN_MENUOPEN },    
97         { "buffer-previous",                      LFUN_PREVBUFFER },  
98         { "buffer-print",                         LFUN_MENUPRINT },   
99         { "buffer-reload",                        LFUN_MENURELOAD },   
100         { "buffer-toggle-read-only",              LFUN_READ_ONLY_TOGGLE },
101         { "buffer-typeset",                       LFUN_RUNLATEX },        // Alejandro's proposal
102         { "buffer-typeset-ps",                    LFUN_RUNDVIPS },
103         { "buffer-view",                          LFUN_PREVIEW },
104         { "buffer-view-ps",                       LFUN_PREVIEWPS },
105         { "buffer-write",                         LFUN_MENUWRITE },   
106         { "buffer-write-as",                      LFUN_MENUWRITEAS }, 
107         { "build-program",                        LFUN_BUILDPROG },
108         { "cancel",                               LFUN_CANCEL },
109         { "char-backward",                        LFUN_LEFT },
110         { "char-forward",                         LFUN_RIGHT },
111         { "citation-insert",                      LFUN_INSERT_CITATION },
112         { "command-execute",                      LFUN_EXEC_COMMAND },
113         { "command-prefix",                       LFUN_PREFIX },
114         { "copy",                                 LFUN_COPY },
115         { "cut",                                  LFUN_CUT },
116         { "delete-backward",                      LFUN_BACKSPACE },
117         { "delete-backward-skip",                 LFUN_BACKSPACE_SKIP },
118         { "delete-forward",                       LFUN_DELETE },
119         { "delete-forward-skip",                  LFUN_DELETE_SKIP },
120         { "depth-decrement",                      LFUN_DEPTH_MIN },
121         { "depth-increment",                      LFUN_DEPTH_PLUS },  
122         { "depth-next",                           LFUN_DEPTH },
123         { "dots-insert",                          LFUN_LDOTS },
124         { "down",                                 LFUN_DOWN },                 
125         { "down-select",                          LFUN_DOWNSEL },
126         { "drop-layouts-choice",                  LFUN_DROP_LAYOUTS_CHOICE },
127         { "end-of-sentence-period-insert",        LFUN_END_OF_SENTENCE },
128         { "error-next",                           LFUN_GOTOERROR },   
129         { "error-remove-all",                     LFUN_REMOVEERRORS },
130         { "figure-insert",                        LFUN_FIGURE },
131         { "file-insert",                          LFUN_FILE_INSERT },
132         { "file-insert-ascii",                    LFUN_FILE_INSERT_ASCII },
133         { "file-new",                             LFUN_FILE_NEW },
134         { "file-open",                            LFUN_FILE_OPEN },
135         { "find-replace",                         LFUN_MENUSEARCH },  
136         { "font-bold",                            LFUN_BOLD },        
137         { "font-code",                            LFUN_CODE },        
138         { "font-default",                         LFUN_DEFAULT },
139         { "font-emph",                            LFUN_EMPH },
140         { "font-free",                            LFUN_FREE },
141         { "font-noun",                            LFUN_NOUN },
142         { "font-roman",                           LFUN_ROMAN },
143         { "font-sans",                            LFUN_SANS },
144         { "font-size",                            LFUN_FONT_SIZE },
145         { "font-state",                           LFUN_FONT_STATE }, 
146         { "font-underline",                       LFUN_UNDERLINE },   
147         { "footnote-insert",                      LFUN_FOOTMELT },    
148         { "forward-select",                       LFUN_RIGHTSEL },
149         { "hfill-insert",                         LFUN_HFILL },
150         { "html-insert",                          LFUN_HTMLURL },
151         { "hyphenation-point-insert",             LFUN_HYPHENATION },
152         { "index-insert",                         LFUN_INDEX_INSERT },
153         { "index-insert-last",                    LFUN_INDEX_INSERT_LAST },
154         { "index-print",                          LFUN_INDEX_PRINT },
155         { "inset-latex-insert",                   LFUN_INSERT_INSET_LATEX },
156         { "keymap-off",                           LFUN_KMAP_OFF },
157         { "keymap-primary",                       LFUN_KMAP_PRIM },
158         { "keymap-secondary",                     LFUN_KMAP_SEC },
159         { "keymap-toggle",                        LFUN_KMAP_TOGGLE },
160         { "label-insert",                         LFUN_INSERT_LABEL },
161         { "latex-view-log",                       LFUN_LATEX_LOG },
162         { "layout",                               LFUN_LAYOUT },
163         { "layout-character",                     LFUN_LAYOUT_CHARACTER },
164         { "layout-copy",                          LFUN_LAYOUT_COPY },
165         { "layout-document",                      LFUN_LAYOUT_DOCUMENT },
166         //{ "layout-number",                      LFUN_LAYOUTNO }, // internal only
167         { "layout-paper",                         LFUN_LAYOUT_PAPER },
168         { "layout-paragraph",                     LFUN_LAYOUT_PARAGRAPH },
169         { "layout-paste",                         LFUN_LAYOUT_PASTE },
170         { "layout-preamble",                      LFUN_LAYOUT_PREAMBLE },
171         { "layout-quotes",                        LFUN_LAYOUT_QUOTES },
172         { "layout-save-default",                  LFUN_LAYOUT_SAVE_DEFAULT },
173         { "layout-table",                         LFUN_LAYOUT_TABLE },
174         { "line-begin",                           LFUN_HOME },        
175         { "line-begin-select",                    LFUN_HOMESEL },     
176         { "line-delete-forward",                  LFUN_DELETE_LINE_FORWARD },  
177         { "line-end",                             LFUN_END },         
178         { "line-end-select",                      LFUN_ENDSEL },
179         { "loa-insert",                           LFUN_LOA_INSERT },
180         { "lof-insert",                           LFUN_LOF_INSERT },
181         { "lot-insert",                           LFUN_LOT_INSERT },
182         { "lyx-quit",                             LFUN_QUIT },
183         { "marginpar-insert",                     LFUN_MARGINMELT },  
184         { "mark-off",                             LFUN_MARK_OFF },    
185         { "mark-on",                              LFUN_MARK_ON },     
186         { "mark-toggle",                          LFUN_SETMARK }, 
187         { "math-delim",                           LFUN_MATH_DELIM },
188         { "math-display",                         LFUN_MATH_DISPLAY }, // Alejandro's proposal
189         { "math-greek",                           LFUN_GREEK },   
190         { "math-greek-toggle",                    LFUN_GREEK_TOGGLE },   
191         { "math-insert",                          LFUN_INSERT_MATH },   
192         { "math-limits",                          LFUN_MATH_LIMITS },
193         { "math-macro",                           LFUN_MATH_MACRO },
194         { "math-macro-arg",                       LFUN_MATH_MACROARG },
195         { "math-matrix",                          LFUN_INSERT_MATRIX }, 
196         { "math-mode",                            LFUN_MATH_MODE },
197         { "math-nonumber",                        LFUN_MATH_NONUMBER },
198         { "math-number",                          LFUN_MATH_NUMBER }, 
199         { "math-size",                            LFUN_MATH_SIZE }, 
200         { "melt",                                 LFUN_MELT },            // Needs better name. What about "float-disolve" or "float-extract" (Lgb)
201         { "menu-open",                            LFUN_MENU_OPEN_BY_NAME },
202         { "menu-separator-insert",                LFUN_MENU_SEPARATOR },
203         { "meta-prefix",                          LFUN_META_FAKE },
204         { "note-insert",                          LFUN_INSERT_NOTE },
205         { "note-next",                            LFUN_GOTONOTE },
206         { "open-stuff",                           LFUN_OPENSTUFF },       // Needs better name.
207         { "paragraph-down",                       LFUN_DOWN_PARAGRAPH },
208         { "paragraph-down-select",                LFUN_DOWN_PARAGRAPHSEL },
209         { "paragraph-up",                         LFUN_UP_PARAGRAPH },
210         { "paragraph-up-select",                  LFUN_UP_PARAGRAPHSEL },
211         { "parent-insert",                        LFUN_PARENTINSERT },
212         { "paste",                                LFUN_PASTE },
213         { "primary-selection-paste",              LFUN_PASTESELECTION },
214         { "protected-space-insert",               LFUN_PROTECTEDSPACE },
215         { "quote-insert",                         LFUN_QUOTE },
216         { "reconfigure",                          LFUN_RECONFIGURE },
217         { "redo",                                 LFUN_REDO },
218         { "reference-back",                       LFUN_REFBACK },
219         { "reference-goto",                       LFUN_REFGOTO }, 
220         { "reference-insert",                     LFUN_INSERT_REF },
221         { "reference-toggle",                     LFUN_REFTOGGLE },
222         { "screen-down",                          LFUN_NEXT },        
223         { "screen-down-select",                   LFUN_NEXTSEL },     
224         { "screen-recenter",                      LFUN_CENTER },
225         { "screen-up",                            LFUN_PRIOR },       
226         { "screen-up-select",                     LFUN_PRIORSEL },    
227         { "self-insert",                          LFUN_SELFINSERT },
228         { "server-char-after",                    LFUN_CHARATCURSOR },
229         { "server-get-font",                      LFUN_GETFONT },
230         { "server-get-latex",                     LFUN_GETLATEX },
231         { "server-get-layout",                    LFUN_GETLAYOUT },
232         { "server-get-name",                      LFUN_GETNAME },
233         { "server-get-tip",                       LFUN_GETTIP },
234         { "server-get-xy",                        LFUN_GETXY },
235         { "server-goto-file-row",                 LFUN_GOTOFILEROW },
236         { "server-notify",                        LFUN_NOTIFY },
237         { "server-set-xy",                        LFUN_SETXY },
238         { "spellchecker",                         LFUN_SPELLCHECK },  
239         { "symbol-insert",                        LFUN_INSERT_MATH },
240         { "tab-forward",                          LFUN_TAB },
241         { "tab-insert",                           LFUN_TABINSERT },
242         { "table-insert",                         LFUN_TABLE },
243         { "tex-mode",                             LFUN_TEX },
244         { "toc-insert",                           LFUN_TOC_INSERT },
245         { "toc-view",                             LFUN_TOCVIEW },
246         { "toggle-cursor-follows-scrollbar",      LFUN_TOGGLECURSORFOLLOW },
247         { "toolbar-add-to",                       LFUN_ADD_TO_TOOLBAR },
248         { "toolbar-push",                         LFUN_PUSH_TOOLBAR },
249         { "undo",                                 LFUN_UNDO },
250         { "up",                                   LFUN_UP },          
251         { "up-select",                            LFUN_UPSEL },
252         { "url-insert",                           LFUN_URL },
253         { "vc-check-in",                          LFUN_VC_CHECKIN }, 
254         { "vc-check-out",                         LFUN_VC_CHECKOUT }, 
255         { "vc-history",                           LFUN_VC_HISTORY }, 
256         { "vc-register",                          LFUN_VC_REGISTER }, 
257         { "vc-revert",                            LFUN_VC_REVERT }, 
258         { "vc-undo-last",                         LFUN_VC_UNDO }, 
259         { "word-backward",                        LFUN_WORDLEFT },
260         { "word-backward-select",                 LFUN_WORDLEFTSEL },
261         { "word-capitalize",                      LFUN_CAPITALIZE_WORD },
262         { "word-delete-backward",                 LFUN_DELETE_WORD_BACKWARD },
263         { "word-delete-forward",                  LFUN_DELETE_WORD_FORWARD }, 
264         { "word-find-backward",                   LFUN_WORDFINDBACKWARD },
265         { "word-find-forward",                    LFUN_WORDFINDFORWARD },
266         { "word-forward",                         LFUN_WORDRIGHT },
267         { "word-forward-select",                  LFUN_WORDRIGHTSEL },
268         { "word-lowcase",                         LFUN_LOWCASE_WORD },
269         { "word-upcase",                          LFUN_UPCASE_WORD }
270 };
271
272
273 /* 
274    This table contains the actions that modify a buffer and therefore
275    are not allowed for RO files. Do you think we have too much tables?
276    Each single integer in this table replaces 5 lines of code in lyxfunc
277    that include at least 3 function calls (several integers in the 
278    code segment).
279  
280    The table could in the near future be expanded adding a second integer 
281    field with attributes using these tags:
282
283 enum FUNC_ATTRIB {
284     LFAT_NONE=0,     // Nothing special
285     LFAT_ISRO=1,     // Is readonly (does not modify a buffer)
286     LFAT_ISIA=2,     // Is interactive (requires a GUI)
287     LFAT_REQARG=4,   // Requires argument
288     LFAT_ISMO=8,     // Only math mode
289     LFAT_ETCETC
290 };
291
292 struct {
293     kb_action action;
294     unsigned  attrib; 
295 };
296
297 Alejandro-970604
298 */
299
300 kb_action func_attrib_table[] = {
301         LFUN_ACUTE,
302         LFUN_AUTOSAVE,
303         LFUN_BACKSPACE,
304         LFUN_BACKSPACE_SKIP,
305         LFUN_BIBDB_ADD,
306         LFUN_BIBDB_DEL,
307         LFUN_BIBTEX_STYLE,
308         LFUN_BOLD,
309         LFUN_BREAKLINE,
310         LFUN_BREAKPARAGRAPH,
311         LFUN_BREAKPARAGRAPHKEEPLAYOUT,
312         LFUN_BREAKPARAGRAPH_SKIP,
313         LFUN_BREVE,
314         LFUN_BUFFERBULLETSSELECT,
315         LFUN_CAPITALIZE_WORD,
316         LFUN_CARON,
317         LFUN_CEDILLA,
318         LFUN_CHILDINSERT,
319         LFUN_CIRCLE,
320         LFUN_CIRCUMFLEX,
321         LFUN_CODE,
322         LFUN_CUT,
323         LFUN_DEFAULT,
324         LFUN_DELETE,
325         LFUN_DELETE_LINE_FORWARD,
326         LFUN_DELETE_SKIP,
327         LFUN_DELETE_WORD_BACKWARD,
328         LFUN_DELETE_WORD_FORWARD,
329         LFUN_DEPTH,
330         LFUN_DEPTH_MIN,
331         LFUN_DEPTH_PLUS,
332         LFUN_DOT,
333         LFUN_EMPH,
334         LFUN_END_OF_SENTENCE,
335         LFUN_FIGURE,
336         LFUN_FILE_INSERT,
337         LFUN_FILE_INSERT_ASCII,
338         LFUN_FONT_SIZE,
339         LFUN_FOOTMELT,
340         LFUN_FREE,
341         LFUN_GRAVE,
342         LFUN_HFILL,
343         LFUN_HTMLURL,
344         LFUN_HUNG_UMLAUT,
345         LFUN_HYPHENATION,
346         LFUN_INDEX_INSERT,
347         LFUN_INDEX_INSERT_LAST,
348         LFUN_INDEX_PRINT,
349         LFUN_INSERTFOOTNOTE,
350         LFUN_INSERT_BIBTEX,
351         LFUN_INSERT_CITATION,
352         LFUN_INSERT_INSET_LATEX,
353         LFUN_INSERT_LABEL,
354         LFUN_INSERT_MATH,
355         LFUN_INSERT_MATRIX,
356         LFUN_INSERT_NOTE,
357         LFUN_INSERT_REF,
358         LFUN_LAYOUT,
359         LFUN_LAYOUTNO,
360         LFUN_LAYOUT_CHARACTER,
361         LFUN_LAYOUT_PASTE,
362         LFUN_LAYOUT_QUOTES,
363         LFUN_LDOTS,
364         LFUN_LOA_INSERT,
365         LFUN_LOF_INSERT,
366         LFUN_LOT_INSERT,
367         LFUN_LOWCASE_WORD,
368         LFUN_MACRON,
369         LFUN_MARGINMELT,
370         LFUN_MATH_DELIM,
371         LFUN_MATH_DISPLAY,
372         LFUN_MATH_MACRO,
373         LFUN_MATH_MACROARG,
374         LFUN_MATH_MODE,
375         LFUN_MATH_NONUMBER,
376         LFUN_MATH_NUMBER,
377         LFUN_MATH_SIZE,
378         LFUN_MELT,
379         LFUN_MENU_SEPARATOR,
380         LFUN_NOUN,
381         LFUN_OGONEK,
382         LFUN_PARENTINSERT,
383         LFUN_PASTE,
384         LFUN_PASTESELECTION,
385         LFUN_PROTECTEDSPACE,
386         LFUN_QUOTE,
387         LFUN_REDO,
388         LFUN_REFTOGGLE,
389         LFUN_ROMAN,
390         LFUN_SANS,
391         LFUN_SELFINSERT,
392         LFUN_SPECIAL_CARON,
393         LFUN_TABINSERT,
394         LFUN_TABLE,
395         LFUN_TEX,
396         LFUN_TIE,
397         LFUN_TILDE,
398         LFUN_TOC_INSERT,
399         LFUN_UMLAUT,
400         LFUN_UNDERBAR,
401         LFUN_UNDERDOT,
402         LFUN_UNDERLINE,
403         LFUN_UNDO,
404         LFUN_UNKNOWN_ACTION,
405         LFUN_UPCASE_WORD,
406         LFUN_URL,
407         LFUN_VECTOR,
408         LFUN_WORDFINDFORWARD,
409         LFUN_WORDFINDBACKWARD
410 };
411
412 int LyXAction::psd_idx = 0;
413 kb_func_table const * LyXAction::lyx_func_table = &::lyx_func_table[0];
414 kb_func_table* LyXAction::lyx_func_args = 0;
415  
416 /* === code ============================================================== */
417
418 /* This routines allow binding actions with argument.
419  * Provisionally a fixed size array and global functions are used.
420  * [asierra 20Jan96]
421  */
422 #define MAX_PSEUDO_ACTION 128
423
424
425 LyXAction::LyXAction()
426 {
427         lyx_func_args= new kb_func_table[MAX_PSEUDO_ACTION];
428         funcCount = sizeof(::lyx_func_table) / sizeof(::kb_func_table);
429 }
430
431
432 LyXAction::~LyXAction()
433 {
434         if (lyx_func_args) {
435 // This is wrong, so I'll just disable it for now.
436 // The problem is that we might free memory twice in some situations...
437 // It's better to leak a bit that to crash. (Asger)
438 //              for (int i=0; i < psd_idx; i++) {
439 //                      free(lyx_func_args[i].name);
440 //              }
441                 delete[] lyx_func_args;
442                 lyx_func_args = 0;
443         }
444 }
445
446
447 // Search for an existent pseudoaction, return -1 if it doesn't exist.
448 int  LyXAction::searchActionArg(kb_action action, char const *arg)
449 {
450         kb_func_table *tb = &lyx_func_args[0];
451         for (int i=0; i<psd_idx; i++) {
452                 if (action==tb->action && !strcmp(tb->name, arg)) {      
453
454                         lyxerr.debug(LString("Pseudoaction already exist[") 
455                                      + int(action) + '|' 
456                                      + PTR_AS_INT(arg) + ']',
457                                      Error::KEY);
458
459                         return LFUN_LASTACTION+i;
460                 }
461                 tb++;
462         }
463         return -1;
464 }
465
466
467 // Returns a pseudo-action given an action and its argument.
468 int LyXAction::getPseudoAction(kb_action action, char const *arg)
469 {
470         // Check if the pseudo-action already exist.
471         int psdaction = searchActionArg(action, arg);
472    
473         if (psdaction >= 0) return psdaction;
474
475         if (psd_idx>=MAX_PSEUDO_ACTION) {
476                 lyxerr.print("Lyx Error: No more pseudo-actions allowed");
477                 lyxerr.print("           Tell the developers.");
478                 return -1;
479         }
480         lyx_func_args[psd_idx].name = strdup(arg);
481         lyx_func_args[psd_idx].action = action;
482         psd_idx++;
483         return LFUN_LASTACTION+psd_idx-1;
484 }
485
486
487 // Retrieves the real action and its argument.
488 int LyXAction::retrieveActionArg(int i, char const** arg)
489 {
490         i -= LFUN_LASTACTION;
491         if (i >= 0 &&  i <psd_idx) {
492                 *arg = lyx_func_args[i].name;
493                 return (int)lyx_func_args[i].action;
494         } else {
495                 lyxerr.print("Lyx Error: Unrecognized pseudo-action");
496                 return -1;
497         }
498 }
499
500
501 // Returns an action tag from a string.
502 int LyXAction::LookupFunc(char const *func)
503 {
504         if (!func) 
505                 return LFUN_UNKNOWN_ACTION;         
506         if (func[0] == '\0')
507                 return LFUN_NOACTION;
508  
509    /* In the scan loop below, l can never become r, so set r one past the last
510       valid func table slot. RVDK_PATCH_5 */
511         int k, l= 0, r = funcCount;
512         int action = LFUN_UNKNOWN_ACTION;
513         char *arg = strchr(func, ' ');
514
515         if (arg) *(arg++) = '\0';      
516
517         while (l < r) {
518                 last_action_idx = (l+r)/2;
519                 k = strcmp(lyx_func_table[last_action_idx].name, func);
520                 if (k==0) {
521                         action = lyx_func_table[last_action_idx].action;
522                         break;
523                 } else
524                         if (k<0) l = last_action_idx+1; else r = last_action_idx;
525         }
526         if (arg && action >= 0) {
527                 action = getPseudoAction((kb_action)action, arg);
528                 lyxerr.debug(LString("Pseudo action_arg[")
529                              + int(action) + '|' 
530                              + PTR_AS_INT(arg) + ']',Error::KEY);
531         }
532         return action;
533 }
534
535
536 int LyXAction::getApproxFunc(char const *func)
537 {
538     int action = LookupFunc(func);
539     if (action<0) {
540         int k = strncmp(lyx_func_table[last_action_idx].name,
541                         func, strlen(func));
542         if (k<0 && last_action_idx<funcCount-1) 
543           last_action_idx++;
544         else if (k>0 && last_action_idx>0)
545           last_action_idx--;
546         
547         action = lyx_func_table[last_action_idx].action;
548     } else if (last_action_idx<funcCount-1)
549       last_action_idx++;
550     
551     return action;
552 }
553
554
555 char const *LyXAction::getApproxFuncName(char const *func)
556 {
557     getApproxFunc(func);
558     return lyx_func_table[last_action_idx].name;
559 }
560
561
562 char const *LyXAction::getActionName(int action) const
563 {
564         // why LFUN_LASTACTION -1? Because LFUN_LASTACTION is too
565         // large an because there is one internal command, hence
566         // lyx_func_table is 2 less then LFUN_LASTACTION (Lgb)
567     for (int i=0; i < funcCount; i++) {
568         if (lyx_func_table[i].action==action)
569           return lyx_func_table[i].name;
570     }
571     
572     // Hmm.. let's see whether this is a pseudoaction
573     action -= LFUN_LASTACTION;
574     if (action>=0 && action<psd_idx) {
575         return lyx_func_args[action].name;
576     }
577     
578     return "";
579 }
580
581
582 // Returns one line help associated with function
583 // Now the missing strings are replaced by the command names. (Alejandro)
584 char const *LyXAction::helpText(kb_action action) const
585 {
586         static bool is_sorted = false;
587         static kb_func_table helpTexts[LFUN_LASTACTION] =
588         {
589                 { _("Describe command"), LFUN_APROPOS },
590                 { _("Select previous char"), LFUN_LEFTSEL },
591                 { _("Insert bibtex"), LFUN_INSERT_BIBTEX },
592                 { _("Autosave"), LFUN_AUTOSAVE },
593                 { _("Go to beginning of document"), LFUN_BEGINNINGBUF },
594                 { _("Select to beginning of document"), LFUN_BEGINNINGBUFSEL },
595                 { _("Close"), LFUN_CLOSEBUFFER },
596                 { _("Go to end of document"), LFUN_ENDBUF },
597                 { _("Select to end of document"), LFUN_ENDBUFSEL },
598                 { _("Fax"), LFUN_FAX },
599                 { _("New document"), LFUN_MENUNEW },
600                 { _("New document from template"), LFUN_MENUNEWTMPLT },
601                 { _("Open"), LFUN_MENUOPEN },
602                 { _("Switch to previous document"), LFUN_PREVBUFFER },
603                 { _("Print"), LFUN_MENUPRINT },
604                 { _("Revert to saved"), LFUN_MENURELOAD },
605                 { _("Update DVI"), LFUN_RUNLATEX },
606                 { _("Update PostScript"), LFUN_RUNDVIPS },
607                 { _("View DVI"), LFUN_PREVIEW },
608                 { _("View PostScript"), LFUN_PREVIEWPS },
609                 { _("Build program"), LFUN_BUILDPROG },
610                 { _("Check TeX"), LFUN_RUNCHKTEX },
611                 { _("Save"), LFUN_MENUWRITE },
612                 { _("Save As"), LFUN_MENUWRITEAS },
613                 { _("Cancel"), LFUN_CANCEL },
614                 { _("Go one char back"), LFUN_LEFT },
615                 { _("Go one char forward"), LFUN_RIGHT },
616                 { _("Insert citation"), LFUN_INSERT_CITATION },
617                 { _("Execute command"), LFUN_EXEC_COMMAND },
618                 { _("Copy"), LFUN_COPY },
619                 { _("Cut"), LFUN_CUT },
620                 { _("Decrement environment depth"), LFUN_DEPTH_MIN },
621                 { _("Increment environment depth"), LFUN_DEPTH_PLUS },
622                 { _("Change environment depth"), LFUN_DEPTH },
623                 { _("Change itemize bullet settings"), LFUN_BUFFERBULLETSSELECT },
624                 { _("Go down"), LFUN_DOWN },
625                 { _("Select next line"), LFUN_DOWNSEL },
626                 { _("Choose Paragraph Environment"), LFUN_DROP_LAYOUTS_CHOICE },
627                 { _("Go to next error"), LFUN_GOTOERROR },
628                 { _("Insert Figure"), LFUN_FIGURE },
629                 { _("Find & Replace"), LFUN_MENUSEARCH },
630                 { _("Toggle cursor does/doesn't follow the scrollbar"), LFUN_TOGGLECURSORFOLLOW },
631                 { _("Toggle bold"), LFUN_BOLD },
632                 { _("Toggle code style"), LFUN_CODE },
633                 { _("Default font style"), LFUN_DEFAULT },
634                 { _("Toggle emphasize"), LFUN_EMPH },
635                 { _("Toggle user defined style"), LFUN_FREE },
636                 { _("Toggle noun style"), LFUN_NOUN },
637                 { _("Toggle roman font style"), LFUN_ROMAN },
638                 { _("Toggle sans font style"), LFUN_SANS },
639                 { _("Set font size"), LFUN_FONT_SIZE },
640                 { _("Show font state"), LFUN_FONT_STATE },
641                 { _("Toggle font underline"), LFUN_UNDERLINE },
642                 { _("Insert Footnote"), LFUN_FOOTMELT },
643                 { _("Select next char"), LFUN_RIGHTSEL },
644                 { _("Insert horizontal fill"), LFUN_HFILL },
645                 { _("Insert hyphenation point"), LFUN_HYPHENATION },
646                 { _("Insert ... dots"), LFUN_LDOTS },
647                 { _("Insert end of sentence period"), LFUN_END_OF_SENTENCE },
648                 { _("Turn off keymap"), LFUN_KMAP_OFF },
649                 { _("Use primary keymap"), LFUN_KMAP_PRIM },
650                 { _("Use secondary keymap"), LFUN_KMAP_SEC },
651                 { _("Toggle keymap"), LFUN_KMAP_TOGGLE },
652                 { _("Insert Label"), LFUN_INSERT_LABEL },
653                 { _("Copy paragraph environment type"), LFUN_LAYOUT_COPY },
654                 { _("Paste paragraph environment type"), LFUN_LAYOUT_PASTE },
655                 { _("Specify paper size and margins"), LFUN_LAYOUT_PAPER },
656                 { _("Go to beginning of line"), LFUN_HOME },
657                 { _("Select to beginning of line"), LFUN_HOMESEL },
658                 { _("Go to end of line"), LFUN_END },
659                 { _("Select to end of line"), LFUN_ENDSEL },
660                 { _("Exit"), LFUN_QUIT },
661                 { _("Insert Margin note"), LFUN_MARGINMELT },
662                 { _("Math Greek"), LFUN_GREEK },   
663                 { _("Math mode"), LFUN_MATH_MODE },
664                 { _("Go one paragraph down"), LFUN_DOWN_PARAGRAPH },
665                 { _("Select next paragraph"), LFUN_DOWN_PARAGRAPHSEL },
666                 { _("Go one paragraph up"), LFUN_UP_PARAGRAPH },
667                 { _("Select previous paragraph"), LFUN_UP_PARAGRAPHSEL },
668                 { _("Paste"), LFUN_PASTE },
669                 { _("Insert protected space"), LFUN_PROTECTEDSPACE },
670                 { _("Insert quote"), LFUN_QUOTE },
671                 { _("Reconfigure"), LFUN_RECONFIGURE },
672                 { _("Redo"), LFUN_REDO },
673                 { _("Insert cross reference"), LFUN_INSERT_REF },
674                 { _("Insert Table"), LFUN_TABLE },
675                 { _("Toggle TeX style"), LFUN_TEX },
676                 { _("Undo"), LFUN_UNDO },
677                 { _("Melt"), LFUN_MELT },
678                 { _("Import document"), LFUN_IMPORT },
679                 { _("Remove all error boxes"), LFUN_REMOVEERRORS },
680                 { _("Insert menu separator"), LFUN_MENU_SEPARATOR }
681 //              { "reference-back", LFUN_REFBACK },
682 //              { "reference-goto", LFUN_REFGOTO }, 
683         };
684         
685         // Sort to make it faster
686         if (!is_sorted) {
687                 int i=0; 
688                 while (i < LFUN_LASTACTION-1) {
689                     if (helpTexts[i].action == 0) {
690                         helpTexts[i].action = (kb_action)i;
691                         helpTexts[i].name = getActionName(i);
692                         i++;
693                     } else if (helpTexts[i].action != i) {
694                         int k = helpTexts[i].action;
695                         kb_func_table tmp = helpTexts[k];
696                         helpTexts[k] = helpTexts[i];
697                         helpTexts[i] = tmp;
698                         if (k < i) i++;
699                     } else {
700                         i++;
701                     }
702                 }
703                 is_sorted = true;
704         }
705
706         if (action <=1 || action >=funcCount)
707                 goto no_desc;
708                 
709         if (helpTexts[action].action == action) {
710             return helpTexts[action].name;
711         } 
712         //        + LString(' ') + int(is_ro) + LString(']'));
713   no_desc:
714         // In an ideal world, this never happens:
715         return _("No description available!");
716 }
717
718
719 // Function to compare items from the attrib table.
720 int actioncomp(const void *a, const void *b)
721 {
722     int const *ia=(int const*)a, *ib=(int const*)b;
723     return (*ia)-(*ib);
724 }
725
726  
727 bool LyXAction::isFuncRO(kb_action action) const
728 {
729     static bool is_sorted = false;    
730     static int fCount = sizeof(::func_attrib_table) / sizeof(kb_action);
731     
732     if (!is_sorted) {
733         qsort(func_attrib_table, fCount, sizeof(kb_action), actioncomp);
734         is_sorted = true;
735 //      for (int i=0; i<fCount; i++) 
736 //        fprintf(stdout, "%d: %d\n", i, func_attrib_table[i]);
737     }
738
739     int m, k, l= 0, r = fCount;
740     int is_ro = false;
741
742     while (l < r) {
743         m = (l+r)/2;
744         k = func_attrib_table[m] - action;
745         if (k==0) {
746             is_ro = true;
747             break;
748         } else
749           if (k<0) l = m+1; else r = m;
750     }
751     lyxerr.debug(LString("RO[") + int(action)
752                   + LString(" ") + int(is_ro) + LString("]"));
753     return is_ro;
754 }
755