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"
18 #include "BufferView.h"
19 #include "lyxserver.h"
23 #include "LyXAction.h"
29 #include "trans_mgr.h"
32 #include "bufferview_funcs.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/lyx_gui.h"
36 #include "FloatList.h"
40 #include "TextCache.h"
42 #include "undo_funcs.h"
43 #include "ParagraphParameters.h"
45 #include "insets/insetcommand.h"
46 #include "insets/inseterror.h"
47 #include "insets/insetert.h"
48 #include "insets/insettabular.h"
50 #include "mathed/formulamacro.h"
51 #include "mathed/math_cursor.h"
52 #include "mathed/math_inset.h"
54 #include "frontends/FileDialog.h"
55 #include "frontends/Dialogs.h"
56 #include "frontends/Toolbar.h"
57 #include "frontends/Menubar.h"
58 #include "frontends/Alert.h"
60 #include "graphics/GraphicsCache.h"
62 #include "support/lyxalgo.h"
63 #include "support/LAssert.h"
64 #include "support/filetools.h"
65 #include "support/FileInfo.h"
66 #include "support/forkedcontr.h"
67 #include "support/lstrings.h"
68 #include "support/path.h"
69 #include "support/lyxfunctional.h"
71 #include "BoostFormat.h"
87 using std::back_inserter;
89 extern BufferList bufferlist;
90 extern LyXServer * lyxserver;
91 extern bool selection_possible;
93 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
96 extern tex_accent_struct get_accent(kb_action action);
98 extern void ShowLatexLog();
101 LyXFunc::LyXFunc(LyXView * o)
104 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
105 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
106 meta_fake_bit(key_modifier::none)
112 LyXText * LyXFunc::TEXT(bool flag = true) const
116 return view()->getLyXText();
121 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
123 if (selecting || TEXT(flag)->selection.mark()) {
124 TEXT(flag)->setSelection(view());
125 if (TEXT(flag)->isTopLevel())
126 view()->toggleToggle();
128 view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
129 view()->showCursor();
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),
150 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
151 TEXT(false)->selection.cursor = TEXT(false)->cursor;
155 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
156 key_modifier::state state)
160 if (lyxerr.debugging(Debug::KEY)) {
161 lyxerr << "KeySym is "
162 << keysym->getSymbolName()
166 // Do nothing if we have nothing (JMarc)
167 if (!keysym->isOK()) {
168 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
173 if (keysym->isModifier()) {
174 lyxerr[Debug::KEY] << "isModifier true" << endl;
178 Encoding const * encoding = view()->getEncoding();
180 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
182 // Do a one-deep top-level lookup for
183 // cancel and meta-fake keys. RVDK_PATCH_5
184 cancel_meta_seq.reset();
186 int action = cancel_meta_seq.addkey(keysym, state);
187 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
189 // When not cancel or meta-fake, do the normal lookup.
190 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
191 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
192 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
193 // remove Caps Lock and Mod2 as a modifiers
194 action = keyseq.addkey(keysym, (state | meta_fake_bit));
195 lyxerr[Debug::KEY] << "action now set to ["
196 << action << ']' << endl;
199 // Dont remove this unless you know what you are doing.
200 meta_fake_bit = key_modifier::none;
202 // can this happen now ?
203 if (action == LFUN_NOACTION) {
204 action = LFUN_PREFIX;
207 if (lyxerr.debugging(Debug::KEY)) {
208 lyxerr << "Key [action="
210 << keyseq.print() << ']'
214 // already here we know if it any point in going further
215 // why not return already here if action == -1 and
216 // num_bytes == 0? (Lgb)
218 if (keyseq.length() > 1) {
219 owner->message(keyseq.print());
223 // Maybe user can only reach the key via holding down shift.
224 // Let's see. But only if shift is the only modifier
225 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
226 lyxerr[Debug::KEY] << "Trying without shift" << endl;
227 action = keyseq.addkey(keysym, key_modifier::none);
228 lyxerr[Debug::KEY] << "Action now " << action << endl;
231 if (action == LFUN_UNKNOWN_ACTION) {
232 // Hmm, we didn't match any of the keysequences. See
233 // if it's normal insertable text not already covered
235 if (keysym->isText() && keyseq.length() == 1) {
236 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
237 action = LFUN_SELFINSERT;
239 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
240 owner->message(_("Unknown function."));
245 if (action == LFUN_SELFINSERT) {
246 if (encoded_last_key != 0) {
248 arg += encoded_last_key;
250 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
252 lyxerr[Debug::KEY] << "SelfInsert arg[`"
253 << argument << "']" << endl;
261 FuncStatus LyXFunc::getStatus(int ac) const
263 return getStatus(lyxaction.retrieveActionArg(ac));
267 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
270 Buffer * buf = owner->buffer();
272 if (ev.action == LFUN_NOACTION) {
273 setStatusMessage(N_("Nothing to do"));
274 return flag.disabled(true);
278 case LFUN_UNKNOWN_ACTION:
279 #ifndef HAVE_LIBAIKSAURUS
280 case LFUN_THESAURUS_ENTRY:
285 flag |= lyx_gui::getStatus(ev);
288 if (flag.unknown()) {
289 setStatusMessage(N_("Unknown action"));
293 // the default error message if we disable the command
294 setStatusMessage(N_("Command disabled"));
296 // Check whether we need a buffer
297 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
298 // Yes we need a buffer, do we have one?
301 // Can we use a readonly buffer?
302 if (buf->isReadonly() &&
303 !lyxaction.funcHasFlag(ev.action,
304 LyXAction::ReadOnly)) {
306 setStatusMessage(N_("Document is read-only"));
311 setStatusMessage(N_("Command not allowed with"
312 "out any document open"));
313 return flag.disabled(true);
317 UpdatableInset * tli = view()->theLockingInset();
319 // I would really like to avoid having this switch and rather try to
320 // encode this in the function itself.
321 bool disable = false;
324 disable = !Exporter::IsExportable(buf, "dvi")
325 || lyxrc.print_command == "none";
328 disable = ev.argument != "custom"
329 && !Exporter::IsExportable(buf, ev.argument);
332 disable = buf->undostack.empty();
335 disable = buf->redostack.empty();
340 UpdatableInset * in = tli;
341 if (in->lyxCode() != Inset::TABULAR_CODE) {
342 in = tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
344 if (in && static_cast<InsetTabular*>(in)->hasSelection()) {
349 disable = !mathcursor && !view()->getLyXText()->selection.set();
352 disable = lyxrc.chktex_command == "none";
355 disable = !Exporter::IsExportable(buf, "program");
358 case LFUN_LAYOUT_CHARACTER:
359 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
362 case LFUN_LAYOUT_TABULAR:
364 || (tli->lyxCode() != Inset::TABULAR_CODE
365 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
369 case LFUN_LAYOUT_PARAGRAPH: {
370 Inset * inset = TEXT(false)->cursor.par()->inInset();
371 disable = inset && inset->forceDefaultParagraphs(inset);
375 case LFUN_INSET_OPTARG:
376 disable = (TEXT(false)->cursor.par()->layout()->optionalargs == 0);
379 case LFUN_TABULAR_FEATURE:
382 // FIXME: check temporarily disabled
384 char align = mathcursor->valign();
389 if (ev.argument.empty()) {
393 if (!contains("tcb", ev.argument[0])) {
397 flag.setOnOff(ev.argument[0] == align);
401 char align = mathcursor->halign();
406 if (ev.argument.empty()) {
410 if (!contains("lcr", ev.argument[0])) {
414 flag.setOnOff(ev.argument[0] == align);
417 disable = !mathcursor->halign();
423 //ret.disabled(true);
424 if (tli->lyxCode() == Inset::TABULAR_CODE) {
425 ret = static_cast<InsetTabular *>(tli)
426 ->getStatus(ev.argument);
427 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
428 ret = static_cast<InsetTabular *>
429 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
430 ->getStatus(ev.argument);
435 static InsetTabular inset(*owner->buffer(), 1, 1);
439 ret = inset.getStatus(ev.argument);
440 if (ret.onoff(true) || ret.onoff(false))
441 flag.setOnOff(false);
445 case LFUN_VC_REGISTER:
446 disable = buf->lyxvc.inUse();
448 case LFUN_VC_CHECKIN:
449 disable = !buf->lyxvc.inUse() || buf->isReadonly();
451 case LFUN_VC_CHECKOUT:
452 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
456 case LFUN_VC_HISTORY:
457 disable = !buf->lyxvc.inUse();
459 case LFUN_MENURELOAD:
460 disable = buf->isUnnamed() || buf->isClean();
462 case LFUN_BOOKMARK_GOTO:
464 isSavedPosition(strToUnsignedInt(ev.argument));
466 case LFUN_MERGE_CHANGES:
467 case LFUN_ACCEPT_CHANGE:
468 case LFUN_REJECT_CHANGE:
469 case LFUN_ACCEPT_ALL_CHANGES:
470 case LFUN_REJECT_ALL_CHANGES:
471 disable = !buf->params.tracking_changes;
473 case LFUN_INSET_TOGGLE: {
474 LyXText * lt = view()->getLyXText();
475 disable = !(isEditableInset(lt->getInset())
477 && lt->inset_owner->owner()
478 && lt->inset_owner->owner()->isOpen()));
482 disable = !IsFileReadable(buf->getLogName().second);
485 case LFUN_MATH_MUTATE:
487 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
488 flag.setOnOff(false);
493 // we just need to be in math mode to enable that
495 case LFUN_MATH_SPACE:
496 case LFUN_MATH_LIMITS:
497 case LFUN_MATH_NONUMBER:
498 case LFUN_MATH_NUMBER:
499 case LFUN_MATH_EXTERN:
500 disable = !mathcursor;
507 // the functions which insert insets
508 Inset::Code code = Inset::NO_CODE;
510 case LFUN_DIALOG_SHOW_NEW_INSET:
511 if (ev.argument == "bibitem")
512 code = Inset::BIBITEM_CODE;
513 else if (ev.argument == "bibtex")
514 code = Inset::BIBTEX_CODE;
515 else if (ev.argument == "citation")
516 code = Inset::CITE_CODE;
517 else if (ev.argument == "ert")
518 code = Inset::ERT_CODE;
519 else if (ev.argument == "external")
520 code = Inset::EXTERNAL_CODE;
521 else if (ev.argument == "float")
522 code = Inset::FLOAT_CODE;
523 else if (ev.argument == "graphics")
524 code = Inset::GRAPHICS_CODE;
525 else if (ev.argument == "include")
526 code = Inset::INCLUDE_CODE;
527 else if (ev.argument == "index")
528 code = Inset::INDEX_CODE;
529 else if (ev.argument == "label")
530 code = Inset::LABEL_CODE;
531 else if (ev.argument == "minipage")
532 code = Inset::MINIPAGE_CODE;
533 else if (ev.argument == "ref")
534 code = Inset::REF_CODE;
535 else if (ev.argument == "toc")
536 code = Inset::TOC_CODE;
537 else if (ev.argument == "url")
538 code = Inset::URL_CODE;
539 else if (ev.argument == "wrap")
540 code = Inset::WRAP_CODE;
544 code = Inset::ERT_CODE;
546 case LFUN_INSET_GRAPHICS:
547 code = Inset::GRAPHICS_CODE;
549 case LFUN_INSET_FOOTNOTE:
550 code = Inset::FOOT_CODE;
552 case LFUN_TABULAR_INSERT:
553 code = Inset::TABULAR_CODE;
555 case LFUN_INSET_MARGINAL:
556 code = Inset::MARGIN_CODE;
558 case LFUN_INSET_MINIPAGE:
559 code = Inset::MINIPAGE_CODE;
561 case LFUN_INSET_FLOAT:
562 case LFUN_INSET_WIDE_FLOAT:
563 code = Inset::FLOAT_CODE;
565 case LFUN_INSET_WRAP:
566 code = Inset::WRAP_CODE;
568 case LFUN_FLOAT_LIST:
569 code = Inset::FLOAT_LIST_CODE;
572 case LFUN_INSET_LIST:
573 code = Inset::LIST_CODE;
575 case LFUN_INSET_THEOREM:
576 code = Inset::THEOREM_CODE;
579 case LFUN_INSET_CAPTION:
580 code = Inset::CAPTION_CODE;
582 case LFUN_INSERT_NOTE:
583 code = Inset::NOTE_CODE;
585 case LFUN_INSERT_LABEL:
586 code = Inset::LABEL_CODE;
588 case LFUN_INSET_OPTARG:
589 code = Inset::OPTARG_CODE;
591 case LFUN_INDEX_INSERT:
592 code = Inset::INDEX_CODE;
594 case LFUN_INDEX_PRINT:
595 code = Inset::INDEX_PRINT_CODE;
597 case LFUN_TOC_INSERT:
598 code = Inset::TOC_CODE;
600 case LFUN_PARENTINSERT:
601 code = Inset::PARENT_CODE;
605 code = Inset::URL_CODE;
608 // always allow this, since we will inset a raw quote
609 // if an inset is not allowed.
611 case LFUN_HYPHENATION:
612 case LFUN_LIGATURE_BREAK:
614 case LFUN_MENU_SEPARATOR:
616 case LFUN_END_OF_SENTENCE:
617 code = Inset::SPECIALCHAR_CODE;
619 case LFUN_PROTECTEDSPACE:
620 // slight hack: we know this is allowed in math mode
622 code = Inset::SPECIALCHAR_CODE;
627 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
633 // A few general toggles
635 case LFUN_TOOLTIPS_TOGGLE:
636 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
639 case LFUN_READ_ONLY_TOGGLE:
640 flag.setOnOff(buf->isReadonly());
643 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
645 case LFUN_SWITCHBUFFER:
646 // toggle on the current buffer, but do not toggle off
647 // the other ones (is that a good idea?)
648 if (ev.argument == buf->fileName())
651 case LFUN_TRACK_CHANGES:
652 flag.setOnOff(buf->params.tracking_changes);
658 // the font related toggles
660 LyXFont const & font = TEXT(false)->real_current_font;
663 flag.setOnOff(font.emph() == LyXFont::ON);
666 flag.setOnOff(font.noun() == LyXFont::ON);
669 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
672 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
675 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
678 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
684 string tc = mathcursor->getLastCode();
687 flag.setOnOff(tc == "mathbf");
690 flag.setOnOff(tc == "mathsf");
693 flag.setOnOff(tc == "mathcal");
696 flag.setOnOff(tc == "mathrm");
699 flag.setOnOff(tc == "mathtt");
702 flag.setOnOff(tc == "mathbb");
705 flag.setOnOff(tc == "mathnormal");
712 // this one is difficult to get right. As a half-baked
713 // solution, we consider only the first action of the sequence
714 if (ev.action == LFUN_SEQUENCE) {
715 // argument contains ';'-terminated commands
716 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
723 void LyXFunc::dispatch(string const & s, bool verbose)
725 int const action = lyxaction.LookupFunc(s);
727 if (action == LFUN_UNKNOWN_ACTION) {
729 boost::format fmt(_("Unknown function (%1$s)"));
731 owner->message(fmt.str());
733 string const msg = string(_("Unknown function ("))
740 dispatch(action, verbose);
744 void LyXFunc::dispatch(int ac, bool verbose)
746 dispatch(lyxaction.retrieveActionArg(ac), verbose);
751 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
753 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
754 <<"] arg[" << ev.argument << ']' << endl;
756 // we have not done anything wrong yet.
758 dispatch_buffer.erase();
760 #ifdef NEW_DISPATCHER
761 // We try do call the most specific dispatcher first:
762 // 1. the lockinginset's dispatch
763 // 2. the bufferview's dispatch
764 // 3. the lyxview's dispatch
767 selection_possible = false;
769 if (view()->available())
770 view()->hideCursor();
772 string argument = ev.argument;
773 kb_action action = ev.action;
775 // We cannot use this function here
776 if (getStatus(ev).disabled()) {
777 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
778 << lyxaction.getActionName(action)
779 << " [" << action << "] is disabled at this location"
781 setErrorMessage(getStatusMessage());
782 goto exit_with_message;
785 if (view()->available() && view()->theLockingInset()) {
786 Inset::RESULT result;
787 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
788 (!keyseq.deleted())))
790 UpdatableInset * inset = view()->theLockingInset();
794 inset->getCursorPos(view(), inset_x, dummy_y);
796 if ((action == LFUN_UNKNOWN_ACTION)
797 && argument.empty()) {
798 argument = encoded_last_key;
800 // Undo/Redo is a bit tricky for insets.
801 if (action == LFUN_UNDO) {
803 goto exit_with_message;
804 } else if (action == LFUN_REDO) {
806 goto exit_with_message;
807 } else if (((result=inset->
808 // Hand-over to inset's own dispatch:
809 localDispatch(FuncRequest(view(), action, argument))) ==
811 (result == DISPATCHED_NOUPDATE))
812 goto exit_with_message;
813 // If UNDISPATCHED, just soldier on
814 else if (result == FINISHED) {
815 goto exit_with_message;
816 // We do not need special RTL handling here:
817 // FINISHED means that the cursor should be
818 // one position after the inset.
819 } else if (result == FINISHED_RIGHT) {
820 TEXT()->cursorRight(view());
821 moveCursorUpdate(true, false);
822 owner->view_state_changed();
823 goto exit_with_message;
824 } else if (result == FINISHED_UP) {
825 if (TEXT()->cursor.irow()->previous()) {
827 TEXT()->setCursorFromCoordinates(
828 view(), TEXT()->cursor.ix() + inset_x,
829 TEXT()->cursor.iy() -
830 TEXT()->cursor.irow()->baseline() - 1);
831 TEXT()->cursor.x_fix(TEXT()->cursor.x());
833 TEXT()->cursorUp(view());
835 moveCursorUpdate(true, false);
836 owner->view_state_changed();
838 view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
840 goto exit_with_message;
841 } else if (result == FINISHED_DOWN) {
842 if (TEXT()->cursor.irow()->next()) {
844 TEXT()->setCursorFromCoordinates(
845 view(), TEXT()->cursor.ix() + inset_x,
846 TEXT()->cursor.iy() -
847 TEXT()->cursor.irow()->baseline() +
848 TEXT()->cursor.irow()->height() + 1);
849 TEXT()->cursor.x_fix(TEXT()->cursor.x());
851 TEXT()->cursorDown(view());
854 TEXT()->cursorRight(view());
856 moveCursorUpdate(true, false);
857 owner->view_state_changed();
858 goto exit_with_message;
860 #warning I am not sure this is still right, please have a look! (Jug 20020417)
861 else { // result == UNDISPATCHED
862 //setMessage(N_("Text mode"));
864 case LFUN_UNKNOWN_ACTION:
865 case LFUN_BREAKPARAGRAPH:
867 TEXT()->cursorRight(view());
868 view()->switchKeyMap();
869 owner->view_state_changed();
872 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
873 TEXT()->cursorRight(view());
874 moveCursorUpdate(true, false);
875 owner->view_state_changed();
877 goto exit_with_message;
879 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
880 TEXT()->cursorRight(view());
881 moveCursorUpdate(true, false);
882 owner->view_state_changed();
884 goto exit_with_message;
886 if (TEXT()->cursor.row()->next())
887 TEXT()->cursorDown(view());
889 TEXT()->cursorRight(view());
890 moveCursorUpdate(true, false);
891 owner->view_state_changed();
892 goto exit_with_message;
904 if (!view()->available()) break;
905 // this function should be used always [asierra060396]
906 UpdatableInset * tli =
907 view()->theLockingInset();
909 UpdatableInset * lock = tli->getLockingInset();
912 view()->unlockInset(tli);
913 TEXT()->cursorRight(view());
914 moveCursorUpdate(true, false);
915 owner->view_state_changed();
917 tli->unlockInsetInInset(view(),
922 // Tell the paragraph dialog that we changed paragraph
923 owner->getDialogs().updateParagraph();
928 // --- Misc -------------------------------------------
929 case LFUN_WORDFINDFORWARD :
930 case LFUN_WORDFINDBACKWARD : {
931 static string last_search;
932 string searched_string;
934 if (!argument.empty()) {
935 last_search = argument;
936 searched_string = argument;
938 searched_string = last_search;
940 bool fw = (action == LFUN_WORDFINDFORWARD);
941 if (!searched_string.empty()) {
942 lyxfind::LyXFind(view(), searched_string, fw);
944 // view()->showCursor();
950 if (view()->available() && !view()->theLockingInset()) {
951 view()->update(TEXT(),
952 BufferView::SELECT|BufferView::FITCUR);
954 owner->message(keyseq.printOptions());
958 // --- Misc -------------------------------------------
959 case LFUN_EXEC_COMMAND:
960 owner->focus_command_buffer();
963 case LFUN_CANCEL: // RVDK_PATCH_5
965 meta_fake_bit = key_modifier::none;
966 if (view()->available())
967 // cancel any selection
968 dispatch(LFUN_MARK_OFF);
969 setMessage(N_("Cancel"));
972 case LFUN_META_FAKE: // RVDK_PATCH_5
974 meta_fake_bit = key_modifier::alt;
975 setMessage(keyseq.print());
979 case LFUN_READ_ONLY_TOGGLE:
980 if (owner->buffer()->lyxvc.inUse()) {
981 owner->buffer()->lyxvc.toggleReadOnly();
983 owner->buffer()->setReadonly(
984 !owner->buffer()->isReadonly());
988 case LFUN_CENTER: // this is center and redraw.
992 // --- Menus -----------------------------------------------
994 menuNew(argument, false);
997 case LFUN_MENUNEWTMPLT:
998 menuNew(argument, true);
1001 case LFUN_CLOSEBUFFER:
1005 case LFUN_MENUWRITE:
1006 if (!owner->buffer()->isUnnamed()) {
1008 #if USE_BOOST_FORMAT
1009 s1 << boost::format(_("Saving document %1$s..."))
1010 % MakeDisplayPath(owner->buffer()->fileName());
1012 s1 << _("Saving document ")
1013 << MakeDisplayPath(owner->buffer()->fileName())
1016 owner->message(STRCONV(s1.str()));
1017 MenuWrite(view(), owner->buffer());
1019 owner->message(STRCONV(s1.str()));
1021 WriteAs(view(), owner->buffer());
1025 WriteAs(view(), owner->buffer(), argument);
1028 case LFUN_MENURELOAD:
1033 Exporter::Export(owner->buffer(), argument, true);
1037 Exporter::Preview(owner->buffer(), argument);
1040 case LFUN_BUILDPROG:
1041 Exporter::Export(owner->buffer(), "program", true);
1044 case LFUN_RUNCHKTEX:
1045 MenuRunChktex(owner->buffer());
1048 case LFUN_MENUPRINT:
1049 owner->getDialogs().showPrint();
1053 if (argument == "custom")
1054 owner->getDialogs().showSendto();
1056 Exporter::Export(owner->buffer(), argument, false);
1069 InsetCommandParams p("tableofcontents");
1070 string const data = InsetCommandMailer::params2string("toc", p);
1071 owner->getDialogs().show("toc", data, 0);
1087 case LFUN_MENUSEARCH:
1088 owner->getDialogs().showSearch();
1091 case LFUN_REMOVEERRORS:
1092 if (view()->removeAutoInsets()) {
1093 #warning repaint() or update() or nothing ?
1095 view()->fitCursor();
1099 case LFUN_DEPTH_MIN:
1100 changeDepth(view(), TEXT(false), -1);
1103 case LFUN_DEPTH_PLUS:
1104 changeDepth(view(), TEXT(false), 1);
1108 owner->getDialogs().setUserFreeFont();
1111 case LFUN_RECONFIGURE:
1112 Reconfigure(view());
1116 case LFUN_FLOATSOPERATE:
1117 if (argument == "openfoot")
1118 view()->allFloats(1,0);
1119 else if (argument == "closefoot")
1120 view()->allFloats(0,0);
1121 else if (argument == "openfig")
1122 view()->allFloats(1,1);
1123 else if (argument == "closefig")
1124 view()->allFloats(0,1);
1127 #ifdef WITH_WARNINGS
1128 //#warning Find another implementation here (or another lyxfunc)!
1131 case LFUN_HELP_ABOUTLYX:
1132 owner->getDialogs().showAboutlyx();
1135 case LFUN_HELP_TEXINFO:
1136 owner->getDialogs().showTexinfo();
1139 case LFUN_HELP_OPEN:
1141 string const arg = argument;
1143 setErrorMessage(N_("Missing argument"));
1146 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1147 if (fname.empty()) {
1148 lyxerr << "LyX: unable to find documentation file `"
1149 << arg << "'. Bad installation?" << endl;
1153 #if USE_BOOST_FORMAT
1154 str << boost::format(_("Opening help file %1$s..."))
1155 % MakeDisplayPath(fname);
1157 str << _("Opening help file ")
1158 << MakeDisplayPath(fname) << _("...");
1160 owner->message(STRCONV(str.str()));
1161 view()->buffer(bufferlist.loadLyXFile(fname, false));
1165 // --- version control -------------------------------
1166 case LFUN_VC_REGISTER:
1168 if (!owner->buffer()->lyxvc.inUse())
1169 owner->buffer()->lyxvc.registrer();
1173 case LFUN_VC_CHECKIN:
1175 if (owner->buffer()->lyxvc.inUse()
1176 && !owner->buffer()->isReadonly())
1177 owner->buffer()->lyxvc.checkIn();
1181 case LFUN_VC_CHECKOUT:
1183 if (owner->buffer()->lyxvc.inUse()
1184 && owner->buffer()->isReadonly())
1185 owner->buffer()->lyxvc.checkOut();
1189 case LFUN_VC_REVERT:
1191 owner->buffer()->lyxvc.revert();
1197 owner->buffer()->lyxvc.undoLast();
1201 case LFUN_VC_HISTORY:
1203 owner->getDialogs().showVCLogFile();
1207 // --- buffers ----------------------------------------
1209 case LFUN_SWITCHBUFFER:
1210 view()->buffer(bufferlist.getBuffer(argument));
1215 // servercmd: argument must be <file>:<template>
1216 Buffer * tmpbuf = NewFile(argument);
1218 view()->buffer(tmpbuf);
1222 case LFUN_FILE_OPEN:
1226 case LFUN_LATEX_LOG:
1227 owner->getDialogs().showLogFile();
1230 case LFUN_LAYOUT_DOCUMENT:
1231 owner->getDialogs().showDocument();
1234 case LFUN_LAYOUT_PARAGRAPH:
1235 owner->getDialogs().showParagraph();
1238 case LFUN_LAYOUT_CHARACTER:
1239 owner->getDialogs().showCharacter();
1242 case LFUN_LAYOUT_TABULAR:
1243 if (view()->theLockingInset()) {
1244 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1245 InsetTabular * inset = static_cast<InsetTabular *>
1246 (view()->theLockingInset());
1247 inset->openLayoutDialog(view());
1248 } else if (view()->theLockingInset()->
1249 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1250 InsetTabular * inset = static_cast<InsetTabular *>(
1251 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1252 inset->openLayoutDialog(view());
1257 case LFUN_LAYOUT_PREAMBLE:
1258 owner->getDialogs().showPreamble();
1261 case LFUN_DROP_LAYOUTS_CHOICE:
1262 owner->getToolbar().openLayoutList();
1265 case LFUN_MENU_OPEN_BY_NAME:
1266 owner->getMenubar().openByName(argument);
1267 break; // RVDK_PATCH_5
1269 case LFUN_SPELLCHECK:
1270 owner->getDialogs().showSpellchecker();
1273 // --- lyxserver commands ----------------------------
1277 setMessage(owner->buffer()->fileName());
1278 lyxerr[Debug::INFO] << "FNAME["
1279 << owner->buffer()->fileName()
1285 dispatch_buffer = keyseq.print();
1286 lyxserver->notifyClient(dispatch_buffer);
1290 case LFUN_GOTOFILEROW:
1294 istringstream istr(argument.c_str());
1295 istr >> file_name >> row;
1296 // Must replace extension of the file to be .lyx and get full path
1297 string const s(ChangeExtension(file_name, ".lyx"));
1299 // Either change buffer or load the file
1300 if (bufferlist.exists(s)) {
1301 view()->buffer(bufferlist.getBuffer(s));
1303 view()->buffer(bufferlist.loadLyXFile(s));
1306 view()->setCursorFromRow(row);
1309 // see BufferView_pimpl::center()
1310 view()->updateScrollbar();
1314 case LFUN_GOTO_PARAGRAPH:
1316 istringstream istr(argument.c_str());
1320 Paragraph * par = owner->buffer()->getParFromID(id);
1322 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1323 << id << ']' << endl;
1326 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1327 << " found." << endl;
1330 if (view()->theLockingInset())
1331 view()->unlockInset(view()->theLockingInset());
1332 if (par->inInset()) {
1333 par->inInset()->edit(view());
1336 view()->getLyXText()->setCursor(view(), par, 0);
1337 view()->switchKeyMap();
1338 owner->view_state_changed();
1341 // see BufferView_pimpl::center()
1342 view()->updateScrollbar();
1349 int const qa = lyxaction.LookupFunc(argument);
1350 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1354 // --- insert characters ----------------------------------------
1356 // --- Mathed stuff. If we are here, there is no locked inset yet.
1357 case LFUN_MATH_EXTERN:
1358 case LFUN_MATH_NUMBER:
1359 case LFUN_MATH_NONUMBER:
1360 case LFUN_MATH_LIMITS:
1362 setErrorMessage(N_("This is only allowed in math mode!"));
1366 // passthrough hat and underscore outside mathed:
1367 case LFUN_SUBSCRIPT:
1368 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1370 case LFUN_SUPERSCRIPT:
1371 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1374 case LFUN_MATH_PANEL:
1375 owner->getDialogs().showMathPanel();
1378 case LFUN_DIALOG_SHOW_NEW_INSET: {
1379 string const & name = argument;
1381 if (name == "bibitem" ||
1383 name == "include" ||
1388 InsetCommandParams p(name);
1389 data = InsetCommandMailer::params2string(name, p);
1390 } else if (name == "citation") {
1391 InsetCommandParams p("cite");
1392 data = InsetCommandMailer::params2string(name, p);
1394 owner->getDialogs().show(name, data, 0);
1398 case LFUN_DIALOG_SHOW_NEXT_INSET: {
1402 case LFUN_DIALOG_UPDATE: {
1403 string const & name = argument;
1404 // Can only update a dialog connected to an existing inset
1405 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1407 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1409 inset->localDispatch(fr);
1414 case LFUN_DIALOG_HIDE:
1415 owner ->getDialogs().hide(argument);
1418 case LFUN_DIALOG_DISCONNECT_INSET:
1419 owner->getDialogs().disconnect(argument);
1422 case LFUN_CHILDOPEN:
1424 string const filename =
1425 MakeAbsPath(argument,
1426 owner->buffer()->filePath());
1427 setMessage(N_("Opening child document ") +
1428 MakeDisplayPath(filename) + "...");
1429 view()->savePosition(0);
1430 if (bufferlist.exists(filename))
1431 view()->buffer(bufferlist.getBuffer(filename));
1433 view()->buffer(bufferlist.loadLyXFile(filename));
1437 case LFUN_TOGGLECURSORFOLLOW:
1438 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1442 owner->getIntl().KeyMapOn(false);
1445 case LFUN_KMAP_PRIM:
1446 owner->getIntl().KeyMapPrim();
1450 owner->getIntl().KeyMapSec();
1453 case LFUN_KMAP_TOGGLE:
1454 owner->getIntl().ToggleKeyMap();
1459 // argument contains ';'-terminated commands
1460 while (!argument.empty()) {
1462 argument = split(argument, first, ';');
1468 case LFUN_DIALOG_PREFERENCES:
1469 owner->getDialogs().showPreferences();
1472 case LFUN_SAVEPREFERENCES:
1474 Path p(user_lyxdir);
1475 lyxrc.write("preferences");
1479 case LFUN_SCREEN_FONT_UPDATE:
1481 // handle the screen font changes.
1482 lyxrc.set_font_norm_type();
1483 lyx_gui::update_fonts();
1484 // We also need to empty the textcache so that
1485 // the buffer will be formatted correctly after
1488 // Of course we should only do the resize and the textcache.clear
1489 // if values really changed...but not very important right now. (Lgb)
1490 // All visible buffers will need resize
1496 case LFUN_SET_COLOR:
1499 string const x11_name = split(argument, lyx_name, ' ');
1500 if (lyx_name.empty() || x11_name.empty()) {
1501 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1506 bool const graphicsbg_changed =
1507 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1508 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1510 if (!lcolor.setColor(lyx_name, x11_name)) {
1511 #if USE_BOOST_FORMAT
1515 _("Set-color \"%1$s\" failed "
1516 "- color is undefined or "
1517 "may not be redefined"))
1520 setErrorMessage(_("Set-color ") + lyx_name
1521 + _(" failed - color is undefined"
1522 " or may not be redefined"));
1528 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1530 if (graphicsbg_changed) {
1531 #ifdef WITH_WARNINGS
1532 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1535 grfx::GCache & gc = grfx::GCache::get();
1536 gc.changeDisplay(true);
1545 owner->message(argument);
1548 case LFUN_FORKS_SHOW:
1549 owner->getDialogs().showForks();
1552 case LFUN_FORKS_KILL:
1554 if (!isStrInt(argument))
1557 pid_t const pid = strToInt(argument);
1558 ForkedcallsController & fcc = ForkedcallsController::get();
1563 case LFUN_TOOLTIPS_TOGGLE:
1564 owner->getDialogs().toggleTooltips();
1568 // Then if it was none of the above
1569 // Trying the BufferView::pimpl dispatch:
1570 if (!view()->dispatch(ev))
1571 lyxerr << "A truly unknown func ["
1572 << lyxaction.getActionName(ev.action) << "]!"
1578 sendDispatchMessage(getMessage(), ev, verbose);
1582 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1584 owner->updateMenubar();
1585 owner->updateToolbar();
1587 if (ev.action == LFUN_SELFINSERT || !verbose) {
1588 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1590 owner->message(msg);
1594 string dispatch_msg = msg;
1595 if (!dispatch_msg.empty())
1596 dispatch_msg += ' ';
1598 string comname = lyxaction.getActionName(ev.action);
1600 int pseudoaction = ev.action;
1601 bool argsadded = false;
1603 if (!ev.argument.empty()) {
1604 // the pseudoaction is useful for the bindings
1605 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1607 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1608 pseudoaction = ev.action;
1610 comname += ' ' + ev.argument;
1615 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1617 if (!shortcuts.empty()) {
1618 comname += ": " + shortcuts;
1619 } else if (!argsadded && !ev.argument.empty()) {
1620 comname += ' ' + ev.argument;
1623 if (!comname.empty()) {
1624 comname = rtrim(comname);
1625 dispatch_msg += '(' + comname + ')';
1628 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1629 if (!dispatch_msg.empty())
1630 owner->message(dispatch_msg);
1634 void LyXFunc::setupLocalKeymap()
1636 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1637 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1641 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1643 string initpath = lyxrc.document_path;
1644 string filename(name);
1646 if (view()->available()) {
1647 string const trypath = owner->buffer()->filePath();
1648 // If directory is writeable, use this as default.
1649 if (IsDirWriteable(trypath))
1653 static int newfile_number;
1655 if (filename.empty()) {
1656 filename = AddName(lyxrc.document_path,
1657 "newfile" + tostr(++newfile_number) + ".lyx");
1658 FileInfo fi(filename);
1659 while (bufferlist.exists(filename) || fi.readable()) {
1661 filename = AddName(lyxrc.document_path,
1662 "newfile" + tostr(newfile_number) +
1664 fi.newFile(filename);
1668 // The template stuff
1671 FileDialog fileDlg(_("Select template file"),
1672 LFUN_SELECT_FILE_SYNC,
1673 make_pair(string(_("Documents|#o#O")),
1674 string(lyxrc.document_path)),
1675 make_pair(string(_("Templates|#T#t")),
1676 string(lyxrc.template_path)));
1678 FileDialog::Result result =
1679 fileDlg.open(lyxrc.template_path,
1680 _("*.lyx| LyX Documents (*.lyx)"));
1682 if (result.first == FileDialog::Later)
1685 string const fname = result.second;
1692 view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1696 void LyXFunc::open(string const & fname)
1698 string initpath = lyxrc.document_path;
1700 if (view()->available()) {
1701 string const trypath = owner->buffer()->filePath();
1702 // If directory is writeable, use this as default.
1703 if (IsDirWriteable(trypath))
1709 if (fname.empty()) {
1710 FileDialog fileDlg(_("Select document to open"),
1712 make_pair(string(_("Documents|#o#O")),
1713 string(lyxrc.document_path)),
1714 make_pair(string(_("Examples|#E#e")),
1715 string(AddPath(system_lyxdir, "examples"))));
1717 FileDialog::Result result =
1718 fileDlg.open(initpath,
1719 _("*.lyx| LyX Documents (*.lyx)"));
1721 if (result.first == FileDialog::Later)
1724 filename = result.second;
1726 // check selected filename
1727 if (filename.empty()) {
1728 owner->message(_("Canceled."));
1734 // get absolute path of file and add ".lyx" to the filename if
1736 string const fullpath = FileSearch(string(), filename, "lyx");
1737 if (!fullpath.empty()) {
1738 filename = fullpath;
1741 string const disp_fn(MakeDisplayPath(filename));
1743 // if the file doesn't exist, let the user create one
1744 FileInfo const f(filename, true);
1746 if (!Alert::askQuestion(_("No such file"), disp_fn,
1747 _("Start a new document with this filename ?"))) {
1748 owner->message(_("Canceled."));
1751 // the user specifically chose this name. Believe them.
1752 Buffer * buffer = bufferlist.newFile(filename, "", true);
1753 view()->buffer(buffer);
1758 #if USE_BOOST_FORMAT
1759 str << boost::format(_("Opening document %1$s...")) % disp_fn;
1761 str << _("Opening document ") << disp_fn << _("...");
1764 owner->message(STRCONV(str.str()));
1766 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1769 view()->buffer(openbuf);
1770 #if USE_BOOST_FORMAT
1771 str2 << boost::format(_("Document %1$s opened.")) % disp_fn;
1773 str2 << _("Document ") << disp_fn << _(" opened.");
1776 #if USE_BOOST_FORMAT
1777 str2 << boost::format(_("Could not open document %1$s"))
1780 str2 << _("Could not open document ") << disp_fn;
1783 owner->message(STRCONV(str2.str()));
1787 void LyXFunc::doImport(string const & argument)
1790 string filename = split(argument, format, ' ');
1792 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1793 << " file: " << filename << endl;
1795 // need user interaction
1796 if (filename.empty()) {
1797 string initpath = lyxrc.document_path;
1799 if (view()->available()) {
1800 string const trypath = owner->buffer()->filePath();
1801 // If directory is writeable, use this as default.
1802 if (IsDirWriteable(trypath))
1806 #if USE_BOOST_FORMAT
1807 boost::format fmt(_("Select %1$s file to import"));
1808 fmt % formats.prettyName(format);
1809 string const text = fmt.str();
1811 string const text = _("Select ") + formats.prettyName(format)
1812 + _(" file to import");;
1815 FileDialog fileDlg(text,
1817 make_pair(string(_("Documents|#o#O")),
1818 string(lyxrc.document_path)),
1819 make_pair(string(_("Examples|#E#e")),
1820 string(AddPath(system_lyxdir, "examples"))));
1822 string const extension = "*." + formats.extension(format)
1823 + "| " + formats.prettyName(format)
1824 + " (*." + formats.extension(format) + ')';
1826 FileDialog::Result result = fileDlg.open(initpath,
1829 if (result.first == FileDialog::Later)
1832 filename = result.second;
1834 // check selected filename
1835 if (filename.empty())
1836 owner->message(_("Canceled."));
1839 if (filename.empty())
1842 // get absolute path of file
1843 filename = MakeAbsPath(filename);
1845 string const lyxfile = ChangeExtension(filename, ".lyx");
1847 // Check if the document already is open
1848 if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1849 switch (Alert::askConfirmation(_("Document is already open:"),
1850 MakeDisplayPath(lyxfile, 50),
1851 _("Do you want to close that document now?\n"
1852 "('No' will just switch to the open version)")))
1855 // If close is canceled, we cancel here too.
1856 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1860 view()->buffer(bufferlist.getBuffer(lyxfile));
1863 owner->message(_("Canceled."));
1868 // if the file exists already, and we didn't do
1869 // -i lyx thefile.lyx, warn
1870 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1871 if (!Alert::askQuestion(_("A document by the name"),
1872 MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1873 owner->message(_("Canceled."));
1878 Importer::Import(owner, filename, format);
1882 void LyXFunc::reloadBuffer()
1884 string const fn = owner->buffer()->fileName();
1885 if (bufferlist.close(owner->buffer()))
1886 view()->buffer(bufferlist.loadLyXFile(fn));
1890 void LyXFunc::closeBuffer()
1892 if (bufferlist.close(owner->buffer()) && !quitting) {
1893 if (bufferlist.empty()) {
1894 // need this otherwise SEGV may occur while trying to
1895 // set variables that don't exist
1896 // since there's no current buffer
1897 owner->getDialogs().hideBufferDependent();
1899 view()->buffer(bufferlist.first());
1905 // Each "owner" should have it's own message method. lyxview and
1906 // the minibuffer would use the minibuffer, but lyxserver would
1907 // send an ERROR signal to its client. Alejandro 970603
1908 // This func is bit problematic when it comes to NLS, to make the
1909 // lyx servers client be language indepenent we must not translate
1910 // strings sent to this func.
1911 void LyXFunc::setErrorMessage(string const & m) const
1913 dispatch_buffer = m;
1918 void LyXFunc::setMessage(string const & m) const
1920 dispatch_buffer = m;
1924 void LyXFunc::setStatusMessage(string const & m) const
1930 string const LyXFunc::view_status_message()
1932 // When meta-fake key is pressed, show the key sequence so far + "M-".
1934 return keyseq.print() + "M-";
1937 // Else, when a non-complete key sequence is pressed,
1938 // show the available options.
1939 if (keyseq.length() > 0 && !keyseq.deleted()) {
1940 return keyseq.printOptions();
1943 if (!view()->available())
1944 return _("Welcome to LyX!");
1946 return currentState(view());
1950 BufferView * LyXFunc::view() const
1953 return owner->view().get();