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));
422 case LFUN_INSET_TOGGLE: {
423 LyXText * lt = owner->view()->getLyXText();
424 disable = !(isEditableInset(lt->getInset())
426 && lt->inset_owner->owner()
427 && lt->inset_owner->owner()->isOpen()));
430 case LFUN_MATH_VALIGN:
431 if (mathcursor && mathcursor->formula()->hullType() != "simple") {
432 char align = mathcursor->valign();
437 if (argument.empty()) {
441 if (!contains("tcb", argument[0])) {
445 flag.setOnOff(argument[0] == align);
450 case LFUN_MATH_HALIGN:
451 if (mathcursor && mathcursor->formula()->hullType() != "simple") {
452 char align = mathcursor->halign();
457 if (argument.empty()) {
461 if (!contains("lcr", argument[0])) {
465 flag.setOnOff(argument[0] == align);
470 case LFUN_MATH_MUTATE:
471 if (tli && (tli->lyxCode() == Inset::MATH_CODE))
472 flag.setOnOff(mathcursor->formula()->hullType() == argument);
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 disable = !mathcursor;
486 // we need to be math mode and a math array for that
487 // Hack: halign produces non-zero result iff we are in a math array
488 case LFUN_MATH_ROW_INSERT:
489 case LFUN_MATH_ROW_DELETE:
490 case LFUN_MATH_COLUMN_INSERT:
491 case LFUN_MATH_COLUMN_DELETE:
492 disable = !mathcursor || !mathcursor->halign() ||
493 mathcursor->formula()->hullType() == "simple";
500 // the functions which insert insets
501 Inset::Code code = Inset::NO_CODE;
504 code = Inset::ERT_CODE;
506 case LFUN_INSET_GRAPHICS:
507 code = Inset::GRAPHICS_CODE;
509 case LFUN_INSET_FOOTNOTE:
510 code = Inset::FOOT_CODE;
512 case LFUN_DIALOG_TABULAR_INSERT:
513 case LFUN_INSET_TABULAR:
514 code = Inset::TABULAR_CODE;
516 case LFUN_INSET_EXTERNAL:
517 code = Inset::EXTERNAL_CODE;
519 case LFUN_INSET_MARGINAL:
520 code = Inset::MARGIN_CODE;
522 case LFUN_INSET_MINIPAGE:
523 code = Inset::MINIPAGE_CODE;
525 case LFUN_INSET_FLOAT:
526 case LFUN_INSET_WIDE_FLOAT:
527 code = Inset::FLOAT_CODE;
529 case LFUN_FLOAT_LIST:
530 code = Inset::FLOAT_LIST_CODE;
533 case LFUN_INSET_LIST:
534 code = Inset::LIST_CODE;
536 case LFUN_INSET_THEOREM:
537 code = Inset::THEOREM_CODE;
540 case LFUN_INSET_CAPTION:
541 code = Inset::CAPTION_CODE;
543 case LFUN_INSERT_NOTE:
544 code = Inset::IGNORE_CODE;
546 case LFUN_INSERT_LABEL:
547 code = Inset::LABEL_CODE;
549 case LFUN_REF_INSERT:
550 code = Inset::REF_CODE;
552 case LFUN_CITATION_CREATE:
553 case LFUN_CITATION_INSERT:
554 code = Inset::CITE_CODE;
556 case LFUN_INSERT_BIBTEX:
557 code = Inset::BIBTEX_CODE;
559 case LFUN_INDEX_INSERT:
560 code = Inset::INDEX_CODE;
562 case LFUN_INDEX_PRINT:
563 code = Inset::INDEX_PRINT_CODE;
565 case LFUN_CHILD_INSERT:
566 code = Inset::INCLUDE_CODE;
568 case LFUN_TOC_INSERT:
569 code = Inset::TOC_CODE;
571 case LFUN_PARENTINSERT:
572 code = Inset::PARENT_CODE;
576 case LFUN_INSERT_URL:
577 code = Inset::URL_CODE;
580 // always allow this, since we will inset a raw quote
581 // if an inset is not allowed.
583 case LFUN_HYPHENATION:
584 case LFUN_LIGATURE_BREAK:
586 case LFUN_MENU_SEPARATOR:
588 case LFUN_END_OF_SENTENCE:
589 code = Inset::SPECIALCHAR_CODE;
591 case LFUN_PROTECTEDSPACE:
592 // slight hack: we know this is allowed in math mode
594 code = Inset::SPECIALCHAR_CODE;
599 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code)) {
606 // A few general toggles
608 case LFUN_TOOLTIPS_TOGGLE:
609 flag.setOnOff(owner->getDialogs()->tooltipsEnabled());
612 case LFUN_READ_ONLY_TOGGLE:
613 flag.setOnOff(buf->isReadonly());
616 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
618 case LFUN_SWITCHBUFFER:
619 // toggle on the current buffer, but do not toggle off
620 // the other ones (is that a good idea?)
621 if (argument == buf->fileName())
628 // the font related toggles
630 LyXFont const & font = TEXT(false)->real_current_font;
633 flag.setOnOff(font.emph() == LyXFont::ON);
636 flag.setOnOff(font.noun() == LyXFont::ON);
639 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
642 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
645 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
648 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
654 string tc = mathcursor->getLastCode();
657 flag.setOnOff(tc == "mathbf");
660 flag.setOnOff(tc == "mathsf");
663 flag.setOnOff(tc == "mathcal");
666 flag.setOnOff(tc == "mathrm");
669 flag.setOnOff(tc == "mathtt");
672 flag.setOnOff(tc == "mathbb");
675 flag.setOnOff(tc == "mathnormal");
686 void LyXFunc::dispatch(string const & s, bool verbose)
688 int const action = lyxaction.LookupFunc(frontStrip(strip(s)));
690 if (action == LFUN_UNKNOWN_ACTION) {
691 string const msg = string(_("Unknown function ("))
697 dispatch(action, verbose);
701 void LyXFunc::dispatch(int ac, bool verbose)
704 kb_action const action = lyxaction.retrieveActionArg(ac, argument);
705 dispatch(action, argument, verbose);
710 void LyXFunc::dispatch(kb_action action, string argument, bool verbose)
712 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << action
713 <<"] arg[" << argument << "]" << endl;
715 // we have not done anything wrong yet.
717 dispatch_buffer.erase();
719 #ifdef NEW_DISPATCHER
720 // We try do call the most specific dispatcher first:
721 // 1. the lockinginset's dispatch
722 // 2. the bufferview's dispatch
723 // 3. the lyxview's dispatch
726 selection_possible = false;
728 if (owner->view()->available())
729 owner->view()->hideCursor();
731 // We cannot use this function here
732 if (getStatus(action, argument).disabled()) {
733 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
734 << lyxaction.getActionName(action)
735 << " [" << action << "] is disabled at this location"
737 setErrorMessage(getStatusMessage());
738 goto exit_with_message;
741 if (owner->view()->available() && owner->view()->theLockingInset()) {
742 UpdatableInset::RESULT result;
743 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
744 (!keyseq.deleted())))
746 UpdatableInset * inset = owner->view()->theLockingInset();
750 inset->getCursorPos(owner->view(), inset_x, dummy_y);
752 if ((action == LFUN_UNKNOWN_ACTION)
753 && argument.empty()) {
754 argument = keyseq.getLastKeyEncoded();
756 // Undo/Redo is a bit tricky for insets.
757 if (action == LFUN_UNDO) {
758 owner->view()->menuUndo();
759 goto exit_with_message;
760 } else if (action == LFUN_REDO) {
761 owner->view()->menuRedo();
762 goto exit_with_message;
763 } else if (((result=inset->
764 // Hand-over to inset's own dispatch:
765 localDispatch(owner->view(), action, argument)) ==
766 UpdatableInset::DISPATCHED) ||
767 (result == UpdatableInset::DISPATCHED_NOUPDATE))
768 goto exit_with_message;
769 // If UNDISPATCHED, just soldier on
770 else if (result == UpdatableInset::FINISHED) {
771 goto exit_with_message;
772 // We do not need special RTL handling here:
773 // FINISHED means that the cursor should be
774 // one position after the inset.
775 } else if (result == UpdatableInset::FINISHED_RIGHT) {
776 TEXT()->cursorRight(owner->view());
777 moveCursorUpdate(true, false);
778 owner->view_state_changed();
779 goto exit_with_message;
780 } else if (result == UpdatableInset::FINISHED_UP) {
781 if (TEXT()->cursor.irow()->previous()) {
783 TEXT()->setCursorFromCoordinates(
784 owner->view(), TEXT()->cursor.ix() + inset_x,
785 TEXT()->cursor.iy() -
786 TEXT()->cursor.irow()->baseline() - 1);
787 TEXT()->cursor.x_fix(TEXT()->cursor.x());
789 TEXT()->cursorUp(owner->view());
791 moveCursorUpdate(true, false);
792 owner->view_state_changed();
794 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
796 goto exit_with_message;
797 } else if (result == UpdatableInset::FINISHED_DOWN) {
798 if (TEXT()->cursor.irow()->next()) {
800 TEXT()->setCursorFromCoordinates(
801 owner->view(), TEXT()->cursor.ix() + inset_x,
802 TEXT()->cursor.iy() -
803 TEXT()->cursor.irow()->baseline() +
804 TEXT()->cursor.irow()->height() + 1);
805 TEXT()->cursor.x_fix(TEXT()->cursor.x());
807 TEXT()->cursorDown(owner->view());
810 TEXT()->cursorRight(owner->view());
812 moveCursorUpdate(true, false);
813 owner->view_state_changed();
814 goto exit_with_message;
816 #warning I am not sure this is still right, please have a look! (Jug 20020417)
817 else { // result == UNDISPATCHED
818 //setMessage(N_("Text mode"));
820 case LFUN_UNKNOWN_ACTION:
821 case LFUN_BREAKPARAGRAPH:
823 TEXT()->cursorRight(owner->view());
824 owner->view()->switchKeyMap();
825 owner->view_state_changed();
828 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
829 TEXT()->cursorRight(owner->view());
830 moveCursorUpdate(true, false);
831 owner->view_state_changed();
833 goto exit_with_message;
835 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
836 TEXT()->cursorRight(owner->view());
837 moveCursorUpdate(true, false);
838 owner->view_state_changed();
840 goto exit_with_message;
842 if (TEXT()->cursor.row()->next())
843 TEXT()->cursorDown(owner->view());
845 TEXT()->cursorRight(owner->view());
846 moveCursorUpdate(true, false);
847 owner->view_state_changed();
848 goto exit_with_message;
860 if (!owner->view()->available()) break;
861 // this function should be used always [asierra060396]
862 UpdatableInset * tli =
863 owner->view()->theLockingInset();
865 UpdatableInset * lock = tli->getLockingInset();
868 owner->view()->unlockInset(tli);
869 TEXT()->cursorRight(owner->view());
870 moveCursorUpdate(true, false);
871 owner->view_state_changed();
873 tli->unlockInsetInInset(owner->view(),
878 // Tell the paragraph dialog that we changed paragraph
879 owner->getDialogs()->updateParagraph();
884 // --- Misc -------------------------------------------
885 case LFUN_WORDFINDFORWARD :
886 case LFUN_WORDFINDBACKWARD : {
887 static string last_search;
888 string searched_string;
890 if (!argument.empty()) {
891 last_search = argument;
892 searched_string = argument;
894 searched_string = last_search;
896 bool fw = (action == LFUN_WORDFINDBACKWARD);
897 if (!searched_string.empty()) {
898 lyxfind::LyXFind(owner->view(), searched_string, fw);
900 // owner->view()->showCursor();
906 if (owner->view()->available() && !owner->view()->theLockingInset()) {
907 owner->view()->update(TEXT(),
908 BufferView::SELECT|BufferView::FITCUR);
910 owner->message(keyseq.printOptions());
914 // --- Misc -------------------------------------------
915 case LFUN_EXEC_COMMAND:
916 owner->focus_command_buffer();
919 case LFUN_CANCEL: // RVDK_PATCH_5
921 meta_fake_bit = key_modifier::none;
922 if (owner->view()->available())
923 // cancel any selection
924 dispatch(LFUN_MARK_OFF);
925 setMessage(N_("Cancel"));
928 case LFUN_META_FAKE: // RVDK_PATCH_5
930 meta_fake_bit = key_modifier::alt;
931 setMessage(keyseq.print());
935 case LFUN_READ_ONLY_TOGGLE:
936 if (owner->buffer()->lyxvc.inUse()) {
937 owner->buffer()->lyxvc.toggleReadOnly();
939 owner->buffer()->setReadonly(
940 !owner->buffer()->isReadonly());
944 case LFUN_CENTER: // this is center and redraw.
945 owner->view()->center();
948 // --- Menus -----------------------------------------------
950 menuNew(argument, false);
953 case LFUN_MENUNEWTMPLT:
954 menuNew(argument, true);
957 case LFUN_CLOSEBUFFER:
962 if (!owner->buffer()->isUnnamed()) {
964 s1 << _("Saving document") << ' '
965 << MakeDisplayPath(owner->buffer()->fileName() + "...");
966 owner->message(s1.str().c_str());
967 MenuWrite(owner->view(), owner->buffer());
969 owner->message(s1.str().c_str());
971 WriteAs(owner->view(), owner->buffer());
975 WriteAs(owner->view(), owner->buffer(), argument);
978 case LFUN_MENURELOAD:
983 Exporter::Export(owner->buffer(), argument, true);
987 Exporter::Preview(owner->buffer(), argument);
991 Exporter::Export(owner->buffer(), "program", true);
995 MenuRunChktex(owner->buffer());
999 owner->getDialogs()->showPrint();
1003 if (argument == "custom")
1004 owner->getDialogs()->showSendto();
1006 Exporter::Export(owner->buffer(), argument, false);
1024 InsetCommandParams p;
1027 if (action == LFUN_TOCVIEW)
1029 p.setCmdName("tableofcontents");
1031 else if (action == LFUN_LOAVIEW)
1032 p.setCmdName("listof{algorithm}{List of Algorithms}");
1033 else if (action == LFUN_LOFVIEW)
1034 p.setCmdName("listoffigures");
1036 p.setCmdName("listoftables");
1038 owner->getDialogs()->createTOC(p.getAsString());
1042 case LFUN_DIALOG_TABULAR_INSERT:
1043 owner->getDialogs()->showTabularCreate();
1047 AutoSave(owner->view());
1051 owner->view()->menuUndo();
1055 owner->view()->menuRedo();
1058 case LFUN_MENUSEARCH:
1059 owner->getDialogs()->showSearch();
1062 case LFUN_REMOVEERRORS:
1063 if (owner->view()->removeAutoInsets()) {
1064 #warning repaint() or update() or nothing ?
1065 owner->view()->repaint();
1066 owner->view()->fitCursor();
1070 case LFUN_DEPTH_MIN:
1071 changeDepth(owner->view(), TEXT(false), -1);
1074 case LFUN_DEPTH_PLUS:
1075 changeDepth(owner->view(), TEXT(false), 1);
1079 owner->getDialogs()->setUserFreeFont();
1082 case LFUN_RECONFIGURE:
1083 Reconfigure(owner->view());
1087 case LFUN_FLOATSOPERATE:
1088 if (argument == "openfoot")
1089 owner->view()->allFloats(1,0);
1090 else if (argument == "closefoot")
1091 owner->view()->allFloats(0,0);
1092 else if (argument == "openfig")
1093 owner->view()->allFloats(1,1);
1094 else if (argument == "closefig")
1095 owner->view()->allFloats(0,1);
1098 #ifdef WITH_WARNINGS
1099 //#warning Find another implementation here (or another lyxfunc)!
1102 case LFUN_HELP_ABOUTLYX:
1103 owner->getDialogs()->showAboutlyx();
1106 case LFUN_HELP_TEXINFO:
1107 owner->getDialogs()->showTexinfo();
1110 case LFUN_HELP_OPEN:
1112 string const arg = argument;
1114 setErrorMessage(N_("Missing argument"));
1117 owner->prohibitInput();
1118 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1119 if (fname.empty()) {
1120 lyxerr << "LyX: unable to find documentation file `"
1121 << arg << "'. Bad installation?" << endl;
1122 owner->allowInput();
1126 str << _("Opening help file") << ' '
1127 << MakeDisplayPath(fname) << "...";
1128 owner->message(str.str().c_str());
1129 owner->view()->buffer(bufferlist.loadLyXFile(fname, false));
1130 owner->allowInput();
1134 // --- version control -------------------------------
1135 case LFUN_VC_REGISTER:
1137 if (!owner->buffer()->lyxvc.inUse())
1138 owner->buffer()->lyxvc.registrer();
1142 case LFUN_VC_CHECKIN:
1144 if (owner->buffer()->lyxvc.inUse()
1145 && !owner->buffer()->isReadonly())
1146 owner->buffer()->lyxvc.checkIn();
1150 case LFUN_VC_CHECKOUT:
1152 if (owner->buffer()->lyxvc.inUse()
1153 && owner->buffer()->isReadonly())
1154 owner->buffer()->lyxvc.checkOut();
1158 case LFUN_VC_REVERT:
1160 owner->buffer()->lyxvc.revert();
1166 owner->buffer()->lyxvc.undoLast();
1170 case LFUN_VC_HISTORY:
1172 owner->getDialogs()->showVCLogFile();
1176 // --- buffers ----------------------------------------
1178 case LFUN_SWITCHBUFFER:
1179 owner->view()->buffer(bufferlist.getBuffer(argument));
1184 // servercmd: argument must be <file>:<template>
1185 Buffer * tmpbuf = NewLyxFile(argument);
1187 owner->view()->buffer(tmpbuf);
1191 case LFUN_FILE_OPEN:
1195 case LFUN_LATEX_LOG:
1196 owner->getDialogs()->showLogFile();
1199 case LFUN_LAYOUT_DOCUMENT:
1200 owner->getDialogs()->showDocument();
1203 case LFUN_LAYOUT_PARAGRAPH:
1204 owner->getDialogs()->showParagraph();
1207 case LFUN_LAYOUT_CHARACTER:
1208 owner->getDialogs()->showCharacter();
1211 case LFUN_LAYOUT_TABULAR:
1212 if (owner->view()->theLockingInset()) {
1213 if (owner->view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1214 InsetTabular * inset = static_cast<InsetTabular *>
1215 (owner->view()->theLockingInset());
1216 inset->openLayoutDialog(owner->view());
1217 } else if (owner->view()->theLockingInset()->
1218 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1219 InsetTabular * inset = static_cast<InsetTabular *>(
1220 owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1221 inset->openLayoutDialog(owner->view());
1226 case LFUN_LAYOUT_PREAMBLE:
1227 owner->getDialogs()->showPreamble();
1230 case LFUN_DROP_LAYOUTS_CHOICE:
1231 owner->getToolbar()->openLayoutList();
1234 case LFUN_MENU_OPEN_BY_NAME:
1235 owner->getMenubar()->openByName(argument);
1236 break; // RVDK_PATCH_5
1238 case LFUN_SPELLCHECK:
1239 if (lyxrc.isp_command != "none")
1240 owner->getDialogs()->showSpellchecker();
1243 // --- lyxserver commands ----------------------------
1247 setMessage(owner->buffer()->fileName());
1248 lyxerr[Debug::INFO] << "FNAME["
1249 << owner->buffer()->fileName()
1255 dispatch_buffer = keyseq.print();
1256 lyxserver->notifyClient(dispatch_buffer);
1260 case LFUN_GOTOFILEROW:
1264 istringstream istr(argument.c_str());
1265 istr >> file_name >> row;
1266 // Must replace extension of the file to be .lyx and get full path
1267 string const s(ChangeExtension(file_name, ".lyx"));
1269 // Either change buffer or load the file
1270 if (bufferlist.exists(s)) {
1271 owner->view()->buffer(bufferlist.getBuffer(s));
1273 owner->view()->buffer(bufferlist.loadLyXFile(s));
1276 owner->view()->setCursorFromRow(row);
1278 owner->view()->center();
1279 // see BufferView_pimpl::center()
1280 owner->view()->updateScrollbar();
1284 case LFUN_GOTO_PARAGRAPH:
1286 istringstream istr(argument.c_str());
1290 Paragraph * par = owner->buffer()->getParFromID(id);
1292 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1293 << id << "]" << endl;
1296 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1297 << " found." << endl;
1300 if (owner->view()->theLockingInset())
1301 owner->view()->unlockInset(owner->view()->theLockingInset());
1302 if (par->inInset()) {
1303 par->inInset()->edit(owner->view());
1306 owner->view()->getLyXText()->setCursor(owner->view(), par, 0);
1307 owner->view()->switchKeyMap();
1308 owner->view_state_changed();
1310 owner->view()->center();
1311 // see BufferView_pimpl::center()
1312 owner->view()->updateScrollbar();
1319 int const qa = lyxaction.LookupFunc(argument);
1320 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1324 // --- toolbar ----------------------------------
1325 case LFUN_PUSH_TOOLBAR:
1327 int nth = strToInt(argument);
1329 setErrorMessage(N_("Push-toolbar needs argument > 0"));
1331 owner->getToolbar()->push(nth);
1336 case LFUN_ADD_TO_TOOLBAR:
1338 if (lyxerr.debugging(Debug::GUI)) {
1339 lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1340 "argument = `" << argument << '\'' << endl;
1342 string tmp(argument);
1343 //lyxerr <<string("Argument: ") + argument);
1344 //lyxerr <<string("Tmp : ") + tmp);
1346 setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1348 owner->getToolbar()->add(argument, false);
1349 owner->getToolbar()->set();
1354 // --- insert characters ----------------------------------------
1356 // --- Mathed stuff. If we are here, there is no locked inset yet.
1357 case LFUN_MATH_EXTERN:
1358 case LFUN_MATH_NUMBER:
1359 case LFUN_MATH_NONUMBER:
1360 case LFUN_MATH_LIMITS:
1362 setErrorMessage(N_("This is only allowed in math mode!"));
1366 // passthrough hat and underscore outside mathed:
1367 case LFUN_SUBSCRIPT:
1368 dispatch(LFUN_SELFINSERT, "_", false);
1370 case LFUN_SUPERSCRIPT:
1371 dispatch(LFUN_SELFINSERT, "^", false);
1374 case LFUN_MATH_PANEL:
1375 owner->getDialogs()->showMathPanel();
1378 case LFUN_CITATION_CREATE:
1380 InsetCommandParams p("cite");
1382 if (!argument.empty()) {
1383 // This should be set at source, ie when typing
1384 // "citation-insert foo" in the minibuffer.
1385 // Question: would pybibliographer also need to be
1386 // changed. Suspect so. Leave as-is therefore.
1387 if (contains(argument, "|")) {
1388 p.setContents(token(argument, '|', 0));
1389 p.setOptions( token(argument, '|', 1));
1391 p.setContents(argument);
1393 dispatch(LFUN_CITATION_INSERT, p.getAsString());
1395 owner->getDialogs()->createCitation(p.getAsString());
1399 case LFUN_CHILDOPEN:
1401 string const filename =
1402 MakeAbsPath(argument,
1403 owner->buffer()->filePath());
1404 setMessage(N_("Opening child document ") +
1405 MakeDisplayPath(filename) + "...");
1406 owner->view()->savePosition(0);
1407 if (bufferlist.exists(filename))
1408 owner->view()->buffer(bufferlist.getBuffer(filename));
1410 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1414 case LFUN_TOGGLECURSORFOLLOW:
1415 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1418 case LFUN_KMAP_OFF: // keymap off
1419 owner->getIntl()->KeyMapOn(false);
1422 case LFUN_KMAP_PRIM: // primary keymap
1423 owner->getIntl()->KeyMapPrim();
1426 case LFUN_KMAP_SEC: // secondary keymap
1427 owner->getIntl()->KeyMapSec();
1430 case LFUN_KMAP_TOGGLE: // toggle keymap
1431 owner->getIntl()->ToggleKeyMap();
1436 // argument contains ';'-terminated commands
1437 while (!argument.empty()) {
1439 argument = split(argument, first, ';');
1445 case LFUN_DIALOG_PREFERENCES:
1446 owner->getDialogs()->showPreferences();
1449 case LFUN_SAVEPREFERENCES:
1451 Path p(user_lyxdir);
1452 lyxrc.write("preferences");
1456 case LFUN_SCREEN_FONT_UPDATE:
1458 // handle the screen font changes.
1460 lyxrc.set_font_norm_type();
1461 lyx_gui::update_fonts();
1462 // Of course we should only do the resize and the textcache.clear
1463 // if values really changed...but not very important right now. (Lgb)
1464 // All visible buffers will need resize
1465 owner->view()->resize();
1466 owner->view()->repaint();
1467 // We also need to empty the textcache so that
1468 // the buffer will be formatted correctly after
1474 case LFUN_SET_COLOR:
1477 string const x11_name = split(argument, lyx_name, ' ');
1478 if (lyx_name.empty() || x11_name.empty()) {
1479 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1484 bool const graphicsbg_changed =
1485 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1486 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1488 if (!lcolor.setColor(lyx_name, x11_name)) {
1489 static string const err1 (N_("Set-color \""));
1490 static string const err2 (
1491 N_("\" failed - color is undefined "
1492 "or may not be redefined"));
1493 setErrorMessage(_(err1) + lyx_name + _(err2));
1497 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1499 if (graphicsbg_changed) {
1500 #ifdef WITH_WARNINGS
1501 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1504 grfx::GCache & gc = grfx::GCache::get();
1505 gc.changeDisplay(true);
1509 owner->view()->repaint();
1514 owner->message(argument);
1517 case LFUN_FORKS_SHOW:
1518 owner->getDialogs()->showForks();
1521 case LFUN_FORKS_KILL:
1523 if (!isStrInt(argument))
1526 pid_t const pid = strToInt(argument);
1527 ForkedcallsController & fcc = ForkedcallsController::get();
1532 case LFUN_TOOLTIPS_TOGGLE:
1533 owner->getDialogs()->toggleTooltips();
1537 // Then if it was none of the above
1538 // Trying the BufferView::pimpl dispatch:
1539 if (!owner->view()->dispatch(action, argument))
1540 lyxerr << "A truly unknown func ["
1541 << lyxaction.getActionName(action) << "]!"
1547 string const & msg = getMessage();
1548 sendDispatchMessage(msg, action, argument, verbose);
1552 void LyXFunc::sendDispatchMessage(string const & msg, kb_action action, string const & arg, bool verbose)
1554 owner->updateMenubar();
1555 owner->updateToolbar();
1557 if (action == LFUN_SELFINSERT || !verbose) {
1558 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1560 owner->message(msg);
1564 string dispatch_msg(msg);
1565 if (!dispatch_msg.empty())
1566 dispatch_msg += " ";
1568 string comname = lyxaction.getActionName(action);
1570 int pseudoaction = action;
1571 bool argsadded = false;
1574 // the pseudoaction is useful for the bindings
1575 pseudoaction = lyxaction.searchActionArg(action, arg);
1577 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1578 pseudoaction = action;
1580 comname += " " + arg;
1585 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1587 if (!shortcuts.empty()) {
1588 comname += ": " + shortcuts;
1589 } else if (!argsadded && !arg.empty()) {
1590 comname += " " + arg;
1593 if (!comname.empty()) {
1594 comname = strip(comname);
1595 dispatch_msg += "(" + comname + ')';
1598 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1599 if (!dispatch_msg.empty())
1600 owner->message(dispatch_msg);
1604 void LyXFunc::setupLocalKeymap()
1606 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1607 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1611 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1613 string initpath = lyxrc.document_path;
1614 string filename(name);
1616 if (owner->view()->available()) {
1617 string const trypath = owner->buffer()->filePath();
1618 // If directory is writeable, use this as default.
1619 if (IsDirWriteable(trypath))
1623 static int newfile_number;
1625 if (filename.empty()) {
1626 filename = AddName(lyxrc.document_path,
1627 "newfile" + tostr(++newfile_number) + ".lyx");
1628 FileInfo fi(filename);
1629 while (bufferlist.exists(filename) || fi.readable()) {
1631 filename = AddName(lyxrc.document_path,
1632 "newfile" + tostr(newfile_number) +
1634 fi.newFile(filename);
1638 // The template stuff
1641 FileDialog fileDlg(owner, _("Select template file"),
1642 LFUN_SELECT_FILE_SYNC,
1643 make_pair(string(_("Documents|#o#O")),
1644 string(lyxrc.document_path)),
1645 make_pair(string(_("Templates|#T#t")),
1646 string(lyxrc.template_path)));
1648 FileDialog::Result result =
1649 fileDlg.Select(lyxrc.template_path,
1650 _("*.lyx|LyX Documents (*.lyx)"));
1652 if (result.first == FileDialog::Later)
1655 string const fname = result.second;
1662 owner->view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1666 void LyXFunc::open(string const & fname)
1668 string initpath = lyxrc.document_path;
1670 if (owner->view()->available()) {
1671 string const trypath = owner->buffer()->filePath();
1672 // If directory is writeable, use this as default.
1673 if (IsDirWriteable(trypath))
1679 if (fname.empty()) {
1680 FileDialog fileDlg(owner, _("Select document to open"),
1682 make_pair(string(_("Documents|#o#O")),
1683 string(lyxrc.document_path)),
1684 make_pair(string(_("Examples|#E#e")),
1685 string(AddPath(system_lyxdir, "examples"))));
1687 FileDialog::Result result =
1688 fileDlg.Select(initpath,
1689 "*.lyx|LyX Documents (*.lyx)");
1691 if (result.first == FileDialog::Later)
1694 filename = result.second;
1696 // check selected filename
1697 if (filename.empty()) {
1698 owner->message(_("Canceled."));
1704 // get absolute path of file and add ".lyx" to the filename if
1706 string const fullpath = FileSearch(string(), filename, "lyx");
1707 if (fullpath.empty()) {
1708 Alert::alert(_("Error"), _("Could not find file"), filename);
1712 filename = fullpath;
1715 string const disp_fn(MakeDisplayPath(filename));
1718 str << _("Opening document") << ' ' << disp_fn << "...";
1720 owner->message(str.str().c_str());
1722 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1724 owner->view()->buffer(openbuf);
1726 str << _("Document") << ' ' << disp_fn << ' ' << _("opened.");
1727 owner->message(str.str().c_str());
1730 str << _("Could not open document") << ' ' << disp_fn;
1731 owner->message(str.str().c_str());
1736 void LyXFunc::doImport(string const & argument)
1739 string filename = split(argument, format, ' ');
1741 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1742 << " file: " << filename << endl;
1744 // need user interaction
1745 if (filename.empty()) {
1746 string initpath = lyxrc.document_path;
1748 if (owner->view()->available()) {
1749 string const trypath = owner->buffer()->filePath();
1750 // If directory is writeable, use this as default.
1751 if (IsDirWriteable(trypath))
1755 string const text = _("Select ") + formats.prettyName(format)
1756 + _(" file to import");
1758 FileDialog fileDlg(owner, text,
1760 make_pair(string(_("Documents|#o#O")),
1761 string(lyxrc.document_path)),
1762 make_pair(string(_("Examples|#E#e")),
1763 string(AddPath(system_lyxdir, "examples"))));
1765 string const extension = "*." + formats.extension(format)
1766 + "| " + formats.prettyName(format)
1767 + " (*." + formats.extension(format) + ")";
1769 FileDialog::Result result = fileDlg.Select(initpath,
1772 if (result.first == FileDialog::Later)
1775 filename = result.second;
1777 // check selected filename
1778 if (filename.empty())
1779 owner->message(_("Canceled."));
1782 if (filename.empty())
1785 // get absolute path of file
1786 filename = MakeAbsPath(filename);
1788 string const lyxfile = ChangeExtension(filename, ".lyx");
1790 // Check if the document already is open
1791 if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1792 switch (Alert::askConfirmation(_("Document is already open:"),
1793 MakeDisplayPath(lyxfile, 50),
1794 _("Do you want to close that document now?\n"
1795 "('No' will just switch to the open version)")))
1798 // If close is canceled, we cancel here too.
1799 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1803 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1806 owner->message(_("Canceled."));
1811 // if the file exists already, and we didn't do
1812 // -i lyx thefile.lyx, warn
1813 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1814 if (!Alert::askQuestion(_("A document by the name"),
1815 MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1816 owner->message(_("Canceled"));
1821 Importer::Import(owner, filename, format);
1825 void LyXFunc::reloadBuffer()
1827 string const fn = owner->buffer()->fileName();
1828 if (bufferlist.close(owner->buffer()))
1829 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1833 void LyXFunc::closeBuffer()
1835 if (bufferlist.close(owner->buffer()) && !quitting) {
1836 if (bufferlist.empty()) {
1837 // need this otherwise SEGV may occur while trying to
1838 // set variables that don't exist
1839 // since there's no current buffer
1840 owner->getDialogs()->hideBufferDependent();
1842 owner->view()->buffer(bufferlist.first());
1848 // Each "owner" should have it's own message method. lyxview and
1849 // the minibuffer would use the minibuffer, but lyxserver would
1850 // send an ERROR signal to its client. Alejandro 970603
1851 // This func is bit problematic when it comes to NLS, to make the
1852 // lyx servers client be language indepenent we must not translate
1853 // strings sent to this func.
1854 void LyXFunc::setErrorMessage(string const & m) const
1856 dispatch_buffer = m;
1861 void LyXFunc::setMessage(string const & m) const
1863 dispatch_buffer = m;
1867 void LyXFunc::setStatusMessage(string const & m) const
1873 string const LyXFunc::view_status_message()
1875 // When meta-fake key is pressed, show the key sequence so far + "M-".
1877 return keyseq.print() + "M-";
1880 // Else, when a non-complete key sequence is pressed,
1881 // show the available options.
1882 if (keyseq.length() > 0 && !keyseq.deleted()) {
1883 return keyseq.printOptions();
1886 if (!owner->view()->available())
1887 return _("Welcome to LyX!");
1889 return currentState(owner->view());