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