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 "LayoutFile.h"
25 #include "BranchList.h"
26 #include "buffer_funcs.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
33 #include "Converter.h"
35 #include "CutAndPaste.h"
36 #include "DispatchResult.h"
38 #include "ErrorList.h"
40 #include "FuncRequest.h"
41 #include "FuncStatus.h"
42 #include "InsetIterator.h"
47 #include "LyXAction.h"
52 #include "Paragraph.h"
53 #include "ParagraphParameters.h"
54 #include "ParIterator.h"
58 #include "SpellChecker.h"
60 #include "insets/InsetBox.h"
61 #include "insets/InsetBranch.h"
62 #include "insets/InsetCommand.h"
63 #include "insets/InsetERT.h"
64 #include "insets/InsetExternal.h"
65 #include "insets/InsetFloat.h"
66 #include "insets/InsetGraphics.h"
67 #include "insets/InsetInclude.h"
68 #include "insets/InsetListings.h"
69 #include "insets/InsetNote.h"
70 #include "insets/InsetPhantom.h"
71 #include "insets/InsetSpace.h"
72 #include "insets/InsetTabular.h"
73 #include "insets/InsetVSpace.h"
74 #include "insets/InsetWrap.h"
76 #include "frontends/alert.h"
77 #include "frontends/Application.h"
78 #include "frontends/KeySymbol.h"
79 #include "frontends/LyXView.h"
80 #include "frontends/Selection.h"
82 #include "support/debug.h"
83 #include "support/environment.h"
84 #include "support/FileName.h"
85 #include "support/filetools.h"
86 #include "support/gettext.h"
87 #include "support/lstrings.h"
88 #include "support/Path.h"
89 #include "support/Package.h"
90 #include "support/Systemcall.h"
91 #include "support/convert.h"
92 #include "support/os.h"
98 using namespace lyx::support;
102 using frontend::LyXView;
104 namespace Alert = frontend::Alert;
109 // This function runs "configure" and then rereads lyx.defaults to
110 // reconfigure the automatic settings.
111 void reconfigure(LyXView * lv, string const & option)
113 // emit message signal.
115 lv->message(_("Running configure..."));
117 // Run configure in user lyx directory
118 PathChanger p(package().user_support());
119 string configure_command = package().configure_command();
120 configure_command += option;
122 int ret = one.startscript(Systemcall::Wait, configure_command);
124 // emit message signal.
126 lv->message(_("Reloading configuration..."));
127 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
128 // Re-read packages.lst
129 LaTeXFeatures::getAvailable();
132 Alert::information(_("System reconfiguration failed"),
133 _("The system reconfiguration has failed.\n"
134 "Default textclass is used but LyX may "
135 "not be able to work properly.\n"
136 "Please reconfigure again if needed."));
139 Alert::information(_("System reconfigured"),
140 _("The system has been reconfigured.\n"
141 "You need to restart LyX to make use of any\n"
142 "updated document class specifications."));
149 : encoded_last_key(0), meta_fake_bit(NoModifier)
154 void LyXFunc::initKeySequences(KeyMap * kb)
156 keyseq = KeySequence(kb, kb);
157 cancel_meta_seq = KeySequence(kb, kb);
161 void LyXFunc::handleKeyFunc(FuncCode action)
163 char_type c = encoded_last_key;
167 LyXView * lv = theApp()->currentWindow();
168 LASSERT(lv && lv->currentBufferView(), /**/);
169 BufferView * bv = lv->currentBufferView();
170 bv->getIntl().getTransManager().deadkey(
171 c, get_accent(action).accent, bv->cursor().innerText(),
173 // Need to clear, in case the minibuffer calls these
176 // copied verbatim from do_accent_char
177 bv->cursor().resetAnchor();
178 bv->processUpdateFlags(Update::FitCursor);
181 //FIXME: bookmark handling is a frontend issue. This code should be transferred
182 // to GuiView and be GuiView and be window dependent.
183 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
185 LyXView * lv = theApp()->currentWindow();
187 if (!theSession().bookmarks().isValid(idx))
189 BookmarksSection::Bookmark const & bm = theSession().bookmarks().bookmark(idx);
190 LASSERT(!bm.filename.empty(), /**/);
191 string const file = bm.filename.absFilename();
192 // if the file is not opened, open it.
193 if (!theBufferList().exists(bm.filename)) {
195 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
199 // open may fail, so we need to test it again
200 if (!theBufferList().exists(bm.filename))
203 // bm can be changed when saving
204 BookmarksSection::Bookmark tmp = bm;
206 // Special case idx == 0 used for back-from-back jump navigation
208 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
210 // if the current buffer is not that one, switch to it.
211 if (!lv->documentBufferView()
212 || lv->documentBufferView()->buffer().fileName() != tmp.filename) {
215 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
218 // moveToPosition try paragraph id first and then paragraph (pit, pos).
219 if (!lv->documentBufferView()->moveToPosition(
220 tmp.bottom_pit, tmp.bottom_pos, tmp.top_id, tmp.top_pos))
227 // Cursor jump succeeded!
228 Cursor const & cur = lv->documentBufferView()->cursor();
229 pit_type new_pit = cur.pit();
230 pos_type new_pos = cur.pos();
231 int new_id = cur.paragraph().id();
233 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
234 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
235 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
236 || bm.top_id != new_id) {
237 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
238 new_pit, new_pos, new_id);
243 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
245 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
247 LyXView * lv = theApp()->currentWindow();
249 // Do nothing if we have nothing (JMarc)
250 if (!keysym.isOK()) {
251 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
256 if (keysym.isModifier()) {
257 LYXERR(Debug::KEY, "isModifier true");
263 //Encoding const * encoding = lv->documentBufferView()->cursor().getEncoding();
264 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
265 // FIXME: encoded_last_key shadows the member variable of the same
266 // name. Is that intended?
267 char_type encoded_last_key = keysym.getUCSEncoded();
269 // Do a one-deep top-level lookup for
270 // cancel and meta-fake keys. RVDK_PATCH_5
271 cancel_meta_seq.reset();
273 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
274 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
276 // When not cancel or meta-fake, do the normal lookup.
277 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
278 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
279 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
280 // remove Caps Lock and Mod2 as a modifiers
281 func = keyseq.addkey(keysym, (state | meta_fake_bit));
282 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
285 // Dont remove this unless you know what you are doing.
286 meta_fake_bit = NoModifier;
288 // Can this happen now ?
289 if (func.action == LFUN_NOACTION)
290 func = FuncRequest(LFUN_COMMAND_PREFIX);
292 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
293 << keyseq.print(KeySequence::Portable) << ']');
295 // already here we know if it any point in going further
296 // why not return already here if action == -1 and
297 // num_bytes == 0? (Lgb)
299 if (keyseq.length() > 1)
300 lv->message(keyseq.print(KeySequence::ForGui));
303 // Maybe user can only reach the key via holding down shift.
304 // Let's see. But only if shift is the only modifier
305 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
306 LYXERR(Debug::KEY, "Trying without shift");
307 func = keyseq.addkey(keysym, NoModifier);
308 LYXERR(Debug::KEY, "Action now " << func.action);
311 if (func.action == LFUN_UNKNOWN_ACTION) {
312 // Hmm, we didn't match any of the keysequences. See
313 // if it's normal insertable text not already covered
315 if (keysym.isText() && keyseq.length() == 1) {
316 LYXERR(Debug::KEY, "isText() is true, inserting.");
317 func = FuncRequest(LFUN_SELF_INSERT,
318 FuncRequest::KEYBOARD);
320 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
321 lv->message(_("Unknown function."));
327 if (func.action == LFUN_SELF_INSERT) {
328 if (encoded_last_key != 0) {
329 docstring const arg(1, encoded_last_key);
330 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
331 FuncRequest::KEYBOARD));
332 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
342 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
344 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
347 if (cmd.action == LFUN_NOACTION) {
348 flag.message(from_utf8(N_("Nothing to do")));
349 flag.setEnabled(false);
353 switch (cmd.action) {
354 case LFUN_UNKNOWN_ACTION:
356 flag.setEnabled(false);
363 if (flag.unknown()) {
364 flag.message(from_utf8(N_("Unknown action")));
368 if (!flag.enabled()) {
369 if (flag.message().empty())
370 flag.message(from_utf8(N_("Command disabled")));
374 // I would really like to avoid having this switch and rather try to
375 // encode this in the function itself.
376 // -- And I'd rather let an inset decide which LFUNs it is willing
377 // to handle (Andre')
379 switch (cmd.action) {
381 case LFUN_CITATION_INSERT: {
382 FuncRequest fr(LFUN_INSET_INSERT, "citation");
383 enable = getStatus(fr).enabled();
387 // This could be used for the no-GUI version. The GUI version is handled in
388 // LyXView::getStatus(). See above.
390 case LFUN_BUFFER_WRITE:
391 case LFUN_BUFFER_WRITE_AS: {
392 Buffer * b = theBufferList().getBuffer(FileName(cmd.getArg(0)));
393 enable = b && (b->isUnnamed() || !b->isClean());
398 case LFUN_BOOKMARK_GOTO: {
399 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
400 enable = theSession().bookmarks().isValid(num);
404 case LFUN_BOOKMARK_CLEAR:
405 enable = theSession().bookmarks().hasValid();
408 // this one is difficult to get right. As a half-baked
409 // solution, we consider only the first action of the sequence
410 case LFUN_COMMAND_SEQUENCE: {
411 // argument contains ';'-terminated commands
412 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
413 FuncRequest func(lyxaction.lookupFunc(firstcmd));
414 func.origin = cmd.origin;
415 flag = getStatus(func);
419 // we want to check if at least one of these is enabled
420 case LFUN_COMMAND_ALTERNATIVES: {
421 // argument contains ';'-terminated commands
422 string arg = to_utf8(cmd.argument());
423 while (!arg.empty()) {
425 arg = split(arg, first, ';');
426 FuncRequest func(lyxaction.lookupFunc(first));
427 func.origin = cmd.origin;
428 flag = getStatus(func);
429 // if this one is enabled, the whole thing is
438 string name = to_utf8(cmd.argument());
439 if (theTopLevelCmdDef().lock(name, func)) {
440 func.origin = cmd.origin;
441 flag = getStatus(func);
442 theTopLevelCmdDef().release(name);
444 // catch recursion or unknown command
445 // definition. all operations until the
446 // recursion or unknown command definition
447 // occurs are performed, so set the state to
454 case LFUN_COMMAND_PREFIX:
456 case LFUN_META_PREFIX:
457 case LFUN_RECONFIGURE:
458 case LFUN_DROP_LAYOUTS_CHOICE:
459 case LFUN_SERVER_GET_FILENAME:
460 case LFUN_SERVER_NOTIFY:
461 case LFUN_SERVER_GOTO_FILE_ROW:
462 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
464 case LFUN_PREFERENCES_SAVE:
465 case LFUN_INSET_EDIT:
466 case LFUN_BUFFER_SAVE_AS_DEFAULT:
467 case LFUN_LYXRC_APPLY:
468 // these are handled in our dispatch()
476 if (theApp()->getStatus(cmd, flag))
479 // Does the view know something?
480 LyXView * lv = theApp()->currentWindow();
485 if (lv->getStatus(cmd, flag))
488 BufferView * bv = lv->currentBufferView();
489 BufferView * doc_bv = lv->documentBufferView();
490 // If we do not have a BufferView, then other functions are disabled
495 // try the BufferView
496 bool decided = bv->getStatus(cmd, flag);
499 decided = bv->buffer().getStatus(cmd, flag);
500 if (!decided && doc_bv)
501 // try the Document Buffer
502 decided = doc_bv->buffer().getStatus(cmd, flag);
506 flag.setEnabled(false);
508 // the default error message if we disable the command
509 if (!flag.enabled() && flag.message().empty())
510 flag.message(from_utf8(N_("Command disabled")));
518 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
523 void LyXFunc::dispatch(FuncRequest const & cmd)
525 string const argument = to_utf8(cmd.argument());
526 FuncCode const action = cmd.action;
528 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
529 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
531 // we have not done anything wrong yet.
533 dispatch_buffer.erase();
535 // redraw the screen at the end (first of the two drawing steps).
536 //This is done unless explicitely requested otherwise
537 Update::flags updateFlags = Update::FitCursor;
539 LyXView * lv = theApp()->currentWindow();
541 FuncStatus const flag = getStatus(cmd);
542 if (!flag.enabled()) {
543 // We cannot use this function here
544 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
545 << lyxaction.getActionName(action)
546 << " [" << action << "] is disabled at this location");
547 setErrorMessage(flag.message());
552 if (lv && lv->currentBufferView())
553 buffer = &lv->currentBufferView()->buffer();
556 case LFUN_COMMAND_PREFIX:
558 lv->message(keyseq.printOptions(true));
562 LASSERT(lv && lv->currentBufferView(), /**/);
564 meta_fake_bit = NoModifier;
566 // cancel any selection
567 dispatch(FuncRequest(LFUN_MARK_OFF));
568 setMessage(from_ascii(N_("Cancel")));
571 case LFUN_META_PREFIX:
572 meta_fake_bit = AltModifier;
573 setMessage(keyseq.print(KeySequence::ForGui));
576 // --- Menus -----------------------------------------------
577 case LFUN_RECONFIGURE:
578 // argument is any additional parameter to the configure.py command
579 reconfigure(lv, argument);
582 // --- lyxserver commands ----------------------------
583 case LFUN_SERVER_GET_FILENAME:
584 LASSERT(lv && buffer, /**/);
585 setMessage(from_utf8(buffer->absFileName()));
586 LYXERR(Debug::INFO, "FNAME["
587 << buffer->absFileName() << ']');
590 case LFUN_SERVER_NOTIFY:
591 dispatch_buffer = keyseq.print(KeySequence::Portable);
592 theServer().notifyClient(to_utf8(dispatch_buffer));
595 case LFUN_SERVER_GOTO_FILE_ROW: {
599 istringstream is(argument);
600 is >> file_name >> row;
601 file_name = os::internal_path(file_name);
604 string const abstmp = package().temp_dir().absFilename();
605 string const realtmp = package().temp_dir().realPath();
606 // We have to use os::path_prefix_is() here, instead of
607 // simply prefixIs(), because the file name comes from
608 // an external application and may need case adjustment.
609 if (os::path_prefix_is(file_name, abstmp, os::CASE_ADJUSTED)
610 || os::path_prefix_is(file_name, realtmp, os::CASE_ADJUSTED)) {
611 // Needed by inverse dvi search. If it is a file
612 // in tmpdir, call the apropriated function.
613 // If tmpdir is a symlink, we may have the real
614 // path passed back, so we correct for that.
615 if (!prefixIs(file_name, abstmp))
616 file_name = subst(file_name, realtmp, abstmp);
617 buf = theBufferList().getBufferFromTmp(file_name);
619 // Must replace extension of the file to be .lyx
621 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
622 // Either change buffer or load the file
623 if (theBufferList().exists(s))
624 buf = theBufferList().getBuffer(s);
625 else if (s.exists()) {
626 buf = lv->loadDocument(s);
630 _("File does not exist: %1$s"),
631 makeDisplayPath(file_name)));
635 updateFlags = Update::None;
641 lv->documentBufferView()->setCursorFromRow(row);
643 buf->errors("Parse");
644 updateFlags = Update::FitCursor;
649 case LFUN_DIALOG_SHOW_NEW_INSET: {
651 string const name = cmd.getArg(0);
652 InsetCode code = insetCode(name);
653 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
654 bool insetCodeOK = true;
661 case NOMENCL_PRINT_CODE:
664 case HYPERLINK_CODE: {
665 InsetCommandParams p(code);
666 data = InsetCommand::params2string(name, p);
670 // data is the include type: one of "include",
671 // "input", "verbatiminput" or "verbatiminput*"
673 // default type is requested
675 InsetCommandParams p(INCLUDE_CODE, data);
676 data = InsetCommand::params2string("include", p);
680 // \c data == "Boxed" || "Frameless" etc
681 InsetBoxParams p(data);
682 data = InsetBox::params2string(p);
687 data = InsetBranch::params2string(p);
691 InsetCommandParams p(CITE_CODE);
692 data = InsetCommand::params2string(name, p);
696 data = InsetERT::params2string(InsetCollapsable::Open);
699 case EXTERNAL_CODE: {
700 InsetExternalParams p;
701 data = InsetExternal::params2string(p, *buffer);
706 data = InsetFloat::params2string(p);
709 case LISTINGS_CODE: {
710 InsetListingsParams p;
711 data = InsetListings::params2string(p);
714 case GRAPHICS_CODE: {
715 InsetGraphicsParams p;
716 data = InsetGraphics::params2string(p, *buffer);
721 data = InsetNote::params2string(p);
725 InsetPhantomParams p;
726 data = InsetPhantom::params2string(p);
731 data = InsetSpace::params2string(p);
736 data = InsetVSpace::params2string(space);
741 data = InsetWrap::params2string(p);
745 lyxerr << "Inset type '" << name <<
746 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
749 } // end switch(code)
751 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
755 case LFUN_CITATION_INSERT: {
757 if (!argument.empty()) {
758 // we can have one optional argument, delimited by '|'
759 // citation-insert <key>|<text_before>
760 // this should be enhanced to also support text_after
761 // and citation style
762 string arg = argument;
764 if (contains(argument, "|")) {
765 arg = token(argument, '|', 0);
766 opt1 = token(argument, '|', 1);
768 InsetCommandParams icp(CITE_CODE);
769 icp["key"] = from_utf8(arg);
771 icp["before"] = from_utf8(opt1);
772 string icstr = InsetCommand::params2string("citation", icp);
773 FuncRequest fr(LFUN_INSET_INSERT, icstr);
776 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
780 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
782 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
788 string rest = split(argument, countstr, ' ');
789 istringstream is(countstr);
792 //lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
793 for (int i = 0; i < count; ++i)
794 dispatch(lyxaction.lookupFunc(rest));
798 case LFUN_COMMAND_SEQUENCE: {
799 // argument contains ';'-terminated commands
800 string arg = argument;
801 if (theBufferList().isLoaded(buffer))
802 buffer->undo().beginUndoGroup();
803 while (!arg.empty()) {
805 arg = split(arg, first, ';');
806 FuncRequest func(lyxaction.lookupFunc(first));
807 func.origin = cmd.origin;
810 if (theBufferList().isLoaded(buffer))
811 buffer->undo().endUndoGroup();
815 case LFUN_COMMAND_ALTERNATIVES: {
816 // argument contains ';'-terminated commands
817 string arg = argument;
818 while (!arg.empty()) {
820 arg = split(arg, first, ';');
821 FuncRequest func(lyxaction.lookupFunc(first));
822 func.origin = cmd.origin;
823 FuncStatus stat = getStatus(func);
824 if (stat.enabled()) {
834 if (theTopLevelCmdDef().lock(argument, func)) {
835 func.origin = cmd.origin;
837 theTopLevelCmdDef().release(argument);
839 if (func.action == LFUN_UNKNOWN_ACTION) {
840 // unknown command definition
841 lyxerr << "Warning: unknown command definition `"
845 // recursion detected
846 lyxerr << "Warning: Recursion in the command definition `"
847 << argument << "' detected"
854 case LFUN_PREFERENCES_SAVE: {
855 lyxrc.write(makeAbsPath("preferences",
856 package().user_support().absFilename()),
861 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
863 addName(addPath(package().user_support().absFilename(), "templates/"),
865 Buffer defaults(fname);
867 istringstream ss(argument);
870 int const unknown_tokens = defaults.readHeader(lex);
872 if (unknown_tokens != 0) {
873 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
874 << unknown_tokens << " unknown token"
875 << (unknown_tokens == 1 ? "" : "s")
879 if (defaults.writeFile(FileName(defaults.absFileName())))
880 setMessage(bformat(_("Document defaults saved in %1$s"),
881 makeDisplayPath(fname)));
883 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
887 case LFUN_LYXRC_APPLY: {
888 // reset active key sequences, since the bindings
889 // are updated (bug 6064)
891 LyXRC const lyxrc_orig = lyxrc;
893 istringstream ss(argument);
894 bool const success = lyxrc.read(ss) == 0;
897 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
898 << "Unable to read lyxrc data"
903 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
907 theApp()->resetGui();
909 /// We force the redraw in any case because there might be
910 /// some screen font changes.
911 /// FIXME: only the current view will be updated. the Gui
912 /// class is able to furnish the list of views.
913 updateFlags = Update::Force;
917 case LFUN_BOOKMARK_GOTO:
918 // go to bookmark, open unopened file and switch to buffer if necessary
919 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
920 updateFlags = Update::FitCursor;
923 case LFUN_BOOKMARK_CLEAR:
924 theSession().bookmarks().clear();
928 LASSERT(theApp(), /**/);
929 // Let the frontend dispatch its own actions.
930 if (theApp()->dispatch(cmd))
931 // Nothing more to do.
934 // Everything below is only for active window
938 // Start an undo group. This may be needed for
939 // some stuff like inset-apply on labels.
940 if (theBufferList().isLoaded(buffer))
941 buffer->undo().beginUndoGroup();
943 // Let the current LyXView dispatch its own actions.
944 if (lv->dispatch(cmd)) {
945 BufferView * bv = lv->currentBufferView();
947 buffer = &(bv->buffer());
948 updateFlags = bv->cursor().result().update();
949 if (theBufferList().isLoaded(buffer))
950 buffer->undo().endUndoGroup();
955 BufferView * bv = lv->currentBufferView();
958 // Let the current BufferView dispatch its own actions.
959 if (bv->dispatch(cmd)) {
960 // The BufferView took care of its own updates if needed.
961 buffer = &(bv->buffer());
962 updateFlags = Update::None;
963 if (theBufferList().isLoaded(buffer))
964 buffer->undo().endUndoGroup();
968 BufferView * doc_bv = lv->documentBufferView();
969 // Try with the document BufferView dispatch if any.
970 if (doc_bv && doc_bv->dispatch(cmd)) {
971 // The BufferView took care of its own updates if needed.
972 buffer = &(doc_bv->buffer());
973 updateFlags = Update::None;
974 if (theBufferList().isLoaded(buffer))
975 buffer->undo().endUndoGroup();
979 // OK, so try the current Buffer itself...
981 bv->buffer().dispatch(cmd, dr);
982 if (dr.dispatched()) {
983 updateFlags = dr.update();
986 // and with the document Buffer.
988 doc_bv->buffer().dispatch(cmd, dr);
989 if (dr.dispatched()) {
990 updateFlags = dr.update();
995 // Is this a function that acts on inset at point?
996 Inset * inset = bv->cursor().nextInset();
997 if (lyxaction.funcHasFlag(action, LyXAction::AtPoint)
999 bv->cursor().result().dispatched(true);
1000 bv->cursor().result().update(Update::FitCursor | Update::Force);
1001 FuncRequest tmpcmd = cmd;
1002 inset->dispatch(bv->cursor(), tmpcmd);
1003 if (bv->cursor().result().dispatched()) {
1004 updateFlags = bv->cursor().result().update();
1009 // Let the current Cursor dispatch its own actions.
1010 Cursor old = bv->cursor();
1011 bv->cursor().getPos(cursorPosBeforeDispatchX_,
1012 cursorPosBeforeDispatchY_);
1013 bv->cursor().dispatch(cmd);
1015 // notify insets we just left
1016 if (bv->cursor() != old) {
1018 bool badcursor = notifyCursorLeavesOrEnters(old, bv->cursor());
1020 bv->cursor().fixIfBroken();
1023 if (theBufferList().isLoaded(buffer))
1024 buffer->undo().endUndoGroup();
1026 // update completion. We do it here and not in
1027 // processKeySym to avoid another redraw just for a
1028 // changed inline completion
1029 if (cmd.origin == FuncRequest::KEYBOARD) {
1030 if (cmd.action == LFUN_SELF_INSERT
1031 || (cmd.action == LFUN_ERT_INSERT && bv->cursor().inMathed()))
1032 lv->updateCompletion(bv->cursor(), true, true);
1033 else if (cmd.action == LFUN_CHAR_DELETE_BACKWARD)
1034 lv->updateCompletion(bv->cursor(), false, true);
1036 lv->updateCompletion(bv->cursor(), false, false);
1039 updateFlags = bv->cursor().result().update();
1042 // if we executed a mutating lfun, mark the buffer as dirty
1043 if (theBufferList().isLoaded(buffer) && flag.enabled()
1044 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1045 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1046 buffer->markDirty();
1048 if (lv && lv->currentBufferView()) {
1049 // BufferView::update() updates the ViewMetricsInfo and
1050 // also initializes the position cache for all insets in
1051 // (at least partially) visible top-level paragraphs.
1052 // We will redraw the screen only if needed.
1053 lv->currentBufferView()->processUpdateFlags(updateFlags);
1055 // Do we have a selection?
1056 theSelection().haveSelection(
1057 lv->currentBufferView()->cursor().selection());
1060 lv->restartCursor();
1064 // Some messages may already be translated, so we cannot use _()
1065 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1070 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1072 const bool verbose = (cmd.origin == FuncRequest::MENU
1073 || cmd.origin == FuncRequest::TOOLBAR
1074 || cmd.origin == FuncRequest::COMMANDBUFFER);
1076 LyXView * lv = theApp()->currentWindow();
1077 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1078 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1084 docstring dispatch_msg = msg;
1085 if (!dispatch_msg.empty())
1086 dispatch_msg += ' ';
1088 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1090 bool argsadded = false;
1092 if (!cmd.argument().empty()) {
1093 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1094 comname += ' ' + cmd.argument();
1099 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd, KeySequence::ForGui);
1101 if (!shortcuts.empty())
1102 comname += ": " + shortcuts;
1103 else if (!argsadded && !cmd.argument().empty())
1104 comname += ' ' + cmd.argument();
1106 if (!comname.empty()) {
1107 comname = rtrim(comname);
1108 dispatch_msg += '(' + rtrim(comname) + ')';
1111 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1112 if (!dispatch_msg.empty())
1113 lv->message(dispatch_msg);
1117 // Each LyXView should have it's own message method. lyxview and
1118 // the minibuffer would use the minibuffer, but lyxserver would
1119 // send an ERROR signal to its client. Alejandro 970603
1120 // This function is bit problematic when it comes to NLS, to make the
1121 // lyx servers client be language indepenent we must not translate
1122 // strings sent to this func.
1123 void LyXFunc::setErrorMessage(docstring const & m) const
1125 dispatch_buffer = m;
1130 void LyXFunc::setMessage(docstring const & m) const
1132 dispatch_buffer = m;
1136 docstring LyXFunc::viewStatusMessage()
1138 // When meta-fake key is pressed, show the key sequence so far + "M-".
1140 return keyseq.print(KeySequence::ForGui) + "M-";
1142 // Else, when a non-complete key sequence is pressed,
1143 // show the available options.
1144 if (keyseq.length() > 0 && !keyseq.deleted())
1145 return keyseq.printOptions(true);
1147 LyXView * lv = theApp()->currentWindow();
1149 if (!lv->currentBufferView())
1150 return _("Welcome to LyX!");
1152 return lv->currentBufferView()->cursor().currentState();
1156 bool LyXFunc::wasMetaKey() const
1158 return (meta_fake_bit != NoModifier);
1164 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1166 // Why the switch you might ask. It is a trick to ensure that all
1167 // the elements in the LyXRCTags enum is handled. As you can see
1168 // there are no breaks at all. So it is just a huge fall-through.
1169 // The nice thing is that we will get a warning from the compiler
1170 // if we forget an element.
1171 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1173 case LyXRC::RC_ACCEPT_COMPOUND:
1174 case LyXRC::RC_ALT_LANG:
1175 case LyXRC::RC_PLAINTEXT_LINELEN:
1176 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1177 case LyXRC::RC_AUTOCORRECTION_MATH:
1178 case LyXRC::RC_AUTOREGIONDELETE:
1179 case LyXRC::RC_AUTORESET_OPTIONS:
1180 case LyXRC::RC_AUTOSAVE:
1181 case LyXRC::RC_AUTO_NUMBER:
1182 case LyXRC::RC_BACKUPDIR_PATH:
1183 case LyXRC::RC_BIBTEX_ALTERNATIVES:
1184 case LyXRC::RC_BIBTEX_COMMAND:
1185 case LyXRC::RC_BINDFILE:
1186 case LyXRC::RC_CHECKLASTFILES:
1187 case LyXRC::RC_COMPLETION_CURSOR_TEXT:
1188 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1189 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1190 case LyXRC::RC_COMPLETION_INLINE_MATH:
1191 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1192 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1193 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1194 case LyXRC::RC_COMPLETION_POPUP_MATH:
1195 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1196 case LyXRC::RC_USELASTFILEPOS:
1197 case LyXRC::RC_LOADSESSION:
1198 case LyXRC::RC_CHKTEX_COMMAND:
1199 case LyXRC::RC_CONVERTER:
1200 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1201 case LyXRC::RC_COPIER:
1202 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1203 case LyXRC::RC_SCROLL_BELOW_DOCUMENT:
1204 case LyXRC::RC_DATE_INSERT_FORMAT:
1205 case LyXRC::RC_DEFAULT_LANGUAGE:
1206 case LyXRC::RC_GUI_LANGUAGE:
1207 case LyXRC::RC_DEFAULT_PAPERSIZE:
1208 case LyXRC::RC_DEFAULT_VIEW_FORMAT:
1209 case LyXRC::RC_DEFFILE:
1210 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1211 case LyXRC::RC_DISPLAY_GRAPHICS:
1212 case LyXRC::RC_DOCUMENTPATH:
1213 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1214 FileName path(lyxrc_new.document_path);
1215 if (path.exists() && path.isDirectory())
1216 package().document_dir() = FileName(lyxrc.document_path);
1218 case LyXRC::RC_EDITOR_ALTERNATIVES:
1219 case LyXRC::RC_ESC_CHARS:
1220 case LyXRC::RC_EXAMPLEPATH:
1221 case LyXRC::RC_FONT_ENCODING:
1222 case LyXRC::RC_FORMAT:
1223 case LyXRC::RC_GROUP_LAYOUTS:
1224 case LyXRC::RC_HUNSPELLDIR_PATH:
1225 case LyXRC::RC_INDEX_ALTERNATIVES:
1226 case LyXRC::RC_INDEX_COMMAND:
1227 case LyXRC::RC_JBIBTEX_COMMAND:
1228 case LyXRC::RC_JINDEX_COMMAND:
1229 case LyXRC::RC_NOMENCL_COMMAND:
1230 case LyXRC::RC_INPUT:
1231 case LyXRC::RC_KBMAP:
1232 case LyXRC::RC_KBMAP_PRIMARY:
1233 case LyXRC::RC_KBMAP_SECONDARY:
1234 case LyXRC::RC_LABEL_INIT_LENGTH:
1235 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1236 case LyXRC::RC_LANGUAGE_AUTO_END:
1237 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1238 case LyXRC::RC_LANGUAGE_COMMAND_END:
1239 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1240 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1241 case LyXRC::RC_LANGUAGE_PACKAGE:
1242 case LyXRC::RC_LANGUAGE_USE_BABEL:
1243 case LyXRC::RC_MAC_LIKE_WORD_MOVEMENT:
1244 case LyXRC::RC_MACRO_EDIT_STYLE:
1245 case LyXRC::RC_MAKE_BACKUP:
1246 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1247 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1248 case LyXRC::RC_NUMLASTFILES:
1249 case LyXRC::RC_PARAGRAPH_MARKERS:
1250 case LyXRC::RC_PATH_PREFIX:
1251 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1252 prependEnvPath("PATH", lyxrc.path_prefix);
1254 case LyXRC::RC_PERS_DICT:
1255 case LyXRC::RC_PREVIEW:
1256 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1257 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1258 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1259 case LyXRC::RC_PRINTCOPIESFLAG:
1260 case LyXRC::RC_PRINTER:
1261 case LyXRC::RC_PRINTEVENPAGEFLAG:
1262 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1263 case LyXRC::RC_PRINTFILEEXTENSION:
1264 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1265 case LyXRC::RC_PRINTODDPAGEFLAG:
1266 case LyXRC::RC_PRINTPAGERANGEFLAG:
1267 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1268 case LyXRC::RC_PRINTPAPERFLAG:
1269 case LyXRC::RC_PRINTREVERSEFLAG:
1270 case LyXRC::RC_PRINTSPOOL_COMMAND:
1271 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1272 case LyXRC::RC_PRINTTOFILE:
1273 case LyXRC::RC_PRINTTOPRINTER:
1274 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1275 case LyXRC::RC_PRINT_COMMAND:
1276 case LyXRC::RC_RTL_SUPPORT:
1277 case LyXRC::RC_SCREEN_DPI:
1278 case LyXRC::RC_SCREEN_FONT_ROMAN:
1279 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1280 case LyXRC::RC_SCREEN_FONT_SANS:
1281 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1282 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1283 case LyXRC::RC_SCREEN_FONT_SIZES:
1284 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1285 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1286 case LyXRC::RC_GEOMETRY_SESSION:
1287 case LyXRC::RC_SCREEN_ZOOM:
1288 case LyXRC::RC_SERVERPIPE:
1289 case LyXRC::RC_SET_COLOR:
1290 case LyXRC::RC_SHOW_BANNER:
1291 case LyXRC::RC_OPEN_BUFFERS_IN_TABS:
1292 case LyXRC::RC_SPELL_COMMAND:
1293 case LyXRC::RC_SPELLCHECKER:
1294 case LyXRC::RC_SPELLCHECK_CONTINUOUSLY:
1295 case LyXRC::RC_SPLITINDEX_COMMAND:
1296 case LyXRC::RC_TEMPDIRPATH:
1297 case LyXRC::RC_TEMPLATEPATH:
1298 case LyXRC::RC_TEX_ALLOWS_SPACES:
1299 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
1300 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
1301 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
1303 case LyXRC::RC_THESAURUSDIRPATH:
1304 case LyXRC::RC_UIFILE:
1305 case LyXRC::RC_USER_EMAIL:
1306 case LyXRC::RC_USER_NAME:
1307 case LyXRC::RC_USETEMPDIR:
1308 case LyXRC::RC_USE_ALT_LANG:
1309 case LyXRC::RC_USE_CONVERTER_CACHE:
1310 case LyXRC::RC_USE_ESC_CHARS:
1311 case LyXRC::RC_USE_INP_ENC:
1312 case LyXRC::RC_USE_PERS_DICT:
1313 case LyXRC::RC_USE_TOOLTIP:
1314 case LyXRC::RC_USE_PIXMAP_CACHE:
1315 case LyXRC::RC_USE_SPELL_LIB:
1316 case LyXRC::RC_VIEWDVI_PAPEROPTION:
1317 case LyXRC::RC_SORT_LAYOUTS:
1318 case LyXRC::RC_FULL_SCREEN_LIMIT:
1319 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
1320 case LyXRC::RC_FULL_SCREEN_MENUBAR:
1321 case LyXRC::RC_FULL_SCREEN_TABBAR:
1322 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
1323 case LyXRC::RC_FULL_SCREEN_WIDTH:
1324 case LyXRC::RC_VISUAL_CURSOR:
1325 case LyXRC::RC_VIEWER:
1326 case LyXRC::RC_VIEWER_ALTERNATIVES:
1327 case LyXRC::RC_LAST: