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"
46 #include "LyXAction.h"
51 #include "Paragraph.h"
52 #include "ParagraphParameters.h"
53 #include "ParIterator.h"
56 #include "SpellChecker.h"
58 #include "frontends/alert.h"
59 #include "frontends/Application.h"
60 #include "frontends/KeySymbol.h"
61 #include "frontends/LyXView.h"
62 #include "frontends/Selection.h"
64 #include "support/debug.h"
65 #include "support/environment.h"
66 #include "support/FileName.h"
67 #include "support/filetools.h"
68 #include "support/gettext.h"
69 #include "support/lassert.h"
70 #include "support/lstrings.h"
71 #include "support/Package.h"
72 #include "support/convert.h"
73 #include "support/os.h"
79 using namespace lyx::support;
83 using frontend::LyXView;
85 namespace Alert = frontend::Alert;
92 //FIXME: bookmark handling is a frontend issue. This code should be transferred
93 // to GuiView and be GuiView and be window dependent.
94 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
96 LyXView * lv = theApp()->currentWindow();
98 if (!theSession().bookmarks().isValid(idx))
100 BookmarksSection::Bookmark const & bm = theSession().bookmarks().bookmark(idx);
101 LASSERT(!bm.filename.empty(), /**/);
102 string const file = bm.filename.absFilename();
103 // if the file is not opened, open it.
104 if (!theBufferList().exists(bm.filename)) {
106 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
110 // open may fail, so we need to test it again
111 if (!theBufferList().exists(bm.filename))
114 // bm can be changed when saving
115 BookmarksSection::Bookmark tmp = bm;
117 // Special case idx == 0 used for back-from-back jump navigation
119 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
121 // if the current buffer is not that one, switch to it.
122 if (!lv->documentBufferView()
123 || lv->documentBufferView()->buffer().fileName() != tmp.filename) {
126 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
129 // moveToPosition try paragraph id first and then paragraph (pit, pos).
130 if (!lv->documentBufferView()->moveToPosition(
131 tmp.bottom_pit, tmp.bottom_pos, tmp.top_id, tmp.top_pos))
138 // Cursor jump succeeded!
139 Cursor const & cur = lv->documentBufferView()->cursor();
140 pit_type new_pit = cur.pit();
141 pos_type new_pos = cur.pos();
142 int new_id = cur.paragraph().id();
144 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
145 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
146 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
147 || bm.top_id != new_id) {
148 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
149 new_pit, new_pos, new_id);
154 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
156 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
159 if (cmd.action == LFUN_NOACTION) {
160 flag.message(from_utf8(N_("Nothing to do")));
161 flag.setEnabled(false);
165 switch (cmd.action) {
166 case LFUN_UNKNOWN_ACTION:
168 flag.setEnabled(false);
175 if (flag.unknown()) {
176 flag.message(from_utf8(N_("Unknown action")));
180 if (!flag.enabled()) {
181 if (flag.message().empty())
182 flag.message(from_utf8(N_("Command disabled")));
186 // I would really like to avoid having this switch and rather try to
187 // encode this in the function itself.
188 // -- And I'd rather let an inset decide which LFUNs it is willing
189 // to handle (Andre')
191 switch (cmd.action) {
193 // This could be used for the no-GUI version. The GUI version is handled in
194 // LyXView::getStatus(). See above.
196 case LFUN_BUFFER_WRITE:
197 case LFUN_BUFFER_WRITE_AS: {
198 Buffer * b = theBufferList().getBuffer(FileName(cmd.getArg(0)));
199 enable = b && (b->isUnnamed() || !b->isClean());
204 case LFUN_BOOKMARK_GOTO: {
205 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
206 enable = theSession().bookmarks().isValid(num);
210 case LFUN_BOOKMARK_CLEAR:
211 enable = theSession().bookmarks().hasValid();
214 // this one is difficult to get right. As a half-baked
215 // solution, we consider only the first action of the sequence
216 case LFUN_COMMAND_SEQUENCE: {
217 // argument contains ';'-terminated commands
218 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
219 FuncRequest func(lyxaction.lookupFunc(firstcmd));
220 func.origin = cmd.origin;
221 flag = getStatus(func);
225 // we want to check if at least one of these is enabled
226 case LFUN_COMMAND_ALTERNATIVES: {
227 // argument contains ';'-terminated commands
228 string arg = to_utf8(cmd.argument());
229 while (!arg.empty()) {
231 arg = split(arg, first, ';');
232 FuncRequest func(lyxaction.lookupFunc(first));
233 func.origin = cmd.origin;
234 flag = getStatus(func);
235 // if this one is enabled, the whole thing is
244 string name = to_utf8(cmd.argument());
245 if (theTopLevelCmdDef().lock(name, func)) {
246 func.origin = cmd.origin;
247 flag = getStatus(func);
248 theTopLevelCmdDef().release(name);
250 // catch recursion or unknown command
251 // definition. all operations until the
252 // recursion or unknown command definition
253 // occurs are performed, so set the state to
260 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
262 case LFUN_PREFERENCES_SAVE:
263 case LFUN_BUFFER_SAVE_AS_DEFAULT:
264 // these are handled in our dispatch()
272 if (theApp()->getStatus(cmd, flag))
275 // Does the view know something?
276 LyXView * lv = theApp()->currentWindow();
281 if (lv->getStatus(cmd, flag))
284 BufferView * bv = lv->currentBufferView();
285 BufferView * doc_bv = lv->documentBufferView();
286 // If we do not have a BufferView, then other functions are disabled
291 // try the BufferView
292 bool decided = bv->getStatus(cmd, flag);
295 decided = bv->buffer().getStatus(cmd, flag);
296 if (!decided && doc_bv)
297 // try the Document Buffer
298 decided = doc_bv->buffer().getStatus(cmd, flag);
302 flag.setEnabled(false);
304 // the default error message if we disable the command
305 if (!flag.enabled() && flag.message().empty())
306 flag.message(from_utf8(N_("Command disabled")));
311 /// send a post-dispatch status message
312 static docstring sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
314 const bool verbose = (cmd.origin == FuncRequest::MENU
315 || cmd.origin == FuncRequest::TOOLBAR
316 || cmd.origin == FuncRequest::COMMANDBUFFER);
318 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
319 LYXERR(Debug::ACTION, "dispatch msg is " << msg);
323 docstring dispatch_msg = msg;
324 if (!dispatch_msg.empty())
327 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
329 bool argsadded = false;
331 if (!cmd.argument().empty()) {
332 if (cmd.action != LFUN_UNKNOWN_ACTION) {
333 comname += ' ' + cmd.argument();
337 docstring const shortcuts = theTopLevelKeymap().
338 printBindings(cmd, KeySequence::ForGui);
340 if (!shortcuts.empty())
341 comname += ": " + shortcuts;
342 else if (!argsadded && !cmd.argument().empty())
343 comname += ' ' + cmd.argument();
345 if (!comname.empty()) {
346 comname = rtrim(comname);
347 dispatch_msg += '(' + rtrim(comname) + ')';
349 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
354 void LyXFunc::dispatch(FuncRequest const & cmd)
356 string const argument = to_utf8(cmd.argument());
357 FuncCode const action = cmd.action;
359 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
360 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
362 // we have not done anything wrong yet.
364 dispatch_buffer.erase();
366 // redraw the screen at the end (first of the two drawing steps).
367 //This is done unless explicitely requested otherwise
368 Update::flags updateFlags = Update::FitCursor;
370 LyXView * lv = theApp()->currentWindow();
372 FuncStatus const flag = getStatus(cmd);
373 if (!flag.enabled()) {
374 // We cannot use this function here
375 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
376 << lyxaction.getActionName(action)
377 << " [" << action << "] is disabled at this location");
378 setErrorMessage(flag.message());
384 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
385 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
391 string rest = split(argument, countstr, ' ');
392 istringstream is(countstr);
395 //lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
396 for (int i = 0; i < count; ++i)
397 dispatch(lyxaction.lookupFunc(rest));
401 case LFUN_COMMAND_SEQUENCE: {
402 // argument contains ';'-terminated commands
403 string arg = argument;
404 // FIXME: this LFUN should also work without any view.
405 Buffer * buffer = (lv && lv->documentBufferView())
406 ? &(lv->documentBufferView()->buffer()) : 0;
407 buffer = &lv->currentBufferView()->buffer();
408 if (buffer && !theBufferList().isLoaded(buffer))
411 buffer->undo().beginUndoGroup();
412 while (!arg.empty()) {
414 arg = split(arg, first, ';');
415 FuncRequest func(lyxaction.lookupFunc(first));
416 func.origin = cmd.origin;
420 buffer->undo().endUndoGroup();
424 case LFUN_COMMAND_ALTERNATIVES: {
425 // argument contains ';'-terminated commands
426 string arg = argument;
427 while (!arg.empty()) {
429 arg = split(arg, first, ';');
430 FuncRequest func(lyxaction.lookupFunc(first));
431 func.origin = cmd.origin;
432 FuncStatus stat = getStatus(func);
433 if (stat.enabled()) {
443 if (theTopLevelCmdDef().lock(argument, func)) {
444 func.origin = cmd.origin;
446 theTopLevelCmdDef().release(argument);
448 if (func.action == LFUN_UNKNOWN_ACTION) {
449 // unknown command definition
450 lyxerr << "Warning: unknown command definition `"
454 // recursion detected
455 lyxerr << "Warning: Recursion in the command definition `"
456 << argument << "' detected"
463 case LFUN_PREFERENCES_SAVE: {
464 lyxrc.write(makeAbsPath("preferences",
465 package().user_support().absFilename()),
470 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
472 addName(addPath(package().user_support().absFilename(), "templates/"),
474 Buffer defaults(fname);
476 istringstream ss(argument);
479 int const unknown_tokens = defaults.readHeader(lex);
481 if (unknown_tokens != 0) {
482 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
483 << unknown_tokens << " unknown token"
484 << (unknown_tokens == 1 ? "" : "s")
488 if (defaults.writeFile(FileName(defaults.absFileName())))
489 setMessage(bformat(_("Document defaults saved in %1$s"),
490 makeDisplayPath(fname)));
492 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
496 case LFUN_BOOKMARK_GOTO:
497 // go to bookmark, open unopened file and switch to buffer if necessary
498 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
499 updateFlags = Update::FitCursor;
502 case LFUN_BOOKMARK_CLEAR:
503 theSession().bookmarks().clear();
509 LASSERT(theApp(), /**/);
510 // Let the frontend dispatch its own actions.
511 theApp()->dispatch(cmd, dr);
513 // Nothing more to do.
516 // Everything below is only for active window
520 // Let the current LyXView dispatch its own actions.
521 if (lv->dispatch(cmd)) {
522 BufferView * bv = lv->currentBufferView();
524 updateFlags = bv->cursor().result().update();
528 BufferView * bv = lv->currentBufferView();
531 // Let the current BufferView dispatch its own actions.
532 if (bv->dispatch(cmd)) {
533 // The BufferView took care of its own updates if needed.
534 updateFlags = Update::None;
538 BufferView * doc_bv = lv->documentBufferView();
539 // Try with the document BufferView dispatch if any.
540 if (doc_bv && doc_bv->dispatch(cmd)) {
541 updateFlags = Update::None;
545 // OK, so try the current Buffer itself...
546 bv->buffer().dispatch(cmd, dr);
547 if (dr.dispatched()) {
548 updateFlags = dr.update();
551 // and with the document Buffer.
553 doc_bv->buffer().dispatch(cmd, dr);
554 if (dr.dispatched()) {
555 updateFlags = dr.update();
560 // Is this a function that acts on inset at point?
561 Inset * inset = bv->cursor().nextInset();
562 if (lyxaction.funcHasFlag(action, LyXAction::AtPoint)
564 bv->cursor().result().dispatched(true);
565 bv->cursor().result().update(Update::FitCursor | Update::Force);
566 FuncRequest tmpcmd = cmd;
567 inset->dispatch(bv->cursor(), tmpcmd);
568 if (bv->cursor().result().dispatched()) {
569 updateFlags = bv->cursor().result().update();
574 // Let the current Cursor dispatch its own actions.
575 Cursor old = bv->cursor();
576 bv->cursor().getPos(cursorPosBeforeDispatchX_,
577 cursorPosBeforeDispatchY_);
578 bv->cursor().dispatch(cmd);
580 // notify insets we just left
581 if (bv->cursor() != old) {
583 bool badcursor = notifyCursorLeavesOrEnters(old, bv->cursor());
585 bv->cursor().fixIfBroken();
588 // update completion. We do it here and not in
589 // processKeySym to avoid another redraw just for a
590 // changed inline completion
591 if (cmd.origin == FuncRequest::KEYBOARD) {
592 if (cmd.action == LFUN_SELF_INSERT
593 || (cmd.action == LFUN_ERT_INSERT && bv->cursor().inMathed()))
594 lv->updateCompletion(bv->cursor(), true, true);
595 else if (cmd.action == LFUN_CHAR_DELETE_BACKWARD)
596 lv->updateCompletion(bv->cursor(), false, true);
598 lv->updateCompletion(bv->cursor(), false, false);
601 updateFlags = bv->cursor().result().update();
604 // if we executed a mutating lfun, mark the buffer as dirty
605 Buffer * doc_buffer = (lv && lv->documentBufferView())
606 ? &(lv->documentBufferView()->buffer()) : 0;
607 if (doc_buffer && theBufferList().isLoaded(doc_buffer)
609 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
610 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
611 doc_buffer->markDirty();
613 if (lv && lv->currentBufferView()) {
614 // BufferView::update() updates the ViewMetricsInfo and
615 // also initializes the position cache for all insets in
616 // (at least partially) visible top-level paragraphs.
617 // We will redraw the screen only if needed.
618 lv->currentBufferView()->processUpdateFlags(updateFlags);
620 // Do we have a selection?
621 theSelection().haveSelection(
622 lv->currentBufferView()->cursor().selection());
629 // Some messages may already be translated, so we cannot use _()
630 lv->message(sendDispatchMessage(
631 translateIfPossible(getMessage()), cmd));
636 // Each LyXView should have it's own message method. lyxview and
637 // the minibuffer would use the minibuffer, but lyxserver would
638 // send an ERROR signal to its client. Alejandro 970603
639 // This function is bit problematic when it comes to NLS, to make the
640 // lyx servers client be language indepenent we must not translate
641 // strings sent to this func.
642 void LyXFunc::setErrorMessage(docstring const & m) const
649 void LyXFunc::setMessage(docstring const & m) const