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 * ====================================================== */
14 #pragma implementation
21 #include "bufferlist.h"
22 #include "BufferView.h"
23 #include "lyxserver.h"
27 #include "LyXAction.h"
33 #include "trans_mgr.h"
35 #include "bufferview_funcs.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/lyx_gui.h"
39 #include "FloatList.h"
40 #include "converter.h"
43 #include "TextCache.h"
45 #include "undo_funcs.h"
46 #include "ParagraphParameters.h"
48 #include "insets/insetcommand.h"
49 #include "insets/insettabular.h"
51 #include "mathed/formulamacro.h"
52 #include "mathed/math_cursor.h"
53 #include "mathed/math_inset.h"
55 #include "frontends/FileDialog.h"
56 #include "frontends/Dialogs.h"
57 #include "frontends/Toolbar.h"
58 #include "frontends/Menubar.h"
59 #include "frontends/Alert.h"
61 #include "graphics/GraphicsCache.h"
63 #include "support/lyxalgo.h"
64 #include "support/LAssert.h"
65 #include "support/filetools.h"
66 #include "support/FileInfo.h"
67 #include "support/forkedcontr.h"
68 #include "support/lstrings.h"
69 #include "support/path.h"
70 #include "support/lyxfunctional.h"
72 #include "BoostFormat.h"
88 using std::back_inserter;
90 extern BufferList bufferlist;
91 extern LyXServer * lyxserver;
92 extern bool selection_possible;
94 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
97 extern tex_accent_struct get_accent(kb_action action);
99 extern void ShowLatexLog();
102 LyXFunc::LyXFunc(LyXView * o)
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)->bv_owner)
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 = keyseq.getLastKeyEncoded();
139 if (keyseq.length() > 1) {
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 // Do a one-deep top-level lookup for
179 // cancel and meta-fake keys. RVDK_PATCH_5
180 cancel_meta_seq.reset();
182 int action = cancel_meta_seq.addkey(keysym, state);
183 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
185 // When not cancel or meta-fake, do the normal lookup.
186 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
187 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
188 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
189 // remove Caps Lock and Mod2 as a modifiers
190 action = keyseq.addkey(keysym, (state | meta_fake_bit));
191 lyxerr[Debug::KEY] << "action now set to ["
192 << action << ']' << endl;
195 // Dont remove this unless you know what you are doing.
196 meta_fake_bit = key_modifier::none;
198 // can this happen now ?
199 if (action == LFUN_NOACTION) {
200 action = LFUN_PREFIX;
203 if (lyxerr.debugging(Debug::KEY)) {
204 lyxerr << "Key [action="
206 << keyseq.print() << ']'
210 // already here we know if it any point in going further
211 // why not return already here if action == -1 and
212 // num_bytes == 0? (Lgb)
214 if (keyseq.length() > 1) {
215 owner->message(keyseq.print());
219 // Maybe user can only reach the key via holding down shift.
220 // Let's see. But only if shift is the only modifier
221 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
222 lyxerr[Debug::KEY] << "Trying without shift" << endl;
223 action = keyseq.addkey(keysym, key_modifier::none);
224 lyxerr[Debug::KEY] << "Action now " << action << endl;
227 if (action == LFUN_UNKNOWN_ACTION) {
228 // Hmm, we didn't match any of the keysequences. See
229 // if it's normal insertable text not already covered
231 if (keysym->isText() && keyseq.length() == 1) {
232 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
233 action = LFUN_SELFINSERT;
235 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
236 owner->message(_("Unknown function."));
241 if (action == LFUN_SELFINSERT) {
242 char c = keysym->getISOEncoded();
249 dispatch(FuncRequest(view(), LFUN_SELFINSERT, argument));
250 lyxerr[Debug::KEY] << "SelfInsert arg[`"
251 << 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 == "fax" &&
326 !Exporter::IsExportable(buf, ev.argument);
329 disable = buf->undostack.empty();
332 disable = buf->redostack.empty();
336 if (tli && tli->lyxCode() == Inset::TABULAR_CODE) {
337 InsetTabular * t(static_cast<InsetTabular*>(tli));
338 if (t->hasSelection()) {
343 disable = !mathcursor && !view()->getLyXText()->selection.set();
346 disable = lyxrc.chktex_command == "none";
349 disable = !Exporter::IsExportable(buf, "program");
352 case LFUN_LAYOUT_CHARACTER:
353 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
356 case LFUN_LAYOUT_TABULAR:
358 || (tli->lyxCode() != Inset::TABULAR_CODE
359 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
363 case LFUN_LAYOUT_PARAGRAPH: {
364 Inset * inset = TEXT(false)->cursor.par()->inInset();
365 disable = inset && inset->forceDefaultParagraphs(inset);
369 case LFUN_INSET_OPTARG:
370 disable = (TEXT(false)->cursor.par()->layout()->optionalargs == 0);
373 case LFUN_TABULAR_FEATURE:
376 // FIXME: check temporarily disabled
378 char align = mathcursor->valign();
383 if (ev.argument.empty()) {
387 if (!contains("tcb", ev.argument[0])) {
391 flag.setOnOff(ev.argument[0] == align);
395 char align = mathcursor->halign();
400 if (ev.argument.empty()) {
404 if (!contains("lcr", ev.argument[0])) {
408 flag.setOnOff(ev.argument[0] == align);
411 disable = !mathcursor->halign();
417 //ret.disabled(true);
418 if (tli->lyxCode() == Inset::TABULAR_CODE) {
419 ret = static_cast<InsetTabular *>(tli)
420 ->getStatus(ev.argument);
421 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
422 ret = static_cast<InsetTabular *>
423 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
424 ->getStatus(ev.argument);
429 static InsetTabular inset(*owner->buffer(), 1, 1);
433 ret = inset.getStatus(ev.argument);
434 if (ret.onoff(true) || ret.onoff(false))
435 flag.setOnOff(false);
439 case LFUN_VC_REGISTER:
440 disable = buf->lyxvc.inUse();
442 case LFUN_VC_CHECKIN:
443 disable = !buf->lyxvc.inUse() || buf->isReadonly();
445 case LFUN_VC_CHECKOUT:
446 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
450 case LFUN_VC_HISTORY:
451 disable = !buf->lyxvc.inUse();
453 case LFUN_BOOKMARK_GOTO:
455 isSavedPosition(strToUnsignedInt(ev.argument));
457 case LFUN_INSET_TOGGLE: {
458 LyXText * lt = view()->getLyXText();
459 disable = !(isEditableInset(lt->getInset())
461 && lt->inset_owner->owner()
462 && lt->inset_owner->owner()->isOpen()));
466 disable = !IsFileReadable(buf->getLogName().second);
469 case LFUN_MATH_MUTATE:
471 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
472 flag.setOnOff(false);
477 // we just need to be in math mode to enable that
479 case LFUN_MATH_SPACE:
480 case LFUN_MATH_LIMITS:
481 case LFUN_MATH_NONUMBER:
482 case LFUN_MATH_NUMBER:
483 case LFUN_MATH_EXTERN:
484 disable = !mathcursor;
491 // the functions which insert insets
492 Inset::Code code = Inset::NO_CODE;
495 code = Inset::ERT_CODE;
497 case LFUN_INSET_GRAPHICS:
498 code = Inset::GRAPHICS_CODE;
500 case LFUN_INSET_FOOTNOTE:
501 code = Inset::FOOT_CODE;
503 case LFUN_TABULAR_INSERT:
504 code = Inset::TABULAR_CODE;
506 case LFUN_INSET_EXTERNAL:
507 code = Inset::EXTERNAL_CODE;
509 case LFUN_INSET_MARGINAL:
510 code = Inset::MARGIN_CODE;
512 case LFUN_INSET_MINIPAGE:
513 code = Inset::MINIPAGE_CODE;
515 case LFUN_INSET_FLOAT:
516 case LFUN_INSET_WIDE_FLOAT:
517 code = Inset::FLOAT_CODE;
519 case LFUN_INSET_WRAP:
520 code = Inset::WRAP_CODE;
522 case LFUN_FLOAT_LIST:
523 code = Inset::FLOAT_LIST_CODE;
526 case LFUN_INSET_LIST:
527 code = Inset::LIST_CODE;
529 case LFUN_INSET_THEOREM:
530 code = Inset::THEOREM_CODE;
533 case LFUN_INSET_CAPTION:
534 code = Inset::CAPTION_CODE;
536 case LFUN_INSERT_NOTE:
537 code = Inset::NOTE_CODE;
539 case LFUN_INSERT_LABEL:
540 code = Inset::LABEL_CODE;
542 case LFUN_INSET_OPTARG:
543 code = Inset::OPTARG_CODE;
545 case LFUN_REF_INSERT:
546 code = Inset::REF_CODE;
548 case LFUN_CITATION_CREATE:
549 case LFUN_CITATION_INSERT:
550 code = Inset::CITE_CODE;
552 case LFUN_INSERT_BIBTEX:
553 code = Inset::BIBTEX_CODE;
555 case LFUN_INDEX_INSERT:
556 code = Inset::INDEX_CODE;
558 case LFUN_INDEX_PRINT:
559 code = Inset::INDEX_PRINT_CODE;
561 case LFUN_CHILD_INSERT:
562 code = Inset::INCLUDE_CODE;
564 case LFUN_TOC_INSERT:
565 code = Inset::TOC_CODE;
567 case LFUN_PARENTINSERT:
568 code = Inset::PARENT_CODE;
572 case LFUN_INSERT_URL:
573 code = Inset::URL_CODE;
576 // always allow this, since we will inset a raw quote
577 // if an inset is not allowed.
579 case LFUN_HYPHENATION:
580 case LFUN_LIGATURE_BREAK:
582 case LFUN_MENU_SEPARATOR:
584 case LFUN_END_OF_SENTENCE:
585 code = Inset::SPECIALCHAR_CODE;
587 case LFUN_PROTECTEDSPACE:
588 // slight hack: we know this is allowed in math mode
590 code = Inset::SPECIALCHAR_CODE;
595 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
601 // A few general toggles
603 case LFUN_TOOLTIPS_TOGGLE:
604 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
607 case LFUN_READ_ONLY_TOGGLE:
608 flag.setOnOff(buf->isReadonly());
611 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
613 case LFUN_SWITCHBUFFER:
614 // toggle on the current buffer, but do not toggle off
615 // the other ones (is that a good idea?)
616 if (ev.argument == buf->fileName())
623 // the font related toggles
625 LyXFont const & font = TEXT(false)->real_current_font;
628 flag.setOnOff(font.emph() == LyXFont::ON);
631 flag.setOnOff(font.noun() == LyXFont::ON);
634 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
637 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
640 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
643 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
649 string tc = mathcursor->getLastCode();
652 flag.setOnOff(tc == "mathbf");
655 flag.setOnOff(tc == "mathsf");
658 flag.setOnOff(tc == "mathcal");
661 flag.setOnOff(tc == "mathrm");
664 flag.setOnOff(tc == "mathtt");
667 flag.setOnOff(tc == "mathbb");
670 flag.setOnOff(tc == "mathnormal");
677 // this one is difficult to get right. As a half-baked
678 // solution, we consider only the first action of the sequence
679 if (ev.action == LFUN_SEQUENCE) {
680 // argument contains ';'-terminated commands
681 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
688 void LyXFunc::dispatch(string const & s, bool verbose)
690 int const action = lyxaction.LookupFunc(s);
692 if (action == LFUN_UNKNOWN_ACTION) {
694 boost::format fmt(_("Unknown function (%1$s)"));
696 owner->message(fmt.str());
698 string const msg = string(_("Unknown function ("))
705 dispatch(action, verbose);
709 void LyXFunc::dispatch(int ac, bool verbose)
711 dispatch(lyxaction.retrieveActionArg(ac), verbose);
716 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
718 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
719 <<"] arg[" << ev.argument << ']' << endl;
721 // we have not done anything wrong yet.
723 dispatch_buffer.erase();
725 #ifdef NEW_DISPATCHER
726 // We try do call the most specific dispatcher first:
727 // 1. the lockinginset's dispatch
728 // 2. the bufferview's dispatch
729 // 3. the lyxview's dispatch
732 selection_possible = false;
734 if (view()->available())
735 view()->hideCursor();
737 string argument = ev.argument;
738 kb_action action = ev.action;
740 // We cannot use this function here
741 if (getStatus(ev).disabled()) {
742 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
743 << lyxaction.getActionName(action)
744 << " [" << action << "] is disabled at this location"
746 setErrorMessage(getStatusMessage());
747 goto exit_with_message;
750 if (view()->available() && view()->theLockingInset()) {
751 Inset::RESULT result;
752 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
753 (!keyseq.deleted())))
755 UpdatableInset * inset = view()->theLockingInset();
759 inset->getCursorPos(view(), inset_x, dummy_y);
761 if ((action == LFUN_UNKNOWN_ACTION)
762 && argument.empty()) {
763 argument = keyseq.getLastKeyEncoded();
765 // Undo/Redo is a bit tricky for insets.
766 if (action == LFUN_UNDO) {
768 goto exit_with_message;
769 } else if (action == LFUN_REDO) {
771 goto exit_with_message;
772 } else if (((result=inset->
773 // Hand-over to inset's own dispatch:
774 localDispatch(FuncRequest(view(), action, argument))) ==
775 UpdatableInset::DISPATCHED) ||
776 (result == UpdatableInset::DISPATCHED_NOUPDATE))
777 goto exit_with_message;
778 // If UNDISPATCHED, just soldier on
779 else if (result == UpdatableInset::FINISHED) {
780 goto exit_with_message;
781 // We do not need special RTL handling here:
782 // FINISHED means that the cursor should be
783 // one position after the inset.
784 } else if (result == UpdatableInset::FINISHED_RIGHT) {
785 TEXT()->cursorRight(view());
786 moveCursorUpdate(true, false);
787 owner->view_state_changed();
788 goto exit_with_message;
789 } else if (result == UpdatableInset::FINISHED_UP) {
790 if (TEXT()->cursor.irow()->previous()) {
792 TEXT()->setCursorFromCoordinates(
793 view(), TEXT()->cursor.ix() + inset_x,
794 TEXT()->cursor.iy() -
795 TEXT()->cursor.irow()->baseline() - 1);
796 TEXT()->cursor.x_fix(TEXT()->cursor.x());
798 TEXT()->cursorUp(view());
800 moveCursorUpdate(true, false);
801 owner->view_state_changed();
803 view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
805 goto exit_with_message;
806 } else if (result == UpdatableInset::FINISHED_DOWN) {
807 if (TEXT()->cursor.irow()->next()) {
809 TEXT()->setCursorFromCoordinates(
810 view(), TEXT()->cursor.ix() + inset_x,
811 TEXT()->cursor.iy() -
812 TEXT()->cursor.irow()->baseline() +
813 TEXT()->cursor.irow()->height() + 1);
814 TEXT()->cursor.x_fix(TEXT()->cursor.x());
816 TEXT()->cursorDown(view());
819 TEXT()->cursorRight(view());
821 moveCursorUpdate(true, false);
822 owner->view_state_changed();
823 goto exit_with_message;
825 #warning I am not sure this is still right, please have a look! (Jug 20020417)
826 else { // result == UNDISPATCHED
827 //setMessage(N_("Text mode"));
829 case LFUN_UNKNOWN_ACTION:
830 case LFUN_BREAKPARAGRAPH:
832 TEXT()->cursorRight(view());
833 view()->switchKeyMap();
834 owner->view_state_changed();
837 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
838 TEXT()->cursorRight(view());
839 moveCursorUpdate(true, false);
840 owner->view_state_changed();
842 goto exit_with_message;
844 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
845 TEXT()->cursorRight(view());
846 moveCursorUpdate(true, false);
847 owner->view_state_changed();
849 goto exit_with_message;
851 if (TEXT()->cursor.row()->next())
852 TEXT()->cursorDown(view());
854 TEXT()->cursorRight(view());
855 moveCursorUpdate(true, false);
856 owner->view_state_changed();
857 goto exit_with_message;
869 if (!view()->available()) break;
870 // this function should be used always [asierra060396]
871 UpdatableInset * tli =
872 view()->theLockingInset();
874 UpdatableInset * lock = tli->getLockingInset();
877 view()->unlockInset(tli);
878 TEXT()->cursorRight(view());
879 moveCursorUpdate(true, false);
880 owner->view_state_changed();
882 tli->unlockInsetInInset(view(),
887 // Tell the paragraph dialog that we changed paragraph
888 owner->getDialogs().updateParagraph();
893 // --- Misc -------------------------------------------
894 case LFUN_WORDFINDFORWARD :
895 case LFUN_WORDFINDBACKWARD : {
896 static string last_search;
897 string searched_string;
899 if (!argument.empty()) {
900 last_search = argument;
901 searched_string = argument;
903 searched_string = last_search;
905 bool fw = (action == LFUN_WORDFINDFORWARD);
906 if (!searched_string.empty()) {
907 lyxfind::LyXFind(view(), searched_string, fw);
909 // view()->showCursor();
915 if (view()->available() && !view()->theLockingInset()) {
916 view()->update(TEXT(),
917 BufferView::SELECT|BufferView::FITCUR);
919 owner->message(keyseq.printOptions());
923 // --- Misc -------------------------------------------
924 case LFUN_EXEC_COMMAND:
925 owner->focus_command_buffer();
928 case LFUN_CANCEL: // RVDK_PATCH_5
930 meta_fake_bit = key_modifier::none;
931 if (view()->available())
932 // cancel any selection
933 dispatch(LFUN_MARK_OFF);
934 setMessage(N_("Cancel"));
937 case LFUN_META_FAKE: // RVDK_PATCH_5
939 meta_fake_bit = key_modifier::alt;
940 setMessage(keyseq.print());
944 case LFUN_READ_ONLY_TOGGLE:
945 if (owner->buffer()->lyxvc.inUse()) {
946 owner->buffer()->lyxvc.toggleReadOnly();
948 owner->buffer()->setReadonly(
949 !owner->buffer()->isReadonly());
953 case LFUN_CENTER: // this is center and redraw.
957 // --- Menus -----------------------------------------------
959 menuNew(argument, false);
962 case LFUN_MENUNEWTMPLT:
963 menuNew(argument, true);
966 case LFUN_CLOSEBUFFER:
971 if (!owner->buffer()->isUnnamed()) {
974 s1 << boost::format(_("Saving document %1$s..."))
975 % MakeDisplayPath(owner->buffer()->fileName());
977 s1 << _("Saving document ")
978 << MakeDisplayPath(owner->buffer()->fileName())
981 owner->message(STRCONV(s1.str()));
982 MenuWrite(view(), owner->buffer());
984 owner->message(STRCONV(s1.str()));
986 WriteAs(view(), owner->buffer());
990 WriteAs(view(), owner->buffer(), argument);
993 case LFUN_MENURELOAD:
998 Exporter::Export(owner->buffer(), argument, true);
1002 Exporter::Preview(owner->buffer(), argument);
1005 case LFUN_BUILDPROG:
1006 Exporter::Export(owner->buffer(), "program", true);
1009 case LFUN_RUNCHKTEX:
1010 MenuRunChktex(owner->buffer());
1013 case LFUN_MENUPRINT:
1014 owner->getDialogs().showPrint();
1018 if (argument == "custom")
1019 owner->getDialogs().showSendto();
1021 Exporter::Export(owner->buffer(), argument, false);
1039 InsetCommandParams p;
1042 if (action == LFUN_TOCVIEW)
1044 p.setCmdName("tableofcontents");
1046 else if (action == LFUN_LOAVIEW)
1047 p.setCmdName("listof{algorithm}{List of Algorithms}");
1048 else if (action == LFUN_LOFVIEW)
1049 p.setCmdName("listoffigures");
1051 p.setCmdName("listoftables");
1053 owner->getDialogs().createTOC(p.getAsString());
1069 case LFUN_MENUSEARCH:
1070 owner->getDialogs().showSearch();
1073 case LFUN_REMOVEERRORS:
1074 if (view()->removeAutoInsets()) {
1075 #warning repaint() or update() or nothing ?
1077 view()->fitCursor();
1081 case LFUN_DEPTH_MIN:
1082 changeDepth(view(), TEXT(false), -1);
1085 case LFUN_DEPTH_PLUS:
1086 changeDepth(view(), TEXT(false), 1);
1090 owner->getDialogs().setUserFreeFont();
1093 case LFUN_RECONFIGURE:
1094 Reconfigure(view());
1098 case LFUN_FLOATSOPERATE:
1099 if (argument == "openfoot")
1100 view()->allFloats(1,0);
1101 else if (argument == "closefoot")
1102 view()->allFloats(0,0);
1103 else if (argument == "openfig")
1104 view()->allFloats(1,1);
1105 else if (argument == "closefig")
1106 view()->allFloats(0,1);
1109 #ifdef WITH_WARNINGS
1110 //#warning Find another implementation here (or another lyxfunc)!
1113 case LFUN_HELP_ABOUTLYX:
1114 owner->getDialogs().showAboutlyx();
1117 case LFUN_HELP_TEXINFO:
1118 owner->getDialogs().showTexinfo();
1121 case LFUN_HELP_OPEN:
1123 string const arg = argument;
1125 setErrorMessage(N_("Missing argument"));
1128 owner->prohibitInput();
1129 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1130 if (fname.empty()) {
1131 lyxerr << "LyX: unable to find documentation file `"
1132 << arg << "'. Bad installation?" << endl;
1133 owner->allowInput();
1137 #if USE_BOOST_FORMAT
1138 str << boost::format(_("Opening help file %1$s..."))
1139 % MakeDisplayPath(fname);
1141 str << _("Opening help file ")
1142 << MakeDisplayPath(fname) << _("...");
1144 owner->message(STRCONV(str.str()));
1145 view()->buffer(bufferlist.loadLyXFile(fname, false));
1146 owner->allowInput();
1150 // --- version control -------------------------------
1151 case LFUN_VC_REGISTER:
1153 if (!owner->buffer()->lyxvc.inUse())
1154 owner->buffer()->lyxvc.registrer();
1158 case LFUN_VC_CHECKIN:
1160 if (owner->buffer()->lyxvc.inUse()
1161 && !owner->buffer()->isReadonly())
1162 owner->buffer()->lyxvc.checkIn();
1166 case LFUN_VC_CHECKOUT:
1168 if (owner->buffer()->lyxvc.inUse()
1169 && owner->buffer()->isReadonly())
1170 owner->buffer()->lyxvc.checkOut();
1174 case LFUN_VC_REVERT:
1176 owner->buffer()->lyxvc.revert();
1182 owner->buffer()->lyxvc.undoLast();
1186 case LFUN_VC_HISTORY:
1188 owner->getDialogs().showVCLogFile();
1192 // --- buffers ----------------------------------------
1194 case LFUN_SWITCHBUFFER:
1195 view()->buffer(bufferlist.getBuffer(argument));
1200 // servercmd: argument must be <file>:<template>
1201 Buffer * tmpbuf = NewFile(argument);
1203 view()->buffer(tmpbuf);
1207 case LFUN_FILE_OPEN:
1211 case LFUN_LATEX_LOG:
1212 owner->getDialogs().showLogFile();
1215 case LFUN_LAYOUT_DOCUMENT:
1216 owner->getDialogs().showDocument();
1219 case LFUN_LAYOUT_PARAGRAPH:
1220 owner->getDialogs().showParagraph();
1223 case LFUN_LAYOUT_CHARACTER:
1224 owner->getDialogs().showCharacter();
1227 case LFUN_LAYOUT_TABULAR:
1228 if (view()->theLockingInset()) {
1229 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1230 InsetTabular * inset = static_cast<InsetTabular *>
1231 (view()->theLockingInset());
1232 inset->openLayoutDialog(view());
1233 } else if (view()->theLockingInset()->
1234 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1235 InsetTabular * inset = static_cast<InsetTabular *>(
1236 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1237 inset->openLayoutDialog(view());
1242 case LFUN_LAYOUT_PREAMBLE:
1243 owner->getDialogs().showPreamble();
1246 case LFUN_DROP_LAYOUTS_CHOICE:
1247 owner->getToolbar().openLayoutList();
1250 case LFUN_MENU_OPEN_BY_NAME:
1251 owner->getMenubar().openByName(argument);
1252 break; // RVDK_PATCH_5
1254 case LFUN_SPELLCHECK:
1255 owner->getDialogs().showSpellchecker();
1258 // --- lyxserver commands ----------------------------
1262 setMessage(owner->buffer()->fileName());
1263 lyxerr[Debug::INFO] << "FNAME["
1264 << owner->buffer()->fileName()
1270 dispatch_buffer = keyseq.print();
1271 lyxserver->notifyClient(dispatch_buffer);
1275 case LFUN_GOTOFILEROW:
1279 istringstream istr(argument.c_str());
1280 istr >> file_name >> row;
1281 // Must replace extension of the file to be .lyx and get full path
1282 string const s(ChangeExtension(file_name, ".lyx"));
1284 // Either change buffer or load the file
1285 if (bufferlist.exists(s)) {
1286 view()->buffer(bufferlist.getBuffer(s));
1288 view()->buffer(bufferlist.loadLyXFile(s));
1291 view()->setCursorFromRow(row);
1294 // see BufferView_pimpl::center()
1295 view()->updateScrollbar();
1299 case LFUN_GOTO_PARAGRAPH:
1301 istringstream istr(argument.c_str());
1305 Paragraph * par = owner->buffer()->getParFromID(id);
1307 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1308 << id << ']' << endl;
1311 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1312 << " found." << endl;
1315 if (view()->theLockingInset())
1316 view()->unlockInset(view()->theLockingInset());
1317 if (par->inInset()) {
1318 par->inInset()->edit(view());
1321 view()->getLyXText()->setCursor(view(), par, 0);
1322 view()->switchKeyMap();
1323 owner->view_state_changed();
1326 // see BufferView_pimpl::center()
1327 view()->updateScrollbar();
1334 int const qa = lyxaction.LookupFunc(argument);
1335 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1339 // --- insert characters ----------------------------------------
1341 // --- Mathed stuff. If we are here, there is no locked inset yet.
1342 case LFUN_MATH_EXTERN:
1343 case LFUN_MATH_NUMBER:
1344 case LFUN_MATH_NONUMBER:
1345 case LFUN_MATH_LIMITS:
1347 setErrorMessage(N_("This is only allowed in math mode!"));
1351 // passthrough hat and underscore outside mathed:
1352 case LFUN_SUBSCRIPT:
1353 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1355 case LFUN_SUPERSCRIPT:
1356 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1359 case LFUN_MATH_PANEL:
1360 owner->getDialogs().showMathPanel();
1363 case LFUN_CITATION_CREATE:
1365 InsetCommandParams p("cite");
1367 if (!argument.empty()) {
1368 // This should be set at source, ie when typing
1369 // "citation-insert foo" in the minibuffer.
1370 // Question: would pybibliographer also need to be
1371 // changed. Suspect so. Leave as-is therefore.
1372 if (contains(argument, "|")) {
1373 p.setContents(token(argument, '|', 0));
1374 p.setOptions(token(argument, '|', 1));
1376 p.setContents(argument);
1378 dispatch(FuncRequest(view(), LFUN_CITATION_INSERT, p.getAsString()));
1380 owner->getDialogs().createCitation(p.getAsString());
1384 case LFUN_CHILDOPEN:
1386 string const filename =
1387 MakeAbsPath(argument,
1388 owner->buffer()->filePath());
1389 setMessage(N_("Opening child document ") +
1390 MakeDisplayPath(filename) + "...");
1391 view()->savePosition(0);
1392 if (bufferlist.exists(filename))
1393 view()->buffer(bufferlist.getBuffer(filename));
1395 view()->buffer(bufferlist.loadLyXFile(filename));
1399 case LFUN_TOGGLECURSORFOLLOW:
1400 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1404 owner->getIntl().KeyMapOn(false);
1407 case LFUN_KMAP_PRIM:
1408 owner->getIntl().KeyMapPrim();
1412 owner->getIntl().KeyMapSec();
1415 case LFUN_KMAP_TOGGLE:
1416 owner->getIntl().ToggleKeyMap();
1421 // argument contains ';'-terminated commands
1422 while (!argument.empty()) {
1424 argument = split(argument, first, ';');
1430 case LFUN_DIALOG_PREFERENCES:
1431 owner->getDialogs().showPreferences();
1434 case LFUN_SAVEPREFERENCES:
1436 Path p(user_lyxdir);
1437 lyxrc.write("preferences");
1441 case LFUN_SCREEN_FONT_UPDATE:
1443 // handle the screen font changes.
1444 lyxrc.set_font_norm_type();
1445 lyx_gui::update_fonts();
1446 // We also need to empty the textcache so that
1447 // the buffer will be formatted correctly after
1450 // Of course we should only do the resize and the textcache.clear
1451 // if values really changed...but not very important right now. (Lgb)
1452 // All visible buffers will need resize
1458 case LFUN_SET_COLOR:
1461 string const x11_name = split(argument, lyx_name, ' ');
1462 if (lyx_name.empty() || x11_name.empty()) {
1463 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1468 bool const graphicsbg_changed =
1469 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1470 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1472 if (!lcolor.setColor(lyx_name, x11_name)) {
1473 #if USE_BOOST_FORMAT
1477 _("Set-color \"%1$s\" failed "
1478 "- color is undefined or "
1479 "may not be redefined"))
1482 setErrorMessage(_("Set-color ") + lyx_name
1483 + _(" failed - color is undefined"
1484 " or may not be redefined"));
1490 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1492 if (graphicsbg_changed) {
1493 #ifdef WITH_WARNINGS
1494 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1497 grfx::GCache & gc = grfx::GCache::get();
1498 gc.changeDisplay(true);
1507 owner->message(argument);
1510 case LFUN_FORKS_SHOW:
1511 owner->getDialogs().showForks();
1514 case LFUN_FORKS_KILL:
1516 if (!isStrInt(argument))
1519 pid_t const pid = strToInt(argument);
1520 ForkedcallsController & fcc = ForkedcallsController::get();
1525 case LFUN_TOOLTIPS_TOGGLE:
1526 owner->getDialogs().toggleTooltips();
1530 // Then if it was none of the above
1531 // Trying the BufferView::pimpl dispatch:
1532 if (!view()->dispatch(ev))
1533 lyxerr << "A truly unknown func ["
1534 << lyxaction.getActionName(ev.action) << "]!"
1540 sendDispatchMessage(getMessage(), ev, verbose);
1544 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1546 owner->updateMenubar();
1547 owner->updateToolbar();
1549 if (ev.action == LFUN_SELFINSERT || !verbose) {
1550 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1552 owner->message(msg);
1556 string dispatch_msg = msg;
1557 if (!dispatch_msg.empty())
1558 dispatch_msg += ' ';
1560 string comname = lyxaction.getActionName(ev.action);
1562 int pseudoaction = ev.action;
1563 bool argsadded = false;
1565 if (!ev.argument.empty()) {
1566 // the pseudoaction is useful for the bindings
1567 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1569 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1570 pseudoaction = ev.action;
1572 comname += ' ' + ev.argument;
1577 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1579 if (!shortcuts.empty()) {
1580 comname += ": " + shortcuts;
1581 } else if (!argsadded && !ev.argument.empty()) {
1582 comname += ' ' + ev.argument;
1585 if (!comname.empty()) {
1586 comname = rtrim(comname);
1587 dispatch_msg += '(' + comname + ')';
1590 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1591 if (!dispatch_msg.empty())
1592 owner->message(dispatch_msg);
1596 void LyXFunc::setupLocalKeymap()
1598 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1599 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1603 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1605 string initpath = lyxrc.document_path;
1606 string filename(name);
1608 if (view()->available()) {
1609 string const trypath = owner->buffer()->filePath();
1610 // If directory is writeable, use this as default.
1611 if (IsDirWriteable(trypath))
1615 static int newfile_number;
1617 if (filename.empty()) {
1618 filename = AddName(lyxrc.document_path,
1619 "newfile" + tostr(++newfile_number) + ".lyx");
1620 FileInfo fi(filename);
1621 while (bufferlist.exists(filename) || fi.readable()) {
1623 filename = AddName(lyxrc.document_path,
1624 "newfile" + tostr(newfile_number) +
1626 fi.newFile(filename);
1630 // The template stuff
1633 FileDialog fileDlg(owner, _("Select template file"),
1634 LFUN_SELECT_FILE_SYNC,
1635 make_pair(string(_("Documents|#o#O")),
1636 string(lyxrc.document_path)),
1637 make_pair(string(_("Templates|#T#t")),
1638 string(lyxrc.template_path)));
1640 FileDialog::Result result =
1641 fileDlg.open(lyxrc.template_path,
1642 _("*.lyx|LyX Documents (*.lyx)"));
1644 if (result.first == FileDialog::Later)
1647 string const fname = result.second;
1654 view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1658 void LyXFunc::open(string const & fname)
1660 string initpath = lyxrc.document_path;
1662 if (view()->available()) {
1663 string const trypath = owner->buffer()->filePath();
1664 // If directory is writeable, use this as default.
1665 if (IsDirWriteable(trypath))
1671 if (fname.empty()) {
1672 FileDialog fileDlg(owner, _("Select document to open"),
1674 make_pair(string(_("Documents|#o#O")),
1675 string(lyxrc.document_path)),
1676 make_pair(string(_("Examples|#E#e")),
1677 string(AddPath(system_lyxdir, "examples"))));
1679 FileDialog::Result result =
1680 fileDlg.open(initpath,
1681 "*.lyx|LyX Documents (*.lyx)");
1683 if (result.first == FileDialog::Later)
1686 filename = result.second;
1688 // check selected filename
1689 if (filename.empty()) {
1690 owner->message(_("Canceled."));
1696 // get absolute path of file and add ".lyx" to the filename if
1698 string const fullpath = FileSearch(string(), filename, "lyx");
1699 if (!fullpath.empty()) {
1700 filename = fullpath;
1703 string const disp_fn(MakeDisplayPath(filename));
1705 // if the file doesn't exist, let the user create one
1706 FileInfo const f(filename, true);
1708 if (!Alert::askQuestion(_("No such file"), disp_fn,
1709 _("Start a new document with this filename ?"))) {
1710 owner->message(_("Canceled"));
1713 // the user specifically chose this name. Believe them.
1714 Buffer * buffer = bufferlist.newFile(filename, "", true);
1715 view()->buffer(buffer);
1720 #if USE_BOOST_FORMAT
1721 str << boost::format(_("Opening document %1$s...")) % disp_fn;
1723 str << _("Opening document ") << disp_fn << _("...");
1726 owner->message(STRCONV(str.str()));
1728 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1731 view()->buffer(openbuf);
1732 #if USE_BOOST_FORMAT
1733 str2 << boost::format(_("Document %1$s opened.")) % disp_fn;
1735 str2 << _("Document ") << disp_fn << _(" opened.");
1738 #if USE_BOOST_FORMAT
1739 str2 << boost::format(_("Could not open document %1$s"))
1742 str2 << _("Could not open document ") << disp_fn;
1745 owner->message(STRCONV(str2.str()));
1749 void LyXFunc::doImport(string const & argument)
1752 string filename = split(argument, format, ' ');
1754 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1755 << " file: " << filename << endl;
1757 // need user interaction
1758 if (filename.empty()) {
1759 string initpath = lyxrc.document_path;
1761 if (view()->available()) {
1762 string const trypath = owner->buffer()->filePath();
1763 // If directory is writeable, use this as default.
1764 if (IsDirWriteable(trypath))
1768 #if USE_BOOST_FORMAT
1769 boost::format fmt(_("Select %1$s file to import"));
1770 fmt % formats.prettyName(format);
1771 string const text = fmt.str();
1773 string const text = _("Select ") + formats.prettyName(format)
1774 + _(" file to import");;
1777 FileDialog fileDlg(owner, text,
1779 make_pair(string(_("Documents|#o#O")),
1780 string(lyxrc.document_path)),
1781 make_pair(string(_("Examples|#E#e")),
1782 string(AddPath(system_lyxdir, "examples"))));
1784 string const extension = "*." + formats.extension(format)
1785 + "| " + formats.prettyName(format)
1786 + " (*." + formats.extension(format) + ')';
1788 FileDialog::Result result = fileDlg.open(initpath,
1791 if (result.first == FileDialog::Later)
1794 filename = result.second;
1796 // check selected filename
1797 if (filename.empty())
1798 owner->message(_("Canceled."));
1801 if (filename.empty())
1804 // get absolute path of file
1805 filename = MakeAbsPath(filename);
1807 string const lyxfile = ChangeExtension(filename, ".lyx");
1809 // Check if the document already is open
1810 if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1811 switch (Alert::askConfirmation(_("Document is already open:"),
1812 MakeDisplayPath(lyxfile, 50),
1813 _("Do you want to close that document now?\n"
1814 "('No' will just switch to the open version)")))
1817 // If close is canceled, we cancel here too.
1818 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1822 view()->buffer(bufferlist.getBuffer(lyxfile));
1825 owner->message(_("Canceled."));
1830 // if the file exists already, and we didn't do
1831 // -i lyx thefile.lyx, warn
1832 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1833 if (!Alert::askQuestion(_("A document by the name"),
1834 MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1835 owner->message(_("Canceled"));
1840 Importer::Import(owner, filename, format);
1844 void LyXFunc::reloadBuffer()
1846 string const fn = owner->buffer()->fileName();
1847 if (bufferlist.close(owner->buffer()))
1848 view()->buffer(bufferlist.loadLyXFile(fn));
1852 void LyXFunc::closeBuffer()
1854 if (bufferlist.close(owner->buffer()) && !quitting) {
1855 if (bufferlist.empty()) {
1856 // need this otherwise SEGV may occur while trying to
1857 // set variables that don't exist
1858 // since there's no current buffer
1859 owner->getDialogs().hideBufferDependent();
1861 view()->buffer(bufferlist.first());
1867 // Each "owner" should have it's own message method. lyxview and
1868 // the minibuffer would use the minibuffer, but lyxserver would
1869 // send an ERROR signal to its client. Alejandro 970603
1870 // This func is bit problematic when it comes to NLS, to make the
1871 // lyx servers client be language indepenent we must not translate
1872 // strings sent to this func.
1873 void LyXFunc::setErrorMessage(string const & m) const
1875 dispatch_buffer = m;
1880 void LyXFunc::setMessage(string const & m) const
1882 dispatch_buffer = m;
1886 void LyXFunc::setStatusMessage(string const & m) const
1892 string const LyXFunc::view_status_message()
1894 // When meta-fake key is pressed, show the key sequence so far + "M-".
1896 return keyseq.print() + "M-";
1899 // Else, when a non-complete key sequence is pressed,
1900 // show the available options.
1901 if (keyseq.length() > 0 && !keyseq.deleted()) {
1902 return keyseq.printOptions();
1905 if (!view()->available())
1906 return _("Welcome to LyX!");
1908 return currentState(view());
1912 BufferView * LyXFunc::view() const
1915 return owner->view().get();