]> git.lyx.org Git - lyx.git/blob - src/lyxfunc.C
3a4545e67be54d866cf5fdc3798b435eee41e098
[lyx.git] / src / lyxfunc.C
1 /**
2  * \file lyxfunc.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Allan Rae
13  * \author Dekel Tsur
14  * \author Martin Vermeer
15  * \author Jürgen Vigna
16  *
17  * Full author contact details are available in file CREDITS.
18  */
19
20 #include <config.h>
21
22 #include "lyxfunc.h"
23
24 #include "BranchList.h"
25 #include "buffer.h"
26 #include "buffer_funcs.h"
27 #include "bufferlist.h"
28 #include "bufferparams.h"
29 #include "BufferView.h"
30 #include "cursor.h"
31 #include "debug.h"
32 #include "dispatchresult.h"
33 #include "encoding.h"
34 #include "exporter.h"
35 #include "format.h"
36 #include "funcrequest.h"
37 #include "gettext.h"
38 #include "importer.h"
39 #include "intl.h"
40 #include "iterators.h"
41 #include "kbmap.h"
42 #include "LColor.h"
43 #include "lyx_cb.h"
44 #include "LyXAction.h"
45 #include "lyxfind.h"
46 #include "lyxrc.h"
47 #include "lyxrow.h"
48 #include "lyxserver.h"
49 #include "lyxvc.h"
50 #include "paragraph.h"
51 #include "ParagraphParameters.h"
52 #include "undo.h"
53
54 #include "insets/insetbox.h"
55 #include "insets/insetbranch.h"
56 #include "insets/insetcommand.h"
57 #include "insets/insetert.h"
58 #include "insets/insetexternal.h"
59 #include "insets/insetfloat.h"
60 #include "insets/insetgraphics.h"
61 #include "insets/insetnote.h"
62 #include "insets/insettabular.h"
63 #include "insets/insetvspace.h"
64 #include "insets/insetwrap.h"
65
66 #include "frontends/Alert.h"
67 #include "frontends/Dialogs.h"
68 #include "frontends/FileDialog.h"
69 #include "frontends/lyx_gui.h"
70 #include "frontends/LyXKeySym.h"
71 #include "frontends/LyXView.h"
72 #include "frontends/Menubar.h"
73 #include "frontends/Toolbar.h"
74
75 #include "support/FileInfo.h"
76 #include "support/filetools.h"
77 #include "support/forkedcontr.h"
78 #include "support/globbing.h"
79 #include "support/path.h"
80 #include "support/path_defines.h"
81 #include "support/tostr.h"
82 #include "support/std_sstream.h"
83 #include "support/os.h"
84
85 using bv_funcs::freefont2string;
86
87 using lyx::support::AddName;
88 using lyx::support::AddPath;
89 using lyx::support::bformat;
90 using lyx::support::ChangeExtension;
91 using lyx::support::FileFilterList;
92 using lyx::support::FileInfo;
93 using lyx::support::FileSearch;
94 using lyx::support::ForkedcallsController;
95 using lyx::support::i18nLibFileSearch;
96 using lyx::support::IsDirWriteable;
97 using lyx::support::IsFileReadable;
98 using lyx::support::isStrInt;
99 using lyx::support::MakeAbsPath;
100 using lyx::support::MakeDisplayPath;
101 using lyx::support::Path;
102 using lyx::support::rtrim;
103 using lyx::support::split;
104 using lyx::support::strToInt;
105 using lyx::support::strToUnsignedInt;
106 using lyx::support::system_lyxdir;
107 using lyx::support::token;
108 using lyx::support::trim;
109 using lyx::support::user_lyxdir;
110 using lyx::support::prefixIs;
111 using lyx::support::os::getTmpDir;
112
113 using std::endl;
114 using std::make_pair;
115 using std::pair;
116 using std::string;
117 using std::istringstream;
118
119
120 extern BufferList bufferlist;
121 extern LyXServer * lyxserver;
122 extern bool selection_possible;
123
124 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
125
126 // (alkis)
127 extern tex_accent_struct get_accent(kb_action action);
128
129
130 LyXFunc::LyXFunc(LyXView * lv)
131         : owner(lv),
132         encoded_last_key(0),
133         keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
134         cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
135         meta_fake_bit(key_modifier::none)
136 {
137 }
138
139
140 void LyXFunc::handleKeyFunc(kb_action action)
141 {
142         char c = encoded_last_key;
143
144         if (keyseq.length()) {
145                 c = 0;
146         }
147
148         owner->getIntl().getTransManager()
149                 .deadkey(c, get_accent(action).accent, view()->getLyXText());
150         // Need to clear, in case the minibuffer calls these
151         // actions
152         keyseq.clear();
153         // copied verbatim from do_accent_char
154         view()->cursor().resetAnchor();
155         view()->update();
156 }
157
158
159 void LyXFunc::processKeySym(LyXKeySymPtr keysym, key_modifier::state state)
160 {
161         lyxerr[Debug::KEY] << "KeySym is " << keysym->getSymbolName() << endl;
162
163         // Do nothing if we have nothing (JMarc)
164         if (!keysym->isOK()) {
165                 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
166                                    << endl;
167                 return;
168         }
169
170         if (keysym->isModifier()) {
171                 lyxerr[Debug::KEY] << "isModifier true" << endl;
172                 return;
173         }
174
175         Encoding const * encoding = view()->cursor().getEncoding();
176
177         encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
178
179         // Do a one-deep top-level lookup for
180         // cancel and meta-fake keys. RVDK_PATCH_5
181         cancel_meta_seq.reset();
182
183         FuncRequest func = cancel_meta_seq.addkey(keysym, state);
184         lyxerr[Debug::KEY] << "action first set to [" << func.action << ']' << endl;
185
186         // When not cancel or meta-fake, do the normal lookup.
187         // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
188         // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
189         if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_FAKE)) {
190                 // remove Caps Lock and Mod2 as a modifiers
191                 func = keyseq.addkey(keysym, (state | meta_fake_bit));
192                 lyxerr[Debug::KEY] << "action now set to ["
193                         << func.action << ']' << endl;
194         }
195
196         // Dont remove this unless you know what you are doing.
197         meta_fake_bit = key_modifier::none;
198
199         // can this happen now ?
200         if (func.action == LFUN_NOACTION) {
201                 func = FuncRequest(LFUN_PREFIX);
202         }
203
204         if (lyxerr.debugging(Debug::KEY)) {
205                 lyxerr << "Key [action="
206                        << func.action << "]["
207                        << keyseq.print() << ']'
208                        << endl;
209         }
210
211         // already here we know if it any point in going further
212         // why not return already here if action == -1 and
213         // num_bytes == 0? (Lgb)
214
215         if (keyseq.length() > 1) {
216                 owner->message(keyseq.print());
217         }
218
219
220         // Maybe user can only reach the key via holding down shift.
221         // Let's see. But only if shift is the only modifier
222         if (func.action == LFUN_UNKNOWN_ACTION &&
223             state == key_modifier::shift) {
224                 lyxerr[Debug::KEY] << "Trying without shift" << endl;
225                 func = keyseq.addkey(keysym, key_modifier::none);
226                 lyxerr[Debug::KEY] << "Action now " << func.action << endl;
227         }
228
229         if (func.action == LFUN_UNKNOWN_ACTION) {
230                 // Hmm, we didn't match any of the keysequences. See
231                 // if it's normal insertable text not already covered
232                 // by a binding
233                 if (keysym->isText() && keyseq.length() == 1) {
234                         lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
235                         func = FuncRequest(LFUN_SELFINSERT);
236                 } else {
237                         lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
238                         owner->message(_("Unknown function."));
239                         return;
240                 }
241         }
242
243         if (func.action == LFUN_SELFINSERT) {
244                 if (encoded_last_key != 0) {
245                         string arg(1, encoded_last_key);
246                         dispatch(FuncRequest(LFUN_SELFINSERT, arg));
247                         lyxerr[Debug::KEY]
248                                 << "SelfInsert arg[`" << arg << "']" << endl;
249                 }
250         } else {
251                 dispatch(func);
252         }
253 }
254
255
256 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
257 {
258         //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
259         FuncStatus flag;
260         Buffer * buf = owner->buffer();
261         LCursor & cur = view()->cursor();
262
263         if (cmd.action == LFUN_NOACTION) {
264                 setStatusMessage(N_("Nothing to do"));
265                 flag.enabled(false);
266                 return flag;
267         }
268
269         switch (cmd.action) {
270         case LFUN_UNKNOWN_ACTION:
271 #ifndef HAVE_LIBAIKSAURUS
272         case LFUN_THESAURUS_ENTRY:
273 #endif
274                 flag.unknown(true);
275                 flag.enabled(false);
276                 break;
277         default:
278                 flag |= lyx_gui::getStatus(cmd);
279         }
280
281         if (flag.unknown()) {
282                 setStatusMessage(N_("Unknown action"));
283                 return flag;
284         }
285
286         // the default error message if we disable the command
287         setStatusMessage(N_("Command disabled"));
288
289         // Check whether we need a buffer
290         if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
291                 // Yes we need a buffer, do we have one?
292                 if (buf) {
293                         // yes
294                         // Can we use a readonly buffer?
295                         if (buf->isReadonly() &&
296                             !lyxaction.funcHasFlag(cmd.action,
297                                                    LyXAction::ReadOnly)) {
298                                 // no
299                                 setStatusMessage(N_("Document is read-only"));
300                                 flag.enabled(false);
301                         }
302                 } else {
303                         // no
304                         setStatusMessage(N_("Command not allowed with"
305                                             "out any document open"));
306                         flag.enabled(false);
307                         return flag;
308                 }
309         }
310
311         // I would really like to avoid having this switch and rather try to
312         // encode this in the function itself.
313         // -- And I'd rather let an inset decide which LFUNs it is willing
314         // to handle (Andre')
315         bool enable = true;
316         switch (cmd.action) {
317         case LFUN_TOOLTIPS_TOGGLE:
318                 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
319                 break;
320
321         case LFUN_READ_ONLY_TOGGLE:
322                 flag.setOnOff(buf->isReadonly());
323                 break;
324
325         case LFUN_SWITCHBUFFER:
326                 // toggle on the current buffer, but do not toggle off
327                 // the other ones (is that a good idea?)
328                 if (cmd.argument == buf->fileName())
329                         flag.setOnOff(true);
330                 break;
331
332         case LFUN_TRACK_CHANGES:
333                 flag.setOnOff(buf->params().tracking_changes);
334                 break;
335
336         case LFUN_EXPORT:
337                 enable = cmd.argument == "custom"
338                         || Exporter::IsExportable(*buf, cmd.argument);
339                 break;
340         case LFUN_UNDO:
341                 enable = !buf->undostack().empty();
342                 break;
343         case LFUN_REDO:
344                 enable = !buf->redostack().empty();
345                 break;
346         case LFUN_CUT:
347         case LFUN_COPY:
348                 enable = cur.selection();
349                 break;
350
351         case LFUN_RUNCHKTEX:
352                 enable = buf->isLatex() && lyxrc.chktex_command != "none";
353                 break;
354
355         case LFUN_BUILDPROG:
356                 enable = Exporter::IsExportable(*buf, "program");
357                 break;
358
359         case LFUN_LAYOUT_TABULAR:
360                 enable = cur.innerInsetOfType(InsetBase::TABULAR_CODE);
361                 break;
362
363         case LFUN_LAYOUT:
364         case LFUN_LAYOUT_PARAGRAPH:
365                 enable = !cur.inset().forceDefaultParagraphs(&cur.inset());
366                 break;
367
368         case LFUN_VC_REGISTER:
369                 enable = !buf->lyxvc().inUse();
370                 break;
371         case LFUN_VC_CHECKIN:
372                 enable = buf->lyxvc().inUse() && !buf->isReadonly();
373                 break;
374         case LFUN_VC_CHECKOUT:
375                 enable = buf->lyxvc().inUse() && buf->isReadonly();
376                 break;
377         case LFUN_VC_REVERT:
378         case LFUN_VC_UNDO:
379                 enable = buf->lyxvc().inUse();
380                 break;
381         case LFUN_MENURELOAD:
382                 enable = !buf->isUnnamed() && !buf->isClean();
383                 break;
384         case LFUN_BOOKMARK_GOTO:
385                 enable = view()->isSavedPosition(strToUnsignedInt(cmd.argument));
386                 break;
387
388         case LFUN_MERGE_CHANGES:
389         case LFUN_ACCEPT_CHANGE:
390         case LFUN_REJECT_CHANGE:
391         case LFUN_ACCEPT_ALL_CHANGES:
392         case LFUN_REJECT_ALL_CHANGES:
393                 enable = buf && buf->params().tracking_changes;
394                 break;
395
396         case LFUN_INSET_SETTINGS: {
397                 enable = false;
398                 if (!cur.size())
399                         break;
400                 UpdatableInset * inset = cur.inset().asUpdatableInset();
401                 lyxerr << "inset: " << inset << endl;
402                 if (!inset)
403                         break;
404
405                 // jump back to owner if an InsetText, so
406                 // we get back to the InsetTabular or whatever
407                 if (inset->lyxCode() == InsetOld::TEXT_CODE)
408                         inset = inset->owner();
409                 lyxerr << "inset 2: " << inset << endl;
410                 if (!inset)
411                         break;
412
413                 InsetOld::Code code = inset->lyxCode();
414                 switch (code) {
415                         case InsetOld::TABULAR_CODE:
416                                 enable = cmd.argument == "tabular";
417                                 break;
418                         case InsetOld::ERT_CODE:
419                                 enable = cmd.argument == "ert";
420                                 break;
421                         case InsetOld::FLOAT_CODE:
422                                 enable = cmd.argument == "float";
423                                 break;
424                         case InsetOld::WRAP_CODE:
425                                 enable = cmd.argument == "wrap";
426                                 break;
427                         case InsetOld::NOTE_CODE:
428                                 enable = cmd.argument == "note";
429                                 break;
430                         case InsetOld::BRANCH_CODE:
431                                 enable = cmd.argument == "branch";
432                                 break;
433                         case InsetOld::BOX_CODE:
434                                 enable = cmd.argument == "box";
435                                 break;
436                         default:
437                                 break;
438                 }
439                 break;
440         }
441
442         case LFUN_DIALOG_SHOW: {
443                 string const name = cmd.getArg(0);
444                 if (!buf)
445                         enable = name == "aboutlyx"
446                                 || name == "file"
447                                 || name == "preferences"
448                                 || name == "texinfo";
449                 else if (name == "print")
450                         enable = Exporter::IsExportable(*buf, "dvi")
451                                 && lyxrc.print_command != "none";
452                 else if (name == "character")
453                         enable = cur.inset().lyxCode() != InsetOld::ERT_CODE;
454                 else if (name == "vclog")
455                         enable = buf->lyxvc().inUse();
456                 else if (name == "latexlog")
457                         enable = IsFileReadable(buf->getLogName().second);
458                 break;
459         }
460
461         case LFUN_MENUNEW:
462         case LFUN_MENUNEWTMPLT:
463         case LFUN_WORDFINDFORWARD:
464         case LFUN_WORDFINDBACKWARD:
465         case LFUN_PREFIX:
466         case LFUN_EXEC_COMMAND:
467         case LFUN_CANCEL:
468         case LFUN_META_FAKE:
469         case LFUN_CLOSEBUFFER:
470         case LFUN_MENUWRITE:
471         case LFUN_WRITEAS:
472         case LFUN_UPDATE:
473         case LFUN_PREVIEW:
474         case LFUN_IMPORT:
475         case LFUN_QUIT:
476         case LFUN_TOCVIEW:
477         case LFUN_AUTOSAVE:
478         case LFUN_RECONFIGURE:
479         case LFUN_HELP_OPEN:
480         case LFUN_FILE_NEW:
481         case LFUN_FILE_OPEN:
482         case LFUN_DROP_LAYOUTS_CHOICE:
483         case LFUN_MENU_OPEN_BY_NAME:
484         case LFUN_GETNAME:
485         case LFUN_NOTIFY:
486         case LFUN_GOTOFILEROW:
487         case LFUN_GOTO_PARAGRAPH:
488         case LFUN_DIALOG_SHOW_NEW_INSET:
489         case LFUN_DIALOG_SHOW_NEXT_INSET:
490         case LFUN_DIALOG_UPDATE:
491         case LFUN_DIALOG_HIDE:
492         case LFUN_DIALOG_DISCONNECT_INSET:
493         case LFUN_CHILDOPEN:
494         case LFUN_TOGGLECURSORFOLLOW:
495         case LFUN_KMAP_OFF:
496         case LFUN_KMAP_PRIM:
497         case LFUN_KMAP_SEC:
498         case LFUN_KMAP_TOGGLE:
499         case LFUN_REPEAT:
500         case LFUN_SEQUENCE:
501         case LFUN_SAVEPREFERENCES:
502         case LFUN_SCREEN_FONT_UPDATE:
503         case LFUN_SET_COLOR:
504         case LFUN_MESSAGE:
505         case LFUN_EXTERNAL_EDIT:
506                 // these are handled in our dispatch()
507                 break;
508
509         default:
510                 cur.getStatus(cmd, flag);
511                 return flag;
512         }
513
514         flag.enabled(enable);
515         return flag;
516 }
517
518
519 namespace {
520
521 bool ensureBufferClean(BufferView * bv)
522 {
523         Buffer & buf = *bv->buffer();
524         if (buf.isClean())
525                 return true;
526
527         string const file = MakeDisplayPath(buf.fileName(), 30);
528         string text = bformat(_("The document %1$s has unsaved "
529                                 "changes.\n\nDo you want to save "
530                                 "the document?"), file);
531         int const ret = Alert::prompt(_("Save changed document?"),
532                                       text, 0, 1, _("&Save"),
533                                       _("&Cancel"));
534
535         if (ret == 0)
536                 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
537
538         return buf.isClean();
539 }
540
541 } //namespace anon
542
543
544 void LyXFunc::dispatch(FuncRequest const & cmd, bool verbose)
545 {
546         string const argument = cmd.argument;
547         kb_action const action = cmd.action;
548
549         lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << cmd << endl;
550         //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
551
552         // we have not done anything wrong yet.
553         errorstat = false;
554         dispatch_buffer.erase();
555         selection_possible = false;
556
557         // We cannot use this function here
558         if (!getStatus(cmd).enabled()) {
559                 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
560                        << lyxaction.getActionName(action)
561                        << " [" << action << "] is disabled at this location"
562                        << endl;
563                 setErrorMessage(getStatusMessage());
564
565         } else {
566
567                 if (view()->available())
568                         view()->hideCursor();
569
570                 switch (action) {
571
572                 case LFUN_WORDFINDFORWARD:
573                 case LFUN_WORDFINDBACKWARD: {
574                         static string last_search;
575                         string searched_string;
576
577                         if (!argument.empty()) {
578                                 last_search = argument;
579                                 searched_string = argument;
580                         } else
581                                 searched_string = last_search;
582
583                         if (searched_string.empty())
584                                 break;
585
586                         bool const fw = action == LFUN_WORDFINDFORWARD;
587                         string const data =
588                                 lyx::find::find2string(searched_string, true, false, fw);
589                         view()->dispatch(FuncRequest(LFUN_WORD_FIND, data));
590                         break;
591                 }
592
593                 case LFUN_PREFIX:
594                         owner->message(keyseq.printOptions());
595                         break;
596
597                 case LFUN_EXEC_COMMAND:
598                         owner->focus_command_buffer();
599                         break;
600
601                 case LFUN_CANCEL:
602                         keyseq.reset();
603                         meta_fake_bit = key_modifier::none;
604                         if (view()->available())
605                                 // cancel any selection
606                                 dispatch(FuncRequest(LFUN_MARK_OFF));
607                         setMessage(N_("Cancel"));
608                         break;
609
610                 case LFUN_META_FAKE:
611                         meta_fake_bit = key_modifier::alt;
612                         setMessage(keyseq.print());
613                         break;
614
615                 case LFUN_READ_ONLY_TOGGLE:
616                         if (owner->buffer()->lyxvc().inUse())
617                                 owner->buffer()->lyxvc().toggleReadOnly();
618                         else
619                                 owner->buffer()->setReadonly(
620                                         !owner->buffer()->isReadonly());
621                         break;
622
623                 // --- Menus -----------------------------------------------
624                 case LFUN_MENUNEW:
625                         menuNew(argument, false);
626                         break;
627
628                 case LFUN_MENUNEWTMPLT:
629                         menuNew(argument, true);
630                         break;
631
632                 case LFUN_CLOSEBUFFER:
633                         closeBuffer();
634                         break;
635
636                 case LFUN_MENUWRITE:
637                         if (!owner->buffer()->isUnnamed()) {
638                                 string const str = bformat(_("Saving document %1$s..."),
639                                          MakeDisplayPath(owner->buffer()->fileName()));
640                                 owner->message(str);
641                                 MenuWrite(owner->buffer());
642                                 owner->message(str + _(" done."));
643                         } else
644                                 WriteAs(owner->buffer());
645                         break;
646
647                 case LFUN_WRITEAS:
648                         WriteAs(owner->buffer(), argument);
649                         break;
650
651                 case LFUN_MENURELOAD: {
652                         string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
653                         string text = bformat(_("Any changes will be lost. Are you sure "
654                                 "you want to revert to the saved version of the document %1$s?"), file);
655                         int const ret = Alert::prompt(_("Revert to saved document?"),
656                                 text, 0, 1, _("&Revert"), _("&Cancel"));
657
658                         if (ret == 0)
659                                 view()->reload();
660                         break;
661                 }
662
663                 case LFUN_UPDATE:
664                         Exporter::Export(owner->buffer(), argument, true);
665                         view()->showErrorList(BufferFormat(*owner->buffer()));
666                         break;
667
668                 case LFUN_PREVIEW:
669                         Exporter::Preview(owner->buffer(), argument);
670                         view()->showErrorList(BufferFormat(*owner->buffer()));
671                         break;
672
673                 case LFUN_BUILDPROG:
674                         Exporter::Export(owner->buffer(), "program", true);
675                         view()->showErrorList(_("Build"));
676                         break;
677
678                 case LFUN_RUNCHKTEX:
679                         owner->buffer()->runChktex();
680                         view()->showErrorList(_("ChkTeX"));
681                         break;
682
683                 case LFUN_EXPORT:
684                         if (argument == "custom")
685                                 owner->getDialogs().showSendto();
686                         else {
687                                 Exporter::Export(owner->buffer(), argument, false);
688                                 view()->showErrorList(BufferFormat(*owner->buffer()));
689                         }
690                         break;
691
692                 case LFUN_IMPORT:
693                         doImport(argument);
694                         break;
695
696                 case LFUN_QUIT:
697                         QuitLyX();
698                         break;
699
700                 case LFUN_TOCVIEW: {
701                         InsetCommandParams p("tableofcontents");
702                         string const data = InsetCommandMailer::params2string("toc", p);
703                         owner->getDialogs().show("toc", data, 0);
704                         break;
705                 }
706
707                 case LFUN_AUTOSAVE:
708                         AutoSave(view());
709                         break;
710
711                 case LFUN_RECONFIGURE:
712                         Reconfigure(view());
713                         break;
714
715                 case LFUN_HELP_OPEN: {
716                         string const arg = argument;
717                         if (arg.empty()) {
718                                 setErrorMessage(N_("Missing argument"));
719                                 break;
720                         }
721                         string const fname = i18nLibFileSearch("doc", arg, "lyx");
722                         if (fname.empty()) {
723                                 lyxerr << "LyX: unable to find documentation file `"
724                                                          << arg << "'. Bad installation?" << endl;
725                                 break;
726                         }
727                         owner->message(bformat(_("Opening help file %1$s..."),
728                                 MakeDisplayPath(fname)));
729                         view()->loadLyXFile(fname, false);
730                         break;
731                 }
732
733                 // --- version control -------------------------------
734                 case LFUN_VC_REGISTER:
735                         if (!ensureBufferClean(view()))
736                                 break;
737                         if (!owner->buffer()->lyxvc().inUse()) {
738                                 owner->buffer()->lyxvc().registrer();
739                                 view()->reload();
740                         }
741                         break;
742
743                 case LFUN_VC_CHECKIN:
744                         if (!ensureBufferClean(view()))
745                                 break;
746                         if (owner->buffer()->lyxvc().inUse()
747                                         && !owner->buffer()->isReadonly()) {
748                                 owner->buffer()->lyxvc().checkIn();
749                                 view()->reload();
750                         }
751                         break;
752
753                 case LFUN_VC_CHECKOUT:
754                         if (!ensureBufferClean(view()))
755                                 break;
756                         if (owner->buffer()->lyxvc().inUse()
757                                         && owner->buffer()->isReadonly()) {
758                                 owner->buffer()->lyxvc().checkOut();
759                                 view()->reload();
760                         }
761                         break;
762
763                 case LFUN_VC_REVERT:
764                         owner->buffer()->lyxvc().revert();
765                         view()->reload();
766                         break;
767
768                 case LFUN_VC_UNDO:
769                         owner->buffer()->lyxvc().undoLast();
770                         view()->reload();
771                         break;
772
773                 // --- buffers ----------------------------------------
774                 case LFUN_SWITCHBUFFER:
775                         view()->setBuffer(bufferlist.getBuffer(argument));
776                         break;
777
778                 case LFUN_FILE_NEW:
779                         NewFile(view(), argument);
780                         break;
781
782                 case LFUN_FILE_OPEN:
783                         open(argument);
784                         break;
785
786                 case LFUN_DROP_LAYOUTS_CHOICE:
787                         owner->getToolbar().openLayoutList();
788                         break;
789
790                 case LFUN_MENU_OPEN_BY_NAME:
791                         owner->getMenubar().openByName(argument);
792                         break;
793
794                 // --- lyxserver commands ----------------------------
795                 case LFUN_GETNAME:
796                         setMessage(owner->buffer()->fileName());
797                         lyxerr[Debug::INFO] << "FNAME["
798                                                          << owner->buffer()->fileName()
799                                                          << "] " << endl;
800                         break;
801
802                 case LFUN_NOTIFY:
803                         dispatch_buffer = keyseq.print();
804                         lyxserver->notifyClient(dispatch_buffer);
805                         break;
806
807                 case LFUN_GOTOFILEROW: {
808                         string file_name;
809                         int row;
810                         istringstream is(argument);
811                         is >> file_name >> row;
812                         if (prefixIs(file_name, getTmpDir())) {
813                                 // Needed by inverse dvi search. If it is a file
814                                 // in tmpdir, call the apropriated function
815                                 view()->setBuffer(bufferlist.getBufferFromTmp(file_name));
816                         } else {
817                                 // Must replace extension of the file to be .lyx
818                                 // and get full path
819                                 string const s = ChangeExtension(file_name, ".lyx");
820                                 // Either change buffer or load the file
821                                 if (bufferlist.exists(s)) {
822                                         view()->setBuffer(bufferlist.getBuffer(s));
823                                 } else {
824                                         view()->loadLyXFile(s);
825                                 }
826                         }
827
828                         view()->setCursorFromRow(row);
829
830                         view()->center();
831                         // see BufferView_pimpl::center()
832                         view()->updateScrollbar();
833                         break;
834                 }
835
836                 case LFUN_GOTO_PARAGRAPH: {
837                         istringstream is(argument);
838                         int id;
839                         is >> id;
840                         ParIterator par = owner->buffer()->getParFromID(id);
841                         if (par == owner->buffer()->par_iterator_end()) {
842                                 lyxerr[Debug::INFO] << "No matching paragraph found! ["
843                                                                 << id << ']' << endl;
844                                 break;
845                         } else {
846                                 lyxerr[Debug::INFO] << "Paragraph " << par->id()
847                                                                 << " found." << endl;
848                         }
849
850                         // Set the cursor
851                         view()->setCursor(par, 0);
852
853                         view()->switchKeyMap();
854                         owner->view_state_changed();
855
856                         view()->center();
857                         // see BufferView_pimpl::center()
858                         view()->updateScrollbar();
859                         break;
860                 }
861
862                 case LFUN_DIALOG_SHOW: {
863                         string const name = cmd.getArg(0);
864                         string data = trim(cmd.argument.substr(name.size()));
865
866                         if (name == "character") {
867                                 data = freefont2string();
868                                 if (!data.empty())
869                                         owner->getDialogs().show("character", data);
870                         }
871                         else if (name == "document")
872                                 owner->getDialogs().showDocument();
873                         else if (name == "preamble")
874                                 owner->getDialogs().showPreamble();
875                         else if (name == "preferences")
876                                 owner->getDialogs().showPreferences();
877                         else if (name == "print")
878                                 owner->getDialogs().showPrint();
879                         else if (name == "spellchecker")
880                                 owner->getDialogs().showSpellchecker();
881
882                         else if (name == "latexlog") {
883                                 pair<Buffer::LogType, string> const logfile =
884                                         owner->buffer()->getLogName();
885                                 switch (logfile.first) {
886                                 case Buffer::latexlog:
887                                         data = "latex ";
888                                         break;
889                                 case Buffer::buildlog:
890                                         data = "literate ";
891                                         break;
892                                 }
893                                 data += logfile.second;
894                                 owner->getDialogs().show("log", data);
895                         }
896                         else if (name == "vclog") {
897                                 string const data = "vc " +
898                                         owner->buffer()->lyxvc().getLogFile();
899                                 owner->getDialogs().show("log", data);
900                         }
901                         else
902                                 owner->getDialogs().show(name, data);
903                         break;
904                 }
905
906                 case LFUN_DIALOG_SHOW_NEW_INSET: {
907                         string const name = cmd.getArg(0);
908                         string data = trim(cmd.argument.substr(name.size()));
909                         if (name == "bibitem" ||
910                             name == "bibtex" ||
911                             name == "include" ||
912                             name == "index" ||
913                             name == "label" ||
914                             name == "ref" ||
915                             name == "toc" ||
916                             name == "url") {
917                                 InsetCommandParams p(name);
918                                 data = InsetCommandMailer::params2string(name, p);
919                         } else if (name == "box") {
920                                 // \c data == "Boxed" || "Frameless" etc
921                                 InsetBoxParams p(data);
922                                 data = InsetBoxMailer::params2string(p);
923                         } else if (name == "branch") {
924                                 InsetBranchParams p;
925                                 data = InsetBranchMailer::params2string(p);
926                         } else if (name == "citation") {
927                                 InsetCommandParams p("cite");
928                                 data = InsetCommandMailer::params2string(name, p);
929                         } else if (name == "ert") {
930                                 data = InsetERTMailer::params2string(InsetCollapsable::Open);
931                         } else if (name == "external") {
932                                 InsetExternalParams p;
933                                 Buffer const & buffer = *owner->buffer();
934                                 data = InsetExternalMailer::params2string(p, buffer);
935                         } else if (name == "float") {
936                                 InsetFloatParams p;
937                                 data = InsetFloatMailer::params2string(p);
938                         } else if (name == "graphics") {
939                                 InsetGraphicsParams p;
940                                 Buffer const & buffer = *owner->buffer();
941                                 data = InsetGraphicsMailer::params2string(p, buffer);
942                         } else if (name == "note") {
943                                 InsetNoteParams p;
944                                 data = InsetNoteMailer::params2string(p);
945                         } else if (name == "vspace") {
946                                 VSpace space;
947                                 data = InsetVSpaceMailer::params2string(space);
948                         } else if (name == "wrap") {
949                                 InsetWrapParams p;
950                                 data = InsetWrapMailer::params2string(p);
951                         }
952                         owner->getDialogs().show(name, data, 0);
953                         break;
954                 }
955
956                 case LFUN_DIALOG_SHOW_NEXT_INSET:
957                         break;
958
959                 case LFUN_DIALOG_UPDATE: {
960                         string const & name = argument;
961                         // Can only update a dialog connected to an existing inset
962                         InsetBase * inset = owner->getDialogs().getOpenInset(name);
963                         if (inset) {
964                                 FuncRequest fr(LFUN_INSET_DIALOG_UPDATE, cmd.argument);
965                                 inset->dispatch(view()->cursor(), fr);
966                         } else if (name == "paragraph") {
967                                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
968                         }
969                         break;
970                 }
971
972                 case LFUN_DIALOG_HIDE:
973                         Dialogs::hide(argument, 0);
974                         break;
975
976                 case LFUN_DIALOG_DISCONNECT_INSET:
977                         owner->getDialogs().disconnect(argument);
978                         break;
979
980                 case LFUN_CHILDOPEN: {
981                         string const filename =
982                                 MakeAbsPath(argument, owner->buffer()->filePath());
983                         setMessage(N_("Opening child document ") +
984                                          MakeDisplayPath(filename) + "...");
985                         view()->savePosition(0);
986                         string const parentfilename = owner->buffer()->fileName();
987                         if (bufferlist.exists(filename))
988                                 view()->setBuffer(bufferlist.getBuffer(filename));
989                         else
990                                 view()->loadLyXFile(filename);
991                         // Set the parent name of the child document.
992                         // This makes insertion of citations and references in the child work,
993                         // when the target is in the parent or another child document.
994                         owner->buffer()->setParentName(parentfilename);
995                         break;
996                 }
997
998                 case LFUN_TOGGLECURSORFOLLOW:
999                         lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1000                         break;
1001
1002                 case LFUN_KMAP_OFF:
1003                         owner->getIntl().KeyMapOn(false);
1004                         break;
1005
1006                 case LFUN_KMAP_PRIM:
1007                         owner->getIntl().KeyMapPrim();
1008                         break;
1009
1010                 case LFUN_KMAP_SEC:
1011                         owner->getIntl().KeyMapSec();
1012                         break;
1013
1014                 case LFUN_KMAP_TOGGLE:
1015                         owner->getIntl().ToggleKeyMap();
1016                         break;
1017
1018                 case LFUN_REPEAT: {
1019                         // repeat command
1020                         string countstr;
1021                         string rest = split(argument, countstr, ' ');
1022                         istringstream is(countstr);
1023                         int count = 0;
1024                         is >> count;
1025                         lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1026                         for (int i = 0; i < count; ++i)
1027                                 dispatch(lyxaction.lookupFunc(rest));
1028                         break;
1029                 }
1030
1031                 case LFUN_SEQUENCE:
1032                         // argument contains ';'-terminated commands
1033                         while (!argument.empty()) {
1034                                 string first;
1035                                 string rest = split(argument, first, ';');
1036                                 dispatch(lyxaction.lookupFunc(rest));
1037                         }
1038                         break;
1039
1040                 case LFUN_SAVEPREFERENCES: {
1041                         Path p(user_lyxdir());
1042                         lyxrc.write("preferences");
1043                         break;
1044                 }
1045
1046                 case LFUN_SCREEN_FONT_UPDATE:
1047                         // handle the screen font changes.
1048                         lyxrc.set_font_norm_type();
1049                         lyx_gui::update_fonts();
1050                         // All visible buffers will need resize
1051                         view()->resize();
1052                         break;
1053
1054                 case LFUN_SET_COLOR: {
1055                         string lyx_name;
1056                         string const x11_name = split(argument, lyx_name, ' ');
1057                         if (lyx_name.empty() || x11_name.empty()) {
1058                                 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1059                                                         " <x11_name>"));
1060                                 break;
1061                         }
1062
1063                         bool const graphicsbg_changed =
1064                                 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1065                                  x11_name != lcolor.getX11Name(LColor::graphicsbg));
1066
1067                         LColor::color col = lcolor.getFromLyXName(lyx_name);
1068                         if (!lcolor.setColor(col, x11_name)) {
1069                                 setErrorMessage(
1070                                         bformat(_("Set-color \"%1$s\" failed "
1071                                                                 "- color is undefined or "
1072                                                                 "may not be redefined"), lyx_name));
1073                                 break;
1074                         }
1075
1076                         lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1077
1078                         if (graphicsbg_changed) {
1079 #ifdef WITH_WARNINGS
1080 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1081 #endif
1082 #if 0
1083                                 lyx::graphics::GCache::get().changeDisplay(true);
1084 #endif
1085                         }
1086                         break;
1087                 }
1088
1089                 case LFUN_MESSAGE:
1090                         owner->message(argument);
1091                         break;
1092
1093                 case LFUN_TOOLTIPS_TOGGLE:
1094                         owner->getDialogs().toggleTooltips();
1095                         break;
1096
1097                 case LFUN_EXTERNAL_EDIT: {
1098                         FuncRequest fr(action, argument);
1099                         InsetExternal().dispatch(view()->cursor(), fr);
1100                         break;
1101                 }
1102
1103                 default: {
1104                         DispatchResult res = view()->cursor().dispatch(cmd);
1105                         if (!res.dispatched());
1106                                 view()->dispatch(cmd);
1107                         break;
1108                 }
1109                 }
1110         }
1111
1112         if (view()->available()) {
1113                 view()->fitCursor();
1114                 view()->update();
1115                 view()->cursor().updatePos();
1116                 // if we executed a mutating lfun, mark the buffer as dirty
1117                 if (getStatus(cmd).enabled()
1118                     && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)
1119                     && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly))
1120                         view()->buffer()->markDirty();
1121         }
1122
1123         if (view()->cursor().inTexted()) {
1124                 view()->owner()->updateLayoutChoice();
1125                 sendDispatchMessage(getMessage(), cmd, verbose);
1126         }
1127 }
1128
1129
1130 void LyXFunc::sendDispatchMessage(string const & msg,
1131                                   FuncRequest const & cmd, bool verbose)
1132 {
1133         owner->updateMenubar();
1134         owner->updateToolbar();
1135
1136         if (cmd.action == LFUN_SELFINSERT || !verbose) {
1137                 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1138                 if (!msg.empty())
1139                         owner->message(msg);
1140                 return;
1141         }
1142
1143         string dispatch_msg = msg;
1144         if (!dispatch_msg.empty())
1145                 dispatch_msg += ' ';
1146
1147         string comname = lyxaction.getActionName(cmd.action);
1148
1149         bool argsadded = false;
1150
1151         if (!cmd.argument.empty()) {
1152                 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1153                         comname += ' ' + cmd.argument;
1154                         argsadded = true;
1155                 }
1156         }
1157
1158         string const shortcuts = toplevel_keymap->findbinding(cmd);
1159
1160         if (!shortcuts.empty()) {
1161                 comname += ": " + shortcuts;
1162         } else if (!argsadded && !cmd.argument.empty()) {
1163                 comname += ' ' + cmd.argument;
1164         }
1165
1166         if (!comname.empty()) {
1167                 comname = rtrim(comname);
1168                 dispatch_msg += '(' + comname + ')';
1169         }
1170
1171         lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1172         if (!dispatch_msg.empty())
1173                 owner->message(dispatch_msg);
1174 }
1175
1176
1177 void LyXFunc::setupLocalKeymap()
1178 {
1179         keyseq.stdmap = toplevel_keymap.get();
1180         keyseq.curmap = toplevel_keymap.get();
1181         cancel_meta_seq.stdmap = toplevel_keymap.get();
1182         cancel_meta_seq.curmap = toplevel_keymap.get();
1183 }
1184
1185
1186 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1187 {
1188         string initpath = lyxrc.document_path;
1189         string filename(name);
1190
1191         if (view()->available()) {
1192                 string const trypath = owner->buffer()->filePath();
1193                 // If directory is writeable, use this as default.
1194                 if (IsDirWriteable(trypath))
1195                         initpath = trypath;
1196         }
1197
1198         static int newfile_number;
1199
1200         if (filename.empty()) {
1201                 filename = AddName(lyxrc.document_path,
1202                             "newfile" + tostr(++newfile_number) + ".lyx");
1203                 FileInfo fi(filename);
1204                 while (bufferlist.exists(filename) || fi.readable()) {
1205                         ++newfile_number;
1206                         filename = AddName(lyxrc.document_path,
1207                                     "newfile" + tostr(newfile_number) +
1208                                     ".lyx");
1209                         fi.newFile(filename);
1210                 }
1211         }
1212
1213         // The template stuff
1214         string templname;
1215         if (fromTemplate) {
1216                 FileDialog fileDlg(_("Select template file"),
1217                         LFUN_SELECT_FILE_SYNC,
1218                         make_pair(string(_("Documents|#o#O")),
1219                                   string(lyxrc.document_path)),
1220                         make_pair(string(_("Templates|#T#t")),
1221                                   string(lyxrc.template_path)));
1222
1223                 FileDialog::Result result =
1224                         fileDlg.open(lyxrc.template_path,
1225                                      FileFilterList(_("LyX Documents (*.lyx)")),
1226                                      string());
1227
1228                 if (result.first == FileDialog::Later)
1229                         return;
1230                 if (result.second.empty())
1231                         return;
1232                 templname = result.second;
1233         }
1234
1235         view()->newFile(filename, templname, !name.empty());
1236 }
1237
1238
1239 void LyXFunc::open(string const & fname)
1240 {
1241         string initpath = lyxrc.document_path;
1242
1243         if (view()->available()) {
1244                 string const trypath = owner->buffer()->filePath();
1245                 // If directory is writeable, use this as default.
1246                 if (IsDirWriteable(trypath))
1247                         initpath = trypath;
1248         }
1249
1250         string filename;
1251
1252         if (fname.empty()) {
1253                 FileDialog fileDlg(_("Select document to open"),
1254                         LFUN_FILE_OPEN,
1255                         make_pair(string(_("Documents|#o#O")),
1256                                   string(lyxrc.document_path)),
1257                         make_pair(string(_("Examples|#E#e")),
1258                                   string(AddPath(system_lyxdir(), "examples"))));
1259
1260                 FileDialog::Result result =
1261                         fileDlg.open(initpath,
1262                                      FileFilterList(_("LyX Documents (*.lyx)")),
1263                                      string());
1264
1265                 if (result.first == FileDialog::Later)
1266                         return;
1267
1268                 filename = result.second;
1269
1270                 // check selected filename
1271                 if (filename.empty()) {
1272                         owner->message(_("Canceled."));
1273                         return;
1274                 }
1275         } else
1276                 filename = fname;
1277
1278         // get absolute path of file and add ".lyx" to the filename if
1279         // necessary
1280         string const fullpath = FileSearch(string(), filename, "lyx");
1281         if (!fullpath.empty()) {
1282                 filename = fullpath;
1283         }
1284
1285         string const disp_fn(MakeDisplayPath(filename));
1286
1287         // if the file doesn't exist, let the user create one
1288         FileInfo const f(filename, true);
1289         if (!f.exist()) {
1290                 // the user specifically chose this name. Believe them.
1291                 view()->newFile(filename, "", true);
1292                 return;
1293         }
1294
1295         owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1296
1297         string str2;
1298         if (view()->loadLyXFile(filename)) {
1299                 str2 = bformat(_("Document %1$s opened."), disp_fn);
1300         } else {
1301                 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1302         }
1303         owner->message(str2);
1304 }
1305
1306
1307 void LyXFunc::doImport(string const & argument)
1308 {
1309         string format;
1310         string filename = split(argument, format, ' ');
1311
1312         lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1313                             << " file: " << filename << endl;
1314
1315         // need user interaction
1316         if (filename.empty()) {
1317                 string initpath = lyxrc.document_path;
1318
1319                 if (view()->available()) {
1320                         string const trypath = owner->buffer()->filePath();
1321                         // If directory is writeable, use this as default.
1322                         if (IsDirWriteable(trypath))
1323                                 initpath = trypath;
1324                 }
1325
1326                 string const text = bformat(_("Select %1$s file to import"),
1327                         formats.prettyName(format));
1328
1329                 FileDialog fileDlg(text,
1330                         LFUN_IMPORT,
1331                         make_pair(string(_("Documents|#o#O")),
1332                                   string(lyxrc.document_path)),
1333                         make_pair(string(_("Examples|#E#e")),
1334                                   string(AddPath(system_lyxdir(), "examples"))));
1335
1336                 string const filter = formats.prettyName(format)
1337                         + " (*." + formats.extension(format) + ')';
1338
1339                 FileDialog::Result result =
1340                         fileDlg.open(initpath,
1341                                      FileFilterList(filter),
1342                                      string());
1343
1344                 if (result.first == FileDialog::Later)
1345                         return;
1346
1347                 filename = result.second;
1348
1349                 // check selected filename
1350                 if (filename.empty())
1351                         owner->message(_("Canceled."));
1352         }
1353
1354         if (filename.empty())
1355                 return;
1356
1357         // get absolute path of file
1358         filename = MakeAbsPath(filename);
1359
1360         string const lyxfile = ChangeExtension(filename, ".lyx");
1361
1362         // Check if the document already is open
1363         if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1364                 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1365                         owner->message(_("Canceled."));
1366                         return;
1367                 }
1368         }
1369
1370         // if the file exists already, and we didn't do
1371         // -i lyx thefile.lyx, warn
1372         if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1373                 string const file = MakeDisplayPath(lyxfile, 30);
1374
1375                 string text = bformat(_("The document %1$s already exists.\n\n"
1376                         "Do you want to over-write that document?"), file);
1377                 int const ret = Alert::prompt(_("Over-write document?"),
1378                         text, 0, 1, _("&Over-write"), _("&Cancel"));
1379
1380                 if (ret == 1) {
1381                         owner->message(_("Canceled."));
1382                         return;
1383                 }
1384         }
1385
1386         Importer::Import(owner, filename, format);
1387 }
1388
1389
1390 void LyXFunc::closeBuffer()
1391 {
1392         if (bufferlist.close(owner->buffer(), true) && !quitting) {
1393                 if (bufferlist.empty()) {
1394                         // need this otherwise SEGV may occur while
1395                         // trying to set variables that don't exist
1396                         // since there's no current buffer
1397                         owner->getDialogs().hideBufferDependent();
1398                 } else {
1399                         view()->setBuffer(bufferlist.first());
1400                 }
1401         }
1402 }
1403
1404
1405 // Each "owner" should have it's own message method. lyxview and
1406 // the minibuffer would use the minibuffer, but lyxserver would
1407 // send an ERROR signal to its client.  Alejandro 970603
1408 // This function is bit problematic when it comes to NLS, to make the
1409 // lyx servers client be language indepenent we must not translate
1410 // strings sent to this func.
1411 void LyXFunc::setErrorMessage(string const & m) const
1412 {
1413         dispatch_buffer = m;
1414         errorstat = true;
1415 }
1416
1417
1418 void LyXFunc::setMessage(string const & m) const
1419 {
1420         dispatch_buffer = m;
1421 }
1422
1423
1424 void LyXFunc::setStatusMessage(string const & m) const
1425 {
1426         status_buffer = m;
1427 }
1428
1429
1430 string const LyXFunc::viewStatusMessage()
1431 {
1432         // When meta-fake key is pressed, show the key sequence so far + "M-".
1433         if (wasMetaKey())
1434                 return keyseq.print() + "M-";
1435
1436         // Else, when a non-complete key sequence is pressed,
1437         // show the available options.
1438         if (keyseq.length() > 0 && !keyseq.deleted())
1439                 return keyseq.printOptions();
1440
1441         if (!view()->available())
1442                 return _("Welcome to LyX!");
1443
1444         return view()->cursor().currentState();
1445 }
1446
1447
1448 BufferView * LyXFunc::view() const
1449 {
1450         BOOST_ASSERT(owner);
1451         return owner->view().get();
1452 }
1453
1454
1455 bool LyXFunc::wasMetaKey() const
1456 {
1457         return (meta_fake_bit != key_modifier::none);
1458 }