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 * ====================================================== */
24 #pragma implementation
27 #include "support/lyxalgo.h"
31 #include "bufferlist.h"
32 #include "BufferView.h"
33 #include "ColorHandler.h"
34 #include "lyxserver.h"
38 #include "LyXAction.h"
39 #include "insets/inseturl.h"
40 #include "insets/insetlatexaccent.h"
41 #include "insets/insettoc.h"
42 #include "insets/insetref.h"
43 #include "insets/insetparent.h"
44 #include "insets/insetindex.h"
45 #include "insets/insetinclude.h"
46 #include "insets/insetbib.h"
47 #include "insets/insetcite.h"
48 #include "insets/insettext.h"
49 #include "insets/insetert.h"
50 #include "insets/insetexternal.h"
51 #include "insets/insetgraphics.h"
52 #include "insets/insetfoot.h"
53 #include "insets/insetmarginal.h"
54 #include "insets/insetminipage.h"
55 #include "insets/insetfloat.h"
56 #include "insets/insetlist.h"
57 #include "insets/insettabular.h"
58 #include "insets/insettheorem.h"
59 #include "insets/insetcaption.h"
60 #include "mathed/formulamacro.h"
61 #include "mathed/math_cursor.h"
62 #include "mathed/math_inset.h"
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"
92 #include "undo_funcs.h"
99 extern BufferList bufferlist;
100 extern LyXServer * lyxserver;
101 extern int greek_kb_flag;
102 extern bool selection_possible;
103 extern void MenuSendto();
105 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
107 extern void show_symbols_form(LyXFunc *);
109 extern LyXAction lyxaction;
111 extern tex_accent_struct get_accent(kb_action action);
113 extern LyXTextClass::size_type current_layout;
115 extern void ShowLatexLog();
121 class MiniBufferController : public SigC::Object {
124 MiniBufferController() {
128 &MiniBufferController::receiveCommand));
132 &MiniBufferController::receiveArg));
135 void receiveCmd(string const & cmd) {}
137 void receiveArg(string const & arg) {}
145 MiniBufferController mb_ctrl;
151 /* === globals =========================================================== */
153 // Initialization of static member var
154 bool LyXFunc::show_sc = true;
157 LyXFunc::LyXFunc(LyXView * o)
161 lyx_dead_action = LFUN_NOACTION;
162 lyx_calling_dead_action = LFUN_NOACTION;
168 LyXText * LyXFunc::TEXT(bool flag = true) const
171 return owner->view()->text;
172 return owner->view()->getLyXText();
176 // I changed this func slightly. I commented out the ...FinishUndo(),
177 // this means that all places that used to have a moveCursorUpdate, now
178 // have a ...FinishUndo() as the preceeding statement. I have also added
179 // a moveCursorUpdate to some of the functions that updated the cursor, but
180 // that did not show its new position.
182 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
184 if (selecting || TEXT(flag)->selection.mark()) {
185 TEXT(flag)->setSelection(owner->view());
186 if (TEXT(flag)->bv_owner)
187 owner->view()->toggleToggle();
189 owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
190 owner->view()->showCursor();
192 /* ---> Everytime the cursor is moved, show the current font state. */
193 // should this too me moved out of this func?
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)->selection.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)"
238 // Can we be sure that this will work for all X-Windows
239 // implementations? (Lgb)
240 // This code snippet makes lyx ignore some keys. Perhaps
241 // all of them should be explictly mentioned?
242 if ((keysym >= XK_Shift_L && keysym <= XK_Hyper_R)
243 || keysym == XK_Mode_switch || keysym == 0x0) {
247 // Do a one-deep top-level lookup for
248 // cancel and meta-fake keys. RVDK_PATCH_5
249 cancel_meta_seq.reset();
251 int action = cancel_meta_seq.addkey(keysym, state
252 &(ShiftMask|ControlMask
254 if (lyxerr.debugging(Debug::KEY)) {
255 lyxerr << "action first set to [" << action << "]" << endl;
258 // When not cancel or meta-fake, do the normal lookup.
259 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
260 // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
261 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
262 if (lyxerr.debugging(Debug::KEY)) {
263 lyxerr << "meta_fake_bit is ["
264 << meta_fake_bit << "]" << endl;
266 // remove Caps Lock and Mod2 as a modifiers
267 action = keyseq.addkey(keysym,
268 (state | meta_fake_bit)
269 &(ShiftMask|ControlMask
271 if (lyxerr.debugging(Debug::KEY)) {
272 lyxerr << "action now set to ["
273 << action << "]" << endl;
276 // Dont remove this unless you know what you are doing.
279 if (action == 0) action = LFUN_PREFIX;
281 if (lyxerr.debugging(Debug::KEY)) {
284 lyxerr << "Key [action="
290 // already here we know if it any point in going further
291 // why not return already here if action == -1 and
292 // num_bytes == 0? (Lgb)
294 if (keyseq.length > 1 || keyseq.length < -1) {
301 // It is unknown, but what if we remove all
302 // the modifiers? (Lgb)
303 action = keyseq.addkey(keysym, 0);
305 if (lyxerr.debugging(Debug::KEY)) {
306 lyxerr << "Removing modifiers...\n"
307 << "Action now set to ["
308 << action << "]" << endl;
311 owner->message(_("Unknown function."));
316 if (action == LFUN_SELFINSERT) {
317 // This is very X dependant.
318 unsigned int c = keysym;
320 switch (c & 0x0000FF00) {
321 // latin 1 byte 3 = 0
322 case 0x00000000: break;
323 // latin 2 byte 3 = 1
325 // latin 3 byte 3 = 2
327 // latin 4 byte 3 = 3
329 // latin 8 byte 3 = 18 (0x12)
331 // latin 9 byte 3 = 19 (0x13)
340 argument = static_cast<char>(c);
341 lyxerr[Debug::KEY] << "SelfInsert arg[`"
342 << argument << "']" << endl;
345 bool tmp_sc = show_sc;
347 Dispatch(action, argument);
354 func_status::value_type LyXFunc::getStatus(int ac) const
356 return getStatus(ac, string());
360 func_status::value_type LyXFunc::getStatus(int ac,
361 string const & not_to_use_arg) const
364 func_status::value_type flag = func_status::OK;
366 Buffer * buf = owner->buffer();
368 if (lyxaction.isPseudoAction(ac))
369 action = lyxaction.retrieveActionArg(ac, argument);
371 action = static_cast<kb_action>(ac);
372 if (!not_to_use_arg.empty())
373 argument = not_to_use_arg; // exept here
376 if (action == LFUN_UNKNOWN_ACTION) {
377 setErrorMessage(N_("Unknown action"));
378 return func_status::Unknown;
381 // Check whether we need a buffer
382 if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
383 // Yes we need a buffer, do we have one?
386 // Can we use a readonly buffer?
387 if (buf->isReadonly() &&
388 !lyxaction.funcHasFlag(action,
389 LyXAction::ReadOnly)) {
391 setErrorMessage(N_("Document is read-only"));
392 flag |= func_status::Disabled;
396 setErrorMessage(N_("Command not allowed with"
397 "out any document open"));
398 flag |= func_status::Disabled;
403 // I would really like to avoid having this switch and rather try to
404 // encode this in the function itself.
405 bool disable = false;
408 disable = !Exporter::IsExportable(buf, "dvi")
409 || lyxrc.print_command == "none";
412 disable = argument == "fax" &&
413 !Exporter::IsExportable(buf, argument);
416 disable = buf->undostack.empty();
419 disable = buf->redostack.empty();
421 case LFUN_SPELLCHECK:
422 disable = lyxrc.isp_command == "none";
425 disable = lyxrc.chktex_command == "none";
428 disable = !Exporter::IsExportable(buf, "program");
431 case LFUN_INSERTFOOTNOTE:
432 // Disable insertion of floats in a tabular.
434 if (owner->view()->theLockingInset()) {
435 disable = (owner->view()->theLockingInset()->lyxCode() == Inset::TABULAR_CODE) ||
436 owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
440 case LFUN_LAYOUT_TABULAR:
442 if (owner->view()->theLockingInset()) {
443 disable = (owner->view()->theLockingInset()->lyxCode() != Inset::TABULAR_CODE) &&
444 !owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
448 case LFUN_TABULAR_FEATURE:
450 if (owner->view()->theLockingInset()) {
451 func_status::value_type ret = func_status::Disabled;
452 if (owner->view()->theLockingInset()->lyxCode() == Inset::TABULAR_CODE) {
453 ret = static_cast<InsetTabular *>
454 (owner->view()->theLockingInset())->
456 } else if (owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
457 ret = static_cast<InsetTabular *>
458 (owner->view()->theLockingInset()->
459 getFirstLockingInsetOfType(Inset::TABULAR_CODE))->
465 static InsetTabular inset(*owner->buffer(), 1, 1);
466 func_status::value_type ret;
469 ret = inset.getStatus(argument);
470 if ((ret & func_status::ToggleOn) ||
471 (ret & func_status::ToggleOff))
472 flag |= func_status::ToggleOff;
476 case LFUN_VC_REGISTER:
477 disable = buf->lyxvc.inUse();
479 case LFUN_VC_CHECKIN:
480 disable = !buf->lyxvc.inUse() || buf->isReadonly();
482 case LFUN_VC_CHECKOUT:
483 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
487 case LFUN_VC_HISTORY:
488 disable = !buf->lyxvc.inUse();
490 case LFUN_BOOKMARK_GOTO:
491 disable = !owner->view()->
492 isSavedPosition(strToUnsignedInt(argument));
494 case LFUN_MATH_VALIGN: {
495 // I think this test can be simplified (Andre')
496 // mathcursor is != 0 iff we are in math mode
497 //Inset * tli = owner->view()->theLockingInset();
498 //if (tli && (tli->lyxCode() == Inset::MATH_CODE
499 // || tli->lyxCode() == Inset::MATHMACRO_CODE)) {
502 char align = mathcursor->valign();
507 if (argument.empty()) {
508 flag = func_status::OK;
511 if (!contains("tcb", argument[0])) {
515 if (argument[0] == align)
516 flag |= func_status::ToggleOn;
518 flag |= func_status::ToggleOff;
523 case LFUN_MATH_HALIGN: {
524 //Inset * tli = owner->view()->theLockingInset();
525 //if (tli && (tli->lyxCode() == Inset::MATH_CODE
526 // || tli->lyxCode() == Inset::MATHMACRO_CODE)) {
528 char align = mathcursor->halign();
533 if (argument.empty()) {
534 flag = func_status::OK;
537 if (!contains("lcr", argument[0])) {
541 if (argument[0] == align)
542 flag |= func_status::ToggleOn;
544 flag |= func_status::ToggleOff;
549 case LFUN_MATH_MUTATE: {
550 Inset * tli = owner->view()->theLockingInset();
551 if (tli && (tli->lyxCode() == Inset::MATH_CODE)) {
552 MathInsetTypes type = mathcursor->par()->GetType();
553 func_status::value_type box = func_status::ToggleOff;
554 if (argument == "inline") {
555 if (type == LM_OT_SIMPLE)
556 box = func_status::ToggleOn;
557 } else if (argument == "display") {
558 if (type == LM_OT_EQUATION)
559 box = func_status::ToggleOn;
560 } else if (argument == "eqnarray") {
561 if (type == LM_OT_EQNARRAY)
562 box = func_status::ToggleOn;
563 } else if (argument == "align") {
564 if (type == LM_OT_ALIGN)
565 box = func_status::ToggleOn;
567 box = func_status::OK;
576 // we just need to be in math mode to enable that
578 case LFUN_MATH_LIMITS:
579 case LFUN_MATH_NONUMBER:
580 case LFUN_MATH_NUMBER:
581 disable = !mathcursor;
584 // we need to be math mode and a math array for that
585 // Hack: halign produces non-zero result iff we are in a math array
586 case LFUN_MATH_ROW_INSERT:
587 case LFUN_MATH_ROW_DELETE:
588 case LFUN_MATH_COLUMN_INSERT:
589 case LFUN_MATH_COLUMN_DELETE:
590 disable = !mathcursor || !mathcursor->halign();
597 flag |= func_status::Disabled;
600 func_status::value_type box = func_status::ToggleOff;
601 LyXFont const & font =
602 TEXT(false)->real_current_font;
605 if (font.emph() == LyXFont::ON)
606 box = func_status::ToggleOn;
609 if (font.noun() == LyXFont::ON)
610 box = func_status::ToggleOn;
613 if (font.series() == LyXFont::BOLD_SERIES)
614 box = func_status::ToggleOn;
618 if (font.latex() == LyXFont::ON)
619 box = func_status::ToggleOn;
622 case LFUN_READ_ONLY_TOGGLE:
623 if (buf->isReadonly())
624 box = func_status::ToggleOn;
627 box = func_status::OK;
637 // temporary dispatch method
638 void LyXFunc::miniDispatch(string const & s)
646 string const LyXFunc::Dispatch(string const & s)
648 // Split command string into command and argument
650 string line = frontStrip(s);
651 string const arg = strip(frontStrip(split(line, cmd, ' ')));
653 return Dispatch(lyxaction.LookupFunc(cmd), arg);
657 string const LyXFunc::Dispatch(int ac,
658 string const & do_not_use_this_arg)
660 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << ac
661 <<"] arg[" << do_not_use_this_arg << "]" << endl;
666 // we have not done anything wrong yet.
668 dispatch_buffer.erase();
670 // if action is a pseudo-action, we need the real action
671 if (lyxaction.isPseudoAction(ac)) {
673 action = static_cast<kb_action>
674 (lyxaction.retrieveActionArg(ac, tmparg));
678 action = static_cast<kb_action>(ac);
679 if (!do_not_use_this_arg.empty())
680 argument = do_not_use_this_arg; // except here
683 #ifdef NEW_DISPATCHER
684 // We try do call the most specific dispatcher first:
685 // 1. the lockinginset's dispatch
686 // 2. the bufferview's dispatch
687 // 3. the lyxview's dispatch
690 selection_possible = false;
692 if (owner->view()->available())
693 owner->view()->hideCursor();
695 // We cannot use this function here
696 if (getStatus(ac, do_not_use_this_arg) & func_status::Disabled)
697 goto exit_with_message;
700 if (owner->view()->available() && owner->view()->theLockingInset()) {
701 UpdatableInset::RESULT result;
702 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
703 (keyseq.length >= -1)))
705 if ((action==LFUN_UNKNOWN_ACTION) && argument.empty()){
706 argument = keyseq.getiso();
708 // Undo/Redo is a bit tricky for insets.
709 if (action == LFUN_UNDO) {
710 #ifdef RETHINK_THIS_FOR_NOW_WE_LEAVE_ALL_UNLOCKED
713 UpdatableInset * inset =
714 owner->view()->theLockingInset()->getLockingInset();
715 int inset_id = inset->id();
716 inset->getCursorPos(owner->view(), slx, sly);
717 owner->view()->unlockInset(inset);
719 owner->view()->unlockInset(owner->view()->theLockingInset());
721 owner->view()->menuUndo();
722 #ifdef RETHINK_THIS_FOR_NOW_WE_LEAVE_ALL_UNLOCKED
724 if (TEXT()->cursor.par()->
725 isInset(TEXT()->cursor.pos())) {
726 inset = static_cast<UpdatableInset*>(
727 TEXT()->cursor.par()->
734 inset = static_cast<UpdatableInset *>(owner->view()->buffer()->getInsetFromID(inset_id));
737 inset->edit(owner->view(),slx,sly,0);
740 } else if (action == LFUN_REDO) {
743 UpdatableInset * inset = owner->view()->
745 inset->getCursorPos(owner->view(), slx, sly);
746 owner->view()->unlockInset(inset);
747 owner->view()->menuRedo();
748 inset = static_cast<UpdatableInset*>(
749 TEXT()->cursor.par()->
753 inset->edit(owner->view(),slx,sly,0);
755 } else if (((result=owner->view()->theLockingInset()->
756 localDispatch(owner->view(), action,
758 UpdatableInset::DISPATCHED) ||
759 (result == UpdatableInset::DISPATCHED_NOUPDATE))
762 //setMessage(N_("Text mode"));
764 case LFUN_UNKNOWN_ACTION:
765 case LFUN_BREAKPARAGRAPH:
767 TEXT()->cursorRight(owner->view());
768 owner->view()->setState();
772 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
773 TEXT()->cursorRight(owner->view());
774 moveCursorUpdate(true, false);
779 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
780 TEXT()->cursorRight(owner->view());
781 moveCursorUpdate(true, false);
786 if (TEXT()->cursor.row()->next())
787 TEXT()->cursorDown(owner->view());
789 TEXT()->cursorRight(owner->view());
790 moveCursorUpdate(true, false);
800 lyx::Assert(action != LFUN_SELECT_FILE_SYNC);
806 if (!owner->view()->available()) break;
808 // this function should be used always [asierra060396]
809 UpdatableInset * tli =
810 owner->view()->theLockingInset();
812 UpdatableInset * lock = tli->getLockingInset();
815 owner->view()->unlockInset(tli);
816 TEXT()->cursorRight(owner->view());
817 moveCursorUpdate(true, false);
820 tli->unlockInsetInInset(owner->view(),
828 // --- Misc -------------------------------------------
829 case LFUN_WORDFINDFORWARD :
830 case LFUN_WORDFINDBACKWARD : {
831 static string last_search;
832 string searched_string;
834 if (!argument.empty()) {
835 last_search = argument;
836 searched_string = argument;
838 searched_string = last_search;
841 if (!searched_string.empty() &&
842 ((action == LFUN_WORDFINDBACKWARD) ?
843 SearchBackward(owner->view(), searched_string) :
844 SearchForward(owner->view(), searched_string))) {
846 // ??? What is that ???
847 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
850 // clear the selection (if there is any)
851 owner->view()->toggleSelection();
852 TEXT()->clearSelection(owner->view());
854 // Move cursor so that successive C-s 's will not stand in place.
855 if (action == LFUN_WORDFINDFORWARD )
856 TEXT()->cursorRightOneWord(owner->view());
858 moveCursorUpdate(true, false);
861 // set the new selection
862 // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
863 owner->view()->toggleSelection(false);
866 // REMOVED : if (owner->view()->getWorkArea()->focus)
867 owner->view()->showCursor();
873 if (owner->view()->available() && !owner->view()->theLockingInset()) {
874 owner->view()->update(TEXT(),
875 BufferView::SELECT|BufferView::FITCUR);
878 keyseq.print(buf, true);
883 // --- Misc -------------------------------------------
884 case LFUN_EXEC_COMMAND:
886 std::vector<string> allCmds;
887 std::transform(lyxaction.func_begin(), lyxaction.func_end(),
888 std::back_inserter(allCmds), lyx::firster());
889 static std::vector<string> hist;
890 owner->getMiniBuffer()->getString(MiniBuffer::spaces,
895 case LFUN_CANCEL: // RVDK_PATCH_5
898 if (owner->view()->available())
899 // cancel any selection
900 Dispatch(LFUN_MARK_OFF);
901 setMessage(N_("Cancel"));
904 case LFUN_META_FAKE: // RVDK_PATCH_5
906 meta_fake_bit = Mod1Mask;
908 keyseq.print(buf, true);
909 setMessage(buf); // RVDK_PATCH_5
913 case LFUN_READ_ONLY_TOGGLE:
914 if (owner->buffer()->lyxvc.inUse()) {
915 owner->buffer()->lyxvc.toggleReadOnly();
917 owner->buffer()->setReadonly(
918 !owner->buffer()->isReadonly());
922 case LFUN_CENTER: // this is center and redraw.
923 owner->view()->center();
926 // --- Menus -----------------------------------------------
931 case LFUN_MENUNEWTMPLT:
935 case LFUN_CLOSEBUFFER:
940 if (!owner->buffer()->isUnnamed()) {
942 s1 << _("Saving document") << ' '
943 << MakeDisplayPath(owner->buffer()->fileName() + "...");
944 owner->message(s1.str().c_str());
945 MenuWrite(owner->view(), owner->buffer());
947 WriteAs(owner->view(), owner->buffer());
951 WriteAs(owner->view(), owner->buffer(), argument);
954 case LFUN_MENURELOAD:
959 Exporter::Export(owner->buffer(), argument, true);
963 Exporter::Preview(owner->buffer(), argument);
967 Exporter::Export(owner->buffer(), "program", true);
971 MenuRunChktex(owner->buffer());
975 owner->getDialogs()->showPrint();
979 if (argument == "custom")
982 Exporter::Export(owner->buffer(), argument, false);
1000 InsetCommandParams p;
1003 if (action == LFUN_TOCVIEW)
1005 p.setCmdName("tableofcontents");
1007 else if (action == LFUN_LOAVIEW )
1008 p.setCmdName("listof{algorithm}{List of Algorithms}");
1009 else if (action == LFUN_LOFVIEW)
1010 p.setCmdName("listoffigures");
1012 p.setCmdName("listoftables");
1014 owner->getDialogs()->createTOC(p.getAsString());
1018 case LFUN_DIALOG_TABULAR_INSERT:
1019 owner->getDialogs()->showTabularCreate();
1027 AutoSave(owner->view());
1031 owner->view()->menuUndo();
1035 owner->view()->menuRedo();
1038 case LFUN_MENUSEARCH:
1039 owner->getDialogs()->showSearch();
1042 case LFUN_REMOVEERRORS:
1043 if (owner->view()->removeAutoInsets()) {
1044 owner->view()->redraw();
1045 owner->view()->fitCursor(TEXT());
1050 changeDepth(owner->view(), TEXT(false), 0);
1053 case LFUN_DEPTH_MIN:
1054 changeDepth(owner->view(), TEXT(false), -1);
1057 case LFUN_DEPTH_PLUS:
1058 changeDepth(owner->view(), TEXT(false), 1);
1062 owner->getDialogs()->setUserFreeFont();
1068 owner->view()->setState();
1073 case LFUN_RECONFIGURE:
1074 Reconfigure(owner->view());
1078 case LFUN_FLOATSOPERATE:
1079 if (argument == "openfoot")
1080 owner->view()->allFloats(1,0);
1081 else if (argument == "closefoot")
1082 owner->view()->allFloats(0,0);
1083 else if (argument == "openfig")
1084 owner->view()->allFloats(1,1);
1085 else if (argument == "closefig")
1086 owner->view()->allFloats(0,1);
1089 #ifdef WITH_WARNINGS
1090 #warning Find another implementation here (or another lyxfunc)!
1093 case LFUN_HELP_COPYRIGHT:
1094 owner->getDialogs()->showCopyright();
1097 case LFUN_HELP_CREDITS:
1098 owner->getDialogs()->showCredits();
1101 case LFUN_HELP_OPEN:
1103 string const arg = argument;
1105 setErrorMessage(N_("Missing argument"));
1108 owner->prohibitInput();
1109 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1110 if (fname.empty()) {
1111 lyxerr << "LyX: unable to find documentation file `"
1112 << arg << "'. Bad installation?" << endl;
1113 owner->allowInput();
1117 str << _("Opening help file") << ' '
1118 << MakeDisplayPath(fname) << "...";
1119 owner->message(str.str().c_str());
1120 owner->view()->buffer(bufferlist.loadLyXFile(fname, false));
1121 owner->allowInput();
1125 case LFUN_HELP_VERSION: {
1126 owner->prohibitInput();
1127 string msg(_("LyX Version "));
1131 fl_show_message(msg.c_str(),
1132 (_("Library directory: ")
1133 + MakeDisplayPath(system_lyxdir)).c_str(),
1134 (_("User directory: ")
1135 + MakeDisplayPath(user_lyxdir)).c_str());
1136 owner->allowInput();
1140 // --- version control -------------------------------
1141 case LFUN_VC_REGISTER:
1143 if (!owner->buffer()->lyxvc.inUse())
1144 owner->buffer()->lyxvc.registrer();
1148 case LFUN_VC_CHECKIN:
1150 if (owner->buffer()->lyxvc.inUse()
1151 && !owner->buffer()->isReadonly())
1152 owner->buffer()->lyxvc.checkIn();
1156 case LFUN_VC_CHECKOUT:
1158 if (owner->buffer()->lyxvc.inUse()
1159 && owner->buffer()->isReadonly())
1160 owner->buffer()->lyxvc.checkOut();
1164 case LFUN_VC_REVERT:
1166 owner->buffer()->lyxvc.revert();
1172 owner->buffer()->lyxvc.undoLast();
1176 case LFUN_VC_HISTORY:
1178 owner->getDialogs()->showVCLogFile();
1182 // --- buffers ----------------------------------------
1184 case LFUN_SWITCHBUFFER:
1185 owner->view()->buffer(bufferlist.getBuffer(argument));
1190 // servercmd: argument must be <file>:<template>
1191 Buffer * tmpbuf = NewLyxFile(argument);
1193 owner->view()->buffer(tmpbuf);
1197 case LFUN_FILE_OPEN:
1201 case LFUN_LATEX_LOG:
1202 owner->getDialogs()->showLogFile();
1207 lyxerr[Debug::INFO] << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1208 int sel = strToInt(argument);
1209 lyxerr[Debug::INFO] << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1211 // Should this give a setMessage instead?
1213 return string(); // illegal argument
1215 --sel; // sel 1..., but layout 0...
1217 // Pretend we got the name instead.
1218 Dispatch(int(LFUN_LAYOUT),
1219 textclasslist.NameOfLayout(owner->view()
1220 ->buffer()->params.textclass,
1225 case LFUN_LAYOUT_DOCUMENT:
1226 owner->getDialogs()->showDocument();
1229 case LFUN_LAYOUT_PARAGRAPH:
1230 owner->getDialogs()->showParagraph();
1233 case LFUN_LAYOUT_CHARACTER:
1234 owner->getDialogs()->showCharacter();
1237 case LFUN_LAYOUT_TABULAR:
1238 if (owner->view()->theLockingInset()) {
1239 if (owner->view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1240 InsetTabular * inset = static_cast<InsetTabular *>
1241 (owner->view()->theLockingInset());
1242 inset->openLayoutDialog(owner->view());
1243 } else if (owner->view()->theLockingInset()->
1244 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1245 InsetTabular * inset = static_cast<InsetTabular *>(
1246 owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1247 inset->openLayoutDialog(owner->view());
1252 case LFUN_LAYOUT_PREAMBLE:
1253 owner->getDialogs()->showPreamble();
1256 case LFUN_LAYOUT_SAVE_DEFAULT:
1257 MenuLayoutSave(owner->view());
1260 case LFUN_DROP_LAYOUTS_CHOICE:
1261 owner->getToolbar()->openLayoutList();
1264 case LFUN_MENU_OPEN_BY_NAME:
1265 owner->getMenubar()->openByName(argument);
1266 break; // RVDK_PATCH_5
1268 case LFUN_SPELLCHECK:
1269 if (lyxrc.isp_command != "none")
1270 owner->getDialogs()->showSpellchecker();
1273 // --- lyxserver commands ----------------------------
1277 setMessage(owner->buffer()->fileName());
1278 lyxerr[Debug::INFO] << "FNAME["
1279 << owner->buffer()->fileName()
1287 dispatch_buffer = buf;
1288 lyxserver->notifyClient(dispatch_buffer);
1292 case LFUN_GOTOFILEROW:
1295 char file_name[100];
1297 ::sscanf(argument.c_str(), " %s %d", file_name, &row);
1301 istringstream istr(argument.c_str());
1302 istr >> file_name >> row;
1304 // Must replace extension of the file to be .lyx and get full path
1305 string const s(ChangeExtension(file_name, ".lyx"));
1307 // Either change buffer or load the file
1308 if (bufferlist.exists(s)) {
1309 owner->view()->buffer(bufferlist.getBuffer(s));
1311 owner->view()->buffer(bufferlist.loadLyXFile(s));
1315 owner->view()->setCursorFromRow(row);
1318 owner->view()->center();
1322 case LFUN_GOTO_PARAGRAPH:
1324 istringstream istr(argument.c_str());
1328 Paragraph * par = owner->buffer()->getParFromID(id);
1330 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1331 << id << "]" << std::endl;
1336 TEXT()->setCursor(owner->view(), par, 0);
1337 owner->view()->setState();
1341 owner->view()->center();
1348 int const qa = lyxaction.LookupFunc(argument);
1349 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1353 // --- toolbar ----------------------------------
1354 case LFUN_PUSH_TOOLBAR:
1356 int nth = strToInt(argument);
1358 setErrorMessage(N_("Push-toolbar needs argument > 0"));
1360 owner->getToolbar()->push(nth);
1365 case LFUN_ADD_TO_TOOLBAR:
1367 if (lyxerr.debugging(Debug::GUI)) {
1368 lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1369 "argument = `" << argument << '\'' << endl;
1371 string tmp(argument);
1372 //lyxerr <<string("Argument: ") + argument);
1373 //lyxerr <<string("Tmp : ") + tmp);
1375 setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1377 owner->getToolbar()->add(argument, false);
1378 owner->getToolbar()->set();
1383 // --- insert characters ----------------------------------------
1385 // --- Mathed stuff. If we are here, there is no locked inset yet.
1390 if (!greek_kb_flag) {
1392 setMessage(N_("Math greek mode on"));
1399 case LFUN_GREEK_TOGGLE:
1401 greek_kb_flag = greek_kb_flag ? 0 : 2;
1402 if (greek_kb_flag) {
1403 setMessage(N_("Math greek keyboard on"));
1405 setMessage(N_("Math greek keyboard off"));
1410 case LFUN_MATH_EXTERN:
1411 case LFUN_MATH_NUMBER:
1412 case LFUN_MATH_NONUMBER:
1413 case LFUN_MATH_LIMITS:
1415 setErrorMessage(N_("This is only allowed in math mode!"));
1419 case LFUN_MATH_PANEL:
1420 owner->getDialogs()->showMathPanel();
1423 case LFUN_CITATION_CREATE:
1425 InsetCommandParams p( "cite" );
1427 if (!argument.empty()) {
1428 // This should be set at source, ie when typing
1429 // "citation-insert foo" in the minibuffer.
1430 // Question: would pybibliographer also need to be
1431 // changed. Suspect so. Leave as-is therefore.
1432 if (contains(argument, "|")) {
1433 p.setContents( token(argument, '|', 0) );
1434 p.setOptions( token(argument, '|', 1) );
1436 p.setContents( argument );
1438 Dispatch(LFUN_CITATION_INSERT, p.getAsString());
1440 owner->getDialogs()->createCitation( p.getAsString() );
1444 case LFUN_CHILDOPEN:
1446 string const filename =
1447 MakeAbsPath(argument,
1448 OnlyPath(owner->buffer()->fileName()));
1449 setMessage(N_("Opening child document ") +
1450 MakeDisplayPath(filename) + "...");
1451 owner->view()->savePosition(0);
1452 if (bufferlist.exists(filename))
1453 owner->view()->buffer(bufferlist.getBuffer(filename));
1455 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1459 case LFUN_TOGGLECURSORFOLLOW:
1460 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1463 case LFUN_KMAP_OFF: // keymap off
1464 owner->getIntl()->KeyMapOn(false);
1467 case LFUN_KMAP_PRIM: // primary keymap
1468 owner->getIntl()->KeyMapPrim();
1471 case LFUN_KMAP_SEC: // secondary keymap
1472 owner->getIntl()->KeyMapSec();
1475 case LFUN_KMAP_TOGGLE: // toggle keymap
1476 owner->getIntl()->ToggleKeyMap();
1481 // argument contains ';'-terminated commands
1482 while (argument.find(';') != string::npos) {
1484 argument = split(argument, first, ';');
1490 case LFUN_DIALOG_PREFERENCES:
1491 owner->getDialogs()->showPreferences();
1494 case LFUN_SAVEPREFERENCES:
1496 Path p(user_lyxdir);
1497 lyxrc.write("preferences");
1501 case LFUN_SCREEN_FONT_UPDATE:
1503 // handle the screen font changes.
1505 lyxrc.set_font_norm_type();
1506 fontloader.update();
1507 // Of course we should only do the resize and the textcache.clear
1508 // if values really changed...but not very important right now. (Lgb)
1509 // All visible buffers will need resize
1511 // We also need to empty the textcache so that
1512 // the buffer will be formatted correctly after
1518 case LFUN_SET_COLOR:
1521 string const x11_name = split(argument, lyx_name, ' ');
1522 if (lyx_name.empty() || x11_name.empty()) {
1523 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1528 if (!lcolor.setColor(lyx_name, x11_name)) {
1529 static string const err1 (N_("Set-color \""));
1530 static string const err2 (
1531 N_("\" failed - color is undefined "
1532 "or may not be redefined"));
1533 setErrorMessage(_(err1) + lyx_name + _(err2));
1536 lyxColorHandler->updateColor(lcolor.getFromLyXName(lyx_name));
1537 owner->view()->redraw();
1542 owner->message(argument);
1545 case LFUN_MESSAGE_PUSH:
1546 owner->messagePush(argument);
1549 case LFUN_MESSAGE_POP:
1550 owner->messagePop();
1554 // Then if it was none of the above
1555 if (!owner->view()->Dispatch(action, argument))
1556 lyxerr << "A truly unknown func ["
1557 << lyxaction.getActionName(action) << "]!"
1564 commandshortcut.erase();
1566 if (lyxrc.display_shortcuts && show_sc) {
1567 if (action != LFUN_SELFINSERT) {
1568 // Put name of command and list of shortcuts
1569 // for it in minibuffer
1570 string comname = lyxaction.getActionName(action);
1572 int pseudoaction = action;
1573 bool argsadded = false;
1575 if (!argument.empty()) {
1576 // If we have the command with argument,
1579 lyxaction.searchActionArg(action,
1582 if (pseudoaction == -1) {
1583 pseudoaction = action;
1585 comname += " " + argument;
1590 string const shortcuts =
1591 toplevel_keymap->findbinding(pseudoaction);
1593 if (!shortcuts.empty()) {
1594 comname += ": " + shortcuts;
1595 } else if (!argsadded) {
1596 comname += " " + argument;
1599 if (!comname.empty()) {
1600 comname = strip(comname);
1601 commandshortcut = "(" + comname + ')';
1603 // Here we could even add a small pause,
1604 // to annoy the user and make him learn
1606 // No! That will just annoy, not teach
1607 // anything. The user will read the messages
1608 // if they are interested. (Asger)
1613 string const res = getMessage();
1616 if (!commandshortcut.empty()) {
1617 owner->getMiniBuffer()->addSet(commandshortcut);
1620 string const msg(_(res) + ' ' + commandshortcut);
1621 owner->message(msg);
1628 void LyXFunc::setupLocalKeymap()
1630 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1631 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1635 void LyXFunc::MenuNew(bool fromTemplate)
1637 string initpath = lyxrc.document_path;
1639 if (owner->view()->available()) {
1640 string const trypath = owner->buffer()->filepath;
1641 // If directory is writeable, use this as default.
1642 if (IsDirWriteable(trypath) == 1)
1646 static int newfile_number;
1649 if (lyxrc.new_ask_filename) {
1650 FileDialog fileDlg(owner, _("Enter filename for new document"),
1651 LFUN_SELECT_FILE_SYNC,
1652 make_pair(string(_("Documents")),
1653 string(lyxrc.document_path)),
1654 make_pair(string(_("Templates")),
1655 string(lyxrc.template_path)));
1657 FileDialog::Result result =
1658 fileDlg.Select(initpath,
1659 _("*.lyx|LyX Documents (*.lyx)"),
1662 if (result.second.empty()) {
1663 owner->message(_("Canceled."));
1664 lyxerr[Debug::INFO] << "New Document Cancelled." << endl;
1668 // get absolute path of file and make sure the filename ends
1670 s = MakeAbsPath(result.second);
1671 if (!IsLyXFilename(s))
1674 // Check if the document already is open
1675 if (bufferlist.exists(s)) {
1676 switch (AskConfirmation(_("Document is already open:"),
1677 MakeDisplayPath(s, 50),
1678 _("Do you want to close that document now?\n"
1679 "('No' will just switch to the open version)")))
1681 case 1: // Yes: close the document
1682 if (!bufferlist.close(bufferlist.getBuffer(s)))
1683 // If close is canceled, we cancel here too.
1686 case 2: // No: switch to the open document
1687 owner->view()->buffer(bufferlist.getBuffer(s));
1689 case 3: // Cancel: Do nothing
1690 owner->message(_("Canceled."));
1694 // Check whether the file already exists
1696 if (fi.readable() &&
1697 AskQuestion(_("File already exists:"),
1698 MakeDisplayPath(s, 50),
1699 _("Do you want to open the document?"))) {
1701 string const disp_fn(MakeDisplayPath(s));
1704 str << _("Opening document") << ' '
1705 << disp_fn << "...";
1707 owner->message(str.str().c_str());
1708 //XFlush(fl_get_display());
1709 owner->view()->buffer(bufferlist.loadLyXFile(s));
1711 str2 << _("Document") << ' '
1712 << disp_fn << ' ' << _("opened.");
1714 owner->message(str2.str().c_str());
1719 s = AddName(lyxrc.document_path,
1720 "newfile" + tostr(++newfile_number) + ".lyx");
1722 while (bufferlist.exists(s) || fi.readable()) {
1724 s = AddName(lyxrc.document_path,
1725 "newfile" + tostr(newfile_number) +
1731 // The template stuff
1734 FileDialog fileDlg(owner, _("Select template file"),
1735 LFUN_SELECT_FILE_SYNC,
1736 make_pair(string(_("Documents")),
1737 string(lyxrc.document_path)),
1738 make_pair(string(_("Templates")),
1739 string(lyxrc.template_path)));
1741 FileDialog::Result result =
1742 fileDlg.Select(lyxrc.template_path,
1743 _("*.lyx|LyX Documents (*.lyx)"));
1745 if (result.first == FileDialog::Later)
1748 string const fname = result.second;
1755 // find a free buffer
1756 lyxerr[Debug::INFO] << "Find a free buffer." << endl;
1757 owner->view()->buffer(bufferlist.newFile(s, templname));
1761 void LyXFunc::Open(string const & fname)
1763 string initpath = lyxrc.document_path;
1765 if (owner->view()->available()) {
1766 string const trypath = owner->buffer()->filepath;
1767 // If directory is writeable, use this as default.
1768 if (IsDirWriteable(trypath) == 1)
1774 if (fname.empty()) {
1775 FileDialog fileDlg(owner, _("Select document to open"),
1777 make_pair(string(_("Documents")),
1778 string(lyxrc.document_path)),
1779 make_pair(string(_("Examples")),
1780 string(AddPath(system_lyxdir, "examples"))));
1782 FileDialog::Result result =
1783 fileDlg.Select(initpath,
1784 "*.lyx|LyX Documents (*.lyx)");
1786 if (result.first == FileDialog::Later)
1789 filename = result.second;
1791 // check selected filename
1792 if (filename.empty()) {
1793 owner->message(_("Canceled."));
1799 // get absolute path of file and add ".lyx" to the filename if
1801 filename = FileSearch(string(), filename, "lyx");
1804 string const disp_fn(MakeDisplayPath(filename));
1807 str << _("Opening document") << ' ' << disp_fn << "...";
1809 owner->message(str.str().c_str());
1811 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1813 owner->view()->buffer(openbuf);
1815 str << _("Document") << ' ' << disp_fn << ' ' << _("opened.");
1816 owner->message(str.str().c_str());
1819 str << _("Could not open docuent") << ' ' << disp_fn;
1820 owner->message(str.str().c_str());
1825 // checks for running without gui are missing.
1826 void LyXFunc::doImport(string const & argument)
1829 string filename = split(argument, format, ' ');
1830 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1831 << " file: " << filename << endl;
1833 if (filename.empty()) { // need user interaction
1834 string initpath = lyxrc.document_path;
1836 if (owner->view()->available()) {
1837 string const trypath = owner->buffer()->filepath;
1838 // If directory is writeable, use this as default.
1839 if (IsDirWriteable(trypath) == 1)
1843 string const text = _("Select ") + formats.PrettyName(format)
1844 + _(" file to import");
1846 FileDialog fileDlg(owner, text,
1848 make_pair(string(_("Documents")),
1849 string(lyxrc.document_path)),
1850 make_pair(string(_("Examples")),
1851 string(AddPath(system_lyxdir, "examples"))));
1853 string const extension = "*." + formats.Extension(format)
1854 + "| " + formats.PrettyName(format)
1855 + " (*." + formats.Extension(format) + ")";
1857 FileDialog::Result result = fileDlg.Select(initpath,
1860 if (result.first == FileDialog::Later)
1863 filename = result.second;
1865 // check selected filename
1866 if (filename.empty())
1867 owner->message(_("Canceled."));
1870 // still no filename? abort
1871 if (filename.empty())
1874 // get absolute path of file
1875 filename = MakeAbsPath(filename);
1877 string const lyxfile = ChangeExtension(filename, ".lyx");
1879 // Check if the document already is open
1880 if (bufferlist.exists(lyxfile)) {
1881 switch (AskConfirmation(_("Document is already open:"),
1882 MakeDisplayPath(lyxfile, 50),
1883 _("Do you want to close that document now?\n"
1884 "('No' will just switch to the open version)")))
1886 case 1: // Yes: close the document
1887 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1888 // If close is canceled, we cancel here too.
1891 case 2: // No: switch to the open document
1892 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1894 case 3: // Cancel: Do nothing
1895 owner->message(_("Canceled."));
1900 // Check if a LyX document by the same root exists in filesystem
1901 FileInfo const f(lyxfile, true);
1902 if (f.exist() && !AskQuestion(_("A document by the name"),
1903 MakeDisplayPath(lyxfile),
1904 _("already exists. Overwrite?"))) {
1905 owner->message(_("Canceled"));
1908 // filename should be valid now
1910 Importer::Import(owner, filename, format);
1914 void LyXFunc::reloadBuffer()
1916 string const fn = owner->buffer()->fileName();
1917 if (bufferlist.close(owner->buffer()))
1918 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1922 void LyXFunc::CloseBuffer()
1924 if (bufferlist.close(owner->buffer()) && !quitting) {
1925 if (bufferlist.empty()) {
1926 // need this otherwise SEGV may occur while trying to
1927 // set variables that don't exist
1928 // since there's no current buffer
1929 owner->getDialogs()->hideBufferDependent();
1931 owner->view()->buffer(bufferlist.first());
1937 // Each "owner" should have it's own message method. lyxview and
1938 // the minibuffer would use the minibuffer, but lyxserver would
1939 // send an ERROR signal to its client. Alejandro 970603
1940 // This func is bit problematic when it comes to NLS, to make the
1941 // lyx servers client be language indepenent we must not translate
1942 // strings sent to this func.
1943 void LyXFunc::setErrorMessage(string const & m) const
1945 dispatch_buffer = m;
1950 void LyXFunc::setMessage(string const & m)
1952 dispatch_buffer = m;
1956 void LyXFunc::initMiniBuffer()
1958 string text = _("Welcome to LyX!");
1960 // When meta-fake key is pressed, show the key sequence so far + "M-".
1966 // Else, when a non-complete key sequence is pressed,
1967 // show the available options.
1968 else if (keyseqUncomplete())
1969 text = keyseqOptions();
1971 // Else, show the buffer state.
1972 else if (owner->view()->available()) {
1973 Buffer * tmpbuf = owner->buffer();
1975 string const nicename =
1976 MakeDisplayPath(tmpbuf->fileName());
1977 // Should we do this instead? (kindo like emacs)
1978 // leaves more room for other information
1981 if (tmpbuf->lyxvc.inUse()) {
1983 text += tmpbuf->lyxvc.version();
1985 text += tmpbuf->lyxvc.locker();
1986 if (tmpbuf->isReadonly())
1989 } else if (tmpbuf->isReadonly())
1991 if (!tmpbuf->isLyxClean())
1992 text += _(" (Changed)");
1994 if (text != _("Welcome to LyX!")) // this is a hack
1995 text = _("* No document open *");
1998 owner->message(text);