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