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