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())
121 view()->switchKeyMap();
125 void LyXFunc::handleKeyFunc(kb_action action)
127 char c = encoded_last_key;
129 if (keyseq.length()) {
133 owner->getIntl().getTransManager()
134 .deadkey(c, get_accent(action).accent, view()->getLyXText());
135 // Need to clear, in case the minibuffer calls these
138 // copied verbatim from do_accent_char
140 view()->getLyXText()->selection.cursor = view()->getLyXText()->cursor;
144 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
145 key_modifier::state state)
149 if (lyxerr.debugging(Debug::KEY)) {
150 lyxerr << "KeySym is "
151 << keysym->getSymbolName()
155 // Do nothing if we have nothing (JMarc)
156 if (!keysym->isOK()) {
157 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
162 if (keysym->isModifier()) {
163 lyxerr[Debug::KEY] << "isModifier true" << endl;
167 Encoding const * encoding = view()->getEncoding();
169 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
171 // Do a one-deep top-level lookup for
172 // cancel and meta-fake keys. RVDK_PATCH_5
173 cancel_meta_seq.reset();
175 int action = cancel_meta_seq.addkey(keysym, state);
176 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
178 // When not cancel or meta-fake, do the normal lookup.
179 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
180 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
181 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
182 // remove Caps Lock and Mod2 as a modifiers
183 action = keyseq.addkey(keysym, (state | meta_fake_bit));
184 lyxerr[Debug::KEY] << "action now set to ["
185 << action << ']' << endl;
188 // Dont remove this unless you know what you are doing.
189 meta_fake_bit = key_modifier::none;
191 // can this happen now ?
192 if (action == LFUN_NOACTION) {
193 action = LFUN_PREFIX;
196 if (lyxerr.debugging(Debug::KEY)) {
197 lyxerr << "Key [action="
199 << keyseq.print() << ']'
203 // already here we know if it any point in going further
204 // why not return already here if action == -1 and
205 // num_bytes == 0? (Lgb)
207 if (keyseq.length() > 1) {
208 owner->message(keyseq.print());
212 // Maybe user can only reach the key via holding down shift.
213 // Let's see. But only if shift is the only modifier
214 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
215 lyxerr[Debug::KEY] << "Trying without shift" << endl;
216 action = keyseq.addkey(keysym, key_modifier::none);
217 lyxerr[Debug::KEY] << "Action now " << action << endl;
220 if (action == LFUN_UNKNOWN_ACTION) {
221 // Hmm, we didn't match any of the keysequences. See
222 // if it's normal insertable text not already covered
224 if (keysym->isText() && keyseq.length() == 1) {
225 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
226 action = LFUN_SELFINSERT;
228 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
229 owner->message(_("Unknown function."));
234 if (action == LFUN_SELFINSERT) {
235 if (encoded_last_key != 0) {
237 arg += encoded_last_key;
239 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
241 lyxerr[Debug::KEY] << "SelfInsert arg[`"
242 << argument << "']" << endl;
250 FuncStatus LyXFunc::getStatus(int ac) const
252 return getStatus(lyxaction.retrieveActionArg(ac));
256 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
259 Buffer * buf = owner->buffer();
261 if (ev.action == LFUN_NOACTION) {
262 setStatusMessage(N_("Nothing to do"));
263 return flag.disabled(true);
267 case LFUN_UNKNOWN_ACTION:
268 #ifndef HAVE_LIBAIKSAURUS
269 case LFUN_THESAURUS_ENTRY:
274 flag |= lyx_gui::getStatus(ev);
277 if (flag.unknown()) {
278 setStatusMessage(N_("Unknown action"));
282 // the default error message if we disable the command
283 setStatusMessage(N_("Command disabled"));
285 // Check whether we need a buffer
286 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
287 // Yes we need a buffer, do we have one?
290 // Can we use a readonly buffer?
291 if (buf->isReadonly() &&
292 !lyxaction.funcHasFlag(ev.action,
293 LyXAction::ReadOnly)) {
295 setStatusMessage(N_("Document is read-only"));
300 setStatusMessage(N_("Command not allowed with"
301 "out any document open"));
302 return flag.disabled(true);
306 UpdatableInset * tli = view()->theLockingInset();
308 // I would really like to avoid having this switch and rather try to
309 // encode this in the function itself.
310 bool disable = false;
313 disable = ev.argument != "custom"
314 && !Exporter::IsExportable(buf, ev.argument);
317 disable = buf->undostack.empty();
320 disable = buf->redostack.empty();
325 UpdatableInset * in = tli;
326 if (in->lyxCode() != InsetOld::TABULAR_CODE) {
327 in = tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE);
329 if (in && static_cast<InsetTabular*>(in)->hasSelection()) {
334 disable = !mathcursor && !view()->getLyXText()->selection.set();
337 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
340 disable = !Exporter::IsExportable(buf, "program");
343 case LFUN_LAYOUT_TABULAR:
345 || (tli->lyxCode() != InsetOld::TABULAR_CODE
346 && !tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE));
350 disable = !changeDepth(view(), view()->getLyXText(), DEC_DEPTH, true);
353 case LFUN_DEPTH_PLUS:
354 disable = !changeDepth(view(), view()->getLyXText(), INC_DEPTH, true);
358 case LFUN_LAYOUT_PARAGRAPH: {
359 InsetOld * inset = view()->getLyXText()->cursor.par()->inInset();
360 disable = inset && inset->forceDefaultParagraphs(inset);
364 case LFUN_INSET_OPTARG:
365 disable = (view()->getLyXText()->cursor.par()->layout()->optionalargs == 0);
368 case LFUN_TABULAR_FEATURE:
371 // FIXME: check temporarily disabled
373 char align = mathcursor->valign();
378 if (ev.argument.empty()) {
382 if (!contains("tcb", ev.argument[0])) {
386 flag.setOnOff(ev.argument[0] == align);
390 char align = mathcursor->halign();
395 if (ev.argument.empty()) {
399 if (!contains("lcr", ev.argument[0])) {
403 flag.setOnOff(ev.argument[0] == align);
406 disable = !mathcursor->halign();
412 //ret.disabled(true);
413 if (tli->lyxCode() == InsetOld::TABULAR_CODE) {
414 ret = static_cast<InsetTabular *>(tli)
415 ->getStatus(ev.argument);
418 } else if (tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE)) {
419 ret = static_cast<InsetTabular *>
420 (tli->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE))
421 ->getStatus(ev.argument);
428 static InsetTabular inset(*owner->buffer(), 1, 1);
432 ret = inset.getStatus(ev.argument);
433 if (ret.onoff(true) || ret.onoff(false))
434 flag.setOnOff(false);
438 case LFUN_VC_REGISTER:
439 disable = buf->lyxvc.inUse();
441 case LFUN_VC_CHECKIN:
442 disable = !buf->lyxvc.inUse() || buf->isReadonly();
444 case LFUN_VC_CHECKOUT:
445 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
449 disable = !buf->lyxvc.inUse();
451 case LFUN_MENURELOAD:
452 disable = buf->isUnnamed() || buf->isClean();
454 case LFUN_BOOKMARK_GOTO:
456 isSavedPosition(strToUnsignedInt(ev.argument));
458 case LFUN_MERGE_CHANGES:
459 case LFUN_ACCEPT_CHANGE:
460 case LFUN_REJECT_CHANGE:
461 case LFUN_ACCEPT_ALL_CHANGES:
462 case LFUN_REJECT_ALL_CHANGES:
463 disable = !buf->params.tracking_changes;
465 case LFUN_INSET_TOGGLE: {
466 LyXText * lt = view()->getLyXText();
467 disable = !(isEditableInset(lt->getInset())
469 && lt->inset_owner->owner()
470 && lt->inset_owner->owner()->isOpen()));
474 case LFUN_INSET_SETTINGS: {
476 UpdatableInset * inset = view()->theLockingInset();
481 // get the innermost inset
482 inset = inset->getLockingInset();
484 // jump back to owner if an InsetText, so
485 // we get back to the InsetTabular or whatever
486 if (inset->lyxCode() == InsetOld::TEXT_CODE)
487 inset = inset->owner();
489 InsetOld::Code code = inset->lyxCode();
491 case InsetOld::TABULAR_CODE:
492 disable = ev.argument != "tabular";
494 case InsetOld::ERT_CODE:
495 disable = ev.argument != "ert";
497 case InsetOld::FLOAT_CODE:
498 disable = ev.argument != "float";
500 case InsetOld::MINIPAGE_CODE:
501 disable = ev.argument != "minipage";
503 case InsetOld::WRAP_CODE:
504 disable = ev.argument != "wrap";
506 case InsetOld::NOTE_CODE:
507 disable = ev.argument != "note";
515 case LFUN_MATH_MUTATE:
517 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
518 flag.setOnOff(false);
523 // we just need to be in math mode to enable that
525 case LFUN_MATH_SPACE:
526 case LFUN_MATH_LIMITS:
527 case LFUN_MATH_NONUMBER:
528 case LFUN_MATH_NUMBER:
529 case LFUN_MATH_EXTERN:
530 disable = !mathcursor;
533 case LFUN_DIALOG_SHOW: {
534 string const name = ev.getArg(0);
536 disable = !(name == "aboutlyx" ||
539 name == "preferences" ||
544 if (name == "print") {
545 disable = !Exporter::IsExportable(buf, "dvi") ||
546 lyxrc.print_command == "none";
547 } else if (name == "character") {
548 UpdatableInset * tli = view()->theLockingInset();
549 disable = tli && tli->lyxCode() == InsetOld::ERT_CODE;
550 } else if (name == "vclog") {
551 disable = !buf->lyxvc.inUse();
552 } else if (name == "latexlog") {
553 disable = !IsFileReadable(buf->getLogName().second);
562 // the functions which insert insets
563 InsetOld::Code code = InsetOld::NO_CODE;
565 case LFUN_DIALOG_SHOW_NEW_INSET:
566 if (ev.argument == "bibitem")
567 code = InsetOld::BIBITEM_CODE;
568 else if (ev.argument == "bibtex")
569 code = InsetOld::BIBTEX_CODE;
570 else if (ev.argument == "citation")
571 code = InsetOld::CITE_CODE;
572 else if (ev.argument == "ert")
573 code = InsetOld::ERT_CODE;
574 else if (ev.argument == "external")
575 code = InsetOld::EXTERNAL_CODE;
576 else if (ev.argument == "float")
577 code = InsetOld::FLOAT_CODE;
578 else if (ev.argument == "graphics")
579 code = InsetOld::GRAPHICS_CODE;
580 else if (ev.argument == "include")
581 code = InsetOld::INCLUDE_CODE;
582 else if (ev.argument == "index")
583 code = InsetOld::INDEX_CODE;
584 else if (ev.argument == "label")
585 code = InsetOld::LABEL_CODE;
586 else if (ev.argument == "minipage")
587 code = InsetOld::MINIPAGE_CODE;
588 else if (ev.argument == "ref")
589 code = InsetOld::REF_CODE;
590 else if (ev.argument == "toc")
591 code = InsetOld::TOC_CODE;
592 else if (ev.argument == "url")
593 code = InsetOld::URL_CODE;
594 else if (ev.argument == "wrap")
595 code = InsetOld::WRAP_CODE;
599 code = InsetOld::ERT_CODE;
601 case LFUN_INSET_FOOTNOTE:
602 code = InsetOld::FOOT_CODE;
604 case LFUN_TABULAR_INSERT:
605 code = InsetOld::TABULAR_CODE;
607 case LFUN_INSET_MARGINAL:
608 code = InsetOld::MARGIN_CODE;
610 case LFUN_INSET_MINIPAGE:
611 code = InsetOld::MINIPAGE_CODE;
613 case LFUN_INSET_FLOAT:
614 case LFUN_INSET_WIDE_FLOAT:
615 code = InsetOld::FLOAT_CODE;
617 case LFUN_INSET_WRAP:
618 code = InsetOld::WRAP_CODE;
620 case LFUN_FLOAT_LIST:
621 code = InsetOld::FLOAT_LIST_CODE;
624 case LFUN_INSET_LIST:
625 code = InsetOld::LIST_CODE;
627 case LFUN_INSET_THEOREM:
628 code = InsetOld::THEOREM_CODE;
631 case LFUN_INSET_CAPTION:
632 code = InsetOld::CAPTION_CODE;
634 case LFUN_INSERT_NOTE:
635 code = InsetOld::NOTE_CODE;
637 case LFUN_INSERT_LABEL:
638 code = InsetOld::LABEL_CODE;
640 case LFUN_INSET_OPTARG:
641 code = InsetOld::OPTARG_CODE;
643 case LFUN_ENVIRONMENT_INSERT:
644 code = InsetOld::MINIPAGE_CODE;
646 case LFUN_INDEX_INSERT:
647 code = InsetOld::INDEX_CODE;
649 case LFUN_INDEX_PRINT:
650 code = InsetOld::INDEX_PRINT_CODE;
652 case LFUN_TOC_INSERT:
653 code = InsetOld::TOC_CODE;
657 code = InsetOld::URL_CODE;
660 // always allow this, since we will inset a raw quote
661 // if an inset is not allowed.
663 case LFUN_HYPHENATION:
664 case LFUN_LIGATURE_BREAK:
666 case LFUN_MENU_SEPARATOR:
668 case LFUN_END_OF_SENTENCE:
669 code = InsetOld::SPECIALCHAR_CODE;
671 case LFUN_SPACE_INSERT:
672 // slight hack: we know this is allowed in math mode
674 code = InsetOld::SPACE_CODE;
679 if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
685 // A few general toggles
687 case LFUN_TOOLTIPS_TOGGLE:
688 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
691 case LFUN_READ_ONLY_TOGGLE:
692 flag.setOnOff(buf->isReadonly());
695 flag.setOnOff(view()->getLyXText()->cursor.par()->params().startOfAppendix());
697 case LFUN_SWITCHBUFFER:
698 // toggle on the current buffer, but do not toggle off
699 // the other ones (is that a good idea?)
700 if (ev.argument == buf->fileName())
703 case LFUN_TRACK_CHANGES:
704 flag.setOnOff(buf->params.tracking_changes);
710 // the font related toggles
712 LyXFont const & font = view()->getLyXText()->real_current_font;
715 flag.setOnOff(font.emph() == LyXFont::ON);
718 flag.setOnOff(font.noun() == LyXFont::ON);
721 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
724 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
727 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
730 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
736 string tc = mathcursor->getLastCode();
739 flag.setOnOff(tc == "mathbf");
742 flag.setOnOff(tc == "mathsf");
745 flag.setOnOff(tc == "mathcal");
748 flag.setOnOff(tc == "mathrm");
751 flag.setOnOff(tc == "mathtt");
754 flag.setOnOff(tc == "mathbb");
757 flag.setOnOff(tc == "mathnormal");
764 // this one is difficult to get right. As a half-baked
765 // solution, we consider only the first action of the sequence
766 if (ev.action == LFUN_SEQUENCE) {
767 // argument contains ';'-terminated commands
768 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
775 void LyXFunc::dispatch(string const & s, bool verbose)
777 int const action = lyxaction.LookupFunc(s);
779 if (action == LFUN_UNKNOWN_ACTION) {
780 owner->message(bformat(_("Unknown function (%1$s)"), s));
784 dispatch(action, verbose);
788 void LyXFunc::dispatch(int ac, bool verbose)
790 dispatch(lyxaction.retrieveActionArg(ac), verbose);
794 bool ensureBufferClean(BufferView * bv) {
796 Buffer & buf = *bv->buffer();
800 string const file = MakeDisplayPath(buf.fileName(), 30);
801 string text = bformat(_("The document %1$s has unsaved "
802 "changes.\n\nDo you want to save "
803 "the document?"), file);
804 int const ret = Alert::prompt(_("Save changed document?"),
805 text, 0, 1, _("&Save"),
809 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
811 return buf.isClean();
817 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
819 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
820 <<"] arg[" << ev.argument << ']' << endl;
822 // we have not done anything wrong yet.
824 dispatch_buffer.erase();
826 #ifdef NEW_DISPATCHER
827 // We try do call the most specific dispatcher first:
828 // 1. the lockinginset's dispatch
829 // 2. the bufferview's dispatch
830 // 3. the lyxview's dispatch
833 selection_possible = false;
835 string argument = ev.argument;
836 kb_action action = ev.action;
838 // We cannot use this function here
839 if (getStatus(ev).disabled()) {
840 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
841 << lyxaction.getActionName(action)
842 << " [" << action << "] is disabled at this location"
844 setErrorMessage(getStatusMessage());
845 goto exit_with_message;
848 if (view()->available())
849 view()->hideCursor();
851 if (view()->available() && view()->theLockingInset()) {
852 InsetOld::RESULT result;
853 if (action > 1 || (action == LFUN_UNKNOWN_ACTION &&
856 UpdatableInset * inset = view()->theLockingInset();
860 inset->getCursorPos(view(), inset_x, dummy_y);
862 if ((action == LFUN_UNKNOWN_ACTION)
863 && argument.empty()) {
864 argument = encoded_last_key;
867 // the insets can't try to handle this,
868 // a table cell in the dummy position will
869 // lock its insettext, the insettext will
870 // pass it the bufferview, and succeed,
871 // so it will stay not locked. Not good
872 // if we've just done LFUN_ESCAPE (which
873 // injects an LFUN_PARAGRAPH_UPDATE)
874 if (action == LFUN_PARAGRAPH_UPDATE) {
875 view()->dispatch(ev);
876 goto exit_with_message;
879 // Undo/Redo is a bit tricky for insets.
880 if (action == LFUN_UNDO) {
882 goto exit_with_message;
883 } else if (action == LFUN_REDO) {
885 goto exit_with_message;
886 } else if (((result=inset->
887 // Hand-over to inset's own dispatch:
888 localDispatch(FuncRequest(view(), action, argument))) ==
890 (result == DISPATCHED_NOUPDATE))
891 goto exit_with_message;
892 // If UNDISPATCHED, just soldier on
893 else if (result == FINISHED) {
894 owner->clearMessage();
895 goto exit_with_message;
896 // We do not need special RTL handling here:
897 // FINISHED means that the cursor should be
898 // one position after the inset.
899 } else if (result == FINISHED_RIGHT) {
900 view()->text->cursorRight(view());
902 owner->clearMessage();
903 goto exit_with_message;
904 } else if (result == FINISHED_UP) {
905 RowList::iterator const irow = view()->text->cursorIRow();
906 if (irow != view()->text->rows().begin()) {
908 view()->text->setCursorFromCoordinates(
909 view()->text->cursor.ix() + inset_x,
910 view()->text->cursor.iy() -
911 irow->baseline() - 1);
912 view()->text->cursor.x_fix(view()->text->cursor.x());
914 view()->text->cursorUp(view());
920 owner->clearMessage();
921 goto exit_with_message;
922 } else if (result == FINISHED_DOWN) {
923 RowList::iterator const irow = view()->text->cursorIRow();
924 if (boost::next(irow) != view()->text->rows().end()) {
926 view()->text->setCursorFromCoordinates(
927 view()->text->cursor.ix() + inset_x,
928 view()->text->cursor.iy() -
931 view()->text->cursor.x_fix(view()->text->cursor.x());
933 view()->text->cursorDown(view());
936 view()->text->cursorRight(view());
939 owner->clearMessage();
940 goto exit_with_message;
942 #warning I am not sure this is still right, please have a look! (Jug 20020417)
943 else { // result == UNDISPATCHED
944 //setMessage(N_("Text mode"));
946 case LFUN_UNKNOWN_ACTION:
947 case LFUN_BREAKPARAGRAPH:
949 view()->text->cursorRight(view());
950 view()->switchKeyMap();
951 owner->view_state_changed();
954 if (!view()->text->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
955 view()->text->cursorRight(view());
957 owner->view_state_changed();
959 goto exit_with_message;
961 if (view()->text->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
962 view()->text->cursorRight(view());
964 owner->view_state_changed();
966 goto exit_with_message;
968 if (boost::next(view()->text->cursorRow()) != view()->text->rows().end())
969 view()->text->cursorDown(view());
971 view()->text->cursorRight(view());
973 owner->view_state_changed();
974 goto exit_with_message;
985 if (!view()->available())
987 // this function should be used always [asierra060396]
988 UpdatableInset * tli = view()->theLockingInset();
990 UpdatableInset * lock = tli->getLockingInset();
993 view()->unlockInset(tli);
994 view()->text->cursorRight(view());
996 owner->view_state_changed();
998 tli->unlockInsetInInset(view(), lock, true);
1001 // Tell the paragraph dialog that we changed paragraph
1002 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1007 case LFUN_WORDFINDFORWARD:
1008 case LFUN_WORDFINDBACKWARD: {
1009 static string last_search;
1010 string searched_string;
1012 if (!argument.empty()) {
1013 last_search = argument;
1014 searched_string = argument;
1016 searched_string = last_search;
1018 bool fw = (action == LFUN_WORDFINDFORWARD);
1019 if (!searched_string.empty())
1020 lyx::find::find(view(), searched_string, fw);
1025 if (view()->available() && !view()->theLockingInset())
1027 owner->message(keyseq.printOptions());
1030 // --- Misc -------------------------------------------
1031 case LFUN_EXEC_COMMAND:
1032 owner->focus_command_buffer();
1037 meta_fake_bit = key_modifier::none;
1038 if (view()->available())
1039 // cancel any selection
1040 dispatch(LFUN_MARK_OFF);
1041 setMessage(N_("Cancel"));
1044 case LFUN_META_FAKE:
1045 meta_fake_bit = key_modifier::alt;
1046 setMessage(keyseq.print());
1049 case LFUN_READ_ONLY_TOGGLE:
1050 if (owner->buffer()->lyxvc.inUse())
1051 owner->buffer()->lyxvc.toggleReadOnly();
1053 owner->buffer()->setReadonly(
1054 !owner->buffer()->isReadonly());
1057 case LFUN_CENTER: // this is center and redraw.
1061 // --- Menus -----------------------------------------------
1063 menuNew(argument, false);
1066 case LFUN_MENUNEWTMPLT:
1067 menuNew(argument, true);
1070 case LFUN_CLOSEBUFFER:
1074 case LFUN_MENUWRITE:
1075 if (!owner->buffer()->isUnnamed()) {
1076 string const str = bformat(_("Saving document %1$s..."),
1077 MakeDisplayPath(owner->buffer()->fileName()));
1078 owner->message(str);
1079 MenuWrite(owner->buffer());
1080 owner->message(str + _(" done."));
1082 WriteAs(owner->buffer());
1086 WriteAs(owner->buffer(), argument);
1089 case LFUN_MENURELOAD: {
1090 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
1091 string text = bformat(_("Any changes will be lost. Are you sure "
1092 "you want to revert to the saved version of the document %1$s?"), file);
1093 int const ret = Alert::prompt(_("Revert to saved document?"),
1094 text, 0, 1, _("&Revert"), _("&Cancel"));
1102 Exporter::Export(owner->buffer(), argument, true);
1103 view()->showErrorList(BufferFormat(*owner->buffer()));
1107 Exporter::Preview(owner->buffer(), argument);
1108 view()->showErrorList(BufferFormat(*owner->buffer()));
1111 case LFUN_BUILDPROG:
1112 Exporter::Export(owner->buffer(), "program", true);
1113 view()->showErrorList(_("Build"));
1116 case LFUN_RUNCHKTEX:
1117 owner->buffer()->runChktex();
1118 view()->showErrorList(_("ChkTeX"));
1122 if (argument == "custom")
1123 owner->getDialogs().showSendto();
1125 Exporter::Export(owner->buffer(), argument, false);
1126 view()->showErrorList(BufferFormat(*owner->buffer()));
1140 InsetCommandParams p("tableofcontents");
1141 string const data = InsetCommandMailer::params2string("toc", p);
1142 owner->getDialogs().show("toc", data, 0);
1158 case LFUN_DEPTH_MIN:
1159 changeDepth(view(), view()->getLyXText(), DEC_DEPTH, false);
1160 owner->view_state_changed();
1163 case LFUN_DEPTH_PLUS:
1164 changeDepth(view(), view()->getLyXText(), INC_DEPTH, false);
1165 owner->view_state_changed();
1168 case LFUN_FREEFONT_APPLY:
1169 apply_freefont(view());
1172 case LFUN_FREEFONT_UPDATE:
1173 update_and_apply_freefont(view(), argument);
1176 case LFUN_RECONFIGURE:
1177 Reconfigure(view());
1181 case LFUN_FLOATSOPERATE:
1182 if (argument == "openfoot")
1183 view()->allFloats(1,0);
1184 else if (argument == "closefoot")
1185 view()->allFloats(0,0);
1186 else if (argument == "openfig")
1187 view()->allFloats(1,1);
1188 else if (argument == "closefig")
1189 view()->allFloats(0,1);
1192 #ifdef WITH_WARNINGS
1193 //#warning Find another implementation here (or another lyxfunc)!
1196 case LFUN_HELP_OPEN:
1198 string const arg = argument;
1200 setErrorMessage(N_("Missing argument"));
1203 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1204 if (fname.empty()) {
1205 lyxerr << "LyX: unable to find documentation file `"
1206 << arg << "'. Bad installation?" << endl;
1209 owner->message(bformat(_("Opening help file %1$s..."),
1210 MakeDisplayPath(fname)));
1211 view()->loadLyXFile(fname, false);
1215 // --- version control -------------------------------
1216 case LFUN_VC_REGISTER:
1218 if (!ensureBufferClean(view()))
1220 if (!owner->buffer()->lyxvc.inUse()) {
1221 owner->buffer()->lyxvc.registrer();
1227 case LFUN_VC_CHECKIN:
1229 if (!ensureBufferClean(view()))
1231 if (owner->buffer()->lyxvc.inUse()
1232 && !owner->buffer()->isReadonly()) {
1233 owner->buffer()->lyxvc.checkIn();
1239 case LFUN_VC_CHECKOUT:
1241 if (!ensureBufferClean(view()))
1243 if (owner->buffer()->lyxvc.inUse()
1244 && owner->buffer()->isReadonly()) {
1245 owner->buffer()->lyxvc.checkOut();
1251 case LFUN_VC_REVERT:
1253 owner->buffer()->lyxvc.revert();
1260 owner->buffer()->lyxvc.undoLast();
1265 // --- buffers ----------------------------------------
1267 case LFUN_SWITCHBUFFER:
1268 view()->buffer(bufferlist.getBuffer(argument));
1272 NewFile(view(), argument);
1275 case LFUN_FILE_OPEN:
1279 case LFUN_LAYOUT_TABULAR:
1280 if (view()->theLockingInset()) {
1281 if (view()->theLockingInset()->lyxCode()== InsetOld::TABULAR_CODE) {
1282 InsetTabular * inset = static_cast<InsetTabular *>
1283 (view()->theLockingInset());
1284 inset->openLayoutDialog(view());
1285 } else if (view()->theLockingInset()->
1286 getFirstLockingInsetOfType(InsetOld::TABULAR_CODE)!=0) {
1287 InsetTabular * inset = static_cast<InsetTabular *>(
1288 view()->theLockingInset()->getFirstLockingInsetOfType(InsetOld::TABULAR_CODE));
1289 inset->openLayoutDialog(view());
1294 case LFUN_DROP_LAYOUTS_CHOICE:
1295 owner->getToolbar().openLayoutList();
1298 case LFUN_MENU_OPEN_BY_NAME:
1299 owner->getMenubar().openByName(argument);
1300 break; // RVDK_PATCH_5
1302 // --- lyxserver commands ----------------------------
1306 setMessage(owner->buffer()->fileName());
1307 lyxerr[Debug::INFO] << "FNAME["
1308 << owner->buffer()->fileName()
1314 dispatch_buffer = keyseq.print();
1315 lyxserver->notifyClient(dispatch_buffer);
1319 case LFUN_GOTOFILEROW:
1323 istringstream istr(argument.c_str());
1324 istr >> file_name >> row;
1325 // Must replace extension of the file to be .lyx and get full path
1326 string const s(ChangeExtension(file_name, ".lyx"));
1328 // Either change buffer or load the file
1329 if (bufferlist.exists(s)) {
1330 view()->buffer(bufferlist.getBuffer(s));
1332 view()->loadLyXFile(s);
1335 view()->setCursorFromRow(row);
1338 // see BufferView_pimpl::center()
1339 view()->updateScrollbar();
1343 case LFUN_GOTO_PARAGRAPH:
1345 istringstream istr(argument.c_str());
1349 ParIterator par = owner->buffer()->getParFromID(id);
1350 if (par == owner->buffer()->par_iterator_end()) {
1351 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1352 << id << ']' << endl;
1355 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1356 << " found." << endl;
1359 if (view()->theLockingInset())
1360 view()->unlockInset(view()->theLockingInset());
1361 if (par->inInset()) {
1362 FuncRequest cmd(view(), LFUN_INSET_EDIT, "left");
1363 par->inInset()->localDispatch(cmd);
1366 view()->getLyXText()->setCursor(par.pit(), 0);
1367 view()->switchKeyMap();
1368 owner->view_state_changed();
1371 // see BufferView_pimpl::center()
1372 view()->updateScrollbar();
1376 // --- insert characters ----------------------------------------
1378 // --- Mathed stuff. If we are here, there is no locked inset yet.
1379 case LFUN_MATH_EXTERN:
1380 case LFUN_MATH_NUMBER:
1381 case LFUN_MATH_NONUMBER:
1382 case LFUN_MATH_LIMITS:
1384 setErrorMessage(N_("This is only allowed in math mode!"));
1388 // passthrough hat and underscore outside mathed:
1389 case LFUN_SUBSCRIPT:
1390 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1392 case LFUN_SUPERSCRIPT:
1393 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1396 case LFUN_DIALOG_SHOW: {
1397 string const name = ev.getArg(0);
1398 string data = trim(ev.argument.substr(name.size()));
1400 if (name == "character") {
1401 data = freefont2string();
1403 owner->getDialogs().show("character", data);
1404 } else if (name == "document")
1405 owner->getDialogs().showDocument();
1406 else if (name == "findreplace")
1407 owner->getDialogs().showSearch();
1408 else if (name == "forks")
1409 owner->getDialogs().showForks();
1410 else if (name == "preamble")
1411 owner->getDialogs().showPreamble();
1412 else if (name == "preferences")
1413 owner->getDialogs().showPreferences();
1414 else if (name == "print")
1415 owner->getDialogs().showPrint();
1416 else if (name == "spellchecker")
1417 owner->getDialogs().showSpellchecker();
1419 owner->getDialogs().show(name, data);
1423 case LFUN_DIALOG_SHOW_NEW_INSET: {
1424 string const & name = argument;
1426 if (name == "bibitem" ||
1428 name == "include" ||
1433 InsetCommandParams p(name);
1434 data = InsetCommandMailer::params2string(name, p);
1435 } else if (name == "citation") {
1436 InsetCommandParams p("cite");
1437 data = InsetCommandMailer::params2string(name, p);
1439 owner->getDialogs().show(name, data, 0);
1443 case LFUN_DIALOG_SHOW_NEXT_INSET: {
1447 case LFUN_DIALOG_UPDATE: {
1448 string const & name = argument;
1449 // Can only update a dialog connected to an existing inset
1450 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1452 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1454 inset->localDispatch(fr);
1455 } else if (name == "paragraph") {
1456 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1461 case LFUN_DIALOG_HIDE:
1462 Dialogs::hide(argument, 0);
1465 case LFUN_DIALOG_DISCONNECT_INSET:
1466 owner->getDialogs().disconnect(argument);
1469 case LFUN_CHILDOPEN:
1471 string const filename =
1472 MakeAbsPath(argument,
1473 owner->buffer()->filePath());
1474 setMessage(N_("Opening child document ") +
1475 MakeDisplayPath(filename) + "...");
1476 view()->savePosition(0);
1477 if (bufferlist.exists(filename))
1478 view()->buffer(bufferlist.getBuffer(filename));
1480 view()->loadLyXFile(filename);
1484 case LFUN_TOGGLECURSORFOLLOW:
1485 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1489 owner->getIntl().KeyMapOn(false);
1492 case LFUN_KMAP_PRIM:
1493 owner->getIntl().KeyMapPrim();
1497 owner->getIntl().KeyMapSec();
1500 case LFUN_KMAP_TOGGLE:
1501 owner->getIntl().ToggleKeyMap();
1506 // argument contains ';'-terminated commands
1507 while (!argument.empty()) {
1509 argument = split(argument, first, ';');
1515 case LFUN_SAVEPREFERENCES:
1517 Path p(user_lyxdir());
1518 lyxrc.write("preferences");
1522 case LFUN_SCREEN_FONT_UPDATE:
1524 // handle the screen font changes.
1525 lyxrc.set_font_norm_type();
1526 lyx_gui::update_fonts();
1527 // We also need to empty the textcache so that
1528 // the buffer will be formatted correctly after
1531 // Of course we should only do the resize and the textcache.clear
1532 // if values really changed...but not very important right now. (Lgb)
1533 // All visible buffers will need resize
1539 case LFUN_SET_COLOR:
1542 string const x11_name = split(argument, lyx_name, ' ');
1543 if (lyx_name.empty() || x11_name.empty()) {
1544 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1549 bool const graphicsbg_changed =
1550 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1551 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1553 if (!lcolor.setColor(lyx_name, x11_name)) {
1555 bformat(_("Set-color \"%1$s\" failed "
1556 "- color is undefined or "
1557 "may not be redefined"), lyx_name));
1561 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1563 if (graphicsbg_changed) {
1564 #ifdef WITH_WARNINGS
1565 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1568 lyx::graphics::GCache & gc =
1569 lyx::graphics::GCache::get();
1570 gc.changeDisplay(true);
1579 owner->message(argument);
1582 case LFUN_FORKS_KILL:
1584 if (!isStrInt(argument))
1587 pid_t const pid = strToInt(argument);
1588 ForkedcallsController & fcc = ForkedcallsController::get();
1593 case LFUN_TOOLTIPS_TOGGLE:
1594 owner->getDialogs().toggleTooltips();
1597 case LFUN_EXTERNAL_EDIT: {
1599 .localDispatch(FuncRequest(view(), action, argument));
1604 // Then if it was none of the above
1605 // Trying the BufferView::pimpl dispatch:
1606 if (!view()->dispatch(ev))
1607 lyxerr << "A truly unknown func ["
1608 << lyxaction.getActionName(ev.action) << "]!"
1615 view()->owner()->updateLayoutChoice();
1617 if (view()->available()) {
1618 view()->fitCursor();
1620 // If we executed a mutating lfun, mark the buffer as dirty
1621 if (!getStatus(ev).disabled()
1622 && !lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)
1623 && !lyxaction.funcHasFlag(ev.action, LyXAction::ReadOnly))
1624 view()->buffer()->markDirty();
1627 sendDispatchMessage(getMessage(), ev, verbose);
1631 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1633 owner->updateMenubar();
1634 owner->updateToolbar();
1636 if (ev.action == LFUN_SELFINSERT || !verbose) {
1637 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1639 owner->message(msg);
1643 string dispatch_msg = msg;
1644 if (!dispatch_msg.empty())
1645 dispatch_msg += ' ';
1647 string comname = lyxaction.getActionName(ev.action);
1649 int pseudoaction = ev.action;
1650 bool argsadded = false;
1652 if (!ev.argument.empty()) {
1653 // the pseudoaction is useful for the bindings
1654 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1656 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1657 pseudoaction = ev.action;
1659 comname += ' ' + ev.argument;
1664 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1666 if (!shortcuts.empty()) {
1667 comname += ": " + shortcuts;
1668 } else if (!argsadded && !ev.argument.empty()) {
1669 comname += ' ' + ev.argument;
1672 if (!comname.empty()) {
1673 comname = rtrim(comname);
1674 dispatch_msg += '(' + comname + ')';
1677 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1678 if (!dispatch_msg.empty())
1679 owner->message(dispatch_msg);
1683 void LyXFunc::setupLocalKeymap()
1685 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1686 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1690 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1692 string initpath = lyxrc.document_path;
1693 string filename(name);
1695 if (view()->available()) {
1696 string const trypath = owner->buffer()->filePath();
1697 // If directory is writeable, use this as default.
1698 if (IsDirWriteable(trypath))
1702 static int newfile_number;
1704 if (filename.empty()) {
1705 filename = AddName(lyxrc.document_path,
1706 "newfile" + tostr(++newfile_number) + ".lyx");
1707 FileInfo fi(filename);
1708 while (bufferlist.exists(filename) || fi.readable()) {
1710 filename = AddName(lyxrc.document_path,
1711 "newfile" + tostr(newfile_number) +
1713 fi.newFile(filename);
1717 // The template stuff
1720 FileDialog fileDlg(_("Select template file"),
1721 LFUN_SELECT_FILE_SYNC,
1722 make_pair(string(_("Documents|#o#O")),
1723 string(lyxrc.document_path)),
1724 make_pair(string(_("Templates|#T#t")),
1725 string(lyxrc.template_path)));
1727 FileDialog::Result result =
1728 fileDlg.open(lyxrc.template_path,
1729 _("*.lyx| LyX Documents (*.lyx)"));
1731 if (result.first == FileDialog::Later)
1734 string const fname = result.second;
1741 view()->newFile(filename, templname, !name.empty());
1745 void LyXFunc::open(string const & fname)
1747 string initpath = lyxrc.document_path;
1749 if (view()->available()) {
1750 string const trypath = owner->buffer()->filePath();
1751 // If directory is writeable, use this as default.
1752 if (IsDirWriteable(trypath))
1758 if (fname.empty()) {
1759 FileDialog fileDlg(_("Select document to open"),
1761 make_pair(string(_("Documents|#o#O")),
1762 string(lyxrc.document_path)),
1763 make_pair(string(_("Examples|#E#e")),
1764 string(AddPath(system_lyxdir(), "examples"))));
1766 FileDialog::Result result =
1767 fileDlg.open(initpath,
1768 _("*.lyx| LyX Documents (*.lyx)"));
1770 if (result.first == FileDialog::Later)
1773 filename = result.second;
1775 // check selected filename
1776 if (filename.empty()) {
1777 owner->message(_("Canceled."));
1783 // get absolute path of file and add ".lyx" to the filename if
1785 string const fullpath = FileSearch(string(), filename, "lyx");
1786 if (!fullpath.empty()) {
1787 filename = fullpath;
1790 string const disp_fn(MakeDisplayPath(filename));
1792 // if the file doesn't exist, let the user create one
1793 FileInfo const f(filename, true);
1795 // the user specifically chose this name. Believe them.
1796 view()->newFile(filename, "", true);
1800 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1803 if (view()->loadLyXFile(filename)) {
1804 str2 = bformat(_("Document %1$s opened."), disp_fn);
1806 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1808 owner->message(str2);
1812 void LyXFunc::doImport(string const & argument)
1815 string filename = split(argument, format, ' ');
1817 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1818 << " file: " << filename << endl;
1820 // need user interaction
1821 if (filename.empty()) {
1822 string initpath = lyxrc.document_path;
1824 if (view()->available()) {
1825 string const trypath = owner->buffer()->filePath();
1826 // If directory is writeable, use this as default.
1827 if (IsDirWriteable(trypath))
1831 string const text = bformat(_("Select %1$s file to import"),
1832 formats.prettyName(format));
1834 FileDialog fileDlg(text,
1836 make_pair(string(_("Documents|#o#O")),
1837 string(lyxrc.document_path)),
1838 make_pair(string(_("Examples|#E#e")),
1839 string(AddPath(system_lyxdir(), "examples"))));
1841 string const extension = "*." + formats.extension(format)
1842 + "| " + formats.prettyName(format)
1843 + " (*." + formats.extension(format) + ')';
1845 FileDialog::Result result = fileDlg.open(initpath,
1848 if (result.first == FileDialog::Later)
1851 filename = result.second;
1853 // check selected filename
1854 if (filename.empty())
1855 owner->message(_("Canceled."));
1858 if (filename.empty())
1861 // get absolute path of file
1862 filename = MakeAbsPath(filename);
1864 string const lyxfile = ChangeExtension(filename, ".lyx");
1866 // Check if the document already is open
1867 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1868 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1869 owner->message(_("Canceled."));
1874 // if the file exists already, and we didn't do
1875 // -i lyx thefile.lyx, warn
1876 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1877 string const file = MakeDisplayPath(lyxfile, 30);
1879 string text = bformat(_("The document %1$s already exists.\n\n"
1880 "Do you want to over-write that document?"), file);
1881 int const ret = Alert::prompt(_("Over-write document?"),
1882 text, 0, 1, _("&Over-write"), _("&Cancel"));
1885 owner->message(_("Canceled."));
1890 Importer::Import(owner, filename, format);
1894 void LyXFunc::closeBuffer()
1896 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1897 if (bufferlist.empty()) {
1898 // need this otherwise SEGV may occur while
1899 // trying to set variables that don't exist
1900 // since there's no current buffer
1901 owner->getDialogs().hideBufferDependent();
1903 view()->buffer(bufferlist.first());
1909 // Each "owner" should have it's own message method. lyxview and
1910 // the minibuffer would use the minibuffer, but lyxserver would
1911 // send an ERROR signal to its client. Alejandro 970603
1912 // This func is bit problematic when it comes to NLS, to make the
1913 // lyx servers client be language indepenent we must not translate
1914 // strings sent to this func.
1915 void LyXFunc::setErrorMessage(string const & m) const
1917 dispatch_buffer = m;
1922 void LyXFunc::setMessage(string const & m) const
1924 dispatch_buffer = m;
1928 void LyXFunc::setStatusMessage(string const & m) const
1934 string const LyXFunc::view_status_message()
1936 // When meta-fake key is pressed, show the key sequence so far + "M-".
1938 return keyseq.print() + "M-";
1941 // Else, when a non-complete key sequence is pressed,
1942 // show the available options.
1943 if (keyseq.length() > 0 && !keyseq.deleted()) {
1944 return keyseq.printOptions();
1947 if (!view()->available())
1948 return _("Welcome to LyX!");
1950 return currentState(view());
1954 BufferView * LyXFunc::view() const
1957 return owner->view().get();
1961 bool LyXFunc::wasMetaKey() const
1963 return (meta_fake_bit != key_modifier::none);