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/tostr.h"
82 #include "support/std_sstream.h"
83 #include "support/os.h"
85 using bv_funcs::freefont2string;
87 using lyx::support::AddName;
88 using lyx::support::AddPath;
89 using lyx::support::bformat;
90 using lyx::support::ChangeExtension;
91 using lyx::support::FileFilterList;
92 using lyx::support::FileInfo;
93 using lyx::support::FileSearch;
94 using lyx::support::ForkedcallsController;
95 using lyx::support::i18nLibFileSearch;
96 using lyx::support::IsDirWriteable;
97 using lyx::support::IsFileReadable;
98 using lyx::support::isStrInt;
99 using lyx::support::MakeAbsPath;
100 using lyx::support::MakeDisplayPath;
101 using lyx::support::Path;
102 using lyx::support::rtrim;
103 using lyx::support::split;
104 using lyx::support::strToInt;
105 using lyx::support::strToUnsignedInt;
106 using lyx::support::system_lyxdir;
107 using lyx::support::token;
108 using lyx::support::trim;
109 using lyx::support::user_lyxdir;
110 using lyx::support::prefixIs;
111 using lyx::support::os::getTmpDir;
114 using std::make_pair;
117 using std::istringstream;
120 extern BufferList bufferlist;
121 extern LyXServer * lyxserver;
122 extern bool selection_possible;
124 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
127 extern tex_accent_struct get_accent(kb_action action);
130 LyXFunc::LyXFunc(LyXView * lv)
133 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
134 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
135 meta_fake_bit(key_modifier::none)
140 void LyXFunc::handleKeyFunc(kb_action action)
142 char c = encoded_last_key;
144 if (keyseq.length()) {
148 owner->getIntl().getTransManager()
149 .deadkey(c, get_accent(action).accent, view()->getLyXText());
150 // Need to clear, in case the minibuffer calls these
153 // copied verbatim from do_accent_char
154 view()->cursor().resetAnchor();
159 void LyXFunc::processKeySym(LyXKeySymPtr keysym, key_modifier::state state)
161 lyxerr[Debug::KEY] << "KeySym is " << keysym->getSymbolName() << endl;
163 // Do nothing if we have nothing (JMarc)
164 if (!keysym->isOK()) {
165 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
170 if (keysym->isModifier()) {
171 lyxerr[Debug::KEY] << "isModifier true" << endl;
175 Encoding const * encoding = view()->cursor().getEncoding();
177 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
179 // Do a one-deep top-level lookup for
180 // cancel and meta-fake keys. RVDK_PATCH_5
181 cancel_meta_seq.reset();
183 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
184 lyxerr[Debug::KEY] << "action first set to [" << func.action << ']' << endl;
186 // When not cancel or meta-fake, do the normal lookup.
187 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
188 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
189 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_FAKE)) {
190 // remove Caps Lock and Mod2 as a modifiers
191 func = keyseq.addkey(keysym, (state | meta_fake_bit));
192 lyxerr[Debug::KEY] << "action now set to ["
193 << func.action << ']' << endl;
196 // Dont remove this unless you know what you are doing.
197 meta_fake_bit = key_modifier::none;
199 // can this happen now ?
200 if (func.action == LFUN_NOACTION) {
201 func = FuncRequest(LFUN_PREFIX);
204 if (lyxerr.debugging(Debug::KEY)) {
205 lyxerr << "Key [action="
206 << func.action << "]["
207 << keyseq.print() << ']'
211 // already here we know if it any point in going further
212 // why not return already here if action == -1 and
213 // num_bytes == 0? (Lgb)
215 if (keyseq.length() > 1) {
216 owner->message(keyseq.print());
220 // Maybe user can only reach the key via holding down shift.
221 // Let's see. But only if shift is the only modifier
222 if (func.action == LFUN_UNKNOWN_ACTION &&
223 state == key_modifier::shift) {
224 lyxerr[Debug::KEY] << "Trying without shift" << endl;
225 func = keyseq.addkey(keysym, key_modifier::none);
226 lyxerr[Debug::KEY] << "Action now " << func.action << endl;
229 if (func.action == LFUN_UNKNOWN_ACTION) {
230 // Hmm, we didn't match any of the keysequences. See
231 // if it's normal insertable text not already covered
233 if (keysym->isText() && keyseq.length() == 1) {
234 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
235 func = FuncRequest(LFUN_SELFINSERT);
237 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
238 owner->message(_("Unknown function."));
243 if (func.action == LFUN_SELFINSERT) {
244 if (encoded_last_key != 0) {
245 string arg(1, encoded_last_key);
246 dispatch(FuncRequest(LFUN_SELFINSERT, arg));
248 << "SelfInsert arg[`" << arg << "']" << endl;
256 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
258 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
260 Buffer * buf = owner->buffer();
261 LCursor & cur = view()->cursor();
263 if (cmd.action == LFUN_NOACTION) {
264 setStatusMessage(N_("Nothing to do"));
269 switch (cmd.action) {
270 case LFUN_UNKNOWN_ACTION:
271 #ifndef HAVE_LIBAIKSAURUS
272 case LFUN_THESAURUS_ENTRY:
278 flag |= lyx_gui::getStatus(cmd);
281 if (flag.unknown()) {
282 setStatusMessage(N_("Unknown action"));
286 // the default error message if we disable the command
287 setStatusMessage(N_("Command disabled"));
289 // Check whether we need a buffer
290 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
291 // Yes we need a buffer, do we have one?
294 // Can we use a readonly buffer?
295 if (buf->isReadonly() &&
296 !lyxaction.funcHasFlag(cmd.action,
297 LyXAction::ReadOnly)) {
299 setStatusMessage(N_("Document is read-only"));
304 setStatusMessage(N_("Command not allowed with"
305 "out any document open"));
311 // I would really like to avoid having this switch and rather try to
312 // encode this in the function itself.
313 // -- And I'd rather let an inset decide which LFUNs it is willing
314 // to handle (Andre')
316 switch (cmd.action) {
317 case LFUN_TOOLTIPS_TOGGLE:
318 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
321 case LFUN_READ_ONLY_TOGGLE:
322 flag.setOnOff(buf->isReadonly());
325 case LFUN_SWITCHBUFFER:
326 // toggle on the current buffer, but do not toggle off
327 // the other ones (is that a good idea?)
328 if (cmd.argument == buf->fileName())
332 case LFUN_TRACK_CHANGES:
333 flag.setOnOff(buf->params().tracking_changes);
337 enable = cmd.argument == "custom"
338 || Exporter::IsExportable(*buf, cmd.argument);
341 enable = !buf->undostack().empty();
344 enable = !buf->redostack().empty();
348 enable = cur.selection();
352 enable = buf->isLatex() && lyxrc.chktex_command != "none";
356 enable = Exporter::IsExportable(*buf, "program");
359 case LFUN_LAYOUT_TABULAR:
360 enable = cur.innerInsetOfType(InsetBase::TABULAR_CODE);
364 case LFUN_LAYOUT_PARAGRAPH:
365 enable = !cur.inset().forceDefaultParagraphs(&cur.inset());
368 case LFUN_VC_REGISTER:
369 enable = !buf->lyxvc().inUse();
371 case LFUN_VC_CHECKIN:
372 enable = buf->lyxvc().inUse() && !buf->isReadonly();
374 case LFUN_VC_CHECKOUT:
375 enable = buf->lyxvc().inUse() && buf->isReadonly();
379 enable = buf->lyxvc().inUse();
381 case LFUN_MENURELOAD:
382 enable = !buf->isUnnamed() && !buf->isClean();
384 case LFUN_BOOKMARK_GOTO:
385 enable = view()->isSavedPosition(strToUnsignedInt(cmd.argument));
388 case LFUN_MERGE_CHANGES:
389 case LFUN_ACCEPT_CHANGE:
390 case LFUN_REJECT_CHANGE:
391 case LFUN_ACCEPT_ALL_CHANGES:
392 case LFUN_REJECT_ALL_CHANGES:
393 enable = buf && buf->params().tracking_changes;
396 case LFUN_INSET_SETTINGS: {
400 UpdatableInset * inset = cur.inset().asUpdatableInset();
401 lyxerr << "inset: " << inset << endl;
405 // jump back to owner if an InsetText, so
406 // we get back to the InsetTabular or whatever
407 if (inset->lyxCode() == InsetOld::TEXT_CODE)
408 inset = inset->owner();
409 lyxerr << "inset 2: " << inset << endl;
413 InsetOld::Code code = inset->lyxCode();
415 case InsetOld::TABULAR_CODE:
416 enable = cmd.argument == "tabular";
418 case InsetOld::ERT_CODE:
419 enable = cmd.argument == "ert";
421 case InsetOld::FLOAT_CODE:
422 enable = cmd.argument == "float";
424 case InsetOld::WRAP_CODE:
425 enable = cmd.argument == "wrap";
427 case InsetOld::NOTE_CODE:
428 enable = cmd.argument == "note";
430 case InsetOld::BRANCH_CODE:
431 enable = cmd.argument == "branch";
433 case InsetOld::BOX_CODE:
434 enable = cmd.argument == "box";
442 case LFUN_DIALOG_SHOW: {
443 string const name = cmd.getArg(0);
445 enable = name == "aboutlyx"
447 || name == "preferences"
448 || name == "texinfo";
449 else if (name == "print")
450 enable = Exporter::IsExportable(*buf, "dvi")
451 && lyxrc.print_command != "none";
452 else if (name == "character")
453 enable = cur.inset().lyxCode() != InsetOld::ERT_CODE;
454 else if (name == "vclog")
455 enable = buf->lyxvc().inUse();
456 else if (name == "latexlog")
457 enable = IsFileReadable(buf->getLogName().second);
462 case LFUN_MENUNEWTMPLT:
463 case LFUN_WORDFINDFORWARD:
464 case LFUN_WORDFINDBACKWARD:
466 case LFUN_EXEC_COMMAND:
469 case LFUN_CLOSEBUFFER:
478 case LFUN_RECONFIGURE:
482 case LFUN_DROP_LAYOUTS_CHOICE:
483 case LFUN_MENU_OPEN_BY_NAME:
486 case LFUN_GOTOFILEROW:
487 case LFUN_GOTO_PARAGRAPH:
488 case LFUN_DIALOG_SHOW_NEW_INSET:
489 case LFUN_DIALOG_SHOW_NEXT_INSET:
490 case LFUN_DIALOG_UPDATE:
491 case LFUN_DIALOG_HIDE:
492 case LFUN_DIALOG_DISCONNECT_INSET:
494 case LFUN_TOGGLECURSORFOLLOW:
498 case LFUN_KMAP_TOGGLE:
501 case LFUN_SAVEPREFERENCES:
502 case LFUN_SCREEN_FONT_UPDATE:
505 case LFUN_EXTERNAL_EDIT:
506 // these are handled in our dispatch()
510 cur.getStatus(cmd, flag);
514 flag.enabled(enable);
521 bool ensureBufferClean(BufferView * bv)
523 Buffer & buf = *bv->buffer();
527 string const file = MakeDisplayPath(buf.fileName(), 30);
528 string text = bformat(_("The document %1$s has unsaved "
529 "changes.\n\nDo you want to save "
530 "the document?"), file);
531 int const ret = Alert::prompt(_("Save changed document?"),
532 text, 0, 1, _("&Save"),
536 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
538 return buf.isClean();
544 void LyXFunc::dispatch(FuncRequest const & cmd, bool verbose)
546 string const argument = cmd.argument;
547 kb_action const action = cmd.action;
549 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << cmd << endl;
550 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
552 // we have not done anything wrong yet.
554 dispatch_buffer.erase();
555 selection_possible = false;
557 // We cannot use this function here
558 if (!getStatus(cmd).enabled()) {
559 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
560 << lyxaction.getActionName(action)
561 << " [" << action << "] is disabled at this location"
563 setErrorMessage(getStatusMessage());
567 if (view()->available())
568 view()->hideCursor();
572 case LFUN_WORDFINDFORWARD:
573 case LFUN_WORDFINDBACKWARD: {
574 static string last_search;
575 string searched_string;
577 if (!argument.empty()) {
578 last_search = argument;
579 searched_string = argument;
581 searched_string = last_search;
584 if (searched_string.empty())
587 bool const fw = action == LFUN_WORDFINDFORWARD;
589 lyx::find::find2string(searched_string, true, false, fw);
590 view()->dispatch(FuncRequest(LFUN_WORD_FIND, data));
595 owner->message(keyseq.printOptions());
598 case LFUN_EXEC_COMMAND:
599 owner->focus_command_buffer();
604 meta_fake_bit = key_modifier::none;
605 if (view()->available())
606 // cancel any selection
607 dispatch(FuncRequest(LFUN_MARK_OFF));
608 setMessage(N_("Cancel"));
612 meta_fake_bit = key_modifier::alt;
613 setMessage(keyseq.print());
616 case LFUN_READ_ONLY_TOGGLE:
617 if (owner->buffer()->lyxvc().inUse())
618 owner->buffer()->lyxvc().toggleReadOnly();
620 owner->buffer()->setReadonly(
621 !owner->buffer()->isReadonly());
624 // --- Menus -----------------------------------------------
626 menuNew(argument, false);
629 case LFUN_MENUNEWTMPLT:
630 menuNew(argument, true);
633 case LFUN_CLOSEBUFFER:
638 if (!owner->buffer()->isUnnamed()) {
639 string const str = bformat(_("Saving document %1$s..."),
640 MakeDisplayPath(owner->buffer()->fileName()));
642 MenuWrite(owner->buffer());
643 owner->message(str + _(" done."));
645 WriteAs(owner->buffer());
649 WriteAs(owner->buffer(), argument);
652 case LFUN_MENURELOAD: {
653 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
654 string text = bformat(_("Any changes will be lost. Are you sure "
655 "you want to revert to the saved version of the document %1$s?"), file);
656 int const ret = Alert::prompt(_("Revert to saved document?"),
657 text, 0, 1, _("&Revert"), _("&Cancel"));
665 Exporter::Export(owner->buffer(), argument, true);
666 view()->showErrorList(BufferFormat(*owner->buffer()));
670 Exporter::Preview(owner->buffer(), argument);
671 view()->showErrorList(BufferFormat(*owner->buffer()));
675 Exporter::Export(owner->buffer(), "program", true);
676 view()->showErrorList(_("Build"));
680 owner->buffer()->runChktex();
681 view()->showErrorList(_("ChkTeX"));
685 if (argument == "custom")
686 owner->getDialogs().showSendto();
688 Exporter::Export(owner->buffer(), argument, false);
689 view()->showErrorList(BufferFormat(*owner->buffer()));
702 InsetCommandParams p("tableofcontents");
703 string const data = InsetCommandMailer::params2string("toc", p);
704 owner->getDialogs().show("toc", data, 0);
712 case LFUN_RECONFIGURE:
716 case LFUN_HELP_OPEN: {
717 string const arg = argument;
719 setErrorMessage(N_("Missing argument"));
722 string const fname = i18nLibFileSearch("doc", arg, "lyx");
724 lyxerr << "LyX: unable to find documentation file `"
725 << arg << "'. Bad installation?" << endl;
728 owner->message(bformat(_("Opening help file %1$s..."),
729 MakeDisplayPath(fname)));
730 view()->loadLyXFile(fname, false);
734 // --- version control -------------------------------
735 case LFUN_VC_REGISTER:
736 if (!ensureBufferClean(view()))
738 if (!owner->buffer()->lyxvc().inUse()) {
739 owner->buffer()->lyxvc().registrer();
744 case LFUN_VC_CHECKIN:
745 if (!ensureBufferClean(view()))
747 if (owner->buffer()->lyxvc().inUse()
748 && !owner->buffer()->isReadonly()) {
749 owner->buffer()->lyxvc().checkIn();
754 case LFUN_VC_CHECKOUT:
755 if (!ensureBufferClean(view()))
757 if (owner->buffer()->lyxvc().inUse()
758 && owner->buffer()->isReadonly()) {
759 owner->buffer()->lyxvc().checkOut();
765 owner->buffer()->lyxvc().revert();
770 owner->buffer()->lyxvc().undoLast();
774 // --- buffers ----------------------------------------
775 case LFUN_SWITCHBUFFER:
776 view()->setBuffer(bufferlist.getBuffer(argument));
780 NewFile(view(), argument);
787 case LFUN_DROP_LAYOUTS_CHOICE:
788 owner->getToolbar().openLayoutList();
791 case LFUN_MENU_OPEN_BY_NAME:
792 owner->getMenubar().openByName(argument);
795 // --- lyxserver commands ----------------------------
797 setMessage(owner->buffer()->fileName());
798 lyxerr[Debug::INFO] << "FNAME["
799 << owner->buffer()->fileName()
804 dispatch_buffer = keyseq.print();
805 lyxserver->notifyClient(dispatch_buffer);
808 case LFUN_GOTOFILEROW: {
811 istringstream is(argument);
812 is >> file_name >> row;
813 if (prefixIs(file_name, getTmpDir())) {
814 // Needed by inverse dvi search. If it is a file
815 // in tmpdir, call the apropriated function
816 view()->setBuffer(bufferlist.getBufferFromTmp(file_name));
818 // Must replace extension of the file to be .lyx
820 string const s = ChangeExtension(file_name, ".lyx");
821 // Either change buffer or load the file
822 if (bufferlist.exists(s)) {
823 view()->setBuffer(bufferlist.getBuffer(s));
825 view()->loadLyXFile(s);
829 view()->setCursorFromRow(row);
832 // see BufferView_pimpl::center()
833 view()->updateScrollbar();
837 case LFUN_GOTO_PARAGRAPH: {
838 istringstream is(argument);
841 ParIterator par = owner->buffer()->getParFromID(id);
842 if (par == owner->buffer()->par_iterator_end()) {
843 lyxerr[Debug::INFO] << "No matching paragraph found! ["
844 << id << ']' << endl;
847 lyxerr[Debug::INFO] << "Paragraph " << par->id()
848 << " found." << endl;
852 view()->setCursor(par, 0);
854 view()->switchKeyMap();
855 owner->view_state_changed();
858 // see BufferView_pimpl::center()
859 view()->updateScrollbar();
863 case LFUN_DIALOG_SHOW: {
864 string const name = cmd.getArg(0);
865 string data = trim(cmd.argument.substr(name.size()));
867 if (name == "character") {
868 data = freefont2string();
870 owner->getDialogs().show("character", data);
872 else if (name == "document")
873 owner->getDialogs().showDocument();
874 else if (name == "preamble")
875 owner->getDialogs().showPreamble();
876 else if (name == "preferences")
877 owner->getDialogs().showPreferences();
878 else if (name == "print")
879 owner->getDialogs().showPrint();
880 else if (name == "spellchecker")
881 owner->getDialogs().showSpellchecker();
883 else if (name == "latexlog") {
884 pair<Buffer::LogType, string> const logfile =
885 owner->buffer()->getLogName();
886 switch (logfile.first) {
887 case Buffer::latexlog:
890 case Buffer::buildlog:
894 data += logfile.second;
895 owner->getDialogs().show("log", data);
897 else if (name == "vclog") {
898 string const data = "vc " +
899 owner->buffer()->lyxvc().getLogFile();
900 owner->getDialogs().show("log", data);
903 owner->getDialogs().show(name, data);
907 case LFUN_DIALOG_SHOW_NEW_INSET: {
908 string const name = cmd.getArg(0);
909 string data = trim(cmd.argument.substr(name.size()));
910 if (name == "bibitem" ||
918 InsetCommandParams p(name);
919 data = InsetCommandMailer::params2string(name, p);
920 } else if (name == "box") {
921 // \c data == "Boxed" || "Frameless" etc
922 InsetBoxParams p(data);
923 data = InsetBoxMailer::params2string(p);
924 } else if (name == "branch") {
926 data = InsetBranchMailer::params2string(p);
927 } else if (name == "citation") {
928 InsetCommandParams p("cite");
929 data = InsetCommandMailer::params2string(name, p);
930 } else if (name == "ert") {
931 data = InsetERTMailer::params2string(InsetCollapsable::Open);
932 } else if (name == "external") {
933 InsetExternalParams p;
934 Buffer const & buffer = *owner->buffer();
935 data = InsetExternalMailer::params2string(p, buffer);
936 } else if (name == "float") {
938 data = InsetFloatMailer::params2string(p);
939 } else if (name == "graphics") {
940 InsetGraphicsParams p;
941 Buffer const & buffer = *owner->buffer();
942 data = InsetGraphicsMailer::params2string(p, buffer);
943 } else if (name == "note") {
945 data = InsetNoteMailer::params2string(p);
946 } else if (name == "vspace") {
948 data = InsetVSpaceMailer::params2string(space);
949 } else if (name == "wrap") {
951 data = InsetWrapMailer::params2string(p);
953 owner->getDialogs().show(name, data, 0);
957 case LFUN_DIALOG_SHOW_NEXT_INSET:
960 case LFUN_DIALOG_UPDATE: {
961 string const & name = argument;
962 // Can only update a dialog connected to an existing inset
963 InsetBase * inset = owner->getDialogs().getOpenInset(name);
965 FuncRequest fr(LFUN_INSET_DIALOG_UPDATE, cmd.argument);
966 inset->dispatch(view()->cursor(), fr);
967 } else if (name == "paragraph") {
968 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
973 case LFUN_DIALOG_HIDE:
974 Dialogs::hide(argument, 0);
977 case LFUN_DIALOG_DISCONNECT_INSET:
978 owner->getDialogs().disconnect(argument);
981 case LFUN_CHILDOPEN: {
982 string const filename =
983 MakeAbsPath(argument, owner->buffer()->filePath());
984 setMessage(N_("Opening child document ") +
985 MakeDisplayPath(filename) + "...");
986 view()->savePosition(0);
987 string const parentfilename = owner->buffer()->fileName();
988 if (bufferlist.exists(filename))
989 view()->setBuffer(bufferlist.getBuffer(filename));
991 view()->loadLyXFile(filename);
992 // Set the parent name of the child document.
993 // This makes insertion of citations and references in the child work,
994 // when the target is in the parent or another child document.
995 owner->buffer()->setParentName(parentfilename);
999 case LFUN_TOGGLECURSORFOLLOW:
1000 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1004 owner->getIntl().KeyMapOn(false);
1007 case LFUN_KMAP_PRIM:
1008 owner->getIntl().KeyMapPrim();
1012 owner->getIntl().KeyMapSec();
1015 case LFUN_KMAP_TOGGLE:
1016 owner->getIntl().ToggleKeyMap();
1022 string rest = split(argument, countstr, ' ');
1023 istringstream is(countstr);
1026 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1027 for (int i = 0; i < count; ++i)
1028 dispatch(lyxaction.lookupFunc(rest));
1033 // argument contains ';'-terminated commands
1034 while (!argument.empty()) {
1036 string rest = split(argument, first, ';');
1037 dispatch(lyxaction.lookupFunc(rest));
1041 case LFUN_SAVEPREFERENCES: {
1042 Path p(user_lyxdir());
1043 lyxrc.write("preferences");
1047 case LFUN_SCREEN_FONT_UPDATE:
1048 // handle the screen font changes.
1049 lyxrc.set_font_norm_type();
1050 lyx_gui::update_fonts();
1051 // All visible buffers will need resize
1055 case LFUN_SET_COLOR: {
1057 string const x11_name = split(argument, lyx_name, ' ');
1058 if (lyx_name.empty() || x11_name.empty()) {
1059 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1064 bool const graphicsbg_changed =
1065 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1066 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1068 LColor::color col = lcolor.getFromLyXName(lyx_name);
1069 if (!lcolor.setColor(col, x11_name)) {
1071 bformat(_("Set-color \"%1$s\" failed "
1072 "- color is undefined or "
1073 "may not be redefined"), lyx_name));
1077 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1079 if (graphicsbg_changed) {
1080 #ifdef WITH_WARNINGS
1081 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1084 lyx::graphics::GCache::get().changeDisplay(true);
1091 owner->message(argument);
1094 case LFUN_TOOLTIPS_TOGGLE:
1095 owner->getDialogs().toggleTooltips();
1098 case LFUN_EXTERNAL_EDIT: {
1099 FuncRequest fr(action, argument);
1100 InsetExternal().dispatch(view()->cursor(), fr);
1105 DispatchResult res = view()->cursor().dispatch(cmd);
1106 if (!res.dispatched());
1107 view()->dispatch(cmd);
1113 if (view()->available()) {
1114 view()->fitCursor();
1116 view()->cursor().updatePos();
1117 // if we executed a mutating lfun, mark the buffer as dirty
1118 if (getStatus(cmd).enabled()
1119 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)
1120 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly))
1121 view()->buffer()->markDirty();
1124 if (view()->cursor().inTexted()) {
1125 view()->owner()->updateLayoutChoice();
1126 sendDispatchMessage(getMessage(), cmd, verbose);
1131 void LyXFunc::sendDispatchMessage(string const & msg,
1132 FuncRequest const & cmd, bool verbose)
1134 owner->updateMenubar();
1135 owner->updateToolbar();
1137 if (cmd.action == LFUN_SELFINSERT || !verbose) {
1138 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1140 owner->message(msg);
1144 string dispatch_msg = msg;
1145 if (!dispatch_msg.empty())
1146 dispatch_msg += ' ';
1148 string comname = lyxaction.getActionName(cmd.action);
1150 bool argsadded = false;
1152 if (!cmd.argument.empty()) {
1153 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1154 comname += ' ' + cmd.argument;
1159 string const shortcuts = toplevel_keymap->findbinding(cmd);
1161 if (!shortcuts.empty()) {
1162 comname += ": " + shortcuts;
1163 } else if (!argsadded && !cmd.argument.empty()) {
1164 comname += ' ' + cmd.argument;
1167 if (!comname.empty()) {
1168 comname = rtrim(comname);
1169 dispatch_msg += '(' + comname + ')';
1172 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1173 if (!dispatch_msg.empty())
1174 owner->message(dispatch_msg);
1178 void LyXFunc::setupLocalKeymap()
1180 keyseq.stdmap = toplevel_keymap.get();
1181 keyseq.curmap = toplevel_keymap.get();
1182 cancel_meta_seq.stdmap = toplevel_keymap.get();
1183 cancel_meta_seq.curmap = toplevel_keymap.get();
1187 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1189 string initpath = lyxrc.document_path;
1190 string filename(name);
1192 if (view()->available()) {
1193 string const trypath = owner->buffer()->filePath();
1194 // If directory is writeable, use this as default.
1195 if (IsDirWriteable(trypath))
1199 static int newfile_number;
1201 if (filename.empty()) {
1202 filename = AddName(lyxrc.document_path,
1203 "newfile" + tostr(++newfile_number) + ".lyx");
1204 FileInfo fi(filename);
1205 while (bufferlist.exists(filename) || fi.readable()) {
1207 filename = AddName(lyxrc.document_path,
1208 "newfile" + tostr(newfile_number) +
1210 fi.newFile(filename);
1214 // The template stuff
1217 FileDialog fileDlg(_("Select template file"),
1218 LFUN_SELECT_FILE_SYNC,
1219 make_pair(string(_("Documents|#o#O")),
1220 string(lyxrc.document_path)),
1221 make_pair(string(_("Templates|#T#t")),
1222 string(lyxrc.template_path)));
1224 FileDialog::Result result =
1225 fileDlg.open(lyxrc.template_path,
1226 FileFilterList(_("LyX Documents (*.lyx)")),
1229 if (result.first == FileDialog::Later)
1231 if (result.second.empty())
1233 templname = result.second;
1236 view()->newFile(filename, templname, !name.empty());
1240 void LyXFunc::open(string const & fname)
1242 string initpath = lyxrc.document_path;
1244 if (view()->available()) {
1245 string const trypath = owner->buffer()->filePath();
1246 // If directory is writeable, use this as default.
1247 if (IsDirWriteable(trypath))
1253 if (fname.empty()) {
1254 FileDialog fileDlg(_("Select document to open"),
1256 make_pair(string(_("Documents|#o#O")),
1257 string(lyxrc.document_path)),
1258 make_pair(string(_("Examples|#E#e")),
1259 string(AddPath(system_lyxdir(), "examples"))));
1261 FileDialog::Result result =
1262 fileDlg.open(initpath,
1263 FileFilterList(_("LyX Documents (*.lyx)")),
1266 if (result.first == FileDialog::Later)
1269 filename = result.second;
1271 // check selected filename
1272 if (filename.empty()) {
1273 owner->message(_("Canceled."));
1279 // get absolute path of file and add ".lyx" to the filename if
1281 string const fullpath = FileSearch(string(), filename, "lyx");
1282 if (!fullpath.empty()) {
1283 filename = fullpath;
1286 string const disp_fn(MakeDisplayPath(filename));
1288 // if the file doesn't exist, let the user create one
1289 FileInfo const f(filename, true);
1291 // the user specifically chose this name. Believe them.
1292 view()->newFile(filename, "", true);
1296 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1299 if (view()->loadLyXFile(filename)) {
1300 str2 = bformat(_("Document %1$s opened."), disp_fn);
1302 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1304 owner->message(str2);
1308 void LyXFunc::doImport(string const & argument)
1311 string filename = split(argument, format, ' ');
1313 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1314 << " file: " << filename << endl;
1316 // need user interaction
1317 if (filename.empty()) {
1318 string initpath = lyxrc.document_path;
1320 if (view()->available()) {
1321 string const trypath = owner->buffer()->filePath();
1322 // If directory is writeable, use this as default.
1323 if (IsDirWriteable(trypath))
1327 string const text = bformat(_("Select %1$s file to import"),
1328 formats.prettyName(format));
1330 FileDialog fileDlg(text,
1332 make_pair(string(_("Documents|#o#O")),
1333 string(lyxrc.document_path)),
1334 make_pair(string(_("Examples|#E#e")),
1335 string(AddPath(system_lyxdir(), "examples"))));
1337 string const filter = formats.prettyName(format)
1338 + " (*." + formats.extension(format) + ')';
1340 FileDialog::Result result =
1341 fileDlg.open(initpath,
1342 FileFilterList(filter),
1345 if (result.first == FileDialog::Later)
1348 filename = result.second;
1350 // check selected filename
1351 if (filename.empty())
1352 owner->message(_("Canceled."));
1355 if (filename.empty())
1358 // get absolute path of file
1359 filename = MakeAbsPath(filename);
1361 string const lyxfile = ChangeExtension(filename, ".lyx");
1363 // Check if the document already is open
1364 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1365 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1366 owner->message(_("Canceled."));
1371 // if the file exists already, and we didn't do
1372 // -i lyx thefile.lyx, warn
1373 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1374 string const file = MakeDisplayPath(lyxfile, 30);
1376 string text = bformat(_("The document %1$s already exists.\n\n"
1377 "Do you want to over-write that document?"), file);
1378 int const ret = Alert::prompt(_("Over-write document?"),
1379 text, 0, 1, _("&Over-write"), _("&Cancel"));
1382 owner->message(_("Canceled."));
1387 Importer::Import(owner, filename, format);
1391 void LyXFunc::closeBuffer()
1393 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1394 if (bufferlist.empty()) {
1395 // need this otherwise SEGV may occur while
1396 // trying to set variables that don't exist
1397 // since there's no current buffer
1398 owner->getDialogs().hideBufferDependent();
1400 view()->setBuffer(bufferlist.first());
1406 // Each "owner" should have it's own message method. lyxview and
1407 // the minibuffer would use the minibuffer, but lyxserver would
1408 // send an ERROR signal to its client. Alejandro 970603
1409 // This function is bit problematic when it comes to NLS, to make the
1410 // lyx servers client be language indepenent we must not translate
1411 // strings sent to this func.
1412 void LyXFunc::setErrorMessage(string const & m) const
1414 dispatch_buffer = m;
1419 void LyXFunc::setMessage(string const & m) const
1421 dispatch_buffer = m;
1425 void LyXFunc::setStatusMessage(string const & m) const
1431 string const LyXFunc::viewStatusMessage()
1433 // When meta-fake key is pressed, show the key sequence so far + "M-".
1435 return keyseq.print() + "M-";
1437 // Else, when a non-complete key sequence is pressed,
1438 // show the available options.
1439 if (keyseq.length() > 0 && !keyseq.deleted())
1440 return keyseq.printOptions();
1442 if (!view()->available())
1443 return _("Welcome to LyX!");
1445 return view()->cursor().currentState();
1449 BufferView * LyXFunc::view() const
1451 BOOST_ASSERT(owner);
1452 return owner->view().get();
1456 bool LyXFunc::wasMetaKey() const
1458 return (meta_fake_bit != key_modifier::none);