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 "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/insettabular.h"
49 #include "mathed/formulamacro.h"
50 #include "mathed/math_cursor.h"
51 #include "mathed/math_inset.h"
53 #include "frontends/FileDialog.h"
54 #include "frontends/Dialogs.h"
55 #include "frontends/Toolbar.h"
56 #include "frontends/Menubar.h"
57 #include "frontends/Alert.h"
59 #include "graphics/GraphicsCache.h"
61 #include "support/lyxalgo.h"
62 #include "support/LAssert.h"
63 #include "support/filetools.h"
64 #include "support/FileInfo.h"
65 #include "support/forkedcontr.h"
66 #include "support/lstrings.h"
67 #include "support/tostr.h"
68 #include "support/path.h"
69 #include "support/lyxfunctional.h"
85 using std::back_inserter;
86 using namespace bv_funcs;
88 extern BufferList bufferlist;
89 extern LyXServer * lyxserver;
90 extern bool selection_possible;
92 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
95 extern tex_accent_struct get_accent(kb_action action);
97 extern void ShowLatexLog();
100 LyXFunc::LyXFunc(LyXView * o)
103 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
104 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
105 meta_fake_bit(key_modifier::none)
111 LyXText * LyXFunc::TEXT(bool flag = true) const
115 return view()->getLyXText();
120 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
122 if (selecting || TEXT(flag)->selection.mark()) {
123 TEXT(flag)->setSelection();
124 if (!TEXT(flag)->isInInset())
125 view()->toggleToggle();
127 view()->update(TEXT(flag), BufferView::SELECT);
129 view()->switchKeyMap();
133 void LyXFunc::handleKeyFunc(kb_action action)
135 char c = encoded_last_key;
137 if (keyseq.length()) {
141 owner->getIntl().getTransManager()
142 .deadkey(c, get_accent(action).accent, TEXT(false));
143 // Need to clear, in case the minibuffer calls these
146 // copied verbatim from do_accent_char
147 view()->update(TEXT(false), BufferView::SELECT);
148 TEXT(false)->selection.cursor = TEXT(false)->cursor;
152 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
153 key_modifier::state state)
157 if (lyxerr.debugging(Debug::KEY)) {
158 lyxerr << "KeySym is "
159 << keysym->getSymbolName()
163 // Do nothing if we have nothing (JMarc)
164 if (!keysym->isOK()) {
165 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
170 if (keysym->isModifier()) {
171 lyxerr[Debug::KEY] << "isModifier true" << endl;
175 Encoding const * encoding = view()->getEncoding();
177 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
179 // Do a one-deep top-level lookup for
180 // cancel and meta-fake keys. RVDK_PATCH_5
181 cancel_meta_seq.reset();
183 int action = cancel_meta_seq.addkey(keysym, state);
184 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
186 // When not cancel or meta-fake, do the normal lookup.
187 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
188 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
189 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
190 // remove Caps Lock and Mod2 as a modifiers
191 action = keyseq.addkey(keysym, (state | meta_fake_bit));
192 lyxerr[Debug::KEY] << "action now set to ["
193 << action << ']' << endl;
196 // Dont remove this unless you know what you are doing.
197 meta_fake_bit = key_modifier::none;
199 // can this happen now ?
200 if (action == LFUN_NOACTION) {
201 action = LFUN_PREFIX;
204 if (lyxerr.debugging(Debug::KEY)) {
205 lyxerr << "Key [action="
207 << keyseq.print() << ']'
211 // already here we know if it any point in going further
212 // why not return already here if action == -1 and
213 // num_bytes == 0? (Lgb)
215 if (keyseq.length() > 1) {
216 owner->message(keyseq.print());
220 // Maybe user can only reach the key via holding down shift.
221 // Let's see. But only if shift is the only modifier
222 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
223 lyxerr[Debug::KEY] << "Trying without shift" << endl;
224 action = keyseq.addkey(keysym, key_modifier::none);
225 lyxerr[Debug::KEY] << "Action now " << action << endl;
228 if (action == LFUN_UNKNOWN_ACTION) {
229 // Hmm, we didn't match any of the keysequences. See
230 // if it's normal insertable text not already covered
232 if (keysym->isText() && keyseq.length() == 1) {
233 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
234 action = LFUN_SELFINSERT;
236 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
237 owner->message(_("Unknown function."));
242 if (action == LFUN_SELFINSERT) {
243 if (encoded_last_key != 0) {
245 arg += encoded_last_key;
247 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
249 lyxerr[Debug::KEY] << "SelfInsert arg[`"
250 << argument << "']" << endl;
258 FuncStatus LyXFunc::getStatus(int ac) const
260 return getStatus(lyxaction.retrieveActionArg(ac));
264 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
267 Buffer * buf = owner->buffer();
269 if (ev.action == LFUN_NOACTION) {
270 setStatusMessage(N_("Nothing to do"));
271 return flag.disabled(true);
275 case LFUN_UNKNOWN_ACTION:
276 #ifndef HAVE_LIBAIKSAURUS
277 case LFUN_THESAURUS_ENTRY:
282 flag |= lyx_gui::getStatus(ev);
285 if (flag.unknown()) {
286 setStatusMessage(N_("Unknown action"));
290 // the default error message if we disable the command
291 setStatusMessage(N_("Command disabled"));
293 // Check whether we need a buffer
294 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
295 // Yes we need a buffer, do we have one?
298 // Can we use a readonly buffer?
299 if (buf->isReadonly() &&
300 !lyxaction.funcHasFlag(ev.action,
301 LyXAction::ReadOnly)) {
303 setStatusMessage(N_("Document is read-only"));
308 setStatusMessage(N_("Command not allowed with"
309 "out any document open"));
310 return flag.disabled(true);
314 UpdatableInset * tli = view()->theLockingInset();
316 // I would really like to avoid having this switch and rather try to
317 // encode this in the function itself.
318 bool disable = false;
321 disable = !Exporter::IsExportable(buf, "dvi")
322 || lyxrc.print_command == "none";
325 disable = ev.argument != "custom"
326 && !Exporter::IsExportable(buf, ev.argument);
329 disable = buf->undostack.empty();
332 disable = buf->redostack.empty();
337 UpdatableInset * in = tli;
338 if (in->lyxCode() != Inset::TABULAR_CODE) {
339 in = tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
341 if (in && static_cast<InsetTabular*>(in)->hasSelection()) {
346 disable = !mathcursor && !view()->getLyXText()->selection.set();
349 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
352 disable = !Exporter::IsExportable(buf, "program");
355 case LFUN_LAYOUT_CHARACTER:
356 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
359 case LFUN_LAYOUT_TABULAR:
361 || (tli->lyxCode() != Inset::TABULAR_CODE
362 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
366 disable = !changeDepth(view(), TEXT(false), DEC_DEPTH, true);
369 case LFUN_DEPTH_PLUS:
370 disable = !changeDepth(view(), TEXT(false), INC_DEPTH, true);
374 case LFUN_LAYOUT_PARAGRAPH: {
375 Inset * inset = TEXT(false)->cursor.par()->inInset();
376 disable = inset && inset->forceDefaultParagraphs(inset);
380 case LFUN_INSET_OPTARG:
381 disable = (TEXT(false)->cursor.par()->layout()->optionalargs == 0);
384 case LFUN_TABULAR_FEATURE:
387 // FIXME: check temporarily disabled
389 char align = mathcursor->valign();
394 if (ev.argument.empty()) {
398 if (!contains("tcb", ev.argument[0])) {
402 flag.setOnOff(ev.argument[0] == align);
406 char align = mathcursor->halign();
411 if (ev.argument.empty()) {
415 if (!contains("lcr", ev.argument[0])) {
419 flag.setOnOff(ev.argument[0] == align);
422 disable = !mathcursor->halign();
428 //ret.disabled(true);
429 if (tli->lyxCode() == Inset::TABULAR_CODE) {
430 ret = static_cast<InsetTabular *>(tli)
431 ->getStatus(ev.argument);
434 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
435 ret = static_cast<InsetTabular *>
436 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
437 ->getStatus(ev.argument);
444 static InsetTabular inset(*owner->buffer(), 1, 1);
448 ret = inset.getStatus(ev.argument);
449 if (ret.onoff(true) || ret.onoff(false))
450 flag.setOnOff(false);
454 case LFUN_VC_REGISTER:
455 disable = buf->lyxvc.inUse();
457 case LFUN_VC_CHECKIN:
458 disable = !buf->lyxvc.inUse() || buf->isReadonly();
460 case LFUN_VC_CHECKOUT:
461 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
465 case LFUN_VC_HISTORY:
466 disable = !buf->lyxvc.inUse();
468 case LFUN_MENURELOAD:
469 disable = buf->isUnnamed() || buf->isClean();
471 case LFUN_BOOKMARK_GOTO:
473 isSavedPosition(strToUnsignedInt(ev.argument));
475 case LFUN_MERGE_CHANGES:
476 case LFUN_ACCEPT_CHANGE:
477 case LFUN_REJECT_CHANGE:
478 case LFUN_ACCEPT_ALL_CHANGES:
479 case LFUN_REJECT_ALL_CHANGES:
480 disable = !buf->params.tracking_changes;
482 case LFUN_INSET_TOGGLE: {
483 LyXText * lt = view()->getLyXText();
484 disable = !(isEditableInset(lt->getInset())
486 && lt->inset_owner->owner()
487 && lt->inset_owner->owner()->isOpen()));
491 case LFUN_INSET_SETTINGS: {
493 UpdatableInset * inset = view()->theLockingInset();
498 // get the innermost inset
499 inset = inset->getLockingInset();
501 // jump back to owner if an InsetText, so
502 // we get back to the InsetTabular or whatever
503 if (inset->lyxCode() == Inset::TEXT_CODE)
504 inset = static_cast<UpdatableInset*>(inset->owner());
506 Inset::Code code = inset->lyxCode();
508 case Inset::TABULAR_CODE:
509 disable = ev.argument != "tabular";
511 case Inset::ERT_CODE:
512 disable = ev.argument != "ert";
514 case Inset::FLOAT_CODE:
515 disable = ev.argument != "float";
517 case Inset::MINIPAGE_CODE:
518 disable = ev.argument != "minipage";
520 case Inset::WRAP_CODE:
521 disable = ev.argument != "wrap";
530 disable = !IsFileReadable(buf->getLogName().second);
533 case LFUN_MATH_MUTATE:
535 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
536 flag.setOnOff(false);
541 // we just need to be in math mode to enable that
543 case LFUN_MATH_SPACE:
544 case LFUN_MATH_LIMITS:
545 case LFUN_MATH_NONUMBER:
546 case LFUN_MATH_NUMBER:
547 case LFUN_MATH_EXTERN:
548 disable = !mathcursor;
555 // the functions which insert insets
556 Inset::Code code = Inset::NO_CODE;
558 case LFUN_DIALOG_SHOW_NEW_INSET:
559 if (ev.argument == "bibitem")
560 code = Inset::BIBITEM_CODE;
561 else if (ev.argument == "bibtex")
562 code = Inset::BIBTEX_CODE;
563 else if (ev.argument == "citation")
564 code = Inset::CITE_CODE;
565 else if (ev.argument == "ert")
566 code = Inset::ERT_CODE;
567 else if (ev.argument == "external")
568 code = Inset::EXTERNAL_CODE;
569 else if (ev.argument == "float")
570 code = Inset::FLOAT_CODE;
571 else if (ev.argument == "graphics")
572 code = Inset::GRAPHICS_CODE;
573 else if (ev.argument == "include")
574 code = Inset::INCLUDE_CODE;
575 else if (ev.argument == "index")
576 code = Inset::INDEX_CODE;
577 else if (ev.argument == "label")
578 code = Inset::LABEL_CODE;
579 else if (ev.argument == "minipage")
580 code = Inset::MINIPAGE_CODE;
581 else if (ev.argument == "ref")
582 code = Inset::REF_CODE;
583 else if (ev.argument == "toc")
584 code = Inset::TOC_CODE;
585 else if (ev.argument == "url")
586 code = Inset::URL_CODE;
587 else if (ev.argument == "wrap")
588 code = Inset::WRAP_CODE;
592 code = Inset::ERT_CODE;
594 case LFUN_INSET_FOOTNOTE:
595 code = Inset::FOOT_CODE;
597 case LFUN_TABULAR_INSERT:
598 code = Inset::TABULAR_CODE;
600 case LFUN_INSET_MARGINAL:
601 code = Inset::MARGIN_CODE;
603 case LFUN_INSET_MINIPAGE:
604 code = Inset::MINIPAGE_CODE;
606 case LFUN_INSET_FLOAT:
607 case LFUN_INSET_WIDE_FLOAT:
608 code = Inset::FLOAT_CODE;
610 case LFUN_INSET_WRAP:
611 code = Inset::WRAP_CODE;
613 case LFUN_FLOAT_LIST:
614 code = Inset::FLOAT_LIST_CODE;
617 case LFUN_INSET_LIST:
618 code = Inset::LIST_CODE;
620 case LFUN_INSET_THEOREM:
621 code = Inset::THEOREM_CODE;
624 case LFUN_INSET_CAPTION:
625 code = Inset::CAPTION_CODE;
627 case LFUN_INSERT_NOTE:
628 code = Inset::NOTE_CODE;
630 case LFUN_INSERT_LABEL:
631 code = Inset::LABEL_CODE;
633 case LFUN_INSET_OPTARG:
634 code = Inset::OPTARG_CODE;
636 case LFUN_ENVIRONMENT_INSERT:
637 code = Inset::MINIPAGE_CODE;
639 case LFUN_INDEX_INSERT:
640 code = Inset::INDEX_CODE;
642 case LFUN_INDEX_PRINT:
643 code = Inset::INDEX_PRINT_CODE;
645 case LFUN_TOC_INSERT:
646 code = Inset::TOC_CODE;
648 case LFUN_PARENTINSERT:
649 code = Inset::PARENT_CODE;
653 code = Inset::URL_CODE;
656 // always allow this, since we will inset a raw quote
657 // if an inset is not allowed.
659 case LFUN_HYPHENATION:
660 case LFUN_LIGATURE_BREAK:
662 case LFUN_MENU_SEPARATOR:
664 case LFUN_END_OF_SENTENCE:
665 code = Inset::SPECIALCHAR_CODE;
667 case LFUN_SPACE_INSERT:
668 // slight hack: we know this is allowed in math mode
670 code = Inset::SPACE_CODE;
675 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
681 // A few general toggles
683 case LFUN_TOOLTIPS_TOGGLE:
684 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
687 case LFUN_READ_ONLY_TOGGLE:
688 flag.setOnOff(buf->isReadonly());
691 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
693 case LFUN_SWITCHBUFFER:
694 // toggle on the current buffer, but do not toggle off
695 // the other ones (is that a good idea?)
696 if (ev.argument == buf->fileName())
699 case LFUN_TRACK_CHANGES:
700 flag.setOnOff(buf->params.tracking_changes);
706 // the font related toggles
708 LyXFont const & font = TEXT(false)->real_current_font;
711 flag.setOnOff(font.emph() == LyXFont::ON);
714 flag.setOnOff(font.noun() == LyXFont::ON);
717 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
720 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
723 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
726 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
732 string tc = mathcursor->getLastCode();
735 flag.setOnOff(tc == "mathbf");
738 flag.setOnOff(tc == "mathsf");
741 flag.setOnOff(tc == "mathcal");
744 flag.setOnOff(tc == "mathrm");
747 flag.setOnOff(tc == "mathtt");
750 flag.setOnOff(tc == "mathbb");
753 flag.setOnOff(tc == "mathnormal");
760 // this one is difficult to get right. As a half-baked
761 // solution, we consider only the first action of the sequence
762 if (ev.action == LFUN_SEQUENCE) {
763 // argument contains ';'-terminated commands
764 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
771 void LyXFunc::dispatch(string const & s, bool verbose)
773 int const action = lyxaction.LookupFunc(s);
775 if (action == LFUN_UNKNOWN_ACTION) {
776 owner->message(bformat(_("Unknown function (%1$s)"), s));
780 dispatch(action, verbose);
784 void LyXFunc::dispatch(int ac, bool verbose)
786 dispatch(lyxaction.retrieveActionArg(ac), verbose);
791 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
793 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
794 <<"] arg[" << ev.argument << ']' << endl;
796 // we have not done anything wrong yet.
798 dispatch_buffer.erase();
800 #ifdef NEW_DISPATCHER
801 // We try do call the most specific dispatcher first:
802 // 1. the lockinginset's dispatch
803 // 2. the bufferview's dispatch
804 // 3. the lyxview's dispatch
807 selection_possible = false;
809 string argument = ev.argument;
810 kb_action action = ev.action;
812 // We cannot use this function here
813 if (getStatus(ev).disabled()) {
814 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
815 << lyxaction.getActionName(action)
816 << " [" << action << "] is disabled at this location"
818 setErrorMessage(getStatusMessage());
819 goto exit_with_message;
822 if (view()->available())
823 view()->hideCursor();
825 if (view()->available() && view()->theLockingInset()) {
826 Inset::RESULT result;
827 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
828 (!keyseq.deleted())))
830 UpdatableInset * inset = view()->theLockingInset();
834 inset->getCursorPos(view(), inset_x, dummy_y);
836 if ((action == LFUN_UNKNOWN_ACTION)
837 && argument.empty()) {
838 argument = encoded_last_key;
841 // the insets can't try to handle this,
842 // a table cell in the dummy position will
843 // lock its insettext, the insettext will
844 // pass it the bufferview, and succeed,
845 // so it will stay not locked. Not good
846 // if we've just done LFUN_ESCAPE (which
847 // injects an LFUN_PARAGRAPH_UPDATE)
848 if (action == LFUN_PARAGRAPH_UPDATE) {
849 view()->dispatch(ev);
850 goto exit_with_message;
853 // Undo/Redo is a bit tricky for insets.
854 if (action == LFUN_UNDO) {
856 goto exit_with_message;
857 } else if (action == LFUN_REDO) {
859 goto exit_with_message;
860 } else if (((result=inset->
861 // Hand-over to inset's own dispatch:
862 localDispatch(FuncRequest(view(), action, argument))) ==
864 (result == DISPATCHED_NOUPDATE))
865 goto exit_with_message;
866 // If UNDISPATCHED, just soldier on
867 else if (result == FINISHED) {
868 owner->clearMessage();
869 goto exit_with_message;
870 // We do not need special RTL handling here:
871 // FINISHED means that the cursor should be
872 // one position after the inset.
873 } else if (result == FINISHED_RIGHT) {
874 TEXT()->cursorRight(view());
875 moveCursorUpdate(true, false);
876 owner->clearMessage();
877 goto exit_with_message;
878 } else if (result == FINISHED_UP) {
879 if (TEXT()->cursor.irow() != TEXT()->rows().begin()) {
881 TEXT()->setCursorFromCoordinates(
882 TEXT()->cursor.ix() + inset_x,
883 TEXT()->cursor.iy() -
884 TEXT()->cursor.irow()->baseline() - 1);
885 TEXT()->cursor.x_fix(TEXT()->cursor.x());
887 TEXT()->cursorUp(view());
889 moveCursorUpdate(true, false);
891 view()->update(TEXT(), BufferView::SELECT);
893 owner->clearMessage();
894 goto exit_with_message;
895 } else if (result == FINISHED_DOWN) {
896 if (boost::next(TEXT()->cursor.irow()) != TEXT()->rows().end()) {
898 TEXT()->setCursorFromCoordinates(
899 TEXT()->cursor.ix() + inset_x,
900 TEXT()->cursor.iy() -
901 TEXT()->cursor.irow()->baseline() +
902 TEXT()->cursor.irow()->height() + 1);
903 TEXT()->cursor.x_fix(TEXT()->cursor.x());
905 TEXT()->cursorDown(view());
908 TEXT()->cursorRight(view());
910 moveCursorUpdate(true, false);
911 owner->clearMessage();
912 goto exit_with_message;
914 #warning I am not sure this is still right, please have a look! (Jug 20020417)
915 else { // result == UNDISPATCHED
916 //setMessage(N_("Text mode"));
918 case LFUN_UNKNOWN_ACTION:
919 case LFUN_BREAKPARAGRAPH:
921 TEXT()->cursorRight(view());
922 view()->switchKeyMap();
923 owner->view_state_changed();
926 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
927 TEXT()->cursorRight(view());
928 moveCursorUpdate(true, false);
929 owner->view_state_changed();
931 goto exit_with_message;
933 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
934 TEXT()->cursorRight(view());
935 moveCursorUpdate(true, false);
936 owner->view_state_changed();
938 goto exit_with_message;
940 if (boost::next(TEXT()->cursor.row()) != TEXT()->rows().end())
941 TEXT()->cursorDown(view());
943 TEXT()->cursorRight(view());
944 moveCursorUpdate(true, false);
945 owner->view_state_changed();
946 goto exit_with_message;
958 if (!view()->available()) break;
959 // this function should be used always [asierra060396]
960 UpdatableInset * tli =
961 view()->theLockingInset();
963 UpdatableInset * lock = tli->getLockingInset();
966 view()->unlockInset(tli);
967 TEXT()->cursorRight(view());
968 moveCursorUpdate(true, false);
969 owner->view_state_changed();
971 tli->unlockInsetInInset(view(),
976 // Tell the paragraph dialog that we changed paragraph
977 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
982 // --- Misc -------------------------------------------
983 case LFUN_WORDFINDFORWARD :
984 case LFUN_WORDFINDBACKWARD : {
985 static string last_search;
986 string searched_string;
988 if (!argument.empty()) {
989 last_search = argument;
990 searched_string = argument;
992 searched_string = last_search;
994 bool fw = (action == LFUN_WORDFINDFORWARD);
995 if (!searched_string.empty()) {
996 lyxfind::LyXFind(view(), searched_string, fw);
1003 if (view()->available() && !view()->theLockingInset()) {
1004 view()->update(TEXT(), BufferView::SELECT);
1006 owner->message(keyseq.printOptions());
1010 // --- Misc -------------------------------------------
1011 case LFUN_EXEC_COMMAND:
1012 owner->focus_command_buffer();
1015 case LFUN_CANCEL: // RVDK_PATCH_5
1017 meta_fake_bit = key_modifier::none;
1018 if (view()->available())
1019 // cancel any selection
1020 dispatch(LFUN_MARK_OFF);
1021 setMessage(N_("Cancel"));
1024 case LFUN_META_FAKE: // RVDK_PATCH_5
1026 meta_fake_bit = key_modifier::alt;
1027 setMessage(keyseq.print());
1031 case LFUN_READ_ONLY_TOGGLE:
1032 if (owner->buffer()->lyxvc.inUse()) {
1033 owner->buffer()->lyxvc.toggleReadOnly();
1035 owner->buffer()->setReadonly(
1036 !owner->buffer()->isReadonly());
1040 case LFUN_CENTER: // this is center and redraw.
1044 // --- Menus -----------------------------------------------
1046 menuNew(argument, false);
1049 case LFUN_MENUNEWTMPLT:
1050 menuNew(argument, true);
1053 case LFUN_CLOSEBUFFER:
1057 case LFUN_MENUWRITE:
1058 if (!owner->buffer()->isUnnamed()) {
1059 string const str = bformat(_("Saving document %1$s..."),
1060 MakeDisplayPath(owner->buffer()->fileName()));
1061 owner->message(str);
1062 MenuWrite(view(), owner->buffer());
1063 owner->message(str + _(" done."));
1065 WriteAs(view(), owner->buffer());
1069 WriteAs(view(), owner->buffer(), argument);
1072 case LFUN_MENURELOAD: {
1073 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
1074 string text = bformat(_("Any changes will be lost. Are you sure "
1075 "you want to revert to the saved version of the document %1$s?"), file);
1076 int const ret = Alert::prompt(_("Revert to saved document?"),
1077 text, 0, 1, _("&Revert"), _("&Cancel"));
1085 Exporter::Export(owner->buffer(), argument, true);
1089 Exporter::Preview(owner->buffer(), argument);
1092 case LFUN_BUILDPROG:
1093 Exporter::Export(owner->buffer(), "program", true);
1096 case LFUN_RUNCHKTEX:
1097 owner->buffer()->runChktex();
1100 case LFUN_MENUPRINT:
1101 owner->getDialogs().showPrint();
1105 if (argument == "custom")
1106 owner->getDialogs().showSendto();
1108 Exporter::Export(owner->buffer(), argument, false);
1121 InsetCommandParams p("tableofcontents");
1122 string const data = InsetCommandMailer::params2string("toc", p);
1123 owner->getDialogs().show("toc", data, 0);
1139 case LFUN_MENUSEARCH:
1140 owner->getDialogs().showSearch();
1143 case LFUN_DEPTH_MIN:
1144 changeDepth(view(), TEXT(false), DEC_DEPTH, false);
1145 owner->view_state_changed();
1148 case LFUN_DEPTH_PLUS:
1149 changeDepth(view(), TEXT(false), INC_DEPTH, false);
1150 owner->view_state_changed();
1153 case LFUN_FREEFONT_APPLY:
1154 apply_freefont(view());
1157 case LFUN_FREEFONT_UPDATE:
1158 update_and_apply_freefont(view(), argument);
1161 case LFUN_RECONFIGURE:
1162 Reconfigure(view());
1166 case LFUN_FLOATSOPERATE:
1167 if (argument == "openfoot")
1168 view()->allFloats(1,0);
1169 else if (argument == "closefoot")
1170 view()->allFloats(0,0);
1171 else if (argument == "openfig")
1172 view()->allFloats(1,1);
1173 else if (argument == "closefig")
1174 view()->allFloats(0,1);
1177 #ifdef WITH_WARNINGS
1178 //#warning Find another implementation here (or another lyxfunc)!
1181 case LFUN_HELP_ABOUTLYX:
1182 owner->getDialogs().show("about");
1185 case LFUN_HELP_TEXINFO:
1186 owner->getDialogs().showTexinfo();
1189 case LFUN_HELP_OPEN:
1191 string const arg = argument;
1193 setErrorMessage(N_("Missing argument"));
1196 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1197 if (fname.empty()) {
1198 lyxerr << "LyX: unable to find documentation file `"
1199 << arg << "'. Bad installation?" << endl;
1202 owner->message(bformat(_("Opening help file %1$s..."),
1203 MakeDisplayPath(fname)));
1204 view()->buffer(bufferlist.loadLyXFile(fname, false));
1208 // --- version control -------------------------------
1209 case LFUN_VC_REGISTER:
1211 if (!owner->buffer()->lyxvc.inUse())
1212 owner->buffer()->lyxvc.registrer();
1216 case LFUN_VC_CHECKIN:
1218 if (owner->buffer()->lyxvc.inUse()
1219 && !owner->buffer()->isReadonly())
1220 owner->buffer()->lyxvc.checkIn();
1224 case LFUN_VC_CHECKOUT:
1226 if (owner->buffer()->lyxvc.inUse()
1227 && owner->buffer()->isReadonly())
1228 owner->buffer()->lyxvc.checkOut();
1232 case LFUN_VC_REVERT:
1234 owner->buffer()->lyxvc.revert();
1240 owner->buffer()->lyxvc.undoLast();
1244 case LFUN_VC_HISTORY:
1246 owner->getDialogs().show("vclog");
1250 // --- buffers ----------------------------------------
1252 case LFUN_SWITCHBUFFER:
1253 view()->buffer(bufferlist.getBuffer(argument));
1258 // servercmd: argument must be <file>:<template>
1259 Buffer * tmpbuf = NewFile(argument);
1261 view()->buffer(tmpbuf);
1265 case LFUN_FILE_OPEN:
1269 case LFUN_LATEX_LOG:
1270 owner->getDialogs().show("log");
1273 case LFUN_LAYOUT_DOCUMENT:
1274 owner->getDialogs().showDocument();
1277 case LFUN_LAYOUT_CHARACTER: {
1278 string data = freefont2string();
1280 owner->getDialogs().show("character", data);
1284 case LFUN_LAYOUT_TABULAR:
1285 if (view()->theLockingInset()) {
1286 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1287 InsetTabular * inset = static_cast<InsetTabular *>
1288 (view()->theLockingInset());
1289 inset->openLayoutDialog(view());
1290 } else if (view()->theLockingInset()->
1291 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1292 InsetTabular * inset = static_cast<InsetTabular *>(
1293 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1294 inset->openLayoutDialog(view());
1299 case LFUN_LAYOUT_PREAMBLE:
1300 owner->getDialogs().showPreamble();
1303 case LFUN_DROP_LAYOUTS_CHOICE:
1304 owner->getToolbar().openLayoutList();
1307 case LFUN_MENU_OPEN_BY_NAME:
1308 owner->getMenubar().openByName(argument);
1309 break; // RVDK_PATCH_5
1311 case LFUN_SPELLCHECK:
1312 owner->getDialogs().showSpellchecker();
1315 // --- lyxserver commands ----------------------------
1319 setMessage(owner->buffer()->fileName());
1320 lyxerr[Debug::INFO] << "FNAME["
1321 << owner->buffer()->fileName()
1327 dispatch_buffer = keyseq.print();
1328 lyxserver->notifyClient(dispatch_buffer);
1332 case LFUN_GOTOFILEROW:
1336 istringstream istr(argument.c_str());
1337 istr >> file_name >> row;
1338 // Must replace extension of the file to be .lyx and get full path
1339 string const s(ChangeExtension(file_name, ".lyx"));
1341 // Either change buffer or load the file
1342 if (bufferlist.exists(s)) {
1343 view()->buffer(bufferlist.getBuffer(s));
1345 view()->buffer(bufferlist.loadLyXFile(s));
1348 view()->setCursorFromRow(row);
1351 // see BufferView_pimpl::center()
1352 view()->updateScrollbar();
1356 case LFUN_GOTO_PARAGRAPH:
1358 istringstream istr(argument.c_str());
1362 ParIterator par = owner->buffer()->getParFromID(id);
1363 if (par == owner->buffer()->par_iterator_end()) {
1364 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1365 << id << ']' << endl;
1368 lyxerr[Debug::INFO] << "Paragraph " << (*par)->id()
1369 << " found." << endl;
1372 if (view()->theLockingInset())
1373 view()->unlockInset(view()->theLockingInset());
1374 if ((*par)->inInset()) {
1375 FuncRequest cmd(view(), LFUN_INSET_EDIT, "left");
1376 (*par)->inInset()->localDispatch(cmd);
1379 view()->getLyXText()->setCursor(*par, 0);
1380 view()->switchKeyMap();
1381 owner->view_state_changed();
1384 // see BufferView_pimpl::center()
1385 view()->updateScrollbar();
1389 // --- insert characters ----------------------------------------
1391 // --- Mathed stuff. If we are here, there is no locked inset yet.
1392 case LFUN_MATH_EXTERN:
1393 case LFUN_MATH_NUMBER:
1394 case LFUN_MATH_NONUMBER:
1395 case LFUN_MATH_LIMITS:
1397 setErrorMessage(N_("This is only allowed in math mode!"));
1401 // passthrough hat and underscore outside mathed:
1402 case LFUN_SUBSCRIPT:
1403 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1405 case LFUN_SUPERSCRIPT:
1406 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1409 case LFUN_MATH_PANEL:
1410 owner->getDialogs().showMathPanel();
1413 case LFUN_DIALOG_SHOW_NEW_INSET: {
1414 string const & name = argument;
1416 if (name == "bibitem" ||
1418 name == "include" ||
1423 InsetCommandParams p(name);
1424 data = InsetCommandMailer::params2string(name, p);
1425 } else if (name == "citation") {
1426 InsetCommandParams p("cite");
1427 data = InsetCommandMailer::params2string(name, p);
1429 owner->getDialogs().show(name, data, 0);
1433 case LFUN_DIALOG_SHOW_NEXT_INSET: {
1437 case LFUN_DIALOG_UPDATE: {
1438 string const & name = argument;
1439 // Can only update a dialog connected to an existing inset
1440 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1442 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1444 inset->localDispatch(fr);
1445 } else if (name == "paragraph") {
1446 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1451 case LFUN_DIALOG_HIDE:
1452 Dialogs::hide(argument, 0);
1455 case LFUN_DIALOG_DISCONNECT_INSET:
1456 owner->getDialogs().disconnect(argument);
1459 case LFUN_CHILDOPEN:
1461 string const filename =
1462 MakeAbsPath(argument,
1463 owner->buffer()->filePath());
1464 setMessage(N_("Opening child document ") +
1465 MakeDisplayPath(filename) + "...");
1466 view()->savePosition(0);
1467 if (bufferlist.exists(filename))
1468 view()->buffer(bufferlist.getBuffer(filename));
1470 view()->buffer(bufferlist.loadLyXFile(filename));
1474 case LFUN_TOGGLECURSORFOLLOW:
1475 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1479 owner->getIntl().KeyMapOn(false);
1482 case LFUN_KMAP_PRIM:
1483 owner->getIntl().KeyMapPrim();
1487 owner->getIntl().KeyMapSec();
1490 case LFUN_KMAP_TOGGLE:
1491 owner->getIntl().ToggleKeyMap();
1496 // argument contains ';'-terminated commands
1497 while (!argument.empty()) {
1499 argument = split(argument, first, ';');
1505 case LFUN_DIALOG_PREFERENCES:
1506 owner->getDialogs().showPreferences();
1509 case LFUN_SAVEPREFERENCES:
1511 Path p(user_lyxdir);
1512 lyxrc.write("preferences");
1516 case LFUN_SCREEN_FONT_UPDATE:
1518 // handle the screen font changes.
1519 lyxrc.set_font_norm_type();
1520 lyx_gui::update_fonts();
1521 // We also need to empty the textcache so that
1522 // the buffer will be formatted correctly after
1525 // Of course we should only do the resize and the textcache.clear
1526 // if values really changed...but not very important right now. (Lgb)
1527 // All visible buffers will need resize
1533 case LFUN_SET_COLOR:
1536 string const x11_name = split(argument, lyx_name, ' ');
1537 if (lyx_name.empty() || x11_name.empty()) {
1538 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1543 bool const graphicsbg_changed =
1544 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1545 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1547 if (!lcolor.setColor(lyx_name, x11_name)) {
1549 bformat(_("Set-color \"%1$s\" failed "
1550 "- color is undefined or "
1551 "may not be redefined"), lyx_name));
1555 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1557 if (graphicsbg_changed) {
1558 #ifdef WITH_WARNINGS
1559 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1562 grfx::GCache & gc = grfx::GCache::get();
1563 gc.changeDisplay(true);
1572 owner->message(argument);
1575 case LFUN_FORKS_SHOW:
1576 owner->getDialogs().showForks();
1579 case LFUN_FORKS_KILL:
1581 if (!isStrInt(argument))
1584 pid_t const pid = strToInt(argument);
1585 ForkedcallsController & fcc = ForkedcallsController::get();
1590 case LFUN_TOOLTIPS_TOGGLE:
1591 owner->getDialogs().toggleTooltips();
1595 // Then if it was none of the above
1596 // Trying the BufferView::pimpl dispatch:
1597 if (!view()->dispatch(ev))
1598 lyxerr << "A truly unknown func ["
1599 << lyxaction.getActionName(ev.action) << "]!"
1606 view()->owner()->updateLayoutChoice();
1608 if (view()->available()) {
1609 view()->fitCursor();
1611 // If we executed a mutating lfun, mark the buffer as dirty
1612 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)
1613 && !lyxaction.funcHasFlag(ev.action, LyXAction::ReadOnly))
1614 view()->buffer()->markDirty();
1617 sendDispatchMessage(getMessage(), ev, verbose);
1621 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1623 owner->updateMenubar();
1624 owner->updateToolbar();
1626 if (ev.action == LFUN_SELFINSERT || !verbose) {
1627 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1629 owner->message(msg);
1633 string dispatch_msg = msg;
1634 if (!dispatch_msg.empty())
1635 dispatch_msg += ' ';
1637 string comname = lyxaction.getActionName(ev.action);
1639 int pseudoaction = ev.action;
1640 bool argsadded = false;
1642 if (!ev.argument.empty()) {
1643 // the pseudoaction is useful for the bindings
1644 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1646 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1647 pseudoaction = ev.action;
1649 comname += ' ' + ev.argument;
1654 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1656 if (!shortcuts.empty()) {
1657 comname += ": " + shortcuts;
1658 } else if (!argsadded && !ev.argument.empty()) {
1659 comname += ' ' + ev.argument;
1662 if (!comname.empty()) {
1663 comname = rtrim(comname);
1664 dispatch_msg += '(' + comname + ')';
1667 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1668 if (!dispatch_msg.empty())
1669 owner->message(dispatch_msg);
1673 void LyXFunc::setupLocalKeymap()
1675 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1676 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1680 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1682 string initpath = lyxrc.document_path;
1683 string filename(name);
1685 if (view()->available()) {
1686 string const trypath = owner->buffer()->filePath();
1687 // If directory is writeable, use this as default.
1688 if (IsDirWriteable(trypath))
1692 static int newfile_number;
1694 if (filename.empty()) {
1695 filename = AddName(lyxrc.document_path,
1696 "newfile" + tostr(++newfile_number) + ".lyx");
1697 FileInfo fi(filename);
1698 while (bufferlist.exists(filename) || fi.readable()) {
1700 filename = AddName(lyxrc.document_path,
1701 "newfile" + tostr(newfile_number) +
1703 fi.newFile(filename);
1707 // The template stuff
1710 FileDialog fileDlg(_("Select template file"),
1711 LFUN_SELECT_FILE_SYNC,
1712 make_pair(string(_("Documents|#o#O")),
1713 string(lyxrc.document_path)),
1714 make_pair(string(_("Templates|#T#t")),
1715 string(lyxrc.template_path)));
1717 FileDialog::Result result =
1718 fileDlg.open(lyxrc.template_path,
1719 _("*.lyx| LyX Documents (*.lyx)"));
1721 if (result.first == FileDialog::Later)
1724 string const fname = result.second;
1731 view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1735 void LyXFunc::open(string const & fname)
1737 string initpath = lyxrc.document_path;
1739 if (view()->available()) {
1740 string const trypath = owner->buffer()->filePath();
1741 // If directory is writeable, use this as default.
1742 if (IsDirWriteable(trypath))
1748 if (fname.empty()) {
1749 FileDialog fileDlg(_("Select document to open"),
1751 make_pair(string(_("Documents|#o#O")),
1752 string(lyxrc.document_path)),
1753 make_pair(string(_("Examples|#E#e")),
1754 string(AddPath(system_lyxdir, "examples"))));
1756 FileDialog::Result result =
1757 fileDlg.open(initpath,
1758 _("*.lyx| LyX Documents (*.lyx)"));
1760 if (result.first == FileDialog::Later)
1763 filename = result.second;
1765 // check selected filename
1766 if (filename.empty()) {
1767 owner->message(_("Canceled."));
1773 // get absolute path of file and add ".lyx" to the filename if
1775 string const fullpath = FileSearch(string(), filename, "lyx");
1776 if (!fullpath.empty()) {
1777 filename = fullpath;
1780 string const disp_fn(MakeDisplayPath(filename));
1782 // if the file doesn't exist, let the user create one
1783 FileInfo const f(filename, true);
1785 // the user specifically chose this name. Believe them.
1786 Buffer * buffer = bufferlist.newFile(filename, "", true);
1787 view()->buffer(buffer);
1791 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1793 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1796 view()->buffer(openbuf);
1797 str2 = bformat(_("Document %1$s opened."), disp_fn);
1799 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1801 owner->message(str2);
1805 void LyXFunc::doImport(string const & argument)
1808 string filename = split(argument, format, ' ');
1810 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1811 << " file: " << filename << endl;
1813 // need user interaction
1814 if (filename.empty()) {
1815 string initpath = lyxrc.document_path;
1817 if (view()->available()) {
1818 string const trypath = owner->buffer()->filePath();
1819 // If directory is writeable, use this as default.
1820 if (IsDirWriteable(trypath))
1824 string const text = bformat(_("Select %1$s file to import"),
1825 formats.prettyName(format));
1827 FileDialog fileDlg(text,
1829 make_pair(string(_("Documents|#o#O")),
1830 string(lyxrc.document_path)),
1831 make_pair(string(_("Examples|#E#e")),
1832 string(AddPath(system_lyxdir, "examples"))));
1834 string const extension = "*." + formats.extension(format)
1835 + "| " + formats.prettyName(format)
1836 + " (*." + formats.extension(format) + ')';
1838 FileDialog::Result result = fileDlg.open(initpath,
1841 if (result.first == FileDialog::Later)
1844 filename = result.second;
1846 // check selected filename
1847 if (filename.empty())
1848 owner->message(_("Canceled."));
1851 if (filename.empty())
1854 // get absolute path of file
1855 filename = MakeAbsPath(filename);
1857 string const lyxfile = ChangeExtension(filename, ".lyx");
1859 // Check if the document already is open
1860 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1861 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1862 owner->message(_("Canceled."));
1867 // if the file exists already, and we didn't do
1868 // -i lyx thefile.lyx, warn
1869 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1870 string const file = MakeDisplayPath(lyxfile, 30);
1872 string text = bformat(_("The document %1$s already exists.\n\n"
1873 "Do you want to over-write that document?"), file);
1874 int const ret = Alert::prompt(_("Over-write document?"),
1875 text, 0, 1, _("&Over-write"), _("&Cancel"));
1878 owner->message(_("Canceled."));
1883 Importer::Import(owner, filename, format);
1887 void LyXFunc::closeBuffer()
1889 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1890 if (bufferlist.empty()) {
1891 // need this otherwise SEGV may occur while trying to
1892 // set variables that don't exist
1893 // since there's no current buffer
1894 owner->getDialogs().hideBufferDependent();
1896 view()->buffer(bufferlist.first());
1902 // Each "owner" should have it's own message method. lyxview and
1903 // the minibuffer would use the minibuffer, but lyxserver would
1904 // send an ERROR signal to its client. Alejandro 970603
1905 // This func is bit problematic when it comes to NLS, to make the
1906 // lyx servers client be language indepenent we must not translate
1907 // strings sent to this func.
1908 void LyXFunc::setErrorMessage(string const & m) const
1910 dispatch_buffer = m;
1915 void LyXFunc::setMessage(string const & m) const
1917 dispatch_buffer = m;
1921 void LyXFunc::setStatusMessage(string const & m) const
1927 string const LyXFunc::view_status_message()
1929 // When meta-fake key is pressed, show the key sequence so far + "M-".
1931 return keyseq.print() + "M-";
1934 // Else, when a non-complete key sequence is pressed,
1935 // show the available options.
1936 if (keyseq.length() > 0 && !keyseq.deleted()) {
1937 return keyseq.printOptions();
1940 if (!view()->available())
1941 return _("Welcome to LyX!");
1943 return currentState(view());
1947 BufferView * LyXFunc::view() const
1950 return owner->view().get();