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) && !buf) {
292 setStatusMessage(N_("Command not allowed with"
293 "out any document open"));
298 // I would really like to avoid having this switch and rather try to
299 // encode this in the function itself.
300 // -- And I'd rather let an inset decide which LFUNs it is willing
301 // to handle (Andre')
303 switch (cmd.action) {
304 case LFUN_TOOLTIPS_TOGGLE:
305 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
308 case LFUN_READ_ONLY_TOGGLE:
309 flag.setOnOff(buf->isReadonly());
312 case LFUN_SWITCHBUFFER:
313 // toggle on the current buffer, but do not toggle off
314 // the other ones (is that a good idea?)
315 if (cmd.argument == buf->fileName())
319 case LFUN_TRACK_CHANGES:
320 flag.setOnOff(buf->params().tracking_changes);
324 enable = cmd.argument == "custom"
325 || Exporter::IsExportable(*buf, cmd.argument);
328 enable = !buf->undostack().empty();
331 enable = !buf->redostack().empty();
335 enable = cur.selection();
339 enable = buf->isLatex() && lyxrc.chktex_command != "none";
343 enable = Exporter::IsExportable(*buf, "program");
346 case LFUN_LAYOUT_TABULAR:
347 enable = cur.innerInsetOfType(InsetBase::TABULAR_CODE);
351 case LFUN_LAYOUT_PARAGRAPH:
352 enable = !cur.inset().forceDefaultParagraphs(&cur.inset());
355 case LFUN_VC_REGISTER:
356 enable = !buf->lyxvc().inUse();
358 case LFUN_VC_CHECKIN:
359 enable = buf->lyxvc().inUse() && !buf->isReadonly();
361 case LFUN_VC_CHECKOUT:
362 enable = buf->lyxvc().inUse() && buf->isReadonly();
366 enable = buf->lyxvc().inUse();
368 case LFUN_MENURELOAD:
369 enable = !buf->isUnnamed() && !buf->isClean();
371 case LFUN_BOOKMARK_GOTO:
372 enable = view()->isSavedPosition(strToUnsignedInt(cmd.argument));
375 case LFUN_MERGE_CHANGES:
376 case LFUN_ACCEPT_CHANGE:
377 case LFUN_REJECT_CHANGE:
378 case LFUN_ACCEPT_ALL_CHANGES:
379 case LFUN_REJECT_ALL_CHANGES:
380 enable = buf && buf->params().tracking_changes;
383 case LFUN_INSET_SETTINGS: {
387 UpdatableInset * inset = cur.inset().asUpdatableInset();
388 lyxerr << "inset: " << inset << endl;
392 InsetOld::Code code = inset->lyxCode();
394 case InsetOld::TABULAR_CODE:
395 enable = cmd.argument == "tabular";
397 case InsetOld::ERT_CODE:
398 enable = cmd.argument == "ert";
400 case InsetOld::FLOAT_CODE:
401 enable = cmd.argument == "float";
403 case InsetOld::WRAP_CODE:
404 enable = cmd.argument == "wrap";
406 case InsetOld::NOTE_CODE:
407 enable = cmd.argument == "note";
409 case InsetOld::BRANCH_CODE:
410 enable = cmd.argument == "branch";
412 case InsetOld::BOX_CODE:
413 enable = cmd.argument == "box";
421 case LFUN_DIALOG_SHOW: {
422 string const name = cmd.getArg(0);
424 enable = name == "aboutlyx"
427 || name == "preferences"
428 || name == "texinfo";
429 else if (name == "print")
430 enable = Exporter::IsExportable(*buf, "dvi")
431 && lyxrc.print_command != "none";
432 else if (name == "character")
433 enable = cur.inset().lyxCode() != InsetOld::ERT_CODE;
434 else if (name == "vclog")
435 enable = buf->lyxvc().inUse();
436 else if (name == "latexlog")
437 enable = IsFileReadable(buf->getLogName().second);
442 case LFUN_MENUNEWTMPLT:
443 case LFUN_WORDFINDFORWARD:
444 case LFUN_WORDFINDBACKWARD:
446 case LFUN_EXEC_COMMAND:
449 case LFUN_CLOSEBUFFER:
458 case LFUN_RECONFIGURE:
462 case LFUN_DROP_LAYOUTS_CHOICE:
463 case LFUN_MENU_OPEN_BY_NAME:
466 case LFUN_GOTOFILEROW:
467 case LFUN_GOTO_PARAGRAPH:
468 case LFUN_DIALOG_SHOW_NEW_INSET:
469 case LFUN_DIALOG_SHOW_NEXT_INSET:
470 case LFUN_DIALOG_UPDATE:
471 case LFUN_DIALOG_HIDE:
472 case LFUN_DIALOG_DISCONNECT_INSET:
474 case LFUN_TOGGLECURSORFOLLOW:
478 case LFUN_KMAP_TOGGLE:
481 case LFUN_SAVEPREFERENCES:
482 case LFUN_SCREEN_FONT_UPDATE:
485 case LFUN_EXTERNAL_EDIT:
486 case LFUN_FILE_INSERT:
487 case LFUN_FILE_INSERT_ASCII:
488 case LFUN_FILE_INSERT_ASCII_PARA:
489 // these are handled in our dispatch()
493 cur.getStatus(cmd, flag);
499 // Can we use a readonly buffer?
500 if (buf && buf->isReadonly()
501 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
502 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
503 setStatusMessage(N_("Document is read-only"));
513 bool ensureBufferClean(BufferView * bv)
515 Buffer & buf = *bv->buffer();
519 string const file = MakeDisplayPath(buf.fileName(), 30);
520 string text = bformat(_("The document %1$s has unsaved "
521 "changes.\n\nDo you want to save "
522 "the document?"), file);
523 int const ret = Alert::prompt(_("Save changed document?"),
524 text, 0, 1, _("&Save"),
528 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
530 return buf.isClean();
536 void LyXFunc::dispatch(FuncRequest const & cmd, bool verbose)
538 string const argument = cmd.argument;
539 kb_action const action = cmd.action;
541 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << cmd << endl;
542 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
544 // we have not done anything wrong yet.
546 dispatch_buffer.erase();
547 selection_possible = false;
549 // We cannot use this function here
550 if (!getStatus(cmd).enabled()) {
551 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
552 << lyxaction.getActionName(action)
553 << " [" << action << "] is disabled at this location"
555 setErrorMessage(getStatusMessage());
559 if (view()->available())
560 view()->hideCursor();
564 case LFUN_WORDFINDFORWARD:
565 case LFUN_WORDFINDBACKWARD: {
566 static string last_search;
567 string searched_string;
569 if (!argument.empty()) {
570 last_search = argument;
571 searched_string = argument;
573 searched_string = last_search;
576 if (searched_string.empty())
579 bool const fw = action == LFUN_WORDFINDFORWARD;
581 lyx::find::find2string(searched_string, true, false, fw);
582 lyx::find::find(view(), FuncRequest(LFUN_WORD_FIND, data));
587 owner->message(keyseq.printOptions());
590 case LFUN_EXEC_COMMAND:
591 owner->focus_command_buffer();
596 meta_fake_bit = key_modifier::none;
597 if (view()->available())
598 // cancel any selection
599 dispatch(FuncRequest(LFUN_MARK_OFF));
600 setMessage(N_("Cancel"));
604 meta_fake_bit = key_modifier::alt;
605 setMessage(keyseq.print());
608 case LFUN_READ_ONLY_TOGGLE:
609 if (owner->buffer()->lyxvc().inUse())
610 owner->buffer()->lyxvc().toggleReadOnly();
612 owner->buffer()->setReadonly(
613 !owner->buffer()->isReadonly());
616 // --- Menus -----------------------------------------------
618 menuNew(argument, false);
621 case LFUN_MENUNEWTMPLT:
622 menuNew(argument, true);
625 case LFUN_CLOSEBUFFER:
630 if (!owner->buffer()->isUnnamed()) {
631 string const str = bformat(_("Saving document %1$s..."),
632 MakeDisplayPath(owner->buffer()->fileName()));
634 MenuWrite(owner->buffer());
635 owner->message(str + _(" done."));
637 WriteAs(owner->buffer());
641 WriteAs(owner->buffer(), argument);
644 case LFUN_MENURELOAD: {
645 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
646 string text = bformat(_("Any changes will be lost. Are you sure "
647 "you want to revert to the saved version of the document %1$s?"), file);
648 int const ret = Alert::prompt(_("Revert to saved document?"),
649 text, 0, 1, _("&Revert"), _("&Cancel"));
657 Exporter::Export(owner->buffer(), argument, true);
658 view()->showErrorList(BufferFormat(*owner->buffer()));
662 Exporter::Preview(owner->buffer(), argument);
663 view()->showErrorList(BufferFormat(*owner->buffer()));
667 Exporter::Export(owner->buffer(), "program", true);
668 view()->showErrorList(_("Build"));
672 owner->buffer()->runChktex();
673 view()->showErrorList(_("ChkTeX"));
677 if (argument == "custom")
678 owner->getDialogs().showSendto();
680 Exporter::Export(owner->buffer(), argument, false);
681 view()->showErrorList(BufferFormat(*owner->buffer()));
691 // test speed of DocumentIterator
692 lyxerr << "start" << endl;
693 for (DocumentIterator it(owner->buffer()->inset()), end;
694 it != end; it.forwardPos())
696 lyxerr << "end" << endl;
700 lyxerr << "sizeof Paragraph: " << sizeof(Paragraph) << endl;
701 lyxerr << "sizeof Spacing: " << sizeof(Spacing) << endl;
702 lyxerr << "sizeof LyXLength: " << sizeof(LyXLength) << endl;
703 lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
704 lyxerr << "sizeof LyXAlignment: " << sizeof(LyXAlignment) << endl;
710 InsetCommandParams p("tableofcontents");
711 string const data = InsetCommandMailer::params2string("toc", p);
712 owner->getDialogs().show("toc", data, 0);
720 case LFUN_RECONFIGURE:
724 case LFUN_HELP_OPEN: {
725 string const arg = argument;
727 setErrorMessage(N_("Missing argument"));
730 string const fname = i18nLibFileSearch("doc", arg, "lyx");
732 lyxerr << "LyX: unable to find documentation file `"
733 << arg << "'. Bad installation?" << endl;
736 owner->message(bformat(_("Opening help file %1$s..."),
737 MakeDisplayPath(fname)));
738 view()->loadLyXFile(fname, false);
742 // --- version control -------------------------------
743 case LFUN_VC_REGISTER:
744 if (!ensureBufferClean(view()))
746 if (!owner->buffer()->lyxvc().inUse()) {
747 owner->buffer()->lyxvc().registrer();
752 case LFUN_VC_CHECKIN:
753 if (!ensureBufferClean(view()))
755 if (owner->buffer()->lyxvc().inUse()
756 && !owner->buffer()->isReadonly()) {
757 owner->buffer()->lyxvc().checkIn();
762 case LFUN_VC_CHECKOUT:
763 if (!ensureBufferClean(view()))
765 if (owner->buffer()->lyxvc().inUse()
766 && owner->buffer()->isReadonly()) {
767 owner->buffer()->lyxvc().checkOut();
773 owner->buffer()->lyxvc().revert();
778 owner->buffer()->lyxvc().undoLast();
782 // --- buffers ----------------------------------------
783 case LFUN_SWITCHBUFFER:
784 view()->setBuffer(bufferlist.getBuffer(argument));
788 NewFile(view(), argument);
795 case LFUN_DROP_LAYOUTS_CHOICE:
796 owner->getToolbar().openLayoutList();
799 case LFUN_MENU_OPEN_BY_NAME:
800 owner->getMenubar().openByName(argument);
803 // --- lyxserver commands ----------------------------
805 setMessage(owner->buffer()->fileName());
806 lyxerr[Debug::INFO] << "FNAME["
807 << owner->buffer()->fileName()
812 dispatch_buffer = keyseq.print();
813 lyxserver->notifyClient(dispatch_buffer);
816 case LFUN_GOTOFILEROW: {
819 istringstream is(argument);
820 is >> file_name >> row;
821 if (prefixIs(file_name, getTmpDir())) {
822 // Needed by inverse dvi search. If it is a file
823 // in tmpdir, call the apropriated function
824 view()->setBuffer(bufferlist.getBufferFromTmp(file_name));
826 // Must replace extension of the file to be .lyx
828 string const s = ChangeExtension(file_name, ".lyx");
829 // Either change buffer or load the file
830 if (bufferlist.exists(s)) {
831 view()->setBuffer(bufferlist.getBuffer(s));
833 view()->loadLyXFile(s);
837 view()->setCursorFromRow(row);
840 // see BufferView_pimpl::center()
841 view()->updateScrollbar();
845 case LFUN_GOTO_PARAGRAPH: {
846 istringstream is(argument);
849 ParIterator par = owner->buffer()->getParFromID(id);
850 if (par == owner->buffer()->par_iterator_end()) {
851 lyxerr[Debug::INFO] << "No matching paragraph found! ["
852 << id << ']' << endl;
855 lyxerr[Debug::INFO] << "Paragraph " << par->id()
856 << " found." << endl;
860 view()->setCursor(par, 0);
862 view()->switchKeyMap();
863 owner->view_state_changed();
866 // see BufferView_pimpl::center()
867 view()->updateScrollbar();
871 case LFUN_DIALOG_SHOW: {
872 string const name = cmd.getArg(0);
873 string data = trim(cmd.argument.substr(name.size()));
875 if (name == "character") {
876 data = freefont2string();
878 owner->getDialogs().show("character", data);
880 else if (name == "document")
881 owner->getDialogs().showDocument();
882 else if (name == "preamble")
883 owner->getDialogs().showPreamble();
884 else if (name == "preferences")
885 owner->getDialogs().showPreferences();
886 else if (name == "print")
887 owner->getDialogs().showPrint();
888 else if (name == "spellchecker")
889 owner->getDialogs().showSpellchecker();
891 else if (name == "latexlog") {
892 pair<Buffer::LogType, string> const logfile =
893 owner->buffer()->getLogName();
894 switch (logfile.first) {
895 case Buffer::latexlog:
898 case Buffer::buildlog:
902 data += logfile.second;
903 owner->getDialogs().show("log", data);
905 else if (name == "vclog") {
906 string const data = "vc " +
907 owner->buffer()->lyxvc().getLogFile();
908 owner->getDialogs().show("log", data);
911 owner->getDialogs().show(name, data);
915 case LFUN_DIALOG_SHOW_NEW_INSET: {
916 string const name = cmd.getArg(0);
917 string data = trim(cmd.argument.substr(name.size()));
918 if (name == "bibitem" ||
926 InsetCommandParams p(name);
927 data = InsetCommandMailer::params2string(name, p);
928 } else if (name == "box") {
929 // \c data == "Boxed" || "Frameless" etc
930 InsetBoxParams p(data);
931 data = InsetBoxMailer::params2string(p);
932 } else if (name == "branch") {
934 data = InsetBranchMailer::params2string(p);
935 } else if (name == "citation") {
936 InsetCommandParams p("cite");
937 data = InsetCommandMailer::params2string(name, p);
938 } else if (name == "ert") {
939 data = InsetERTMailer::params2string(InsetCollapsable::Open);
940 } else if (name == "external") {
941 InsetExternalParams p;
942 Buffer const & buffer = *owner->buffer();
943 data = InsetExternalMailer::params2string(p, buffer);
944 } else if (name == "float") {
946 data = InsetFloatMailer::params2string(p);
947 } else if (name == "graphics") {
948 InsetGraphicsParams p;
949 Buffer const & buffer = *owner->buffer();
950 data = InsetGraphicsMailer::params2string(p, buffer);
951 } else if (name == "note") {
953 data = InsetNoteMailer::params2string(p);
954 } else if (name == "vspace") {
956 data = InsetVSpaceMailer::params2string(space);
957 } else if (name == "wrap") {
959 data = InsetWrapMailer::params2string(p);
961 owner->getDialogs().show(name, data, 0);
965 case LFUN_DIALOG_SHOW_NEXT_INSET:
968 case LFUN_DIALOG_UPDATE: {
969 string const & name = argument;
970 // Can only update a dialog connected to an existing inset
971 InsetBase * inset = owner->getDialogs().getOpenInset(name);
973 FuncRequest fr(LFUN_INSET_DIALOG_UPDATE, cmd.argument);
974 inset->dispatch(view()->cursor(), fr);
975 } else if (name == "paragraph") {
976 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
981 case LFUN_DIALOG_HIDE:
982 Dialogs::hide(argument, 0);
985 case LFUN_DIALOG_DISCONNECT_INSET:
986 owner->getDialogs().disconnect(argument);
989 case LFUN_CHILDOPEN: {
990 string const filename =
991 MakeAbsPath(argument, owner->buffer()->filePath());
992 setMessage(N_("Opening child document ") +
993 MakeDisplayPath(filename) + "...");
994 view()->savePosition(0);
995 string const parentfilename = owner->buffer()->fileName();
996 if (bufferlist.exists(filename))
997 view()->setBuffer(bufferlist.getBuffer(filename));
999 view()->loadLyXFile(filename);
1000 // Set the parent name of the child document.
1001 // This makes insertion of citations and references in the child work,
1002 // when the target is in the parent or another child document.
1003 owner->buffer()->setParentName(parentfilename);
1007 case LFUN_TOGGLECURSORFOLLOW:
1008 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1012 owner->getIntl().KeyMapOn(false);
1015 case LFUN_KMAP_PRIM:
1016 owner->getIntl().KeyMapPrim();
1020 owner->getIntl().KeyMapSec();
1023 case LFUN_KMAP_TOGGLE:
1024 owner->getIntl().ToggleKeyMap();
1030 string rest = split(argument, countstr, ' ');
1031 istringstream is(countstr);
1034 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1035 for (int i = 0; i < count; ++i)
1036 dispatch(lyxaction.lookupFunc(rest));
1041 // argument contains ';'-terminated commands
1042 while (!argument.empty()) {
1044 string rest = split(argument, first, ';');
1045 dispatch(lyxaction.lookupFunc(rest));
1049 case LFUN_SAVEPREFERENCES: {
1050 Path p(user_lyxdir());
1051 lyxrc.write("preferences");
1055 case LFUN_SCREEN_FONT_UPDATE:
1056 // handle the screen font changes.
1057 lyxrc.set_font_norm_type();
1058 lyx_gui::update_fonts();
1059 // All visible buffers will need resize
1063 case LFUN_SET_COLOR: {
1065 string const x11_name = split(argument, lyx_name, ' ');
1066 if (lyx_name.empty() || x11_name.empty()) {
1067 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1072 bool const graphicsbg_changed =
1073 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1074 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1076 if (!lcolor.setColor(lyx_name, x11_name)) {
1078 bformat(_("Set-color \"%1$s\" failed "
1079 "- color is undefined or "
1080 "may not be redefined"), lyx_name));
1084 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1086 if (graphicsbg_changed) {
1087 #ifdef WITH_WARNINGS
1088 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1091 lyx::graphics::GCache::get().changeDisplay(true);
1098 owner->message(argument);
1101 case LFUN_TOOLTIPS_TOGGLE:
1102 owner->getDialogs().toggleTooltips();
1105 case LFUN_EXTERNAL_EDIT: {
1106 FuncRequest fr(action, argument);
1107 InsetExternal().dispatch(view()->cursor(), fr);
1111 case LFUN_BREAKLINE: {
1112 #warning swallow 'Return' if the minibuffer is focused. But how?
1116 DispatchResult res = view()->cursor().dispatch(cmd);
1117 if (!res.dispatched());
1118 view()->dispatch(cmd);
1123 if (view()->available()) {
1124 view()->fitCursor();
1126 view()->cursor().updatePos();
1127 // if we executed a mutating lfun, mark the buffer as dirty
1128 if (getStatus(cmd).enabled()
1129 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)
1130 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly))
1131 view()->buffer()->markDirty();
1134 if (view()->cursor().inTexted()) {
1135 view()->owner()->updateLayoutChoice();
1136 sendDispatchMessage(getMessage(), cmd, verbose);
1142 void LyXFunc::sendDispatchMessage(string const & msg,
1143 FuncRequest const & cmd, bool verbose)
1145 owner->updateMenubar();
1146 owner->updateToolbar();
1148 if (cmd.action == LFUN_SELFINSERT || !verbose) {
1149 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1151 owner->message(msg);
1155 string dispatch_msg = msg;
1156 if (!dispatch_msg.empty())
1157 dispatch_msg += ' ';
1159 string comname = lyxaction.getActionName(cmd.action);
1161 bool argsadded = false;
1163 if (!cmd.argument.empty()) {
1164 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1165 comname += ' ' + cmd.argument;
1170 string const shortcuts = toplevel_keymap->findbinding(cmd);
1172 if (!shortcuts.empty()) {
1173 comname += ": " + shortcuts;
1174 } else if (!argsadded && !cmd.argument.empty()) {
1175 comname += ' ' + cmd.argument;
1178 if (!comname.empty()) {
1179 comname = rtrim(comname);
1180 dispatch_msg += '(' + comname + ')';
1183 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1184 if (!dispatch_msg.empty())
1185 owner->message(dispatch_msg);
1189 void LyXFunc::setupLocalKeymap()
1191 keyseq.stdmap = toplevel_keymap.get();
1192 keyseq.curmap = toplevel_keymap.get();
1193 cancel_meta_seq.stdmap = toplevel_keymap.get();
1194 cancel_meta_seq.curmap = toplevel_keymap.get();
1198 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1200 string initpath = lyxrc.document_path;
1201 string filename(name);
1203 if (view()->available()) {
1204 string const trypath = owner->buffer()->filePath();
1205 // If directory is writeable, use this as default.
1206 if (IsDirWriteable(trypath))
1210 static int newfile_number;
1212 if (filename.empty()) {
1213 filename = AddName(lyxrc.document_path,
1214 "newfile" + tostr(++newfile_number) + ".lyx");
1215 FileInfo fi(filename);
1216 while (bufferlist.exists(filename) || fi.readable()) {
1218 filename = AddName(lyxrc.document_path,
1219 "newfile" + tostr(newfile_number) +
1221 fi.newFile(filename);
1225 // The template stuff
1228 FileDialog fileDlg(_("Select template file"),
1229 LFUN_SELECT_FILE_SYNC,
1230 make_pair(string(_("Documents|#o#O")),
1231 string(lyxrc.document_path)),
1232 make_pair(string(_("Templates|#T#t")),
1233 string(lyxrc.template_path)));
1235 FileDialog::Result result =
1236 fileDlg.open(lyxrc.template_path,
1237 FileFilterList(_("LyX Documents (*.lyx)")),
1240 if (result.first == FileDialog::Later)
1242 if (result.second.empty())
1244 templname = result.second;
1247 view()->newFile(filename, templname, !name.empty());
1251 void LyXFunc::open(string const & fname)
1253 string initpath = lyxrc.document_path;
1255 if (view()->available()) {
1256 string const trypath = owner->buffer()->filePath();
1257 // If directory is writeable, use this as default.
1258 if (IsDirWriteable(trypath))
1264 if (fname.empty()) {
1265 FileDialog fileDlg(_("Select document to open"),
1267 make_pair(string(_("Documents|#o#O")),
1268 string(lyxrc.document_path)),
1269 make_pair(string(_("Examples|#E#e")),
1270 string(AddPath(system_lyxdir(), "examples"))));
1272 FileDialog::Result result =
1273 fileDlg.open(initpath,
1274 FileFilterList(_("LyX Documents (*.lyx)")),
1277 if (result.first == FileDialog::Later)
1280 filename = result.second;
1282 // check selected filename
1283 if (filename.empty()) {
1284 owner->message(_("Canceled."));
1290 // get absolute path of file and add ".lyx" to the filename if
1292 string const fullpath = FileSearch(string(), filename, "lyx");
1293 if (!fullpath.empty()) {
1294 filename = fullpath;
1297 string const disp_fn(MakeDisplayPath(filename));
1299 // if the file doesn't exist, let the user create one
1300 FileInfo const f(filename, true);
1302 // the user specifically chose this name. Believe them.
1303 view()->newFile(filename, "", true);
1307 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1310 if (view()->loadLyXFile(filename)) {
1311 str2 = bformat(_("Document %1$s opened."), disp_fn);
1313 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1315 owner->message(str2);
1319 void LyXFunc::doImport(string const & argument)
1322 string filename = split(argument, format, ' ');
1324 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1325 << " file: " << filename << endl;
1327 // need user interaction
1328 if (filename.empty()) {
1329 string initpath = lyxrc.document_path;
1331 if (view()->available()) {
1332 string const trypath = owner->buffer()->filePath();
1333 // If directory is writeable, use this as default.
1334 if (IsDirWriteable(trypath))
1338 string const text = bformat(_("Select %1$s file to import"),
1339 formats.prettyName(format));
1341 FileDialog fileDlg(text,
1343 make_pair(string(_("Documents|#o#O")),
1344 string(lyxrc.document_path)),
1345 make_pair(string(_("Examples|#E#e")),
1346 string(AddPath(system_lyxdir(), "examples"))));
1348 string const filter = formats.prettyName(format)
1349 + " (*." + formats.extension(format) + ')';
1351 FileDialog::Result result =
1352 fileDlg.open(initpath,
1353 FileFilterList(filter),
1356 if (result.first == FileDialog::Later)
1359 filename = result.second;
1361 // check selected filename
1362 if (filename.empty())
1363 owner->message(_("Canceled."));
1366 if (filename.empty())
1369 // get absolute path of file
1370 filename = MakeAbsPath(filename);
1372 string const lyxfile = ChangeExtension(filename, ".lyx");
1374 // Check if the document already is open
1375 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1376 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1377 owner->message(_("Canceled."));
1382 // if the file exists already, and we didn't do
1383 // -i lyx thefile.lyx, warn
1384 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1385 string const file = MakeDisplayPath(lyxfile, 30);
1387 string text = bformat(_("The document %1$s already exists.\n\n"
1388 "Do you want to over-write that document?"), file);
1389 int const ret = Alert::prompt(_("Over-write document?"),
1390 text, 0, 1, _("&Over-write"), _("&Cancel"));
1393 owner->message(_("Canceled."));
1398 Importer::Import(owner, filename, format);
1402 void LyXFunc::closeBuffer()
1404 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1405 if (bufferlist.empty()) {
1406 // need this otherwise SEGV may occur while
1407 // trying to set variables that don't exist
1408 // since there's no current buffer
1409 owner->getDialogs().hideBufferDependent();
1411 view()->setBuffer(bufferlist.first());
1417 // Each "owner" should have it's own message method. lyxview and
1418 // the minibuffer would use the minibuffer, but lyxserver would
1419 // send an ERROR signal to its client. Alejandro 970603
1420 // This function is bit problematic when it comes to NLS, to make the
1421 // lyx servers client be language indepenent we must not translate
1422 // strings sent to this func.
1423 void LyXFunc::setErrorMessage(string const & m) const
1425 dispatch_buffer = m;
1430 void LyXFunc::setMessage(string const & m) const
1432 dispatch_buffer = m;
1436 void LyXFunc::setStatusMessage(string const & m) const
1442 string const LyXFunc::viewStatusMessage()
1444 // When meta-fake key is pressed, show the key sequence so far + "M-".
1446 return keyseq.print() + "M-";
1448 // Else, when a non-complete key sequence is pressed,
1449 // show the available options.
1450 if (keyseq.length() > 0 && !keyseq.deleted())
1451 return keyseq.printOptions();
1453 if (!view()->available())
1454 return _("Welcome to LyX!");
1456 return view()->cursor().currentState();
1460 BufferView * LyXFunc::view() const
1462 BOOST_ASSERT(owner);
1463 return owner->view().get();
1467 bool LyXFunc::wasMetaKey() const
1469 return (meta_fake_bit != key_modifier::none);