1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
23 #pragma implementation
29 #include "bufferlist.h"
30 #include "ColorHandler.h"
31 #include "lyxserver.h"
36 #include "LyXAction.h"
37 #include "insets/inseturl.h"
38 #include "insets/insetlatexaccent.h"
39 #include "insets/insettoc.h"
40 #include "insets/insetref.h"
41 #include "insets/insetparent.h"
42 #include "insets/insetindex.h"
43 #include "insets/insetinclude.h"
44 #include "insets/insetbib.h"
45 #include "insets/insetcite.h"
46 #include "insets/insettext.h"
47 #include "insets/insetert.h"
48 #include "insets/insetexternal.h"
49 #include "insets/insetgraphics.h"
50 #include "insets/insetfoot.h"
51 #include "insets/insetmarginal.h"
52 #include "insets/insetminipage.h"
53 #include "insets/insetfloat.h"
54 #include "insets/insetlist.h"
55 #include "insets/insettabular.h"
56 #include "insets/insettheorem.h"
57 #include "insets/insetcaption.h"
58 #include "mathed/formulamacro.h"
59 #include "spellchecker.h" // RVDK_PATCH_5
60 #include "minibuffer.h"
64 #include "lyx_gui_misc.h"
65 #include "support/filetools.h"
66 #include "support/FileInfo.h"
67 #include "support/syscall.h"
68 #include "support/lstrings.h"
69 #include "support/path.h"
70 #include "support/lyxfunctional.h"
75 #include "trans_mgr.h"
79 #include "bufferview_funcs.h"
80 #include "frontends/Dialogs.h"
81 #include "frontends/Toolbar.h"
82 #include "frontends/Menubar.h"
83 #include "FloatList.h"
84 #include "converter.h"
87 #include "FontLoader.h"
88 #include "TextCache.h"
94 extern void math_insert_symbol(BufferView *, string const &);
95 extern bool math_insert_greek(BufferView *, char);
96 extern BufferList bufferlist;
97 extern LyXServer * lyxserver;
98 extern int greek_kb_flag;
99 extern bool selection_possible;
100 extern void MenuSendto();
102 extern kb_keymap * toplevel_keymap;
104 extern void show_symbols_form(LyXFunc *);
106 extern LyXAction lyxaction;
108 extern tex_accent_struct get_accent(kb_action action);
110 extern LyXTextClass::size_type current_layout;
112 extern void ShowLatexLog();
114 /* === globals =========================================================== */
116 bool LyXFunc::show_sc = true;
118 LyXFunc::LyXFunc(LyXView * o)
122 lyx_dead_action = LFUN_NOACTION;
123 lyx_calling_dead_action = LFUN_NOACTION;
128 LyXText * LyXFunc::TEXT(bool flag=true) const
131 return owner->view()->text;
132 return owner->view()->getLyXText();
135 // I changed this func slightly. I commented out the ...FinishUndo(),
136 // this means that all places that used to have a moveCursorUpdate, now
137 // have a ...FinishUndo() as the preceeding statement. I have also added
138 // a moveCursorUpdate to some of the functions that updated the cursor, but
139 // that did not show its new position.
141 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
143 if (selecting || TEXT(flag)->mark_set) {
144 TEXT(flag)->SetSelection(owner->view());
145 if (TEXT(flag)->bv_owner)
146 owner->view()->toggleToggle();
148 owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
149 owner->view()->showCursor();
151 /* ---> Everytime the cursor is moved, show the current font state. */
152 // should this too me moved out of this func?
153 //owner->showState();
154 owner->view()->setState();
158 void LyXFunc::handleKeyFunc(kb_action action)
162 if (keyseq.length == -1 && keyseq.getiso() != 0)
164 owner->getIntl()->getTrans()
165 .deadkey(c, get_accent(action).accent, TEXT(false));
166 // Need to reset, in case the minibuffer calls these
170 // copied verbatim from do_accent_char
171 owner->view()->update(TEXT(false),
172 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
173 TEXT(false)->sel_cursor = TEXT(false)->cursor;
177 int LyXFunc::processKeySym(KeySym keysym, unsigned int state)
181 if (lyxerr.debugging(Debug::KEY)) {
182 char * tmp = XKeysymToString(keysym);
183 string const stm = (tmp ? tmp : "");
184 lyxerr << "KeySym is "
187 << keysym << "] State is ["
191 // Do nothing if we have nothing (JMarc)
192 if (keysym == NoSymbol) {
193 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
199 if (owner->view()->available()) {
200 // this function should be used always [asierra060396]
201 UpdatableInset * tli = owner->view()->theLockingInset();
202 if (tli && (keysym == XK_Escape)) {
203 if (tli == tli->GetLockingInset()) {
204 owner->view()->unlockInset(tli);
205 TEXT()->CursorRight(owner->view());
206 moveCursorUpdate(true, false);
209 tli->UnlockInsetInInset(owner->view(),
210 tli->GetLockingInset(),true);
217 // Can we be sure that this will work for all X-Windows
218 // implementations? (Lgb)
219 // This code snippet makes lyx ignore some keys. Perhaps
220 // all of them should be explictly mentioned?
221 if ((keysym >= XK_Shift_L && keysym <= XK_Hyper_R)
222 || keysym == XK_Mode_switch || keysym == 0x0)
225 // Do a one-deep top-level lookup for
226 // cancel and meta-fake keys. RVDK_PATCH_5
227 cancel_meta_seq.reset();
229 int action = cancel_meta_seq.addkey(keysym, state
230 &(ShiftMask|ControlMask
232 if (lyxerr.debugging(Debug::KEY)) {
233 lyxerr << "action first set to [" << action << "]" << endl;
236 // When not cancel or meta-fake, do the normal lookup.
237 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
238 // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
239 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
240 if (lyxerr.debugging(Debug::KEY)) {
241 lyxerr << "meta_fake_bit is [" << meta_fake_bit << "]" << endl;
243 // remove Caps Lock and Mod2 as a modifiers
244 action = keyseq.addkey(keysym,
245 (state | meta_fake_bit)
246 &(ShiftMask|ControlMask
248 if (lyxerr.debugging(Debug::KEY)) {
249 lyxerr << "action now set to [" << action << "]" << endl;
252 // Dont remove this unless you know what you are doing.
255 if (action == 0) action = LFUN_PREFIX;
257 if (lyxerr.debugging(Debug::KEY)) {
266 // already here we know if it any point in going further
267 // why not return already here if action == -1 and
268 // num_bytes == 0? (Lgb)
270 if (keyseq.length > 1 || keyseq.length < -1) {
273 owner->getMiniBuffer()->Set(buf);
277 if (keyseq.length < -1) { // unknown key sequence...
280 owner->getMiniBuffer()->Set(_("Unknown sequence:"), buf);
284 char const isochar = keyseq.getiso();
285 if (!(state & ControlMask) &&
286 !(state & Mod1Mask) &&
287 (isochar && keysym < 0xF000)) {
290 if (argument.empty()) {
291 lyxerr.debug() << "Empty argument!" << endl;
292 // This can`t possibly be of any use
293 // so we`ll skip the dispatch.
296 } else if (action == LFUN_SELFINSERT) {
297 // We must set the argument to the char looked up by
302 xke.send_event = False;
303 xke.display = fl_get_display();
313 xke.keycode = XKeysymToKeycode(fl_get_display(), keysym);
314 xke.same_screen = True;
317 int res = XLookupString(&xke, ret, 10, &tmpkeysym, 0);
318 //Assert(keysym == tmpkeysym);
319 lyxerr[Debug::KEY] << "TmpKeysym ["
320 << tmpkeysym << "]" << endl;
323 argument = string(ret, res);
324 lyxerr[Debug::KEY] << "SelfInsert arg["
325 << argument << "]" << endl;
329 bool tmp_sc = show_sc;
331 Dispatch(action, argument);
337 LyXFunc::func_status LyXFunc::getStatus(int ac) const
339 return getStatus(ac, string());
342 LyXFunc::func_status LyXFunc::getStatus(int ac, string const & not_to_use_arg) const
345 func_status flag = LyXFunc::OK;
347 Buffer * buf = owner->buffer();
349 if (lyxaction.isPseudoAction(ac))
350 action = lyxaction.retrieveActionArg(ac, argument);
352 action = static_cast<kb_action>(ac);
353 if (!not_to_use_arg.empty())
354 argument = not_to_use_arg; // exept here
357 if (action == LFUN_UNKNOWN_ACTION) {
358 setErrorMessage(N_("Unknown action"));
359 return LyXFunc::Unknown;
362 // Check whether we need a buffer
363 if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
364 // Yes we need a buffer, do we have one?
367 // Can we use a readonly buffer?
368 if (buf->isReadonly() &&
369 !lyxaction.funcHasFlag(action,
370 LyXAction::ReadOnly)) {
372 setErrorMessage(N_("Document is read-only"));
373 flag |= LyXFunc::Disabled;
377 setErrorMessage(N_("Command not allowed with"
378 "out any document open"));
379 flag |= LyXFunc::Disabled;
384 // I would really like to avoid having this switch and rather try to
385 // encode this in the function itself.
386 bool disable = false;
389 disable = !Exporter::IsExportable(buf, "dvi")
390 || lyxrc.print_command == "none";
393 disable = (argument != "custom") ||
394 (argument == "fax" &&
395 !Exporter::IsExportable(buf, argument));
398 disable = buf->undostack.empty();
401 disable = buf->redostack.empty();
403 case LFUN_SPELLCHECK:
404 disable = lyxrc.isp_command == "none";
407 disable = lyxrc.chktex_command == "none";
410 disable = !Exporter::IsExportable(buf, "program");
413 case LFUN_INSERTFOOTNOTE:
415 case LFUN_MARGINMELT:
416 // Disable insertion of floats in a tabular.
418 if (owner->view()->theLockingInset()) {
419 disable = (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) ||
420 owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
424 case LFUN_LAYOUT_TABULAR:
426 if (owner->view()->theLockingInset()) {
427 disable = (owner->view()->theLockingInset()->LyxCode() != Inset::TABULAR_CODE) &&
428 !owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
432 case LFUN_TABULAR_FEATURE:
434 if (owner->view()->theLockingInset()) {
435 func_status ret = LyXFunc::Disabled;
436 if (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) {
437 ret = static_cast<InsetTabular *>
438 (owner->view()->theLockingInset())->
440 } else if (owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
441 ret = static_cast<InsetTabular *>
442 (owner->view()->theLockingInset()->
443 GetFirstLockingInsetOfType(Inset::TABULAR_CODE))->
449 static InsetTabular inset(*owner->buffer(), 1, 1);
453 ret = inset.getStatus(argument);
454 if ((ret & LyXFunc::ToggleOn) ||
455 (ret & LyXFunc::ToggleOff))
456 flag |= LyXFunc::ToggleOff;
460 case LFUN_VC_REGISTER:
461 disable = buf->lyxvc.inUse();
463 case LFUN_VC_CHECKIN:
464 disable = !buf->lyxvc.inUse() || buf->isReadonly();
466 case LFUN_VC_CHECKOUT:
467 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
471 case LFUN_VC_HISTORY:
472 disable = !buf->lyxvc.inUse();
474 case LFUN_BOOKMARK_GOTO:
475 disable = !owner->view()->
476 isSavedPosition(strToUnsignedInt(argument));
481 flag |= LyXFunc::Disabled;
484 func_status box = LyXFunc::ToggleOff;
485 LyXFont const & font =
486 TEXT(false)->real_current_font;
489 if (font.emph() == LyXFont::ON)
490 box = LyXFunc::ToggleOn;
493 if (font.noun() == LyXFont::ON)
494 box = LyXFunc::ToggleOn;
497 if (font.series() == LyXFont::BOLD_SERIES)
498 box = LyXFunc::ToggleOn;
501 if (font.latex() == LyXFont::ON)
502 box = LyXFunc::ToggleOn;
515 string const LyXFunc::Dispatch(string const & s)
517 // Split command string into command and argument
519 string line = frontStrip(s);
520 string arg = strip(frontStrip(split(line, cmd, ' ')));
522 return Dispatch(lyxaction.LookupFunc(cmd), arg);
526 string const LyXFunc::Dispatch(int ac,
527 string const & do_not_use_this_arg)
529 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << ac
530 <<"] arg[" << do_not_use_this_arg << "]" << endl;
535 // we have not done anything wrong yet.
537 dispatch_buffer.erase();
539 // if action is a pseudo-action, we need the real action
540 if (lyxaction.isPseudoAction(ac)) {
542 action = static_cast<kb_action>
543 (lyxaction.retrieveActionArg(ac, tmparg));
547 action = static_cast<kb_action>(ac);
548 if (!do_not_use_this_arg.empty())
549 argument = do_not_use_this_arg; // except here
552 selection_possible = false;
554 if (owner->view()->available())
555 owner->view()->hideCursor();
557 // We cannot use this function here
558 if (getStatus(ac, do_not_use_this_arg) & Disabled)
559 goto exit_with_message;
561 commandshortcut.erase();
563 if (lyxrc.display_shortcuts && show_sc) {
564 if (action != LFUN_SELFINSERT) {
565 // Put name of command and list of shortcuts
566 // for it in minibuffer
567 string comname = lyxaction.getActionName(action);
569 int pseudoaction = action;
570 bool argsadded = false;
572 if (!argument.empty()) {
573 // If we have the command with argument,
576 lyxaction.searchActionArg(action,
579 if (pseudoaction == -1) {
580 pseudoaction = action;
582 comname += " " + argument;
587 string const shortcuts =
588 toplevel_keymap->findbinding(pseudoaction);
590 if (!shortcuts.empty()) {
591 comname += ": " + shortcuts;
592 } else if (!argsadded) {
593 comname += " " + argument;
596 if (!comname.empty()) {
597 comname = strip(comname);
598 commandshortcut = "(" + comname + ')';
599 owner->getMiniBuffer()->Set(commandshortcut);
600 // Here we could even add a small pause,
601 // to annoy the user and make him learn
603 // No! That will just annoy, not teach
604 // anything. The user will read the messages
605 // if they are interested. (Asger)
610 if (owner->view()->available() && owner->view()->theLockingInset()) {
611 UpdatableInset::RESULT result;
612 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
613 (keyseq.length >= -1)))
615 if ((action==LFUN_UNKNOWN_ACTION) && argument.empty()){
616 argument = keyseq.getiso();
618 // Undo/Redo pre 0.13 is a bit tricky for insets.
619 if (action == LFUN_UNDO) {
622 UpdatableInset * inset =
623 owner->view()->theLockingInset();
624 inset->GetCursorPos(owner->view(), slx, sly);
625 owner->view()->unlockInset(inset);
626 owner->view()->menuUndo();
627 if (TEXT()->cursor.par()->
628 IsInset(TEXT()->cursor.pos())) {
629 inset = static_cast<UpdatableInset*>(
630 TEXT()->cursor.par()->
637 inset->Edit(owner->view(),slx,sly,0);
639 } else if (action == LFUN_REDO) {
642 UpdatableInset * inset = owner->view()->
644 inset->GetCursorPos(owner->view(), slx, sly);
645 owner->view()->unlockInset(inset);
646 owner->view()->menuRedo();
647 inset = static_cast<UpdatableInset*>(
648 TEXT()->cursor.par()->
652 inset->Edit(owner->view(),slx,sly,0);
654 } else if (((result=owner->view()->theLockingInset()->
655 LocalDispatch(owner->view(), action,
657 UpdatableInset::DISPATCHED) ||
658 (result == UpdatableInset::DISPATCHED_NOUPDATE))
661 //setMessage(N_("Text mode"));
663 case LFUN_UNKNOWN_ACTION:
664 case LFUN_BREAKPARAGRAPH:
666 TEXT()->CursorRight(owner->view());
667 owner->view()->setState();
671 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
672 TEXT()->CursorRight(owner->view());
673 moveCursorUpdate(true, false);
678 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
679 TEXT()->CursorRight(owner->view());
680 moveCursorUpdate(true, false);
685 TEXT()->CursorDown(owner->view());
686 moveCursorUpdate(true, false);
697 // --- Misc -------------------------------------------
698 case LFUN_WORDFINDFORWARD :
699 case LFUN_WORDFINDBACKWARD : {
700 static string last_search;
701 string searched_string;
703 if (!argument.empty()) {
704 last_search = argument;
705 searched_string = argument;
707 searched_string = last_search;
710 if (!searched_string.empty() &&
711 ((action == LFUN_WORDFINDBACKWARD) ?
712 TEXT()->SearchBackward(owner->view(), searched_string) :
713 TEXT()->SearchForward(owner->view(), searched_string))) {
715 // ??? What is that ???
716 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
719 // clear the selection (if there is any)
720 owner->view()->toggleSelection();
721 TEXT()->ClearSelection(owner->view());
723 // Move cursor so that successive C-s 's will not stand in place.
724 if (action == LFUN_WORDFINDFORWARD )
725 TEXT()->CursorRightOneWord(owner->view());
726 TEXT()->FinishUndo();
727 moveCursorUpdate(true, false);
730 // set the new selection
731 // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
732 owner->view()->toggleSelection(false);
735 // REMOVED : if (owner->view()->getWorkArea()->focus)
736 owner->view()->showCursor();
742 if (owner->view()->available()) {
743 owner->view()->update(TEXT(),
744 BufferView::SELECT|BufferView::FITCUR);
747 keyseq.print(buf, true);
748 owner->getMiniBuffer()->Set(buf, string(), string(), 1);
752 // --- Misc -------------------------------------------
753 case LFUN_EXEC_COMMAND:
754 owner->getMiniBuffer()->PrepareForCommand();
757 case LFUN_CANCEL: // RVDK_PATCH_5
760 if (owner->view()->available())
761 // cancel any selection
762 Dispatch(LFUN_MARK_OFF);
763 setMessage(N_("Cancel"));
766 case LFUN_META_FAKE: // RVDK_PATCH_5
768 meta_fake_bit = Mod1Mask;
770 keyseq.print(buf, true);
771 setMessage(buf); // RVDK_PATCH_5
775 case LFUN_READ_ONLY_TOGGLE:
776 if (owner->buffer()->lyxvc.inUse()) {
777 owner->buffer()->lyxvc.toggleReadOnly();
779 owner->buffer()->setReadonly(
780 !owner->buffer()->isReadonly());
784 case LFUN_CENTER: // this is center and redraw.
785 owner->view()->center();
788 // --- Menus -----------------------------------------------
793 case LFUN_MENUNEWTMPLT:
801 case LFUN_CLOSEBUFFER:
806 if (!owner->buffer()->isUnnamed()) {
807 owner->getMiniBuffer()->Set(_("Saving document"),
808 MakeDisplayPath(owner->buffer()->fileName()),
810 MenuWrite(owner->view(), owner->buffer());
811 //owner->getMiniBuffer()-> {
812 // Set(_("Document saved as"),
813 // MakeDisplayPath(owner->buffer()->fileName()));
815 //owner->getMiniBuffer()->Set(_("Save failed!"));
818 MenuWriteAs(owner->view(), owner->buffer());
822 case LFUN_MENUWRITEAS:
823 MenuWriteAs(owner->view(), owner->buffer());
826 case LFUN_MENURELOAD:
831 Exporter::Export(owner->buffer(), argument, true);
835 Exporter::Preview(owner->buffer(), argument);
839 Exporter::Export(owner->buffer(), "program", true);
843 MenuRunChktex(owner->buffer());
847 owner->getDialogs()->showPrint();
851 if (argument == "custom")
854 Exporter::Export(owner->buffer(), argument, false);
870 InsetCommandParams p;
872 if (action == LFUN_TOCVIEW )
873 p.setCmdName( "tableofcontents" );
874 else if (action == LFUN_LOAVIEW )
875 p.setCmdName( "listofalgorithms" );
876 else if (action == LFUN_LOFVIEW )
877 p.setCmdName( "listoffigures" );
879 p.setCmdName( "listoftables" );
881 owner->getDialogs()->createTOC( p.getAsString() );
885 case LFUN_DIALOG_TABULAR_INSERT:
886 owner->getDialogs()->showTabularCreate();
894 AutoSave(owner->view());
898 owner->view()->menuUndo();
902 owner->view()->menuRedo();
905 case LFUN_MENUSEARCH:
907 // Ok this is one _very_ bad solution, but I think that some
908 // of this will be rewritten as part of GUI indep anyway.
910 static LyXFindReplace FR_;
911 FR_.StartSearch(owner->view());
915 case LFUN_REMOVEERRORS:
916 if (owner->view()->removeAutoInsets()) {
917 owner->view()->redraw();
918 owner->view()->fitCursor(TEXT());
924 owner->view()->openStuff();
928 case LFUN_HYPHENATION:
929 owner->view()->hyphenationPoint();
933 owner->view()->ldots();
936 case LFUN_END_OF_SENTENCE:
937 owner->view()->endOfSentenceDot();
940 case LFUN_MENU_SEPARATOR:
941 owner->view()->menuSeparator();
945 owner->view()->hfill();
949 changeDepth(owner->view(), TEXT(false), 0);
953 changeDepth(owner->view(), TEXT(false), -1);
956 case LFUN_DEPTH_PLUS:
957 changeDepth(owner->view(), TEXT(false), 1);
962 owner->view()->setState();
968 owner->view()->setState();
976 case LFUN_RECONFIGURE:
977 Reconfigure(owner->view());
981 if (owner->view()->available()
982 && !TEXT()->selection
983 && TEXT()->cursor.par()->footnoteflag
984 != LyXParagraph::NO_FOOTNOTE)
985 { // only melt footnotes with FOOTMELT, not margins etc
986 if (TEXT()->cursor.par()->footnotekind == LyXParagraph::FOOTNOTE)
991 owner->view()->setState();
994 case LFUN_MARGINMELT:
995 if (owner->view()->available()
996 && !TEXT()->selection
997 && TEXT()->cursor.par()->footnoteflag
998 != LyXParagraph::NO_FOOTNOTE) {
1000 if (TEXT()->cursor.par()->footnotekind == LyXParagraph::MARGIN)
1001 Melt(owner->view());
1003 Margin(owner->view());
1004 owner->view()->setState();
1008 case LFUN_FLOATSOPERATE:
1009 if (argument == "openfoot")
1010 owner->view()->allFloats(1,0);
1011 else if (argument == "closefoot")
1012 owner->view()->allFloats(0,0);
1013 else if (argument == "openfig")
1014 owner->view()->allFloats(1,1);
1015 else if (argument == "closefig")
1016 owner->view()->allFloats(0,1);
1019 #warning Find another implementation here (or another lyxfunc)!
1021 case LFUN_HELP_COPYRIGHT:
1022 owner->getDialogs()->showCopyright();
1025 case LFUN_HELP_CREDITS:
1026 owner->getDialogs()->showCredits();
1029 case LFUN_HELP_OPEN: {
1030 string arg = argument;
1032 setErrorMessage(N_("Missing argument"));
1035 ProhibitInput(owner->view());
1036 string fname = i18nLibFileSearch("doc", arg, "lyx");
1037 if (fname.empty()) {
1038 lyxerr << "LyX: unable to find documentation file `"
1039 << arg << "'. Bad installation?" << endl;
1040 AllowInput(owner->view());
1043 owner->getMiniBuffer()->Set(_("Opening help file"),
1044 MakeDisplayPath(fname),"...");
1045 owner->view()->buffer(bufferlist.loadLyXFile(fname,false));
1046 AllowInput(owner->view());
1050 case LFUN_HELP_VERSION: {
1051 ProhibitInput(owner->view());
1052 string msg(_("LyX Version "));
1056 fl_show_message(msg.c_str(),
1057 (_("Library directory: ")
1058 + MakeDisplayPath(system_lyxdir)).c_str(),
1059 (_("User directory: ")
1060 + MakeDisplayPath(user_lyxdir)).c_str());
1061 AllowInput(owner->view());
1065 // --- version control -------------------------------
1066 case LFUN_VC_REGISTER:
1068 if (!owner->buffer()->lyxvc.inUse())
1069 owner->buffer()->lyxvc.registrer();
1073 case LFUN_VC_CHECKIN:
1075 if (owner->buffer()->lyxvc.inUse()
1076 && !owner->buffer()->isReadonly())
1077 owner->buffer()->lyxvc.checkIn();
1081 case LFUN_VC_CHECKOUT:
1083 if (owner->buffer()->lyxvc.inUse()
1084 && owner->buffer()->isReadonly())
1085 owner->buffer()->lyxvc.checkOut();
1089 case LFUN_VC_REVERT:
1091 owner->buffer()->lyxvc.revert();
1097 owner->buffer()->lyxvc.undoLast();
1101 case LFUN_VC_HISTORY:
1103 owner->getDialogs()->showVCLogFile();
1107 // --- buffers ----------------------------------------
1109 case LFUN_SWITCHBUFFER:
1110 owner->view()->buffer(bufferlist.getBuffer(argument));
1115 // servercmd: argument must be <file>:<template>
1116 Buffer * tmpbuf = NewLyxFile(argument);
1118 owner->view()->buffer(tmpbuf);
1122 case LFUN_FILE_OPEN:
1123 owner->view()->buffer(bufferlist.loadLyXFile(argument));
1126 case LFUN_LATEX_LOG:
1127 owner->getDialogs()->showLogFile();
1132 lyxerr.debug() << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1133 int sel = strToInt(argument);
1134 lyxerr.debug() << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1136 // Should this give a setMessage instead?
1138 return string(); // illegal argument
1140 --sel; // sel 1..., but layout 0...
1142 // Pretend we got the name instead.
1143 Dispatch(int(LFUN_LAYOUT),
1144 textclasslist.NameOfLayout(owner->view()
1145 ->buffer()->params.textclass,
1150 case LFUN_LAYOUT_DOCUMENT:
1151 owner->getDialogs()->showLayoutDocument();
1154 case LFUN_LAYOUT_PARAGRAPH:
1155 #ifdef USE_OLD_PARAGRAPH_LAYOUT
1156 MenuLayoutParagraph();
1158 owner->getDialogs()->showLayoutParagraph();
1162 case LFUN_LAYOUT_CHARACTER:
1163 MenuLayoutCharacter();
1166 case LFUN_LAYOUT_TABULAR:
1167 if (owner->view()->theLockingInset()) {
1168 if (owner->view()->theLockingInset()->LyxCode()==Inset::TABULAR_CODE) {
1169 InsetTabular * inset = static_cast<InsetTabular *>
1170 (owner->view()->theLockingInset());
1171 inset->OpenLayoutDialog(owner->view());
1172 } else if (owner->view()->theLockingInset()->
1173 GetFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1174 InsetTabular * inset = static_cast<InsetTabular *>(
1175 owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE));
1176 inset->OpenLayoutDialog(owner->view());
1181 case LFUN_LAYOUT_PREAMBLE:
1182 owner->getDialogs()->showPreamble();
1185 case LFUN_LAYOUT_SAVE_DEFAULT:
1186 MenuLayoutSave(owner->view());
1189 case LFUN_DROP_LAYOUTS_CHOICE:
1190 owner->getToolbar()->openLayoutList();
1193 case LFUN_MENU_OPEN_BY_NAME:
1194 owner->getMenubar()->openByName(argument);
1195 break; // RVDK_PATCH_5
1197 case LFUN_SPELLCHECK:
1198 if (lyxrc.isp_command != "none")
1199 ShowSpellChecker(owner->view());
1200 break; // RVDK_PATCH_5
1202 // --- lyxserver commands ----------------------------
1206 setMessage(owner->buffer()->fileName());
1207 lyxerr.debug() << "FNAME["
1208 << owner->buffer()->fileName()
1216 dispatch_buffer = buf;
1217 lyxserver->notifyClient(dispatch_buffer);
1221 case LFUN_GOTOFILEROW:
1223 char file_name[100];
1225 ::sscanf(argument.c_str(), " %s %d", file_name, &row);
1227 // Must replace extension of the file to be .lyx and get full path
1228 string s = ChangeExtension(string(file_name), ".lyx");
1230 // Either change buffer or load the file
1231 if (bufferlist.exists(s))
1232 owner->view()->buffer(bufferlist.getBuffer(s));
1234 owner->view()->buffer(bufferlist.loadLyXFile(s));
1237 owner->view()->setCursorFromRow(row);
1240 owner->view()->center();
1244 case LFUN_GOTO_PARAGRAPH:
1246 istringstream istr(argument.c_str());
1250 LyXParagraph * par = TEXT()->GetParFromID(id);
1253 TEXT()->SetCursor(owner->view(), par, 0);
1254 owner->view()->setState();
1258 owner->view()->center();
1265 int const qa = lyxaction.LookupFunc(argument);
1266 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1270 // --- toolbar ----------------------------------
1271 case LFUN_PUSH_TOOLBAR:
1273 int nth = strToInt(argument);
1275 setErrorMessage(N_("Push-toolbar needs argument > 0"));
1277 owner->getToolbar()->push(nth);
1282 case LFUN_ADD_TO_TOOLBAR:
1284 if (lyxerr.debugging(Debug::GUI)) {
1285 lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1286 "argument = `" << argument << '\'' << endl;
1288 string tmp(argument);
1289 //lyxerr <<string("Argument: ") + argument);
1290 //lyxerr <<string("Tmp : ") + tmp);
1292 setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1294 owner->getToolbar()->add(argument, false);
1295 owner->getToolbar()->set();
1300 // --- insert characters ----------------------------------------
1302 // --- Mathed stuff. If we are here, there is no locked inset yet.
1307 if (!greek_kb_flag) {
1309 setMessage(N_("Math greek mode on"));
1316 case LFUN_GREEK_TOGGLE:
1318 greek_kb_flag = greek_kb_flag ? 0 : 2;
1319 if (greek_kb_flag) {
1320 setMessage(N_("Math greek keyboard on"));
1322 setMessage(N_("Math greek keyboard off"));
1327 case LFUN_MATH_NUMBER:
1328 case LFUN_MATH_LIMITS:
1330 setErrorMessage(N_("This is only allowed in math mode!"));
1335 case LFUN_MATH_PANEL:
1337 show_symbols_form(this);
1341 case LFUN_CITATION_CREATE:
1343 InsetCommandParams p( "cite" );
1345 if (!argument.empty()) {
1346 // This should be set at source, ie when typing
1347 // "citation-insert foo" in the minibuffer.
1348 // Question: would pybibliographer also need to be
1349 // changed. Suspect so. Leave as-is therefore.
1350 if (contains(argument, "|")) {
1351 p.setContents( token(argument, '|', 0) );
1352 p.setOptions( token(argument, '|', 1) );
1354 p.setContents( argument );
1356 Dispatch(LFUN_CITATION_INSERT, p.getAsString());
1358 owner->getDialogs()->createCitation( p.getAsString() );
1362 case LFUN_CHILD_CREATE:
1364 InsetCommandParams p("include");
1366 if (!argument.empty()) {
1367 if (contains(argument, "|")) {
1368 p.setContents(token(argument, '|', 0));
1369 p.setOptions(token(argument, '|', 1));
1371 p.setContents(argument);
1372 Dispatch(LFUN_CHILD_INSERT, p.getAsString());
1374 owner->getDialogs()->createInclude(p.getAsString());
1378 case LFUN_CHILDOPEN:
1380 string const filename =
1381 MakeAbsPath(argument,
1382 OnlyPath(owner->buffer()->fileName()));
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));
1394 case LFUN_INSERTFOOTNOTE:
1396 LyXParagraph::footnote_kind kind;
1397 if (argument == "footnote") {
1398 kind = LyXParagraph::FOOTNOTE;
1399 } else if (argument == "margin") {
1400 kind = LyXParagraph::MARGIN;
1401 } else if (argument == "figure") {
1402 kind = LyXParagraph::FIG;
1403 } else if (argument == "table") {
1404 kind = LyXParagraph::TAB;
1405 } else if (argument == "wide-fig") {
1406 kind = LyXParagraph::WIDE_FIG;
1407 } else if (argument == "wide-tab") {
1408 kind = LyXParagraph::WIDE_TAB;
1409 } else if (argument == "algorithm") {
1410 kind = LyXParagraph::ALGORITHM;
1412 setErrorMessage(N_("Unknown kind of footnote"));
1415 TEXT()->InsertFootnoteEnvironment(owner->view(), kind);
1416 owner->view()->update(TEXT(),
1417 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1418 owner->view()->setState();
1422 case LFUN_TOGGLECURSORFOLLOW:
1423 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1426 case LFUN_KMAP_OFF: // keymap off
1427 owner->getIntl()->KeyMapOn(false);
1430 case LFUN_KMAP_PRIM: // primary keymap
1431 owner->getIntl()->KeyMapPrim();
1434 case LFUN_KMAP_SEC: // secondary keymap
1435 owner->getIntl()->KeyMapSec();
1438 case LFUN_KMAP_TOGGLE: // toggle keymap
1439 owner->getIntl()->ToggleKeyMap();
1444 // argument contains ';'-terminated commands
1445 while (argument.find(';') != string::npos) {
1447 argument = split(argument, first, ';');
1453 case LFUN_DIALOG_PREFERENCES:
1454 owner->getDialogs()->showPreferences();
1457 case LFUN_SAVEPREFERENCES:
1459 Path p(user_lyxdir);
1460 lyxrc.write("preferences");
1464 case LFUN_SCREEN_FONT_UPDATE:
1466 // handle the screen font changes.
1468 lyxrc.set_font_norm_type();
1469 fontloader.update();
1470 // Of course we should only do the resize and the textcache.clear
1471 // if values really changed...but not very important right now. (Lgb)
1472 // All buffers will need resize
1473 bufferlist.resize();
1474 // We also need to empty the textcache so that
1475 // the buffer will be formatted correctly after
1481 case LFUN_SET_COLOR:
1484 string const x11_name = split(argument, lyx_name, ' ');
1485 if (lyx_name.empty() || x11_name.empty()) {
1486 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1491 if (!lcolor.setColor(lyx_name, x11_name)) {
1492 static string const err1 (N_("Set-color \""));
1493 static string const err2 (
1494 N_("\" failed - color is undefined "
1495 "or may not be redefined"));
1496 setErrorMessage(_(err1) + lyx_name + _(err2));
1499 lyxColorHandler->updateColor(lcolor.getFromLyXName(lyx_name));
1500 owner->view()->redraw();
1505 // Then if it was none of the above
1506 if (!owner->view()->Dispatch(action, argument))
1507 lyxerr << "A truly unknown func!" << endl;
1513 string const res = getMessage();
1516 if (!commandshortcut.empty()) {
1517 string const newbuf =
1518 owner->getMiniBuffer()->GetText();
1519 if (newbuf != commandshortcut) {
1520 owner->getMiniBuffer()->Set(newbuf
1528 msg += commandshortcut;
1529 owner->getMiniBuffer()->Set(msg);
1536 void LyXFunc::setupLocalKeymap()
1538 keyseq.stdmap = keyseq.curmap = toplevel_keymap;
1539 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap;
1543 void LyXFunc::MenuNew(bool fromTemplate)
1545 string initpath = lyxrc.document_path;
1548 if (owner->view()->available()) {
1549 string const trypath = owner->buffer()->filepath;
1550 // If directory is writeable, use this as default.
1551 if (IsDirWriteable(trypath) == 1)
1555 static int newfile_number = 0;
1558 if (lyxrc.new_ask_filename) {
1559 ProhibitInput(owner->view());
1560 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
1561 fileDlg.SetButton(1, _("Templates"), lyxrc.template_path);
1562 string fname = fileDlg.Select(_("Enter Filename for new document"),
1563 initpath, "*.lyx", _("newfile"));
1564 AllowInput(owner->view());
1566 if (fname.empty()) {
1567 owner->getMiniBuffer()->Set(_("Canceled."));
1568 lyxerr.debug() << "New Document Cancelled." << endl;
1572 // get absolute path of file and make sure the filename ends
1574 s = MakeAbsPath(fname);
1575 if (!IsLyXFilename(s))
1578 // Check if the document already is open
1579 if (bufferlist.exists(s)) {
1580 switch (AskConfirmation(_("Document is already open:"),
1581 MakeDisplayPath(s, 50),
1582 _("Do you want to close that document now?\n"
1583 "('No' will just switch to the open version)")))
1585 case 1: // Yes: close the document
1586 if (!bufferlist.close(bufferlist.getBuffer(s)))
1587 // If close is canceled, we cancel here too.
1590 case 2: // No: switch to the open document
1591 owner->view()->buffer(bufferlist.getBuffer(s));
1593 case 3: // Cancel: Do nothing
1594 owner->getMiniBuffer()->Set(_("Canceled."));
1598 // Check whether the file already exists
1599 if (IsLyXFilename(s)) {
1601 if (fi.readable() &&
1602 AskQuestion(_("File already exists:"),
1603 MakeDisplayPath(s, 50),
1604 _("Do you want to open the document?"))) {
1606 owner->getMiniBuffer()->Set(_("Opening document"),
1607 MakeDisplayPath(s), "...");
1608 XFlush(fl_get_display());
1609 owner->view()->buffer(
1610 bufferlist.loadLyXFile(s));
1611 owner->getMiniBuffer()->Set(_("Document"),
1618 s = AddName(lyxrc.document_path,
1619 "newfile" + tostr(++newfile_number) + ".lyx");
1621 while (bufferlist.exists(s) || fi.readable()) {
1623 s = AddName(lyxrc.document_path,
1624 "newfile" + tostr(newfile_number) +
1630 // The template stuff
1633 ProhibitInput(owner->view());
1634 string const fname = fileDlg.Select(_("Choose template"),
1635 lyxrc.template_path,
1637 AllowInput(owner->view());
1638 if (fname.empty()) return;
1642 // find a free buffer
1643 lyxerr.debug() << "Find a free buffer." << endl;
1644 owner->view()->buffer(bufferlist.newFile(s, templname));
1648 void LyXFunc::MenuOpen()
1650 string initpath = lyxrc.document_path;
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) == 1)
1661 ProhibitInput(owner->view());
1662 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
1663 fileDlg.SetButton(1, _("Examples"),
1664 AddPath(system_lyxdir, "examples"));
1665 string filename = fileDlg.Select(_("Select Document to Open"),
1667 AllowInput(owner->view());
1669 // check selected filename
1670 if (filename.empty()) {
1671 owner->getMiniBuffer()->Set(_("Canceled."));
1675 // get absolute path of file and make sure the filename ends
1677 filename = MakeAbsPath(filename);
1678 if (!IsLyXFilename(filename))
1682 owner->getMiniBuffer()->Set(_("Opening document"),
1683 MakeDisplayPath(filename), "...");
1684 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1686 owner->view()->buffer(openbuf);
1687 owner->getMiniBuffer()->Set(_("Document"),
1688 MakeDisplayPath(filename),
1691 owner->getMiniBuffer()->Set(_("Could not open document"),
1692 MakeDisplayPath(filename));
1697 // checks for running without gui are missing.
1698 void LyXFunc::doImport(string const & argument)
1701 string filename = split(argument, format, ' ');
1702 lyxerr.debug() << "LyXFunc::doImport: " << format
1703 << " file: " << filename << endl;
1705 if (filename.empty()) { // need user interaction
1706 string initpath = lyxrc.document_path;
1709 if (owner->view()->available()) {
1710 string const trypath = owner->buffer()->filepath;
1711 // If directory is writeable, use this as default.
1712 if (IsDirWriteable(trypath) == 1)
1717 ProhibitInput(owner->view());
1718 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
1719 fileDlg.SetButton(1, _("Examples"),
1720 AddPath(system_lyxdir, "examples"));
1721 string const text = _("Select ") + formats.PrettyName(format)
1722 + _(" file to import");
1723 string const extension = "*." + formats.Extension(format);
1724 filename = fileDlg.Select(text, initpath, extension);
1725 AllowInput(owner->view());
1727 // check selected filename
1728 if (filename.empty())
1729 owner->getMiniBuffer()->Set(_("Canceled."));
1732 // still no filename? abort
1733 if (filename.empty())
1736 // get absolute path of file
1737 filename = MakeAbsPath(filename);
1739 string const lyxfile = ChangeExtension(filename, ".lyx");
1741 // Check if the document already is open
1742 if (bufferlist.exists(lyxfile)) {
1743 switch (AskConfirmation(_("Document is already open:"),
1744 MakeDisplayPath(lyxfile, 50),
1745 _("Do you want to close that document now?\n"
1746 "('No' will just switch to the open version)")))
1748 case 1: // Yes: close the document
1749 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1750 // If close is canceled, we cancel here too.
1753 case 2: // No: switch to the open document
1754 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1756 case 3: // Cancel: Do nothing
1757 owner->getMiniBuffer()->Set(_("Canceled."));
1762 // Check if a LyX document by the same root exists in filesystem
1763 FileInfo const f(lyxfile, true);
1764 if (f.exist() && !AskQuestion(_("A document by the name"),
1765 MakeDisplayPath(lyxfile),
1766 _("already exists. Overwrite?"))) {
1767 owner->getMiniBuffer()->Set(_("Canceled."));
1770 // filename should be valid now
1772 Importer::Import(owner, filename, format);
1776 void LyXFunc::reloadBuffer()
1778 string const fn = owner->buffer()->fileName();
1779 if (bufferlist.close(owner->buffer()))
1780 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1784 void LyXFunc::CloseBuffer()
1786 if (bufferlist.close(owner->buffer()) && !quitting) {
1787 if (bufferlist.empty()) {
1788 // need this otherwise SEGV may occur while trying to
1789 // set variables that don't exist
1790 // since there's no current buffer
1791 owner->getDialogs()->hideBufferDependent();
1793 owner->view()->buffer(bufferlist.first());
1799 // Each "owner" should have it's own message method. lyxview and
1800 // the minibuffer would use the minibuffer, but lyxserver would
1801 // send an ERROR signal to its client. Alejandro 970603
1802 // This func is bit problematic when it comes to NLS, to make the
1803 // lyx servers client be language indepenent we must not translate
1804 // strings sent to this func.
1805 void LyXFunc::setErrorMessage(string const & m) const
1807 dispatch_buffer = m;
1812 void LyXFunc::setMessage(string const & m)
1814 dispatch_buffer = m;