1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
17 #include "bufferlist.h"
19 #include "buffer_funcs.h"
20 #include "BufferView.h"
21 #include "lyxserver.h"
25 #include "LyXAction.h"
31 #include "trans_mgr.h"
34 #include "bufferview_funcs.h"
35 #include "frontends/LyXView.h"
36 #include "frontends/lyx_gui.h"
38 #include "FloatList.h"
42 #include "TextCache.h"
44 #include "undo_funcs.h"
45 #include "ParagraphParameters.h"
47 #include "insets/insetcommand.h"
48 #include "insets/insetexternal.h"
49 #include "insets/insettabular.h"
51 #include "mathed/formulamacro.h"
52 #include "mathed/math_cursor.h"
53 #include "mathed/math_inset.h"
55 #include "frontends/FileDialog.h"
56 #include "frontends/Dialogs.h"
57 #include "frontends/Toolbar.h"
58 #include "frontends/Menubar.h"
59 #include "frontends/Alert.h"
61 #include "graphics/GraphicsCache.h"
63 #include "support/lyxalgo.h"
64 #include "support/LAssert.h"
65 #include "support/filetools.h"
66 #include "support/FileInfo.h"
67 #include "support/forkedcontr.h"
68 #include "support/lstrings.h"
69 #include "support/tostr.h"
70 #include "support/path.h"
71 #include "support/path_defines.h"
72 #include "support/lyxfunctional.h"
82 using namespace lyx::support;
90 using std::back_inserter;
91 using namespace bv_funcs;
93 extern BufferList bufferlist;
94 extern LyXServer * lyxserver;
95 extern bool selection_possible;
97 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
100 extern tex_accent_struct get_accent(kb_action action);
102 extern void ShowLatexLog();
105 LyXFunc::LyXFunc(LyXView * o)
108 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
109 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
110 meta_fake_bit(key_modifier::none)
115 void LyXFunc::moveCursorUpdate()
117 LyXText * lt = view()->text;
118 if (lt->selection.mark()) {
120 if (!lt->isInInset())
123 view()->update(lt, BufferView::SELECT);
124 view()->switchKeyMap();
128 void LyXFunc::handleKeyFunc(kb_action action)
130 char c = encoded_last_key;
132 if (keyseq.length()) {
136 owner->getIntl().getTransManager()
137 .deadkey(c, get_accent(action).accent, view()->getLyXText());
138 // Need to clear, in case the minibuffer calls these
141 // copied verbatim from do_accent_char
142 view()->update(view()->getLyXText(), BufferView::SELECT);
143 view()->getLyXText()->selection.cursor = view()->getLyXText()->cursor;
147 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
148 key_modifier::state state)
152 if (lyxerr.debugging(Debug::KEY)) {
153 lyxerr << "KeySym is "
154 << keysym->getSymbolName()
158 // Do nothing if we have nothing (JMarc)
159 if (!keysym->isOK()) {
160 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
165 if (keysym->isModifier()) {
166 lyxerr[Debug::KEY] << "isModifier true" << endl;
170 Encoding const * encoding = view()->getEncoding();
172 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
174 // Do a one-deep top-level lookup for
175 // cancel and meta-fake keys. RVDK_PATCH_5
176 cancel_meta_seq.reset();
178 int action = cancel_meta_seq.addkey(keysym, state);
179 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
181 // When not cancel or meta-fake, do the normal lookup.
182 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
183 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
184 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
185 // remove Caps Lock and Mod2 as a modifiers
186 action = keyseq.addkey(keysym, (state | meta_fake_bit));
187 lyxerr[Debug::KEY] << "action now set to ["
188 << action << ']' << endl;
191 // Dont remove this unless you know what you are doing.
192 meta_fake_bit = key_modifier::none;
194 // can this happen now ?
195 if (action == LFUN_NOACTION) {
196 action = LFUN_PREFIX;
199 if (lyxerr.debugging(Debug::KEY)) {
200 lyxerr << "Key [action="
202 << keyseq.print() << ']'
206 // already here we know if it any point in going further
207 // why not return already here if action == -1 and
208 // num_bytes == 0? (Lgb)
210 if (keyseq.length() > 1) {
211 owner->message(keyseq.print());
215 // Maybe user can only reach the key via holding down shift.
216 // Let's see. But only if shift is the only modifier
217 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
218 lyxerr[Debug::KEY] << "Trying without shift" << endl;
219 action = keyseq.addkey(keysym, key_modifier::none);
220 lyxerr[Debug::KEY] << "Action now " << action << endl;
223 if (action == LFUN_UNKNOWN_ACTION) {
224 // Hmm, we didn't match any of the keysequences. See
225 // if it's normal insertable text not already covered
227 if (keysym->isText() && keyseq.length() == 1) {
228 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
229 action = LFUN_SELFINSERT;
231 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
232 owner->message(_("Unknown function."));
237 if (action == LFUN_SELFINSERT) {
238 if (encoded_last_key != 0) {
240 arg += encoded_last_key;
242 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
244 lyxerr[Debug::KEY] << "SelfInsert arg[`"
245 << argument << "']" << endl;
253 FuncStatus LyXFunc::getStatus(int ac) const
255 return getStatus(lyxaction.retrieveActionArg(ac));
259 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
262 Buffer * buf = owner->buffer();
264 if (ev.action == LFUN_NOACTION) {
265 setStatusMessage(N_("Nothing to do"));
266 return flag.disabled(true);
270 case LFUN_UNKNOWN_ACTION:
271 #ifndef HAVE_LIBAIKSAURUS
272 case LFUN_THESAURUS_ENTRY:
277 flag |= lyx_gui::getStatus(ev);
280 if (flag.unknown()) {
281 setStatusMessage(N_("Unknown action"));
285 // the default error message if we disable the command
286 setStatusMessage(N_("Command disabled"));
288 // Check whether we need a buffer
289 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
290 // Yes we need a buffer, do we have one?
293 // Can we use a readonly buffer?
294 if (buf->isReadonly() &&
295 !lyxaction.funcHasFlag(ev.action,
296 LyXAction::ReadOnly)) {
298 setStatusMessage(N_("Document is read-only"));
303 setStatusMessage(N_("Command not allowed with"
304 "out any document open"));
305 return flag.disabled(true);
309 UpdatableInset * tli = view()->theLockingInset();
311 // I would really like to avoid having this switch and rather try to
312 // encode this in the function itself.
313 bool disable = false;
316 disable = ev.argument != "custom"
317 && !Exporter::IsExportable(buf, ev.argument);
320 disable = buf->undostack.empty();
323 disable = buf->redostack.empty();
328 UpdatableInset * in = tli;
329 if (in->lyxCode() != InsetOld::TABULAR_CODE) {
330 in = tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE);
332 if (in && static_cast<InsetTabular*>(in)->hasSelection()) {
337 disable = !mathcursor && !view()->getLyXText()->selection.set();
340 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
343 disable = !Exporter::IsExportable(buf, "program");
346 case LFUN_LAYOUT_TABULAR:
348 || (tli->lyxCode() != InsetOld::TABULAR_CODE
349 && !tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE));
353 disable = !changeDepth(view(), view()->getLyXText(), DEC_DEPTH, true);
356 case LFUN_DEPTH_PLUS:
357 disable = !changeDepth(view(), view()->getLyXText(), INC_DEPTH, true);
361 case LFUN_LAYOUT_PARAGRAPH: {
362 InsetOld * inset = view()->getLyXText()->cursor.par()->inInset();
363 disable = inset && inset->forceDefaultParagraphs(inset);
367 case LFUN_INSET_OPTARG:
368 disable = (view()->getLyXText()->cursor.par()->layout()->optionalargs == 0);
371 case LFUN_TABULAR_FEATURE:
374 // FIXME: check temporarily disabled
376 char align = mathcursor->valign();
381 if (ev.argument.empty()) {
385 if (!contains("tcb", ev.argument[0])) {
389 flag.setOnOff(ev.argument[0] == align);
393 char align = mathcursor->halign();
398 if (ev.argument.empty()) {
402 if (!contains("lcr", ev.argument[0])) {
406 flag.setOnOff(ev.argument[0] == align);
409 disable = !mathcursor->halign();
415 //ret.disabled(true);
416 if (tli->lyxCode() == InsetOld::TABULAR_CODE) {
417 ret = static_cast<InsetTabular *>(tli)
418 ->getStatus(ev.argument);
421 } else if (tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE)) {
422 ret = static_cast<InsetTabular *>
423 (tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE))
424 ->getStatus(ev.argument);
431 static InsetTabular inset(*owner->buffer(), 1, 1);
435 ret = inset.getStatus(ev.argument);
436 if (ret.onoff(true) || ret.onoff(false))
437 flag.setOnOff(false);
441 case LFUN_VC_REGISTER:
442 disable = buf->lyxvc.inUse();
444 case LFUN_VC_CHECKIN:
445 disable = !buf->lyxvc.inUse() || buf->isReadonly();
447 case LFUN_VC_CHECKOUT:
448 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
452 disable = !buf->lyxvc.inUse();
454 case LFUN_MENURELOAD:
455 disable = buf->isUnnamed() || buf->isClean();
457 case LFUN_BOOKMARK_GOTO:
459 isSavedPosition(strToUnsignedInt(ev.argument));
461 case LFUN_MERGE_CHANGES:
462 case LFUN_ACCEPT_CHANGE:
463 case LFUN_REJECT_CHANGE:
464 case LFUN_ACCEPT_ALL_CHANGES:
465 case LFUN_REJECT_ALL_CHANGES:
466 disable = !buf->params.tracking_changes;
468 case LFUN_INSET_TOGGLE: {
469 LyXText * lt = view()->getLyXText();
470 disable = !(isEditableInset(lt->getInset())
472 && lt->inset_owner->owner()
473 && lt->inset_owner->owner()->isOpen()));
477 case LFUN_INSET_SETTINGS: {
479 UpdatableInset * inset = view()->theLockingInset();
484 // get the innermost inset
485 inset = inset->getLockingInset();
487 // jump back to owner if an InsetText, so
488 // we get back to the InsetTabular or whatever
489 if (inset->lyxCode() == InsetOld::TEXT_CODE)
490 inset = inset->owner();
492 InsetOld::Code code = inset->lyxCode();
494 case InsetOld::TABULAR_CODE:
495 disable = ev.argument != "tabular";
497 case InsetOld::ERT_CODE:
498 disable = ev.argument != "ert";
500 case InsetOld::FLOAT_CODE:
501 disable = ev.argument != "float";
503 case InsetOld::MINIPAGE_CODE:
504 disable = ev.argument != "minipage";
506 case InsetOld::WRAP_CODE:
507 disable = ev.argument != "wrap";
509 case InsetOld::NOTE_CODE:
510 disable = ev.argument != "note";
518 case LFUN_MATH_MUTATE:
520 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
521 flag.setOnOff(false);
526 // we just need to be in math mode to enable that
528 case LFUN_MATH_SPACE:
529 case LFUN_MATH_LIMITS:
530 case LFUN_MATH_NONUMBER:
531 case LFUN_MATH_NUMBER:
532 case LFUN_MATH_EXTERN:
533 disable = !mathcursor;
536 case LFUN_DIALOG_SHOW: {
537 string const name = ev.getArg(0);
539 disable = !(name == "aboutlyx" ||
542 name == "preferences" ||
547 if (name == "print") {
548 disable = !Exporter::IsExportable(buf, "dvi") ||
549 lyxrc.print_command == "none";
550 } else if (name == "character") {
551 UpdatableInset * tli = view()->theLockingInset();
552 disable = tli && tli->lyxCode() == InsetOld::ERT_CODE;
553 } else if (name == "vclog") {
554 disable = !buf->lyxvc.inUse();
555 } else if (name == "latexlog") {
556 disable = !IsFileReadable(buf->getLogName().second);
565 // the functions which insert insets
566 InsetOld::Code code = InsetOld::NO_CODE;
568 case LFUN_DIALOG_SHOW_NEW_INSET:
569 if (ev.argument == "bibitem")
570 code = InsetOld::BIBITEM_CODE;
571 else if (ev.argument == "bibtex")
572 code = InsetOld::BIBTEX_CODE;
573 else if (ev.argument == "citation")
574 code = InsetOld::CITE_CODE;
575 else if (ev.argument == "ert")
576 code = InsetOld::ERT_CODE;
577 else if (ev.argument == "external")
578 code = InsetOld::EXTERNAL_CODE;
579 else if (ev.argument == "float")
580 code = InsetOld::FLOAT_CODE;
581 else if (ev.argument == "graphics")
582 code = InsetOld::GRAPHICS_CODE;
583 else if (ev.argument == "include")
584 code = InsetOld::INCLUDE_CODE;
585 else if (ev.argument == "index")
586 code = InsetOld::INDEX_CODE;
587 else if (ev.argument == "label")
588 code = InsetOld::LABEL_CODE;
589 else if (ev.argument == "minipage")
590 code = InsetOld::MINIPAGE_CODE;
591 else if (ev.argument == "ref")
592 code = InsetOld::REF_CODE;
593 else if (ev.argument == "toc")
594 code = InsetOld::TOC_CODE;
595 else if (ev.argument == "url")
596 code = InsetOld::URL_CODE;
597 else if (ev.argument == "wrap")
598 code = InsetOld::WRAP_CODE;
602 code = InsetOld::ERT_CODE;
604 case LFUN_INSET_FOOTNOTE:
605 code = InsetOld::FOOT_CODE;
607 case LFUN_TABULAR_INSERT:
608 code = InsetOld::TABULAR_CODE;
610 case LFUN_INSET_MARGINAL:
611 code = InsetOld::MARGIN_CODE;
613 case LFUN_INSET_MINIPAGE:
614 code = InsetOld::MINIPAGE_CODE;
616 case LFUN_INSET_FLOAT:
617 case LFUN_INSET_WIDE_FLOAT:
618 code = InsetOld::FLOAT_CODE;
620 case LFUN_INSET_WRAP:
621 code = InsetOld::WRAP_CODE;
623 case LFUN_FLOAT_LIST:
624 code = InsetOld::FLOAT_LIST_CODE;
627 case LFUN_INSET_LIST:
628 code = InsetOld::LIST_CODE;
630 case LFUN_INSET_THEOREM:
631 code = InsetOld::THEOREM_CODE;
634 case LFUN_INSET_CAPTION:
635 code = InsetOld::CAPTION_CODE;
637 case LFUN_INSERT_NOTE:
638 code = InsetOld::NOTE_CODE;
640 case LFUN_INSERT_LABEL:
641 code = InsetOld::LABEL_CODE;
643 case LFUN_INSET_OPTARG:
644 code = InsetOld::OPTARG_CODE;
646 case LFUN_ENVIRONMENT_INSERT:
647 code = InsetOld::MINIPAGE_CODE;
649 case LFUN_INDEX_INSERT:
650 code = InsetOld::INDEX_CODE;
652 case LFUN_INDEX_PRINT:
653 code = InsetOld::INDEX_PRINT_CODE;
655 case LFUN_TOC_INSERT:
656 code = InsetOld::TOC_CODE;
660 code = InsetOld::URL_CODE;
663 // always allow this, since we will inset a raw quote
664 // if an inset is not allowed.
666 case LFUN_HYPHENATION:
667 case LFUN_LIGATURE_BREAK:
669 case LFUN_MENU_SEPARATOR:
671 case LFUN_END_OF_SENTENCE:
672 code = InsetOld::SPECIALCHAR_CODE;
674 case LFUN_SPACE_INSERT:
675 // slight hack: we know this is allowed in math mode
677 code = InsetOld::SPACE_CODE;
682 if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
688 // A few general toggles
690 case LFUN_TOOLTIPS_TOGGLE:
691 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
694 case LFUN_READ_ONLY_TOGGLE:
695 flag.setOnOff(buf->isReadonly());
698 flag.setOnOff(view()->getLyXText()->cursor.par()->params().startOfAppendix());
700 case LFUN_SWITCHBUFFER:
701 // toggle on the current buffer, but do not toggle off
702 // the other ones (is that a good idea?)
703 if (ev.argument == buf->fileName())
706 case LFUN_TRACK_CHANGES:
707 flag.setOnOff(buf->params.tracking_changes);
713 // the font related toggles
715 LyXFont const & font = view()->getLyXText()->real_current_font;
718 flag.setOnOff(font.emph() == LyXFont::ON);
721 flag.setOnOff(font.noun() == LyXFont::ON);
724 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
727 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
730 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
733 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
739 string tc = mathcursor->getLastCode();
742 flag.setOnOff(tc == "mathbf");
745 flag.setOnOff(tc == "mathsf");
748 flag.setOnOff(tc == "mathcal");
751 flag.setOnOff(tc == "mathrm");
754 flag.setOnOff(tc == "mathtt");
757 flag.setOnOff(tc == "mathbb");
760 flag.setOnOff(tc == "mathnormal");
767 // this one is difficult to get right. As a half-baked
768 // solution, we consider only the first action of the sequence
769 if (ev.action == LFUN_SEQUENCE) {
770 // argument contains ';'-terminated commands
771 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
778 void LyXFunc::dispatch(string const & s, bool verbose)
780 int const action = lyxaction.LookupFunc(s);
782 if (action == LFUN_UNKNOWN_ACTION) {
783 owner->message(bformat(_("Unknown function (%1$s)"), s));
787 dispatch(action, verbose);
791 void LyXFunc::dispatch(int ac, bool verbose)
793 dispatch(lyxaction.retrieveActionArg(ac), verbose);
797 bool ensureBufferClean(BufferView * bv) {
799 Buffer & buf = *bv->buffer();
803 string const file = MakeDisplayPath(buf.fileName(), 30);
804 string text = bformat(_("The document %1$s has unsaved "
805 "changes.\n\nDo you want to save "
806 "the document?"), file);
807 int const ret = Alert::prompt(_("Save changed document?"),
808 text, 0, 1, _("&Save"),
812 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
814 return buf.isClean();
820 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
822 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
823 <<"] arg[" << ev.argument << ']' << endl;
825 // we have not done anything wrong yet.
827 dispatch_buffer.erase();
829 #ifdef NEW_DISPATCHER
830 // We try do call the most specific dispatcher first:
831 // 1. the lockinginset's dispatch
832 // 2. the bufferview's dispatch
833 // 3. the lyxview's dispatch
836 selection_possible = false;
838 string argument = ev.argument;
839 kb_action action = ev.action;
841 // We cannot use this function here
842 if (getStatus(ev).disabled()) {
843 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
844 << lyxaction.getActionName(action)
845 << " [" << action << "] is disabled at this location"
847 setErrorMessage(getStatusMessage());
848 goto exit_with_message;
851 if (view()->available())
852 view()->hideCursor();
854 if (view()->available() && view()->theLockingInset()) {
855 InsetOld::RESULT result;
856 if (action > 1 || (action == LFUN_UNKNOWN_ACTION &&
859 UpdatableInset * inset = view()->theLockingInset();
863 inset->getCursorPos(view(), inset_x, dummy_y);
865 if ((action == LFUN_UNKNOWN_ACTION)
866 && argument.empty()) {
867 argument = encoded_last_key;
870 // the insets can't try to handle this,
871 // a table cell in the dummy position will
872 // lock its insettext, the insettext will
873 // pass it the bufferview, and succeed,
874 // so it will stay not locked. Not good
875 // if we've just done LFUN_ESCAPE (which
876 // injects an LFUN_PARAGRAPH_UPDATE)
877 if (action == LFUN_PARAGRAPH_UPDATE) {
878 view()->dispatch(ev);
879 goto exit_with_message;
882 // Undo/Redo is a bit tricky for insets.
883 if (action == LFUN_UNDO) {
885 goto exit_with_message;
886 } else if (action == LFUN_REDO) {
888 goto exit_with_message;
889 } else if (((result=inset->
890 // Hand-over to inset's own dispatch:
891 localDispatch(FuncRequest(view(), action, argument))) ==
893 (result == DISPATCHED_NOUPDATE))
894 goto exit_with_message;
895 // If UNDISPATCHED, just soldier on
896 else if (result == FINISHED) {
897 owner->clearMessage();
898 goto exit_with_message;
899 // We do not need special RTL handling here:
900 // FINISHED means that the cursor should be
901 // one position after the inset.
902 } else if (result == FINISHED_RIGHT) {
903 view()->text->cursorRight(view());
905 owner->clearMessage();
906 goto exit_with_message;
907 } else if (result == FINISHED_UP) {
908 RowList::iterator const irow = view()->text->cursorIRow();
909 if (irow != view()->text->rows().begin()) {
911 view()->text->setCursorFromCoordinates(
912 view()->text->cursor.ix() + inset_x,
913 view()->text->cursor.iy() -
914 irow->baseline() - 1);
915 view()->text->cursor.x_fix(view()->text->cursor.x());
917 view()->text->cursorUp(view());
921 view()->update(view()->text, BufferView::SELECT);
923 owner->clearMessage();
924 goto exit_with_message;
925 } else if (result == FINISHED_DOWN) {
926 RowList::iterator const irow = view()->text->cursorIRow();
927 if (boost::next(irow) != view()->text->rows().end()) {
929 view()->text->setCursorFromCoordinates(
930 view()->text->cursor.ix() + inset_x,
931 view()->text->cursor.iy() -
934 view()->text->cursor.x_fix(view()->text->cursor.x());
936 view()->text->cursorDown(view());
939 view()->text->cursorRight(view());
942 owner->clearMessage();
943 goto exit_with_message;
945 #warning I am not sure this is still right, please have a look! (Jug 20020417)
946 else { // result == UNDISPATCHED
947 //setMessage(N_("Text mode"));
949 case LFUN_UNKNOWN_ACTION:
950 case LFUN_BREAKPARAGRAPH:
952 view()->text->cursorRight(view());
953 view()->switchKeyMap();
954 owner->view_state_changed();
957 if (!view()->text->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
958 view()->text->cursorRight(view());
960 owner->view_state_changed();
962 goto exit_with_message;
964 if (view()->text->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
965 view()->text->cursorRight(view());
967 owner->view_state_changed();
969 goto exit_with_message;
971 if (boost::next(view()->text->cursorRow()) != view()->text->rows().end())
972 view()->text->cursorDown(view());
974 view()->text->cursorRight(view());
976 owner->view_state_changed();
977 goto exit_with_message;
988 if (!view()->available())
990 // this function should be used always [asierra060396]
991 UpdatableInset * tli = view()->theLockingInset();
993 UpdatableInset * lock = tli->getLockingInset();
996 view()->unlockInset(tli);
997 view()->text->cursorRight(view());
999 owner->view_state_changed();
1001 tli->unlockInsetInInset(view(), lock, true);
1004 // Tell the paragraph dialog that we changed paragraph
1005 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1010 case LFUN_WORDFINDFORWARD:
1011 case LFUN_WORDFINDBACKWARD: {
1012 static string last_search;
1013 string searched_string;
1015 if (!argument.empty()) {
1016 last_search = argument;
1017 searched_string = argument;
1019 searched_string = last_search;
1021 bool fw = (action == LFUN_WORDFINDFORWARD);
1022 if (!searched_string.empty())
1023 lyx::find::find(view(), searched_string, fw);
1028 if (view()->available() && !view()->theLockingInset())
1029 view()->update(view()->text, BufferView::SELECT);
1030 owner->message(keyseq.printOptions());
1033 // --- Misc -------------------------------------------
1034 case LFUN_EXEC_COMMAND:
1035 owner->focus_command_buffer();
1040 meta_fake_bit = key_modifier::none;
1041 if (view()->available())
1042 // cancel any selection
1043 dispatch(LFUN_MARK_OFF);
1044 setMessage(N_("Cancel"));
1047 case LFUN_META_FAKE:
1048 meta_fake_bit = key_modifier::alt;
1049 setMessage(keyseq.print());
1052 case LFUN_READ_ONLY_TOGGLE:
1053 if (owner->buffer()->lyxvc.inUse())
1054 owner->buffer()->lyxvc.toggleReadOnly();
1056 owner->buffer()->setReadonly(
1057 !owner->buffer()->isReadonly());
1060 case LFUN_CENTER: // this is center and redraw.
1064 // --- Menus -----------------------------------------------
1066 menuNew(argument, false);
1069 case LFUN_MENUNEWTMPLT:
1070 menuNew(argument, true);
1073 case LFUN_CLOSEBUFFER:
1077 case LFUN_MENUWRITE:
1078 if (!owner->buffer()->isUnnamed()) {
1079 string const str = bformat(_("Saving document %1$s..."),
1080 MakeDisplayPath(owner->buffer()->fileName()));
1081 owner->message(str);
1082 MenuWrite(owner->buffer());
1083 owner->message(str + _(" done."));
1085 WriteAs(owner->buffer());
1089 WriteAs(owner->buffer(), argument);
1092 case LFUN_MENURELOAD: {
1093 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
1094 string text = bformat(_("Any changes will be lost. Are you sure "
1095 "you want to revert to the saved version of the document %1$s?"), file);
1096 int const ret = Alert::prompt(_("Revert to saved document?"),
1097 text, 0, 1, _("&Revert"), _("&Cancel"));
1105 Exporter::Export(owner->buffer(), argument, true);
1106 view()->showErrorList(BufferFormat(*owner->buffer()));
1110 Exporter::Preview(owner->buffer(), argument);
1111 view()->showErrorList(BufferFormat(*owner->buffer()));
1114 case LFUN_BUILDPROG:
1115 Exporter::Export(owner->buffer(), "program", true);
1116 view()->showErrorList(_("Build"));
1119 case LFUN_RUNCHKTEX:
1120 owner->buffer()->runChktex();
1121 view()->showErrorList(_("ChkTeX"));
1125 if (argument == "custom")
1126 owner->getDialogs().showSendto();
1128 Exporter::Export(owner->buffer(), argument, false);
1129 view()->showErrorList(BufferFormat(*owner->buffer()));
1143 InsetCommandParams p("tableofcontents");
1144 string const data = InsetCommandMailer::params2string("toc", p);
1145 owner->getDialogs().show("toc", data, 0);
1161 case LFUN_DEPTH_MIN:
1162 changeDepth(view(), view()->getLyXText(), DEC_DEPTH, false);
1163 owner->view_state_changed();
1166 case LFUN_DEPTH_PLUS:
1167 changeDepth(view(), view()->getLyXText(), INC_DEPTH, false);
1168 owner->view_state_changed();
1171 case LFUN_FREEFONT_APPLY:
1172 apply_freefont(view());
1175 case LFUN_FREEFONT_UPDATE:
1176 update_and_apply_freefont(view(), argument);
1179 case LFUN_RECONFIGURE:
1180 Reconfigure(view());
1184 case LFUN_FLOATSOPERATE:
1185 if (argument == "openfoot")
1186 view()->allFloats(1,0);
1187 else if (argument == "closefoot")
1188 view()->allFloats(0,0);
1189 else if (argument == "openfig")
1190 view()->allFloats(1,1);
1191 else if (argument == "closefig")
1192 view()->allFloats(0,1);
1195 #ifdef WITH_WARNINGS
1196 //#warning Find another implementation here (or another lyxfunc)!
1199 case LFUN_HELP_OPEN:
1201 string const arg = argument;
1203 setErrorMessage(N_("Missing argument"));
1206 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1207 if (fname.empty()) {
1208 lyxerr << "LyX: unable to find documentation file `"
1209 << arg << "'. Bad installation?" << endl;
1212 owner->message(bformat(_("Opening help file %1$s..."),
1213 MakeDisplayPath(fname)));
1214 view()->loadLyXFile(fname, false);
1218 // --- version control -------------------------------
1219 case LFUN_VC_REGISTER:
1221 if (!ensureBufferClean(view()))
1223 if (!owner->buffer()->lyxvc.inUse()) {
1224 owner->buffer()->lyxvc.registrer();
1230 case LFUN_VC_CHECKIN:
1232 if (!ensureBufferClean(view()))
1234 if (owner->buffer()->lyxvc.inUse()
1235 && !owner->buffer()->isReadonly()) {
1236 owner->buffer()->lyxvc.checkIn();
1242 case LFUN_VC_CHECKOUT:
1244 if (!ensureBufferClean(view()))
1246 if (owner->buffer()->lyxvc.inUse()
1247 && owner->buffer()->isReadonly()) {
1248 owner->buffer()->lyxvc.checkOut();
1254 case LFUN_VC_REVERT:
1256 owner->buffer()->lyxvc.revert();
1263 owner->buffer()->lyxvc.undoLast();
1268 // --- buffers ----------------------------------------
1270 case LFUN_SWITCHBUFFER:
1271 view()->buffer(bufferlist.getBuffer(argument));
1275 NewFile(view(), argument);
1278 case LFUN_FILE_OPEN:
1282 case LFUN_LAYOUT_TABULAR:
1283 if (view()->theLockingInset()) {
1284 if (view()->theLockingInset()->lyxCode()== InsetOld::TABULAR_CODE) {
1285 InsetTabular * inset = static_cast<InsetTabular *>
1286 (view()->theLockingInset());
1287 inset->openLayoutDialog(view());
1288 } else if (view()->theLockingInset()->
1289 getFirstLockingInsetOfType(InsetOld::TABULAR_CODE)!=0) {
1290 InsetTabular * inset = static_cast<InsetTabular *>(
1291 view()->theLockingInset()->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE));
1292 inset->openLayoutDialog(view());
1297 case LFUN_DROP_LAYOUTS_CHOICE:
1298 owner->getToolbar().openLayoutList();
1301 case LFUN_MENU_OPEN_BY_NAME:
1302 owner->getMenubar().openByName(argument);
1303 break; // RVDK_PATCH_5
1305 // --- lyxserver commands ----------------------------
1309 setMessage(owner->buffer()->fileName());
1310 lyxerr[Debug::INFO] << "FNAME["
1311 << owner->buffer()->fileName()
1317 dispatch_buffer = keyseq.print();
1318 lyxserver->notifyClient(dispatch_buffer);
1322 case LFUN_GOTOFILEROW:
1326 istringstream istr(argument.c_str());
1327 istr >> file_name >> row;
1328 // Must replace extension of the file to be .lyx and get full path
1329 string const s(ChangeExtension(file_name, ".lyx"));
1331 // Either change buffer or load the file
1332 if (bufferlist.exists(s)) {
1333 view()->buffer(bufferlist.getBuffer(s));
1335 view()->loadLyXFile(s);
1338 view()->setCursorFromRow(row);
1341 // see BufferView_pimpl::center()
1342 view()->updateScrollbar();
1346 case LFUN_GOTO_PARAGRAPH:
1348 istringstream istr(argument.c_str());
1352 ParIterator par = owner->buffer()->getParFromID(id);
1353 if (par == owner->buffer()->par_iterator_end()) {
1354 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1355 << id << ']' << endl;
1358 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1359 << " found." << endl;
1362 if (view()->theLockingInset())
1363 view()->unlockInset(view()->theLockingInset());
1364 if (par->inInset()) {
1365 FuncRequest cmd(view(), LFUN_INSET_EDIT, "left");
1366 par->inInset()->localDispatch(cmd);
1369 view()->getLyXText()->setCursor(par.pit(), 0);
1370 view()->switchKeyMap();
1371 owner->view_state_changed();
1374 // see BufferView_pimpl::center()
1375 view()->updateScrollbar();
1379 // --- insert characters ----------------------------------------
1381 // --- Mathed stuff. If we are here, there is no locked inset yet.
1382 case LFUN_MATH_EXTERN:
1383 case LFUN_MATH_NUMBER:
1384 case LFUN_MATH_NONUMBER:
1385 case LFUN_MATH_LIMITS:
1387 setErrorMessage(N_("This is only allowed in math mode!"));
1391 // passthrough hat and underscore outside mathed:
1392 case LFUN_SUBSCRIPT:
1393 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1395 case LFUN_SUPERSCRIPT:
1396 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1399 case LFUN_DIALOG_SHOW: {
1400 string const name = ev.getArg(0);
1401 string data = trim(ev.argument.substr(name.size()));
1403 if (name == "character") {
1404 data = freefont2string();
1406 owner->getDialogs().show("character", data);
1407 } else if (name == "document")
1408 owner->getDialogs().showDocument();
1409 else if (name == "findreplace")
1410 owner->getDialogs().showSearch();
1411 else if (name == "forks")
1412 owner->getDialogs().showForks();
1413 else if (name == "preamble")
1414 owner->getDialogs().showPreamble();
1415 else if (name == "preferences")
1416 owner->getDialogs().showPreferences();
1417 else if (name == "print")
1418 owner->getDialogs().showPrint();
1419 else if (name == "spellchecker")
1420 owner->getDialogs().showSpellchecker();
1422 owner->getDialogs().show(name, data);
1426 case LFUN_DIALOG_SHOW_NEW_INSET: {
1427 string const & name = argument;
1429 if (name == "bibitem" ||
1431 name == "include" ||
1436 InsetCommandParams p(name);
1437 data = InsetCommandMailer::params2string(name, p);
1438 } else if (name == "citation") {
1439 InsetCommandParams p("cite");
1440 data = InsetCommandMailer::params2string(name, p);
1442 owner->getDialogs().show(name, data, 0);
1446 case LFUN_DIALOG_SHOW_NEXT_INSET: {
1450 case LFUN_DIALOG_UPDATE: {
1451 string const & name = argument;
1452 // Can only update a dialog connected to an existing inset
1453 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1455 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1457 inset->localDispatch(fr);
1458 } else if (name == "paragraph") {
1459 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1464 case LFUN_DIALOG_HIDE:
1465 Dialogs::hide(argument, 0);
1468 case LFUN_DIALOG_DISCONNECT_INSET:
1469 owner->getDialogs().disconnect(argument);
1472 case LFUN_CHILDOPEN:
1474 string const filename =
1475 MakeAbsPath(argument,
1476 owner->buffer()->filePath());
1477 setMessage(N_("Opening child document ") +
1478 MakeDisplayPath(filename) + "...");
1479 view()->savePosition(0);
1480 if (bufferlist.exists(filename))
1481 view()->buffer(bufferlist.getBuffer(filename));
1483 view()->loadLyXFile(filename);
1487 case LFUN_TOGGLECURSORFOLLOW:
1488 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1492 owner->getIntl().KeyMapOn(false);
1495 case LFUN_KMAP_PRIM:
1496 owner->getIntl().KeyMapPrim();
1500 owner->getIntl().KeyMapSec();
1503 case LFUN_KMAP_TOGGLE:
1504 owner->getIntl().ToggleKeyMap();
1509 // argument contains ';'-terminated commands
1510 while (!argument.empty()) {
1512 argument = split(argument, first, ';');
1518 case LFUN_SAVEPREFERENCES:
1520 Path p(user_lyxdir());
1521 lyxrc.write("preferences");
1525 case LFUN_SCREEN_FONT_UPDATE:
1527 // handle the screen font changes.
1528 lyxrc.set_font_norm_type();
1529 lyx_gui::update_fonts();
1530 // We also need to empty the textcache so that
1531 // the buffer will be formatted correctly after
1534 // Of course we should only do the resize and the textcache.clear
1535 // if values really changed...but not very important right now. (Lgb)
1536 // All visible buffers will need resize
1542 case LFUN_SET_COLOR:
1545 string const x11_name = split(argument, lyx_name, ' ');
1546 if (lyx_name.empty() || x11_name.empty()) {
1547 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1552 bool const graphicsbg_changed =
1553 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1554 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1556 if (!lcolor.setColor(lyx_name, x11_name)) {
1558 bformat(_("Set-color \"%1$s\" failed "
1559 "- color is undefined or "
1560 "may not be redefined"), lyx_name));
1564 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1566 if (graphicsbg_changed) {
1567 #ifdef WITH_WARNINGS
1568 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1571 lyx::graphics::GCache & gc =
1572 lyx::graphics::GCache::get();
1573 gc.changeDisplay(true);
1582 owner->message(argument);
1585 case LFUN_FORKS_KILL:
1587 if (!isStrInt(argument))
1590 pid_t const pid = strToInt(argument);
1591 ForkedcallsController & fcc = ForkedcallsController::get();
1596 case LFUN_TOOLTIPS_TOGGLE:
1597 owner->getDialogs().toggleTooltips();
1600 case LFUN_EXTERNAL_EDIT: {
1602 .localDispatch(FuncRequest(view(), action, argument));
1607 // Then if it was none of the above
1608 // Trying the BufferView::pimpl dispatch:
1609 if (!view()->dispatch(ev))
1610 lyxerr << "A truly unknown func ["
1611 << lyxaction.getActionName(ev.action) << "]!"
1618 view()->owner()->updateLayoutChoice();
1620 if (view()->available()) {
1621 view()->fitCursor();
1623 // If we executed a mutating lfun, mark the buffer as dirty
1624 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)
1625 && !lyxaction.funcHasFlag(ev.action, LyXAction::ReadOnly))
1626 view()->buffer()->markDirty();
1629 sendDispatchMessage(getMessage(), ev, verbose);
1633 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1635 owner->updateMenubar();
1636 owner->updateToolbar();
1638 if (ev.action == LFUN_SELFINSERT || !verbose) {
1639 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1641 owner->message(msg);
1645 string dispatch_msg = msg;
1646 if (!dispatch_msg.empty())
1647 dispatch_msg += ' ';
1649 string comname = lyxaction.getActionName(ev.action);
1651 int pseudoaction = ev.action;
1652 bool argsadded = false;
1654 if (!ev.argument.empty()) {
1655 // the pseudoaction is useful for the bindings
1656 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1658 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1659 pseudoaction = ev.action;
1661 comname += ' ' + ev.argument;
1666 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1668 if (!shortcuts.empty()) {
1669 comname += ": " + shortcuts;
1670 } else if (!argsadded && !ev.argument.empty()) {
1671 comname += ' ' + ev.argument;
1674 if (!comname.empty()) {
1675 comname = rtrim(comname);
1676 dispatch_msg += '(' + comname + ')';
1679 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1680 if (!dispatch_msg.empty())
1681 owner->message(dispatch_msg);
1685 void LyXFunc::setupLocalKeymap()
1687 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1688 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1692 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1694 string initpath = lyxrc.document_path;
1695 string filename(name);
1697 if (view()->available()) {
1698 string const trypath = owner->buffer()->filePath();
1699 // If directory is writeable, use this as default.
1700 if (IsDirWriteable(trypath))
1704 static int newfile_number;
1706 if (filename.empty()) {
1707 filename = AddName(lyxrc.document_path,
1708 "newfile" + tostr(++newfile_number) + ".lyx");
1709 FileInfo fi(filename);
1710 while (bufferlist.exists(filename) || fi.readable()) {
1712 filename = AddName(lyxrc.document_path,
1713 "newfile" + tostr(newfile_number) +
1715 fi.newFile(filename);
1719 // The template stuff
1722 FileDialog fileDlg(_("Select template file"),
1723 LFUN_SELECT_FILE_SYNC,
1724 make_pair(string(_("Documents|#o#O")),
1725 string(lyxrc.document_path)),
1726 make_pair(string(_("Templates|#T#t")),
1727 string(lyxrc.template_path)));
1729 FileDialog::Result result =
1730 fileDlg.open(lyxrc.template_path,
1731 _("*.lyx| LyX Documents (*.lyx)"));
1733 if (result.first == FileDialog::Later)
1736 string const fname = result.second;
1743 view()->newFile(filename, templname, !name.empty());
1747 void LyXFunc::open(string const & fname)
1749 string initpath = lyxrc.document_path;
1751 if (view()->available()) {
1752 string const trypath = owner->buffer()->filePath();
1753 // If directory is writeable, use this as default.
1754 if (IsDirWriteable(trypath))
1760 if (fname.empty()) {
1761 FileDialog fileDlg(_("Select document to open"),
1763 make_pair(string(_("Documents|#o#O")),
1764 string(lyxrc.document_path)),
1765 make_pair(string(_("Examples|#E#e")),
1766 string(AddPath(system_lyxdir(), "examples"))));
1768 FileDialog::Result result =
1769 fileDlg.open(initpath,
1770 _("*.lyx| LyX Documents (*.lyx)"));
1772 if (result.first == FileDialog::Later)
1775 filename = result.second;
1777 // check selected filename
1778 if (filename.empty()) {
1779 owner->message(_("Canceled."));
1785 // get absolute path of file and add ".lyx" to the filename if
1787 string const fullpath = FileSearch(string(), filename, "lyx");
1788 if (!fullpath.empty()) {
1789 filename = fullpath;
1792 string const disp_fn(MakeDisplayPath(filename));
1794 // if the file doesn't exist, let the user create one
1795 FileInfo const f(filename, true);
1797 // the user specifically chose this name. Believe them.
1798 view()->newFile(filename, "", true);
1802 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1805 if (view()->loadLyXFile(filename)) {
1806 str2 = bformat(_("Document %1$s opened."), disp_fn);
1808 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1810 owner->message(str2);
1814 void LyXFunc::doImport(string const & argument)
1817 string filename = split(argument, format, ' ');
1819 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1820 << " file: " << filename << endl;
1822 // need user interaction
1823 if (filename.empty()) {
1824 string initpath = lyxrc.document_path;
1826 if (view()->available()) {
1827 string const trypath = owner->buffer()->filePath();
1828 // If directory is writeable, use this as default.
1829 if (IsDirWriteable(trypath))
1833 string const text = bformat(_("Select %1$s file to import"),
1834 formats.prettyName(format));
1836 FileDialog fileDlg(text,
1838 make_pair(string(_("Documents|#o#O")),
1839 string(lyxrc.document_path)),
1840 make_pair(string(_("Examples|#E#e")),
1841 string(AddPath(system_lyxdir(), "examples"))));
1843 string const extension = "*." + formats.extension(format)
1844 + "| " + formats.prettyName(format)
1845 + " (*." + formats.extension(format) + ')';
1847 FileDialog::Result result = fileDlg.open(initpath,
1850 if (result.first == FileDialog::Later)
1853 filename = result.second;
1855 // check selected filename
1856 if (filename.empty())
1857 owner->message(_("Canceled."));
1860 if (filename.empty())
1863 // get absolute path of file
1864 filename = MakeAbsPath(filename);
1866 string const lyxfile = ChangeExtension(filename, ".lyx");
1868 // Check if the document already is open
1869 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1870 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1871 owner->message(_("Canceled."));
1876 // if the file exists already, and we didn't do
1877 // -i lyx thefile.lyx, warn
1878 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1879 string const file = MakeDisplayPath(lyxfile, 30);
1881 string text = bformat(_("The document %1$s already exists.\n\n"
1882 "Do you want to over-write that document?"), file);
1883 int const ret = Alert::prompt(_("Over-write document?"),
1884 text, 0, 1, _("&Over-write"), _("&Cancel"));
1887 owner->message(_("Canceled."));
1892 Importer::Import(owner, filename, format);
1896 void LyXFunc::closeBuffer()
1898 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1899 if (bufferlist.empty()) {
1900 // need this otherwise SEGV may occur while
1901 // trying to set variables that don't exist
1902 // since there's no current buffer
1903 owner->getDialogs().hideBufferDependent();
1905 view()->buffer(bufferlist.first());
1911 // Each "owner" should have it's own message method. lyxview and
1912 // the minibuffer would use the minibuffer, but lyxserver would
1913 // send an ERROR signal to its client. Alejandro 970603
1914 // This func is bit problematic when it comes to NLS, to make the
1915 // lyx servers client be language indepenent we must not translate
1916 // strings sent to this func.
1917 void LyXFunc::setErrorMessage(string const & m) const
1919 dispatch_buffer = m;
1924 void LyXFunc::setMessage(string const & m) const
1926 dispatch_buffer = m;
1930 void LyXFunc::setStatusMessage(string const & m) const
1936 string const LyXFunc::view_status_message()
1938 // When meta-fake key is pressed, show the key sequence so far + "M-".
1940 return keyseq.print() + "M-";
1943 // Else, when a non-complete key sequence is pressed,
1944 // show the available options.
1945 if (keyseq.length() > 0 && !keyseq.deleted()) {
1946 return keyseq.printOptions();
1949 if (!view()->available())
1950 return _("Welcome to LyX!");
1952 return currentState(view());
1956 BufferView * LyXFunc::view() const
1959 return owner->view().get();
1963 bool LyXFunc::wasMetaKey() const
1965 return (meta_fake_bit != key_modifier::none);