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"
59 #include "insets/InsetBox.h"
60 #include "insets/InsetBranch.h"
61 #include "insets/InsetCommand.h"
62 #include "insets/InsetERT.h"
63 #include "insets/InsetExternal.h"
64 #include "insets/InsetFloat.h"
65 #include "insets/InsetGraphics.h"
66 #include "insets/InsetInclude.h"
67 #include "insets/InsetListings.h"
68 #include "insets/InsetNote.h"
69 #include "insets/InsetSpace.h"
70 #include "insets/InsetTabular.h"
71 #include "insets/InsetVSpace.h"
72 #include "insets/InsetWrap.h"
74 #include "frontends/alert.h"
75 #include "frontends/Application.h"
76 #include "frontends/KeySymbol.h"
77 #include "frontends/LyXView.h"
78 #include "frontends/Selection.h"
80 #include "support/debug.h"
81 #include "support/environment.h"
82 #include "support/FileName.h"
83 #include "support/filetools.h"
84 #include "support/gettext.h"
85 #include "support/lstrings.h"
86 #include "support/Path.h"
87 #include "support/Package.h"
88 #include "support/Systemcall.h"
89 #include "support/convert.h"
90 #include "support/os.h"
96 using namespace lyx::support;
100 using frontend::LyXView;
102 namespace Alert = frontend::Alert;
107 // This function runs "configure" and then rereads lyx.defaults to
108 // reconfigure the automatic settings.
109 void reconfigure(LyXView * lv, string const & option)
111 // emit message signal.
113 lv->message(_("Running configure..."));
115 // Run configure in user lyx directory
116 PathChanger p(package().user_support());
117 string configure_command = package().configure_command();
118 configure_command += option;
120 int ret = one.startscript(Systemcall::Wait, configure_command);
122 // emit message signal.
124 lv->message(_("Reloading configuration..."));
125 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
126 // Re-read packages.lst
127 LaTeXFeatures::getAvailable();
130 Alert::information(_("System reconfiguration failed"),
131 _("The system reconfiguration has failed.\n"
132 "Default textclass is used but LyX may "
133 "not be able to work properly.\n"
134 "Please reconfigure again if needed."));
137 Alert::information(_("System reconfigured"),
138 _("The system has been reconfigured.\n"
139 "You need to restart LyX to make use of any\n"
140 "updated document class specifications."));
144 bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status)
146 // Try to fix cursor in case it is broken.
147 cursor.fixIfBroken();
149 // This is, of course, a mess. Better create a new doc iterator and use
150 // this in Inset::getStatus. This might require an additional
151 // BufferView * arg, though (which should be avoided)
152 //Cursor safe = *this;
154 for ( ; cursor.depth(); cursor.pop()) {
155 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
156 LASSERT(cursor.idx() <= cursor.lastidx(), /**/);
157 LASSERT(cursor.pit() <= cursor.lastpit(), /**/);
158 LASSERT(cursor.pos() <= cursor.lastpos(), /**/);
160 // The inset's getStatus() will return 'true' if it made
161 // a definitive decision on whether it want to handle the
162 // request or not. The result of this decision is put into
163 // the 'status' parameter.
164 if (cursor.inset().getStatus(cursor, cmd, status)) {
173 /** Return the change status at cursor position, taking in account the
174 * status at each level of the document iterator (a table in a deleted
175 * footnote is deleted).
176 * When \param outer is true, the top slice is not looked at.
178 Change::Type lookupChangeType(DocIterator const & dit, bool outer = false)
180 size_t const depth = dit.depth() - (outer ? 1 : 0);
182 for (size_t i = 0 ; i < depth ; ++i) {
183 CursorSlice const & slice = dit[i];
184 if (!slice.inset().inMathed()
185 && slice.pos() < slice.paragraph().size()) {
186 Change::Type const ch = slice.paragraph().lookupChange(slice.pos()).type;
187 if (ch != Change::UNCHANGED)
191 return Change::UNCHANGED;
198 : lyx_view_(0), encoded_last_key(0), meta_fake_bit(NoModifier)
203 void LyXFunc::initKeySequences(KeyMap * kb)
205 keyseq = KeySequence(kb, kb);
206 cancel_meta_seq = KeySequence(kb, kb);
210 void LyXFunc::setLyXView(LyXView * lv)
212 if (lyx_view_ && lyx_view_->view() && lyx_view_ != lv)
213 // save current selection to the selection buffer to allow
214 // middle-button paste in another window
215 cap::saveSelection(lyx_view_->view()->cursor());
220 void LyXFunc::handleKeyFunc(FuncCode action)
222 char_type c = encoded_last_key;
227 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
228 lyx_view_->view()->getIntl().getTransManager().deadkey(
229 c, get_accent(action).accent, view()->cursor().innerText(), view()->cursor());
230 // Need to clear, in case the minibuffer calls these
233 // copied verbatim from do_accent_char
234 view()->cursor().resetAnchor();
235 view()->processUpdateFlags(Update::FitCursor);
238 //FIXME: bookmark handling is a frontend issue. This code should be transferred
239 // to GuiView and be GuiView and be window dependent.
240 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
242 LASSERT(lyx_view_, /**/);
243 if (!LyX::ref().session().bookmarks().isValid(idx))
245 BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
246 LASSERT(!bm.filename.empty(), /**/);
247 string const file = bm.filename.absFilename();
248 // if the file is not opened, open it.
249 if (!theBufferList().exists(file)) {
251 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
255 // open may fail, so we need to test it again
256 if (!theBufferList().exists(file))
259 // if the current buffer is not that one, switch to it.
260 if (lyx_view_->buffer()->absFileName() != file) {
263 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
265 // moveToPosition try paragraph id first and then paragraph (pit, pos).
266 if (!view()->moveToPosition(bm.bottom_pit, bm.bottom_pos,
267 bm.top_id, bm.top_pos))
270 // Cursor jump succeeded!
271 Cursor const & cur = view()->cursor();
272 pit_type new_pit = cur.pit();
273 pos_type new_pos = cur.pos();
274 int new_id = cur.paragraph().id();
276 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
277 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
278 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
279 || bm.top_id != new_id) {
280 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
281 new_pit, new_pos, new_id);
286 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
288 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
290 // Do nothing if we have nothing (JMarc)
291 if (!keysym.isOK()) {
292 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
293 lyx_view_->restartCursor();
297 if (keysym.isModifier()) {
298 LYXERR(Debug::KEY, "isModifier true");
300 lyx_view_->restartCursor();
304 //Encoding const * encoding = view()->cursor().getEncoding();
305 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
306 // FIXME: encoded_last_key shadows the member variable of the same
307 // name. Is that intended?
308 char_type encoded_last_key = keysym.getUCSEncoded();
310 // Do a one-deep top-level lookup for
311 // cancel and meta-fake keys. RVDK_PATCH_5
312 cancel_meta_seq.reset();
314 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
315 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
317 // When not cancel or meta-fake, do the normal lookup.
318 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
319 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
320 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
321 // remove Caps Lock and Mod2 as a modifiers
322 func = keyseq.addkey(keysym, (state | meta_fake_bit));
323 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
326 // Dont remove this unless you know what you are doing.
327 meta_fake_bit = NoModifier;
329 // Can this happen now ?
330 if (func.action == LFUN_NOACTION)
331 func = FuncRequest(LFUN_COMMAND_PREFIX);
333 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
334 << keyseq.print(KeySequence::Portable) << ']');
336 // already here we know if it any point in going further
337 // why not return already here if action == -1 and
338 // num_bytes == 0? (Lgb)
340 if (keyseq.length() > 1)
341 lyx_view_->message(keyseq.print(KeySequence::ForGui));
344 // Maybe user can only reach the key via holding down shift.
345 // Let's see. But only if shift is the only modifier
346 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
347 LYXERR(Debug::KEY, "Trying without shift");
348 func = keyseq.addkey(keysym, NoModifier);
349 LYXERR(Debug::KEY, "Action now " << func.action);
352 if (func.action == LFUN_UNKNOWN_ACTION) {
353 // Hmm, we didn't match any of the keysequences. See
354 // if it's normal insertable text not already covered
356 if (keysym.isText() && keyseq.length() == 1) {
357 LYXERR(Debug::KEY, "isText() is true, inserting.");
358 func = FuncRequest(LFUN_SELF_INSERT,
359 FuncRequest::KEYBOARD);
361 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
362 lyx_view_->message(_("Unknown function."));
363 lyx_view_->restartCursor();
368 if (func.action == LFUN_SELF_INSERT) {
369 if (encoded_last_key != 0) {
370 docstring const arg(1, encoded_last_key);
371 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
372 FuncRequest::KEYBOARD));
373 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
383 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
385 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
388 Buffer * buf = lyx_view_ ? lyx_view_->buffer() : 0;
390 if (cmd.action == LFUN_NOACTION) {
391 flag.message(from_utf8(N_("Nothing to do")));
392 flag.setEnabled(false);
396 switch (cmd.action) {
397 case LFUN_UNKNOWN_ACTION:
398 #ifndef HAVE_LIBAIKSAURUS
399 case LFUN_THESAURUS_ENTRY:
402 flag.setEnabled(false);
409 if (flag.unknown()) {
410 flag.message(from_utf8(N_("Unknown action")));
414 if (!flag.enabled()) {
415 if (flag.message().empty())
416 flag.message(from_utf8(N_("Command disabled")));
420 // Check whether we need a buffer
421 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
423 flag.message(from_utf8(N_("Command not allowed with"
424 "out any document open")));
425 flag.setEnabled(false);
429 // I would really like to avoid having this switch and rather try to
430 // encode this in the function itself.
431 // -- And I'd rather let an inset decide which LFUNs it is willing
432 // to handle (Andre')
434 switch (cmd.action) {
436 case LFUN_BUFFER_TOGGLE_READ_ONLY:
437 flag.setOnOff(buf->isReadonly());
440 case LFUN_BUFFER_SWITCH:
441 // toggle on the current buffer, but do not toggle off
442 // the other ones (is that a good idea?)
443 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
447 case LFUN_BUFFER_EXPORT:
448 enable = cmd.argument() == "custom"
449 || buf->isExportable(to_utf8(cmd.argument()));
452 case LFUN_BUFFER_CHKTEX:
453 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
456 case LFUN_BUILD_PROGRAM:
457 enable = buf->isExportable("program");
460 case LFUN_VC_REGISTER:
461 enable = !buf->lyxvc().inUse();
463 case LFUN_VC_CHECK_IN:
464 enable = buf->lyxvc().inUse() && !buf->isReadonly();
466 case LFUN_VC_CHECK_OUT:
467 enable = buf->lyxvc().inUse() && buf->isReadonly();
470 case LFUN_VC_UNDO_LAST:
471 enable = buf->lyxvc().inUse();
473 case LFUN_BUFFER_RELOAD:
474 enable = !buf->isUnnamed() && buf->fileName().exists()
475 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
478 case LFUN_CITATION_INSERT: {
479 FuncRequest fr(LFUN_INSET_INSERT, "citation");
480 enable = getStatus(fr).enabled();
484 // This could be used for the no-GUI version. The GUI version is handled in
485 // LyXView::getStatus(). See above.
487 case LFUN_BUFFER_WRITE:
488 case LFUN_BUFFER_WRITE_AS: {
489 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
490 enable = b && (b->isUnnamed() || !b->isClean());
495 case LFUN_BUFFER_WRITE_ALL: {
496 // We enable the command only if there are some modified buffers
497 Buffer * first = theBufferList().first();
502 // We cannot use a for loop as the buffer list is a cycle.
508 b = theBufferList().next(b);
509 } while (b != first);
513 case LFUN_BOOKMARK_GOTO: {
514 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
515 enable = LyX::ref().session().bookmarks().isValid(num);
519 case LFUN_BOOKMARK_CLEAR:
520 enable = LyX::ref().session().bookmarks().size() > 0;
523 // this one is difficult to get right. As a half-baked
524 // solution, we consider only the first action of the sequence
525 case LFUN_COMMAND_SEQUENCE: {
526 // argument contains ';'-terminated commands
527 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
528 FuncRequest func(lyxaction.lookupFunc(firstcmd));
529 func.origin = cmd.origin;
530 flag = getStatus(func);
536 string name = to_utf8(cmd.argument());
537 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
538 func.origin = cmd.origin;
539 flag = getStatus(func);
540 LyX::ref().topLevelCmdDef().release(name);
542 // catch recursion or unknown command definiton
543 // all operations until the recursion or unknown command
544 // definiton occures are performed, so set the state to enabled
550 case LFUN_WORD_FIND_FORWARD:
551 case LFUN_WORD_FIND_BACKWARD:
552 case LFUN_COMMAND_PREFIX:
553 case LFUN_COMMAND_EXECUTE:
555 case LFUN_META_PREFIX:
556 case LFUN_BUFFER_CLOSE:
557 case LFUN_BUFFER_UPDATE:
558 case LFUN_BUFFER_VIEW:
559 case LFUN_MASTER_BUFFER_UPDATE:
560 case LFUN_MASTER_BUFFER_VIEW:
561 case LFUN_BUFFER_IMPORT:
562 case LFUN_BUFFER_AUTO_SAVE:
563 case LFUN_RECONFIGURE:
565 case LFUN_DROP_LAYOUTS_CHOICE:
567 case LFUN_SERVER_GET_NAME:
568 case LFUN_SERVER_NOTIFY:
569 case LFUN_SERVER_GOTO_FILE_ROW:
570 case LFUN_DIALOG_HIDE:
571 case LFUN_DIALOG_DISCONNECT_INSET:
572 case LFUN_BUFFER_CHILD_OPEN:
573 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
574 case LFUN_KEYMAP_OFF:
575 case LFUN_KEYMAP_PRIMARY:
576 case LFUN_KEYMAP_SECONDARY:
577 case LFUN_KEYMAP_TOGGLE:
579 case LFUN_BUFFER_EXPORT_CUSTOM:
580 case LFUN_BUFFER_PRINT:
581 case LFUN_PREFERENCES_SAVE:
583 case LFUN_INSET_EDIT:
584 case LFUN_ALL_INSETS_TOGGLE:
585 case LFUN_BUFFER_LANGUAGE:
586 case LFUN_TEXTCLASS_APPLY:
587 case LFUN_TEXTCLASS_LOAD:
588 case LFUN_BUFFER_SAVE_AS_DEFAULT:
589 case LFUN_BUFFER_PARAMS_APPLY:
590 case LFUN_LAYOUT_MODULES_CLEAR:
591 case LFUN_LAYOUT_MODULE_ADD:
592 case LFUN_LAYOUT_RELOAD:
593 case LFUN_LYXRC_APPLY:
594 case LFUN_BUFFER_NEXT:
595 case LFUN_BUFFER_PREVIOUS:
596 // these are handled in our dispatch()
604 if (theApp()->getStatus(cmd, flag))
607 // Does the view know something?
612 if (lyx_view_->getStatus(cmd, flag))
615 // If we have a BufferView, try cursor position and
616 // then the BufferView.
621 if (!getLocalStatus(view()->cursor(), cmd, flag))
622 flag = view()->getStatus(cmd);
626 flag.setEnabled(false);
628 // Can we use a readonly buffer?
629 if (buf && buf->isReadonly()
630 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
631 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
632 flag.message(from_utf8(N_("Document is read-only")));
633 flag.setEnabled(false);
636 // Are we in a DELETED change-tracking region?
638 && lookupChangeType(view()->cursor(), true) == Change::DELETED
639 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
640 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
641 flag.message(from_utf8(N_("This portion of the document is deleted.")));
642 flag.setEnabled(false);
645 // the default error message if we disable the command
646 if (!flag.enabled() && flag.message().empty())
647 flag.message(from_utf8(N_("Command disabled")));
653 bool LyXFunc::ensureBufferClean(BufferView * bv)
655 Buffer & buf = bv->buffer();
659 docstring const file = buf.fileName().displayName(30);
660 docstring text = bformat(_("The document %1$s has unsaved "
661 "changes.\n\nDo you want to save "
662 "the document?"), file);
663 int const ret = Alert::prompt(_("Save changed document?"),
664 text, 0, 1, _("&Save"),
668 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
670 return buf.isClean();
676 void showPrintError(string const & name)
678 docstring str = bformat(_("Could not print the document %1$s.\n"
679 "Check that your printer is set up correctly."),
680 makeDisplayPath(name, 50));
681 Alert::error(_("Print document failed"), str);
685 bool loadLayoutFile(string const & name, string const & buf_path)
687 if (!LayoutFileList::get().haveClass(name)) {
688 lyxerr << "Document class \"" << name
689 << "\" does not exist."
694 LayoutFile & tc = LayoutFileList::get()[name];
695 if (!tc.load(buf_path)) {
696 docstring s = bformat(_("The document class %1$s "
697 "could not be loaded."), from_utf8(name));
698 Alert::error(_("Could not load class"), s);
705 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
710 void LyXFunc::dispatch(FuncRequest const & cmd)
712 string const argument = to_utf8(cmd.argument());
713 FuncCode const action = cmd.action;
715 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
716 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
718 // we have not done anything wrong yet.
720 dispatch_buffer.erase();
722 // redraw the screen at the end (first of the two drawing steps).
723 //This is done unless explicitely requested otherwise
724 Update::flags updateFlags = Update::FitCursor;
726 FuncStatus const flag = getStatus(cmd);
727 if (!flag.enabled()) {
728 // We cannot use this function here
729 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
730 << lyxaction.getActionName(action)
731 << " [" << action << "] is disabled at this location");
732 setErrorMessage(flag.message());
736 case LFUN_WORD_FIND_FORWARD:
737 case LFUN_WORD_FIND_BACKWARD: {
738 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
739 static docstring last_search;
740 docstring searched_string;
742 if (!cmd.argument().empty()) {
743 last_search = cmd.argument();
744 searched_string = cmd.argument();
746 searched_string = last_search;
749 if (searched_string.empty())
752 bool const fw = action == LFUN_WORD_FIND_FORWARD;
753 docstring const data =
754 find2string(searched_string, true, false, fw);
755 find(view(), FuncRequest(LFUN_WORD_FIND, data));
759 case LFUN_COMMAND_PREFIX:
760 LASSERT(lyx_view_, /**/);
761 lyx_view_->message(keyseq.printOptions(true));
765 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
767 meta_fake_bit = NoModifier;
768 if (lyx_view_->buffer())
769 // cancel any selection
770 dispatch(FuncRequest(LFUN_MARK_OFF));
771 setMessage(from_ascii(N_("Cancel")));
774 case LFUN_META_PREFIX:
775 meta_fake_bit = AltModifier;
776 setMessage(keyseq.print(KeySequence::ForGui));
779 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
780 LASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer(), /**/);
781 Buffer * buf = lyx_view_->buffer();
782 if (buf->lyxvc().inUse())
783 buf->lyxvc().toggleReadOnly();
785 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
789 // --- Menus -----------------------------------------------
790 case LFUN_BUFFER_CLOSE:
791 lyx_view_->closeBuffer();
792 updateFlags = Update::None;
795 case LFUN_BUFFER_RELOAD: {
796 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
797 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
798 docstring text = bformat(_("Any changes will be lost. Are you sure "
799 "you want to revert to the saved version of the document %1$s?"), file);
800 int const ret = Alert::prompt(_("Revert to saved document?"),
801 text, 1, 1, _("&Revert"), _("&Cancel"));
808 case LFUN_BUFFER_UPDATE:
809 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
810 lyx_view_->buffer()->doExport(argument, true);
813 case LFUN_BUFFER_VIEW:
814 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
815 lyx_view_->buffer()->preview(argument);
818 case LFUN_MASTER_BUFFER_UPDATE:
819 LASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer(), /**/);
820 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
823 case LFUN_MASTER_BUFFER_VIEW:
824 LASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer(), /**/);
825 lyx_view_->buffer()->masterBuffer()->preview(argument);
828 case LFUN_BUILD_PROGRAM:
829 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
830 lyx_view_->buffer()->doExport("program", true);
833 case LFUN_BUFFER_CHKTEX:
834 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
835 lyx_view_->buffer()->runChktex();
838 case LFUN_BUFFER_EXPORT:
839 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
840 if (argument == "custom")
841 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
843 lyx_view_->buffer()->doExport(argument, false);
846 case LFUN_BUFFER_EXPORT_CUSTOM: {
847 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
849 string command = split(argument, format_name, ' ');
850 Format const * format = formats.getFormat(format_name);
852 lyxerr << "Format \"" << format_name
853 << "\" not recognized!"
858 Buffer * buffer = lyx_view_->buffer();
860 // The name of the file created by the conversion process
863 // Output to filename
864 if (format->name() == "lyx") {
865 string const latexname = buffer->latexName(false);
866 filename = changeExtension(latexname,
867 format->extension());
868 filename = addName(buffer->temppath(), filename);
870 if (!buffer->writeFile(FileName(filename)))
874 buffer->doExport(format_name, true, filename);
877 // Substitute $$FName for filename
878 if (!contains(command, "$$FName"))
879 command = "( " + command + " ) < $$FName";
880 command = subst(command, "$$FName", filename);
882 // Execute the command in the background
884 call.startscript(Systemcall::DontWait, command);
888 case LFUN_BUFFER_PRINT: {
889 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
890 // FIXME: cmd.getArg() might fail if one of the arguments
891 // contains double quotes
892 string target = cmd.getArg(0);
893 string target_name = cmd.getArg(1);
894 string command = cmd.getArg(2);
897 || target_name.empty()
898 || command.empty()) {
899 lyxerr << "Unable to parse \""
900 << argument << '"' << endl;
903 if (target != "printer" && target != "file") {
904 lyxerr << "Unrecognized target \""
905 << target << '"' << endl;
909 Buffer * buffer = lyx_view_->buffer();
911 if (!buffer->doExport("dvi", true)) {
912 showPrintError(buffer->absFileName());
916 // Push directory path.
917 string const path = buffer->temppath();
918 // Prevent the compiler from optimizing away p
922 // there are three cases here:
923 // 1. we print to a file
924 // 2. we print directly to a printer
925 // 3. we print using a spool command (print to file first)
928 string const dviname =
929 changeExtension(buffer->latexName(true), "dvi");
931 if (target == "printer") {
932 if (!lyxrc.print_spool_command.empty()) {
933 // case 3: print using a spool
934 string const psname =
935 changeExtension(dviname,".ps");
936 command += ' ' + lyxrc.print_to_file
939 + quoteName(dviname);
942 lyxrc.print_spool_command + ' ';
943 if (target_name != "default") {
944 command2 += lyxrc.print_spool_printerprefix
948 command2 += quoteName(psname);
950 // If successful, then spool command
951 res = one.startscript(
956 res = one.startscript(
957 Systemcall::DontWait,
960 // case 2: print directly to a printer
961 if (target_name != "default")
962 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
963 res = one.startscript(
964 Systemcall::DontWait,
965 command + quoteName(dviname));
969 // case 1: print to a file
970 FileName const filename(makeAbsPath(target_name,
971 lyx_view_->buffer()->filePath()));
972 FileName const dvifile(makeAbsPath(dviname, path));
973 if (filename.exists()) {
974 docstring text = bformat(
975 _("The file %1$s already exists.\n\n"
976 "Do you want to overwrite that file?"),
977 makeDisplayPath(filename.absFilename()));
978 if (Alert::prompt(_("Overwrite file?"),
979 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
982 command += ' ' + lyxrc.print_to_file
983 + quoteName(filename.toFilesystemEncoding())
985 + quoteName(dvifile.toFilesystemEncoding());
986 res = one.startscript(Systemcall::DontWait,
991 showPrintError(buffer->absFileName());
995 // FIXME: There is need for a command-line import.
997 case LFUN_BUFFER_IMPORT:
1002 case LFUN_BUFFER_AUTO_SAVE:
1003 lyx_view_->buffer()->autoSave();
1006 case LFUN_RECONFIGURE:
1007 // argument is any additional parameter to the configure.py command
1008 reconfigure(lyx_view_, argument);
1011 case LFUN_HELP_OPEN: {
1012 LASSERT(lyx_view_, /**/);
1013 string const arg = argument;
1015 setErrorMessage(from_ascii(N_("Missing argument")));
1018 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1019 if (fname.empty()) {
1020 lyxerr << "LyX: unable to find documentation file `"
1021 << arg << "'. Bad installation?" << endl;
1024 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1025 makeDisplayPath(fname.absFilename())));
1026 Buffer * buf = lyx_view_->loadDocument(fname, false);
1029 lyx_view_->setBuffer(buf);
1030 buf->errors("Parse");
1032 updateFlags = Update::None;
1036 // --- version control -------------------------------
1037 case LFUN_VC_REGISTER:
1038 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1039 if (!ensureBufferClean(view()))
1041 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1042 lyx_view_->buffer()->lyxvc().registrer();
1045 updateFlags = Update::Force;
1048 case LFUN_VC_CHECK_IN:
1049 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1050 if (!ensureBufferClean(view()))
1052 if (lyx_view_->buffer()->lyxvc().inUse()
1053 && !lyx_view_->buffer()->isReadonly()) {
1054 lyx_view_->buffer()->lyxvc().checkIn();
1059 case LFUN_VC_CHECK_OUT:
1060 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1061 if (!ensureBufferClean(view()))
1063 if (lyx_view_->buffer()->lyxvc().inUse()
1064 && lyx_view_->buffer()->isReadonly()) {
1065 lyx_view_->buffer()->lyxvc().checkOut();
1070 case LFUN_VC_REVERT:
1071 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1072 lyx_view_->buffer()->lyxvc().revert();
1076 case LFUN_VC_UNDO_LAST:
1077 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1078 lyx_view_->buffer()->lyxvc().undoLast();
1082 // --- lyxserver commands ----------------------------
1083 case LFUN_SERVER_GET_NAME:
1084 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1085 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1086 LYXERR(Debug::INFO, "FNAME["
1087 << lyx_view_->buffer()->absFileName() << ']');
1090 case LFUN_SERVER_NOTIFY:
1091 dispatch_buffer = keyseq.print(KeySequence::Portable);
1092 theServer().notifyClient(to_utf8(dispatch_buffer));
1095 case LFUN_SERVER_GOTO_FILE_ROW: {
1096 LASSERT(lyx_view_, /**/);
1099 istringstream is(argument);
1100 is >> file_name >> row;
1102 bool loaded = false;
1103 if (prefixIs(file_name, package().temp_dir().absFilename()))
1104 // Needed by inverse dvi search. If it is a file
1105 // in tmpdir, call the apropriated function
1106 buf = theBufferList().getBufferFromTmp(file_name);
1108 // Must replace extension of the file to be .lyx
1109 // and get full path
1110 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1111 // Either change buffer or load the file
1112 if (theBufferList().exists(s.absFilename()))
1113 buf = theBufferList().getBuffer(s.absFilename());
1115 buf = lyx_view_->loadDocument(s);
1121 updateFlags = Update::None;
1126 lyx_view_->setBuffer(buf);
1127 view()->setCursorFromRow(row);
1129 buf->errors("Parse");
1130 updateFlags = Update::FitCursor;
1135 case LFUN_DIALOG_SHOW_NEW_INSET: {
1136 LASSERT(lyx_view_, /**/);
1137 string const name = cmd.getArg(0);
1138 InsetCode code = insetCode(name);
1139 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1140 bool insetCodeOK = true;
1149 case HYPERLINK_CODE: {
1150 InsetCommandParams p(code);
1151 data = InsetCommand::params2string(name, p);
1154 case INCLUDE_CODE: {
1155 // data is the include type: one of "include",
1156 // "input", "verbatiminput" or "verbatiminput*"
1158 // default type is requested
1160 InsetCommandParams p(INCLUDE_CODE, data);
1161 data = InsetCommand::params2string("include", p);
1165 // \c data == "Boxed" || "Frameless" etc
1166 InsetBoxParams p(data);
1167 data = InsetBox::params2string(p);
1171 InsetBranchParams p;
1172 data = InsetBranch::params2string(p);
1176 InsetCommandParams p(CITE_CODE);
1177 data = InsetCommand::params2string(name, p);
1181 data = InsetERT::params2string(InsetCollapsable::Open);
1184 case EXTERNAL_CODE: {
1185 InsetExternalParams p;
1186 Buffer const & buffer = *lyx_view_->buffer();
1187 data = InsetExternal::params2string(p, buffer);
1192 data = InsetFloat::params2string(p);
1195 case LISTINGS_CODE: {
1196 InsetListingsParams p;
1197 data = InsetListings::params2string(p);
1200 case GRAPHICS_CODE: {
1201 InsetGraphicsParams p;
1202 Buffer const & buffer = *lyx_view_->buffer();
1203 data = InsetGraphics::params2string(p, buffer);
1208 data = InsetNote::params2string(p);
1213 data = InsetSpace::params2string(p);
1218 data = InsetVSpace::params2string(space);
1223 data = InsetWrap::params2string(p);
1227 lyxerr << "Inset type '" << name <<
1228 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1229 insetCodeOK = false;
1231 } // end switch(code)
1233 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1237 case LFUN_CITATION_INSERT: {
1238 LASSERT(lyx_view_, /**/);
1239 if (!argument.empty()) {
1240 // we can have one optional argument, delimited by '|'
1241 // citation-insert <key>|<text_before>
1242 // this should be enhanced to also support text_after
1243 // and citation style
1244 string arg = argument;
1246 if (contains(argument, "|")) {
1247 arg = token(argument, '|', 0);
1248 opt1 = token(argument, '|', 1);
1250 InsetCommandParams icp(CITE_CODE);
1251 icp["key"] = from_utf8(arg);
1253 icp["before"] = from_utf8(opt1);
1254 string icstr = InsetCommand::params2string("citation", icp);
1255 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1258 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1262 case LFUN_BUFFER_CHILD_OPEN: {
1263 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1264 Buffer * parent = lyx_view_->buffer();
1265 FileName filename = makeAbsPath(argument, parent->filePath());
1266 view()->saveBookmark(false);
1268 bool parsed = false;
1269 if (theBufferList().exists(filename.absFilename())) {
1270 child = theBufferList().getBuffer(filename.absFilename());
1272 setMessage(bformat(_("Opening child document %1$s..."),
1273 makeDisplayPath(filename.absFilename())));
1274 child = lyx_view_->loadDocument(filename, false);
1278 // Set the parent name of the child document.
1279 // This makes insertion of citations and references in the child work,
1280 // when the target is in the parent or another child document.
1281 child->setParent(parent);
1282 updateLabels(*child->masterBuffer());
1283 lyx_view_->setBuffer(child);
1285 child->errors("Parse");
1288 // If a screen update is required (in case where auto_open is false),
1289 // setBuffer() would have taken care of it already. Otherwise we shall
1290 // reset the update flag because it can cause a circular problem.
1292 updateFlags = Update::None;
1296 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
1297 LASSERT(lyx_view_, /**/);
1298 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1301 case LFUN_KEYMAP_OFF:
1302 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1303 lyx_view_->view()->getIntl().keyMapOn(false);
1306 case LFUN_KEYMAP_PRIMARY:
1307 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1308 lyx_view_->view()->getIntl().keyMapPrim();
1311 case LFUN_KEYMAP_SECONDARY:
1312 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1313 lyx_view_->view()->getIntl().keyMapSec();
1316 case LFUN_KEYMAP_TOGGLE:
1317 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1318 lyx_view_->view()->getIntl().toggleKeyMap();
1324 string rest = split(argument, countstr, ' ');
1325 istringstream is(countstr);
1328 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1329 for (int i = 0; i < count; ++i)
1330 dispatch(lyxaction.lookupFunc(rest));
1334 case LFUN_COMMAND_SEQUENCE: {
1335 // argument contains ';'-terminated commands
1336 string arg = argument;
1337 while (!arg.empty()) {
1339 arg = split(arg, first, ';');
1340 FuncRequest func(lyxaction.lookupFunc(first));
1341 func.origin = cmd.origin;
1349 if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1350 func.origin = cmd.origin;
1352 LyX::ref().topLevelCmdDef().release(argument);
1354 if (func.action == LFUN_UNKNOWN_ACTION) {
1355 // unknown command definition
1356 lyxerr << "Warning: unknown command definition `"
1360 // recursion detected
1361 lyxerr << "Warning: Recursion in the command definition `"
1362 << argument << "' detected"
1369 case LFUN_PREFERENCES_SAVE: {
1370 lyxrc.write(makeAbsPath("preferences",
1371 package().user_support().absFilename()),
1377 LASSERT(lyx_view_, /**/);
1378 lyx_view_->message(from_utf8(argument));
1382 case LFUN_ALL_INSETS_TOGGLE: {
1383 LASSERT(lyx_view_, /**/);
1385 string const name = split(argument, action, ' ');
1386 InsetCode const inset_code = insetCode(name);
1388 Cursor & cur = view()->cursor();
1389 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1391 Inset & inset = lyx_view_->buffer()->inset();
1392 InsetIterator it = inset_iterator_begin(inset);
1393 InsetIterator const end = inset_iterator_end(inset);
1394 for (; it != end; ++it) {
1395 if (!it->asInsetMath()
1396 && (inset_code == NO_CODE
1397 || inset_code == it->lyxCode())) {
1398 Cursor tmpcur = cur;
1399 tmpcur.pushBackward(*it);
1400 it->dispatch(tmpcur, fr);
1403 updateFlags = Update::Force | Update::FitCursor;
1407 case LFUN_BUFFER_LANGUAGE: {
1408 LASSERT(lyx_view_, /**/);
1409 Buffer & buffer = *lyx_view_->buffer();
1410 Language const * oldL = buffer.params().language;
1411 Language const * newL = languages.getLanguage(argument);
1412 if (!newL || oldL == newL)
1415 if (oldL->rightToLeft() == newL->rightToLeft()
1416 && !buffer.isMultiLingual())
1417 buffer.changeLanguage(oldL, newL);
1421 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1422 string const fname =
1423 addName(addPath(package().user_support().absFilename(), "templates/"),
1425 Buffer defaults(fname);
1427 istringstream ss(argument);
1430 int const unknown_tokens = defaults.readHeader(lex);
1432 if (unknown_tokens != 0) {
1433 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1434 << unknown_tokens << " unknown token"
1435 << (unknown_tokens == 1 ? "" : "s")
1439 if (defaults.writeFile(FileName(defaults.absFileName())))
1440 setMessage(bformat(_("Document defaults saved in %1$s"),
1441 makeDisplayPath(fname)));
1443 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1447 case LFUN_BUFFER_PARAMS_APPLY: {
1448 LASSERT(lyx_view_, /**/);
1450 Buffer * buffer = lyx_view_->buffer();
1451 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1452 Cursor & cur = view()->cursor();
1453 cur.recordUndoFullDocument();
1455 istringstream ss(argument);
1458 int const unknown_tokens = buffer->readHeader(lex);
1460 if (unknown_tokens != 0) {
1461 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1462 << unknown_tokens << " unknown token"
1463 << (unknown_tokens == 1 ? "" : "s")
1467 updateLayout(oldClass, buffer);
1469 updateFlags = Update::Force | Update::FitCursor;
1470 // We are most certainly here because of a change in the document
1471 // It is then better to make sure that all dialogs are in sync with
1472 // current document settings. LyXView::restartCursor() achieve this.
1473 lyx_view_->restartCursor();
1477 case LFUN_LAYOUT_MODULES_CLEAR: {
1478 LASSERT(lyx_view_, /**/);
1479 Buffer * buffer = lyx_view_->buffer();
1480 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1481 view()->cursor().recordUndoFullDocument();
1482 buffer->params().clearLayoutModules();
1483 buffer->params().makeDocumentClass();
1484 updateLayout(oldClass, buffer);
1485 updateFlags = Update::Force | Update::FitCursor;
1489 case LFUN_LAYOUT_MODULE_ADD: {
1490 LASSERT(lyx_view_, /**/);
1491 Buffer * buffer = lyx_view_->buffer();
1492 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1493 view()->cursor().recordUndoFullDocument();
1494 buffer->params().addLayoutModule(argument);
1495 buffer->params().makeDocumentClass();
1496 updateLayout(oldClass, buffer);
1497 updateFlags = Update::Force | Update::FitCursor;
1501 case LFUN_TEXTCLASS_APPLY: {
1502 LASSERT(lyx_view_, /**/);
1503 Buffer * buffer = lyx_view_->buffer();
1505 if (!loadLayoutFile(argument, buffer->temppath()) &&
1506 !loadLayoutFile(argument, buffer->filePath()))
1509 LayoutFile const * old_layout = buffer->params().baseClass();
1510 LayoutFile const * new_layout = &(LayoutFileList::get()[argument]);
1512 if (old_layout == new_layout)
1516 //Save the old, possibly modular, layout for use in conversion.
1517 DocumentClass const * const oldDocClass = buffer->params().documentClassPtr();
1518 view()->cursor().recordUndoFullDocument();
1519 buffer->params().setBaseClass(argument);
1520 buffer->params().makeDocumentClass();
1521 updateLayout(oldDocClass, buffer);
1522 updateFlags = Update::Force | Update::FitCursor;
1526 case LFUN_LAYOUT_RELOAD: {
1527 LASSERT(lyx_view_, /**/);
1528 Buffer * buffer = lyx_view_->buffer();
1529 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1530 LayoutFileIndex bc = buffer->params().baseClassID();
1531 LayoutFileList::get().reset(bc);
1532 buffer->params().setBaseClass(bc);
1533 buffer->params().makeDocumentClass();
1534 updateLayout(oldClass, buffer);
1535 updateFlags = Update::Force | Update::FitCursor;
1539 case LFUN_TEXTCLASS_LOAD:
1540 loadLayoutFile(argument, lyx_view_->buffer()->temppath()) ||
1541 loadLayoutFile(argument, lyx_view_->buffer()->filePath());
1544 case LFUN_LYXRC_APPLY: {
1545 LyXRC const lyxrc_orig = lyxrc;
1547 istringstream ss(argument);
1548 bool const success = lyxrc.read(ss) == 0;
1551 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1552 << "Unable to read lyxrc data"
1557 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1559 theApp()->resetGui();
1561 /// We force the redraw in any case because there might be
1562 /// some screen font changes.
1563 /// FIXME: only the current view will be updated. the Gui
1564 /// class is able to furnish the list of views.
1565 updateFlags = Update::Force;
1569 case LFUN_BOOKMARK_GOTO:
1570 // go to bookmark, open unopened file and switch to buffer if necessary
1571 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1572 updateFlags = Update::FitCursor;
1575 case LFUN_BOOKMARK_CLEAR:
1576 LyX::ref().session().bookmarks().clear();
1580 LASSERT(theApp(), /**/);
1581 // Let the frontend dispatch its own actions.
1582 if (theApp()->dispatch(cmd))
1583 // Nothing more to do.
1586 // Everything below is only for active lyx_view_
1590 // Let the current LyXView dispatch its own actions.
1591 if (lyx_view_->dispatch(cmd)) {
1592 if (lyx_view_->view())
1593 updateFlags = lyx_view_->view()->cursor().result().update();
1597 LASSERT(lyx_view_->view(), /**/);
1598 // Let the current BufferView dispatch its own actions.
1599 if (view()->dispatch(cmd)) {
1600 // The BufferView took care of its own updates if needed.
1601 updateFlags = Update::None;
1605 // Let the current Cursor dispatch its own actions.
1606 Cursor old = view()->cursor();
1607 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1608 cursorPosBeforeDispatchY_);
1609 view()->cursor().dispatch(cmd);
1611 // notify insets we just left
1612 if (view()->cursor() != old) {
1614 bool badcursor = notifyCursorLeaves(old, view()->cursor());
1616 view()->cursor().fixIfBroken();
1619 // update completion. We do it here and not in
1620 // processKeySym to avoid another redraw just for a
1621 // changed inline completion
1622 if (cmd.origin == FuncRequest::KEYBOARD) {
1623 if (cmd.action == LFUN_SELF_INSERT)
1624 lyx_view_->updateCompletion(view()->cursor(), true, true);
1625 else if (cmd.action == LFUN_CHAR_DELETE_BACKWARD)
1626 lyx_view_->updateCompletion(view()->cursor(), false, true);
1628 lyx_view_->updateCompletion(view()->cursor(), false, false);
1631 updateFlags = view()->cursor().result().update();
1634 if (lyx_view_ && lyx_view_->buffer()) {
1635 // BufferView::update() updates the ViewMetricsInfo and
1636 // also initializes the position cache for all insets in
1637 // (at least partially) visible top-level paragraphs.
1638 // We will redraw the screen only if needed.
1639 view()->processUpdateFlags(updateFlags);
1641 // if we executed a mutating lfun, mark the buffer as dirty
1643 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1644 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1645 lyx_view_->buffer()->markDirty();
1647 // Do we have a selection?
1648 theSelection().haveSelection(view()->cursor().selection());
1651 lyx_view_->restartCursor();
1655 // Some messages may already be translated, so we cannot use _()
1656 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1661 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1663 const bool verbose = (cmd.origin == FuncRequest::MENU
1664 || cmd.origin == FuncRequest::TOOLBAR
1665 || cmd.origin == FuncRequest::COMMANDBUFFER);
1667 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1668 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1670 lyx_view_->message(msg);
1674 docstring dispatch_msg = msg;
1675 if (!dispatch_msg.empty())
1676 dispatch_msg += ' ';
1678 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1680 bool argsadded = false;
1682 if (!cmd.argument().empty()) {
1683 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1684 comname += ' ' + cmd.argument();
1689 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1691 if (!shortcuts.empty())
1692 comname += ": " + shortcuts;
1693 else if (!argsadded && !cmd.argument().empty())
1694 comname += ' ' + cmd.argument();
1696 if (!comname.empty()) {
1697 comname = rtrim(comname);
1698 dispatch_msg += '(' + rtrim(comname) + ')';
1701 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1702 if (!dispatch_msg.empty())
1703 lyx_view_->message(dispatch_msg);
1707 void LyXFunc::reloadBuffer()
1709 FileName filename = lyx_view_->buffer()->fileName();
1710 // The user has already confirmed that the changes, if any, should
1711 // be discarded. So we just release the Buffer and don't call closeBuffer();
1712 theBufferList().release(lyx_view_->buffer());
1713 Buffer * buf = lyx_view_->loadDocument(filename);
1714 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1718 lyx_view_->setBuffer(buf);
1719 buf->errors("Parse");
1720 str = bformat(_("Document %1$s reloaded."), disp_fn);
1722 str = bformat(_("Could not reload document %1$s"), disp_fn);
1724 lyx_view_->message(str);
1727 // Each "lyx_view_" should have it's own message method. lyxview and
1728 // the minibuffer would use the minibuffer, but lyxserver would
1729 // send an ERROR signal to its client. Alejandro 970603
1730 // This function is bit problematic when it comes to NLS, to make the
1731 // lyx servers client be language indepenent we must not translate
1732 // strings sent to this func.
1733 void LyXFunc::setErrorMessage(docstring const & m) const
1735 dispatch_buffer = m;
1740 void LyXFunc::setMessage(docstring const & m) const
1742 dispatch_buffer = m;
1746 docstring LyXFunc::viewStatusMessage()
1748 // When meta-fake key is pressed, show the key sequence so far + "M-".
1750 return keyseq.print(KeySequence::ForGui) + "M-";
1752 // Else, when a non-complete key sequence is pressed,
1753 // show the available options.
1754 if (keyseq.length() > 0 && !keyseq.deleted())
1755 return keyseq.printOptions(true);
1757 LASSERT(lyx_view_, /**/);
1758 if (!lyx_view_->buffer())
1759 return _("Welcome to LyX!");
1761 return view()->cursor().currentState();
1765 BufferView * LyXFunc::view() const
1767 LASSERT(lyx_view_, /**/);
1768 return lyx_view_->view();
1772 bool LyXFunc::wasMetaKey() const
1774 return (meta_fake_bit != NoModifier);
1778 void LyXFunc::updateLayout(DocumentClass const * const oldlayout, Buffer * buffer)
1780 lyx_view_->message(_("Converting document to new document class..."));
1782 StableDocIterator backcur(view()->cursor());
1783 ErrorList & el = buffer->errorList("Class Switch");
1784 cap::switchBetweenClasses(
1785 oldlayout, buffer->params().documentClassPtr(),
1786 static_cast<InsetText &>(buffer->inset()), el);
1788 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1790 buffer->errors("Class Switch");
1791 updateLabels(*buffer);
1797 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1799 // Why the switch you might ask. It is a trick to ensure that all
1800 // the elements in the LyXRCTags enum is handled. As you can see
1801 // there are no breaks at all. So it is just a huge fall-through.
1802 // The nice thing is that we will get a warning from the compiler
1803 // if we forget an element.
1804 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1806 case LyXRC::RC_ACCEPT_COMPOUND:
1807 case LyXRC::RC_ALT_LANG:
1808 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1809 case LyXRC::RC_PLAINTEXT_LINELEN:
1810 case LyXRC::RC_AUTOREGIONDELETE:
1811 case LyXRC::RC_AUTORESET_OPTIONS:
1812 case LyXRC::RC_AUTOSAVE:
1813 case LyXRC::RC_AUTO_NUMBER:
1814 case LyXRC::RC_BACKUPDIR_PATH:
1815 case LyXRC::RC_BIBTEX_COMMAND:
1816 case LyXRC::RC_BINDFILE:
1817 case LyXRC::RC_CHECKLASTFILES:
1818 case LyXRC::RC_COMPLETION_CURSOR_TEXT:
1819 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1820 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1821 case LyXRC::RC_COMPLETION_INLINE_MATH:
1822 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1823 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1824 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1825 case LyXRC::RC_COMPLETION_POPUP_MATH:
1826 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1827 case LyXRC::RC_USELASTFILEPOS:
1828 case LyXRC::RC_LOADSESSION:
1829 case LyXRC::RC_CHKTEX_COMMAND:
1830 case LyXRC::RC_CONVERTER:
1831 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1832 case LyXRC::RC_COPIER:
1833 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1834 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1835 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1836 case LyXRC::RC_DATE_INSERT_FORMAT:
1837 case LyXRC::RC_DEFAULT_LANGUAGE:
1838 case LyXRC::RC_DEFAULT_PAPERSIZE:
1839 case LyXRC::RC_DEFFILE:
1840 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1841 case LyXRC::RC_DISPLAY_GRAPHICS:
1842 case LyXRC::RC_DOCUMENTPATH:
1843 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1844 FileName path(lyxrc_new.document_path);
1845 if (path.exists() && path.isDirectory())
1846 package().document_dir() = FileName(lyxrc.document_path);
1848 case LyXRC::RC_ESC_CHARS:
1849 case LyXRC::RC_EXAMPLEPATH:
1850 case LyXRC::RC_FONT_ENCODING:
1851 case LyXRC::RC_FORMAT:
1852 case LyXRC::RC_GROUP_LAYOUTS:
1853 case LyXRC::RC_INDEX_COMMAND:
1854 case LyXRC::RC_INPUT:
1855 case LyXRC::RC_KBMAP:
1856 case LyXRC::RC_KBMAP_PRIMARY:
1857 case LyXRC::RC_KBMAP_SECONDARY:
1858 case LyXRC::RC_LABEL_INIT_LENGTH:
1859 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1860 case LyXRC::RC_LANGUAGE_AUTO_END:
1861 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1862 case LyXRC::RC_LANGUAGE_COMMAND_END:
1863 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1864 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1865 case LyXRC::RC_LANGUAGE_PACKAGE:
1866 case LyXRC::RC_LANGUAGE_USE_BABEL:
1867 case LyXRC::RC_MACRO_EDIT_STYLE:
1868 case LyXRC::RC_MAKE_BACKUP:
1869 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1870 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1871 case LyXRC::RC_NUMLASTFILES:
1872 case LyXRC::RC_PATH_PREFIX:
1873 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1874 prependEnvPath("PATH", lyxrc.path_prefix);
1876 case LyXRC::RC_PERS_DICT:
1877 case LyXRC::RC_PREVIEW:
1878 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1879 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1880 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1881 case LyXRC::RC_PRINTCOPIESFLAG:
1882 case LyXRC::RC_PRINTER:
1883 case LyXRC::RC_PRINTEVENPAGEFLAG:
1884 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1885 case LyXRC::RC_PRINTFILEEXTENSION:
1886 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1887 case LyXRC::RC_PRINTODDPAGEFLAG:
1888 case LyXRC::RC_PRINTPAGERANGEFLAG:
1889 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1890 case LyXRC::RC_PRINTPAPERFLAG:
1891 case LyXRC::RC_PRINTREVERSEFLAG:
1892 case LyXRC::RC_PRINTSPOOL_COMMAND:
1893 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1894 case LyXRC::RC_PRINTTOFILE:
1895 case LyXRC::RC_PRINTTOPRINTER:
1896 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1897 case LyXRC::RC_PRINT_COMMAND:
1898 case LyXRC::RC_RTL_SUPPORT:
1899 case LyXRC::RC_SCREEN_DPI:
1900 case LyXRC::RC_SCREEN_FONT_ROMAN:
1901 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1902 case LyXRC::RC_SCREEN_FONT_SANS:
1903 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1904 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1905 case LyXRC::RC_SCREEN_FONT_SIZES:
1906 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1907 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1908 case LyXRC::RC_GEOMETRY_SESSION:
1909 case LyXRC::RC_SCREEN_ZOOM:
1910 case LyXRC::RC_SERVERPIPE:
1911 case LyXRC::RC_SET_COLOR:
1912 case LyXRC::RC_SHOW_BANNER:
1913 case LyXRC::RC_OPEN_BUFFERS_IN_TABS:
1914 case LyXRC::RC_SPELL_COMMAND:
1915 case LyXRC::RC_TEMPDIRPATH:
1916 case LyXRC::RC_TEMPLATEPATH:
1917 case LyXRC::RC_TEX_ALLOWS_SPACES:
1918 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
1919 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
1920 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
1922 case LyXRC::RC_UIFILE:
1923 case LyXRC::RC_USER_EMAIL:
1924 case LyXRC::RC_USER_NAME:
1925 case LyXRC::RC_USETEMPDIR:
1926 case LyXRC::RC_USE_ALT_LANG:
1927 case LyXRC::RC_USE_CONVERTER_CACHE:
1928 case LyXRC::RC_USE_ESC_CHARS:
1929 case LyXRC::RC_USE_INP_ENC:
1930 case LyXRC::RC_USE_PERS_DICT:
1931 case LyXRC::RC_USE_TOOLTIP:
1932 case LyXRC::RC_USE_PIXMAP_CACHE:
1933 case LyXRC::RC_USE_SPELL_LIB:
1934 case LyXRC::RC_VIEWDVI_PAPEROPTION:
1935 case LyXRC::RC_SORT_LAYOUTS:
1936 case LyXRC::RC_FULL_SCREEN_LIMIT:
1937 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
1938 case LyXRC::RC_FULL_SCREEN_TABBAR:
1939 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
1940 case LyXRC::RC_FULL_SCREEN_WIDTH:
1941 case LyXRC::RC_VISUAL_CURSOR:
1942 case LyXRC::RC_VIEWER:
1943 case LyXRC::RC_LAST: