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/inseturl.h"
49 #include "insets/insetlatexaccent.h"
50 #include "insets/insettoc.h"
51 #include "insets/insetref.h"
52 #include "insets/insetparent.h"
53 #include "insets/insetindex.h"
54 #include "insets/insetinclude.h"
55 #include "insets/insetbib.h"
56 #include "insets/insetcite.h"
57 #include "insets/insettext.h"
58 #include "insets/insetert.h"
59 #include "insets/insetexternal.h"
60 #include "insets/insetgraphics.h"
61 #include "insets/insetfoot.h"
62 #include "insets/insetmarginal.h"
63 #include "insets/insetminipage.h"
64 #include "insets/insetfloat.h"
66 #include "insets/insetlist.h"
67 #include "insets/insettheorem.h"
69 #include "insets/insettabular.h"
70 #include "insets/insetcaption.h"
72 #include "mathed/formulamacro.h"
73 #include "mathed/math_cursor.h"
74 #include "mathed/math_inset.h"
76 #include "frontends/FileDialog.h"
77 #include "frontends/Dialogs.h"
78 #include "frontends/Toolbar.h"
79 #include "frontends/Menubar.h"
80 #include "frontends/Alert.h"
82 #include "graphics/GraphicsCache.h"
84 #include "support/lyxalgo.h"
85 #include "support/LAssert.h"
86 #include "support/filetools.h"
87 #include "support/FileInfo.h"
88 #include "support/forkedcontr.h"
89 #include "support/lstrings.h"
90 #include "support/path.h"
91 #include "support/lyxfunctional.h"
102 using std::make_pair;
106 using std::transform;
107 using std::back_inserter;
109 extern BufferList bufferlist;
110 extern LyXServer * lyxserver;
111 extern bool selection_possible;
113 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
115 extern void show_symbols_form(LyXFunc *);
117 extern LyXAction lyxaction;
119 extern tex_accent_struct get_accent(kb_action action);
121 extern void ShowLatexLog();
124 /* === globals =========================================================== */
127 LyXFunc::LyXFunc(LyXView * o)
129 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
130 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get())
132 meta_fake_bit = key_modifier::none;
133 lyx_dead_action = LFUN_NOACTION;
134 lyx_calling_dead_action = LFUN_NOACTION;
139 LyXText * LyXFunc::TEXT(bool flag = true) const
142 return owner->view()->text;
143 return owner->view()->getLyXText();
148 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
150 if (selecting || TEXT(flag)->selection.mark()) {
151 TEXT(flag)->setSelection(owner->view());
152 if (TEXT(flag)->bv_owner)
153 owner->view()->toggleToggle();
155 owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
156 owner->view()->showCursor();
158 owner->view()->switchKeyMap();
162 void LyXFunc::handleKeyFunc(kb_action action)
164 char c = keyseq.getLastKeyEncoded();
166 if (keyseq.length() > 1) {
170 owner->getIntl()->getTransManager()
171 .deadkey(c, get_accent(action).accent, TEXT(false));
172 // Need to clear, in case the minibuffer calls these
175 // copied verbatim from do_accent_char
176 owner->view()->update(TEXT(false),
177 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
178 TEXT(false)->selection.cursor = TEXT(false)->cursor;
182 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
183 key_modifier::state state)
187 if (lyxerr.debugging(Debug::KEY)) {
188 lyxerr << "KeySym is "
189 << keysym->getSymbolName()
192 // Do nothing if we have nothing (JMarc)
193 if ( ! keysym->isOK() ) {
194 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
199 if (keysym->isModifier()) {
203 // Do a one-deep top-level lookup for
204 // cancel and meta-fake keys. RVDK_PATCH_5
205 cancel_meta_seq.reset();
207 int action = cancel_meta_seq.addkey(keysym, state);
208 if (lyxerr.debugging(Debug::KEY)) {
209 lyxerr << "action first set to [" << action << "]" << endl;
212 // When not cancel or meta-fake, do the normal lookup.
213 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
214 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
215 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
216 // remove Caps Lock and Mod2 as a modifiers
217 action = keyseq.addkey(keysym, (state | meta_fake_bit));
218 if (lyxerr.debugging(Debug::KEY)) {
219 lyxerr << "action now set to ["
220 << action << "]" << endl;
223 // Dont remove this unless you know what you are doing.
224 meta_fake_bit = key_modifier::none;
226 // can this happen now ?
227 if (action == LFUN_NOACTION) {
228 action = LFUN_PREFIX;
231 if (lyxerr.debugging(Debug::KEY)) {
232 lyxerr << "Key [action="
234 << keyseq.print() << "]"
238 // already here we know if it any point in going further
239 // why not return already here if action == -1 and
240 // num_bytes == 0? (Lgb)
242 if (keyseq.length() > 1) {
243 owner->message(keyseq.print());
246 if (action == LFUN_UNKNOWN_ACTION) {
247 // It is unknown, but what if we remove all
248 // the modifiers? (Lgb)
249 action = keyseq.addkey(keysym, key_modifier::none);
251 if (lyxerr.debugging(Debug::KEY)) {
252 lyxerr << "Removing modifiers...\n"
253 << "Action now set to ["
254 << action << "]" << endl;
256 if (action == LFUN_UNKNOWN_ACTION) {
257 owner->message(_("Unknown function."));
262 if (action == LFUN_SELFINSERT) {
263 // This is very X dependent.
264 char c = keysym->getISOEncoded();
270 dispatch(LFUN_SELFINSERT, argument);
271 lyxerr[Debug::KEY] << "SelfInsert arg[`"
272 << argument << "']" << endl;
279 FuncStatus LyXFunc::getStatus(int ac) const
283 action = lyxaction.retrieveActionArg(ac, argument);
284 return getStatus(action, argument);
288 FuncStatus LyXFunc::getStatus(kb_action action,
289 string const & argument) const
292 Buffer * buf = owner->buffer();
294 if (action == LFUN_NOACTION) {
295 setStatusMessage(N_("Nothing to do"));
296 return flag.disabled(true);
299 if (action == LFUN_UNKNOWN_ACTION) {
300 setStatusMessage(N_("Unknown action"));
301 return flag.unknown(true);
304 // the default error message if we disable the command
305 setStatusMessage(N_("Command disabled"));
307 // Check whether we need a buffer
308 if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
309 // Yes we need a buffer, do we have one?
312 // Can we use a readonly buffer?
313 if (buf->isReadonly() &&
314 !lyxaction.funcHasFlag(action,
315 LyXAction::ReadOnly)) {
317 setStatusMessage(N_("Document is read-only"));
322 setStatusMessage(N_("Command not allowed with"
323 "out any document open"));
324 return flag.disabled(true);
328 UpdatableInset * tli = owner->view()->theLockingInset();
330 // I would really like to avoid having this switch and rather try to
331 // encode this in the function itself.
332 bool disable = false;
335 disable = !Exporter::IsExportable(buf, "dvi")
336 || lyxrc.print_command == "none";
339 disable = argument == "fax" &&
340 !Exporter::IsExportable(buf, argument);
343 disable = buf->undostack.empty();
346 disable = buf->redostack.empty();
348 case LFUN_SPELLCHECK:
349 disable = lyxrc.isp_command == "none";
351 #ifndef HAVE_LIBAIKSAURUS
352 case LFUN_THESAURUS_ENTRY:
357 disable = lyxrc.chktex_command == "none";
360 disable = !Exporter::IsExportable(buf, "program");
363 case LFUN_LAYOUT_TABULAR:
365 || (tli->lyxCode() != Inset::TABULAR_CODE
366 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
370 case LFUN_LAYOUT_PARAGRAPH: {
371 Inset * inset = TEXT(false)->cursor.par()->inInset();
372 disable = inset && inset->forceDefaultParagraphs(inset);
376 case LFUN_TABULAR_FEATURE:
380 //ret.disabled(true);
381 if (tli->lyxCode() == Inset::TABULAR_CODE) {
382 ret = static_cast<InsetTabular *>(tli)
383 ->getStatus(argument);
384 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
385 ret = static_cast<InsetTabular *>
386 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
387 ->getStatus(argument);
392 static InsetTabular inset(*owner->buffer(), 1, 1);
396 ret = inset.getStatus(argument);
397 if (ret.onoff(true) || ret.onoff(false))
398 flag.setOnOff(false);
402 case LFUN_VC_REGISTER:
403 disable = buf->lyxvc.inUse();
405 case LFUN_VC_CHECKIN:
406 disable = !buf->lyxvc.inUse() || buf->isReadonly();
408 case LFUN_VC_CHECKOUT:
409 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
413 case LFUN_VC_HISTORY:
414 disable = !buf->lyxvc.inUse();
416 case LFUN_BOOKMARK_GOTO:
417 disable = !owner->view()->
418 isSavedPosition(strToUnsignedInt(argument));
420 case LFUN_INSET_TOGGLE: {
421 LyXText * lt = owner->view()->getLyXText();
422 disable = !(isEditableInset(lt->getInset())
424 && lt->inset_owner->owner()
425 && lt->inset_owner->owner()->isOpen()));
429 disable = !IsFileReadable(buf->getLogName().second);
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);
655 string tc = mathcursor->getLastCode();
658 flag.setOnOff(tc == "mathbf");
661 flag.setOnOff(tc == "mathsf");
664 flag.setOnOff(tc == "mathcal");
667 flag.setOnOff(tc == "mathrm");
670 flag.setOnOff(tc == "mathtt");
673 flag.setOnOff(tc == "mathbb");
676 flag.setOnOff(tc == "mathnormal");
683 // this one is difficult to get right. As a half-baked
684 // solution, we consider only the first action of the sequence
685 if (action == LFUN_SEQUENCE) {
686 // argument contains ';'-terminated commands
687 const int ac = lyxaction.LookupFunc(token(argument, ';', 0));
688 flag = getStatus(ac);
695 void LyXFunc::dispatch(string const & s, bool verbose)
697 int const action = lyxaction.LookupFunc(s);
699 if (action == LFUN_UNKNOWN_ACTION) {
700 string const msg = string(_("Unknown function ("))
706 dispatch(action, verbose);
710 void LyXFunc::dispatch(int ac, bool verbose)
713 kb_action const action = lyxaction.retrieveActionArg(ac, argument);
714 dispatch(action, argument, verbose);
719 void LyXFunc::dispatch(kb_action action, string argument, bool verbose)
721 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << action
722 <<"] arg[" << argument << "]" << endl;
724 // we have not done anything wrong yet.
726 dispatch_buffer.erase();
728 #ifdef NEW_DISPATCHER
729 // We try do call the most specific dispatcher first:
730 // 1. the lockinginset's dispatch
731 // 2. the bufferview's dispatch
732 // 3. the lyxview's dispatch
735 selection_possible = false;
737 if (owner->view()->available())
738 owner->view()->hideCursor();
740 // We cannot use this function here
741 if (getStatus(action, argument).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 (owner->view()->available() && owner->view()->theLockingInset()) {
751 UpdatableInset::RESULT result;
752 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
753 (!keyseq.deleted())))
755 UpdatableInset * inset = owner->view()->theLockingInset();
759 inset->getCursorPos(owner->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) {
767 owner->view()->menuUndo();
768 goto exit_with_message;
769 } else if (action == LFUN_REDO) {
770 owner->view()->menuRedo();
771 goto exit_with_message;
772 } else if (((result=inset->
773 // Hand-over to inset's own dispatch:
774 localDispatch(owner->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(owner->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 owner->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(owner->view());
800 moveCursorUpdate(true, false);
801 owner->view_state_changed();
803 owner->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 owner->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(owner->view());
819 TEXT()->cursorRight(owner->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(owner->view());
833 owner->view()->switchKeyMap();
834 owner->view_state_changed();
837 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
838 TEXT()->cursorRight(owner->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(owner->view());
846 moveCursorUpdate(true, false);
847 owner->view_state_changed();
849 goto exit_with_message;
851 if (TEXT()->cursor.row()->next())
852 TEXT()->cursorDown(owner->view());
854 TEXT()->cursorRight(owner->view());
855 moveCursorUpdate(true, false);
856 owner->view_state_changed();
857 goto exit_with_message;
869 if (!owner->view()->available()) break;
870 // this function should be used always [asierra060396]
871 UpdatableInset * tli =
872 owner->view()->theLockingInset();
874 UpdatableInset * lock = tli->getLockingInset();
877 owner->view()->unlockInset(tli);
878 TEXT()->cursorRight(owner->view());
879 moveCursorUpdate(true, false);
880 owner->view_state_changed();
882 tli->unlockInsetInInset(owner->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_WORDFINDBACKWARD);
906 if (!searched_string.empty()) {
907 lyxfind::LyXFind(owner->view(), searched_string, fw);
909 // owner->view()->showCursor();
915 if (owner->view()->available() && !owner->view()->theLockingInset()) {
916 owner->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 (owner->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.
954 owner->view()->center();
957 // --- Menus -----------------------------------------------
959 menuNew(argument, false);
962 case LFUN_MENUNEWTMPLT:
963 menuNew(argument, true);
966 case LFUN_CLOSEBUFFER:
971 if (!owner->buffer()->isUnnamed()) {
973 s1 << _("Saving document") << ' '
974 << MakeDisplayPath(owner->buffer()->fileName() + "...");
975 owner->message(s1.str().c_str());
976 MenuWrite(owner->view(), owner->buffer());
978 owner->message(s1.str().c_str());
980 WriteAs(owner->view(), owner->buffer());
984 WriteAs(owner->view(), owner->buffer(), argument);
987 case LFUN_MENURELOAD:
992 Exporter::Export(owner->buffer(), argument, true);
996 Exporter::Preview(owner->buffer(), argument);
1000 Exporter::Export(owner->buffer(), "program", true);
1003 case LFUN_RUNCHKTEX:
1004 MenuRunChktex(owner->buffer());
1007 case LFUN_MENUPRINT:
1008 owner->getDialogs()->showPrint();
1012 if (argument == "custom")
1013 owner->getDialogs()->showSendto();
1015 Exporter::Export(owner->buffer(), argument, false);
1033 InsetCommandParams p;
1036 if (action == LFUN_TOCVIEW)
1038 p.setCmdName("tableofcontents");
1040 else if (action == LFUN_LOAVIEW)
1041 p.setCmdName("listof{algorithm}{List of Algorithms}");
1042 else if (action == LFUN_LOFVIEW)
1043 p.setCmdName("listoffigures");
1045 p.setCmdName("listoftables");
1047 owner->getDialogs()->createTOC(p.getAsString());
1051 case LFUN_DIALOG_TABULAR_INSERT:
1052 owner->getDialogs()->showTabularCreate();
1056 AutoSave(owner->view());
1060 owner->view()->menuUndo();
1064 owner->view()->menuRedo();
1067 case LFUN_MENUSEARCH:
1068 owner->getDialogs()->showSearch();
1071 case LFUN_REMOVEERRORS:
1072 if (owner->view()->removeAutoInsets()) {
1073 #warning repaint() or update() or nothing ?
1074 owner->view()->repaint();
1075 owner->view()->fitCursor();
1079 case LFUN_DEPTH_MIN:
1080 changeDepth(owner->view(), TEXT(false), -1);
1083 case LFUN_DEPTH_PLUS:
1084 changeDepth(owner->view(), TEXT(false), 1);
1088 owner->getDialogs()->setUserFreeFont();
1091 case LFUN_RECONFIGURE:
1092 Reconfigure(owner->view());
1096 case LFUN_FLOATSOPERATE:
1097 if (argument == "openfoot")
1098 owner->view()->allFloats(1,0);
1099 else if (argument == "closefoot")
1100 owner->view()->allFloats(0,0);
1101 else if (argument == "openfig")
1102 owner->view()->allFloats(1,1);
1103 else if (argument == "closefig")
1104 owner->view()->allFloats(0,1);
1107 #ifdef WITH_WARNINGS
1108 //#warning Find another implementation here (or another lyxfunc)!
1111 case LFUN_HELP_ABOUTLYX:
1112 owner->getDialogs()->showAboutlyx();
1115 case LFUN_HELP_TEXINFO:
1116 owner->getDialogs()->showTexinfo();
1119 case LFUN_HELP_OPEN:
1121 string const arg = argument;
1123 setErrorMessage(N_("Missing argument"));
1126 owner->prohibitInput();
1127 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1128 if (fname.empty()) {
1129 lyxerr << "LyX: unable to find documentation file `"
1130 << arg << "'. Bad installation?" << endl;
1131 owner->allowInput();
1135 str << _("Opening help file") << ' '
1136 << MakeDisplayPath(fname) << "...";
1137 owner->message(str.str().c_str());
1138 owner->view()->buffer(bufferlist.loadLyXFile(fname, false));
1139 owner->allowInput();
1143 // --- version control -------------------------------
1144 case LFUN_VC_REGISTER:
1146 if (!owner->buffer()->lyxvc.inUse())
1147 owner->buffer()->lyxvc.registrer();
1151 case LFUN_VC_CHECKIN:
1153 if (owner->buffer()->lyxvc.inUse()
1154 && !owner->buffer()->isReadonly())
1155 owner->buffer()->lyxvc.checkIn();
1159 case LFUN_VC_CHECKOUT:
1161 if (owner->buffer()->lyxvc.inUse()
1162 && owner->buffer()->isReadonly())
1163 owner->buffer()->lyxvc.checkOut();
1167 case LFUN_VC_REVERT:
1169 owner->buffer()->lyxvc.revert();
1175 owner->buffer()->lyxvc.undoLast();
1179 case LFUN_VC_HISTORY:
1181 owner->getDialogs()->showVCLogFile();
1185 // --- buffers ----------------------------------------
1187 case LFUN_SWITCHBUFFER:
1188 owner->view()->buffer(bufferlist.getBuffer(argument));
1193 // servercmd: argument must be <file>:<template>
1194 Buffer * tmpbuf = NewLyxFile(argument);
1196 owner->view()->buffer(tmpbuf);
1200 case LFUN_FILE_OPEN:
1204 case LFUN_LATEX_LOG:
1205 owner->getDialogs()->showLogFile();
1208 case LFUN_LAYOUT_DOCUMENT:
1209 owner->getDialogs()->showDocument();
1212 case LFUN_LAYOUT_PARAGRAPH:
1213 owner->getDialogs()->showParagraph();
1216 case LFUN_LAYOUT_CHARACTER:
1217 owner->getDialogs()->showCharacter();
1220 case LFUN_LAYOUT_TABULAR:
1221 if (owner->view()->theLockingInset()) {
1222 if (owner->view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1223 InsetTabular * inset = static_cast<InsetTabular *>
1224 (owner->view()->theLockingInset());
1225 inset->openLayoutDialog(owner->view());
1226 } else if (owner->view()->theLockingInset()->
1227 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1228 InsetTabular * inset = static_cast<InsetTabular *>(
1229 owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1230 inset->openLayoutDialog(owner->view());
1235 case LFUN_LAYOUT_PREAMBLE:
1236 owner->getDialogs()->showPreamble();
1239 case LFUN_DROP_LAYOUTS_CHOICE:
1240 owner->getToolbar()->openLayoutList();
1243 case LFUN_MENU_OPEN_BY_NAME:
1244 owner->getMenubar()->openByName(argument);
1245 break; // RVDK_PATCH_5
1247 case LFUN_SPELLCHECK:
1248 if (lyxrc.isp_command != "none")
1249 owner->getDialogs()->showSpellchecker();
1252 // --- lyxserver commands ----------------------------
1256 setMessage(owner->buffer()->fileName());
1257 lyxerr[Debug::INFO] << "FNAME["
1258 << owner->buffer()->fileName()
1264 dispatch_buffer = keyseq.print();
1265 lyxserver->notifyClient(dispatch_buffer);
1269 case LFUN_GOTOFILEROW:
1273 istringstream istr(argument.c_str());
1274 istr >> file_name >> row;
1275 // Must replace extension of the file to be .lyx and get full path
1276 string const s(ChangeExtension(file_name, ".lyx"));
1278 // Either change buffer or load the file
1279 if (bufferlist.exists(s)) {
1280 owner->view()->buffer(bufferlist.getBuffer(s));
1282 owner->view()->buffer(bufferlist.loadLyXFile(s));
1285 owner->view()->setCursorFromRow(row);
1287 owner->view()->center();
1288 // see BufferView_pimpl::center()
1289 owner->view()->updateScrollbar();
1293 case LFUN_GOTO_PARAGRAPH:
1295 istringstream istr(argument.c_str());
1299 Paragraph * par = owner->buffer()->getParFromID(id);
1301 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1302 << id << "]" << endl;
1305 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1306 << " found." << endl;
1309 if (owner->view()->theLockingInset())
1310 owner->view()->unlockInset(owner->view()->theLockingInset());
1311 if (par->inInset()) {
1312 par->inInset()->edit(owner->view());
1315 owner->view()->getLyXText()->setCursor(owner->view(), par, 0);
1316 owner->view()->switchKeyMap();
1317 owner->view_state_changed();
1319 owner->view()->center();
1320 // see BufferView_pimpl::center()
1321 owner->view()->updateScrollbar();
1328 int const qa = lyxaction.LookupFunc(argument);
1329 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1333 // --- insert characters ----------------------------------------
1335 // --- Mathed stuff. If we are here, there is no locked inset yet.
1336 case LFUN_MATH_EXTERN:
1337 case LFUN_MATH_NUMBER:
1338 case LFUN_MATH_NONUMBER:
1339 case LFUN_MATH_LIMITS:
1341 setErrorMessage(N_("This is only allowed in math mode!"));
1345 // passthrough hat and underscore outside mathed:
1346 case LFUN_SUBSCRIPT:
1347 dispatch(LFUN_SELFINSERT, "_", false);
1349 case LFUN_SUPERSCRIPT:
1350 dispatch(LFUN_SELFINSERT, "^", false);
1353 case LFUN_MATH_PANEL:
1354 owner->getDialogs()->showMathPanel();
1357 case LFUN_CITATION_CREATE:
1359 InsetCommandParams p("cite");
1361 if (!argument.empty()) {
1362 // This should be set at source, ie when typing
1363 // "citation-insert foo" in the minibuffer.
1364 // Question: would pybibliographer also need to be
1365 // changed. Suspect so. Leave as-is therefore.
1366 if (contains(argument, "|")) {
1367 p.setContents(token(argument, '|', 0));
1368 p.setOptions( token(argument, '|', 1));
1370 p.setContents(argument);
1372 dispatch(LFUN_CITATION_INSERT, p.getAsString());
1374 owner->getDialogs()->createCitation(p.getAsString());
1378 case LFUN_CHILDOPEN:
1380 string const filename =
1381 MakeAbsPath(argument,
1382 owner->buffer()->filePath());
1383 setMessage(N_("Opening child document ") +
1384 MakeDisplayPath(filename) + "...");
1385 owner->view()->savePosition(0);
1386 if (bufferlist.exists(filename))
1387 owner->view()->buffer(bufferlist.getBuffer(filename));
1389 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1393 case LFUN_TOGGLECURSORFOLLOW:
1394 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1397 case LFUN_KMAP_OFF: // keymap off
1398 owner->getIntl()->KeyMapOn(false);
1401 case LFUN_KMAP_PRIM: // primary keymap
1402 owner->getIntl()->KeyMapPrim();
1405 case LFUN_KMAP_SEC: // secondary keymap
1406 owner->getIntl()->KeyMapSec();
1409 case LFUN_KMAP_TOGGLE: // toggle keymap
1410 owner->getIntl()->ToggleKeyMap();
1415 // argument contains ';'-terminated commands
1416 while (!argument.empty()) {
1418 argument = split(argument, first, ';');
1424 case LFUN_DIALOG_PREFERENCES:
1425 owner->getDialogs()->showPreferences();
1428 case LFUN_SAVEPREFERENCES:
1430 Path p(user_lyxdir);
1431 lyxrc.write("preferences");
1435 case LFUN_SCREEN_FONT_UPDATE:
1437 // handle the screen font changes.
1439 lyxrc.set_font_norm_type();
1440 lyx_gui::update_fonts();
1441 // Of course we should only do the resize and the textcache.clear
1442 // if values really changed...but not very important right now. (Lgb)
1443 // All visible buffers will need resize
1444 owner->view()->resize();
1445 owner->view()->repaint();
1446 // We also need to empty the textcache so that
1447 // the buffer will be formatted correctly after
1453 case LFUN_SET_COLOR:
1456 string const x11_name = split(argument, lyx_name, ' ');
1457 if (lyx_name.empty() || x11_name.empty()) {
1458 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1463 bool const graphicsbg_changed =
1464 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1465 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1467 if (!lcolor.setColor(lyx_name, x11_name)) {
1468 static string const err1 (N_("Set-color \""));
1469 static string const err2 (
1470 N_("\" failed - color is undefined "
1471 "or may not be redefined"));
1472 setErrorMessage(_(err1) + lyx_name + _(err2));
1476 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1478 if (graphicsbg_changed) {
1479 #ifdef WITH_WARNINGS
1480 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1483 grfx::GCache & gc = grfx::GCache::get();
1484 gc.changeDisplay(true);
1488 owner->view()->repaint();
1493 owner->message(argument);
1496 case LFUN_FORKS_SHOW:
1497 owner->getDialogs()->showForks();
1500 case LFUN_FORKS_KILL:
1502 if (!isStrInt(argument))
1505 pid_t const pid = strToInt(argument);
1506 ForkedcallsController & fcc = ForkedcallsController::get();
1511 case LFUN_TOOLTIPS_TOGGLE:
1512 owner->getDialogs()->toggleTooltips();
1516 // Then if it was none of the above
1517 // Trying the BufferView::pimpl dispatch:
1518 if (!owner->view()->dispatch(action, argument))
1519 lyxerr << "A truly unknown func ["
1520 << lyxaction.getActionName(action) << "]!"
1526 string const & msg = getMessage();
1527 sendDispatchMessage(msg, action, argument, verbose);
1531 void LyXFunc::sendDispatchMessage(string const & msg, kb_action action, string const & arg, bool verbose)
1533 owner->updateMenubar();
1534 owner->updateToolbar();
1536 if (action == LFUN_SELFINSERT || !verbose) {
1537 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1539 owner->message(msg);
1543 string dispatch_msg(msg);
1544 if (!dispatch_msg.empty())
1545 dispatch_msg += " ";
1547 string comname = lyxaction.getActionName(action);
1549 int pseudoaction = action;
1550 bool argsadded = false;
1553 // the pseudoaction is useful for the bindings
1554 pseudoaction = lyxaction.searchActionArg(action, arg);
1556 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1557 pseudoaction = action;
1559 comname += " " + arg;
1564 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1566 if (!shortcuts.empty()) {
1567 comname += ": " + shortcuts;
1568 } else if (!argsadded && !arg.empty()) {
1569 comname += " " + arg;
1572 if (!comname.empty()) {
1573 comname = rtrim(comname);
1574 dispatch_msg += "(" + comname + ')';
1577 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1578 if (!dispatch_msg.empty())
1579 owner->message(dispatch_msg);
1583 void LyXFunc::setupLocalKeymap()
1585 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1586 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1590 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1592 string initpath = lyxrc.document_path;
1593 string filename(name);
1595 if (owner->view()->available()) {
1596 string const trypath = owner->buffer()->filePath();
1597 // If directory is writeable, use this as default.
1598 if (IsDirWriteable(trypath))
1602 static int newfile_number;
1604 if (filename.empty()) {
1605 filename = AddName(lyxrc.document_path,
1606 "newfile" + tostr(++newfile_number) + ".lyx");
1607 FileInfo fi(filename);
1608 while (bufferlist.exists(filename) || fi.readable()) {
1610 filename = AddName(lyxrc.document_path,
1611 "newfile" + tostr(newfile_number) +
1613 fi.newFile(filename);
1617 // The template stuff
1620 FileDialog fileDlg(owner, _("Select template file"),
1621 LFUN_SELECT_FILE_SYNC,
1622 make_pair(string(_("Documents|#o#O")),
1623 string(lyxrc.document_path)),
1624 make_pair(string(_("Templates|#T#t")),
1625 string(lyxrc.template_path)));
1627 FileDialog::Result result =
1628 fileDlg.Select(lyxrc.template_path,
1629 _("*.lyx|LyX Documents (*.lyx)"));
1631 if (result.first == FileDialog::Later)
1634 string const fname = result.second;
1641 owner->view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1645 void LyXFunc::open(string const & fname)
1647 string initpath = lyxrc.document_path;
1649 if (owner->view()->available()) {
1650 string const trypath = owner->buffer()->filePath();
1651 // If directory is writeable, use this as default.
1652 if (IsDirWriteable(trypath))
1658 if (fname.empty()) {
1659 FileDialog fileDlg(owner, _("Select document to open"),
1661 make_pair(string(_("Documents|#o#O")),
1662 string(lyxrc.document_path)),
1663 make_pair(string(_("Examples|#E#e")),
1664 string(AddPath(system_lyxdir, "examples"))));
1666 FileDialog::Result result =
1667 fileDlg.Select(initpath,
1668 "*.lyx|LyX Documents (*.lyx)");
1670 if (result.first == FileDialog::Later)
1673 filename = result.second;
1675 // check selected filename
1676 if (filename.empty()) {
1677 owner->message(_("Canceled."));
1683 // get absolute path of file and add ".lyx" to the filename if
1685 string const fullpath = FileSearch(string(), filename, "lyx");
1686 if (fullpath.empty()) {
1687 Alert::alert(_("Error"), _("Could not find file"), filename);
1691 filename = fullpath;
1694 string const disp_fn(MakeDisplayPath(filename));
1697 str << _("Opening document") << ' ' << disp_fn << "...";
1699 owner->message(str.str().c_str());
1701 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1703 owner->view()->buffer(openbuf);
1705 str << _("Document") << ' ' << disp_fn << ' ' << _("opened.");
1706 owner->message(str.str().c_str());
1709 str << _("Could not open document") << ' ' << disp_fn;
1710 owner->message(str.str().c_str());
1715 void LyXFunc::doImport(string const & argument)
1718 string filename = split(argument, format, ' ');
1720 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1721 << " file: " << filename << endl;
1723 // need user interaction
1724 if (filename.empty()) {
1725 string initpath = lyxrc.document_path;
1727 if (owner->view()->available()) {
1728 string const trypath = owner->buffer()->filePath();
1729 // If directory is writeable, use this as default.
1730 if (IsDirWriteable(trypath))
1734 string const text = _("Select ") + formats.prettyName(format)
1735 + _(" file to import");
1737 FileDialog fileDlg(owner, text,
1739 make_pair(string(_("Documents|#o#O")),
1740 string(lyxrc.document_path)),
1741 make_pair(string(_("Examples|#E#e")),
1742 string(AddPath(system_lyxdir, "examples"))));
1744 string const extension = "*." + formats.extension(format)
1745 + "| " + formats.prettyName(format)
1746 + " (*." + formats.extension(format) + ")";
1748 FileDialog::Result result = fileDlg.Select(initpath,
1751 if (result.first == FileDialog::Later)
1754 filename = result.second;
1756 // check selected filename
1757 if (filename.empty())
1758 owner->message(_("Canceled."));
1761 if (filename.empty())
1764 // get absolute path of file
1765 filename = MakeAbsPath(filename);
1767 string const lyxfile = ChangeExtension(filename, ".lyx");
1769 // Check if the document already is open
1770 if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1771 switch (Alert::askConfirmation(_("Document is already open:"),
1772 MakeDisplayPath(lyxfile, 50),
1773 _("Do you want to close that document now?\n"
1774 "('No' will just switch to the open version)")))
1777 // If close is canceled, we cancel here too.
1778 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1782 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1785 owner->message(_("Canceled."));
1790 // if the file exists already, and we didn't do
1791 // -i lyx thefile.lyx, warn
1792 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1793 if (!Alert::askQuestion(_("A document by the name"),
1794 MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1795 owner->message(_("Canceled"));
1800 Importer::Import(owner, filename, format);
1804 void LyXFunc::reloadBuffer()
1806 string const fn = owner->buffer()->fileName();
1807 if (bufferlist.close(owner->buffer()))
1808 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1812 void LyXFunc::closeBuffer()
1814 if (bufferlist.close(owner->buffer()) && !quitting) {
1815 if (bufferlist.empty()) {
1816 // need this otherwise SEGV may occur while trying to
1817 // set variables that don't exist
1818 // since there's no current buffer
1819 owner->getDialogs()->hideBufferDependent();
1821 owner->view()->buffer(bufferlist.first());
1827 // Each "owner" should have it's own message method. lyxview and
1828 // the minibuffer would use the minibuffer, but lyxserver would
1829 // send an ERROR signal to its client. Alejandro 970603
1830 // This func is bit problematic when it comes to NLS, to make the
1831 // lyx servers client be language indepenent we must not translate
1832 // strings sent to this func.
1833 void LyXFunc::setErrorMessage(string const & m) const
1835 dispatch_buffer = m;
1840 void LyXFunc::setMessage(string const & m) const
1842 dispatch_buffer = m;
1846 void LyXFunc::setStatusMessage(string const & m) const
1852 string const LyXFunc::view_status_message()
1854 // When meta-fake key is pressed, show the key sequence so far + "M-".
1856 return keyseq.print() + "M-";
1859 // Else, when a non-complete key sequence is pressed,
1860 // show the available options.
1861 if (keyseq.length() > 0 && !keyseq.deleted()) {
1862 return keyseq.printOptions();
1865 if (!owner->view()->available())
1866 return _("Welcome to LyX!");
1868 return currentState(owner->view());