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 * ====================================================== */
25 #pragma implementation
28 #include "support/lyxalgo.h"
32 #include "bufferlist.h"
33 #include "ColorHandler.h"
34 #include "lyxserver.h"
35 #include "figure_form.h"
39 #include "LyXAction.h"
40 #include "insets/inseturl.h"
41 #include "insets/insetlatexaccent.h"
42 #include "insets/insettoc.h"
43 #include "insets/insetref.h"
44 #include "insets/insetparent.h"
45 #include "insets/insetindex.h"
46 #include "insets/insetinclude.h"
47 #include "insets/insetbib.h"
48 #include "insets/insetcite.h"
49 #include "insets/insettext.h"
50 #include "insets/insetert.h"
51 #include "insets/insetexternal.h"
52 #include "insets/insetgraphics.h"
53 #include "insets/insetfoot.h"
54 #include "insets/insetmarginal.h"
55 #include "insets/insetminipage.h"
56 #include "insets/insetfloat.h"
57 #include "insets/insetlist.h"
58 #include "insets/insettabular.h"
59 #include "insets/insettheorem.h"
60 #include "insets/insetcaption.h"
61 #include "mathed/formulamacro.h"
62 #include "spellchecker.h" // RVDK_PATCH_5
63 #include "minibuffer.h"
66 #include "lyx_gui_misc.h"
67 #include "support/filetools.h"
68 #include "support/FileInfo.h"
69 #include "support/syscall.h"
70 #include "support/lstrings.h"
71 #include "support/path.h"
72 #include "support/lyxfunctional.h"
77 #include "trans_mgr.h"
80 #include "bufferview_funcs.h"
81 #include "frontends/FileDialog.h"
82 #include "frontends/Dialogs.h"
83 #include "frontends/Toolbar.h"
84 #include "frontends/Menubar.h"
85 #include "FloatList.h"
86 #include "converter.h"
89 #include "FontLoader.h"
90 #include "TextCache.h"
98 extern BufferList bufferlist;
99 extern LyXServer * lyxserver;
100 extern int greek_kb_flag;
101 extern bool selection_possible;
102 extern void MenuSendto();
104 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
106 extern void show_symbols_form(LyXFunc *);
108 extern LyXAction lyxaction;
110 extern tex_accent_struct get_accent(kb_action action);
112 extern LyXTextClass::size_type current_layout;
114 extern void ShowLatexLog();
120 class MiniBufferController : public SigC::Object {
123 MiniBufferController() {
127 &MiniBufferController::receiveCommand));
131 &MiniBufferController::receiveArg));
134 void receiveCmd(string const & cmd) {}
136 void receiveArg(string const & arg) {}
144 MiniBufferController mb_ctrl;
150 /* === globals =========================================================== */
152 // Initialization of static member var
153 bool LyXFunc::show_sc = true;
156 LyXFunc::LyXFunc(LyXView * o)
160 lyx_dead_action = LFUN_NOACTION;
161 lyx_calling_dead_action = LFUN_NOACTION;
167 LyXText * LyXFunc::TEXT(bool flag = true) const
170 return owner->view()->text;
171 return owner->view()->getLyXText();
175 // I changed this func slightly. I commented out the ...FinishUndo(),
176 // this means that all places that used to have a moveCursorUpdate, now
177 // have a ...FinishUndo() as the preceeding statement. I have also added
178 // a moveCursorUpdate to some of the functions that updated the cursor, but
179 // that did not show its new position.
181 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
183 if (selecting || TEXT(flag)->mark_set) {
184 TEXT(flag)->SetSelection(owner->view());
185 if (TEXT(flag)->bv_owner)
186 owner->view()->toggleToggle();
188 owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
189 owner->view()->showCursor();
191 /* ---> Everytime the cursor is moved, show the current font state. */
192 // should this too me moved out of this func?
193 //owner->showState();
194 owner->view()->setState();
198 void LyXFunc::handleKeyFunc(kb_action action)
200 char c = keyseq.getiso();
202 if (keyseq.length != -1) c = 0;
204 owner->getIntl()->getTrans()
205 .deadkey(c, get_accent(action).accent, TEXT(false));
206 // Need to reset, in case the minibuffer calls these
210 // copied verbatim from do_accent_char
211 owner->view()->update(TEXT(false),
212 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
213 TEXT(false)->sel_cursor = TEXT(false)->cursor;
217 void LyXFunc::processKeySym(KeySym keysym, unsigned int state)
221 if (lyxerr.debugging(Debug::KEY)) {
222 char const * tmp = XKeysymToString(keysym);
223 string const stm = (tmp ? tmp : "");
224 lyxerr << "KeySym is "
227 << keysym << "] State is ["
231 // Do nothing if we have nothing (JMarc)
232 if (keysym == NoSymbol) {
233 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
240 if (owner->view()->available()) {
241 // this function should be used always [asierra060396]
242 UpdatableInset * tli = owner->view()->theLockingInset();
243 if (tli && (keysym == XK_Escape)) {
244 if (tli == tli->GetLockingInset()) {
245 owner->view()->unlockInset(tli);
246 TEXT()->CursorRight(owner->view());
247 moveCursorUpdate(true, false);
250 tli->UnlockInsetInInset(owner->view(),
251 tli->GetLockingInset(),
260 // Can we be sure that this will work for all X-Windows
261 // implementations? (Lgb)
262 // This code snippet makes lyx ignore some keys. Perhaps
263 // all of them should be explictly mentioned?
264 if ((keysym >= XK_Shift_L && keysym <= XK_Hyper_R)
265 || keysym == XK_Mode_switch || keysym == 0x0) {
270 // Do a one-deep top-level lookup for
271 // cancel and meta-fake keys. RVDK_PATCH_5
272 cancel_meta_seq.reset();
274 int action = cancel_meta_seq.addkey(keysym, state
275 &(ShiftMask|ControlMask
277 if (lyxerr.debugging(Debug::KEY)) {
278 lyxerr << "action first set to [" << action << "]" << endl;
281 // When not cancel or meta-fake, do the normal lookup.
282 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
283 // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
284 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
285 if (lyxerr.debugging(Debug::KEY)) {
286 lyxerr << "meta_fake_bit is ["
287 << meta_fake_bit << "]" << endl;
289 // remove Caps Lock and Mod2 as a modifiers
290 action = keyseq.addkey(keysym,
291 (state | meta_fake_bit)
292 &(ShiftMask|ControlMask
294 if (lyxerr.debugging(Debug::KEY)) {
295 lyxerr << "action now set to ["
296 << action << "]" << endl;
299 // Dont remove this unless you know what you are doing.
302 if (action == 0) action = LFUN_PREFIX;
304 if (lyxerr.debugging(Debug::KEY)) {
313 // already here we know if it any point in going further
314 // why not return already here if action == -1 and
315 // num_bytes == 0? (Lgb)
317 if (keyseq.length > 1 || keyseq.length < -1) {
325 if (keyseq.length < -1) { // unknown key sequence...
328 owner->message(_("Unknown sequence:") + ' ' + buf);
332 char const isochar = keyseq.getiso();
333 if (!(state & ControlMask) &&
334 !(state & Mod1Mask) &&
335 (isochar && keysym < 0xF000)) {
338 if (argument.empty()) {
339 lyxerr.debug() << "Empty argument!" << endl;
340 // This can`t possibly be of any use
341 // so we`ll skip the dispatch.
345 // It is unknown, but what if we remove all
346 // the modifiers? (Lgb)
347 action = keyseq.addkey(keysym, 0);
349 // We keep the shift state, but remove the others.
350 // This is for the sake of the LFUN_SELFINSERT below.
353 if (lyxerr.debugging(Debug::KEY)) {
354 lyxerr << "Removing modifiers...\n"
355 << "Action now set to ["
356 << action << "]" << endl;
359 owner->message(_("Unknown function."));
366 if (action == LFUN_SELFINSERT) {
367 // We must set the argument to the char looked up by
372 xke.send_event = False;
373 xke.display = fl_get_display();
383 xke.keycode = XKeysymToKeycode(fl_get_display(), keysym);
384 xke.same_screen = True;
387 int const res = XLookupString(&xke, ret, 10, &tmpkeysym, 0);
388 //Assert(keysym == tmpkeysym);
389 lyxerr[Debug::KEY] << "TmpKeysym ["
390 << tmpkeysym << "]" << endl;
393 argument = string(ret, res);
395 lyxerr[Debug::KEY] << "SelfInsert arg["
396 << argument << "]" << endl;
400 bool tmp_sc = show_sc;
402 Dispatch(action, argument);
409 LyXFunc::func_status LyXFunc::getStatus(int ac) const
411 return getStatus(ac, string());
415 LyXFunc::func_status LyXFunc::getStatus(int ac,
416 string const & not_to_use_arg) const
419 func_status flag = LyXFunc::OK;
421 Buffer * buf = owner->buffer();
423 if (lyxaction.isPseudoAction(ac))
424 action = lyxaction.retrieveActionArg(ac, argument);
426 action = static_cast<kb_action>(ac);
427 if (!not_to_use_arg.empty())
428 argument = not_to_use_arg; // exept here
431 if (action == LFUN_UNKNOWN_ACTION) {
432 setErrorMessage(N_("Unknown action"));
433 return LyXFunc::Unknown;
436 // Check whether we need a buffer
437 if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
438 // Yes we need a buffer, do we have one?
441 // Can we use a readonly buffer?
442 if (buf->isReadonly() &&
443 !lyxaction.funcHasFlag(action,
444 LyXAction::ReadOnly)) {
446 setErrorMessage(N_("Document is read-only"));
447 flag |= LyXFunc::Disabled;
451 setErrorMessage(N_("Command not allowed with"
452 "out any document open"));
453 flag |= LyXFunc::Disabled;
458 // I would really like to avoid having this switch and rather try to
459 // encode this in the function itself.
460 bool disable = false;
463 disable = !Exporter::IsExportable(buf, "dvi")
464 || lyxrc.print_command == "none";
467 disable = argument == "fax" &&
468 !Exporter::IsExportable(buf, argument);
471 disable = buf->undostack.empty();
474 disable = buf->redostack.empty();
476 case LFUN_SPELLCHECK:
477 disable = lyxrc.isp_command == "none";
480 disable = lyxrc.chktex_command == "none";
483 disable = !Exporter::IsExportable(buf, "program");
486 case LFUN_INSERTFOOTNOTE:
487 // Disable insertion of floats in a tabular.
489 if (owner->view()->theLockingInset()) {
490 disable = (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) ||
491 owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
495 case LFUN_LAYOUT_TABULAR:
497 if (owner->view()->theLockingInset()) {
498 disable = (owner->view()->theLockingInset()->LyxCode() != Inset::TABULAR_CODE) &&
499 !owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
503 case LFUN_TABULAR_FEATURE:
505 if (owner->view()->theLockingInset()) {
506 func_status ret = LyXFunc::Disabled;
507 if (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) {
508 ret = static_cast<InsetTabular *>
509 (owner->view()->theLockingInset())->
511 } else if (owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
512 ret = static_cast<InsetTabular *>
513 (owner->view()->theLockingInset()->
514 GetFirstLockingInsetOfType(Inset::TABULAR_CODE))->
520 static InsetTabular inset(*owner->buffer(), 1, 1);
524 ret = inset.getStatus(argument);
525 if ((ret & LyXFunc::ToggleOn) ||
526 (ret & LyXFunc::ToggleOff))
527 flag |= LyXFunc::ToggleOff;
531 case LFUN_VC_REGISTER:
532 disable = buf->lyxvc.inUse();
534 case LFUN_VC_CHECKIN:
535 disable = !buf->lyxvc.inUse() || buf->isReadonly();
537 case LFUN_VC_CHECKOUT:
538 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
542 case LFUN_VC_HISTORY:
543 disable = !buf->lyxvc.inUse();
545 case LFUN_BOOKMARK_GOTO:
546 disable = !owner->view()->
547 isSavedPosition(strToUnsignedInt(argument));
552 flag |= LyXFunc::Disabled;
555 func_status box = LyXFunc::ToggleOff;
556 LyXFont const & font =
557 TEXT(false)->real_current_font;
560 if (font.emph() == LyXFont::ON)
561 box = LyXFunc::ToggleOn;
564 if (font.noun() == LyXFont::ON)
565 box = LyXFunc::ToggleOn;
568 if (font.series() == LyXFont::BOLD_SERIES)
569 box = LyXFunc::ToggleOn;
572 if (font.latex() == LyXFont::ON)
573 box = LyXFunc::ToggleOn;
586 // temporary dispatch method
587 void LyXFunc::miniDispatch(string const & s)
593 string const LyXFunc::Dispatch(string const & s)
595 // Split command string into command and argument
597 string line = frontStrip(s);
598 string arg = strip(frontStrip(split(line, cmd, ' ')));
600 return Dispatch(lyxaction.LookupFunc(cmd), arg);
604 string const LyXFunc::Dispatch(int ac,
605 string const & do_not_use_this_arg)
607 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << ac
608 <<"] arg[" << do_not_use_this_arg << "]" << endl;
613 // we have not done anything wrong yet.
615 dispatch_buffer.erase();
617 // if action is a pseudo-action, we need the real action
618 if (lyxaction.isPseudoAction(ac)) {
620 action = static_cast<kb_action>
621 (lyxaction.retrieveActionArg(ac, tmparg));
625 action = static_cast<kb_action>(ac);
626 if (!do_not_use_this_arg.empty())
627 argument = do_not_use_this_arg; // except here
630 selection_possible = false;
632 if (owner->view()->available())
633 owner->view()->hideCursor();
635 // We cannot use this function here
636 if (getStatus(ac, do_not_use_this_arg) & Disabled)
637 goto exit_with_message;
639 commandshortcut.erase();
641 if (lyxrc.display_shortcuts && show_sc) {
642 if (action != LFUN_SELFINSERT) {
643 // Put name of command and list of shortcuts
644 // for it in minibuffer
645 string comname = lyxaction.getActionName(action);
647 int pseudoaction = action;
648 bool argsadded = false;
650 if (!argument.empty()) {
651 // If we have the command with argument,
654 lyxaction.searchActionArg(action,
657 if (pseudoaction == -1) {
658 pseudoaction = action;
660 comname += " " + argument;
665 string const shortcuts =
666 toplevel_keymap->findbinding(pseudoaction);
668 if (!shortcuts.empty()) {
669 comname += ": " + shortcuts;
670 } else if (!argsadded) {
671 comname += " " + argument;
674 if (!comname.empty()) {
675 comname = strip(comname);
676 commandshortcut = "(" + comname + ')';
677 owner->message(commandshortcut);
679 // Here we could even add a small pause,
680 // to annoy the user and make him learn
682 // No! That will just annoy, not teach
683 // anything. The user will read the messages
684 // if they are interested. (Asger)
689 if (owner->view()->available() && owner->view()->theLockingInset()) {
690 UpdatableInset::RESULT result;
691 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
692 (keyseq.length >= -1)))
694 if ((action==LFUN_UNKNOWN_ACTION) && argument.empty()){
695 argument = keyseq.getiso();
697 // Undo/Redo pre 0.13 is a bit tricky for insets.
698 if (action == LFUN_UNDO) {
701 UpdatableInset * inset =
702 owner->view()->theLockingInset();
703 inset->GetCursorPos(owner->view(), slx, sly);
704 owner->view()->unlockInset(inset);
705 owner->view()->menuUndo();
706 if (TEXT()->cursor.par()->
707 IsInset(TEXT()->cursor.pos())) {
708 inset = static_cast<UpdatableInset*>(
709 TEXT()->cursor.par()->
716 inset->Edit(owner->view(),slx,sly,0);
718 } else if (action == LFUN_REDO) {
721 UpdatableInset * inset = owner->view()->
723 inset->GetCursorPos(owner->view(), slx, sly);
724 owner->view()->unlockInset(inset);
725 owner->view()->menuRedo();
726 inset = static_cast<UpdatableInset*>(
727 TEXT()->cursor.par()->
731 inset->Edit(owner->view(),slx,sly,0);
733 } else if (((result=owner->view()->theLockingInset()->
734 LocalDispatch(owner->view(), action,
736 UpdatableInset::DISPATCHED) ||
737 (result == UpdatableInset::DISPATCHED_NOUPDATE))
740 //setMessage(N_("Text mode"));
742 case LFUN_UNKNOWN_ACTION:
743 case LFUN_BREAKPARAGRAPH:
745 TEXT()->CursorRight(owner->view());
746 owner->view()->setState();
750 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
751 TEXT()->CursorRight(owner->view());
752 moveCursorUpdate(true, false);
757 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
758 TEXT()->CursorRight(owner->view());
759 moveCursorUpdate(true, false);
764 TEXT()->CursorDown(owner->view());
765 moveCursorUpdate(true, false);
775 lyx::Assert(action != LFUN_SELECT_FILE_SYNC);
778 // --- Misc -------------------------------------------
779 case LFUN_WORDFINDFORWARD :
780 case LFUN_WORDFINDBACKWARD : {
781 static string last_search;
782 string searched_string;
784 if (!argument.empty()) {
785 last_search = argument;
786 searched_string = argument;
788 searched_string = last_search;
791 if (!searched_string.empty() &&
792 ((action == LFUN_WORDFINDBACKWARD) ?
793 SearchBackward(owner->view(), searched_string) :
794 SearchForward(owner->view(), searched_string))) {
796 // ??? What is that ???
797 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
800 // clear the selection (if there is any)
801 owner->view()->toggleSelection();
802 TEXT()->ClearSelection(owner->view());
804 // Move cursor so that successive C-s 's will not stand in place.
805 if (action == LFUN_WORDFINDFORWARD )
806 TEXT()->CursorRightOneWord(owner->view());
807 TEXT()->FinishUndo();
808 moveCursorUpdate(true, false);
811 // set the new selection
812 // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
813 owner->view()->toggleSelection(false);
816 // REMOVED : if (owner->view()->getWorkArea()->focus)
817 owner->view()->showCursor();
823 if (owner->view()->available()) {
824 owner->view()->update(TEXT(),
825 BufferView::SELECT|BufferView::FITCUR);
828 keyseq.print(buf, true);
829 //owner->getMiniBuffer()->Set(buf, string(), string(), 1);
834 // --- Misc -------------------------------------------
835 case LFUN_EXEC_COMMAND:
837 std::vector<string> allCmds;
838 std::transform(lyxaction.func_begin(), lyxaction.func_end(),
839 std::back_inserter(allCmds), lyx::firster());
840 static std::vector<string> hist;
841 owner->getMiniBuffer()->getString(MiniBuffer::spaces,
846 case LFUN_CANCEL: // RVDK_PATCH_5
849 if (owner->view()->available())
850 // cancel any selection
851 Dispatch(LFUN_MARK_OFF);
852 setMessage(N_("Cancel"));
855 case LFUN_META_FAKE: // RVDK_PATCH_5
857 meta_fake_bit = Mod1Mask;
859 keyseq.print(buf, true);
860 setMessage(buf); // RVDK_PATCH_5
864 case LFUN_READ_ONLY_TOGGLE:
865 if (owner->buffer()->lyxvc.inUse()) {
866 owner->buffer()->lyxvc.toggleReadOnly();
868 owner->buffer()->setReadonly(
869 !owner->buffer()->isReadonly());
873 case LFUN_CENTER: // this is center and redraw.
874 owner->view()->center();
877 // --- Menus -----------------------------------------------
882 case LFUN_MENUNEWTMPLT:
886 case LFUN_CLOSEBUFFER:
891 if (!owner->buffer()->isUnnamed()) {
892 string const s1 = _("Saving document") + ' '
893 + MakeDisplayPath(owner->buffer()->fileName()
897 MenuWrite(owner->view(), owner->buffer());
899 WriteAs(owner->view(), owner->buffer());
903 WriteAs(owner->view(), owner->buffer(), argument);
906 case LFUN_MENURELOAD:
911 Exporter::Export(owner->buffer(), argument, true);
915 Exporter::Preview(owner->buffer(), argument);
919 Exporter::Export(owner->buffer(), "program", true);
923 MenuRunChktex(owner->buffer());
927 owner->getDialogs()->showPrint();
931 if (argument == "custom")
934 Exporter::Export(owner->buffer(), argument, false);
952 InsetCommandParams p;
955 if (action == LFUN_TOCVIEW)
957 p.setCmdName("tableofcontents");
959 else if (action == LFUN_LOAVIEW )
960 p.setCmdName("listof{algorithm}{List of Algorithms}");
961 else if (action == LFUN_LOFVIEW)
962 p.setCmdName("listoffigures");
964 p.setCmdName("listoftables");
966 owner->getDialogs()->createTOC(p.getAsString());
970 case LFUN_DIALOG_TABULAR_INSERT:
971 owner->getDialogs()->showTabularCreate();
979 AutoSave(owner->view());
983 owner->view()->menuUndo();
987 owner->view()->menuRedo();
990 case LFUN_MENUSEARCH:
991 owner->getDialogs()->showSearch();
994 case LFUN_REMOVEERRORS:
995 if (owner->view()->removeAutoInsets()) {
996 owner->view()->redraw();
997 owner->view()->fitCursor(TEXT());
1001 case LFUN_HYPHENATION:
1002 owner->view()->hyphenationPoint();
1006 owner->view()->ldots();
1009 case LFUN_END_OF_SENTENCE:
1010 owner->view()->endOfSentenceDot();
1013 case LFUN_MENU_SEPARATOR:
1014 owner->view()->menuSeparator();
1018 owner->view()->hfill();
1022 changeDepth(owner->view(), TEXT(false), 0);
1025 case LFUN_DEPTH_MIN:
1026 changeDepth(owner->view(), TEXT(false), -1);
1029 case LFUN_DEPTH_PLUS:
1030 changeDepth(owner->view(), TEXT(false), 1);
1034 owner->getDialogs()->setUserFreeFont();
1039 owner->view()->setState();
1043 case LFUN_RECONFIGURE:
1044 Reconfigure(owner->view());
1048 case LFUN_FLOATSOPERATE:
1049 if (argument == "openfoot")
1050 owner->view()->allFloats(1,0);
1051 else if (argument == "closefoot")
1052 owner->view()->allFloats(0,0);
1053 else if (argument == "openfig")
1054 owner->view()->allFloats(1,1);
1055 else if (argument == "closefig")
1056 owner->view()->allFloats(0,1);
1059 #ifdef WITH_WARNINGS
1060 #warning Find another implementation here (or another lyxfunc)!
1063 case LFUN_HELP_COPYRIGHT:
1064 owner->getDialogs()->showCopyright();
1067 case LFUN_HELP_CREDITS:
1068 owner->getDialogs()->showCredits();
1071 case LFUN_HELP_OPEN:
1073 string const arg = argument;
1075 setErrorMessage(N_("Missing argument"));
1078 ProhibitInput(owner->view());
1079 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1080 if (fname.empty()) {
1081 lyxerr << "LyX: unable to find documentation file `"
1082 << arg << "'. Bad installation?" << endl;
1083 AllowInput(owner->view());
1086 string const str = _("Opening help file") + ' '
1087 + MakeDisplayPath(fname) + "...";
1089 owner->message(str);
1090 owner->view()->buffer(bufferlist.loadLyXFile(fname,false));
1091 AllowInput(owner->view());
1095 case LFUN_HELP_VERSION: {
1096 ProhibitInput(owner->view());
1097 string msg(_("LyX Version "));
1101 fl_show_message(msg.c_str(),
1102 (_("Library directory: ")
1103 + MakeDisplayPath(system_lyxdir)).c_str(),
1104 (_("User directory: ")
1105 + MakeDisplayPath(user_lyxdir)).c_str());
1106 AllowInput(owner->view());
1110 // --- version control -------------------------------
1111 case LFUN_VC_REGISTER:
1113 if (!owner->buffer()->lyxvc.inUse())
1114 owner->buffer()->lyxvc.registrer();
1118 case LFUN_VC_CHECKIN:
1120 if (owner->buffer()->lyxvc.inUse()
1121 && !owner->buffer()->isReadonly())
1122 owner->buffer()->lyxvc.checkIn();
1126 case LFUN_VC_CHECKOUT:
1128 if (owner->buffer()->lyxvc.inUse()
1129 && owner->buffer()->isReadonly())
1130 owner->buffer()->lyxvc.checkOut();
1134 case LFUN_VC_REVERT:
1136 owner->buffer()->lyxvc.revert();
1142 owner->buffer()->lyxvc.undoLast();
1146 case LFUN_VC_HISTORY:
1148 owner->getDialogs()->showVCLogFile();
1152 // --- buffers ----------------------------------------
1154 case LFUN_SWITCHBUFFER:
1155 owner->view()->buffer(bufferlist.getBuffer(argument));
1160 // servercmd: argument must be <file>:<template>
1161 Buffer * tmpbuf = NewLyxFile(argument);
1163 owner->view()->buffer(tmpbuf);
1167 case LFUN_FILE_OPEN:
1171 case LFUN_LATEX_LOG:
1172 owner->getDialogs()->showLogFile();
1177 lyxerr.debug() << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1178 int sel = strToInt(argument);
1179 lyxerr.debug() << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1181 // Should this give a setMessage instead?
1183 return string(); // illegal argument
1185 --sel; // sel 1..., but layout 0...
1187 // Pretend we got the name instead.
1188 Dispatch(int(LFUN_LAYOUT),
1189 textclasslist.NameOfLayout(owner->view()
1190 ->buffer()->params.textclass,
1195 case LFUN_LAYOUT_DOCUMENT:
1196 owner->getDialogs()->showLayoutDocument();
1199 case LFUN_LAYOUT_PARAGRAPH:
1200 owner->getDialogs()->showLayoutParagraph();
1203 case LFUN_LAYOUT_CHARACTER:
1204 owner->getDialogs()->showLayoutCharacter();
1207 case LFUN_LAYOUT_TABULAR:
1208 if (owner->view()->theLockingInset()) {
1209 if (owner->view()->theLockingInset()->LyxCode()==Inset::TABULAR_CODE) {
1210 InsetTabular * inset = static_cast<InsetTabular *>
1211 (owner->view()->theLockingInset());
1212 inset->OpenLayoutDialog(owner->view());
1213 } else if (owner->view()->theLockingInset()->
1214 GetFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1215 InsetTabular * inset = static_cast<InsetTabular *>(
1216 owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE));
1217 inset->OpenLayoutDialog(owner->view());
1222 case LFUN_LAYOUT_PREAMBLE:
1223 owner->getDialogs()->showPreamble();
1226 case LFUN_LAYOUT_SAVE_DEFAULT:
1227 MenuLayoutSave(owner->view());
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 ShowSpellChecker(owner->view());
1241 break; // RVDK_PATCH_5
1243 // --- lyxserver commands ----------------------------
1247 setMessage(owner->buffer()->fileName());
1248 lyxerr.debug() << "FNAME["
1249 << owner->buffer()->fileName()
1257 dispatch_buffer = buf;
1258 lyxserver->notifyClient(dispatch_buffer);
1262 case LFUN_GOTOFILEROW:
1264 char file_name[100];
1266 ::sscanf(argument.c_str(), " %s %d", file_name, &row);
1268 // Must replace extension of the file to be .lyx and get full path
1269 string s = ChangeExtension(string(file_name), ".lyx");
1271 // Either change buffer or load the file
1272 if (bufferlist.exists(s))
1273 owner->view()->buffer(bufferlist.getBuffer(s));
1275 owner->view()->buffer(bufferlist.loadLyXFile(s));
1278 owner->view()->setCursorFromRow(row);
1281 owner->view()->center();
1285 case LFUN_GOTO_PARAGRAPH:
1287 istringstream istr(argument.c_str());
1291 LyXParagraph * par = TEXT()->GetParFromID(id);
1296 TEXT()->SetCursor(owner->view(), par, 0);
1297 owner->view()->setState();
1301 owner->view()->center();
1308 int const qa = lyxaction.LookupFunc(argument);
1309 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1313 // --- toolbar ----------------------------------
1314 case LFUN_PUSH_TOOLBAR:
1316 int nth = strToInt(argument);
1318 setErrorMessage(N_("Push-toolbar needs argument > 0"));
1320 owner->getToolbar()->push(nth);
1325 case LFUN_ADD_TO_TOOLBAR:
1327 if (lyxerr.debugging(Debug::GUI)) {
1328 lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1329 "argument = `" << argument << '\'' << endl;
1331 string tmp(argument);
1332 //lyxerr <<string("Argument: ") + argument);
1333 //lyxerr <<string("Tmp : ") + tmp);
1335 setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1337 owner->getToolbar()->add(argument, false);
1338 owner->getToolbar()->set();
1343 // --- insert characters ----------------------------------------
1345 // --- Mathed stuff. If we are here, there is no locked inset yet.
1350 if (!greek_kb_flag) {
1352 setMessage(N_("Math greek mode on"));
1359 case LFUN_GREEK_TOGGLE:
1361 greek_kb_flag = greek_kb_flag ? 0 : 2;
1362 if (greek_kb_flag) {
1363 setMessage(N_("Math greek keyboard on"));
1365 setMessage(N_("Math greek keyboard off"));
1370 case LFUN_MATH_EXTERN:
1371 case LFUN_MATH_NUMBER:
1372 case LFUN_MATH_LIMITS:
1374 setErrorMessage(N_("This is only allowed in math mode!"));
1378 case LFUN_MATH_PANEL:
1379 owner->getDialogs()->showMathPanel();
1382 case LFUN_CITATION_CREATE:
1384 InsetCommandParams p( "cite" );
1386 if (!argument.empty()) {
1387 // This should be set at source, ie when typing
1388 // "citation-insert foo" in the minibuffer.
1389 // Question: would pybibliographer also need to be
1390 // changed. Suspect so. Leave as-is therefore.
1391 if (contains(argument, "|")) {
1392 p.setContents( token(argument, '|', 0) );
1393 p.setOptions( token(argument, '|', 1) );
1395 p.setContents( argument );
1397 Dispatch(LFUN_CITATION_INSERT, p.getAsString());
1399 owner->getDialogs()->createCitation( p.getAsString() );
1403 case LFUN_CHILDOPEN:
1405 string const filename =
1406 MakeAbsPath(argument,
1407 OnlyPath(owner->buffer()->fileName()));
1408 setMessage(N_("Opening child document ") +
1409 MakeDisplayPath(filename) + "...");
1410 owner->view()->savePosition(0);
1411 if (bufferlist.exists(filename))
1412 owner->view()->buffer(bufferlist.getBuffer(filename));
1414 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1418 case LFUN_TOGGLECURSORFOLLOW:
1419 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1422 case LFUN_KMAP_OFF: // keymap off
1423 owner->getIntl()->KeyMapOn(false);
1426 case LFUN_KMAP_PRIM: // primary keymap
1427 owner->getIntl()->KeyMapPrim();
1430 case LFUN_KMAP_SEC: // secondary keymap
1431 owner->getIntl()->KeyMapSec();
1434 case LFUN_KMAP_TOGGLE: // toggle keymap
1435 owner->getIntl()->ToggleKeyMap();
1440 // argument contains ';'-terminated commands
1441 while (argument.find(';') != string::npos) {
1443 argument = split(argument, first, ';');
1449 case LFUN_DIALOG_PREFERENCES:
1450 owner->getDialogs()->showPreferences();
1453 case LFUN_SAVEPREFERENCES:
1455 Path p(user_lyxdir);
1456 lyxrc.write("preferences");
1460 case LFUN_SCREEN_FONT_UPDATE:
1462 // handle the screen font changes.
1464 lyxrc.set_font_norm_type();
1465 fontloader.update();
1466 // Of course we should only do the resize and the textcache.clear
1467 // if values really changed...but not very important right now. (Lgb)
1468 // All buffers will need resize
1469 bufferlist.resize();
1470 // We also need to empty the textcache so that
1471 // the buffer will be formatted correctly after
1477 case LFUN_SET_COLOR:
1480 string const x11_name = split(argument, lyx_name, ' ');
1481 if (lyx_name.empty() || x11_name.empty()) {
1482 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1487 if (!lcolor.setColor(lyx_name, x11_name)) {
1488 static string const err1 (N_("Set-color \""));
1489 static string const err2 (
1490 N_("\" failed - color is undefined "
1491 "or may not be redefined"));
1492 setErrorMessage(_(err1) + lyx_name + _(err2));
1495 lyxColorHandler->updateColor(lcolor.getFromLyXName(lyx_name));
1496 owner->view()->redraw();
1501 owner->message(argument);
1504 case LFUN_MESSAGE_PUSH:
1505 owner->messagePush(argument);
1508 case LFUN_MESSAGE_POP:
1509 owner->messagePop();
1513 // Then if it was none of the above
1514 if (!owner->view()->Dispatch(action, argument))
1515 lyxerr << "A truly unknown func ["
1516 << action << "]!" << endl;
1522 string const res = getMessage();
1525 if (!commandshortcut.empty()) {
1526 owner->getMiniBuffer()->addSet(commandshortcut);
1529 string const msg(_(res) + ' ' + commandshortcut);
1530 owner->message(msg);
1537 void LyXFunc::setupLocalKeymap()
1539 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1540 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1544 void LyXFunc::MenuNew(bool fromTemplate)
1546 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 FileDialog fileDlg(owner, _("Enter filename for new document"),
1560 LFUN_SELECT_FILE_SYNC,
1561 make_pair(string(_("Documents")),
1562 string(lyxrc.document_path)),
1563 make_pair(string(_("Templates")),
1564 string(lyxrc.template_path)));
1566 FileDialog::Result result =
1567 fileDlg.Select(initpath,
1568 _("*.lyx|LyX Documents (*.lyx)"),
1571 if (result.second.empty()) {
1572 owner->message(_("Canceled."));
1573 lyxerr.debug() << "New Document Cancelled." << endl;
1577 // get absolute path of file and make sure the filename ends
1579 s = MakeAbsPath(result.second);
1580 if (!IsLyXFilename(s))
1583 // Check if the document already is open
1584 if (bufferlist.exists(s)) {
1585 switch (AskConfirmation(_("Document is already open:"),
1586 MakeDisplayPath(s, 50),
1587 _("Do you want to close that document now?\n"
1588 "('No' will just switch to the open version)")))
1590 case 1: // Yes: close the document
1591 if (!bufferlist.close(bufferlist.getBuffer(s)))
1592 // If close is canceled, we cancel here too.
1595 case 2: // No: switch to the open document
1596 owner->view()->buffer(bufferlist.getBuffer(s));
1598 case 3: // Cancel: Do nothing
1599 owner->message(_("Canceled."));
1603 // Check whether the file already exists
1604 if (IsLyXFilename(s)) {
1606 if (fi.readable() &&
1607 AskQuestion(_("File already exists:"),
1608 MakeDisplayPath(s, 50),
1609 _("Do you want to open the document?"))) {
1611 owner->message(_("Opening document") + ' '
1612 + MakeDisplayPath(s) + "...");
1613 XFlush(fl_get_display());
1614 owner->view()->buffer(
1615 bufferlist.loadLyXFile(s));
1616 owner->message(_("Document") + ' '
1617 + MakeDisplayPath(s) + ' '
1623 s = AddName(lyxrc.document_path,
1624 "newfile" + tostr(++newfile_number) + ".lyx");
1626 while (bufferlist.exists(s) || fi.readable()) {
1628 s = AddName(lyxrc.document_path,
1629 "newfile" + tostr(newfile_number) +
1635 // The template stuff
1638 FileDialog fileDlg(owner, _("Select template file"),
1639 LFUN_SELECT_FILE_SYNC,
1640 make_pair(string(_("Documents")),
1641 string(lyxrc.document_path)),
1642 make_pair(string(_("Templates")),
1643 string(lyxrc.template_path)));
1645 FileDialog::Result result =
1646 fileDlg.Select(initpath,
1647 _("*.lyx|LyX Documents (*.lyx)"));
1649 if (result.first == FileDialog::Later)
1652 string const fname = result.second;
1659 // find a free buffer
1660 lyxerr.debug() << "Find a free buffer." << endl;
1661 owner->view()->buffer(bufferlist.newFile(s, templname));
1665 void LyXFunc::Open(string const & fname)
1667 string initpath = lyxrc.document_path;
1669 if (owner->view()->available()) {
1670 string const trypath = owner->buffer()->filepath;
1671 // If directory is writeable, use this as default.
1672 if (IsDirWriteable(trypath) == 1)
1678 if (fname.empty()) {
1679 FileDialog fileDlg(owner, _("Select document to open"),
1681 make_pair(string(_("Documents")),
1682 string(lyxrc.document_path)),
1683 make_pair(string(_("Examples")),
1684 string(AddPath(system_lyxdir, "examples"))));
1686 FileDialog::Result result =
1687 fileDlg.Select(initpath,
1688 "*.lyx|LyX Documents (*.lyx)");
1690 if (result.first == FileDialog::Later)
1693 filename = result.second;
1695 // check selected filename
1696 if (filename.empty()) {
1697 owner->message(_("Canceled."));
1703 // get absolute path of file and make sure the filename ends
1705 filename = MakeAbsPath(filename);
1706 if (!IsLyXFilename(filename))
1710 owner->message(_("Opening document") + ' '
1711 + MakeDisplayPath(filename) + "...");
1712 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1714 owner->view()->buffer(openbuf);
1715 owner->message(_("Document") + ' '
1716 + MakeDisplayPath(filename)
1717 + ' ' + _("opened."));
1719 owner->message(_("Could not open document") + ' '
1720 + MakeDisplayPath(filename));
1725 // checks for running without gui are missing.
1726 void LyXFunc::doImport(string const & argument)
1729 string filename = split(argument, format, ' ');
1730 lyxerr.debug() << "LyXFunc::doImport: " << format
1731 << " file: " << filename << endl;
1733 if (filename.empty()) { // need user interaction
1734 string initpath = lyxrc.document_path;
1736 if (owner->view()->available()) {
1737 string const trypath = owner->buffer()->filepath;
1738 // If directory is writeable, use this as default.
1739 if (IsDirWriteable(trypath) == 1)
1743 string const text = _("Select ") + formats.PrettyName(format)
1744 + _(" file to import");
1746 FileDialog fileDlg(owner, text,
1748 make_pair(string(_("Documents")),
1749 string(lyxrc.document_path)),
1750 make_pair(string(_("Examples")),
1751 string(AddPath(system_lyxdir, "examples"))));
1753 string const extension = "*." + formats.Extension(format)
1754 + "| " + formats.PrettyName(format)
1755 + " (*." + formats.Extension(format) + ")";
1757 FileDialog::Result result = fileDlg.Select(initpath,
1760 if (result.first == FileDialog::Later)
1763 filename = result.second;
1765 // check selected filename
1766 if (filename.empty())
1767 owner->message(_("Canceled."));
1770 // still no filename? abort
1771 if (filename.empty())
1774 // get absolute path of file
1775 filename = MakeAbsPath(filename);
1777 string const lyxfile = ChangeExtension(filename, ".lyx");
1779 // Check if the document already is open
1780 if (bufferlist.exists(lyxfile)) {
1781 switch (AskConfirmation(_("Document is already open:"),
1782 MakeDisplayPath(lyxfile, 50),
1783 _("Do you want to close that document now?\n"
1784 "('No' will just switch to the open version)")))
1786 case 1: // Yes: close the document
1787 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1788 // If close is canceled, we cancel here too.
1791 case 2: // No: switch to the open document
1792 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1794 case 3: // Cancel: Do nothing
1795 owner->message(_("Canceled."));
1800 // Check if a LyX document by the same root exists in filesystem
1801 FileInfo const f(lyxfile, true);
1802 if (f.exist() && !AskQuestion(_("A document by the name"),
1803 MakeDisplayPath(lyxfile),
1804 _("already exists. Overwrite?"))) {
1805 owner->message(_("Canceled"));
1808 // filename should be valid now
1810 Importer::Import(owner, filename, format);
1814 void LyXFunc::reloadBuffer()
1816 string const fn = owner->buffer()->fileName();
1817 if (bufferlist.close(owner->buffer()))
1818 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1822 void LyXFunc::CloseBuffer()
1824 if (bufferlist.close(owner->buffer()) && !quitting) {
1825 if (bufferlist.empty()) {
1826 // need this otherwise SEGV may occur while trying to
1827 // set variables that don't exist
1828 // since there's no current buffer
1829 owner->getDialogs()->hideBufferDependent();
1831 owner->view()->buffer(bufferlist.first());
1837 // Each "owner" should have it's own message method. lyxview and
1838 // the minibuffer would use the minibuffer, but lyxserver would
1839 // send an ERROR signal to its client. Alejandro 970603
1840 // This func is bit problematic when it comes to NLS, to make the
1841 // lyx servers client be language indepenent we must not translate
1842 // strings sent to this func.
1843 void LyXFunc::setErrorMessage(string const & m) const
1845 dispatch_buffer = m;
1850 void LyXFunc::setMessage(string const & m)
1852 dispatch_buffer = m;
1856 void LyXFunc::initMiniBuffer()
1858 string text = _("Welcome to LyX!");
1860 // When meta-fake key is pressed, show the key sequence so far + "M-".
1866 // Else, when a non-complete key sequence is pressed,
1867 // show the available options.
1868 else if (keyseqUncomplete())
1869 text = keyseqOptions();
1871 // Else, show the buffer state.
1872 else if (owner->view()->available()) {
1873 Buffer * tmpbuf = owner->buffer();
1875 string const nicename =
1876 MakeDisplayPath(tmpbuf->fileName());
1877 // Should we do this instead? (kindo like emacs)
1878 // leaves more room for other information
1881 if (tmpbuf->lyxvc.inUse()) {
1883 text += tmpbuf->lyxvc.version();
1885 text += tmpbuf->lyxvc.locker();
1886 if (tmpbuf->isReadonly())
1889 } else if (tmpbuf->isReadonly())
1891 if (!tmpbuf->isLyxClean())
1892 text += _(" (Changed)");
1894 if (text != _("Welcome to LyX!")) // this is a hack
1895 text = _("* No document open *");
1898 owner->message(text);