3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
14 * \author Martin Vermeer
15 * \author Jürgen Vigna
17 * Full author contact details are available in file CREDITS.
24 #include "BranchList.h"
26 #include "buffer_funcs.h"
27 #include "bufferlist.h"
28 #include "bufferparams.h"
29 #include "BufferView.h"
32 #include "dispatchresult.h"
36 #include "funcrequest.h"
40 #include "iterators.h"
44 #include "LyXAction.h"
48 #include "lyxserver.h"
50 #include "paragraph.h"
51 #include "ParagraphParameters.h"
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"
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"
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"
86 using bv_funcs::freefont2string;
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;
118 using std::make_pair;
121 using std::istringstream;
124 extern BufferList bufferlist;
125 extern LyXServer * lyxserver;
126 extern bool selection_possible;
128 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
131 extern tex_accent_struct get_accent(kb_action action);
134 LyXFunc::LyXFunc(LyXView * lv)
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)
144 void LyXFunc::handleKeyFunc(kb_action action)
146 char c = encoded_last_key;
148 if (keyseq.length()) {
152 owner->getIntl().getTransManager()
153 .deadkey(c, get_accent(action).accent, view()->getLyXText());
154 // Need to clear, in case the minibuffer calls these
157 // copied verbatim from do_accent_char
158 view()->cursor().resetAnchor();
163 void LyXFunc::processKeySym(LyXKeySymPtr keysym, key_modifier::state state)
165 lyxerr[Debug::KEY] << "KeySym is " << keysym->getSymbolName() << endl;
167 // Do nothing if we have nothing (JMarc)
168 if (!keysym->isOK()) {
169 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
174 if (keysym->isModifier()) {
175 lyxerr[Debug::KEY] << "isModifier true" << endl;
179 Encoding const * encoding = view()->cursor().getEncoding();
181 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
183 // Do a one-deep top-level lookup for
184 // cancel and meta-fake keys. RVDK_PATCH_5
185 cancel_meta_seq.reset();
187 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
188 lyxerr[Debug::KEY] << "action first set to [" << func.action << ']' << endl;
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;
200 // Dont remove this unless you know what you are doing.
201 meta_fake_bit = key_modifier::none;
203 // can this happen now ?
204 if (func.action == LFUN_NOACTION) {
205 func = FuncRequest(LFUN_PREFIX);
208 if (lyxerr.debugging(Debug::KEY)) {
209 lyxerr << "Key [action="
210 << func.action << "]["
211 << keyseq.print() << ']'
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)
219 if (keyseq.length() > 1) {
220 owner->message(keyseq.print());
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;
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
237 if (keysym->isText() && keyseq.length() == 1) {
238 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
239 func = FuncRequest(LFUN_SELFINSERT);
241 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
242 owner->message(_("Unknown function."));
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));
252 << "SelfInsert arg[`" << arg << "']" << endl;
260 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
262 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
264 Buffer * buf = owner->buffer();
265 LCursor & cur = view()->cursor();
267 if (cmd.action == LFUN_NOACTION) {
268 setStatusMessage(N_("Nothing to do"));
273 switch (cmd.action) {
274 case LFUN_UNKNOWN_ACTION:
275 #ifndef HAVE_LIBAIKSAURUS
276 case LFUN_THESAURUS_ENTRY:
282 flag |= lyx_gui::getStatus(cmd);
285 if (flag.unknown()) {
286 setStatusMessage(N_("Unknown action"));
290 // the default error message if we disable the command
291 setStatusMessage(N_("Command disabled"));
293 // Check whether we need a buffer
294 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
296 setStatusMessage(N_("Command not allowed with"
297 "out any document open"));
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')
307 switch (cmd.action) {
308 case LFUN_TOOLTIPS_TOGGLE:
309 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
312 case LFUN_READ_ONLY_TOGGLE:
313 flag.setOnOff(buf->isReadonly());
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())
323 case LFUN_TRACK_CHANGES:
324 flag.setOnOff(buf->params().tracking_changes);
328 enable = cmd.argument == "custom"
329 || Exporter::IsExportable(*buf, cmd.argument);
332 enable = !buf->undostack().empty();
335 enable = !buf->redostack().empty();
339 enable = cur.selection();
343 enable = buf->isLatex() && lyxrc.chktex_command != "none";
347 enable = Exporter::IsExportable(*buf, "program");
350 case LFUN_LAYOUT_TABULAR:
351 enable = cur.innerInsetOfType(InsetBase::TABULAR_CODE);
355 case LFUN_LAYOUT_PARAGRAPH:
356 enable = !cur.inset().forceDefaultParagraphs(&cur.inset());
359 case LFUN_VC_REGISTER:
360 enable = !buf->lyxvc().inUse();
362 case LFUN_VC_CHECKIN:
363 enable = buf->lyxvc().inUse() && !buf->isReadonly();
365 case LFUN_VC_CHECKOUT:
366 enable = buf->lyxvc().inUse() && buf->isReadonly();
370 enable = buf->lyxvc().inUse();
372 case LFUN_MENURELOAD:
373 enable = !buf->isUnnamed() && !buf->isClean();
375 case LFUN_BOOKMARK_GOTO:
376 enable = view()->isSavedPosition(strToUnsignedInt(cmd.argument));
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;
387 case LFUN_INSET_SETTINGS: {
391 UpdatableInset * inset = cur.inset().asUpdatableInset();
392 lyxerr << "inset: " << inset << endl;
396 InsetOld::Code code = inset->lyxCode();
398 case InsetOld::TABULAR_CODE:
399 enable = cmd.argument == "tabular";
401 case InsetOld::ERT_CODE:
402 enable = cmd.argument == "ert";
404 case InsetOld::FLOAT_CODE:
405 enable = cmd.argument == "float";
407 case InsetOld::WRAP_CODE:
408 enable = cmd.argument == "wrap";
410 case InsetOld::NOTE_CODE:
411 enable = cmd.argument == "note";
413 case InsetOld::BRANCH_CODE:
414 enable = cmd.argument == "branch";
416 case InsetOld::BOX_CODE:
417 enable = cmd.argument == "box";
425 case LFUN_DIALOG_SHOW: {
426 string const name = cmd.getArg(0);
428 enable = name == "aboutlyx"
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);
446 case LFUN_MENUNEWTMPLT:
447 case LFUN_WORDFINDFORWARD:
448 case LFUN_WORDFINDBACKWARD:
450 case LFUN_EXEC_COMMAND:
453 case LFUN_CLOSEBUFFER:
462 case LFUN_RECONFIGURE:
466 case LFUN_DROP_LAYOUTS_CHOICE:
467 case LFUN_MENU_OPEN_BY_NAME:
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:
478 case LFUN_TOGGLECURSORFOLLOW:
482 case LFUN_KMAP_TOGGLE:
484 case LFUN_EXPORT_CUSTOM:
486 case LFUN_SAVEPREFERENCES:
487 case LFUN_SCREEN_FONT_UPDATE:
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()
498 cur.getStatus(cmd, flag);
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"));
518 bool ensureBufferClean(BufferView * bv)
520 Buffer & buf = *bv->buffer();
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"),
533 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
535 return buf.isClean();
541 void LyXFunc::dispatch(FuncRequest const & cmd, bool verbose)
543 string const argument = cmd.argument;
544 kb_action const action = cmd.action;
546 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << cmd << endl;
547 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
549 // we have not done anything wrong yet.
551 dispatch_buffer.erase();
552 selection_possible = false;
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"
560 setErrorMessage(getStatusMessage());
564 if (view()->available())
565 view()->hideCursor();
569 case LFUN_WORDFINDFORWARD:
570 case LFUN_WORDFINDBACKWARD: {
571 static string last_search;
572 string searched_string;
574 if (!argument.empty()) {
575 last_search = argument;
576 searched_string = argument;
578 searched_string = last_search;
581 if (searched_string.empty())
584 bool const fw = action == LFUN_WORDFINDFORWARD;
586 lyx::find::find2string(searched_string, true, false, fw);
587 lyx::find::find(view(), FuncRequest(LFUN_WORD_FIND, data));
592 owner->message(keyseq.printOptions());
595 case LFUN_EXEC_COMMAND:
596 owner->focus_command_buffer();
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"));
609 meta_fake_bit = key_modifier::alt;
610 setMessage(keyseq.print());
613 case LFUN_READ_ONLY_TOGGLE:
614 if (owner->buffer()->lyxvc().inUse())
615 owner->buffer()->lyxvc().toggleReadOnly();
617 owner->buffer()->setReadonly(
618 !owner->buffer()->isReadonly());
621 // --- Menus -----------------------------------------------
623 menuNew(argument, false);
626 case LFUN_MENUNEWTMPLT:
627 menuNew(argument, true);
630 case LFUN_CLOSEBUFFER:
635 if (!owner->buffer()->isUnnamed()) {
636 string const str = bformat(_("Saving document %1$s..."),
637 MakeDisplayPath(owner->buffer()->fileName()));
639 MenuWrite(owner->buffer());
640 owner->message(str + _(" done."));
642 WriteAs(owner->buffer());
646 WriteAs(owner->buffer(), argument);
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"));
662 Exporter::Export(owner->buffer(), argument, true);
663 view()->showErrorList(BufferFormat(*owner->buffer()));
667 Exporter::Preview(owner->buffer(), argument);
668 view()->showErrorList(BufferFormat(*owner->buffer()));
672 Exporter::Export(owner->buffer(), "program", true);
673 view()->showErrorList(_("Build"));
677 owner->buffer()->runChktex();
678 view()->showErrorList(_("ChkTeX"));
682 if (argument == "custom")
683 owner->getDialogs().show("sendto");
685 Exporter::Export(owner->buffer(), argument, false);
686 view()->showErrorList(BufferFormat(*owner->buffer()));
690 case LFUN_EXPORT_CUSTOM: {
692 string command = split(argument, format_name, ' ');
693 Format const * format = formats.getFormat(format_name);
695 lyxerr << "Format \"" << format_name
696 << "\" not recognized!"
701 Buffer * buffer = owner->buffer();
703 // The name of the file created by the conversion process
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);
714 if (!buffer->writeFile(filename))
718 Exporter::Export(buffer, format_name, true,
722 // Substitute $$FName for filename
723 if (!contains(command, "$$FName"))
724 command = "( " + command + " ) < $$FName";
725 command = subst(command, "$$FName", filename);
727 // Execute the command in the background
729 call.startscript(Systemcall::DontWait, command);
742 InsetCommandParams p("tableofcontents");
743 string const data = InsetCommandMailer::params2string("toc", p);
744 owner->getDialogs().show("toc", data, 0);
752 case LFUN_RECONFIGURE:
756 case LFUN_HELP_OPEN: {
757 string const arg = argument;
759 setErrorMessage(N_("Missing argument"));
762 string const fname = i18nLibFileSearch("doc", arg, "lyx");
764 lyxerr << "LyX: unable to find documentation file `"
765 << arg << "'. Bad installation?" << endl;
768 owner->message(bformat(_("Opening help file %1$s..."),
769 MakeDisplayPath(fname)));
770 view()->loadLyXFile(fname, false);
774 // --- version control -------------------------------
775 case LFUN_VC_REGISTER:
776 if (!ensureBufferClean(view()))
778 if (!owner->buffer()->lyxvc().inUse()) {
779 owner->buffer()->lyxvc().registrer();
784 case LFUN_VC_CHECKIN:
785 if (!ensureBufferClean(view()))
787 if (owner->buffer()->lyxvc().inUse()
788 && !owner->buffer()->isReadonly()) {
789 owner->buffer()->lyxvc().checkIn();
794 case LFUN_VC_CHECKOUT:
795 if (!ensureBufferClean(view()))
797 if (owner->buffer()->lyxvc().inUse()
798 && owner->buffer()->isReadonly()) {
799 owner->buffer()->lyxvc().checkOut();
805 owner->buffer()->lyxvc().revert();
810 owner->buffer()->lyxvc().undoLast();
814 // --- buffers ----------------------------------------
815 case LFUN_SWITCHBUFFER:
816 view()->setBuffer(bufferlist.getBuffer(argument));
820 NewFile(view(), argument);
827 case LFUN_DROP_LAYOUTS_CHOICE:
828 owner->getToolbar().openLayoutList();
831 case LFUN_MENU_OPEN_BY_NAME:
832 owner->getMenubar().openByName(argument);
835 // --- lyxserver commands ----------------------------
837 setMessage(owner->buffer()->fileName());
838 lyxerr[Debug::INFO] << "FNAME["
839 << owner->buffer()->fileName()
844 dispatch_buffer = keyseq.print();
845 lyxserver->notifyClient(dispatch_buffer);
848 case LFUN_GOTOFILEROW: {
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));
858 // Must replace extension of the file to be .lyx
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));
865 view()->loadLyXFile(s);
869 view()->setCursorFromRow(row);
872 // see BufferView_pimpl::center()
873 view()->updateScrollbar();
877 case LFUN_GOTO_PARAGRAPH: {
878 istringstream is(argument);
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;
887 lyxerr[Debug::INFO] << "Paragraph " << par->id()
888 << " found." << endl;
892 view()->setCursor(par, 0);
894 view()->switchKeyMap();
895 owner->view_state_changed();
898 // see BufferView_pimpl::center()
899 view()->updateScrollbar();
903 case LFUN_DIALOG_SHOW: {
904 string const name = cmd.getArg(0);
905 string data = trim(cmd.argument.substr(name.size()));
907 if (name == "character") {
908 data = freefont2string();
910 owner->getDialogs().show("character", data);
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();
923 else if (name == "latexlog") {
924 pair<Buffer::LogType, string> const logfile =
925 owner->buffer()->getLogName();
926 switch (logfile.first) {
927 case Buffer::latexlog:
930 case Buffer::buildlog:
934 data += logfile.second;
935 owner->getDialogs().show("log", data);
937 else if (name == "vclog") {
938 string const data = "vc " +
939 owner->buffer()->lyxvc().getLogFile();
940 owner->getDialogs().show("log", data);
943 owner->getDialogs().show(name, data);
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" ||
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") {
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") {
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") {
985 data = InsetNoteMailer::params2string(p);
986 } else if (name == "vspace") {
988 data = InsetVSpaceMailer::params2string(space);
989 } else if (name == "wrap") {
991 data = InsetWrapMailer::params2string(p);
993 owner->getDialogs().show(name, data, 0);
997 case LFUN_DIALOG_SHOW_NEXT_INSET:
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);
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));
1013 case LFUN_DIALOG_HIDE:
1014 Dialogs::hide(argument, 0);
1017 case LFUN_DIALOG_DISCONNECT_INSET:
1018 owner->getDialogs().disconnect(argument);
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));
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);
1039 case LFUN_TOGGLECURSORFOLLOW:
1040 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1044 owner->getIntl().KeyMapOn(false);
1047 case LFUN_KMAP_PRIM:
1048 owner->getIntl().KeyMapPrim();
1052 owner->getIntl().KeyMapSec();
1055 case LFUN_KMAP_TOGGLE:
1056 owner->getIntl().ToggleKeyMap();
1062 string rest = split(argument, countstr, ' ');
1063 istringstream is(countstr);
1066 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1067 for (int i = 0; i < count; ++i)
1068 dispatch(lyxaction.lookupFunc(rest));
1073 // argument contains ';'-terminated commands
1074 while (!argument.empty()) {
1076 string rest = split(argument, first, ';');
1077 dispatch(lyxaction.lookupFunc(rest));
1081 case LFUN_SAVEPREFERENCES: {
1082 Path p(user_lyxdir());
1083 lyxrc.write("preferences");
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
1095 case LFUN_SET_COLOR: {
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>"
1104 bool const graphicsbg_changed =
1105 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1106 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1108 if (!lcolor.setColor(lyx_name, x11_name)) {
1110 bformat(_("Set-color \"%1$s\" failed "
1111 "- color is undefined or "
1112 "may not be redefined"), lyx_name));
1116 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1118 if (graphicsbg_changed) {
1119 #ifdef WITH_WARNINGS
1120 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1123 lyx::graphics::GCache::get().changeDisplay(true);
1130 owner->message(argument);
1133 case LFUN_TOOLTIPS_TOGGLE:
1134 owner->getDialogs().toggleTooltips();
1137 case LFUN_EXTERNAL_EDIT: {
1138 FuncRequest fr(action, argument);
1139 InsetExternal().dispatch(view()->cursor(), fr);
1143 case LFUN_BREAKLINE: {
1144 #warning swallow 'Return' if the minibuffer is focused. But how?
1148 DispatchResult res = view()->cursor().dispatch(cmd);
1149 if (!res.dispatched());
1150 view()->dispatch(cmd);
1155 if (view()->available()) {
1156 view()->fitCursor();
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();
1166 if (view()->cursor().inTexted()) {
1167 view()->owner()->updateLayoutChoice();
1168 sendDispatchMessage(getMessage(), cmd, verbose);
1174 void LyXFunc::sendDispatchMessage(string const & msg,
1175 FuncRequest const & cmd, bool verbose)
1177 owner->updateMenubar();
1178 owner->updateToolbar();
1180 if (cmd.action == LFUN_SELFINSERT || !verbose) {
1181 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1183 owner->message(msg);
1187 string dispatch_msg = msg;
1188 if (!dispatch_msg.empty())
1189 dispatch_msg += ' ';
1191 string comname = lyxaction.getActionName(cmd.action);
1193 bool argsadded = false;
1195 if (!cmd.argument.empty()) {
1196 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1197 comname += ' ' + cmd.argument;
1202 string const shortcuts = toplevel_keymap->findbinding(cmd);
1204 if (!shortcuts.empty()) {
1205 comname += ": " + shortcuts;
1206 } else if (!argsadded && !cmd.argument.empty()) {
1207 comname += ' ' + cmd.argument;
1210 if (!comname.empty()) {
1211 comname = rtrim(comname);
1212 dispatch_msg += '(' + comname + ')';
1215 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1216 if (!dispatch_msg.empty())
1217 owner->message(dispatch_msg);
1221 void LyXFunc::setupLocalKeymap()
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();
1230 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1232 string initpath = lyxrc.document_path;
1233 string filename(name);
1235 if (view()->available()) {
1236 string const trypath = owner->buffer()->filePath();
1237 // If directory is writeable, use this as default.
1238 if (IsDirWriteable(trypath))
1242 static int newfile_number;
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()) {
1250 filename = AddName(lyxrc.document_path,
1251 "newfile" + tostr(newfile_number) +
1253 fi.newFile(filename);
1257 // The template stuff
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)));
1267 FileDialog::Result result =
1268 fileDlg.open(lyxrc.template_path,
1269 FileFilterList(_("LyX Documents (*.lyx)")),
1272 if (result.first == FileDialog::Later)
1274 if (result.second.empty())
1276 templname = result.second;
1279 view()->newFile(filename, templname, !name.empty());
1283 void LyXFunc::open(string const & fname)
1285 string initpath = lyxrc.document_path;
1287 if (view()->available()) {
1288 string const trypath = owner->buffer()->filePath();
1289 // If directory is writeable, use this as default.
1290 if (IsDirWriteable(trypath))
1296 if (fname.empty()) {
1297 FileDialog fileDlg(_("Select document to 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"))));
1304 FileDialog::Result result =
1305 fileDlg.open(initpath,
1306 FileFilterList(_("LyX Documents (*.lyx)")),
1309 if (result.first == FileDialog::Later)
1312 filename = result.second;
1314 // check selected filename
1315 if (filename.empty()) {
1316 owner->message(_("Canceled."));
1322 // get absolute path of file and add ".lyx" to the filename if
1324 string const fullpath = FileSearch(string(), filename, "lyx");
1325 if (!fullpath.empty()) {
1326 filename = fullpath;
1329 string const disp_fn(MakeDisplayPath(filename));
1331 // if the file doesn't exist, let the user create one
1332 FileInfo const f(filename, true);
1334 // the user specifically chose this name. Believe them.
1335 view()->newFile(filename, "", true);
1339 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1342 if (view()->loadLyXFile(filename)) {
1343 str2 = bformat(_("Document %1$s opened."), disp_fn);
1345 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1347 owner->message(str2);
1351 void LyXFunc::doImport(string const & argument)
1354 string filename = split(argument, format, ' ');
1356 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1357 << " file: " << filename << endl;
1359 // need user interaction
1360 if (filename.empty()) {
1361 string initpath = lyxrc.document_path;
1363 if (view()->available()) {
1364 string const trypath = owner->buffer()->filePath();
1365 // If directory is writeable, use this as default.
1366 if (IsDirWriteable(trypath))
1370 string const text = bformat(_("Select %1$s file to import"),
1371 formats.prettyName(format));
1373 FileDialog fileDlg(text,
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"))));
1380 string const filter = formats.prettyName(format)
1381 + " (*." + formats.extension(format) + ')';
1383 FileDialog::Result result =
1384 fileDlg.open(initpath,
1385 FileFilterList(filter),
1388 if (result.first == FileDialog::Later)
1391 filename = result.second;
1393 // check selected filename
1394 if (filename.empty())
1395 owner->message(_("Canceled."));
1398 if (filename.empty())
1401 // get absolute path of file
1402 filename = MakeAbsPath(filename);
1404 string const lyxfile = ChangeExtension(filename, ".lyx");
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."));
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);
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"));
1425 owner->message(_("Canceled."));
1430 Importer::Import(owner, filename, format);
1434 void LyXFunc::closeBuffer()
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();
1443 view()->setBuffer(bufferlist.first());
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
1457 dispatch_buffer = m;
1462 void LyXFunc::setMessage(string const & m) const
1464 dispatch_buffer = m;
1468 void LyXFunc::setStatusMessage(string const & m) const
1474 string const LyXFunc::viewStatusMessage()
1476 // When meta-fake key is pressed, show the key sequence so far + "M-".
1478 return keyseq.print() + "M-";
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();
1485 if (!view()->available())
1486 return _("Welcome to LyX!");
1488 return view()->cursor().currentState();
1492 BufferView * LyXFunc::view() const
1494 BOOST_ASSERT(owner);
1495 return owner->view().get();
1499 bool LyXFunc::wasMetaKey() const
1501 return (meta_fake_bit != key_modifier::none);