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