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 * ====================================================== */
16 #include "bufferlist.h"
18 #include "buffer_funcs.h"
19 #include "BufferView.h"
20 #include "lyxserver.h"
24 #include "LyXAction.h"
30 #include "trans_mgr.h"
33 #include "bufferview_funcs.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/lyx_gui.h"
37 #include "FloatList.h"
41 #include "TextCache.h"
43 #include "undo_funcs.h"
44 #include "ParagraphParameters.h"
46 #include "insets/insetcommand.h"
47 #include "insets/insetexternal.h"
48 #include "insets/insettabular.h"
50 #include "mathed/formulamacro.h"
51 #include "mathed/math_cursor.h"
52 #include "mathed/math_inset.h"
54 #include "frontends/FileDialog.h"
55 #include "frontends/Dialogs.h"
56 #include "frontends/Toolbar.h"
57 #include "frontends/Menubar.h"
58 #include "frontends/Alert.h"
60 #include "graphics/GraphicsCache.h"
62 #include "support/lyxalgo.h"
63 #include "support/LAssert.h"
64 #include "support/filetools.h"
65 #include "support/FileInfo.h"
66 #include "support/forkedcontr.h"
67 #include "support/lstrings.h"
68 #include "support/tostr.h"
69 #include "support/path.h"
70 #include "support/path_defines.h"
71 #include "support/lyxfunctional.h"
81 using namespace lyx::support;
89 using std::back_inserter;
90 using namespace bv_funcs;
92 extern BufferList bufferlist;
93 extern LyXServer * lyxserver;
94 extern bool selection_possible;
96 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
99 extern tex_accent_struct get_accent(kb_action action);
101 extern void ShowLatexLog();
104 LyXFunc::LyXFunc(LyXView * o)
107 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
108 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
109 meta_fake_bit(key_modifier::none)
114 void LyXFunc::moveCursorUpdate()
116 LyXText * lt = view()->text;
117 if (lt->selection.mark())
120 view()->switchKeyMap();
124 void LyXFunc::handleKeyFunc(kb_action action)
126 char c = encoded_last_key;
128 if (keyseq.length()) {
132 owner->getIntl().getTransManager()
133 .deadkey(c, get_accent(action).accent, view()->getLyXText());
134 // Need to clear, in case the minibuffer calls these
137 // copied verbatim from do_accent_char
139 view()->getLyXText()->selection.cursor = view()->getLyXText()->cursor;
143 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
144 key_modifier::state state)
148 if (lyxerr.debugging(Debug::KEY)) {
149 lyxerr << "KeySym is "
150 << keysym->getSymbolName()
154 // Do nothing if we have nothing (JMarc)
155 if (!keysym->isOK()) {
156 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
161 if (keysym->isModifier()) {
162 lyxerr[Debug::KEY] << "isModifier true" << endl;
166 Encoding const * encoding = view()->getEncoding();
168 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
170 // Do a one-deep top-level lookup for
171 // cancel and meta-fake keys. RVDK_PATCH_5
172 cancel_meta_seq.reset();
174 int action = cancel_meta_seq.addkey(keysym, state);
175 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
177 // When not cancel or meta-fake, do the normal lookup.
178 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
179 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
180 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
181 // remove Caps Lock and Mod2 as a modifiers
182 action = keyseq.addkey(keysym, (state | meta_fake_bit));
183 lyxerr[Debug::KEY] << "action now set to ["
184 << action << ']' << endl;
187 // Dont remove this unless you know what you are doing.
188 meta_fake_bit = key_modifier::none;
190 // can this happen now ?
191 if (action == LFUN_NOACTION) {
192 action = LFUN_PREFIX;
195 if (lyxerr.debugging(Debug::KEY)) {
196 lyxerr << "Key [action="
198 << keyseq.print() << ']'
202 // already here we know if it any point in going further
203 // why not return already here if action == -1 and
204 // num_bytes == 0? (Lgb)
206 if (keyseq.length() > 1) {
207 owner->message(keyseq.print());
211 // Maybe user can only reach the key via holding down shift.
212 // Let's see. But only if shift is the only modifier
213 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
214 lyxerr[Debug::KEY] << "Trying without shift" << endl;
215 action = keyseq.addkey(keysym, key_modifier::none);
216 lyxerr[Debug::KEY] << "Action now " << action << endl;
219 if (action == LFUN_UNKNOWN_ACTION) {
220 // Hmm, we didn't match any of the keysequences. See
221 // if it's normal insertable text not already covered
223 if (keysym->isText() && keyseq.length() == 1) {
224 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
225 action = LFUN_SELFINSERT;
227 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
228 owner->message(_("Unknown function."));
233 if (action == LFUN_SELFINSERT) {
234 if (encoded_last_key != 0) {
236 arg += encoded_last_key;
238 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
240 lyxerr[Debug::KEY] << "SelfInsert arg[`"
241 << argument << "']" << endl;
249 FuncStatus LyXFunc::getStatus(int ac) const
251 return getStatus(lyxaction.retrieveActionArg(ac));
255 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
258 Buffer * buf = owner->buffer();
260 if (ev.action == LFUN_NOACTION) {
261 setStatusMessage(N_("Nothing to do"));
262 return flag.disabled(true);
266 case LFUN_UNKNOWN_ACTION:
267 #ifndef HAVE_LIBAIKSAURUS
268 case LFUN_THESAURUS_ENTRY:
273 flag |= lyx_gui::getStatus(ev);
276 if (flag.unknown()) {
277 setStatusMessage(N_("Unknown action"));
281 // the default error message if we disable the command
282 setStatusMessage(N_("Command disabled"));
284 // Check whether we need a buffer
285 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
286 // Yes we need a buffer, do we have one?
289 // Can we use a readonly buffer?
290 if (buf->isReadonly() &&
291 !lyxaction.funcHasFlag(ev.action,
292 LyXAction::ReadOnly)) {
294 setStatusMessage(N_("Document is read-only"));
299 setStatusMessage(N_("Command not allowed with"
300 "out any document open"));
301 return flag.disabled(true);
305 UpdatableInset * tli = view()->theLockingInset();
307 // I would really like to avoid having this switch and rather try to
308 // encode this in the function itself.
309 bool disable = false;
312 disable = ev.argument != "custom"
313 && !Exporter::IsExportable(buf, ev.argument);
316 disable = buf->undostack.empty();
319 disable = buf->redostack.empty();
324 UpdatableInset * in = tli;
325 if (in->lyxCode() != InsetOld::TABULAR_CODE) {
326 in = tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE);
328 if (in && static_cast<InsetTabular*>(in)->hasSelection()) {
333 disable = !mathcursor && !view()->getLyXText()->selection.set();
336 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
339 disable = !Exporter::IsExportable(buf, "program");
342 case LFUN_LAYOUT_TABULAR:
344 || (tli->lyxCode() != InsetOld::TABULAR_CODE
345 && !tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE));
349 disable = !changeDepth(view(), view()->getLyXText(), DEC_DEPTH, true);
352 case LFUN_DEPTH_PLUS:
353 disable = !changeDepth(view(), view()->getLyXText(), INC_DEPTH, true);
357 case LFUN_LAYOUT_PARAGRAPH: {
358 InsetOld * inset = view()->getLyXText()->cursor.par()->inInset();
359 disable = inset && inset->forceDefaultParagraphs(inset);
363 case LFUN_INSET_OPTARG:
364 disable = (view()->getLyXText()->cursor.par()->layout()->optionalargs == 0);
367 case LFUN_TABULAR_FEATURE:
370 // FIXME: check temporarily disabled
372 char align = mathcursor->valign();
377 if (ev.argument.empty()) {
381 if (!contains("tcb", ev.argument[0])) {
385 flag.setOnOff(ev.argument[0] == align);
389 char align = mathcursor->halign();
394 if (ev.argument.empty()) {
398 if (!contains("lcr", ev.argument[0])) {
402 flag.setOnOff(ev.argument[0] == align);
405 disable = !mathcursor->halign();
411 //ret.disabled(true);
412 if (tli->lyxCode() == InsetOld::TABULAR_CODE) {
413 ret = static_cast<InsetTabular *>(tli)
414 ->getStatus(ev.argument);
417 } else if (tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE)) {
418 ret = static_cast<InsetTabular *>
419 (tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE))
420 ->getStatus(ev.argument);
427 static InsetTabular inset(*owner->buffer(), 1, 1);
431 ret = inset.getStatus(ev.argument);
432 if (ret.onoff(true) || ret.onoff(false))
433 flag.setOnOff(false);
437 case LFUN_VC_REGISTER:
438 disable = buf->lyxvc.inUse();
440 case LFUN_VC_CHECKIN:
441 disable = !buf->lyxvc.inUse() || buf->isReadonly();
443 case LFUN_VC_CHECKOUT:
444 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
448 disable = !buf->lyxvc.inUse();
450 case LFUN_MENURELOAD:
451 disable = buf->isUnnamed() || buf->isClean();
453 case LFUN_BOOKMARK_GOTO:
455 isSavedPosition(strToUnsignedInt(ev.argument));
457 case LFUN_MERGE_CHANGES:
458 case LFUN_ACCEPT_CHANGE:
459 case LFUN_REJECT_CHANGE:
460 case LFUN_ACCEPT_ALL_CHANGES:
461 case LFUN_REJECT_ALL_CHANGES:
462 disable = !buf->params.tracking_changes;
464 case LFUN_INSET_TOGGLE: {
465 LyXText * lt = view()->getLyXText();
466 disable = !(isEditableInset(lt->getInset())
468 && lt->inset_owner->owner()
469 && lt->inset_owner->owner()->isOpen()));
473 case LFUN_INSET_SETTINGS: {
475 UpdatableInset * inset = view()->theLockingInset();
480 // get the innermost inset
481 inset = inset->getLockingInset();
483 // jump back to owner if an InsetText, so
484 // we get back to the InsetTabular or whatever
485 if (inset->lyxCode() == InsetOld::TEXT_CODE)
486 inset = inset->owner();
488 InsetOld::Code code = inset->lyxCode();
490 case InsetOld::TABULAR_CODE:
491 disable = ev.argument != "tabular";
493 case InsetOld::ERT_CODE:
494 disable = ev.argument != "ert";
496 case InsetOld::FLOAT_CODE:
497 disable = ev.argument != "float";
499 case InsetOld::MINIPAGE_CODE:
500 disable = ev.argument != "minipage";
502 case InsetOld::WRAP_CODE:
503 disable = ev.argument != "wrap";
505 case InsetOld::NOTE_CODE:
506 disable = ev.argument != "note";
514 case LFUN_MATH_MUTATE:
516 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
517 flag.setOnOff(false);
522 // we just need to be in math mode to enable that
524 case LFUN_MATH_SPACE:
525 case LFUN_MATH_LIMITS:
526 case LFUN_MATH_NONUMBER:
527 case LFUN_MATH_NUMBER:
528 case LFUN_MATH_EXTERN:
529 disable = !mathcursor;
532 case LFUN_DIALOG_SHOW: {
533 string const name = ev.getArg(0);
535 disable = !(name == "aboutlyx" ||
538 name == "preferences" ||
543 if (name == "print") {
544 disable = !Exporter::IsExportable(buf, "dvi") ||
545 lyxrc.print_command == "none";
546 } else if (name == "character") {
547 UpdatableInset * tli = view()->theLockingInset();
548 disable = tli && tli->lyxCode() == InsetOld::ERT_CODE;
549 } else if (name == "vclog") {
550 disable = !buf->lyxvc.inUse();
551 } else if (name == "latexlog") {
552 disable = !IsFileReadable(buf->getLogName().second);
561 // the functions which insert insets
562 InsetOld::Code code = InsetOld::NO_CODE;
564 case LFUN_DIALOG_SHOW_NEW_INSET:
565 if (ev.argument == "bibitem")
566 code = InsetOld::BIBITEM_CODE;
567 else if (ev.argument == "bibtex")
568 code = InsetOld::BIBTEX_CODE;
569 else if (ev.argument == "citation")
570 code = InsetOld::CITE_CODE;
571 else if (ev.argument == "ert")
572 code = InsetOld::ERT_CODE;
573 else if (ev.argument == "external")
574 code = InsetOld::EXTERNAL_CODE;
575 else if (ev.argument == "float")
576 code = InsetOld::FLOAT_CODE;
577 else if (ev.argument == "graphics")
578 code = InsetOld::GRAPHICS_CODE;
579 else if (ev.argument == "include")
580 code = InsetOld::INCLUDE_CODE;
581 else if (ev.argument == "index")
582 code = InsetOld::INDEX_CODE;
583 else if (ev.argument == "label")
584 code = InsetOld::LABEL_CODE;
585 else if (ev.argument == "minipage")
586 code = InsetOld::MINIPAGE_CODE;
587 else if (ev.argument == "ref")
588 code = InsetOld::REF_CODE;
589 else if (ev.argument == "toc")
590 code = InsetOld::TOC_CODE;
591 else if (ev.argument == "url")
592 code = InsetOld::URL_CODE;
593 else if (ev.argument == "wrap")
594 code = InsetOld::WRAP_CODE;
598 code = InsetOld::ERT_CODE;
600 case LFUN_INSET_FOOTNOTE:
601 code = InsetOld::FOOT_CODE;
603 case LFUN_TABULAR_INSERT:
604 code = InsetOld::TABULAR_CODE;
606 case LFUN_INSET_MARGINAL:
607 code = InsetOld::MARGIN_CODE;
609 case LFUN_INSET_MINIPAGE:
610 code = InsetOld::MINIPAGE_CODE;
612 case LFUN_INSET_FLOAT:
613 case LFUN_INSET_WIDE_FLOAT:
614 code = InsetOld::FLOAT_CODE;
616 case LFUN_INSET_WRAP:
617 code = InsetOld::WRAP_CODE;
619 case LFUN_FLOAT_LIST:
620 code = InsetOld::FLOAT_LIST_CODE;
623 case LFUN_INSET_LIST:
624 code = InsetOld::LIST_CODE;
626 case LFUN_INSET_THEOREM:
627 code = InsetOld::THEOREM_CODE;
630 case LFUN_INSET_CAPTION:
631 code = InsetOld::CAPTION_CODE;
633 case LFUN_INSERT_NOTE:
634 code = InsetOld::NOTE_CODE;
636 case LFUN_INSERT_LABEL:
637 code = InsetOld::LABEL_CODE;
639 case LFUN_INSET_OPTARG:
640 code = InsetOld::OPTARG_CODE;
642 case LFUN_ENVIRONMENT_INSERT:
643 code = InsetOld::MINIPAGE_CODE;
645 case LFUN_INDEX_INSERT:
646 code = InsetOld::INDEX_CODE;
648 case LFUN_INDEX_PRINT:
649 code = InsetOld::INDEX_PRINT_CODE;
651 case LFUN_TOC_INSERT:
652 code = InsetOld::TOC_CODE;
656 code = InsetOld::URL_CODE;
659 // always allow this, since we will inset a raw quote
660 // if an inset is not allowed.
662 case LFUN_HYPHENATION:
663 case LFUN_LIGATURE_BREAK:
665 case LFUN_MENU_SEPARATOR:
667 case LFUN_END_OF_SENTENCE:
668 code = InsetOld::SPECIALCHAR_CODE;
670 case LFUN_SPACE_INSERT:
671 // slight hack: we know this is allowed in math mode
673 code = InsetOld::SPACE_CODE;
678 if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
684 // A few general toggles
686 case LFUN_TOOLTIPS_TOGGLE:
687 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
690 case LFUN_READ_ONLY_TOGGLE:
691 flag.setOnOff(buf->isReadonly());
694 flag.setOnOff(view()->getLyXText()->cursor.par()->params().startOfAppendix());
696 case LFUN_SWITCHBUFFER:
697 // toggle on the current buffer, but do not toggle off
698 // the other ones (is that a good idea?)
699 if (ev.argument == buf->fileName())
702 case LFUN_TRACK_CHANGES:
703 flag.setOnOff(buf->params.tracking_changes);
709 // the font related toggles
711 LyXFont const & font = view()->getLyXText()->real_current_font;
714 flag.setOnOff(font.emph() == LyXFont::ON);
717 flag.setOnOff(font.noun() == LyXFont::ON);
720 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
723 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
726 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
729 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
735 string tc = mathcursor->getLastCode();
738 flag.setOnOff(tc == "mathbf");
741 flag.setOnOff(tc == "mathsf");
744 flag.setOnOff(tc == "mathcal");
747 flag.setOnOff(tc == "mathrm");
750 flag.setOnOff(tc == "mathtt");
753 flag.setOnOff(tc == "mathbb");
756 flag.setOnOff(tc == "mathnormal");
763 // this one is difficult to get right. As a half-baked
764 // solution, we consider only the first action of the sequence
765 if (ev.action == LFUN_SEQUENCE) {
766 // argument contains ';'-terminated commands
767 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
774 void LyXFunc::dispatch(string const & s, bool verbose)
776 int const action = lyxaction.LookupFunc(s);
778 if (action == LFUN_UNKNOWN_ACTION) {
779 owner->message(bformat(_("Unknown function (%1$s)"), s));
783 dispatch(action, verbose);
787 void LyXFunc::dispatch(int ac, bool verbose)
789 dispatch(lyxaction.retrieveActionArg(ac), verbose);
793 bool ensureBufferClean(BufferView * bv) {
795 Buffer & buf = *bv->buffer();
799 string const file = MakeDisplayPath(buf.fileName(), 30);
800 string text = bformat(_("The document %1$s has unsaved "
801 "changes.\n\nDo you want to save "
802 "the document?"), file);
803 int const ret = Alert::prompt(_("Save changed document?"),
804 text, 0, 1, _("&Save"),
808 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
810 return buf.isClean();
816 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
818 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
819 <<"] arg[" << ev.argument << ']' << endl;
821 // we have not done anything wrong yet.
823 dispatch_buffer.erase();
825 #ifdef NEW_DISPATCHER
826 // We try do call the most specific dispatcher first:
827 // 1. the lockinginset's dispatch
828 // 2. the bufferview's dispatch
829 // 3. the lyxview's dispatch
832 selection_possible = false;
834 string argument = ev.argument;
835 kb_action action = ev.action;
837 // We cannot use this function here
838 if (getStatus(ev).disabled()) {
839 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
840 << lyxaction.getActionName(action)
841 << " [" << action << "] is disabled at this location"
843 setErrorMessage(getStatusMessage());
844 goto exit_with_message;
847 if (view()->available())
848 view()->hideCursor();
850 if (view()->available() && view()->theLockingInset()) {
851 InsetOld::RESULT result;
852 if (action > 1 || (action == LFUN_UNKNOWN_ACTION &&
855 UpdatableInset * inset = view()->theLockingInset();
859 inset->getCursorPos(view(), inset_x, dummy_y);
861 if ((action == LFUN_UNKNOWN_ACTION)
862 && argument.empty()) {
863 argument = encoded_last_key;
866 // the insets can't try to handle this,
867 // a table cell in the dummy position will
868 // lock its insettext, the insettext will
869 // pass it the bufferview, and succeed,
870 // so it will stay not locked. Not good
871 // if we've just done LFUN_ESCAPE (which
872 // injects an LFUN_PARAGRAPH_UPDATE)
873 if (action == LFUN_PARAGRAPH_UPDATE) {
874 view()->dispatch(ev);
875 goto exit_with_message;
878 // Undo/Redo is a bit tricky for insets.
879 if (action == LFUN_UNDO) {
881 goto exit_with_message;
882 } else if (action == LFUN_REDO) {
884 goto exit_with_message;
885 } else if (((result=inset->
886 // Hand-over to inset's own dispatch:
887 localDispatch(FuncRequest(view(), action, argument))) ==
889 (result == DISPATCHED_NOUPDATE))
890 goto exit_with_message;
891 // If UNDISPATCHED, just soldier on
892 else if (result == FINISHED) {
893 owner->clearMessage();
894 goto exit_with_message;
895 // We do not need special RTL handling here:
896 // FINISHED means that the cursor should be
897 // one position after the inset.
898 } else if (result == FINISHED_RIGHT) {
899 view()->text->cursorRight(view());
901 owner->clearMessage();
902 goto exit_with_message;
903 } else if (result == FINISHED_UP) {
904 RowList::iterator const irow = view()->text->cursorIRow();
905 if (irow != view()->text->firstRow()) {
907 view()->text->setCursorFromCoordinates(
908 view()->text->cursor.ix() + inset_x,
909 view()->text->cursor.iy() -
910 irow->baseline() - 1);
911 view()->text->cursor.x_fix(view()->text->cursor.x());
913 view()->text->cursorUp(view());
919 owner->clearMessage();
920 goto exit_with_message;
921 } else if (result == FINISHED_DOWN) {
922 RowList::iterator const irow = view()->text->cursorIRow();
923 if (irow != view()->text->lastRow()) {
925 view()->text->setCursorFromCoordinates(
926 view()->text->cursor.ix() + inset_x,
927 view()->text->cursor.iy() -
930 view()->text->cursor.x_fix(view()->text->cursor.x());
932 view()->text->cursorDown(view());
935 view()->text->cursorRight(view());
938 owner->clearMessage();
939 goto exit_with_message;
941 #warning I am not sure this is still right, please have a look! (Jug 20020417)
942 else { // result == UNDISPATCHED
943 //setMessage(N_("Text mode"));
945 case LFUN_UNKNOWN_ACTION:
946 case LFUN_BREAKPARAGRAPH:
948 view()->text->cursorRight(view());
949 view()->switchKeyMap();
950 owner->view_state_changed();
953 if (!view()->text->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
954 view()->text->cursorRight(view());
956 owner->view_state_changed();
958 goto exit_with_message;
960 if (view()->text->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
961 view()->text->cursorRight(view());
963 owner->view_state_changed();
965 goto exit_with_message;
967 if (view()->text->cursorRow() != view()->text->lastRow())
968 view()->text->cursorDown(view());
970 view()->text->cursorRight(view());
972 owner->view_state_changed();
973 goto exit_with_message;
984 if (!view()->available())
986 // this function should be used always [asierra060396]
987 UpdatableInset * tli = view()->theLockingInset();
989 UpdatableInset * lock = tli->getLockingInset();
992 view()->unlockInset(tli);
993 view()->text->cursorRight(view());
995 owner->view_state_changed();
997 tli->unlockInsetInInset(view(), lock, true);
1000 // Tell the paragraph dialog that we changed paragraph
1001 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1006 case LFUN_WORDFINDFORWARD:
1007 case LFUN_WORDFINDBACKWARD: {
1008 static string last_search;
1009 string searched_string;
1011 if (!argument.empty()) {
1012 last_search = argument;
1013 searched_string = argument;
1015 searched_string = last_search;
1017 bool fw = (action == LFUN_WORDFINDFORWARD);
1018 if (!searched_string.empty())
1019 lyx::find::find(view(), searched_string, fw);
1024 if (view()->available() && !view()->theLockingInset())
1026 owner->message(keyseq.printOptions());
1029 // --- Misc -------------------------------------------
1030 case LFUN_EXEC_COMMAND:
1031 owner->focus_command_buffer();
1036 meta_fake_bit = key_modifier::none;
1037 if (view()->available())
1038 // cancel any selection
1039 dispatch(LFUN_MARK_OFF);
1040 setMessage(N_("Cancel"));
1043 case LFUN_META_FAKE:
1044 meta_fake_bit = key_modifier::alt;
1045 setMessage(keyseq.print());
1048 case LFUN_READ_ONLY_TOGGLE:
1049 if (owner->buffer()->lyxvc.inUse())
1050 owner->buffer()->lyxvc.toggleReadOnly();
1052 owner->buffer()->setReadonly(
1053 !owner->buffer()->isReadonly());
1056 case LFUN_CENTER: // this is center and redraw.
1060 // --- Menus -----------------------------------------------
1062 menuNew(argument, false);
1065 case LFUN_MENUNEWTMPLT:
1066 menuNew(argument, true);
1069 case LFUN_CLOSEBUFFER:
1073 case LFUN_MENUWRITE:
1074 if (!owner->buffer()->isUnnamed()) {
1075 string const str = bformat(_("Saving document %1$s..."),
1076 MakeDisplayPath(owner->buffer()->fileName()));
1077 owner->message(str);
1078 MenuWrite(owner->buffer());
1079 owner->message(str + _(" done."));
1081 WriteAs(owner->buffer());
1085 WriteAs(owner->buffer(), argument);
1088 case LFUN_MENURELOAD: {
1089 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
1090 string text = bformat(_("Any changes will be lost. Are you sure "
1091 "you want to revert to the saved version of the document %1$s?"), file);
1092 int const ret = Alert::prompt(_("Revert to saved document?"),
1093 text, 0, 1, _("&Revert"), _("&Cancel"));
1101 Exporter::Export(owner->buffer(), argument, true);
1102 view()->showErrorList(BufferFormat(*owner->buffer()));
1106 Exporter::Preview(owner->buffer(), argument);
1107 view()->showErrorList(BufferFormat(*owner->buffer()));
1110 case LFUN_BUILDPROG:
1111 Exporter::Export(owner->buffer(), "program", true);
1112 view()->showErrorList(_("Build"));
1115 case LFUN_RUNCHKTEX:
1116 owner->buffer()->runChktex();
1117 view()->showErrorList(_("ChkTeX"));
1121 if (argument == "custom")
1122 owner->getDialogs().showSendto();
1124 Exporter::Export(owner->buffer(), argument, false);
1125 view()->showErrorList(BufferFormat(*owner->buffer()));
1139 InsetCommandParams p("tableofcontents");
1140 string const data = InsetCommandMailer::params2string("toc", p);
1141 owner->getDialogs().show("toc", data, 0);
1157 case LFUN_DEPTH_MIN:
1158 changeDepth(view(), view()->getLyXText(), DEC_DEPTH, false);
1159 owner->view_state_changed();
1162 case LFUN_DEPTH_PLUS:
1163 changeDepth(view(), view()->getLyXText(), INC_DEPTH, false);
1164 owner->view_state_changed();
1167 case LFUN_FREEFONT_APPLY:
1168 apply_freefont(view());
1171 case LFUN_FREEFONT_UPDATE:
1172 update_and_apply_freefont(view(), argument);
1175 case LFUN_RECONFIGURE:
1176 Reconfigure(view());
1180 case LFUN_FLOATSOPERATE:
1181 if (argument == "openfoot")
1182 view()->allFloats(1,0);
1183 else if (argument == "closefoot")
1184 view()->allFloats(0,0);
1185 else if (argument == "openfig")
1186 view()->allFloats(1,1);
1187 else if (argument == "closefig")
1188 view()->allFloats(0,1);
1191 #ifdef WITH_WARNINGS
1192 //#warning Find another implementation here (or another lyxfunc)!
1195 case LFUN_HELP_OPEN:
1197 string const arg = argument;
1199 setErrorMessage(N_("Missing argument"));
1202 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1203 if (fname.empty()) {
1204 lyxerr << "LyX: unable to find documentation file `"
1205 << arg << "'. Bad installation?" << endl;
1208 owner->message(bformat(_("Opening help file %1$s..."),
1209 MakeDisplayPath(fname)));
1210 view()->loadLyXFile(fname, false);
1214 // --- version control -------------------------------
1215 case LFUN_VC_REGISTER:
1217 if (!ensureBufferClean(view()))
1219 if (!owner->buffer()->lyxvc.inUse()) {
1220 owner->buffer()->lyxvc.registrer();
1226 case LFUN_VC_CHECKIN:
1228 if (!ensureBufferClean(view()))
1230 if (owner->buffer()->lyxvc.inUse()
1231 && !owner->buffer()->isReadonly()) {
1232 owner->buffer()->lyxvc.checkIn();
1238 case LFUN_VC_CHECKOUT:
1240 if (!ensureBufferClean(view()))
1242 if (owner->buffer()->lyxvc.inUse()
1243 && owner->buffer()->isReadonly()) {
1244 owner->buffer()->lyxvc.checkOut();
1250 case LFUN_VC_REVERT:
1252 owner->buffer()->lyxvc.revert();
1259 owner->buffer()->lyxvc.undoLast();
1264 // --- buffers ----------------------------------------
1266 case LFUN_SWITCHBUFFER:
1267 view()->buffer(bufferlist.getBuffer(argument));
1271 NewFile(view(), argument);
1274 case LFUN_FILE_OPEN:
1278 case LFUN_LAYOUT_TABULAR:
1279 if (view()->theLockingInset()) {
1280 if (view()->theLockingInset()->lyxCode()== InsetOld::TABULAR_CODE) {
1281 InsetTabular * inset = static_cast<InsetTabular *>
1282 (view()->theLockingInset());
1283 inset->openLayoutDialog(view());
1284 } else if (view()->theLockingInset()->
1285 getFirstLockingInsetOfType(InsetOld::TABULAR_CODE)!=0) {
1286 InsetTabular * inset = static_cast<InsetTabular *>(
1287 view()->theLockingInset()->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE));
1288 inset->openLayoutDialog(view());
1293 case LFUN_DROP_LAYOUTS_CHOICE:
1294 owner->getToolbar().openLayoutList();
1297 case LFUN_MENU_OPEN_BY_NAME:
1298 owner->getMenubar().openByName(argument);
1299 break; // RVDK_PATCH_5
1301 // --- lyxserver commands ----------------------------
1305 setMessage(owner->buffer()->fileName());
1306 lyxerr[Debug::INFO] << "FNAME["
1307 << owner->buffer()->fileName()
1313 dispatch_buffer = keyseq.print();
1314 lyxserver->notifyClient(dispatch_buffer);
1318 case LFUN_GOTOFILEROW:
1322 istringstream istr(argument.c_str());
1323 istr >> file_name >> row;
1324 // Must replace extension of the file to be .lyx and get full path
1325 string const s(ChangeExtension(file_name, ".lyx"));
1327 // Either change buffer or load the file
1328 if (bufferlist.exists(s)) {
1329 view()->buffer(bufferlist.getBuffer(s));
1331 view()->loadLyXFile(s);
1334 view()->setCursorFromRow(row);
1337 // see BufferView_pimpl::center()
1338 view()->updateScrollbar();
1342 case LFUN_GOTO_PARAGRAPH:
1344 istringstream istr(argument.c_str());
1348 ParIterator par = owner->buffer()->getParFromID(id);
1349 if (par == owner->buffer()->par_iterator_end()) {
1350 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1351 << id << ']' << endl;
1354 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1355 << " found." << endl;
1358 if (view()->theLockingInset())
1359 view()->unlockInset(view()->theLockingInset());
1360 if (par->inInset()) {
1361 FuncRequest cmd(view(), LFUN_INSET_EDIT, "left");
1362 par->inInset()->localDispatch(cmd);
1365 view()->getLyXText()->setCursor(par.pit(), 0);
1366 view()->switchKeyMap();
1367 owner->view_state_changed();
1370 // see BufferView_pimpl::center()
1371 view()->updateScrollbar();
1375 // --- insert characters ----------------------------------------
1377 // --- Mathed stuff. If we are here, there is no locked inset yet.
1378 case LFUN_MATH_EXTERN:
1379 case LFUN_MATH_NUMBER:
1380 case LFUN_MATH_NONUMBER:
1381 case LFUN_MATH_LIMITS:
1383 setErrorMessage(N_("This is only allowed in math mode!"));
1387 // passthrough hat and underscore outside mathed:
1388 case LFUN_SUBSCRIPT:
1389 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1391 case LFUN_SUPERSCRIPT:
1392 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1395 case LFUN_DIALOG_SHOW: {
1396 string const name = ev.getArg(0);
1397 string data = trim(ev.argument.substr(name.size()));
1399 if (name == "character") {
1400 data = freefont2string();
1402 owner->getDialogs().show("character", data);
1403 } else if (name == "document")
1404 owner->getDialogs().showDocument();
1405 else if (name == "findreplace")
1406 owner->getDialogs().showSearch();
1407 else if (name == "forks")
1408 owner->getDialogs().showForks();
1409 else if (name == "preamble")
1410 owner->getDialogs().showPreamble();
1411 else if (name == "preferences")
1412 owner->getDialogs().showPreferences();
1413 else if (name == "print")
1414 owner->getDialogs().showPrint();
1415 else if (name == "spellchecker")
1416 owner->getDialogs().showSpellchecker();
1418 owner->getDialogs().show(name, data);
1422 case LFUN_DIALOG_SHOW_NEW_INSET: {
1423 string const & name = argument;
1425 if (name == "bibitem" ||
1427 name == "include" ||
1432 InsetCommandParams p(name);
1433 data = InsetCommandMailer::params2string(name, p);
1434 } else if (name == "citation") {
1435 InsetCommandParams p("cite");
1436 data = InsetCommandMailer::params2string(name, p);
1438 owner->getDialogs().show(name, data, 0);
1442 case LFUN_DIALOG_SHOW_NEXT_INSET: {
1446 case LFUN_DIALOG_UPDATE: {
1447 string const & name = argument;
1448 // Can only update a dialog connected to an existing inset
1449 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1451 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1453 inset->localDispatch(fr);
1454 } else if (name == "paragraph") {
1455 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1460 case LFUN_DIALOG_HIDE:
1461 Dialogs::hide(argument, 0);
1464 case LFUN_DIALOG_DISCONNECT_INSET:
1465 owner->getDialogs().disconnect(argument);
1468 case LFUN_CHILDOPEN:
1470 string const filename =
1471 MakeAbsPath(argument,
1472 owner->buffer()->filePath());
1473 setMessage(N_("Opening child document ") +
1474 MakeDisplayPath(filename) + "...");
1475 view()->savePosition(0);
1476 if (bufferlist.exists(filename))
1477 view()->buffer(bufferlist.getBuffer(filename));
1479 view()->loadLyXFile(filename);
1483 case LFUN_TOGGLECURSORFOLLOW:
1484 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1488 owner->getIntl().KeyMapOn(false);
1491 case LFUN_KMAP_PRIM:
1492 owner->getIntl().KeyMapPrim();
1496 owner->getIntl().KeyMapSec();
1499 case LFUN_KMAP_TOGGLE:
1500 owner->getIntl().ToggleKeyMap();
1505 // argument contains ';'-terminated commands
1506 while (!argument.empty()) {
1508 argument = split(argument, first, ';');
1514 case LFUN_SAVEPREFERENCES:
1516 Path p(user_lyxdir());
1517 lyxrc.write("preferences");
1521 case LFUN_SCREEN_FONT_UPDATE:
1523 // handle the screen font changes.
1524 lyxrc.set_font_norm_type();
1525 lyx_gui::update_fonts();
1526 // We also need to empty the textcache so that
1527 // the buffer will be formatted correctly after
1530 // Of course we should only do the resize and the textcache.clear
1531 // if values really changed...but not very important right now. (Lgb)
1532 // All visible buffers will need resize
1538 case LFUN_SET_COLOR:
1541 string const x11_name = split(argument, lyx_name, ' ');
1542 if (lyx_name.empty() || x11_name.empty()) {
1543 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1548 bool const graphicsbg_changed =
1549 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1550 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1552 if (!lcolor.setColor(lyx_name, x11_name)) {
1554 bformat(_("Set-color \"%1$s\" failed "
1555 "- color is undefined or "
1556 "may not be redefined"), lyx_name));
1560 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1562 if (graphicsbg_changed) {
1563 #ifdef WITH_WARNINGS
1564 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1567 lyx::graphics::GCache & gc =
1568 lyx::graphics::GCache::get();
1569 gc.changeDisplay(true);
1578 owner->message(argument);
1581 case LFUN_FORKS_KILL:
1583 if (!isStrInt(argument))
1586 pid_t const pid = strToInt(argument);
1587 ForkedcallsController & fcc = ForkedcallsController::get();
1592 case LFUN_TOOLTIPS_TOGGLE:
1593 owner->getDialogs().toggleTooltips();
1596 case LFUN_EXTERNAL_EDIT: {
1598 .localDispatch(FuncRequest(view(), action, argument));
1603 // Then if it was none of the above
1604 // Trying the BufferView::pimpl dispatch:
1605 if (!view()->dispatch(ev))
1606 lyxerr << "A truly unknown func ["
1607 << lyxaction.getActionName(ev.action) << "]!"
1614 view()->owner()->updateLayoutChoice();
1616 if (view()->available()) {
1617 view()->fitCursor();
1619 // If we executed a mutating lfun, mark the buffer as dirty
1620 if (!getStatus(ev).disabled()
1621 && !lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)
1622 && !lyxaction.funcHasFlag(ev.action, LyXAction::ReadOnly))
1623 view()->buffer()->markDirty();
1626 sendDispatchMessage(getMessage(), ev, verbose);
1630 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1632 owner->updateMenubar();
1633 owner->updateToolbar();
1635 if (ev.action == LFUN_SELFINSERT || !verbose) {
1636 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1638 owner->message(msg);
1642 string dispatch_msg = msg;
1643 if (!dispatch_msg.empty())
1644 dispatch_msg += ' ';
1646 string comname = lyxaction.getActionName(ev.action);
1648 int pseudoaction = ev.action;
1649 bool argsadded = false;
1651 if (!ev.argument.empty()) {
1652 // the pseudoaction is useful for the bindings
1653 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1655 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1656 pseudoaction = ev.action;
1658 comname += ' ' + ev.argument;
1663 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1665 if (!shortcuts.empty()) {
1666 comname += ": " + shortcuts;
1667 } else if (!argsadded && !ev.argument.empty()) {
1668 comname += ' ' + ev.argument;
1671 if (!comname.empty()) {
1672 comname = rtrim(comname);
1673 dispatch_msg += '(' + comname + ')';
1676 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1677 if (!dispatch_msg.empty())
1678 owner->message(dispatch_msg);
1682 void LyXFunc::setupLocalKeymap()
1684 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1685 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1689 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1691 string initpath = lyxrc.document_path;
1692 string filename(name);
1694 if (view()->available()) {
1695 string const trypath = owner->buffer()->filePath();
1696 // If directory is writeable, use this as default.
1697 if (IsDirWriteable(trypath))
1701 static int newfile_number;
1703 if (filename.empty()) {
1704 filename = AddName(lyxrc.document_path,
1705 "newfile" + tostr(++newfile_number) + ".lyx");
1706 FileInfo fi(filename);
1707 while (bufferlist.exists(filename) || fi.readable()) {
1709 filename = AddName(lyxrc.document_path,
1710 "newfile" + tostr(newfile_number) +
1712 fi.newFile(filename);
1716 // The template stuff
1719 FileDialog fileDlg(_("Select template file"),
1720 LFUN_SELECT_FILE_SYNC,
1721 make_pair(string(_("Documents|#o#O")),
1722 string(lyxrc.document_path)),
1723 make_pair(string(_("Templates|#T#t")),
1724 string(lyxrc.template_path)));
1726 FileDialog::Result result =
1727 fileDlg.open(lyxrc.template_path,
1728 _("*.lyx| LyX Documents (*.lyx)"));
1730 if (result.first == FileDialog::Later)
1733 string const fname = result.second;
1740 view()->newFile(filename, templname, !name.empty());
1744 void LyXFunc::open(string const & fname)
1746 string initpath = lyxrc.document_path;
1748 if (view()->available()) {
1749 string const trypath = owner->buffer()->filePath();
1750 // If directory is writeable, use this as default.
1751 if (IsDirWriteable(trypath))
1757 if (fname.empty()) {
1758 FileDialog fileDlg(_("Select document to open"),
1760 make_pair(string(_("Documents|#o#O")),
1761 string(lyxrc.document_path)),
1762 make_pair(string(_("Examples|#E#e")),
1763 string(AddPath(system_lyxdir(), "examples"))));
1765 FileDialog::Result result =
1766 fileDlg.open(initpath,
1767 _("*.lyx| LyX Documents (*.lyx)"));
1769 if (result.first == FileDialog::Later)
1772 filename = result.second;
1774 // check selected filename
1775 if (filename.empty()) {
1776 owner->message(_("Canceled."));
1782 // get absolute path of file and add ".lyx" to the filename if
1784 string const fullpath = FileSearch(string(), filename, "lyx");
1785 if (!fullpath.empty()) {
1786 filename = fullpath;
1789 string const disp_fn(MakeDisplayPath(filename));
1791 // if the file doesn't exist, let the user create one
1792 FileInfo const f(filename, true);
1794 // the user specifically chose this name. Believe them.
1795 view()->newFile(filename, "", true);
1799 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1802 if (view()->loadLyXFile(filename)) {
1803 str2 = bformat(_("Document %1$s opened."), disp_fn);
1805 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1807 owner->message(str2);
1811 void LyXFunc::doImport(string const & argument)
1814 string filename = split(argument, format, ' ');
1816 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1817 << " file: " << filename << endl;
1819 // need user interaction
1820 if (filename.empty()) {
1821 string initpath = lyxrc.document_path;
1823 if (view()->available()) {
1824 string const trypath = owner->buffer()->filePath();
1825 // If directory is writeable, use this as default.
1826 if (IsDirWriteable(trypath))
1830 string const text = bformat(_("Select %1$s file to import"),
1831 formats.prettyName(format));
1833 FileDialog fileDlg(text,
1835 make_pair(string(_("Documents|#o#O")),
1836 string(lyxrc.document_path)),
1837 make_pair(string(_("Examples|#E#e")),
1838 string(AddPath(system_lyxdir(), "examples"))));
1840 string const extension = "*." + formats.extension(format)
1841 + "| " + formats.prettyName(format)
1842 + " (*." + formats.extension(format) + ')';
1844 FileDialog::Result result = fileDlg.open(initpath,
1847 if (result.first == FileDialog::Later)
1850 filename = result.second;
1852 // check selected filename
1853 if (filename.empty())
1854 owner->message(_("Canceled."));
1857 if (filename.empty())
1860 // get absolute path of file
1861 filename = MakeAbsPath(filename);
1863 string const lyxfile = ChangeExtension(filename, ".lyx");
1865 // Check if the document already is open
1866 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1867 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1868 owner->message(_("Canceled."));
1873 // if the file exists already, and we didn't do
1874 // -i lyx thefile.lyx, warn
1875 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1876 string const file = MakeDisplayPath(lyxfile, 30);
1878 string text = bformat(_("The document %1$s already exists.\n\n"
1879 "Do you want to over-write that document?"), file);
1880 int const ret = Alert::prompt(_("Over-write document?"),
1881 text, 0, 1, _("&Over-write"), _("&Cancel"));
1884 owner->message(_("Canceled."));
1889 Importer::Import(owner, filename, format);
1893 void LyXFunc::closeBuffer()
1895 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1896 if (bufferlist.empty()) {
1897 // need this otherwise SEGV may occur while
1898 // trying to set variables that don't exist
1899 // since there's no current buffer
1900 owner->getDialogs().hideBufferDependent();
1902 view()->buffer(bufferlist.first());
1908 // Each "owner" should have it's own message method. lyxview and
1909 // the minibuffer would use the minibuffer, but lyxserver would
1910 // send an ERROR signal to its client. Alejandro 970603
1911 // This func is bit problematic when it comes to NLS, to make the
1912 // lyx servers client be language indepenent we must not translate
1913 // strings sent to this func.
1914 void LyXFunc::setErrorMessage(string const & m) const
1916 dispatch_buffer = m;
1921 void LyXFunc::setMessage(string const & m) const
1923 dispatch_buffer = m;
1927 void LyXFunc::setStatusMessage(string const & m) const
1933 string const LyXFunc::view_status_message()
1935 // When meta-fake key is pressed, show the key sequence so far + "M-".
1937 return keyseq.print() + "M-";
1940 // Else, when a non-complete key sequence is pressed,
1941 // show the available options.
1942 if (keyseq.length() > 0 && !keyseq.deleted()) {
1943 return keyseq.printOptions();
1946 if (!view()->available())
1947 return _("Welcome to LyX!");
1949 return currentState(view());
1953 BufferView * LyXFunc::view() const
1956 return owner->view().get();
1960 bool LyXFunc::wasMetaKey() const
1962 return (meta_fake_bit != key_modifier::none);