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/lyxfunctional.h"
87 using std::back_inserter;
88 using namespace bv_funcs;
90 extern BufferList bufferlist;
91 extern LyXServer * lyxserver;
92 extern bool selection_possible;
94 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
97 extern tex_accent_struct get_accent(kb_action action);
99 extern void ShowLatexLog();
102 LyXFunc::LyXFunc(LyXView * o)
105 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
106 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
107 meta_fake_bit(key_modifier::none)
113 LyXText * LyXFunc::TEXT(bool flag = true) const
117 return view()->getLyXText();
122 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
124 if (selecting || TEXT(flag)->selection.mark()) {
125 TEXT(flag)->setSelection();
126 if (!TEXT(flag)->isInInset())
127 view()->toggleToggle();
129 view()->update(TEXT(flag), BufferView::SELECT);
131 view()->switchKeyMap();
135 void LyXFunc::handleKeyFunc(kb_action action)
137 char c = encoded_last_key;
139 if (keyseq.length()) {
143 owner->getIntl().getTransManager()
144 .deadkey(c, get_accent(action).accent, TEXT(false));
145 // Need to clear, in case the minibuffer calls these
148 // copied verbatim from do_accent_char
149 view()->update(TEXT(false), BufferView::SELECT);
150 TEXT(false)->selection.cursor = TEXT(false)->cursor;
154 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
155 key_modifier::state state)
159 if (lyxerr.debugging(Debug::KEY)) {
160 lyxerr << "KeySym is "
161 << keysym->getSymbolName()
165 // Do nothing if we have nothing (JMarc)
166 if (!keysym->isOK()) {
167 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
172 if (keysym->isModifier()) {
173 lyxerr[Debug::KEY] << "isModifier true" << endl;
177 Encoding const * encoding = view()->getEncoding();
179 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
181 // Do a one-deep top-level lookup for
182 // cancel and meta-fake keys. RVDK_PATCH_5
183 cancel_meta_seq.reset();
185 int action = cancel_meta_seq.addkey(keysym, state);
186 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
188 // When not cancel or meta-fake, do the normal lookup.
189 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
190 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
191 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
192 // remove Caps Lock and Mod2 as a modifiers
193 action = keyseq.addkey(keysym, (state | meta_fake_bit));
194 lyxerr[Debug::KEY] << "action now set to ["
195 << action << ']' << endl;
198 // Dont remove this unless you know what you are doing.
199 meta_fake_bit = key_modifier::none;
201 // can this happen now ?
202 if (action == LFUN_NOACTION) {
203 action = LFUN_PREFIX;
206 if (lyxerr.debugging(Debug::KEY)) {
207 lyxerr << "Key [action="
209 << keyseq.print() << ']'
213 // already here we know if it any point in going further
214 // why not return already here if action == -1 and
215 // num_bytes == 0? (Lgb)
217 if (keyseq.length() > 1) {
218 owner->message(keyseq.print());
222 // Maybe user can only reach the key via holding down shift.
223 // Let's see. But only if shift is the only modifier
224 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
225 lyxerr[Debug::KEY] << "Trying without shift" << endl;
226 action = keyseq.addkey(keysym, key_modifier::none);
227 lyxerr[Debug::KEY] << "Action now " << action << endl;
230 if (action == LFUN_UNKNOWN_ACTION) {
231 // Hmm, we didn't match any of the keysequences. See
232 // if it's normal insertable text not already covered
234 if (keysym->isText() && keyseq.length() == 1) {
235 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
236 action = LFUN_SELFINSERT;
238 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
239 owner->message(_("Unknown function."));
244 if (action == LFUN_SELFINSERT) {
245 if (encoded_last_key != 0) {
247 arg += encoded_last_key;
249 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
251 lyxerr[Debug::KEY] << "SelfInsert arg[`"
252 << argument << "']" << endl;
260 FuncStatus LyXFunc::getStatus(int ac) const
262 return getStatus(lyxaction.retrieveActionArg(ac));
266 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
269 Buffer * buf = owner->buffer();
271 if (ev.action == LFUN_NOACTION) {
272 setStatusMessage(N_("Nothing to do"));
273 return flag.disabled(true);
277 case LFUN_UNKNOWN_ACTION:
278 #ifndef HAVE_LIBAIKSAURUS
279 case LFUN_THESAURUS_ENTRY:
284 flag |= lyx_gui::getStatus(ev);
287 if (flag.unknown()) {
288 setStatusMessage(N_("Unknown action"));
292 // the default error message if we disable the command
293 setStatusMessage(N_("Command disabled"));
295 // Check whether we need a buffer
296 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
297 // Yes we need a buffer, do we have one?
300 // Can we use a readonly buffer?
301 if (buf->isReadonly() &&
302 !lyxaction.funcHasFlag(ev.action,
303 LyXAction::ReadOnly)) {
305 setStatusMessage(N_("Document is read-only"));
310 setStatusMessage(N_("Command not allowed with"
311 "out any document open"));
312 return flag.disabled(true);
316 UpdatableInset * tli = view()->theLockingInset();
318 // I would really like to avoid having this switch and rather try to
319 // encode this in the function itself.
320 bool disable = false;
323 disable = ev.argument != "custom"
324 && !Exporter::IsExportable(buf, ev.argument);
327 disable = buf->undostack.empty();
330 disable = buf->redostack.empty();
335 UpdatableInset * in = tli;
336 if (in->lyxCode() != Inset::TABULAR_CODE) {
337 in = tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
339 if (in && static_cast<InsetTabular*>(in)->hasSelection()) {
344 disable = !mathcursor && !view()->getLyXText()->selection.set();
347 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
350 disable = !Exporter::IsExportable(buf, "program");
353 case LFUN_LAYOUT_TABULAR:
355 || (tli->lyxCode() != Inset::TABULAR_CODE
356 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
360 disable = !changeDepth(view(), TEXT(false), DEC_DEPTH, true);
363 case LFUN_DEPTH_PLUS:
364 disable = !changeDepth(view(), TEXT(false), INC_DEPTH, true);
368 case LFUN_LAYOUT_PARAGRAPH: {
369 Inset * inset = TEXT(false)->cursor.par()->inInset();
370 disable = inset && inset->forceDefaultParagraphs(inset);
374 case LFUN_INSET_OPTARG:
375 disable = (TEXT(false)->cursor.par()->layout()->optionalargs == 0);
378 case LFUN_TABULAR_FEATURE:
381 // FIXME: check temporarily disabled
383 char align = mathcursor->valign();
388 if (ev.argument.empty()) {
392 if (!contains("tcb", ev.argument[0])) {
396 flag.setOnOff(ev.argument[0] == align);
400 char align = mathcursor->halign();
405 if (ev.argument.empty()) {
409 if (!contains("lcr", ev.argument[0])) {
413 flag.setOnOff(ev.argument[0] == align);
416 disable = !mathcursor->halign();
422 //ret.disabled(true);
423 if (tli->lyxCode() == Inset::TABULAR_CODE) {
424 ret = static_cast<InsetTabular *>(tli)
425 ->getStatus(ev.argument);
428 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
429 ret = static_cast<InsetTabular *>
430 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
431 ->getStatus(ev.argument);
438 static InsetTabular inset(*owner->buffer(), 1, 1);
442 ret = inset.getStatus(ev.argument);
443 if (ret.onoff(true) || ret.onoff(false))
444 flag.setOnOff(false);
448 case LFUN_VC_REGISTER:
449 disable = buf->lyxvc.inUse();
451 case LFUN_VC_CHECKIN:
452 disable = !buf->lyxvc.inUse() || buf->isReadonly();
454 case LFUN_VC_CHECKOUT:
455 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
459 disable = !buf->lyxvc.inUse();
461 case LFUN_MENURELOAD:
462 disable = buf->isUnnamed() || buf->isClean();
464 case LFUN_BOOKMARK_GOTO:
466 isSavedPosition(strToUnsignedInt(ev.argument));
468 case LFUN_MERGE_CHANGES:
469 case LFUN_ACCEPT_CHANGE:
470 case LFUN_REJECT_CHANGE:
471 case LFUN_ACCEPT_ALL_CHANGES:
472 case LFUN_REJECT_ALL_CHANGES:
473 disable = !buf->params.tracking_changes;
475 case LFUN_INSET_TOGGLE: {
476 LyXText * lt = view()->getLyXText();
477 disable = !(isEditableInset(lt->getInset())
479 && lt->inset_owner->owner()
480 && lt->inset_owner->owner()->isOpen()));
484 case LFUN_INSET_SETTINGS: {
486 UpdatableInset * inset = view()->theLockingInset();
491 // get the innermost inset
492 inset = inset->getLockingInset();
494 // jump back to owner if an InsetText, so
495 // we get back to the InsetTabular or whatever
496 if (inset->lyxCode() == Inset::TEXT_CODE)
497 inset = inset->owner();
499 Inset::Code code = inset->lyxCode();
501 case Inset::TABULAR_CODE:
502 disable = ev.argument != "tabular";
504 case Inset::ERT_CODE:
505 disable = ev.argument != "ert";
507 case Inset::FLOAT_CODE:
508 disable = ev.argument != "float";
510 case Inset::MINIPAGE_CODE:
511 disable = ev.argument != "minipage";
513 case Inset::WRAP_CODE:
514 disable = ev.argument != "wrap";
522 case LFUN_MATH_MUTATE:
524 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
525 flag.setOnOff(false);
530 // we just need to be in math mode to enable that
532 case LFUN_MATH_SPACE:
533 case LFUN_MATH_LIMITS:
534 case LFUN_MATH_NONUMBER:
535 case LFUN_MATH_NUMBER:
536 case LFUN_MATH_EXTERN:
537 disable = !mathcursor;
540 case LFUN_DIALOG_SHOW: {
541 string const name = ev.getArg(0);
543 disable = !(name == "aboutlyx" ||
546 name == "preferences" ||
551 if (name == "print") {
552 disable = !Exporter::IsExportable(buf, "dvi") ||
553 lyxrc.print_command == "none";
554 } else if (name == "character") {
555 UpdatableInset * tli = view()->theLockingInset();
556 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
557 } else if (name == "vclog") {
558 disable = !buf->lyxvc.inUse();
559 } else if (name == "latexlog") {
560 disable = !IsFileReadable(buf->getLogName().second);
569 // the functions which insert insets
570 Inset::Code code = Inset::NO_CODE;
572 case LFUN_DIALOG_SHOW_NEW_INSET:
573 if (ev.argument == "bibitem")
574 code = Inset::BIBITEM_CODE;
575 else if (ev.argument == "bibtex")
576 code = Inset::BIBTEX_CODE;
577 else if (ev.argument == "citation")
578 code = Inset::CITE_CODE;
579 else if (ev.argument == "ert")
580 code = Inset::ERT_CODE;
581 else if (ev.argument == "external")
582 code = Inset::EXTERNAL_CODE;
583 else if (ev.argument == "float")
584 code = Inset::FLOAT_CODE;
585 else if (ev.argument == "graphics")
586 code = Inset::GRAPHICS_CODE;
587 else if (ev.argument == "include")
588 code = Inset::INCLUDE_CODE;
589 else if (ev.argument == "index")
590 code = Inset::INDEX_CODE;
591 else if (ev.argument == "label")
592 code = Inset::LABEL_CODE;
593 else if (ev.argument == "minipage")
594 code = Inset::MINIPAGE_CODE;
595 else if (ev.argument == "ref")
596 code = Inset::REF_CODE;
597 else if (ev.argument == "toc")
598 code = Inset::TOC_CODE;
599 else if (ev.argument == "url")
600 code = Inset::URL_CODE;
601 else if (ev.argument == "wrap")
602 code = Inset::WRAP_CODE;
606 code = Inset::ERT_CODE;
608 case LFUN_INSET_FOOTNOTE:
609 code = Inset::FOOT_CODE;
611 case LFUN_TABULAR_INSERT:
612 code = Inset::TABULAR_CODE;
614 case LFUN_INSET_MARGINAL:
615 code = Inset::MARGIN_CODE;
617 case LFUN_INSET_MINIPAGE:
618 code = Inset::MINIPAGE_CODE;
620 case LFUN_INSET_FLOAT:
621 case LFUN_INSET_WIDE_FLOAT:
622 code = Inset::FLOAT_CODE;
624 case LFUN_INSET_WRAP:
625 code = Inset::WRAP_CODE;
627 case LFUN_FLOAT_LIST:
628 code = Inset::FLOAT_LIST_CODE;
631 case LFUN_INSET_LIST:
632 code = Inset::LIST_CODE;
634 case LFUN_INSET_THEOREM:
635 code = Inset::THEOREM_CODE;
638 case LFUN_INSET_CAPTION:
639 code = Inset::CAPTION_CODE;
641 case LFUN_INSERT_NOTE:
642 code = Inset::NOTE_CODE;
644 case LFUN_INSERT_LABEL:
645 code = Inset::LABEL_CODE;
647 case LFUN_INSET_OPTARG:
648 code = Inset::OPTARG_CODE;
650 case LFUN_ENVIRONMENT_INSERT:
651 code = Inset::MINIPAGE_CODE;
653 case LFUN_INDEX_INSERT:
654 code = Inset::INDEX_CODE;
656 case LFUN_INDEX_PRINT:
657 code = Inset::INDEX_PRINT_CODE;
659 case LFUN_TOC_INSERT:
660 code = Inset::TOC_CODE;
664 code = Inset::URL_CODE;
667 // always allow this, since we will inset a raw quote
668 // if an inset is not allowed.
670 case LFUN_HYPHENATION:
671 case LFUN_LIGATURE_BREAK:
673 case LFUN_MENU_SEPARATOR:
675 case LFUN_END_OF_SENTENCE:
676 code = Inset::SPECIALCHAR_CODE;
678 case LFUN_SPACE_INSERT:
679 // slight hack: we know this is allowed in math mode
681 code = Inset::SPACE_CODE;
686 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
692 // A few general toggles
694 case LFUN_TOOLTIPS_TOGGLE:
695 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
698 case LFUN_READ_ONLY_TOGGLE:
699 flag.setOnOff(buf->isReadonly());
702 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
704 case LFUN_SWITCHBUFFER:
705 // toggle on the current buffer, but do not toggle off
706 // the other ones (is that a good idea?)
707 if (ev.argument == buf->fileName())
710 case LFUN_TRACK_CHANGES:
711 flag.setOnOff(buf->params.tracking_changes);
717 // the font related toggles
719 LyXFont const & font = TEXT(false)->real_current_font;
722 flag.setOnOff(font.emph() == LyXFont::ON);
725 flag.setOnOff(font.noun() == LyXFont::ON);
728 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
731 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
734 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
737 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
743 string tc = mathcursor->getLastCode();
746 flag.setOnOff(tc == "mathbf");
749 flag.setOnOff(tc == "mathsf");
752 flag.setOnOff(tc == "mathcal");
755 flag.setOnOff(tc == "mathrm");
758 flag.setOnOff(tc == "mathtt");
761 flag.setOnOff(tc == "mathbb");
764 flag.setOnOff(tc == "mathnormal");
771 // this one is difficult to get right. As a half-baked
772 // solution, we consider only the first action of the sequence
773 if (ev.action == LFUN_SEQUENCE) {
774 // argument contains ';'-terminated commands
775 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
782 void LyXFunc::dispatch(string const & s, bool verbose)
784 int const action = lyxaction.LookupFunc(s);
786 if (action == LFUN_UNKNOWN_ACTION) {
787 owner->message(bformat(_("Unknown function (%1$s)"), s));
791 dispatch(action, verbose);
795 void LyXFunc::dispatch(int ac, bool verbose)
797 dispatch(lyxaction.retrieveActionArg(ac), verbose);
802 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
804 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
805 <<"] arg[" << ev.argument << ']' << endl;
807 // we have not done anything wrong yet.
809 dispatch_buffer.erase();
811 #ifdef NEW_DISPATCHER
812 // We try do call the most specific dispatcher first:
813 // 1. the lockinginset's dispatch
814 // 2. the bufferview's dispatch
815 // 3. the lyxview's dispatch
818 selection_possible = false;
820 string argument = ev.argument;
821 kb_action action = ev.action;
823 // We cannot use this function here
824 if (getStatus(ev).disabled()) {
825 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
826 << lyxaction.getActionName(action)
827 << " [" << action << "] is disabled at this location"
829 setErrorMessage(getStatusMessage());
830 goto exit_with_message;
833 if (view()->available())
834 view()->hideCursor();
836 if (view()->available() && view()->theLockingInset()) {
837 Inset::RESULT result;
838 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
839 (!keyseq.deleted())))
841 UpdatableInset * inset = view()->theLockingInset();
845 inset->getCursorPos(view(), inset_x, dummy_y);
847 if ((action == LFUN_UNKNOWN_ACTION)
848 && argument.empty()) {
849 argument = encoded_last_key;
852 // the insets can't try to handle this,
853 // a table cell in the dummy position will
854 // lock its insettext, the insettext will
855 // pass it the bufferview, and succeed,
856 // so it will stay not locked. Not good
857 // if we've just done LFUN_ESCAPE (which
858 // injects an LFUN_PARAGRAPH_UPDATE)
859 if (action == LFUN_PARAGRAPH_UPDATE) {
860 view()->dispatch(ev);
861 goto exit_with_message;
864 // Undo/Redo is a bit tricky for insets.
865 if (action == LFUN_UNDO) {
867 goto exit_with_message;
868 } else if (action == LFUN_REDO) {
870 goto exit_with_message;
871 } else if (((result=inset->
872 // Hand-over to inset's own dispatch:
873 localDispatch(FuncRequest(view(), action, argument))) ==
875 (result == DISPATCHED_NOUPDATE))
876 goto exit_with_message;
877 // If UNDISPATCHED, just soldier on
878 else if (result == FINISHED) {
879 owner->clearMessage();
880 goto exit_with_message;
881 // We do not need special RTL handling here:
882 // FINISHED means that the cursor should be
883 // one position after the inset.
884 } else if (result == FINISHED_RIGHT) {
885 TEXT()->cursorRight(view());
886 moveCursorUpdate(true, false);
887 owner->clearMessage();
888 goto exit_with_message;
889 } else if (result == FINISHED_UP) {
890 if (TEXT()->cursor.irow() != TEXT()->rows().begin()) {
892 TEXT()->setCursorFromCoordinates(
893 TEXT()->cursor.ix() + inset_x,
894 TEXT()->cursor.iy() -
895 TEXT()->cursor.irow()->baseline() - 1);
896 TEXT()->cursor.x_fix(TEXT()->cursor.x());
898 TEXT()->cursorUp(view());
900 moveCursorUpdate(true, false);
902 view()->update(TEXT(), BufferView::SELECT);
904 owner->clearMessage();
905 goto exit_with_message;
906 } else if (result == FINISHED_DOWN) {
907 if (boost::next(TEXT()->cursor.irow()) != TEXT()->rows().end()) {
909 TEXT()->setCursorFromCoordinates(
910 TEXT()->cursor.ix() + inset_x,
911 TEXT()->cursor.iy() -
912 TEXT()->cursor.irow()->baseline() +
913 TEXT()->cursor.irow()->height() + 1);
914 TEXT()->cursor.x_fix(TEXT()->cursor.x());
916 TEXT()->cursorDown(view());
919 TEXT()->cursorRight(view());
921 moveCursorUpdate(true, false);
922 owner->clearMessage();
923 goto exit_with_message;
925 #warning I am not sure this is still right, please have a look! (Jug 20020417)
926 else { // result == UNDISPATCHED
927 //setMessage(N_("Text mode"));
929 case LFUN_UNKNOWN_ACTION:
930 case LFUN_BREAKPARAGRAPH:
932 TEXT()->cursorRight(view());
933 view()->switchKeyMap();
934 owner->view_state_changed();
937 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
938 TEXT()->cursorRight(view());
939 moveCursorUpdate(true, false);
940 owner->view_state_changed();
942 goto exit_with_message;
944 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
945 TEXT()->cursorRight(view());
946 moveCursorUpdate(true, false);
947 owner->view_state_changed();
949 goto exit_with_message;
951 if (boost::next(TEXT()->cursor.row()) != TEXT()->rows().end())
952 TEXT()->cursorDown(view());
954 TEXT()->cursorRight(view());
955 moveCursorUpdate(true, false);
956 owner->view_state_changed();
957 goto exit_with_message;
969 if (!view()->available()) break;
970 // this function should be used always [asierra060396]
971 UpdatableInset * tli =
972 view()->theLockingInset();
974 UpdatableInset * lock = tli->getLockingInset();
977 view()->unlockInset(tli);
978 TEXT()->cursorRight(view());
979 moveCursorUpdate(true, false);
980 owner->view_state_changed();
982 tli->unlockInsetInInset(view(),
987 // Tell the paragraph dialog that we changed paragraph
988 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
993 // --- Misc -------------------------------------------
994 case LFUN_WORDFINDFORWARD :
995 case LFUN_WORDFINDBACKWARD : {
996 static string last_search;
997 string searched_string;
999 if (!argument.empty()) {
1000 last_search = argument;
1001 searched_string = argument;
1003 searched_string = last_search;
1005 bool fw = (action == LFUN_WORDFINDFORWARD);
1006 if (!searched_string.empty()) {
1007 lyxfind::LyXFind(view(), searched_string, fw);
1014 if (view()->available() && !view()->theLockingInset()) {
1015 view()->update(TEXT(), BufferView::SELECT);
1017 owner->message(keyseq.printOptions());
1021 // --- Misc -------------------------------------------
1022 case LFUN_EXEC_COMMAND:
1023 owner->focus_command_buffer();
1026 case LFUN_CANCEL: // RVDK_PATCH_5
1028 meta_fake_bit = key_modifier::none;
1029 if (view()->available())
1030 // cancel any selection
1031 dispatch(LFUN_MARK_OFF);
1032 setMessage(N_("Cancel"));
1035 case LFUN_META_FAKE: // RVDK_PATCH_5
1037 meta_fake_bit = key_modifier::alt;
1038 setMessage(keyseq.print());
1042 case LFUN_READ_ONLY_TOGGLE:
1043 if (owner->buffer()->lyxvc.inUse()) {
1044 owner->buffer()->lyxvc.toggleReadOnly();
1046 owner->buffer()->setReadonly(
1047 !owner->buffer()->isReadonly());
1051 case LFUN_CENTER: // this is center and redraw.
1055 // --- Menus -----------------------------------------------
1057 menuNew(argument, false);
1060 case LFUN_MENUNEWTMPLT:
1061 menuNew(argument, true);
1064 case LFUN_CLOSEBUFFER:
1068 case LFUN_MENUWRITE:
1069 if (!owner->buffer()->isUnnamed()) {
1070 string const str = bformat(_("Saving document %1$s..."),
1071 MakeDisplayPath(owner->buffer()->fileName()));
1072 owner->message(str);
1073 MenuWrite(owner->buffer());
1074 owner->message(str + _(" done."));
1076 WriteAs(owner->buffer());
1080 WriteAs(owner->buffer(), argument);
1083 case LFUN_MENURELOAD: {
1084 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
1085 string text = bformat(_("Any changes will be lost. Are you sure "
1086 "you want to revert to the saved version of the document %1$s?"), file);
1087 int const ret = Alert::prompt(_("Revert to saved document?"),
1088 text, 0, 1, _("&Revert"), _("&Cancel"));
1096 Exporter::Export(owner->buffer(), argument, true);
1097 view()->showErrorList(BufferFormat(*owner->buffer()));
1101 Exporter::Preview(owner->buffer(), argument);
1102 view()->showErrorList(BufferFormat(*owner->buffer()));
1105 case LFUN_BUILDPROG:
1106 Exporter::Export(owner->buffer(), "program", true);
1107 view()->showErrorList(_("Build"));
1110 case LFUN_RUNCHKTEX:
1111 owner->buffer()->runChktex();
1112 view()->showErrorList(_("ChkTeX"));
1116 if (argument == "custom")
1117 owner->getDialogs().showSendto();
1119 Exporter::Export(owner->buffer(), argument, false);
1120 view()->showErrorList(BufferFormat(*owner->buffer()));
1134 InsetCommandParams p("tableofcontents");
1135 string const data = InsetCommandMailer::params2string("toc", p);
1136 owner->getDialogs().show("toc", data, 0);
1152 case LFUN_DEPTH_MIN:
1153 changeDepth(view(), TEXT(false), DEC_DEPTH, false);
1154 owner->view_state_changed();
1157 case LFUN_DEPTH_PLUS:
1158 changeDepth(view(), TEXT(false), INC_DEPTH, false);
1159 owner->view_state_changed();
1162 case LFUN_FREEFONT_APPLY:
1163 apply_freefont(view());
1166 case LFUN_FREEFONT_UPDATE:
1167 update_and_apply_freefont(view(), argument);
1170 case LFUN_RECONFIGURE:
1171 Reconfigure(view());
1175 case LFUN_FLOATSOPERATE:
1176 if (argument == "openfoot")
1177 view()->allFloats(1,0);
1178 else if (argument == "closefoot")
1179 view()->allFloats(0,0);
1180 else if (argument == "openfig")
1181 view()->allFloats(1,1);
1182 else if (argument == "closefig")
1183 view()->allFloats(0,1);
1186 #ifdef WITH_WARNINGS
1187 //#warning Find another implementation here (or another lyxfunc)!
1190 case LFUN_HELP_OPEN:
1192 string const arg = argument;
1194 setErrorMessage(N_("Missing argument"));
1197 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1198 if (fname.empty()) {
1199 lyxerr << "LyX: unable to find documentation file `"
1200 << arg << "'. Bad installation?" << endl;
1203 owner->message(bformat(_("Opening help file %1$s..."),
1204 MakeDisplayPath(fname)));
1205 view()->loadLyXFile(fname, false);
1209 // --- version control -------------------------------
1210 case LFUN_VC_REGISTER:
1212 if (!owner->buffer()->lyxvc.inUse())
1213 owner->buffer()->lyxvc.registrer();
1217 case LFUN_VC_CHECKIN:
1219 if (owner->buffer()->lyxvc.inUse()
1220 && !owner->buffer()->isReadonly())
1221 owner->buffer()->lyxvc.checkIn();
1225 case LFUN_VC_CHECKOUT:
1227 if (owner->buffer()->lyxvc.inUse()
1228 && owner->buffer()->isReadonly())
1229 owner->buffer()->lyxvc.checkOut();
1233 case LFUN_VC_REVERT:
1235 owner->buffer()->lyxvc.revert();
1241 owner->buffer()->lyxvc.undoLast();
1245 // --- buffers ----------------------------------------
1247 case LFUN_SWITCHBUFFER:
1248 view()->buffer(bufferlist.getBuffer(argument));
1253 // servercmd: argument must be <file>:<template>
1254 Buffer * tmpbuf = NewFile(argument);
1256 view()->buffer(tmpbuf);
1260 case LFUN_FILE_OPEN:
1264 case LFUN_LAYOUT_TABULAR:
1265 if (view()->theLockingInset()) {
1266 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1267 InsetTabular * inset = static_cast<InsetTabular *>
1268 (view()->theLockingInset());
1269 inset->openLayoutDialog(view());
1270 } else if (view()->theLockingInset()->
1271 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1272 InsetTabular * inset = static_cast<InsetTabular *>(
1273 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1274 inset->openLayoutDialog(view());
1279 case LFUN_DROP_LAYOUTS_CHOICE:
1280 owner->getToolbar().openLayoutList();
1283 case LFUN_MENU_OPEN_BY_NAME:
1284 owner->getMenubar().openByName(argument);
1285 break; // RVDK_PATCH_5
1287 // --- lyxserver commands ----------------------------
1291 setMessage(owner->buffer()->fileName());
1292 lyxerr[Debug::INFO] << "FNAME["
1293 << owner->buffer()->fileName()
1299 dispatch_buffer = keyseq.print();
1300 lyxserver->notifyClient(dispatch_buffer);
1304 case LFUN_GOTOFILEROW:
1308 istringstream istr(argument.c_str());
1309 istr >> file_name >> row;
1310 // Must replace extension of the file to be .lyx and get full path
1311 string const s(ChangeExtension(file_name, ".lyx"));
1313 // Either change buffer or load the file
1314 if (bufferlist.exists(s)) {
1315 view()->buffer(bufferlist.getBuffer(s));
1317 view()->loadLyXFile(s);
1320 view()->setCursorFromRow(row);
1323 // see BufferView_pimpl::center()
1324 view()->updateScrollbar();
1328 case LFUN_GOTO_PARAGRAPH:
1330 istringstream istr(argument.c_str());
1334 ParIterator par = owner->buffer()->getParFromID(id);
1335 if (par == owner->buffer()->par_iterator_end()) {
1336 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1337 << id << ']' << endl;
1340 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1341 << " found." << endl;
1344 if (view()->theLockingInset())
1345 view()->unlockInset(view()->theLockingInset());
1346 if (par->inInset()) {
1347 FuncRequest cmd(view(), LFUN_INSET_EDIT, "left");
1348 par->inInset()->localDispatch(cmd);
1351 view()->getLyXText()->setCursor(par.pit(), 0);
1352 view()->switchKeyMap();
1353 owner->view_state_changed();
1356 // see BufferView_pimpl::center()
1357 view()->updateScrollbar();
1361 // --- insert characters ----------------------------------------
1363 // --- Mathed stuff. If we are here, there is no locked inset yet.
1364 case LFUN_MATH_EXTERN:
1365 case LFUN_MATH_NUMBER:
1366 case LFUN_MATH_NONUMBER:
1367 case LFUN_MATH_LIMITS:
1369 setErrorMessage(N_("This is only allowed in math mode!"));
1373 // passthrough hat and underscore outside mathed:
1374 case LFUN_SUBSCRIPT:
1375 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1377 case LFUN_SUPERSCRIPT:
1378 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1381 case LFUN_DIALOG_SHOW: {
1382 string const name = ev.getArg(0);
1383 string data = trim(ev.argument.substr(name.size()));
1385 if (name == "character") {
1386 data = freefont2string();
1388 owner->getDialogs().show("character", data);
1389 } else if (name == "document")
1390 owner->getDialogs().showDocument();
1391 else if (name == "findreplace")
1392 owner->getDialogs().showSearch();
1393 else if (name == "forks")
1394 owner->getDialogs().showForks();
1395 else if (name == "preamble")
1396 owner->getDialogs().showPreamble();
1397 else if (name == "preferences")
1398 owner->getDialogs().showPreferences();
1399 else if (name == "print")
1400 owner->getDialogs().showPrint();
1401 else if (name == "spellchecker")
1402 owner->getDialogs().showSpellchecker();
1404 owner->getDialogs().show(name, data);
1408 case LFUN_DIALOG_SHOW_NEW_INSET: {
1409 string const & name = argument;
1411 if (name == "bibitem" ||
1413 name == "include" ||
1418 InsetCommandParams p(name);
1419 data = InsetCommandMailer::params2string(name, p);
1420 } else if (name == "citation") {
1421 InsetCommandParams p("cite");
1422 data = InsetCommandMailer::params2string(name, p);
1424 owner->getDialogs().show(name, data, 0);
1428 case LFUN_DIALOG_SHOW_NEXT_INSET: {
1432 case LFUN_DIALOG_UPDATE: {
1433 string const & name = argument;
1434 // Can only update a dialog connected to an existing inset
1435 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1437 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1439 inset->localDispatch(fr);
1440 } else if (name == "paragraph") {
1441 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1446 case LFUN_DIALOG_HIDE:
1447 Dialogs::hide(argument, 0);
1450 case LFUN_DIALOG_DISCONNECT_INSET:
1451 owner->getDialogs().disconnect(argument);
1454 case LFUN_CHILDOPEN:
1456 string const filename =
1457 MakeAbsPath(argument,
1458 owner->buffer()->filePath());
1459 setMessage(N_("Opening child document ") +
1460 MakeDisplayPath(filename) + "...");
1461 view()->savePosition(0);
1462 if (bufferlist.exists(filename))
1463 view()->buffer(bufferlist.getBuffer(filename));
1465 view()->loadLyXFile(filename);
1469 case LFUN_TOGGLECURSORFOLLOW:
1470 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1474 owner->getIntl().KeyMapOn(false);
1477 case LFUN_KMAP_PRIM:
1478 owner->getIntl().KeyMapPrim();
1482 owner->getIntl().KeyMapSec();
1485 case LFUN_KMAP_TOGGLE:
1486 owner->getIntl().ToggleKeyMap();
1491 // argument contains ';'-terminated commands
1492 while (!argument.empty()) {
1494 argument = split(argument, first, ';');
1500 case LFUN_SAVEPREFERENCES:
1502 Path p(user_lyxdir);
1503 lyxrc.write("preferences");
1507 case LFUN_SCREEN_FONT_UPDATE:
1509 // handle the screen font changes.
1510 lyxrc.set_font_norm_type();
1511 lyx_gui::update_fonts();
1512 // We also need to empty the textcache so that
1513 // the buffer will be formatted correctly after
1516 // Of course we should only do the resize and the textcache.clear
1517 // if values really changed...but not very important right now. (Lgb)
1518 // All visible buffers will need resize
1524 case LFUN_SET_COLOR:
1527 string const x11_name = split(argument, lyx_name, ' ');
1528 if (lyx_name.empty() || x11_name.empty()) {
1529 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1534 bool const graphicsbg_changed =
1535 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1536 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1538 if (!lcolor.setColor(lyx_name, x11_name)) {
1540 bformat(_("Set-color \"%1$s\" failed "
1541 "- color is undefined or "
1542 "may not be redefined"), lyx_name));
1546 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1548 if (graphicsbg_changed) {
1549 #ifdef WITH_WARNINGS
1550 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1553 grfx::GCache & gc = grfx::GCache::get();
1554 gc.changeDisplay(true);
1563 owner->message(argument);
1566 case LFUN_FORKS_KILL:
1568 if (!isStrInt(argument))
1571 pid_t const pid = strToInt(argument);
1572 ForkedcallsController & fcc = ForkedcallsController::get();
1577 case LFUN_TOOLTIPS_TOGGLE:
1578 owner->getDialogs().toggleTooltips();
1581 case LFUN_EXTERNAL_EDIT: {
1583 .localDispatch(FuncRequest(view(), action, argument));
1588 // Then if it was none of the above
1589 // Trying the BufferView::pimpl dispatch:
1590 if (!view()->dispatch(ev))
1591 lyxerr << "A truly unknown func ["
1592 << lyxaction.getActionName(ev.action) << "]!"
1599 view()->owner()->updateLayoutChoice();
1601 if (view()->available()) {
1602 view()->fitCursor();
1604 // If we executed a mutating lfun, mark the buffer as dirty
1605 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)
1606 && !lyxaction.funcHasFlag(ev.action, LyXAction::ReadOnly))
1607 view()->buffer()->markDirty();
1610 sendDispatchMessage(getMessage(), ev, verbose);
1614 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1616 owner->updateMenubar();
1617 owner->updateToolbar();
1619 if (ev.action == LFUN_SELFINSERT || !verbose) {
1620 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1622 owner->message(msg);
1626 string dispatch_msg = msg;
1627 if (!dispatch_msg.empty())
1628 dispatch_msg += ' ';
1630 string comname = lyxaction.getActionName(ev.action);
1632 int pseudoaction = ev.action;
1633 bool argsadded = false;
1635 if (!ev.argument.empty()) {
1636 // the pseudoaction is useful for the bindings
1637 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1639 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1640 pseudoaction = ev.action;
1642 comname += ' ' + ev.argument;
1647 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1649 if (!shortcuts.empty()) {
1650 comname += ": " + shortcuts;
1651 } else if (!argsadded && !ev.argument.empty()) {
1652 comname += ' ' + ev.argument;
1655 if (!comname.empty()) {
1656 comname = rtrim(comname);
1657 dispatch_msg += '(' + comname + ')';
1660 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1661 if (!dispatch_msg.empty())
1662 owner->message(dispatch_msg);
1666 void LyXFunc::setupLocalKeymap()
1668 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1669 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1673 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1675 string initpath = lyxrc.document_path;
1676 string filename(name);
1678 if (view()->available()) {
1679 string const trypath = owner->buffer()->filePath();
1680 // If directory is writeable, use this as default.
1681 if (IsDirWriteable(trypath))
1685 static int newfile_number;
1687 if (filename.empty()) {
1688 filename = AddName(lyxrc.document_path,
1689 "newfile" + tostr(++newfile_number) + ".lyx");
1690 FileInfo fi(filename);
1691 while (bufferlist.exists(filename) || fi.readable()) {
1693 filename = AddName(lyxrc.document_path,
1694 "newfile" + tostr(newfile_number) +
1696 fi.newFile(filename);
1700 // The template stuff
1703 FileDialog fileDlg(_("Select template file"),
1704 LFUN_SELECT_FILE_SYNC,
1705 make_pair(string(_("Documents|#o#O")),
1706 string(lyxrc.document_path)),
1707 make_pair(string(_("Templates|#T#t")),
1708 string(lyxrc.template_path)));
1710 FileDialog::Result result =
1711 fileDlg.open(lyxrc.template_path,
1712 _("*.lyx| LyX Documents (*.lyx)"));
1714 if (result.first == FileDialog::Later)
1717 string const fname = result.second;
1724 view()->buffer(newFile(filename, templname, !name.empty()));
1728 void LyXFunc::open(string const & fname)
1730 string initpath = lyxrc.document_path;
1732 if (view()->available()) {
1733 string const trypath = owner->buffer()->filePath();
1734 // If directory is writeable, use this as default.
1735 if (IsDirWriteable(trypath))
1741 if (fname.empty()) {
1742 FileDialog fileDlg(_("Select document to open"),
1744 make_pair(string(_("Documents|#o#O")),
1745 string(lyxrc.document_path)),
1746 make_pair(string(_("Examples|#E#e")),
1747 string(AddPath(system_lyxdir, "examples"))));
1749 FileDialog::Result result =
1750 fileDlg.open(initpath,
1751 _("*.lyx| LyX Documents (*.lyx)"));
1753 if (result.first == FileDialog::Later)
1756 filename = result.second;
1758 // check selected filename
1759 if (filename.empty()) {
1760 owner->message(_("Canceled."));
1766 // get absolute path of file and add ".lyx" to the filename if
1768 string const fullpath = FileSearch(string(), filename, "lyx");
1769 if (!fullpath.empty()) {
1770 filename = fullpath;
1773 string const disp_fn(MakeDisplayPath(filename));
1775 // if the file doesn't exist, let the user create one
1776 FileInfo const f(filename, true);
1778 // the user specifically chose this name. Believe them.
1779 Buffer * buffer = newFile(filename, "", true);
1780 view()->buffer(buffer);
1784 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1787 if (view()->loadLyXFile(filename)) {
1788 str2 = bformat(_("Document %1$s opened."), disp_fn);
1790 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1792 owner->message(str2);
1796 void LyXFunc::doImport(string const & argument)
1799 string filename = split(argument, format, ' ');
1801 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1802 << " file: " << filename << endl;
1804 // need user interaction
1805 if (filename.empty()) {
1806 string initpath = lyxrc.document_path;
1808 if (view()->available()) {
1809 string const trypath = owner->buffer()->filePath();
1810 // If directory is writeable, use this as default.
1811 if (IsDirWriteable(trypath))
1815 string const text = bformat(_("Select %1$s file to import"),
1816 formats.prettyName(format));
1818 FileDialog fileDlg(text,
1820 make_pair(string(_("Documents|#o#O")),
1821 string(lyxrc.document_path)),
1822 make_pair(string(_("Examples|#E#e")),
1823 string(AddPath(system_lyxdir, "examples"))));
1825 string const extension = "*." + formats.extension(format)
1826 + "| " + formats.prettyName(format)
1827 + " (*." + formats.extension(format) + ')';
1829 FileDialog::Result result = fileDlg.open(initpath,
1832 if (result.first == FileDialog::Later)
1835 filename = result.second;
1837 // check selected filename
1838 if (filename.empty())
1839 owner->message(_("Canceled."));
1842 if (filename.empty())
1845 // get absolute path of file
1846 filename = MakeAbsPath(filename);
1848 string const lyxfile = ChangeExtension(filename, ".lyx");
1850 // Check if the document already is open
1851 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1852 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1853 owner->message(_("Canceled."));
1858 // if the file exists already, and we didn't do
1859 // -i lyx thefile.lyx, warn
1860 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1861 string const file = MakeDisplayPath(lyxfile, 30);
1863 string text = bformat(_("The document %1$s already exists.\n\n"
1864 "Do you want to over-write that document?"), file);
1865 int const ret = Alert::prompt(_("Over-write document?"),
1866 text, 0, 1, _("&Over-write"), _("&Cancel"));
1869 owner->message(_("Canceled."));
1874 Importer::Import(owner, filename, format);
1878 void LyXFunc::closeBuffer()
1880 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1881 if (bufferlist.empty()) {
1882 // need this otherwise SEGV may occur while trying to
1883 // set variables that don't exist
1884 // since there's no current buffer
1885 owner->getDialogs().hideBufferDependent();
1887 view()->buffer(bufferlist.first());
1893 // Each "owner" should have it's own message method. lyxview and
1894 // the minibuffer would use the minibuffer, but lyxserver would
1895 // send an ERROR signal to its client. Alejandro 970603
1896 // This func is bit problematic when it comes to NLS, to make the
1897 // lyx servers client be language indepenent we must not translate
1898 // strings sent to this func.
1899 void LyXFunc::setErrorMessage(string const & m) const
1901 dispatch_buffer = m;
1906 void LyXFunc::setMessage(string const & m) const
1908 dispatch_buffer = m;
1912 void LyXFunc::setStatusMessage(string const & m) const
1918 string const LyXFunc::view_status_message()
1920 // When meta-fake key is pressed, show the key sequence so far + "M-".
1922 return keyseq.print() + "M-";
1925 // Else, when a non-complete key sequence is pressed,
1926 // show the available options.
1927 if (keyseq.length() > 0 && !keyseq.deleted()) {
1928 return keyseq.printOptions();
1931 if (!view()->available())
1932 return _("Welcome to LyX!");
1934 return currentState(view());
1938 BufferView * LyXFunc::view() const
1941 return owner->view().get();