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/MiniBuffer.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/lyx_gui.h"
40 #include "FloatList.h"
41 #include "converter.h"
44 #include "TextCache.h"
46 #include "undo_funcs.h"
47 #include "ParagraphParameters.h"
49 #include "insets/inseturl.h"
50 #include "insets/insetlatexaccent.h"
51 #include "insets/insettoc.h"
52 #include "insets/insetref.h"
53 #include "insets/insetparent.h"
54 #include "insets/insetindex.h"
55 #include "insets/insetinclude.h"
56 #include "insets/insetbib.h"
57 #include "insets/insetcite.h"
58 #include "insets/insettext.h"
59 #include "insets/insetert.h"
60 #include "insets/insetexternal.h"
61 #include "insets/insetgraphics.h"
62 #include "insets/insetfoot.h"
63 #include "insets/insetmarginal.h"
64 #include "insets/insetminipage.h"
65 #include "insets/insetfloat.h"
67 #include "insets/insetlist.h"
68 #include "insets/insettheorem.h"
70 #include "insets/insettabular.h"
71 #include "insets/insetcaption.h"
73 #include "mathed/formulamacro.h"
74 #include "mathed/math_cursor.h"
75 #include "mathed/math_inset.h"
77 #include "frontends/FileDialog.h"
78 #include "frontends/Dialogs.h"
79 #include "frontends/Toolbar.h"
80 #include "frontends/Menubar.h"
81 #include "frontends/Alert.h"
83 #include "graphics/GraphicsCache.h"
85 #include "support/lyxalgo.h"
86 #include "support/LAssert.h"
87 #include "support/filetools.h"
88 #include "support/FileInfo.h"
89 #include "support/forkedcontr.h"
90 #include "support/lstrings.h"
91 #include "support/path.h"
92 #include "support/lyxfunctional.h"
103 using std::make_pair;
107 using std::transform;
108 using std::back_inserter;
110 extern BufferList bufferlist;
111 extern LyXServer * lyxserver;
112 extern bool selection_possible;
114 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
116 extern void show_symbols_form(LyXFunc *);
118 extern LyXAction lyxaction;
120 extern tex_accent_struct get_accent(kb_action action);
122 extern void ShowLatexLog();
125 /* === globals =========================================================== */
128 LyXFunc::LyXFunc(LyXView * o)
130 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
131 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get())
133 meta_fake_bit = key_modifier::none;
134 lyx_dead_action = LFUN_NOACTION;
135 lyx_calling_dead_action = LFUN_NOACTION;
140 LyXText * LyXFunc::TEXT(bool flag = true) const
143 return owner->view()->text;
144 return owner->view()->getLyXText();
149 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
151 if (selecting || TEXT(flag)->selection.mark()) {
152 TEXT(flag)->setSelection(owner->view());
153 if (TEXT(flag)->bv_owner)
154 owner->view()->toggleToggle();
156 owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
157 owner->view()->showCursor();
159 owner->view()->switchKeyMap();
163 void LyXFunc::handleKeyFunc(kb_action action)
165 char c = keyseq.getLastKeyEncoded();
167 if (keyseq.length() > 1) {
171 owner->getIntl()->getTransManager()
172 .deadkey(c, get_accent(action).accent, TEXT(false));
173 // Need to clear, in case the minibuffer calls these
176 // copied verbatim from do_accent_char
177 owner->view()->update(TEXT(false),
178 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
179 TEXT(false)->selection.cursor = TEXT(false)->cursor;
183 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
184 key_modifier::state state)
188 if (lyxerr.debugging(Debug::KEY)) {
189 lyxerr << "KeySym is "
190 << keysym->getSymbolName()
193 // Do nothing if we have nothing (JMarc)
194 if ( ! keysym->isOK() ) {
195 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
200 if (keysym->isModifier()) {
204 // Do a one-deep top-level lookup for
205 // cancel and meta-fake keys. RVDK_PATCH_5
206 cancel_meta_seq.reset();
208 int action = cancel_meta_seq.addkey(keysym, state);
209 if (lyxerr.debugging(Debug::KEY)) {
210 lyxerr << "action first set to [" << action << "]" << endl;
213 // When not cancel or meta-fake, do the normal lookup.
214 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
215 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
216 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
217 // remove Caps Lock and Mod2 as a modifiers
218 action = keyseq.addkey(keysym, (state | meta_fake_bit));
219 if (lyxerr.debugging(Debug::KEY)) {
220 lyxerr << "action now set to ["
221 << action << "]" << endl;
224 // Dont remove this unless you know what you are doing.
225 meta_fake_bit = key_modifier::none;
227 // can this happen now ?
228 if (action == LFUN_NOACTION) {
229 action = LFUN_PREFIX;
232 if (lyxerr.debugging(Debug::KEY)) {
233 lyxerr << "Key [action="
235 << keyseq.print() << "]"
239 // already here we know if it any point in going further
240 // why not return already here if action == -1 and
241 // num_bytes == 0? (Lgb)
243 if (keyseq.length() > 1) {
244 owner->message(keyseq.print());
247 if (action == LFUN_UNKNOWN_ACTION) {
248 // It is unknown, but what if we remove all
249 // the modifiers? (Lgb)
250 action = keyseq.addkey(keysym, key_modifier::none);
252 if (lyxerr.debugging(Debug::KEY)) {
253 lyxerr << "Removing modifiers...\n"
254 << "Action now set to ["
255 << action << "]" << endl;
257 if (action == LFUN_UNKNOWN_ACTION) {
258 owner->message(_("Unknown function."));
263 if (action == LFUN_SELFINSERT) {
264 // This is very X dependent.
265 char c = keysym->getISOEncoded();
271 dispatch(LFUN_SELFINSERT, argument);
272 lyxerr[Debug::KEY] << "SelfInsert arg[`"
273 << argument << "']" << endl;
275 verboseDispatch(action, false);
280 FuncStatus LyXFunc::getStatus(int ac) const
284 action = lyxaction.retrieveActionArg(ac, argument);
285 return getStatus(action, argument);
289 FuncStatus LyXFunc::getStatus(kb_action action,
290 string const & argument) const
293 Buffer * buf = owner->buffer();
295 if (action == LFUN_NOACTION) {
296 setStatusMessage(N_("Nothing to do"));
297 return flag.disabled(true);
300 if (action == LFUN_UNKNOWN_ACTION) {
301 setStatusMessage(N_("Unknown action"));
302 return flag.unknown(true);
305 // the default error message if we disable the command
306 setStatusMessage(N_("Command disabled"));
308 // Check whether we need a buffer
309 if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
310 // Yes we need a buffer, do we have one?
313 // Can we use a readonly buffer?
314 if (buf->isReadonly() &&
315 !lyxaction.funcHasFlag(action,
316 LyXAction::ReadOnly)) {
318 setStatusMessage(N_("Document is read-only"));
323 setStatusMessage(N_("Command not allowed with"
324 "out any document open"));
325 return flag.disabled(true);
329 UpdatableInset * tli = owner->view()->theLockingInset();
331 // I would really like to avoid having this switch and rather try to
332 // encode this in the function itself.
333 bool disable = false;
336 disable = !Exporter::IsExportable(buf, "dvi")
337 || lyxrc.print_command == "none";
340 disable = argument == "fax" &&
341 !Exporter::IsExportable(buf, argument);
344 disable = buf->undostack.empty();
347 disable = buf->redostack.empty();
349 case LFUN_SPELLCHECK:
350 disable = lyxrc.isp_command == "none";
352 #ifndef HAVE_LIBAIKSAURUS
353 case LFUN_THESAURUS_ENTRY:
358 disable = lyxrc.chktex_command == "none";
361 disable = !Exporter::IsExportable(buf, "program");
364 case LFUN_LAYOUT_TABULAR:
366 || (tli->lyxCode() != Inset::TABULAR_CODE
367 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
371 case LFUN_LAYOUT_PARAGRAPH: {
372 Inset * inset = TEXT(false)->cursor.par()->inInset();
373 disable = inset && inset->forceDefaultParagraphs(inset);
377 case LFUN_TABULAR_FEATURE:
381 //ret.disabled(true);
382 if (tli->lyxCode() == Inset::TABULAR_CODE) {
383 ret = static_cast<InsetTabular *>(tli)
384 ->getStatus(argument);
385 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
386 ret = static_cast<InsetTabular *>
387 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
388 ->getStatus(argument);
393 static InsetTabular inset(*owner->buffer(), 1, 1);
397 ret = inset.getStatus(argument);
398 if (ret.onoff(true) || ret.onoff(false))
399 flag.setOnOff(false);
403 case LFUN_VC_REGISTER:
404 disable = buf->lyxvc.inUse();
406 case LFUN_VC_CHECKIN:
407 disable = !buf->lyxvc.inUse() || buf->isReadonly();
409 case LFUN_VC_CHECKOUT:
410 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
414 case LFUN_VC_HISTORY:
415 disable = !buf->lyxvc.inUse();
417 case LFUN_BOOKMARK_GOTO:
418 disable = !owner->view()->
419 isSavedPosition(strToUnsignedInt(argument));
423 case LFUN_INSET_TOGGLE: {
424 LyXText * lt = owner->view()->getLyXText();
425 disable = !(isEditableInset(lt->getInset())
427 && lt->inset_owner->owner()
428 && lt->inset_owner->owner()->isOpen()));
431 case LFUN_MATH_VALIGN:
432 if (mathcursor && mathcursor->formula()->hullType() != "simple") {
433 char align = mathcursor->valign();
438 if (argument.empty()) {
442 if (!contains("tcb", argument[0])) {
446 flag.setOnOff(argument[0] == align);
451 case LFUN_MATH_HALIGN:
452 if (mathcursor && mathcursor->formula()->hullType() != "simple") {
453 char align = mathcursor->halign();
458 if (argument.empty()) {
462 if (!contains("lcr", argument[0])) {
466 flag.setOnOff(argument[0] == align);
471 case LFUN_MATH_MUTATE:
472 if (tli && (tli->lyxCode() == Inset::MATH_CODE))
473 flag.setOnOff(mathcursor->formula()->hullType() == argument);
478 // we just need to be in math mode to enable that
480 case LFUN_MATH_SPACE:
481 case LFUN_MATH_LIMITS:
482 case LFUN_MATH_NONUMBER:
483 case LFUN_MATH_NUMBER:
484 disable = !mathcursor;
487 // we need to be math mode and a math array for that
488 // Hack: halign produces non-zero result iff we are in a math array
489 case LFUN_MATH_ROW_INSERT:
490 case LFUN_MATH_ROW_DELETE:
491 case LFUN_MATH_COLUMN_INSERT:
492 case LFUN_MATH_COLUMN_DELETE:
493 disable = !mathcursor || !mathcursor->halign() ||
494 mathcursor->formula()->hullType() == "simple";
501 // the functions which insert insets
502 Inset::Code code = Inset::NO_CODE;
505 code = Inset::ERT_CODE;
507 case LFUN_INSET_GRAPHICS:
508 code = Inset::GRAPHICS_CODE;
510 case LFUN_INSET_FOOTNOTE:
511 code = Inset::FOOT_CODE;
513 case LFUN_DIALOG_TABULAR_INSERT:
514 case LFUN_INSET_TABULAR:
515 code = Inset::TABULAR_CODE;
517 case LFUN_INSET_EXTERNAL:
518 code = Inset::EXTERNAL_CODE;
520 case LFUN_INSET_MARGINAL:
521 code = Inset::MARGIN_CODE;
523 case LFUN_INSET_MINIPAGE:
524 code = Inset::MINIPAGE_CODE;
526 case LFUN_INSET_FLOAT:
527 case LFUN_INSET_WIDE_FLOAT:
528 code = Inset::FLOAT_CODE;
530 case LFUN_FLOAT_LIST:
531 code = Inset::FLOAT_LIST_CODE;
534 case LFUN_INSET_LIST:
535 code = Inset::LIST_CODE;
537 case LFUN_INSET_THEOREM:
538 code = Inset::THEOREM_CODE;
541 case LFUN_INSET_CAPTION:
542 code = Inset::CAPTION_CODE;
544 case LFUN_INSERT_NOTE:
545 code = Inset::IGNORE_CODE;
547 case LFUN_INSERT_LABEL:
548 code = Inset::LABEL_CODE;
550 case LFUN_REF_INSERT:
551 code = Inset::REF_CODE;
553 case LFUN_CITATION_CREATE:
554 case LFUN_CITATION_INSERT:
555 code = Inset::CITE_CODE;
557 case LFUN_INSERT_BIBTEX:
558 code = Inset::BIBTEX_CODE;
560 case LFUN_INDEX_INSERT:
561 code = Inset::INDEX_CODE;
563 case LFUN_INDEX_PRINT:
564 code = Inset::INDEX_PRINT_CODE;
566 case LFUN_CHILD_INSERT:
567 code = Inset::INCLUDE_CODE;
569 case LFUN_TOC_INSERT:
570 code = Inset::TOC_CODE;
572 case LFUN_PARENTINSERT:
573 code = Inset::PARENT_CODE;
577 case LFUN_INSERT_URL:
578 code = Inset::URL_CODE;
581 // always allow this, since we will inset a raw quote
582 // if an inset is not allowed.
584 case LFUN_HYPHENATION:
585 case LFUN_LIGATURE_BREAK:
587 case LFUN_MENU_SEPARATOR:
589 case LFUN_END_OF_SENTENCE:
590 code = Inset::SPECIALCHAR_CODE;
592 case LFUN_PROTECTEDSPACE:
593 // slight hack: we know this is allowed in math mode
595 code = Inset::SPECIALCHAR_CODE;
600 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code)) {
607 // A few general toggles
609 case LFUN_TOOLTIPS_TOGGLE:
610 flag.setOnOff(owner->getDialogs()->tooltipsEnabled());
613 case LFUN_READ_ONLY_TOGGLE:
614 flag.setOnOff(buf->isReadonly());
617 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
619 case LFUN_SWITCHBUFFER:
620 // toggle on the current buffer, but do not toggle off
621 // the other ones (is that a good idea?)
622 if (argument == buf->fileName())
629 // the font related toggles
631 LyXFont const & font = TEXT(false)->real_current_font;
634 flag.setOnOff(font.emph() == LyXFont::ON);
637 flag.setOnOff(font.noun() == LyXFont::ON);
640 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
643 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
646 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
649 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
656 string tc = mathcursor->getLastCode();
659 flag.setOnOff(tc == "mathbf");
662 flag.setOnOff(tc == "mathsf");
665 flag.setOnOff(tc == "mathcal");
668 flag.setOnOff(tc == "mathrm");
671 flag.setOnOff(tc == "mathtt");
674 flag.setOnOff(tc == "mathbb");
677 flag.setOnOff(tc == "mathnormal");
688 // temporary dispatch method
689 void LyXFunc::miniDispatch(string const & s)
691 string s2(frontStrip(strip(s)));
694 verboseDispatch(s2, true);
699 void LyXFunc::verboseDispatch(string const & s, bool show_sc)
701 int action = lyxaction.LookupFunc(frontStrip(s));
703 if (action == LFUN_UNKNOWN_ACTION) {
704 string const msg = string(_("Unknown function ("))
708 verboseDispatch(action, show_sc);
713 void LyXFunc::verboseDispatch(int ac, bool show_sc)
718 // get the real action and argument
719 action = lyxaction.retrieveActionArg(ac, argument);
721 verboseDispatch(action, argument, show_sc);
726 void LyXFunc::verboseDispatch(kb_action action,
727 string const & argument, bool show_sc)
729 string res = dispatch(action, argument);
731 commandshortcut.erase();
733 if (show_sc && action != LFUN_SELFINSERT) {
734 // Put name of command and list of shortcuts
735 // for it in minibuffer
736 string comname = lyxaction.getActionName(action);
738 int pseudoaction = action;
739 bool argsadded = false;
741 if (!argument.empty()) {
742 // the pseudoaction is useful for the bindings
744 lyxaction.searchActionArg(action,
747 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
748 pseudoaction = action;
750 comname += " " + argument;
755 string const shortcuts =
756 toplevel_keymap->findbinding(pseudoaction);
758 if (!shortcuts.empty()) {
759 comname += ": " + shortcuts;
760 } else if (!argsadded && !argument.empty()) {
761 comname += " " + argument;
764 if (!comname.empty()) {
765 comname = strip(comname);
766 commandshortcut = "(" + comname + ')';
771 if (!commandshortcut.empty()) {
772 owner->getMiniBuffer()->addSet(commandshortcut);
775 owner->getMiniBuffer()->addSet(' ' + commandshortcut);
780 string const LyXFunc::dispatch(kb_action action, string argument)
782 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << action
783 <<"] arg[" << argument << "]" << endl;
785 // we have not done anything wrong yet.
787 dispatch_buffer.erase();
789 #ifdef NEW_DISPATCHER
790 // We try do call the most specific dispatcher first:
791 // 1. the lockinginset's dispatch
792 // 2. the bufferview's dispatch
793 // 3. the lyxview's dispatch
796 selection_possible = false;
798 if (owner->view()->available())
799 owner->view()->hideCursor();
801 // We cannot use this function here
802 if (getStatus(action, argument).disabled()) {
803 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: "
804 << lyxaction.getActionName(action)
805 << " [" << action << "] is disabled at this location"
807 setErrorMessage(getStatusMessage());
808 goto exit_with_message;
811 if (owner->view()->available() && owner->view()->theLockingInset()) {
812 UpdatableInset::RESULT result;
813 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
814 (!keyseq.deleted())))
816 UpdatableInset * inset = owner->view()->theLockingInset();
820 inset->getCursorPos(owner->view(), inset_x, dummy_y);
822 if ((action == LFUN_UNKNOWN_ACTION)
823 && argument.empty()) {
824 argument = keyseq.getLastKeyEncoded();
826 // Undo/Redo is a bit tricky for insets.
827 if (action == LFUN_UNDO) {
828 owner->view()->menuUndo();
829 goto exit_with_message;
830 } else if (action == LFUN_REDO) {
831 owner->view()->menuRedo();
832 goto exit_with_message;
833 } else if (((result=inset->
834 // Hand-over to inset's own dispatch:
835 localDispatch(owner->view(), action, argument)) ==
836 UpdatableInset::DISPATCHED) ||
837 (result == UpdatableInset::DISPATCHED_NOUPDATE))
838 goto exit_with_message;
839 // If UNDISPATCHED, just soldier on
840 else if (result == UpdatableInset::FINISHED) {
841 goto exit_with_message;
842 // We do not need special RTL handling here:
843 // FINISHED means that the cursor should be
844 // one position after the inset.
845 } else if (result == UpdatableInset::FINISHED_RIGHT) {
846 TEXT()->cursorRight(owner->view());
847 moveCursorUpdate(true, false);
848 owner->view_state_changed();
849 goto exit_with_message;
850 } else if (result == UpdatableInset::FINISHED_UP) {
851 if (TEXT()->cursor.irow()->previous()) {
853 TEXT()->setCursorFromCoordinates(
854 owner->view(), TEXT()->cursor.ix() + inset_x,
855 TEXT()->cursor.iy() -
856 TEXT()->cursor.irow()->baseline() - 1);
857 TEXT()->cursor.x_fix(TEXT()->cursor.x());
859 TEXT()->cursorUp(owner->view());
861 moveCursorUpdate(true, false);
862 owner->view_state_changed();
864 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
866 goto exit_with_message;
867 } else if (result == UpdatableInset::FINISHED_DOWN) {
868 if (TEXT()->cursor.irow()->next()) {
870 TEXT()->setCursorFromCoordinates(
871 owner->view(), TEXT()->cursor.ix() + inset_x,
872 TEXT()->cursor.iy() -
873 TEXT()->cursor.irow()->baseline() +
874 TEXT()->cursor.irow()->height() + 1);
875 TEXT()->cursor.x_fix(TEXT()->cursor.x());
877 TEXT()->cursorDown(owner->view());
880 TEXT()->cursorRight(owner->view());
882 moveCursorUpdate(true, false);
883 owner->view_state_changed();
884 goto exit_with_message;
886 #warning I am not sure this is still right, please have a look! (Jug 20020417)
887 else { // result == UNDISPATCHED
888 //setMessage(N_("Text mode"));
890 case LFUN_UNKNOWN_ACTION:
891 case LFUN_BREAKPARAGRAPH:
893 TEXT()->cursorRight(owner->view());
894 owner->view()->switchKeyMap();
895 owner->view_state_changed();
898 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
899 TEXT()->cursorRight(owner->view());
900 moveCursorUpdate(true, false);
901 owner->view_state_changed();
903 goto exit_with_message;
905 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
906 TEXT()->cursorRight(owner->view());
907 moveCursorUpdate(true, false);
908 owner->view_state_changed();
910 goto exit_with_message;
912 if (TEXT()->cursor.row()->next())
913 TEXT()->cursorDown(owner->view());
915 TEXT()->cursorRight(owner->view());
916 moveCursorUpdate(true, false);
917 owner->view_state_changed();
918 goto exit_with_message;
930 if (!owner->view()->available()) break;
931 // this function should be used always [asierra060396]
932 UpdatableInset * tli =
933 owner->view()->theLockingInset();
935 UpdatableInset * lock = tli->getLockingInset();
938 owner->view()->unlockInset(tli);
939 TEXT()->cursorRight(owner->view());
940 moveCursorUpdate(true, false);
941 owner->view_state_changed();
943 tli->unlockInsetInInset(owner->view(),
948 // Tell the paragraph dialog that we changed paragraph
949 owner->getDialogs()->updateParagraph();
954 // --- Misc -------------------------------------------
955 case LFUN_WORDFINDFORWARD :
956 case LFUN_WORDFINDBACKWARD : {
957 static string last_search;
958 string searched_string;
960 if (!argument.empty()) {
961 last_search = argument;
962 searched_string = argument;
964 searched_string = last_search;
966 bool fw = (action == LFUN_WORDFINDBACKWARD);
967 if (!searched_string.empty()) {
968 lyxfind::LyXFind(owner->view(), searched_string, fw);
970 // owner->view()->showCursor();
976 if (owner->view()->available() && !owner->view()->theLockingInset()) {
977 owner->view()->update(TEXT(),
978 BufferView::SELECT|BufferView::FITCUR);
980 owner->message(keyseq.printOptions());
984 // --- Misc -------------------------------------------
985 case LFUN_EXEC_COMMAND:
987 vector<string> allCmds;
988 transform(lyxaction.func_begin(), lyxaction.func_end(),
989 back_inserter(allCmds), lyx::firster());
990 static vector<string> hist;
991 owner->getMiniBuffer()->prepareForInput(allCmds, hist);
995 case LFUN_CANCEL: // RVDK_PATCH_5
997 meta_fake_bit = key_modifier::none;
998 if (owner->view()->available())
999 // cancel any selection
1000 dispatch(LFUN_MARK_OFF);
1001 setMessage(N_("Cancel"));
1004 case LFUN_META_FAKE: // RVDK_PATCH_5
1006 meta_fake_bit = key_modifier::alt;
1007 setMessage(keyseq.print());
1011 case LFUN_READ_ONLY_TOGGLE:
1012 if (owner->buffer()->lyxvc.inUse()) {
1013 owner->buffer()->lyxvc.toggleReadOnly();
1015 owner->buffer()->setReadonly(
1016 !owner->buffer()->isReadonly());
1020 case LFUN_CENTER: // this is center and redraw.
1021 owner->view()->center();
1024 // --- Menus -----------------------------------------------
1026 menuNew(argument, false);
1029 case LFUN_MENUNEWTMPLT:
1030 menuNew(argument, true);
1033 case LFUN_CLOSEBUFFER:
1037 case LFUN_MENUWRITE:
1038 if (!owner->buffer()->isUnnamed()) {
1040 s1 << _("Saving document") << ' '
1041 << MakeDisplayPath(owner->buffer()->fileName() + "...");
1042 owner->message(s1.str().c_str());
1043 MenuWrite(owner->view(), owner->buffer());
1045 WriteAs(owner->view(), owner->buffer());
1049 WriteAs(owner->view(), owner->buffer(), argument);
1052 case LFUN_MENURELOAD:
1057 Exporter::Export(owner->buffer(), argument, true);
1061 Exporter::Preview(owner->buffer(), argument);
1064 case LFUN_BUILDPROG:
1065 Exporter::Export(owner->buffer(), "program", true);
1068 case LFUN_RUNCHKTEX:
1069 MenuRunChktex(owner->buffer());
1072 case LFUN_MENUPRINT:
1073 owner->getDialogs()->showPrint();
1077 if (argument == "custom")
1078 owner->getDialogs()->showSendto();
1080 Exporter::Export(owner->buffer(), argument, false);
1098 InsetCommandParams p;
1101 if (action == LFUN_TOCVIEW)
1103 p.setCmdName("tableofcontents");
1105 else if (action == LFUN_LOAVIEW)
1106 p.setCmdName("listof{algorithm}{List of Algorithms}");
1107 else if (action == LFUN_LOFVIEW)
1108 p.setCmdName("listoffigures");
1110 p.setCmdName("listoftables");
1112 owner->getDialogs()->createTOC(p.getAsString());
1116 case LFUN_DIALOG_TABULAR_INSERT:
1117 owner->getDialogs()->showTabularCreate();
1121 AutoSave(owner->view());
1125 owner->view()->menuUndo();
1129 owner->view()->menuRedo();
1132 case LFUN_MENUSEARCH:
1133 owner->getDialogs()->showSearch();
1136 case LFUN_REMOVEERRORS:
1137 if (owner->view()->removeAutoInsets()) {
1138 #warning repaint() or update() or nothing ?
1139 owner->view()->repaint();
1140 owner->view()->fitCursor();
1144 case LFUN_DEPTH_MIN:
1145 changeDepth(owner->view(), TEXT(false), -1);
1148 case LFUN_DEPTH_PLUS:
1149 changeDepth(owner->view(), TEXT(false), 1);
1153 owner->getDialogs()->setUserFreeFont();
1156 case LFUN_RECONFIGURE:
1157 Reconfigure(owner->view());
1161 case LFUN_FLOATSOPERATE:
1162 if (argument == "openfoot")
1163 owner->view()->allFloats(1,0);
1164 else if (argument == "closefoot")
1165 owner->view()->allFloats(0,0);
1166 else if (argument == "openfig")
1167 owner->view()->allFloats(1,1);
1168 else if (argument == "closefig")
1169 owner->view()->allFloats(0,1);
1172 #ifdef WITH_WARNINGS
1173 //#warning Find another implementation here (or another lyxfunc)!
1176 case LFUN_HELP_ABOUTLYX:
1177 owner->getDialogs()->showAboutlyx();
1180 case LFUN_HELP_TEXINFO:
1181 owner->getDialogs()->showTexinfo();
1184 case LFUN_HELP_OPEN:
1186 string const arg = argument;
1188 setErrorMessage(N_("Missing argument"));
1191 owner->prohibitInput();
1192 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1193 if (fname.empty()) {
1194 lyxerr << "LyX: unable to find documentation file `"
1195 << arg << "'. Bad installation?" << endl;
1196 owner->allowInput();
1200 str << _("Opening help file") << ' '
1201 << MakeDisplayPath(fname) << "...";
1202 owner->message(str.str().c_str());
1203 owner->view()->buffer(bufferlist.loadLyXFile(fname, false));
1204 owner->allowInput();
1208 // --- version control -------------------------------
1209 case LFUN_VC_REGISTER:
1211 if (!owner->buffer()->lyxvc.inUse())
1212 owner->buffer()->lyxvc.registrer();
1216 case LFUN_VC_CHECKIN:
1218 if (owner->buffer()->lyxvc.inUse()
1219 && !owner->buffer()->isReadonly())
1220 owner->buffer()->lyxvc.checkIn();
1224 case LFUN_VC_CHECKOUT:
1226 if (owner->buffer()->lyxvc.inUse()
1227 && owner->buffer()->isReadonly())
1228 owner->buffer()->lyxvc.checkOut();
1232 case LFUN_VC_REVERT:
1234 owner->buffer()->lyxvc.revert();
1240 owner->buffer()->lyxvc.undoLast();
1244 case LFUN_VC_HISTORY:
1246 owner->getDialogs()->showVCLogFile();
1250 // --- buffers ----------------------------------------
1252 case LFUN_SWITCHBUFFER:
1253 owner->view()->buffer(bufferlist.getBuffer(argument));
1258 // servercmd: argument must be <file>:<template>
1259 Buffer * tmpbuf = NewLyxFile(argument);
1261 owner->view()->buffer(tmpbuf);
1265 case LFUN_FILE_OPEN:
1269 case LFUN_LATEX_LOG:
1270 owner->getDialogs()->showLogFile();
1273 case LFUN_LAYOUT_DOCUMENT:
1274 owner->getDialogs()->showDocument();
1277 case LFUN_LAYOUT_PARAGRAPH:
1278 owner->getDialogs()->showParagraph();
1281 case LFUN_LAYOUT_CHARACTER:
1282 owner->getDialogs()->showCharacter();
1285 case LFUN_LAYOUT_TABULAR:
1286 if (owner->view()->theLockingInset()) {
1287 if (owner->view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1288 InsetTabular * inset = static_cast<InsetTabular *>
1289 (owner->view()->theLockingInset());
1290 inset->openLayoutDialog(owner->view());
1291 } else if (owner->view()->theLockingInset()->
1292 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1293 InsetTabular * inset = static_cast<InsetTabular *>(
1294 owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1295 inset->openLayoutDialog(owner->view());
1300 case LFUN_LAYOUT_PREAMBLE:
1301 owner->getDialogs()->showPreamble();
1304 case LFUN_DROP_LAYOUTS_CHOICE:
1305 owner->getToolbar()->openLayoutList();
1308 case LFUN_MENU_OPEN_BY_NAME:
1309 owner->getMenubar()->openByName(argument);
1310 break; // RVDK_PATCH_5
1312 case LFUN_SPELLCHECK:
1313 if (lyxrc.isp_command != "none")
1314 owner->getDialogs()->showSpellchecker();
1317 // --- lyxserver commands ----------------------------
1321 setMessage(owner->buffer()->fileName());
1322 lyxerr[Debug::INFO] << "FNAME["
1323 << owner->buffer()->fileName()
1329 dispatch_buffer = keyseq.print();
1330 lyxserver->notifyClient(dispatch_buffer);
1334 case LFUN_GOTOFILEROW:
1338 istringstream istr(argument.c_str());
1339 istr >> file_name >> row;
1340 // Must replace extension of the file to be .lyx and get full path
1341 string const s(ChangeExtension(file_name, ".lyx"));
1343 // Either change buffer or load the file
1344 if (bufferlist.exists(s)) {
1345 owner->view()->buffer(bufferlist.getBuffer(s));
1347 owner->view()->buffer(bufferlist.loadLyXFile(s));
1350 owner->view()->setCursorFromRow(row);
1352 owner->view()->center();
1353 // see BufferView_pimpl::center()
1354 owner->view()->updateScrollbar();
1358 case LFUN_GOTO_PARAGRAPH:
1360 istringstream istr(argument.c_str());
1364 Paragraph * par = owner->buffer()->getParFromID(id);
1366 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1367 << id << "]" << endl;
1370 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1371 << " found." << endl;
1374 if (owner->view()->theLockingInset())
1375 owner->view()->unlockInset(owner->view()->theLockingInset());
1376 if (par->inInset()) {
1377 par->inInset()->edit(owner->view());
1380 owner->view()->getLyXText()->setCursor(owner->view(), par, 0);
1381 owner->view()->switchKeyMap();
1382 owner->view_state_changed();
1384 owner->view()->center();
1385 // see BufferView_pimpl::center()
1386 owner->view()->updateScrollbar();
1393 int const qa = lyxaction.LookupFunc(argument);
1394 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1398 // --- toolbar ----------------------------------
1399 case LFUN_PUSH_TOOLBAR:
1401 int nth = strToInt(argument);
1403 setErrorMessage(N_("Push-toolbar needs argument > 0"));
1405 owner->getToolbar()->push(nth);
1410 case LFUN_ADD_TO_TOOLBAR:
1412 if (lyxerr.debugging(Debug::GUI)) {
1413 lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1414 "argument = `" << argument << '\'' << endl;
1416 string tmp(argument);
1417 //lyxerr <<string("Argument: ") + argument);
1418 //lyxerr <<string("Tmp : ") + tmp);
1420 setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1422 owner->getToolbar()->add(argument, false);
1423 owner->getToolbar()->set();
1428 // --- insert characters ----------------------------------------
1430 // --- Mathed stuff. If we are here, there is no locked inset yet.
1431 case LFUN_MATH_EXTERN:
1432 case LFUN_MATH_NUMBER:
1433 case LFUN_MATH_NONUMBER:
1434 case LFUN_MATH_LIMITS:
1436 setErrorMessage(N_("This is only allowed in math mode!"));
1440 // passthrough hat and underscore outside mathed:
1441 case LFUN_SUBSCRIPT:
1442 dispatch(LFUN_SELFINSERT, "_");
1444 case LFUN_SUPERSCRIPT:
1445 dispatch(LFUN_SELFINSERT, "^");
1448 case LFUN_MATH_PANEL:
1449 owner->getDialogs()->showMathPanel();
1452 case LFUN_CITATION_CREATE:
1454 InsetCommandParams p("cite");
1456 if (!argument.empty()) {
1457 // This should be set at source, ie when typing
1458 // "citation-insert foo" in the minibuffer.
1459 // Question: would pybibliographer also need to be
1460 // changed. Suspect so. Leave as-is therefore.
1461 if (contains(argument, "|")) {
1462 p.setContents(token(argument, '|', 0));
1463 p.setOptions( token(argument, '|', 1));
1465 p.setContents(argument);
1467 dispatch(LFUN_CITATION_INSERT, p.getAsString());
1469 owner->getDialogs()->createCitation(p.getAsString());
1473 case LFUN_CHILDOPEN:
1475 string const filename =
1476 MakeAbsPath(argument,
1477 owner->buffer()->filePath());
1478 setMessage(N_("Opening child document ") +
1479 MakeDisplayPath(filename) + "...");
1480 owner->view()->savePosition(0);
1481 if (bufferlist.exists(filename))
1482 owner->view()->buffer(bufferlist.getBuffer(filename));
1484 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1488 case LFUN_TOGGLECURSORFOLLOW:
1489 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1492 case LFUN_KMAP_OFF: // keymap off
1493 owner->getIntl()->KeyMapOn(false);
1496 case LFUN_KMAP_PRIM: // primary keymap
1497 owner->getIntl()->KeyMapPrim();
1500 case LFUN_KMAP_SEC: // secondary keymap
1501 owner->getIntl()->KeyMapSec();
1504 case LFUN_KMAP_TOGGLE: // toggle keymap
1505 owner->getIntl()->ToggleKeyMap();
1510 // argument contains ';'-terminated commands
1511 while (!argument.empty()) {
1513 argument = split(argument, first, ';');
1514 verboseDispatch(first, false);
1519 case LFUN_DIALOG_PREFERENCES:
1520 owner->getDialogs()->showPreferences();
1523 case LFUN_SAVEPREFERENCES:
1525 Path p(user_lyxdir);
1526 lyxrc.write("preferences");
1530 case LFUN_SCREEN_FONT_UPDATE:
1532 // handle the screen font changes.
1534 lyxrc.set_font_norm_type();
1535 lyx_gui::update_fonts();
1536 // Of course we should only do the resize and the textcache.clear
1537 // if values really changed...but not very important right now. (Lgb)
1538 // All visible buffers will need resize
1539 owner->view()->resize();
1540 owner->view()->repaint();
1541 // We also need to empty the textcache so that
1542 // the buffer will be formatted correctly after
1548 case LFUN_SET_COLOR:
1551 string const x11_name = split(argument, lyx_name, ' ');
1552 if (lyx_name.empty() || x11_name.empty()) {
1553 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1558 bool const graphicsbg_changed =
1559 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1560 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1562 if (!lcolor.setColor(lyx_name, x11_name)) {
1563 static string const err1 (N_("Set-color \""));
1564 static string const err2 (
1565 N_("\" failed - color is undefined "
1566 "or may not be redefined"));
1567 setErrorMessage(_(err1) + lyx_name + _(err2));
1571 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1573 if (graphicsbg_changed) {
1574 #ifdef WITH_WARNINGS
1575 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1578 grfx::GCache & gc = grfx::GCache::get();
1579 gc.changeDisplay(true);
1583 owner->view()->repaint();
1588 owner->message(argument);
1591 case LFUN_MESSAGE_PUSH:
1592 owner->messagePush(argument);
1595 case LFUN_MESSAGE_POP:
1596 owner->messagePop();
1599 case LFUN_FORKS_SHOW:
1600 owner->getDialogs()->showForks();
1603 case LFUN_FORKS_KILL:
1605 if (!isStrInt(argument))
1608 pid_t const pid = strToInt(argument);
1609 ForkedcallsController & fcc = ForkedcallsController::get();
1614 case LFUN_TOOLTIPS_TOGGLE:
1615 owner->getDialogs()->toggleTooltips();
1619 // Then if it was none of the above
1620 // Trying the BufferView::pimpl dispatch:
1621 if (!owner->view()->Dispatch(action, argument))
1622 lyxerr << "A truly unknown func ["
1623 << lyxaction.getActionName(action) << "]!"
1630 string const res = getMessage();
1633 owner->message(_(res));
1634 owner->updateMenubar();
1635 owner->updateToolbar();
1641 void LyXFunc::setupLocalKeymap()
1643 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1644 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1648 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1650 string initpath = lyxrc.document_path;
1651 string filename(name);
1653 if (owner->view()->available()) {
1654 string const trypath = owner->buffer()->filePath();
1655 // If directory is writeable, use this as default.
1656 if (IsDirWriteable(trypath))
1660 static int newfile_number;
1662 if (filename.empty()) {
1663 filename = AddName(lyxrc.document_path,
1664 "newfile" + tostr(++newfile_number) + ".lyx");
1665 FileInfo fi(filename);
1666 while (bufferlist.exists(filename) || fi.readable()) {
1668 filename = AddName(lyxrc.document_path,
1669 "newfile" + tostr(newfile_number) +
1671 fi.newFile(filename);
1675 // The template stuff
1678 FileDialog fileDlg(owner, _("Select template file"),
1679 LFUN_SELECT_FILE_SYNC,
1680 make_pair(string(_("Documents|#o#O")),
1681 string(lyxrc.document_path)),
1682 make_pair(string(_("Templates|#T#t")),
1683 string(lyxrc.template_path)));
1685 FileDialog::Result result =
1686 fileDlg.Select(lyxrc.template_path,
1687 _("*.lyx|LyX Documents (*.lyx)"));
1689 if (result.first == FileDialog::Later)
1692 string const fname = result.second;
1699 owner->view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1703 void LyXFunc::open(string const & fname)
1705 string initpath = lyxrc.document_path;
1707 if (owner->view()->available()) {
1708 string const trypath = owner->buffer()->filePath();
1709 // If directory is writeable, use this as default.
1710 if (IsDirWriteable(trypath))
1716 if (fname.empty()) {
1717 FileDialog fileDlg(owner, _("Select document to open"),
1719 make_pair(string(_("Documents|#o#O")),
1720 string(lyxrc.document_path)),
1721 make_pair(string(_("Examples|#E#e")),
1722 string(AddPath(system_lyxdir, "examples"))));
1724 FileDialog::Result result =
1725 fileDlg.Select(initpath,
1726 "*.lyx|LyX Documents (*.lyx)");
1728 if (result.first == FileDialog::Later)
1731 filename = result.second;
1733 // check selected filename
1734 if (filename.empty()) {
1735 owner->message(_("Canceled."));
1741 // get absolute path of file and add ".lyx" to the filename if
1743 string const fullpath = FileSearch(string(), filename, "lyx");
1744 if (fullpath.empty()) {
1745 Alert::alert(_("Error"), _("Could not find file"), filename);
1749 filename = fullpath;
1752 string const disp_fn(MakeDisplayPath(filename));
1755 str << _("Opening document") << ' ' << disp_fn << "...";
1757 owner->message(str.str().c_str());
1759 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1761 owner->view()->buffer(openbuf);
1763 str << _("Document") << ' ' << disp_fn << ' ' << _("opened.");
1764 owner->message(str.str().c_str());
1767 str << _("Could not open document") << ' ' << disp_fn;
1768 owner->message(str.str().c_str());
1773 void LyXFunc::doImport(string const & argument)
1776 string filename = split(argument, format, ' ');
1778 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1779 << " file: " << filename << endl;
1781 // need user interaction
1782 if (filename.empty()) {
1783 string initpath = lyxrc.document_path;
1785 if (owner->view()->available()) {
1786 string const trypath = owner->buffer()->filePath();
1787 // If directory is writeable, use this as default.
1788 if (IsDirWriteable(trypath))
1792 string const text = _("Select ") + formats.prettyName(format)
1793 + _(" file to import");
1795 FileDialog fileDlg(owner, text,
1797 make_pair(string(_("Documents|#o#O")),
1798 string(lyxrc.document_path)),
1799 make_pair(string(_("Examples|#E#e")),
1800 string(AddPath(system_lyxdir, "examples"))));
1802 string const extension = "*." + formats.extension(format)
1803 + "| " + formats.prettyName(format)
1804 + " (*." + formats.extension(format) + ")";
1806 FileDialog::Result result = fileDlg.Select(initpath,
1809 if (result.first == FileDialog::Later)
1812 filename = result.second;
1814 // check selected filename
1815 if (filename.empty())
1816 owner->message(_("Canceled."));
1819 if (filename.empty())
1822 // get absolute path of file
1823 filename = MakeAbsPath(filename);
1825 string const lyxfile = ChangeExtension(filename, ".lyx");
1827 // Check if the document already is open
1828 if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1829 switch (Alert::askConfirmation(_("Document is already open:"),
1830 MakeDisplayPath(lyxfile, 50),
1831 _("Do you want to close that document now?\n"
1832 "('No' will just switch to the open version)")))
1835 // If close is canceled, we cancel here too.
1836 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1840 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1843 owner->message(_("Canceled."));
1848 // if the file exists already, and we didn't do
1849 // -i lyx thefile.lyx, warn
1850 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1851 if (!Alert::askQuestion(_("A document by the name"),
1852 MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1853 owner->message(_("Canceled"));
1858 Importer::Import(owner, filename, format);
1862 void LyXFunc::reloadBuffer()
1864 string const fn = owner->buffer()->fileName();
1865 if (bufferlist.close(owner->buffer()))
1866 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1870 void LyXFunc::closeBuffer()
1872 if (bufferlist.close(owner->buffer()) && !quitting) {
1873 if (bufferlist.empty()) {
1874 // need this otherwise SEGV may occur while trying to
1875 // set variables that don't exist
1876 // since there's no current buffer
1877 owner->getDialogs()->hideBufferDependent();
1879 owner->view()->buffer(bufferlist.first());
1885 // Each "owner" should have it's own message method. lyxview and
1886 // the minibuffer would use the minibuffer, but lyxserver would
1887 // send an ERROR signal to its client. Alejandro 970603
1888 // This func is bit problematic when it comes to NLS, to make the
1889 // lyx servers client be language indepenent we must not translate
1890 // strings sent to this func.
1891 void LyXFunc::setErrorMessage(string const & m) const
1893 dispatch_buffer = m;
1898 void LyXFunc::setMessage(string const & m) const
1900 dispatch_buffer = m;
1904 void LyXFunc::setStatusMessage(string const & m) const
1910 void LyXFunc::initMiniBuffer()
1912 string text = _("Welcome to LyX!");
1914 // When meta-fake key is pressed, show the key sequence so far + "M-".
1916 text = keyseq.print();
1920 // Else, when a non-complete key sequence is pressed,
1921 // show the available options.
1922 if (keyseq.length() > 0 && !keyseq.deleted()) {
1923 text = keyseq.printOptions();
1926 // Else, show the buffer state.
1927 else if (owner->view()->available()) {
1928 Buffer * tmpbuf = owner->buffer();
1930 string const nicename =
1931 MakeDisplayPath(tmpbuf->fileName());
1932 // Should we do this instead? (kindo like emacs)
1933 // leaves more room for other information
1936 if (tmpbuf->lyxvc.inUse()) {
1938 text += tmpbuf->lyxvc.versionString();
1940 text += tmpbuf->lyxvc.locker();
1941 if (tmpbuf->isReadonly())
1944 } else if (tmpbuf->isReadonly())
1946 if (!tmpbuf->isLyxClean())
1947 text += _(" (Changed)");
1949 if (text != _("Welcome to LyX!")) // this is a hack
1950 text = _("* No document open *");
1953 owner->message(text);