]> git.lyx.org Git - lyx.git/blob - src/lyxfunc.C
26d83c051cf590a641832a5623589a265fa05bd4
[lyx.git] / src / lyxfunc.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "lyxfunc.h"
18 #include "version.h"
19 #include "kbmap.h"
20 #include "lyxrow.h"
21 #include "bufferlist.h"
22 #include "BufferView.h"
23 #include "ColorHandler.h"
24 #include "lyxserver.h"
25 #include "intl.h"
26 #include "lyx_main.h"
27 #include "lyx_cb.h"
28 #include "LyXAction.h"
29 #include "debug.h"
30 #include "lyxrc.h"
31 #include "lyxtext.h"
32 #include "gettext.h"
33 #include "Lsstream.h"
34 #include "trans_mgr.h"
35 #include "layout.h"
36 #include "WorkArea.h"
37 #include "bufferview_funcs.h"
38 #include "minibuffer.h"
39 #include "vspace.h"
40 #include "LyXView.h"
41 #include "lyx_gui_misc.h"
42 #include "FloatList.h"
43 #include "converter.h"
44 #include "exporter.h"
45 #include "importer.h"
46 #include "FontLoader.h"
47 #include "TextCache.h"
48 #include "lyxfind.h"
49 #include "undo_funcs.h"
50 #include "ParagraphParameters.h"
51 #include "figureForm.h"
52
53 #include "insets/inseturl.h"
54 #include "insets/insetlatexaccent.h"
55 #include "insets/insettoc.h"
56 #include "insets/insetref.h"
57 #include "insets/insetparent.h"
58 #include "insets/insetindex.h"
59 #include "insets/insetinclude.h"
60 #include "insets/insetbib.h"
61 #include "insets/insetcite.h"
62 #include "insets/insettext.h"
63 #include "insets/insetert.h"
64 #include "insets/insetexternal.h"
65 #include "insets/insetgraphics.h"
66 #include "insets/insetfoot.h"
67 #include "insets/insetmarginal.h"
68 #include "insets/insetminipage.h"
69 #include "insets/insetfloat.h"
70 #if 0
71 #include "insets/insetlist.h"
72 #include "insets/insettheorem.h"
73 #endif
74 #include "insets/insettabular.h"
75 #include "insets/insetcaption.h"
76
77 #include "mathed/formulamacro.h"
78 #include "mathed/math_cursor.h"
79 #include "mathed/math_inset.h"
80
81 #include "frontends/FileDialog.h"
82 #include "frontends/Dialogs.h"
83 #include "frontends/Toolbar.h"
84 #include "frontends/Menubar.h"
85 #include "frontends/Alert.h"
86
87 #include "support/lyxalgo.h"
88 #include "support/LAssert.h"
89 #include "support/filetools.h"
90 #include "support/FileInfo.h"
91 #include "support/syscall.h"
92 #include "support/lstrings.h"
93 #include "support/path.h"
94 #include "support/lyxfunctional.h"
95
96 #include <ctime>
97 #include <clocale>
98 #include <cstdlib>
99 #include <cctype>
100
101 #include <utility> 
102 #include <algorithm> 
103
104
105 using std::pair;
106 using std::make_pair; 
107 using std::endl;
108 using std::find_if;
109
110 extern BufferList bufferlist;
111 extern LyXServer * lyxserver;
112 extern bool selection_possible;
113 extern void MenuSendto();
114
115 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
116
117 extern void show_symbols_form(LyXFunc *);
118
119 extern LyXAction lyxaction;
120 // (alkis)
121 extern tex_accent_struct get_accent(kb_action action);
122
123 extern void ShowLatexLog();
124
125
126 #if 0
127
128 ///
129 class MiniBufferController : public SigC::Object {
130 public:
131         ///
132         MiniBufferController() {
133                 minibuffer
134                         .cmdReady
135                         .connect(slot(this,
136                                       &MiniBufferController::receiveCommand));
137                 minibuffer
138                         .argReady
139                         .connect(slot(this,
140                                       &MiniBufferController::receiveArg));
141         }
142         ///
143         void receiveCmd(string const & cmd) {}
144         ///
145         void receiveArg(string const & arg) {}
146         
147         
148 private:
149 };
150
151 namespace {
152
153 MiniBufferController mb_ctrl;
154
155 }
156 #endif
157
158
159 /* === globals =========================================================== */
160
161
162 LyXFunc::LyXFunc(LyXView * o)
163         : owner(o),
164         keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
165         cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get())
166 {
167         meta_fake_bit = 0;
168         lyx_dead_action = LFUN_NOACTION;
169         lyx_calling_dead_action = LFUN_NOACTION;
170 }
171
172
173 inline
174 LyXText * LyXFunc::TEXT(bool flag = true) const
175 {
176         if (flag)
177                 return owner->view()->text;
178         return owner->view()->getLyXText();
179 }
180
181
182 // I changed this func slightly. I commented out the ...FinishUndo(),
183 // this means that all places that used to have a moveCursorUpdate, now
184 // have a ...FinishUndo() as the preceeding statement. I have also added
185 // a moveCursorUpdate to some of the functions that updated the cursor, but
186 // that did not show its new position.
187 inline
188 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
189 {
190         if (selecting || TEXT(flag)->selection.mark()) {
191                 TEXT(flag)->setSelection(owner->view());
192                 if (TEXT(flag)->bv_owner)
193                     owner->view()->toggleToggle();
194         }
195         owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
196         owner->view()->showCursor();
197         
198         /* ---> Everytime the cursor is moved, show the current font state. */
199         // should this too me moved out of this func?
200         owner->view()->setState();
201 }
202
203
204 void LyXFunc::handleKeyFunc(kb_action action)
205 {
206         char c = keyseq.getiso();
207
208         if (keyseq.length() > 1) {
209                 c = 0;
210         }
211         
212         owner->getIntl()->getTrans()
213                 .deadkey(c, get_accent(action).accent, TEXT(false));
214         // Need to clear, in case the minibuffer calls these
215         // actions
216         keyseq.clear();
217         // copied verbatim from do_accent_char
218         owner->view()->update(TEXT(false),
219                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
220         TEXT(false)->selection.cursor = TEXT(false)->cursor;
221 }
222
223
224 void LyXFunc::processKeySym(KeySym keysym, unsigned int state) 
225 {
226         string argument;
227         
228         if (lyxerr.debugging(Debug::KEY)) {
229                 char const * tmp = XKeysymToString(keysym);
230                 string const stm = (tmp ? tmp : "");
231                 lyxerr << "KeySym is "
232                        << stm
233                        << "["
234                        << keysym << "] State is ["
235                        << state << "]"
236                        << endl;
237         }
238         // Do nothing if we have nothing (JMarc)
239         if (keysym == NoSymbol) {
240                 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
241                                    << endl;
242                 return;
243         }
244
245         // Can we be sure that this will work for all X Window
246         // implementations? (Lgb)
247         // This code snippet makes lyx ignore some keys. Perhaps
248         // all of them should be explictly mentioned?
249         if ((keysym >= XK_Shift_L && keysym <= XK_Hyper_R)
250             || keysym == XK_Mode_switch || keysym == 0x0) {
251                 return;
252         }
253         
254         // Do a one-deep top-level lookup for
255         // cancel and meta-fake keys. RVDK_PATCH_5
256         cancel_meta_seq.reset();
257
258         int action = cancel_meta_seq.addkey(keysym, state
259                                             &(ShiftMask|ControlMask
260                                               |Mod1Mask)); 
261         if (lyxerr.debugging(Debug::KEY)) {
262                 lyxerr << "action first set to [" << action << "]" << endl;
263         }
264         
265         // When not cancel or meta-fake, do the normal lookup. 
266         // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
267         // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
268         if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
269                 if (lyxerr.debugging(Debug::KEY)) {
270                         lyxerr << "meta_fake_bit is ["
271                                << meta_fake_bit << "]" << endl;
272                 }
273                 // remove Caps Lock and Mod2 as a modifiers
274                 action = keyseq.addkey(keysym,
275                                        (state | meta_fake_bit)
276                                        &(ShiftMask|ControlMask
277                                          |Mod1Mask));
278                 if (lyxerr.debugging(Debug::KEY)) {
279                         lyxerr << "action now set to ["
280                                << action << "]" << endl;
281                 }
282         }
283         // Dont remove this unless you know what you are doing.
284         meta_fake_bit = 0;
285                 
286         // can this happen now ? 
287         if (action == LFUN_NOACTION) {
288                 action = LFUN_PREFIX;
289         }
290
291         if (lyxerr.debugging(Debug::KEY)) {
292                 lyxerr << "Key [action="
293                        << action << "]["
294                        << keyseq.print() << "]"
295                        << endl;
296         }
297
298         // already here we know if it any point in going further
299         // why not return already here if action == -1 and
300         // num_bytes == 0? (Lgb)
301
302         if (keyseq.length() > 1 && !keyseq.deleted()) {
303                 owner->message(keyseq.print());
304         }
305
306         if (action == LFUN_UNKNOWN_ACTION) {
307                 // It is unknown, but what if we remove all
308                 // the modifiers? (Lgb)
309                 action = keyseq.addkey(keysym, 0);
310
311                 if (lyxerr.debugging(Debug::KEY)) {
312                         lyxerr << "Removing modifiers...\n"
313                                << "Action now set to ["
314                                << action << "]" << endl;
315                 }
316                 if (action == LFUN_UNKNOWN_ACTION) {
317                         owner->message(_("Unknown function."));
318                         return;
319                 }
320         }
321
322         if (action == LFUN_SELFINSERT) {
323                 // This is very X dependent.
324                 unsigned int c = keysym;
325                 string argument;
326                 
327                 c = kb_keymap::getiso(c);
328
329                 if (c > 0)
330                         argument = static_cast<char>(c);
331                 
332                 dispatch(LFUN_SELFINSERT, argument);
333                 lyxerr[Debug::KEY] << "SelfInsert arg[`"
334                                    << argument << "']" << endl;
335         }
336         else
337                 verboseDispatch(action, false);
338
339
340
341 FuncStatus LyXFunc::getStatus(int ac) const
342 {
343         kb_action action;
344         string argument;
345         action = lyxaction.retrieveActionArg(ac, argument);
346         return getStatus(action, argument);
347 }
348
349
350 FuncStatus LyXFunc::getStatus(kb_action action,
351                               string const & argument) const
352 {
353         FuncStatus flag;
354         Buffer * buf = owner->buffer();
355         
356         if (action == LFUN_UNKNOWN_ACTION) {
357                 setStatusMessage(N_("Unknown action"));
358                 return flag.unknown(true);
359         }
360         
361         // Check whether we need a buffer
362         if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
363                 // Yes we need a buffer, do we have one?
364                 if (buf) {
365                         // yes
366                         // Can we use a readonly buffer?
367                         if (buf->isReadonly() && 
368                             !lyxaction.funcHasFlag(action,
369                                                    LyXAction::ReadOnly)) {
370                                 // no
371                                 setStatusMessage(N_("Document is read-only"));
372                                 flag.disabled(true);
373                         }
374                 } else {
375                         // no
376                         setStatusMessage(N_("Command not allowed with"
377                                            "out any document open"));
378                         return flag.disabled(true);
379                 }
380         }
381
382         UpdatableInset * tli = owner->view()->theLockingInset();
383         
384         // I would really like to avoid having this switch and rather try to
385         // encode this in the function itself.
386         bool disable = false;
387         switch (action) {
388         case LFUN_MENUPRINT:
389                 disable = !Exporter::IsExportable(buf, "dvi")
390                         || lyxrc.print_command == "none";
391                 break;
392         case LFUN_EXPORT:
393                 disable = argument == "fax" &&
394                         !Exporter::IsExportable(buf, argument);
395                 break;
396         case LFUN_UNDO:
397                 disable = buf->undostack.empty();
398                 break;
399         case LFUN_REDO:
400                 disable = buf->redostack.empty();
401                 break;
402         case LFUN_SPELLCHECK:
403                 disable = lyxrc.isp_command == "none";
404                 break;
405 #ifndef HAVE_LIBAIKSAURUS
406         case LFUN_THESAURUS_ENTRY:
407                 disable = true;
408                 break;
409 #endif
410         case LFUN_RUNCHKTEX:
411                 disable = lyxrc.chktex_command == "none";
412                 break;
413         case LFUN_BUILDPROG:
414                 disable = !Exporter::IsExportable(buf, "program");
415                 break;
416
417         case LFUN_LAYOUT_TABULAR:
418                 disable = !tli
419                         || (tli->lyxCode() != Inset::TABULAR_CODE
420                             && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
421                 break;
422
423         case LFUN_LAYOUT_PARAGRAPH: {
424                 Inset * inset = TEXT(false)->cursor.par()->inInset();
425                 disable = inset && inset->forceDefaultParagraphs(inset);
426                 break;
427         }
428
429         case LFUN_TABULAR_FEATURE:
430                 disable = true;
431                 if (tli) {
432                         FuncStatus ret;
433                         //ret.disabled(true);
434                         if (tli->lyxCode() == Inset::TABULAR_CODE) {
435                                 ret = static_cast<InsetTabular *>(tli)
436                                         ->getStatus(argument);
437                         } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
438                                 ret = static_cast<InsetTabular *>
439                                         (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
440                                         ->getStatus(argument);
441                         }
442                         flag |= ret;
443                         disable = false;
444                 } else {
445                         static InsetTabular inset(*owner->buffer(), 1, 1);
446                         FuncStatus ret;
447
448                         disable = true;
449                         ret = inset.getStatus(argument);
450                         if (ret.onoff(true) || ret.onoff(false))
451                                 flag.setOnOff(false);
452                 }
453                 break;
454
455         case LFUN_VC_REGISTER:
456                 disable = buf->lyxvc.inUse();
457                 break;
458         case LFUN_VC_CHECKIN:
459                 disable = !buf->lyxvc.inUse() || buf->isReadonly();
460                 break;
461         case LFUN_VC_CHECKOUT:
462                 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
463                 break;
464         case LFUN_VC_REVERT:
465         case LFUN_VC_UNDO:
466         case LFUN_VC_HISTORY:
467                 disable = !buf->lyxvc.inUse();
468                 break;
469         case LFUN_BOOKMARK_GOTO:
470                 disable =  !owner->view()->
471                         isSavedPosition(strToUnsignedInt(argument));
472                 break;
473
474
475         case LFUN_INSET_TOGGLE:
476                 disable = (TEXT(false)->getInset() == 0);
477                 break;
478
479         case LFUN_MATH_VALIGN: 
480                 if (mathcursor) {
481                         char align = mathcursor->valign();
482                         if (align == '\0') {
483                                 disable = true;
484                                 break;
485                         }
486                         if (argument.empty()) {
487                                 flag.clear();
488                                 break;
489                         }
490                         if (!contains("tcb", argument[0])) {
491                                 disable = true;
492                                 break;
493                         }
494                         flag.setOnOff(argument[0] == align);
495                 } else
496                         disable = true;
497                 break;
498
499         case LFUN_MATH_HALIGN: 
500                 if (mathcursor) {
501                         char align = mathcursor->halign();
502                         if (align == '\0') {
503                                 disable = true;
504                                 break;
505                         }
506                         if (argument.empty()) {
507                                 flag.clear();
508                                 break;
509                         }
510                         if (!contains("lcr", argument[0])) {
511                                 disable = true;
512                                 break;
513                         }
514                         flag.setOnOff(argument[0] == align);
515                 } else
516                         disable = true;
517                 break;
518
519         case LFUN_MATH_MUTATE: 
520                 if (tli && (tli->lyxCode() == Inset::MATH_CODE)) {
521                         MathInsetTypes type = mathcursor->formula()->getType();
522                         if (argument == "inline") {
523                                 flag.setOnOff(type == LM_OT_SIMPLE);
524                         } else if (argument == "display") {
525                                 flag.setOnOff(type == LM_OT_EQUATION);
526                         } else if (argument == "eqnarray") {
527                                 flag.setOnOff(type == LM_OT_EQNARRAY);
528                         } else if (argument == "align") {
529                                 flag.setOnOff(type == LM_OT_ALIGN);
530                         } else {
531                                 disable = true;
532                         }
533                 } else
534                         disable = true;
535                 break;
536
537         // we just need to be in math mode to enable that
538         case LFUN_MATH_SIZE: 
539         case LFUN_MATH_SPACE:
540         case LFUN_MATH_LIMITS: 
541         case LFUN_MATH_NONUMBER: 
542         case LFUN_MATH_NUMBER:
543                 disable = !mathcursor;
544                 break;
545
546         // we need to be math mode and a math array for that
547         // Hack: halign produces non-zero result iff we are in a math array
548         case LFUN_MATH_ROW_INSERT: 
549         case LFUN_MATH_ROW_DELETE: 
550         case LFUN_MATH_COLUMN_INSERT: 
551         case LFUN_MATH_COLUMN_DELETE: 
552                 disable = !mathcursor || !mathcursor->halign();
553                 break;
554
555         default:
556                 break;
557         }
558
559         // the functions which insert insets
560         Inset::Code code = Inset::NO_CODE;
561         switch (action) {
562         case LFUN_INSET_TEXT:
563                 code = Inset::TEXT_CODE;
564                 break;
565         case LFUN_INSET_ERT:
566                 code = Inset::ERT_CODE;         
567                 break;
568         case LFUN_FIGURE:
569         case LFUN_INSET_GRAPHICS:
570                 code = Inset::GRAPHICS_CODE;
571                 break;
572         case LFUN_INSET_FOOTNOTE:
573                 code = Inset::FOOT_CODE;
574                 break;
575         case LFUN_DIALOG_TABULAR_INSERT:
576         case LFUN_INSET_TABULAR:
577                 code = Inset::TABULAR_CODE;
578                 break;
579         case LFUN_INSET_EXTERNAL:
580                 code = Inset::EXTERNAL_CODE;
581                 break;
582         case LFUN_INSET_MARGINAL:
583                 code = Inset::MARGIN_CODE;
584                 break;
585         case LFUN_INSET_MINIPAGE:
586                 code = Inset::MINIPAGE_CODE;
587                 break;
588         case LFUN_INSET_FLOAT:
589         case LFUN_INSET_WIDE_FLOAT:
590                 code = Inset::FLOAT_CODE;
591                 break;
592         case LFUN_FLOAT_LIST:
593                 code = Inset::FLOAT_LIST_CODE;
594                 break;
595 #if 0
596         case LFUN_INSET_LIST:
597                 code = Inset::LIST_CODE;
598                 break;
599         case LFUN_INSET_THEOREM:
600                 code = Inset::THEOREM_CODE;
601                 break;
602 #endif
603         case LFUN_INSET_CAPTION:
604                 code = Inset::CAPTION_CODE;
605                 break;
606         case LFUN_INSERT_NOTE:
607                 code = Inset::IGNORE_CODE;
608                 break;
609         case LFUN_INSERT_LABEL:
610                 code = Inset::LABEL_CODE;
611                 break;
612         case LFUN_REF_INSERT:
613                 code = Inset::REF_CODE;
614                 break;
615         case LFUN_CITATION_CREATE:
616         case LFUN_CITATION_INSERT:
617                 code = Inset::CITE_CODE;
618                 break;
619         case LFUN_INSERT_BIBTEX:
620                 code = Inset::BIBTEX_CODE;
621                 break;
622         case LFUN_INDEX_INSERT:
623         case LFUN_INDEX_INSERT_LAST:
624         case LFUN_INDEX_CREATE:
625                 code = Inset::INDEX_CODE;
626                 break;
627         case LFUN_INDEX_PRINT:
628                 code = Inset::INDEX_PRINT_CODE;
629                 break;
630         case LFUN_CHILD_INSERT:
631                 code = Inset::INCLUDE_CODE;
632                 break;
633         case LFUN_TOC_INSERT:
634                 code = Inset::TOC_CODE;
635                 break;
636         case LFUN_PARENTINSERT:
637                 code = Inset::PARENT_CODE;
638                 break;
639         case LFUN_HTMLURL:
640         case LFUN_URL:
641         case LFUN_INSERT_URL:
642                 code = Inset::URL_CODE;
643                 break;
644         case LFUN_QUOTE:
645                 // always allow this, since we will inset a raw quote
646                 // if an inset is not allowed.
647                 break;
648         case LFUN_HYPHENATION:
649         case LFUN_LIGATURE_BREAK:
650         case LFUN_HFILL:
651         case LFUN_MENU_SEPARATOR:
652         case LFUN_LDOTS:
653         case LFUN_END_OF_SENTENCE:
654         case LFUN_PROTECTEDSPACE:
655                 code = Inset::SPECIALCHAR_CODE;
656                 break;
657         default:
658                 break;
659         }
660         if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code)) {
661                 disable = true;
662         }
663
664         if (disable)
665                         flag.disabled(true);
666         
667         // A few general toggles
668         switch (action) {
669         case LFUN_READ_ONLY_TOGGLE:
670                 flag.setOnOff(buf->isReadonly());
671                 break;
672         case LFUN_APPENDIX:
673                 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
674                 break;
675         default:
676                 break;
677         }
678
679         // the font related toggles
680         if (!mathcursor) {
681                 LyXFont const & font = TEXT(false)->real_current_font;
682                 switch (action) {
683                 case LFUN_EMPH:
684                         flag.setOnOff(font.emph() == LyXFont::ON);
685                         break;
686                 case LFUN_NOUN:
687                         flag.setOnOff(font.noun() == LyXFont::ON);
688                         break;
689                 case LFUN_BOLD:
690                         flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
691                         break;
692                 case LFUN_SANS:
693                         flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
694                         break;
695                 case LFUN_ROMAN:
696                         flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
697                         break;
698                 case LFUN_CODE:
699                         flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
700                         break;
701                 default:
702                         break;
703                 }
704         }
705         else {
706                 MathTextCodes tc = mathcursor->getLastCode();
707                 switch (action) {
708                 case LFUN_BOLD:
709                         flag.setOnOff(tc == LM_TC_BF);
710                         break;
711                 case LFUN_SANS:
712                         flag.setOnOff(tc == LM_TC_SF);
713                         break;
714                 case LFUN_EMPH:
715                         flag.setOnOff(tc == LM_TC_CAL);
716                         break;
717                 case LFUN_ROMAN:
718                         flag.setOnOff(tc == LM_TC_RM);
719                         break;
720                 case LFUN_CODE:
721                         flag.setOnOff(tc == LM_TC_TT);
722                         break;
723                 case LFUN_NOUN:
724                         flag.setOnOff(tc == LM_TC_BB);
725                         break;
726                 case LFUN_DEFAULT:
727                         flag.setOnOff(tc == LM_TC_VAR);
728                         break;
729                 default:
730                         break;
731                 }
732         }
733         
734         return flag;
735 }
736
737
738 // temporary dispatch method
739 void LyXFunc::miniDispatch(string const & s) 
740 {
741         string s2(frontStrip(strip(s))); 
742  
743         if (!s2.empty()) {
744                 verboseDispatch(s2, true);
745         }
746 }
747
748
749 void LyXFunc::verboseDispatch(string const & s, bool show_sc) 
750 {
751         int action = lyxaction.LookupFunc(frontStrip(s));
752  
753         if (action == LFUN_UNKNOWN_ACTION) {
754                 string const msg = string(_("Unknown function ("))
755                         + s + ")";
756                 owner->message(msg);
757         } else {
758                 verboseDispatch(action, show_sc);
759         } 
760 }
761
762
763 void LyXFunc::verboseDispatch(int ac, bool show_sc) 
764 {
765         string argument;
766         kb_action action;
767         
768         // get the real action and argument
769         action = lyxaction.retrieveActionArg(ac, argument);
770
771         verboseDispatch(action, argument, show_sc);
772 }
773
774
775
776 void LyXFunc::verboseDispatch(kb_action action,
777                               string const & argument, bool show_sc)
778 {
779         string res = dispatch(action, argument);
780
781         commandshortcut.erase();
782         
783         if (lyxrc.display_shortcuts && show_sc) {
784                 if (action != LFUN_SELFINSERT) {
785                         // Put name of command and list of shortcuts
786                         // for it in minibuffer
787                         string comname = lyxaction.getActionName(action);
788                         
789                         int pseudoaction = action;
790                         bool argsadded = false;
791                         
792                         if (!argument.empty()) {
793                                 // the pseudoaction is useful for the bindings
794                                 pseudoaction = 
795                                         lyxaction.searchActionArg(action,
796                                                                   argument);
797
798                                 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
799                                         pseudoaction = action;
800                                 } else {
801                                         comname += " " + argument;
802                                         argsadded = true;
803                                 }
804                         }
805
806                         string const shortcuts =
807                                 toplevel_keymap->findbinding(pseudoaction);
808
809                         if (!shortcuts.empty()) {
810                                 comname += ": " + shortcuts;
811                         } else if (!argsadded && !argument.empty()) {
812                                 comname += " " + argument;
813                         }
814
815                         if (!comname.empty()) {
816                                 comname = strip(comname);
817                                 commandshortcut = "(" + comname + ')';
818                         }
819                 }
820         }
821
822         if (res.empty()) {
823                 if (!commandshortcut.empty()) {
824                         owner->getMiniBuffer()->addSet(commandshortcut);
825                 }
826         } else {
827                 owner->getMiniBuffer()->addSet(' ' + commandshortcut);
828         }
829 }
830
831
832 string const LyXFunc::dispatch(kb_action action, string argument)
833 {
834         lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << action
835                               <<"] arg[" << argument << "]" << endl;
836
837         // we have not done anything wrong yet.
838         errorstat = false;
839         dispatch_buffer.erase();
840
841 #ifdef NEW_DISPATCHER
842         // We try do call the most specific dispatcher first:
843         //  1. the lockinginset's dispatch
844         //  2. the bufferview's dispatch
845         //  3. the lyxview's dispatch
846 #endif
847
848         selection_possible = false;
849
850         if (owner->view()->available())
851                 owner->view()->hideCursor();
852
853         // We cannot use this function here
854         if (getStatus(action, argument).disabled()) {
855                 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: "
856                        << lyxaction.getActionName(action)
857                        << " [" << action << "] is disabled at this location"
858                        << endl;
859                 setErrorMessage(getStatusMessage());
860                 goto exit_with_message;
861         }
862
863         if (owner->view()->available() && owner->view()->theLockingInset()) {
864                 UpdatableInset::RESULT result;
865                 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
866                                      (!keyseq.deleted())))
867                 {
868                         if ((action == LFUN_UNKNOWN_ACTION)
869                             && argument.empty()){
870                                 argument = keyseq.getiso();
871                         }
872                         // Undo/Redo is a bit tricky for insets.
873                         if (action == LFUN_UNDO) {
874                                 owner->view()->menuUndo();
875                                 goto exit_with_message;
876                         } else if (action == LFUN_REDO) {
877                                 owner->view()->menuRedo();
878                                 goto exit_with_message;
879                         } else if (((result=owner->view()->theLockingInset()->
880                                      localDispatch(owner->view(), action, argument)) ==
881                                     UpdatableInset::DISPATCHED) ||
882                                    (result == UpdatableInset::DISPATCHED_NOUPDATE))
883                                 goto exit_with_message;
884                         else if (result == UpdatableInset::FINISHED) {
885                                         if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
886                                                 TEXT()->cursorRight(owner->view());
887                                                 moveCursorUpdate(true, false);
888                                                 owner->showState();
889                                         }
890                                         goto exit_with_message;
891                         } else if (result == UpdatableInset::FINISHED_RIGHT) {
892                                 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
893                                         TEXT()->cursorRight(owner->view());
894                                         moveCursorUpdate(true, false);
895                                         owner->showState();
896                                 }
897                                 goto exit_with_message;
898                         } else if (result == UpdatableInset::FINISHED_UP) {
899                                 if (TEXT()->cursor.row()->previous()) {
900                                         TEXT()->cursorUp(owner->view());
901                                         moveCursorUpdate(true, false);
902                                         owner->showState();
903                                 }
904                                 goto exit_with_message;
905                         } else if (result == UpdatableInset::FINISHED_DOWN) {
906                                 if (TEXT()->cursor.row()->next())
907                                         TEXT()->cursorDown(owner->view());
908                                 else
909                                         TEXT()->cursorRight(owner->view());
910                                 moveCursorUpdate(true, false);
911                                 owner->showState();
912                                 goto exit_with_message;
913                         } else {
914                                 //setMessage(N_("Text mode"));
915                                 switch (action) {
916                                 case LFUN_UNKNOWN_ACTION:
917                                 case LFUN_BREAKPARAGRAPH:
918                                 case LFUN_BREAKLINE:
919                                         TEXT()->cursorRight(owner->view());
920                                         owner->view()->setState();
921                                         owner->showState();
922                                         break;
923                                 case LFUN_RIGHT:
924                                         if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
925                                                 TEXT()->cursorRight(owner->view());
926                                                 moveCursorUpdate(true, false);
927                                                 owner->showState();
928                                         }
929                                         goto exit_with_message;
930                                 case LFUN_LEFT: 
931                                         if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
932                                                 TEXT()->cursorRight(owner->view());
933                                                 moveCursorUpdate(true, false);
934                                                 owner->showState();
935                                         }
936                                         goto exit_with_message;
937                                 case LFUN_DOWN:
938                                         if (TEXT()->cursor.row()->next())
939                                                 TEXT()->cursorDown(owner->view());
940                                         else
941                                                 TEXT()->cursorRight(owner->view());
942                                         moveCursorUpdate(true, false);
943                                         owner->showState();
944                                         goto exit_with_message;
945                                 default:
946                                         break;
947                                 }
948                         }
949                 }
950         }
951
952         switch (action) {
953                 
954         case LFUN_ESCAPE:
955         {
956                 if (!owner->view()->available()) break;
957                 // this function should be used always [asierra060396]
958                 UpdatableInset * tli =
959                         owner->view()->theLockingInset();
960                 if (tli) {
961                         UpdatableInset * lock = tli->getLockingInset();
962                         
963                         if (tli == lock) {
964                                 owner->view()->unlockInset(tli);
965                                 TEXT()->cursorRight(owner->view());
966                                 moveCursorUpdate(true, false);
967                                 owner->showState();
968                         } else {
969                                 tli->unlockInsetInInset(owner->view(),
970                                                         lock,
971                                                         true);
972                         }
973                         finishUndo();
974                 }
975         }
976         break;
977                         
978                 // --- Misc -------------------------------------------
979         case LFUN_WORDFINDFORWARD  : 
980         case LFUN_WORDFINDBACKWARD : {
981                 static string last_search;
982                 string searched_string;
983             
984                 if (!argument.empty()) {
985                         last_search = argument;
986                         searched_string = argument;
987                 } else {
988                         searched_string = last_search;
989                 }
990                 bool fw = (action == LFUN_WORDFINDBACKWARD);
991                 if (!searched_string.empty()) {
992                         LyXFind(owner->view(), searched_string, fw);
993                 }
994 //              owner->view()->showCursor();
995         }
996         break;
997                 
998         case LFUN_PREFIX:
999         {
1000                 if (owner->view()->available() && !owner->view()->theLockingInset()) {
1001                         owner->view()->update(TEXT(),
1002                                               BufferView::SELECT|BufferView::FITCUR);
1003                 }
1004                 owner->message(keyseq.print());
1005         }
1006         break;
1007
1008         // --- Misc -------------------------------------------
1009         case LFUN_EXEC_COMMAND:
1010         {
1011                 std::vector<string> allCmds;
1012                 std::transform(lyxaction.func_begin(), lyxaction.func_end(),
1013                                std::back_inserter(allCmds), lyx::firster());
1014                 static std::vector<string> hist;
1015                 owner->getMiniBuffer()->getString(MiniBuffer::spaces,
1016                                                   allCmds, hist);
1017         }
1018         break;
1019                 
1020         case LFUN_CANCEL:                   // RVDK_PATCH_5
1021                 keyseq.reset();
1022                 meta_fake_bit = 0;
1023                 if (owner->view()->available())
1024                         // cancel any selection
1025                         dispatch(LFUN_MARK_OFF);
1026                 setMessage(N_("Cancel"));
1027                 break;
1028
1029         case LFUN_META_FAKE:                                 // RVDK_PATCH_5
1030         {
1031                 meta_fake_bit = Mod1Mask;
1032                 setMessage(keyseq.print());
1033         }
1034         break;  
1035
1036         case LFUN_READ_ONLY_TOGGLE:
1037                 if (owner->buffer()->lyxvc.inUse()) {
1038                         owner->buffer()->lyxvc.toggleReadOnly();
1039                 } else {
1040                         owner->buffer()->setReadonly(
1041                                 !owner->buffer()->isReadonly());
1042                 }
1043                 break;
1044                 
1045         case LFUN_CENTER: // this is center and redraw.
1046                 owner->view()->center();
1047                 break;
1048                 
1049                 // --- Menus -----------------------------------------------
1050         case LFUN_MENUNEW:
1051                 menuNew(false);
1052                 break;
1053                 
1054         case LFUN_MENUNEWTMPLT:
1055                 menuNew(true);
1056                 break;
1057                 
1058         case LFUN_CLOSEBUFFER:
1059                 closeBuffer();
1060                 break;
1061                 
1062         case LFUN_MENUWRITE:
1063                 if (!owner->buffer()->isUnnamed()) {
1064                         ostringstream s1;
1065                         s1 << _("Saving document") << ' '
1066                            << MakeDisplayPath(owner->buffer()->fileName() + "...");
1067                         owner->message(s1.str().c_str());
1068                         MenuWrite(owner->view(), owner->buffer());
1069                 } else
1070                         WriteAs(owner->view(), owner->buffer());
1071                 break;
1072                 
1073         case LFUN_WRITEAS:
1074                 WriteAs(owner->view(), owner->buffer(), argument);
1075                 break;
1076                 
1077         case LFUN_MENURELOAD:
1078                 reloadBuffer();
1079                 break;
1080                 
1081         case LFUN_UPDATE:
1082                 Exporter::Export(owner->buffer(), argument, true);
1083                 break;
1084
1085         case LFUN_PREVIEW:
1086                 Exporter::Preview(owner->buffer(), argument);
1087                 break;
1088                 
1089         case LFUN_BUILDPROG:
1090                 Exporter::Export(owner->buffer(), "program", true);
1091                 break;
1092                 
1093         case LFUN_RUNCHKTEX:
1094                 MenuRunChktex(owner->buffer());
1095                 break;
1096                                 
1097         case LFUN_MENUPRINT:
1098                 owner->getDialogs()->showPrint();
1099                 break;
1100
1101         case LFUN_EXPORT:
1102                 if (argument == "custom")
1103                         MenuSendto();
1104                 else
1105                         Exporter::Export(owner->buffer(), argument, false);
1106                 break;
1107
1108         case LFUN_IMPORT:
1109                 doImport(argument);
1110                 break;
1111                 
1112         case LFUN_QUIT:
1113                 QuitLyX();
1114                 break;
1115                 
1116         case LFUN_TOCVIEW:
1117 #if 0
1118         case LFUN_LOFVIEW:
1119         case LFUN_LOTVIEW:
1120         case LFUN_LOAVIEW:
1121 #endif
1122         {
1123                 InsetCommandParams p;
1124
1125 #if 0
1126                 if (action == LFUN_TOCVIEW)
1127 #endif
1128                         p.setCmdName("tableofcontents");
1129 #if 0
1130                 else if (action == LFUN_LOAVIEW )
1131                         p.setCmdName("listof{algorithm}{List of Algorithms}");
1132                 else if (action == LFUN_LOFVIEW)
1133                         p.setCmdName("listoffigures");
1134                 else
1135                         p.setCmdName("listoftables");
1136 #endif
1137                 owner->getDialogs()->createTOC(p.getAsString());
1138                 break;
1139         }       
1140
1141         case LFUN_DIALOG_TABULAR_INSERT:
1142                 owner->getDialogs()->showTabularCreate();
1143                 break;
1144                 
1145         case LFUN_FIGURE:
1146                 Figure();
1147                 break;
1148
1149         case LFUN_AUTOSAVE:
1150                 AutoSave(owner->view());
1151                 break;
1152                 
1153         case LFUN_UNDO:
1154                 owner->view()->menuUndo();
1155                 break;
1156                 
1157         case LFUN_REDO:
1158                 owner->view()->menuRedo();
1159                 break;
1160                 
1161         case LFUN_MENUSEARCH:
1162                 owner->getDialogs()->showSearch();
1163                 break;
1164                 
1165         case LFUN_REMOVEERRORS:
1166                 if (owner->view()->removeAutoInsets()) {
1167                         owner->view()->redraw();
1168                         owner->view()->fitCursor();
1169                 }
1170                 break;
1171
1172         case LFUN_DEPTH:
1173                 changeDepth(owner->view(), TEXT(false), 0);
1174                 break;
1175                 
1176         case LFUN_DEPTH_MIN:
1177                 changeDepth(owner->view(), TEXT(false), -1);
1178                 break;
1179                 
1180         case LFUN_DEPTH_PLUS:
1181                 changeDepth(owner->view(), TEXT(false), 1);
1182                 break;
1183                 
1184         case LFUN_FREE:
1185                 owner->getDialogs()->setUserFreeFont();
1186                 break;
1187
1188         case LFUN_RECONFIGURE:
1189                 Reconfigure(owner->view());
1190                 break;
1191
1192 #if 0
1193         case LFUN_FLOATSOPERATE:
1194                 if (argument == "openfoot")
1195                         owner->view()->allFloats(1,0);
1196                 else if (argument == "closefoot")
1197                         owner->view()->allFloats(0,0);
1198                 else if (argument == "openfig")
1199                         owner->view()->allFloats(1,1);
1200                 else if (argument == "closefig")
1201                         owner->view()->allFloats(0,1);
1202                 break;
1203 #else
1204 #ifdef WITH_WARNINGS
1205 //#warning Find another implementation here (or another lyxfunc)!
1206 #endif
1207 #endif
1208         case LFUN_HELP_ABOUTLYX:
1209                 owner->getDialogs()->showAboutlyx();
1210                 break;
1211
1212         case LFUN_HELP_TEXINFO:
1213                 owner->getDialogs()->showTexinfo();
1214                 break;
1215
1216         case LFUN_HELP_OPEN:
1217         {
1218                 string const arg = argument;
1219                 if (arg.empty()) {
1220                         setErrorMessage(N_("Missing argument"));
1221                         break;
1222                 }
1223                 owner->prohibitInput();
1224                 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1225                 if (fname.empty()) {
1226                         lyxerr << "LyX: unable to find documentation file `"
1227                                << arg << "'. Bad installation?" << endl;
1228                         owner->allowInput();
1229                         break;
1230                 }
1231                 ostringstream str;
1232                 str << _("Opening help file") << ' '
1233                     << MakeDisplayPath(fname) << "...";
1234                 owner->message(str.str().c_str());
1235                 owner->view()->buffer(bufferlist.loadLyXFile(fname, false));
1236                 owner->allowInput();
1237                 break;
1238         }
1239
1240                 // --- version control -------------------------------
1241         case LFUN_VC_REGISTER:
1242         {
1243                 if (!owner->buffer()->lyxvc.inUse())
1244                         owner->buffer()->lyxvc.registrer();
1245         }
1246         break;
1247                 
1248         case LFUN_VC_CHECKIN:
1249         {
1250                 if (owner->buffer()->lyxvc.inUse()
1251                     && !owner->buffer()->isReadonly())
1252                         owner->buffer()->lyxvc.checkIn();
1253         }
1254         break;
1255                 
1256         case LFUN_VC_CHECKOUT:
1257         {
1258                 if (owner->buffer()->lyxvc.inUse()
1259                     && owner->buffer()->isReadonly())
1260                         owner->buffer()->lyxvc.checkOut();
1261         }
1262         break;
1263         
1264         case LFUN_VC_REVERT:
1265         {
1266                 owner->buffer()->lyxvc.revert();
1267         }
1268         break;
1269                 
1270         case LFUN_VC_UNDO:
1271         {
1272                 owner->buffer()->lyxvc.undoLast();
1273         }
1274         break;
1275                 
1276         case LFUN_VC_HISTORY:
1277         {
1278                 owner->getDialogs()->showVCLogFile();
1279                 break;
1280         }
1281         
1282         // --- buffers ----------------------------------------
1283
1284         case LFUN_SWITCHBUFFER:
1285                 owner->view()->buffer(bufferlist.getBuffer(argument));
1286                 break;
1287
1288         case LFUN_FILE_NEW:
1289         {
1290                 // servercmd: argument must be <file>:<template>
1291                 Buffer * tmpbuf = NewLyxFile(argument);
1292                 if (tmpbuf)
1293                         owner->view()->buffer(tmpbuf);
1294         }
1295         break;
1296                         
1297         case LFUN_FILE_OPEN:
1298                 open(argument);
1299                 break;
1300
1301         case LFUN_LATEX_LOG:
1302                 owner->getDialogs()->showLogFile();
1303                 break;
1304                 
1305         case LFUN_LAYOUT_DOCUMENT:
1306                 owner->getDialogs()->showDocument();
1307                 break;
1308                 
1309         case LFUN_LAYOUT_PARAGRAPH:
1310                 owner->getDialogs()->showParagraph();
1311                 break;
1312                 
1313         case LFUN_LAYOUT_CHARACTER:
1314                 owner->getDialogs()->showCharacter();
1315                 break;
1316
1317         case LFUN_LAYOUT_TABULAR:
1318             if (owner->view()->theLockingInset()) {
1319                 if (owner->view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1320                     InsetTabular * inset = static_cast<InsetTabular *>
1321                         (owner->view()->theLockingInset());
1322                     inset->openLayoutDialog(owner->view());
1323                 } else if (owner->view()->theLockingInset()->
1324                            getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1325                     InsetTabular * inset = static_cast<InsetTabular *>(
1326                         owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1327                     inset->openLayoutDialog(owner->view());
1328                 }
1329             }
1330             break;
1331
1332         case LFUN_LAYOUT_PREAMBLE:
1333                 owner->getDialogs()->showPreamble();
1334                 break;
1335                 
1336         case LFUN_DROP_LAYOUTS_CHOICE:
1337                 owner->getToolbar()->openLayoutList();
1338                 break;
1339
1340         case LFUN_MENU_OPEN_BY_NAME:
1341                 owner->getMenubar()->openByName(argument);
1342                 break; // RVDK_PATCH_5
1343                 
1344         case LFUN_SPELLCHECK:
1345                 if (lyxrc.isp_command != "none")
1346                         owner->getDialogs()->showSpellchecker();
1347                 break;
1348                 
1349         // --- lyxserver commands ----------------------------
1350
1351
1352         case LFUN_GETNAME:
1353                 setMessage(owner->buffer()->fileName());
1354                 lyxerr[Debug::INFO] << "FNAME["
1355                                << owner->buffer()->fileName()
1356                                << "] " << endl;
1357                 break;
1358                 
1359         case LFUN_NOTIFY:
1360         {
1361                 dispatch_buffer = keyseq.print();
1362                 lyxserver->notifyClient(dispatch_buffer);
1363         }
1364         break;
1365
1366         case LFUN_GOTOFILEROW:
1367         {
1368                 string file_name;
1369                 int row;
1370                 istringstream istr(argument.c_str());
1371                 istr >> file_name >> row;
1372                 // Must replace extension of the file to be .lyx and get full path
1373                 string const s(ChangeExtension(file_name, ".lyx"));
1374
1375                 // Either change buffer or load the file
1376                 if (bufferlist.exists(s)) {
1377                         owner->view()->buffer(bufferlist.getBuffer(s));
1378                 } else {
1379                         owner->view()->buffer(bufferlist.loadLyXFile(s));
1380                 }
1381                 
1382                 // Set the cursor  
1383                 owner->view()->setCursorFromRow(row);
1384
1385                 // Recenter screen
1386                 owner->view()->center();
1387         }
1388         break;
1389
1390         case LFUN_GOTO_PARAGRAPH:
1391         {
1392                 istringstream istr(argument.c_str());
1393
1394                 int id;
1395                 istr >> id;
1396                 Paragraph * par = owner->buffer()->getParFromID(id);
1397                 if (par == 0) {
1398                         lyxerr[Debug::INFO] << "No matching paragraph found! ["
1399                                             << id << "]" << std::endl;
1400                         break;
1401                 } else {
1402                         lyxerr << "Paragraph " << par->id()
1403                                << " found." << endl;
1404                 }
1405
1406                 if (owner->view()->theLockingInset())
1407                         owner->view()->unlockInset(owner->view()->theLockingInset());
1408                 if (par->inInset()) {
1409                         par->inInset()->edit(owner->view());
1410                 }
1411                 // Set the cursor
1412                 owner->view()->getLyXText()->setCursor(owner->view(), par, 0);
1413                 owner->view()->setState();
1414                 owner->showState();
1415
1416                 // Recenter screen
1417                 owner->view()->center();
1418         }
1419         break;
1420
1421         case LFUN_APROPOS:
1422         case LFUN_GETTIP:
1423         {
1424                 int const qa = lyxaction.LookupFunc(argument);
1425                 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1426         }
1427         break;
1428
1429         // --- toolbar ----------------------------------
1430         case LFUN_PUSH_TOOLBAR:
1431         {
1432                 int nth = strToInt(argument);
1433                 if (nth <= 0) {
1434                         setErrorMessage(N_("Push-toolbar needs argument > 0"));
1435                 } else {
1436                         owner->getToolbar()->push(nth);
1437                 }
1438         }
1439         break;
1440         
1441         case LFUN_ADD_TO_TOOLBAR:
1442         {
1443                 if (lyxerr.debugging(Debug::GUI)) {
1444                         lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1445                                 "argument = `" << argument << '\'' << endl;
1446                 }
1447                 string tmp(argument);
1448                 //lyxerr <<string("Argument: ") + argument);
1449                 //lyxerr <<string("Tmp     : ") + tmp);
1450                 if (tmp.empty()) {
1451                         setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1452                 } else {
1453                         owner->getToolbar()->add(argument, false);
1454                         owner->getToolbar()->set();
1455                 }
1456         }
1457         break;
1458         
1459         // --- insert characters ----------------------------------------
1460
1461         // ---  Mathed stuff. If we are here, there is no locked inset yet.
1462         case LFUN_MATH_EXTERN:
1463         case LFUN_MATH_NUMBER:
1464         case LFUN_MATH_NONUMBER:
1465         case LFUN_MATH_LIMITS:
1466         {
1467                 setErrorMessage(N_("This is only allowed in math mode!"));
1468         }
1469         break;
1470
1471         // passthrough hat and underscore outside mathed:
1472         case LFUN_SUBSCRIPT:
1473                 dispatch(LFUN_SELFINSERT, "_");
1474                 break;
1475         case LFUN_SUPERSCRIPT:
1476                 dispatch(LFUN_SELFINSERT, "^");
1477                 break;
1478
1479         case LFUN_MATH_PANEL:
1480                 owner->getDialogs()->showMathPanel();
1481                 break;
1482         
1483         case LFUN_CITATION_CREATE:
1484         {
1485                 InsetCommandParams p( "cite" );
1486                 
1487                 if (!argument.empty()) {
1488                         // This should be set at source, ie when typing
1489                         // "citation-insert foo" in the minibuffer.
1490                         // Question: would pybibliographer also need to be
1491                         // changed. Suspect so. Leave as-is therefore.
1492                         if (contains(argument, "|")) {
1493                                 p.setContents( token(argument, '|', 0) );
1494                                 p.setOptions(  token(argument, '|', 1) );
1495                         } else {
1496                                 p.setContents( argument );
1497                         }
1498                         dispatch(LFUN_CITATION_INSERT, p.getAsString());
1499                 } else
1500                         owner->getDialogs()->createCitation( p.getAsString() );
1501         }
1502         break;
1503                     
1504         case LFUN_CHILDOPEN:
1505         {
1506                 string const filename =
1507                         MakeAbsPath(argument, 
1508                                     owner->buffer()->filePath());
1509                 setMessage(N_("Opening child document ") +
1510                            MakeDisplayPath(filename) + "...");
1511                 owner->view()->savePosition(0);
1512                 if (bufferlist.exists(filename))
1513                         owner->view()->buffer(bufferlist.getBuffer(filename));
1514                 else
1515                         owner->view()->buffer(bufferlist.loadLyXFile(filename));
1516         }
1517         break;
1518
1519         case LFUN_TOGGLECURSORFOLLOW:
1520                 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1521                 break;
1522                 
1523         case LFUN_KMAP_OFF:             // keymap off
1524                 owner->getIntl()->KeyMapOn(false);
1525                 break;
1526                 
1527         case LFUN_KMAP_PRIM:    // primary keymap
1528                 owner->getIntl()->KeyMapPrim();
1529                 break;
1530                 
1531         case LFUN_KMAP_SEC:             // secondary keymap
1532                 owner->getIntl()->KeyMapSec();
1533                 break;
1534                 
1535         case LFUN_KMAP_TOGGLE:  // toggle keymap
1536                 owner->getIntl()->ToggleKeyMap();
1537                 break;
1538
1539         case LFUN_SEQUENCE: 
1540         {
1541                 // argument contains ';'-terminated commands
1542                 while (argument.find(';') != string::npos) {
1543                         string first;
1544                         argument = split(argument, first, ';');
1545                         verboseDispatch(first, false);
1546                 }
1547         }
1548         break;
1549
1550         case LFUN_DIALOG_PREFERENCES:
1551                 owner->getDialogs()->showPreferences();
1552                 break;
1553                 
1554         case LFUN_SAVEPREFERENCES:
1555         {
1556                 Path p(user_lyxdir);
1557                 lyxrc.write("preferences");
1558         }
1559         break;
1560
1561         case LFUN_SCREEN_FONT_UPDATE:
1562         {
1563                 // handle the screen font changes.
1564                 // 
1565                 lyxrc.set_font_norm_type();
1566                 fontloader.update();
1567                 // Of course we should only do the resize and the textcache.clear
1568                 // if values really changed...but not very important right now. (Lgb)
1569                 // All visible buffers will need resize
1570                 owner->resize();
1571                 // We also need to empty the textcache so that
1572                 // the buffer will be formatted correctly after
1573                 // a zoom change.
1574                 textcache.clear();
1575         }
1576         break;
1577
1578         case LFUN_SET_COLOR:
1579         {
1580                 string lyx_name;
1581                 string const x11_name = split(argument, lyx_name, ' ');
1582                 if (lyx_name.empty() || x11_name.empty()) {
1583                         setErrorMessage(N_("Syntax: set-color <lyx_name>"
1584                                                 " <x11_name>"));
1585                         break;
1586                         }
1587
1588                 if (!lcolor.setColor(lyx_name, x11_name)) {
1589                         static string const err1 (N_("Set-color \""));
1590                         static string const err2 (
1591                                 N_("\" failed - color is undefined "
1592                                    "or may not be redefined"));
1593                         setErrorMessage(_(err1) + lyx_name + _(err2));
1594                         break;
1595                 }
1596                 lyxColorHandler->updateColor(lcolor.getFromLyXName(lyx_name));
1597                 owner->view()->redraw();
1598                 break;
1599         }
1600
1601         case LFUN_MESSAGE:
1602                 owner->message(argument);
1603                 break;
1604
1605         case LFUN_MESSAGE_PUSH:
1606                 owner->messagePush(argument);
1607                 break;
1608
1609         case LFUN_MESSAGE_POP:
1610                 owner->messagePop();
1611                 break;
1612
1613         default:
1614                 // Then if it was none of the above
1615                 if (!owner->view()->Dispatch(action, argument))
1616                         lyxerr << "A truly unknown func ["
1617                                << lyxaction.getActionName(action) << "]!"
1618                                << endl;
1619                 break;
1620         } // end of switch
1621
1622 exit_with_message:
1623
1624         string const res = getMessage();
1625
1626         if (!res.empty())
1627                 owner->message(_(res));
1628         owner->updateMenubar();
1629         owner->updateToolbar();
1630         
1631         return res;
1632 }
1633
1634
1635 void LyXFunc::setupLocalKeymap()
1636 {
1637         keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1638         cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1639 }
1640
1641
1642 void LyXFunc::menuNew(bool fromTemplate)
1643 {
1644         string initpath = lyxrc.document_path;
1645
1646         if (owner->view()->available()) {
1647                 string const trypath = owner->buffer()->filePath();
1648                 // If directory is writeable, use this as default.
1649                 if (IsDirWriteable(trypath))
1650                         initpath = trypath;
1651         }
1652
1653         static int newfile_number;
1654         string s;
1655         
1656         if (lyxrc.new_ask_filename) {
1657                 FileDialog fileDlg(owner, _("Enter filename for new document"),
1658                                    LFUN_SELECT_FILE_SYNC,
1659                         make_pair(string(_("Documents|#o#O")),
1660                                   string(lyxrc.document_path)),
1661                         make_pair(string(_("Templates|#T#t")),
1662                                   string(lyxrc.template_path)));
1663
1664                 FileDialog::Result result =
1665                         fileDlg.Select(initpath,
1666                                        _("*.lyx|LyX Documents (*.lyx)"),
1667                                        _("newfile"));
1668         
1669                 if (result.second.empty()) {
1670                         owner->message(_("Canceled."));
1671                         lyxerr[Debug::INFO] << "New Document Cancelled." << endl;
1672                         return;
1673                 }
1674         
1675                 // get absolute path of file and make sure the filename ends
1676                 // with .lyx
1677                 s = MakeAbsPath(result.second);
1678                 if (!IsLyXFilename(s))
1679                         s += ".lyx";
1680
1681                 // Check if the document already is open
1682                 if (bufferlist.exists(s)) {
1683                         switch (Alert::askConfirmation(_("Document is already open:"),
1684                                                 MakeDisplayPath(s, 50),
1685                                                 _("Do you want to close that document now?\n"
1686                                                   "('No' will just switch to the open version)")))
1687                         {
1688                         case 1: // Yes: close the document
1689                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
1690                                 // If close is canceled, we cancel here too.
1691                                         return;
1692                                 break;
1693                         case 2: // No: switch to the open document
1694                                 owner->view()->buffer(bufferlist.getBuffer(s));
1695                                 return;
1696                         case 3: // Cancel: Do nothing
1697                                 owner->message(_("Canceled."));
1698                                 return;
1699                         }
1700                 }
1701                 // Check whether the file already exists
1702                 FileInfo fi(s);
1703                 if (fi.readable() &&
1704                     Alert::askQuestion(_("File already exists:"), 
1705                                 MakeDisplayPath(s, 50),
1706                                 _("Do you want to open the document?"))) {
1707                                 // loads document
1708                         string const disp_fn(MakeDisplayPath(s));
1709                         
1710                         ostringstream str;
1711                         str << _("Opening  document") << ' '
1712                             << disp_fn << "...";
1713                         
1714                         owner->message(str.str().c_str());
1715                         //XFlush(fl_get_display());
1716                         owner->view()->buffer(bufferlist.loadLyXFile(s));
1717                         ostringstream str2;
1718                         str2 << _("Document") << ' '
1719                              << disp_fn << ' ' << _("opened.");
1720                         
1721                         owner->message(str2.str().c_str());
1722                         
1723                         return;
1724                 }
1725         } else {
1726                 s = AddName(lyxrc.document_path,
1727                             "newfile" + tostr(++newfile_number) + ".lyx");
1728                 FileInfo fi(s);
1729                 while (bufferlist.exists(s) || fi.readable()) {
1730                         ++newfile_number;
1731                         s = AddName(lyxrc.document_path,
1732                                     "newfile" + tostr(newfile_number) +
1733                                     ".lyx");
1734                         fi.newFile(s);
1735                 }
1736         }
1737
1738         // The template stuff
1739         string templname;
1740         if (fromTemplate) {
1741                 FileDialog fileDlg(owner, _("Select template file"),
1742                         LFUN_SELECT_FILE_SYNC,
1743                         make_pair(string(_("Documents|#o#O")),
1744                                   string(lyxrc.document_path)),
1745                         make_pair(string(_("Templates|#T#t")),
1746                                   string(lyxrc.template_path)));
1747
1748                 FileDialog::Result result =
1749                         fileDlg.Select(lyxrc.template_path,
1750                                        _("*.lyx|LyX Documents (*.lyx)"));
1751         
1752                 if (result.first == FileDialog::Later)
1753                         return;
1754
1755                 string const fname = result.second;
1756
1757                 if (fname.empty()) 
1758                         return;
1759                 templname = fname;
1760         }
1761   
1762         // find a free buffer
1763         lyxerr[Debug::INFO] << "Find a free buffer." << endl;
1764         owner->view()->buffer(bufferlist.newFile(s, templname));
1765 }
1766
1767
1768 void LyXFunc::open(string const & fname)
1769 {
1770         string initpath = lyxrc.document_path;
1771   
1772         if (owner->view()->available()) {
1773                 string const trypath = owner->buffer()->filePath();
1774                 // If directory is writeable, use this as default.
1775                 if (IsDirWriteable(trypath))
1776                         initpath = trypath;
1777         }
1778
1779         string filename;
1780  
1781         if (fname.empty()) {
1782                 FileDialog fileDlg(owner, _("Select document to open"),
1783                         LFUN_FILE_OPEN,
1784                         make_pair(string(_("Documents|#o#O")),
1785                                   string(lyxrc.document_path)),
1786                         make_pair(string(_("Examples|#E#e")),
1787                                   string(AddPath(system_lyxdir, "examples"))));
1788
1789                 FileDialog::Result result =
1790                         fileDlg.Select(initpath,
1791                                        "*.lyx|LyX Documents (*.lyx)");
1792         
1793                 if (result.first == FileDialog::Later)
1794                         return;
1795
1796                 filename = result.second;
1797  
1798                 // check selected filename
1799                 if (filename.empty()) {
1800                         owner->message(_("Canceled."));
1801                         return;
1802                 }
1803         } else
1804                 filename = fname;
1805
1806         // get absolute path of file and add ".lyx" to the filename if
1807         // necessary
1808         string const fullpath = FileSearch(string(), filename, "lyx");
1809         if (fullpath.empty()) {
1810                 Alert::alert(_("Error"), _("Could not find file"), filename);
1811                 return;
1812         }
1813
1814         filename = fullpath;
1815  
1816         // loads document
1817         string const disp_fn(MakeDisplayPath(filename));
1818
1819         ostringstream str;
1820         str << _("Opening document") << ' ' << disp_fn << "...";
1821         
1822         owner->message(str.str().c_str());
1823
1824         Buffer * openbuf = bufferlist.loadLyXFile(filename);
1825         if (openbuf) {
1826                 owner->view()->buffer(openbuf);
1827                 ostringstream str;
1828                 str << _("Document") << ' ' << disp_fn << ' ' << _("opened.");
1829                 owner->message(str.str().c_str());
1830         } else {
1831                 ostringstream str;
1832                 str << _("Could not open document") << ' ' << disp_fn;
1833                 owner->message(str.str().c_str());
1834         }
1835 }
1836
1837
1838 // checks for running without gui are missing.
1839 void LyXFunc::doImport(string const & argument)
1840 {
1841         string format;
1842         string filename = split(argument, format, ' ');
1843         lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format 
1844                             << " file: " << filename << endl;
1845
1846         if (filename.empty()) { // need user interaction
1847                 string initpath = lyxrc.document_path;
1848                 
1849                 if (owner->view()->available()) {
1850                         string const trypath = owner->buffer()->filePath();
1851                         // If directory is writeable, use this as default.
1852                         if (IsDirWriteable(trypath))
1853                                 initpath = trypath;
1854                 }
1855
1856                 string const text = _("Select ") + formats.prettyName(format)
1857                         + _(" file to import");
1858
1859                 FileDialog fileDlg(owner, text, 
1860                         LFUN_IMPORT,
1861                         make_pair(string(_("Documents|#o#O")),
1862                                   string(lyxrc.document_path)),
1863                         make_pair(string(_("Examples|#E#e")),
1864                                   string(AddPath(system_lyxdir, "examples"))));
1865                         
1866                 string const extension = "*." + formats.extension(format)
1867                         + "| " + formats.prettyName(format)
1868                         + " (*." + formats.extension(format) + ")";
1869
1870                 FileDialog::Result result = fileDlg.Select(initpath,
1871                                                            extension);
1872
1873                 if (result.first == FileDialog::Later)
1874                         return;
1875
1876                 filename = result.second;
1877  
1878                 // check selected filename
1879                 if (filename.empty())
1880                         owner->message(_("Canceled."));
1881         }
1882
1883         // still no filename? abort
1884         if (filename.empty()) 
1885                 return;
1886
1887         // get absolute path of file
1888         filename = MakeAbsPath(filename);
1889
1890         string const lyxfile = ChangeExtension(filename, ".lyx");
1891
1892         // Check if the document already is open
1893         if (bufferlist.exists(lyxfile)) {
1894                 switch (Alert::askConfirmation(_("Document is already open:"), 
1895                                         MakeDisplayPath(lyxfile, 50),
1896                                         _("Do you want to close that document now?\n"
1897                                           "('No' will just switch to the open version)")))
1898                         {
1899                         case 1: // Yes: close the document
1900                                 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1901                                 // If close is canceled, we cancel here too.
1902                                         return;
1903                                 break;
1904                         case 2: // No: switch to the open document
1905                                 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1906                                 return;
1907                         case 3: // Cancel: Do nothing
1908                                 owner->message(_("Canceled."));
1909                                 return;
1910                         }
1911         }
1912
1913         // Check if a LyX document by the same root exists in filesystem
1914         FileInfo const f(lyxfile, true);
1915         if (f.exist() && !Alert::askQuestion(_("A document by the name"), 
1916                                       MakeDisplayPath(lyxfile),
1917                                       _("already exists. Overwrite?"))) {
1918                 owner->message(_("Canceled"));
1919                 return;
1920         }
1921         // filename should be valid now
1922         
1923         Importer::Import(owner, filename, format);
1924 }
1925
1926
1927 void LyXFunc::reloadBuffer()
1928 {
1929         string const fn = owner->buffer()->fileName();
1930         if (bufferlist.close(owner->buffer()))
1931                 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1932 }
1933
1934
1935 void LyXFunc::closeBuffer()
1936 {
1937         if (bufferlist.close(owner->buffer()) && !quitting) {
1938                 if (bufferlist.empty()) {
1939                         // need this otherwise SEGV may occur while trying to
1940                         // set variables that don't exist
1941                         // since there's no current buffer
1942                         owner->getDialogs()->hideBufferDependent();
1943                 } else {
1944                         owner->view()->buffer(bufferlist.first());
1945                 }
1946         }
1947 }
1948
1949
1950 // Each "owner" should have it's own message method. lyxview and
1951 // the minibuffer would use the minibuffer, but lyxserver would
1952 // send an ERROR signal to its client.  Alejandro 970603
1953 // This func is bit problematic when it comes to NLS, to make the
1954 // lyx servers client be language indepenent we must not translate
1955 // strings sent to this func.
1956 void LyXFunc::setErrorMessage(string const & m) const
1957 {
1958         dispatch_buffer = m;
1959         errorstat = true;
1960 }
1961
1962
1963 void LyXFunc::setMessage(string const & m) const 
1964 {
1965         dispatch_buffer = m;
1966 }
1967
1968
1969 void LyXFunc::setStatusMessage(string const & m) const
1970 {
1971         status_buffer = m;
1972 }
1973
1974
1975 void LyXFunc::initMiniBuffer() 
1976 {
1977         string text = _("Welcome to LyX!");
1978         
1979         // When meta-fake key is pressed, show the key sequence so far + "M-".
1980         if (wasMetaKey()) {
1981                 text = keyseq.print();
1982                 text += "M-";
1983         }
1984
1985         // Else, when a non-complete key sequence is pressed,
1986         // show the available options.
1987         if (keyseq.length() > 0 && !keyseq.deleted()) {
1988                 text = keyseq.printOptions();
1989         }
1990    
1991         // Else, show the buffer state.
1992         else if (owner->view()->available()) {
1993                 Buffer * tmpbuf = owner->buffer();
1994                 
1995                 string const nicename = 
1996                         MakeDisplayPath(tmpbuf->fileName());
1997                 // Should we do this instead? (kindo like emacs)
1998                 // leaves more room for other information
1999                 text = "LyX: ";
2000                 text += nicename;
2001                 if (tmpbuf->lyxvc.inUse()) {
2002                         text += " [";
2003                         text += tmpbuf->lyxvc.versionString();
2004                         text += ' ';
2005                         text += tmpbuf->lyxvc.locker();
2006                         if (tmpbuf->isReadonly())
2007                                 text += " (RO)";
2008                         text += ']';
2009                 } else if (tmpbuf->isReadonly())
2010                         text += " [RO]";
2011                 if (!tmpbuf->isLyxClean())
2012                         text += _(" (Changed)");
2013         } else {
2014                 if (text != _("Welcome to LyX!")) // this is a hack
2015                         text = _("* No document open *");
2016         }
2017         
2018         owner->message(text);
2019 }
2020