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_FOOTNOTE:
547 code = Inset::FOOT_CODE;
549 case LFUN_TABULAR_INSERT:
550 code = Inset::TABULAR_CODE;
552 case LFUN_INSET_MARGINAL:
553 code = Inset::MARGIN_CODE;
555 case LFUN_INSET_MINIPAGE:
556 code = Inset::MINIPAGE_CODE;
558 case LFUN_INSET_FLOAT:
559 case LFUN_INSET_WIDE_FLOAT:
560 code = Inset::FLOAT_CODE;
562 case LFUN_INSET_WRAP:
563 code = Inset::WRAP_CODE;
565 case LFUN_FLOAT_LIST:
566 code = Inset::FLOAT_LIST_CODE;
569 case LFUN_INSET_LIST:
570 code = Inset::LIST_CODE;
572 case LFUN_INSET_THEOREM:
573 code = Inset::THEOREM_CODE;
576 case LFUN_INSET_CAPTION:
577 code = Inset::CAPTION_CODE;
579 case LFUN_INSERT_NOTE:
580 code = Inset::NOTE_CODE;
582 case LFUN_INSERT_LABEL:
583 code = Inset::LABEL_CODE;
585 case LFUN_INSET_OPTARG:
586 code = Inset::OPTARG_CODE;
588 case LFUN_INDEX_INSERT:
589 code = Inset::INDEX_CODE;
591 case LFUN_INDEX_PRINT:
592 code = Inset::INDEX_PRINT_CODE;
594 case LFUN_TOC_INSERT:
595 code = Inset::TOC_CODE;
597 case LFUN_PARENTINSERT:
598 code = Inset::PARENT_CODE;
602 code = Inset::URL_CODE;
605 // always allow this, since we will inset a raw quote
606 // if an inset is not allowed.
608 case LFUN_HYPHENATION:
609 case LFUN_LIGATURE_BREAK:
611 case LFUN_MENU_SEPARATOR:
613 case LFUN_END_OF_SENTENCE:
614 code = Inset::SPECIALCHAR_CODE;
616 case LFUN_PROTECTEDSPACE:
617 // slight hack: we know this is allowed in math mode
619 code = Inset::SPECIALCHAR_CODE;
624 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
630 // A few general toggles
632 case LFUN_TOOLTIPS_TOGGLE:
633 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
636 case LFUN_READ_ONLY_TOGGLE:
637 flag.setOnOff(buf->isReadonly());
640 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
642 case LFUN_SWITCHBUFFER:
643 // toggle on the current buffer, but do not toggle off
644 // the other ones (is that a good idea?)
645 if (ev.argument == buf->fileName())
648 case LFUN_TRACK_CHANGES:
649 flag.setOnOff(buf->params.tracking_changes);
655 // the font related toggles
657 LyXFont const & font = TEXT(false)->real_current_font;
660 flag.setOnOff(font.emph() == LyXFont::ON);
663 flag.setOnOff(font.noun() == LyXFont::ON);
666 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
669 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
672 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
675 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
681 string tc = mathcursor->getLastCode();
684 flag.setOnOff(tc == "mathbf");
687 flag.setOnOff(tc == "mathsf");
690 flag.setOnOff(tc == "mathcal");
693 flag.setOnOff(tc == "mathrm");
696 flag.setOnOff(tc == "mathtt");
699 flag.setOnOff(tc == "mathbb");
702 flag.setOnOff(tc == "mathnormal");
709 // this one is difficult to get right. As a half-baked
710 // solution, we consider only the first action of the sequence
711 if (ev.action == LFUN_SEQUENCE) {
712 // argument contains ';'-terminated commands
713 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
720 void LyXFunc::dispatch(string const & s, bool verbose)
722 int const action = lyxaction.LookupFunc(s);
724 if (action == LFUN_UNKNOWN_ACTION) {
726 boost::format fmt(_("Unknown function (%1$s)"));
728 owner->message(fmt.str());
730 string const msg = string(_("Unknown function ("))
737 dispatch(action, verbose);
741 void LyXFunc::dispatch(int ac, bool verbose)
743 dispatch(lyxaction.retrieveActionArg(ac), verbose);
748 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
750 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
751 <<"] arg[" << ev.argument << ']' << endl;
753 // we have not done anything wrong yet.
755 dispatch_buffer.erase();
757 #ifdef NEW_DISPATCHER
758 // We try do call the most specific dispatcher first:
759 // 1. the lockinginset's dispatch
760 // 2. the bufferview's dispatch
761 // 3. the lyxview's dispatch
764 selection_possible = false;
766 if (view()->available())
767 view()->hideCursor();
769 string argument = ev.argument;
770 kb_action action = ev.action;
772 // We cannot use this function here
773 if (getStatus(ev).disabled()) {
774 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
775 << lyxaction.getActionName(action)
776 << " [" << action << "] is disabled at this location"
778 setErrorMessage(getStatusMessage());
779 goto exit_with_message;
782 if (view()->available() && view()->theLockingInset()) {
783 Inset::RESULT result;
784 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
785 (!keyseq.deleted())))
787 UpdatableInset * inset = view()->theLockingInset();
791 inset->getCursorPos(view(), inset_x, dummy_y);
793 if ((action == LFUN_UNKNOWN_ACTION)
794 && argument.empty()) {
795 argument = encoded_last_key;
797 // Undo/Redo is a bit tricky for insets.
798 if (action == LFUN_UNDO) {
800 goto exit_with_message;
801 } else if (action == LFUN_REDO) {
803 goto exit_with_message;
804 } else if (((result=inset->
805 // Hand-over to inset's own dispatch:
806 localDispatch(FuncRequest(view(), action, argument))) ==
808 (result == DISPATCHED_NOUPDATE))
809 goto exit_with_message;
810 // If UNDISPATCHED, just soldier on
811 else if (result == FINISHED) {
812 goto exit_with_message;
813 // We do not need special RTL handling here:
814 // FINISHED means that the cursor should be
815 // one position after the inset.
816 } else if (result == FINISHED_RIGHT) {
817 TEXT()->cursorRight(view());
818 moveCursorUpdate(true, false);
819 owner->view_state_changed();
820 goto exit_with_message;
821 } else if (result == FINISHED_UP) {
822 if (TEXT()->cursor.irow()->previous()) {
824 TEXT()->setCursorFromCoordinates(
825 view(), TEXT()->cursor.ix() + inset_x,
826 TEXT()->cursor.iy() -
827 TEXT()->cursor.irow()->baseline() - 1);
828 TEXT()->cursor.x_fix(TEXT()->cursor.x());
830 TEXT()->cursorUp(view());
832 moveCursorUpdate(true, false);
833 owner->view_state_changed();
835 view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
837 goto exit_with_message;
838 } else if (result == FINISHED_DOWN) {
839 if (TEXT()->cursor.irow()->next()) {
841 TEXT()->setCursorFromCoordinates(
842 view(), TEXT()->cursor.ix() + inset_x,
843 TEXT()->cursor.iy() -
844 TEXT()->cursor.irow()->baseline() +
845 TEXT()->cursor.irow()->height() + 1);
846 TEXT()->cursor.x_fix(TEXT()->cursor.x());
848 TEXT()->cursorDown(view());
851 TEXT()->cursorRight(view());
853 moveCursorUpdate(true, false);
854 owner->view_state_changed();
855 goto exit_with_message;
857 #warning I am not sure this is still right, please have a look! (Jug 20020417)
858 else { // result == UNDISPATCHED
859 //setMessage(N_("Text mode"));
861 case LFUN_UNKNOWN_ACTION:
862 case LFUN_BREAKPARAGRAPH:
864 TEXT()->cursorRight(view());
865 view()->switchKeyMap();
866 owner->view_state_changed();
869 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
870 TEXT()->cursorRight(view());
871 moveCursorUpdate(true, false);
872 owner->view_state_changed();
874 goto exit_with_message;
876 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
877 TEXT()->cursorRight(view());
878 moveCursorUpdate(true, false);
879 owner->view_state_changed();
881 goto exit_with_message;
883 if (TEXT()->cursor.row()->next())
884 TEXT()->cursorDown(view());
886 TEXT()->cursorRight(view());
887 moveCursorUpdate(true, false);
888 owner->view_state_changed();
889 goto exit_with_message;
901 if (!view()->available()) break;
902 // this function should be used always [asierra060396]
903 UpdatableInset * tli =
904 view()->theLockingInset();
906 UpdatableInset * lock = tli->getLockingInset();
909 view()->unlockInset(tli);
910 TEXT()->cursorRight(view());
911 moveCursorUpdate(true, false);
912 owner->view_state_changed();
914 tli->unlockInsetInInset(view(),
919 // Tell the paragraph dialog that we changed paragraph
920 owner->getDialogs().updateParagraph();
925 // --- Misc -------------------------------------------
926 case LFUN_WORDFINDFORWARD :
927 case LFUN_WORDFINDBACKWARD : {
928 static string last_search;
929 string searched_string;
931 if (!argument.empty()) {
932 last_search = argument;
933 searched_string = argument;
935 searched_string = last_search;
937 bool fw = (action == LFUN_WORDFINDFORWARD);
938 if (!searched_string.empty()) {
939 lyxfind::LyXFind(view(), searched_string, fw);
941 // view()->showCursor();
947 if (view()->available() && !view()->theLockingInset()) {
948 view()->update(TEXT(),
949 BufferView::SELECT|BufferView::FITCUR);
951 owner->message(keyseq.printOptions());
955 // --- Misc -------------------------------------------
956 case LFUN_EXEC_COMMAND:
957 owner->focus_command_buffer();
960 case LFUN_CANCEL: // RVDK_PATCH_5
962 meta_fake_bit = key_modifier::none;
963 if (view()->available())
964 // cancel any selection
965 dispatch(LFUN_MARK_OFF);
966 setMessage(N_("Cancel"));
969 case LFUN_META_FAKE: // RVDK_PATCH_5
971 meta_fake_bit = key_modifier::alt;
972 setMessage(keyseq.print());
976 case LFUN_READ_ONLY_TOGGLE:
977 if (owner->buffer()->lyxvc.inUse()) {
978 owner->buffer()->lyxvc.toggleReadOnly();
980 owner->buffer()->setReadonly(
981 !owner->buffer()->isReadonly());
985 case LFUN_CENTER: // this is center and redraw.
989 // --- Menus -----------------------------------------------
991 menuNew(argument, false);
994 case LFUN_MENUNEWTMPLT:
995 menuNew(argument, true);
998 case LFUN_CLOSEBUFFER:
1002 case LFUN_MENUWRITE:
1003 if (!owner->buffer()->isUnnamed()) {
1005 #if USE_BOOST_FORMAT
1006 s1 << boost::format(_("Saving document %1$s..."))
1007 % MakeDisplayPath(owner->buffer()->fileName());
1009 s1 << _("Saving document ")
1010 << MakeDisplayPath(owner->buffer()->fileName())
1013 owner->message(STRCONV(s1.str()));
1014 MenuWrite(view(), owner->buffer());
1016 owner->message(STRCONV(s1.str()));
1018 WriteAs(view(), owner->buffer());
1022 WriteAs(view(), owner->buffer(), argument);
1025 case LFUN_MENURELOAD:
1030 Exporter::Export(owner->buffer(), argument, true);
1034 Exporter::Preview(owner->buffer(), argument);
1037 case LFUN_BUILDPROG:
1038 Exporter::Export(owner->buffer(), "program", true);
1041 case LFUN_RUNCHKTEX:
1042 MenuRunChktex(owner->buffer());
1045 case LFUN_MENUPRINT:
1046 owner->getDialogs().showPrint();
1050 if (argument == "custom")
1051 owner->getDialogs().showSendto();
1053 Exporter::Export(owner->buffer(), argument, false);
1066 InsetCommandParams p("tableofcontents");
1067 string const data = InsetCommandMailer::params2string("toc", p);
1068 owner->getDialogs().show("toc", data, 0);
1084 case LFUN_MENUSEARCH:
1085 owner->getDialogs().showSearch();
1088 case LFUN_REMOVEERRORS:
1089 if (view()->removeAutoInsets()) {
1090 #warning repaint() or update() or nothing ?
1092 view()->fitCursor();
1096 case LFUN_DEPTH_MIN:
1097 changeDepth(view(), TEXT(false), -1);
1100 case LFUN_DEPTH_PLUS:
1101 changeDepth(view(), TEXT(false), 1);
1105 owner->getDialogs().setUserFreeFont();
1108 case LFUN_RECONFIGURE:
1109 Reconfigure(view());
1113 case LFUN_FLOATSOPERATE:
1114 if (argument == "openfoot")
1115 view()->allFloats(1,0);
1116 else if (argument == "closefoot")
1117 view()->allFloats(0,0);
1118 else if (argument == "openfig")
1119 view()->allFloats(1,1);
1120 else if (argument == "closefig")
1121 view()->allFloats(0,1);
1124 #ifdef WITH_WARNINGS
1125 //#warning Find another implementation here (or another lyxfunc)!
1128 case LFUN_HELP_ABOUTLYX:
1129 owner->getDialogs().show("about");
1132 case LFUN_HELP_TEXINFO:
1133 owner->getDialogs().showTexinfo();
1136 case LFUN_HELP_OPEN:
1138 string const arg = argument;
1140 setErrorMessage(N_("Missing argument"));
1143 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1144 if (fname.empty()) {
1145 lyxerr << "LyX: unable to find documentation file `"
1146 << arg << "'. Bad installation?" << endl;
1150 #if USE_BOOST_FORMAT
1151 str << boost::format(_("Opening help file %1$s..."))
1152 % MakeDisplayPath(fname);
1154 str << _("Opening help file ")
1155 << MakeDisplayPath(fname) << _("...");
1157 owner->message(STRCONV(str.str()));
1158 view()->buffer(bufferlist.loadLyXFile(fname, false));
1162 // --- version control -------------------------------
1163 case LFUN_VC_REGISTER:
1165 if (!owner->buffer()->lyxvc.inUse())
1166 owner->buffer()->lyxvc.registrer();
1170 case LFUN_VC_CHECKIN:
1172 if (owner->buffer()->lyxvc.inUse()
1173 && !owner->buffer()->isReadonly())
1174 owner->buffer()->lyxvc.checkIn();
1178 case LFUN_VC_CHECKOUT:
1180 if (owner->buffer()->lyxvc.inUse()
1181 && owner->buffer()->isReadonly())
1182 owner->buffer()->lyxvc.checkOut();
1186 case LFUN_VC_REVERT:
1188 owner->buffer()->lyxvc.revert();
1194 owner->buffer()->lyxvc.undoLast();
1198 case LFUN_VC_HISTORY:
1200 owner->getDialogs().showVCLogFile();
1204 // --- buffers ----------------------------------------
1206 case LFUN_SWITCHBUFFER:
1207 view()->buffer(bufferlist.getBuffer(argument));
1212 // servercmd: argument must be <file>:<template>
1213 Buffer * tmpbuf = NewFile(argument);
1215 view()->buffer(tmpbuf);
1219 case LFUN_FILE_OPEN:
1223 case LFUN_LATEX_LOG:
1224 owner->getDialogs().showLogFile();
1227 case LFUN_LAYOUT_DOCUMENT:
1228 owner->getDialogs().showDocument();
1231 case LFUN_LAYOUT_PARAGRAPH:
1232 owner->getDialogs().showParagraph();
1235 case LFUN_LAYOUT_CHARACTER:
1236 owner->getDialogs().showCharacter();
1239 case LFUN_LAYOUT_TABULAR:
1240 if (view()->theLockingInset()) {
1241 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1242 InsetTabular * inset = static_cast<InsetTabular *>
1243 (view()->theLockingInset());
1244 inset->openLayoutDialog(view());
1245 } else if (view()->theLockingInset()->
1246 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1247 InsetTabular * inset = static_cast<InsetTabular *>(
1248 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1249 inset->openLayoutDialog(view());
1254 case LFUN_LAYOUT_PREAMBLE:
1255 owner->getDialogs().showPreamble();
1258 case LFUN_DROP_LAYOUTS_CHOICE:
1259 owner->getToolbar().openLayoutList();
1262 case LFUN_MENU_OPEN_BY_NAME:
1263 owner->getMenubar().openByName(argument);
1264 break; // RVDK_PATCH_5
1266 case LFUN_SPELLCHECK:
1267 owner->getDialogs().showSpellchecker();
1270 // --- lyxserver commands ----------------------------
1274 setMessage(owner->buffer()->fileName());
1275 lyxerr[Debug::INFO] << "FNAME["
1276 << owner->buffer()->fileName()
1282 dispatch_buffer = keyseq.print();
1283 lyxserver->notifyClient(dispatch_buffer);
1287 case LFUN_GOTOFILEROW:
1291 istringstream istr(argument.c_str());
1292 istr >> file_name >> row;
1293 // Must replace extension of the file to be .lyx and get full path
1294 string const s(ChangeExtension(file_name, ".lyx"));
1296 // Either change buffer or load the file
1297 if (bufferlist.exists(s)) {
1298 view()->buffer(bufferlist.getBuffer(s));
1300 view()->buffer(bufferlist.loadLyXFile(s));
1303 view()->setCursorFromRow(row);
1306 // see BufferView_pimpl::center()
1307 view()->updateScrollbar();
1311 case LFUN_GOTO_PARAGRAPH:
1313 istringstream istr(argument.c_str());
1317 Paragraph * par = owner->buffer()->getParFromID(id);
1319 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1320 << id << ']' << endl;
1323 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1324 << " found." << endl;
1327 if (view()->theLockingInset())
1328 view()->unlockInset(view()->theLockingInset());
1329 if (par->inInset()) {
1330 par->inInset()->edit(view());
1333 view()->getLyXText()->setCursor(view(), par, 0);
1334 view()->switchKeyMap();
1335 owner->view_state_changed();
1338 // see BufferView_pimpl::center()
1339 view()->updateScrollbar();
1346 int const qa = lyxaction.LookupFunc(argument);
1347 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1351 // --- insert characters ----------------------------------------
1353 // --- Mathed stuff. If we are here, there is no locked inset yet.
1354 case LFUN_MATH_EXTERN:
1355 case LFUN_MATH_NUMBER:
1356 case LFUN_MATH_NONUMBER:
1357 case LFUN_MATH_LIMITS:
1359 setErrorMessage(N_("This is only allowed in math mode!"));
1363 // passthrough hat and underscore outside mathed:
1364 case LFUN_SUBSCRIPT:
1365 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1367 case LFUN_SUPERSCRIPT:
1368 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1371 case LFUN_MATH_PANEL:
1372 owner->getDialogs().showMathPanel();
1375 case LFUN_DIALOG_SHOW_NEW_INSET: {
1376 string const & name = argument;
1378 if (name == "bibitem" ||
1380 name == "include" ||
1385 InsetCommandParams p(name);
1386 data = InsetCommandMailer::params2string(name, p);
1387 } else if (name == "citation") {
1388 InsetCommandParams p("cite");
1389 data = InsetCommandMailer::params2string(name, p);
1391 owner->getDialogs().show(name, data, 0);
1395 case LFUN_DIALOG_SHOW_NEXT_INSET: {
1399 case LFUN_DIALOG_UPDATE: {
1400 string const & name = argument;
1401 // Can only update a dialog connected to an existing inset
1402 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1404 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1406 inset->localDispatch(fr);
1411 case LFUN_DIALOG_HIDE:
1412 owner ->getDialogs().hide(argument);
1415 case LFUN_DIALOG_DISCONNECT_INSET:
1416 owner->getDialogs().disconnect(argument);
1419 case LFUN_CHILDOPEN:
1421 string const filename =
1422 MakeAbsPath(argument,
1423 owner->buffer()->filePath());
1424 setMessage(N_("Opening child document ") +
1425 MakeDisplayPath(filename) + "...");
1426 view()->savePosition(0);
1427 if (bufferlist.exists(filename))
1428 view()->buffer(bufferlist.getBuffer(filename));
1430 view()->buffer(bufferlist.loadLyXFile(filename));
1434 case LFUN_TOGGLECURSORFOLLOW:
1435 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1439 owner->getIntl().KeyMapOn(false);
1442 case LFUN_KMAP_PRIM:
1443 owner->getIntl().KeyMapPrim();
1447 owner->getIntl().KeyMapSec();
1450 case LFUN_KMAP_TOGGLE:
1451 owner->getIntl().ToggleKeyMap();
1456 // argument contains ';'-terminated commands
1457 while (!argument.empty()) {
1459 argument = split(argument, first, ';');
1465 case LFUN_DIALOG_PREFERENCES:
1466 owner->getDialogs().showPreferences();
1469 case LFUN_SAVEPREFERENCES:
1471 Path p(user_lyxdir);
1472 lyxrc.write("preferences");
1476 case LFUN_SCREEN_FONT_UPDATE:
1478 // handle the screen font changes.
1479 lyxrc.set_font_norm_type();
1480 lyx_gui::update_fonts();
1481 // We also need to empty the textcache so that
1482 // the buffer will be formatted correctly after
1485 // Of course we should only do the resize and the textcache.clear
1486 // if values really changed...but not very important right now. (Lgb)
1487 // All visible buffers will need resize
1493 case LFUN_SET_COLOR:
1496 string const x11_name = split(argument, lyx_name, ' ');
1497 if (lyx_name.empty() || x11_name.empty()) {
1498 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1503 bool const graphicsbg_changed =
1504 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1505 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1507 if (!lcolor.setColor(lyx_name, x11_name)) {
1508 #if USE_BOOST_FORMAT
1512 _("Set-color \"%1$s\" failed "
1513 "- color is undefined or "
1514 "may not be redefined"))
1517 setErrorMessage(_("Set-color ") + lyx_name
1518 + _(" failed - color is undefined"
1519 " or may not be redefined"));
1525 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1527 if (graphicsbg_changed) {
1528 #ifdef WITH_WARNINGS
1529 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1532 grfx::GCache & gc = grfx::GCache::get();
1533 gc.changeDisplay(true);
1542 owner->message(argument);
1545 case LFUN_FORKS_SHOW:
1546 owner->getDialogs().showForks();
1549 case LFUN_FORKS_KILL:
1551 if (!isStrInt(argument))
1554 pid_t const pid = strToInt(argument);
1555 ForkedcallsController & fcc = ForkedcallsController::get();
1560 case LFUN_TOOLTIPS_TOGGLE:
1561 owner->getDialogs().toggleTooltips();
1565 // Then if it was none of the above
1566 // Trying the BufferView::pimpl dispatch:
1567 if (!view()->dispatch(ev))
1568 lyxerr << "A truly unknown func ["
1569 << lyxaction.getActionName(ev.action) << "]!"
1575 sendDispatchMessage(getMessage(), ev, verbose);
1579 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1581 owner->updateMenubar();
1582 owner->updateToolbar();
1584 if (ev.action == LFUN_SELFINSERT || !verbose) {
1585 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1587 owner->message(msg);
1591 string dispatch_msg = msg;
1592 if (!dispatch_msg.empty())
1593 dispatch_msg += ' ';
1595 string comname = lyxaction.getActionName(ev.action);
1597 int pseudoaction = ev.action;
1598 bool argsadded = false;
1600 if (!ev.argument.empty()) {
1601 // the pseudoaction is useful for the bindings
1602 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1604 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1605 pseudoaction = ev.action;
1607 comname += ' ' + ev.argument;
1612 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1614 if (!shortcuts.empty()) {
1615 comname += ": " + shortcuts;
1616 } else if (!argsadded && !ev.argument.empty()) {
1617 comname += ' ' + ev.argument;
1620 if (!comname.empty()) {
1621 comname = rtrim(comname);
1622 dispatch_msg += '(' + comname + ')';
1625 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1626 if (!dispatch_msg.empty())
1627 owner->message(dispatch_msg);
1631 void LyXFunc::setupLocalKeymap()
1633 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1634 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1638 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1640 string initpath = lyxrc.document_path;
1641 string filename(name);
1643 if (view()->available()) {
1644 string const trypath = owner->buffer()->filePath();
1645 // If directory is writeable, use this as default.
1646 if (IsDirWriteable(trypath))
1650 static int newfile_number;
1652 if (filename.empty()) {
1653 filename = AddName(lyxrc.document_path,
1654 "newfile" + tostr(++newfile_number) + ".lyx");
1655 FileInfo fi(filename);
1656 while (bufferlist.exists(filename) || fi.readable()) {
1658 filename = AddName(lyxrc.document_path,
1659 "newfile" + tostr(newfile_number) +
1661 fi.newFile(filename);
1665 // The template stuff
1668 FileDialog fileDlg(_("Select template file"),
1669 LFUN_SELECT_FILE_SYNC,
1670 make_pair(string(_("Documents|#o#O")),
1671 string(lyxrc.document_path)),
1672 make_pair(string(_("Templates|#T#t")),
1673 string(lyxrc.template_path)));
1675 FileDialog::Result result =
1676 fileDlg.open(lyxrc.template_path,
1677 _("*.lyx| LyX Documents (*.lyx)"));
1679 if (result.first == FileDialog::Later)
1682 string const fname = result.second;
1689 view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1693 void LyXFunc::open(string const & fname)
1695 string initpath = lyxrc.document_path;
1697 if (view()->available()) {
1698 string const trypath = owner->buffer()->filePath();
1699 // If directory is writeable, use this as default.
1700 if (IsDirWriteable(trypath))
1706 if (fname.empty()) {
1707 FileDialog fileDlg(_("Select document to open"),
1709 make_pair(string(_("Documents|#o#O")),
1710 string(lyxrc.document_path)),
1711 make_pair(string(_("Examples|#E#e")),
1712 string(AddPath(system_lyxdir, "examples"))));
1714 FileDialog::Result result =
1715 fileDlg.open(initpath,
1716 _("*.lyx| LyX Documents (*.lyx)"));
1718 if (result.first == FileDialog::Later)
1721 filename = result.second;
1723 // check selected filename
1724 if (filename.empty()) {
1725 owner->message(_("Canceled."));
1731 // get absolute path of file and add ".lyx" to the filename if
1733 string const fullpath = FileSearch(string(), filename, "lyx");
1734 if (!fullpath.empty()) {
1735 filename = fullpath;
1738 string const disp_fn(MakeDisplayPath(filename));
1740 // if the file doesn't exist, let the user create one
1741 FileInfo const f(filename, true);
1743 if (!Alert::askQuestion(_("No such file"), disp_fn,
1744 _("Start a new document with this filename ?"))) {
1745 owner->message(_("Canceled."));
1748 // the user specifically chose this name. Believe them.
1749 Buffer * buffer = bufferlist.newFile(filename, "", true);
1750 view()->buffer(buffer);
1755 #if USE_BOOST_FORMAT
1756 str << boost::format(_("Opening document %1$s...")) % disp_fn;
1758 str << _("Opening document ") << disp_fn << _("...");
1761 owner->message(STRCONV(str.str()));
1763 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1766 view()->buffer(openbuf);
1767 #if USE_BOOST_FORMAT
1768 str2 << boost::format(_("Document %1$s opened.")) % disp_fn;
1770 str2 << _("Document ") << disp_fn << _(" opened.");
1773 #if USE_BOOST_FORMAT
1774 str2 << boost::format(_("Could not open document %1$s"))
1777 str2 << _("Could not open document ") << disp_fn;
1780 owner->message(STRCONV(str2.str()));
1784 void LyXFunc::doImport(string const & argument)
1787 string filename = split(argument, format, ' ');
1789 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1790 << " file: " << filename << endl;
1792 // need user interaction
1793 if (filename.empty()) {
1794 string initpath = lyxrc.document_path;
1796 if (view()->available()) {
1797 string const trypath = owner->buffer()->filePath();
1798 // If directory is writeable, use this as default.
1799 if (IsDirWriteable(trypath))
1803 #if USE_BOOST_FORMAT
1804 boost::format fmt(_("Select %1$s file to import"));
1805 fmt % formats.prettyName(format);
1806 string const text = fmt.str();
1808 string const text = _("Select ") + formats.prettyName(format)
1809 + _(" file to import");;
1812 FileDialog fileDlg(text,
1814 make_pair(string(_("Documents|#o#O")),
1815 string(lyxrc.document_path)),
1816 make_pair(string(_("Examples|#E#e")),
1817 string(AddPath(system_lyxdir, "examples"))));
1819 string const extension = "*." + formats.extension(format)
1820 + "| " + formats.prettyName(format)
1821 + " (*." + formats.extension(format) + ')';
1823 FileDialog::Result result = fileDlg.open(initpath,
1826 if (result.first == FileDialog::Later)
1829 filename = result.second;
1831 // check selected filename
1832 if (filename.empty())
1833 owner->message(_("Canceled."));
1836 if (filename.empty())
1839 // get absolute path of file
1840 filename = MakeAbsPath(filename);
1842 string const lyxfile = ChangeExtension(filename, ".lyx");
1844 // Check if the document already is open
1845 if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1846 switch (Alert::askConfirmation(_("Document is already open:"),
1847 MakeDisplayPath(lyxfile, 50),
1848 _("Do you want to close that document now?\n"
1849 "('No' will just switch to the open version)")))
1852 // If close is canceled, we cancel here too.
1853 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1857 view()->buffer(bufferlist.getBuffer(lyxfile));
1860 owner->message(_("Canceled."));
1865 // if the file exists already, and we didn't do
1866 // -i lyx thefile.lyx, warn
1867 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1868 if (!Alert::askQuestion(_("A document by the name"),
1869 MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1870 owner->message(_("Canceled."));
1875 Importer::Import(owner, filename, format);
1879 void LyXFunc::closeBuffer()
1881 if (bufferlist.close(owner->buffer()) && !quitting) {
1882 if (bufferlist.empty()) {
1883 // need this otherwise SEGV may occur while trying to
1884 // set variables that don't exist
1885 // since there's no current buffer
1886 owner->getDialogs().hideBufferDependent();
1888 view()->buffer(bufferlist.first());
1894 // Each "owner" should have it's own message method. lyxview and
1895 // the minibuffer would use the minibuffer, but lyxserver would
1896 // send an ERROR signal to its client. Alejandro 970603
1897 // This func is bit problematic when it comes to NLS, to make the
1898 // lyx servers client be language indepenent we must not translate
1899 // strings sent to this func.
1900 void LyXFunc::setErrorMessage(string const & m) const
1902 dispatch_buffer = m;
1907 void LyXFunc::setMessage(string const & m) const
1909 dispatch_buffer = m;
1913 void LyXFunc::setStatusMessage(string const & m) const
1919 string const LyXFunc::view_status_message()
1921 // When meta-fake key is pressed, show the key sequence so far + "M-".
1923 return keyseq.print() + "M-";
1926 // Else, when a non-complete key sequence is pressed,
1927 // show the available options.
1928 if (keyseq.length() > 0 && !keyseq.deleted()) {
1929 return keyseq.printOptions();
1932 if (!view()->available())
1933 return _("Welcome to LyX!");
1935 return currentState(view());
1939 BufferView * LyXFunc::view() const
1942 return owner->view().get();