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