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