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"
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 "mathed/math_cursor.h"
63 #include "mathed/math_inset.h"
64 #include "spellchecker.h"
65 #include "minibuffer.h"
68 #include "lyx_gui_misc.h"
69 #include "support/filetools.h"
70 #include "support/FileInfo.h"
71 #include "support/syscall.h"
72 #include "support/lstrings.h"
73 #include "support/path.h"
74 #include "support/lyxfunctional.h"
79 #include "trans_mgr.h"
82 #include "bufferview_funcs.h"
83 #include "frontends/FileDialog.h"
84 #include "frontends/Dialogs.h"
85 #include "frontends/Toolbar.h"
86 #include "frontends/Menubar.h"
87 #include "FloatList.h"
88 #include "converter.h"
91 #include "FontLoader.h"
92 #include "TextCache.h"
100 extern BufferList bufferlist;
101 extern LyXServer * lyxserver;
102 extern int greek_kb_flag;
103 extern bool selection_possible;
104 extern void MenuSendto();
106 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
108 extern void show_symbols_form(LyXFunc *);
110 extern LyXAction lyxaction;
112 extern tex_accent_struct get_accent(kb_action action);
114 extern LyXTextClass::size_type current_layout;
116 extern void ShowLatexLog();
122 class MiniBufferController : public SigC::Object {
125 MiniBufferController() {
129 &MiniBufferController::receiveCommand));
133 &MiniBufferController::receiveArg));
136 void receiveCmd(string const & cmd) {}
138 void receiveArg(string const & arg) {}
146 MiniBufferController mb_ctrl;
152 /* === globals =========================================================== */
154 // Initialization of static member var
155 bool LyXFunc::show_sc = true;
158 LyXFunc::LyXFunc(LyXView * o)
162 lyx_dead_action = LFUN_NOACTION;
163 lyx_calling_dead_action = LFUN_NOACTION;
169 LyXText * LyXFunc::TEXT(bool flag = true) const
172 return owner->view()->text;
173 return owner->view()->getLyXText();
177 // I changed this func slightly. I commented out the ...FinishUndo(),
178 // this means that all places that used to have a moveCursorUpdate, now
179 // have a ...FinishUndo() as the preceeding statement. I have also added
180 // a moveCursorUpdate to some of the functions that updated the cursor, but
181 // that did not show its new position.
183 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
185 if (selecting || TEXT(flag)->selection.mark()) {
186 TEXT(flag)->setSelection(owner->view());
187 if (TEXT(flag)->bv_owner)
188 owner->view()->toggleToggle();
190 owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
191 owner->view()->showCursor();
193 /* ---> Everytime the cursor is moved, show the current font state. */
194 // should this too me moved out of this func?
195 owner->view()->setState();
199 void LyXFunc::handleKeyFunc(kb_action action)
201 char c = keyseq.getiso();
203 if (keyseq.length != -1) c = 0;
205 owner->getIntl()->getTrans()
206 .deadkey(c, get_accent(action).accent, TEXT(false));
207 // Need to reset, in case the minibuffer calls these
211 // copied verbatim from do_accent_char
212 owner->view()->update(TEXT(false),
213 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
214 TEXT(false)->selection.cursor = TEXT(false)->cursor;
218 void LyXFunc::processKeySym(KeySym keysym, unsigned int state)
222 if (lyxerr.debugging(Debug::KEY)) {
223 char const * tmp = XKeysymToString(keysym);
224 string const stm = (tmp ? tmp : "");
225 lyxerr << "KeySym is "
228 << keysym << "] State is ["
232 // Do nothing if we have nothing (JMarc)
233 if (keysym == NoSymbol) {
234 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
239 // Can we be sure that this will work for all X-Windows
240 // implementations? (Lgb)
241 // This code snippet makes lyx ignore some keys. Perhaps
242 // all of them should be explictly mentioned?
243 if ((keysym >= XK_Shift_L && keysym <= XK_Hyper_R)
244 || keysym == XK_Mode_switch || keysym == 0x0) {
248 // Do a one-deep top-level lookup for
249 // cancel and meta-fake keys. RVDK_PATCH_5
250 cancel_meta_seq.reset();
252 int action = cancel_meta_seq.addkey(keysym, state
253 &(ShiftMask|ControlMask
255 if (lyxerr.debugging(Debug::KEY)) {
256 lyxerr << "action first set to [" << action << "]" << endl;
259 // When not cancel or meta-fake, do the normal lookup.
260 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
261 // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
262 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
263 if (lyxerr.debugging(Debug::KEY)) {
264 lyxerr << "meta_fake_bit is ["
265 << meta_fake_bit << "]" << endl;
267 // remove Caps Lock and Mod2 as a modifiers
268 action = keyseq.addkey(keysym,
269 (state | meta_fake_bit)
270 &(ShiftMask|ControlMask
272 if (lyxerr.debugging(Debug::KEY)) {
273 lyxerr << "action now set to ["
274 << action << "]" << endl;
277 // Dont remove this unless you know what you are doing.
280 if (action == 0) action = LFUN_PREFIX;
282 if (lyxerr.debugging(Debug::KEY)) {
285 lyxerr << "Key [action="
291 // already here we know if it any point in going further
292 // why not return already here if action == -1 and
293 // num_bytes == 0? (Lgb)
295 if (keyseq.length > 1 || keyseq.length < -1) {
302 // It is unknown, but what if we remove all
303 // the modifiers? (Lgb)
304 action = keyseq.addkey(keysym, 0);
306 if (lyxerr.debugging(Debug::KEY)) {
307 lyxerr << "Removing modifiers...\n"
308 << "Action now set to ["
309 << action << "]" << endl;
312 owner->message(_("Unknown function."));
317 if (action == LFUN_SELFINSERT) {
318 // This is very X dependant.
319 unsigned int c = keysym;
321 switch (c & 0x0000FF00) {
322 // latin 1 byte 3 = 0
323 case 0x00000000: break;
324 // latin 2 byte 3 = 1
326 // latin 3 byte 3 = 2
328 // latin 4 byte 3 = 3
330 // latin 8 byte 3 = 18 (0x12)
332 // latin 9 byte 3 = 19 (0x13)
341 argument = static_cast<char>(c);
342 lyxerr[Debug::KEY] << "SelfInsert arg[`"
343 << argument << "']" << endl;
346 bool tmp_sc = show_sc;
348 Dispatch(action, argument);
355 func_status::value_type LyXFunc::getStatus(int ac) const
357 return getStatus(ac, string());
361 func_status::value_type LyXFunc::getStatus(int ac,
362 string const & not_to_use_arg) const
365 func_status::value_type flag = func_status::OK;
367 Buffer * buf = owner->buffer();
369 if (lyxaction.isPseudoAction(ac))
370 action = lyxaction.retrieveActionArg(ac, argument);
372 action = static_cast<kb_action>(ac);
373 if (!not_to_use_arg.empty())
374 argument = not_to_use_arg; // exept here
377 if (action == LFUN_UNKNOWN_ACTION) {
378 setErrorMessage(N_("Unknown action"));
379 return func_status::Unknown;
382 // Check whether we need a buffer
383 if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
384 // Yes we need a buffer, do we have one?
387 // Can we use a readonly buffer?
388 if (buf->isReadonly() &&
389 !lyxaction.funcHasFlag(action,
390 LyXAction::ReadOnly)) {
392 setErrorMessage(N_("Document is read-only"));
393 flag |= func_status::Disabled;
397 setErrorMessage(N_("Command not allowed with"
398 "out any document open"));
399 flag |= func_status::Disabled;
404 // I would really like to avoid having this switch and rather try to
405 // encode this in the function itself.
406 bool disable = false;
409 disable = !Exporter::IsExportable(buf, "dvi")
410 || lyxrc.print_command == "none";
413 disable = argument == "fax" &&
414 !Exporter::IsExportable(buf, argument);
417 disable = buf->undostack.empty();
420 disable = buf->redostack.empty();
422 case LFUN_SPELLCHECK:
423 disable = lyxrc.isp_command == "none";
426 disable = lyxrc.chktex_command == "none";
429 disable = !Exporter::IsExportable(buf, "program");
432 case LFUN_INSERTFOOTNOTE:
433 // Disable insertion of floats in a tabular.
435 if (owner->view()->theLockingInset()) {
436 disable = (owner->view()->theLockingInset()->lyxCode() == Inset::TABULAR_CODE) ||
437 owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
441 case LFUN_LAYOUT_TABULAR:
443 if (owner->view()->theLockingInset()) {
444 disable = (owner->view()->theLockingInset()->lyxCode() != Inset::TABULAR_CODE) &&
445 !owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
449 case LFUN_TABULAR_FEATURE:
451 if (owner->view()->theLockingInset()) {
452 func_status::value_type ret = func_status::Disabled;
453 if (owner->view()->theLockingInset()->lyxCode() == Inset::TABULAR_CODE) {
454 ret = static_cast<InsetTabular *>
455 (owner->view()->theLockingInset())->
457 } else if (owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
458 ret = static_cast<InsetTabular *>
459 (owner->view()->theLockingInset()->
460 getFirstLockingInsetOfType(Inset::TABULAR_CODE))->
466 static InsetTabular inset(*owner->buffer(), 1, 1);
467 func_status::value_type ret;
470 ret = inset.getStatus(argument);
471 if ((ret & func_status::ToggleOn) ||
472 (ret & func_status::ToggleOff))
473 flag |= func_status::ToggleOff;
477 case LFUN_VC_REGISTER:
478 disable = buf->lyxvc.inUse();
480 case LFUN_VC_CHECKIN:
481 disable = !buf->lyxvc.inUse() || buf->isReadonly();
483 case LFUN_VC_CHECKOUT:
484 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
488 case LFUN_VC_HISTORY:
489 disable = !buf->lyxvc.inUse();
491 case LFUN_BOOKMARK_GOTO:
492 disable = !owner->view()->
493 isSavedPosition(strToUnsignedInt(argument));
494 case LFUN_MATH_VALIGN: {
495 Inset * tli = owner->view()->theLockingInset();
496 if (tli && (tli->lyxCode() == Inset::MATH_CODE
497 || tli->lyxCode() == Inset::MATHMACRO_CODE)) {
498 char align = mathcursor->valign();
503 if (argument.empty()) {
504 flag = func_status::OK;
507 if (!contains("tcb", argument[0])) {
511 if (argument[0] == align)
512 flag |= func_status::ToggleOn;
514 flag |= func_status::ToggleOff;
519 case LFUN_MATH_HALIGN: {
520 Inset * tli = owner->view()->theLockingInset();
521 if (tli && (tli->lyxCode() == Inset::MATH_CODE
522 || tli->lyxCode() == Inset::MATHMACRO_CODE)) {
523 char align = mathcursor->halign();
528 if (argument.empty()) {
529 flag = func_status::OK;
532 if (!contains("lcr", argument[0])) {
536 if (argument[0] == align)
537 flag |= func_status::ToggleOn;
539 flag |= func_status::ToggleOff;
544 case LFUN_MATH_MUTATE: {
545 Inset * tli = owner->view()->theLockingInset();
546 if (tli && (tli->lyxCode() == Inset::MATH_CODE)) {
547 MathInsetTypes type = mathcursor->par()->GetType();
548 func_status::value_type box = func_status::ToggleOff;
549 if (argument == "inline") {
550 if (type == LM_OT_SIMPLE)
551 box = func_status::ToggleOn;
552 } else if (argument == "display") {
553 if (type == LM_OT_EQUATION)
554 box = func_status::ToggleOn;
555 } else if (argument == "eqnarray") {
556 if (type == LM_OT_EQNARRAY)
557 box = func_status::ToggleOn;
558 } else if (argument == "align") {
559 if (type == LM_OT_ALIGN)
560 box = func_status::ToggleOn;
562 box = func_status::OK;
574 flag |= func_status::Disabled;
577 func_status::value_type box = func_status::ToggleOff;
578 LyXFont const & font =
579 TEXT(false)->real_current_font;
582 if (font.emph() == LyXFont::ON)
583 box = func_status::ToggleOn;
586 if (font.noun() == LyXFont::ON)
587 box = func_status::ToggleOn;
590 if (font.series() == LyXFont::BOLD_SERIES)
591 box = func_status::ToggleOn;
595 if (font.latex() == LyXFont::ON)
596 box = func_status::ToggleOn;
599 case LFUN_READ_ONLY_TOGGLE:
600 if (buf->isReadonly())
601 box = func_status::ToggleOn;
604 box = func_status::OK;
614 // temporary dispatch method
615 void LyXFunc::miniDispatch(string const & s)
623 string const LyXFunc::Dispatch(string const & s)
625 // Split command string into command and argument
627 string line = frontStrip(s);
628 string const arg = strip(frontStrip(split(line, cmd, ' ')));
630 return Dispatch(lyxaction.LookupFunc(cmd), arg);
634 string const LyXFunc::Dispatch(int ac,
635 string const & do_not_use_this_arg)
637 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << ac
638 <<"] arg[" << do_not_use_this_arg << "]" << endl;
643 // we have not done anything wrong yet.
645 dispatch_buffer.erase();
647 // if action is a pseudo-action, we need the real action
648 if (lyxaction.isPseudoAction(ac)) {
650 action = static_cast<kb_action>
651 (lyxaction.retrieveActionArg(ac, tmparg));
655 action = static_cast<kb_action>(ac);
656 if (!do_not_use_this_arg.empty())
657 argument = do_not_use_this_arg; // except here
660 selection_possible = false;
662 if (owner->view()->available())
663 owner->view()->hideCursor();
665 // We cannot use this function here
666 if (getStatus(ac, do_not_use_this_arg) & func_status::Disabled)
667 goto exit_with_message;
670 if (owner->view()->available() && owner->view()->theLockingInset()) {
671 UpdatableInset::RESULT result;
672 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
673 (keyseq.length >= -1)))
675 if ((action==LFUN_UNKNOWN_ACTION) && argument.empty()){
676 argument = keyseq.getiso();
678 // Undo/Redo is a bit tricky for insets.
679 if (action == LFUN_UNDO) {
682 UpdatableInset * inset =
683 owner->view()->theLockingInset();
684 inset->getCursorPos(owner->view(), slx, sly);
685 owner->view()->unlockInset(inset);
686 owner->view()->menuUndo();
687 if (TEXT()->cursor.par()->
688 isInset(TEXT()->cursor.pos())) {
689 inset = static_cast<UpdatableInset*>(
690 TEXT()->cursor.par()->
697 inset->edit(owner->view(),slx,sly,0);
699 } else if (action == LFUN_REDO) {
702 UpdatableInset * inset = owner->view()->
704 inset->getCursorPos(owner->view(), slx, sly);
705 owner->view()->unlockInset(inset);
706 owner->view()->menuRedo();
707 inset = static_cast<UpdatableInset*>(
708 TEXT()->cursor.par()->
712 inset->edit(owner->view(),slx,sly,0);
714 } else if (((result=owner->view()->theLockingInset()->
715 localDispatch(owner->view(), action,
717 UpdatableInset::DISPATCHED) ||
718 (result == UpdatableInset::DISPATCHED_NOUPDATE))
721 //setMessage(N_("Text mode"));
723 case LFUN_UNKNOWN_ACTION:
724 case LFUN_BREAKPARAGRAPH:
726 TEXT()->cursorRight(owner->view());
727 owner->view()->setState();
731 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
732 TEXT()->cursorRight(owner->view());
733 moveCursorUpdate(true, false);
738 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
739 TEXT()->cursorRight(owner->view());
740 moveCursorUpdate(true, false);
745 if (TEXT()->cursor.row()->next())
746 TEXT()->cursorDown(owner->view());
748 TEXT()->cursorRight(owner->view());
749 moveCursorUpdate(true, false);
759 lyx::Assert(action != LFUN_SELECT_FILE_SYNC);
765 if (!owner->view()->available()) break;
767 // this function should be used always [asierra060396]
768 UpdatableInset * tli =
769 owner->view()->theLockingInset();
771 UpdatableInset * lock = tli->getLockingInset();
774 owner->view()->unlockInset(tli);
775 TEXT()->cursorRight(owner->view());
776 moveCursorUpdate(true, false);
779 tli->unlockInsetInInset(owner->view(),
787 // --- Misc -------------------------------------------
788 case LFUN_WORDFINDFORWARD :
789 case LFUN_WORDFINDBACKWARD : {
790 static string last_search;
791 string searched_string;
793 if (!argument.empty()) {
794 last_search = argument;
795 searched_string = argument;
797 searched_string = last_search;
800 if (!searched_string.empty() &&
801 ((action == LFUN_WORDFINDBACKWARD) ?
802 SearchBackward(owner->view(), searched_string) :
803 SearchForward(owner->view(), searched_string))) {
805 // ??? What is that ???
806 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
809 // clear the selection (if there is any)
810 owner->view()->toggleSelection();
811 TEXT()->clearSelection(owner->view());
813 // Move cursor so that successive C-s 's will not stand in place.
814 if (action == LFUN_WORDFINDFORWARD )
815 TEXT()->cursorRightOneWord(owner->view());
816 TEXT()->finishUndo();
817 moveCursorUpdate(true, false);
820 // set the new selection
821 // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
822 owner->view()->toggleSelection(false);
825 // REMOVED : if (owner->view()->getWorkArea()->focus)
826 owner->view()->showCursor();
832 if (owner->view()->available() && !owner->view()->theLockingInset()) {
833 owner->view()->update(TEXT(),
834 BufferView::SELECT|BufferView::FITCUR);
837 keyseq.print(buf, true);
842 // --- Misc -------------------------------------------
843 case LFUN_EXEC_COMMAND:
845 std::vector<string> allCmds;
846 std::transform(lyxaction.func_begin(), lyxaction.func_end(),
847 std::back_inserter(allCmds), lyx::firster());
848 static std::vector<string> hist;
849 owner->getMiniBuffer()->getString(MiniBuffer::spaces,
854 case LFUN_CANCEL: // RVDK_PATCH_5
857 if (owner->view()->available())
858 // cancel any selection
859 Dispatch(LFUN_MARK_OFF);
860 setMessage(N_("Cancel"));
863 case LFUN_META_FAKE: // RVDK_PATCH_5
865 meta_fake_bit = Mod1Mask;
867 keyseq.print(buf, true);
868 setMessage(buf); // RVDK_PATCH_5
872 case LFUN_READ_ONLY_TOGGLE:
873 if (owner->buffer()->lyxvc.inUse()) {
874 owner->buffer()->lyxvc.toggleReadOnly();
876 owner->buffer()->setReadonly(
877 !owner->buffer()->isReadonly());
881 case LFUN_CENTER: // this is center and redraw.
882 owner->view()->center();
885 // --- Menus -----------------------------------------------
890 case LFUN_MENUNEWTMPLT:
894 case LFUN_CLOSEBUFFER:
899 if (!owner->buffer()->isUnnamed()) {
901 s1 << _("Saving document") << ' '
902 << MakeDisplayPath(owner->buffer()->fileName() + "...");
903 owner->message(s1.str().c_str());
904 MenuWrite(owner->view(), owner->buffer());
906 WriteAs(owner->view(), owner->buffer());
910 WriteAs(owner->view(), owner->buffer(), argument);
913 case LFUN_MENURELOAD:
918 Exporter::Export(owner->buffer(), argument, true);
922 Exporter::Preview(owner->buffer(), argument);
926 Exporter::Export(owner->buffer(), "program", true);
930 MenuRunChktex(owner->buffer());
934 owner->getDialogs()->showPrint();
938 if (argument == "custom")
941 Exporter::Export(owner->buffer(), argument, false);
959 InsetCommandParams p;
962 if (action == LFUN_TOCVIEW)
964 p.setCmdName("tableofcontents");
966 else if (action == LFUN_LOAVIEW )
967 p.setCmdName("listof{algorithm}{List of Algorithms}");
968 else if (action == LFUN_LOFVIEW)
969 p.setCmdName("listoffigures");
971 p.setCmdName("listoftables");
973 owner->getDialogs()->createTOC(p.getAsString());
977 case LFUN_DIALOG_TABULAR_INSERT:
978 owner->getDialogs()->showTabularCreate();
986 AutoSave(owner->view());
990 owner->view()->menuUndo();
994 owner->view()->menuRedo();
997 case LFUN_MENUSEARCH:
998 owner->getDialogs()->showSearch();
1001 case LFUN_REMOVEERRORS:
1002 if (owner->view()->removeAutoInsets()) {
1003 owner->view()->redraw();
1004 owner->view()->fitCursor(TEXT());
1009 changeDepth(owner->view(), TEXT(false), 0);
1012 case LFUN_DEPTH_MIN:
1013 changeDepth(owner->view(), TEXT(false), -1);
1016 case LFUN_DEPTH_PLUS:
1017 changeDepth(owner->view(), TEXT(false), 1);
1021 owner->getDialogs()->setUserFreeFont();
1027 owner->view()->setState();
1032 case LFUN_RECONFIGURE:
1033 Reconfigure(owner->view());
1037 case LFUN_FLOATSOPERATE:
1038 if (argument == "openfoot")
1039 owner->view()->allFloats(1,0);
1040 else if (argument == "closefoot")
1041 owner->view()->allFloats(0,0);
1042 else if (argument == "openfig")
1043 owner->view()->allFloats(1,1);
1044 else if (argument == "closefig")
1045 owner->view()->allFloats(0,1);
1048 #ifdef WITH_WARNINGS
1049 #warning Find another implementation here (or another lyxfunc)!
1052 case LFUN_HELP_COPYRIGHT:
1053 owner->getDialogs()->showCopyright();
1056 case LFUN_HELP_CREDITS:
1057 owner->getDialogs()->showCredits();
1060 case LFUN_HELP_OPEN:
1062 string const arg = argument;
1064 setErrorMessage(N_("Missing argument"));
1067 ProhibitInput(owner->view());
1068 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1069 if (fname.empty()) {
1070 lyxerr << "LyX: unable to find documentation file `"
1071 << arg << "'. Bad installation?" << endl;
1072 AllowInput(owner->view());
1076 str << _("Opening help file") << ' '
1077 << MakeDisplayPath(fname) << "...";
1078 owner->message(str.str().c_str());
1079 owner->view()->buffer(bufferlist.loadLyXFile(fname,false));
1080 AllowInput(owner->view());
1084 case LFUN_HELP_VERSION: {
1085 ProhibitInput(owner->view());
1086 string msg(_("LyX Version "));
1090 fl_show_message(msg.c_str(),
1091 (_("Library directory: ")
1092 + MakeDisplayPath(system_lyxdir)).c_str(),
1093 (_("User directory: ")
1094 + MakeDisplayPath(user_lyxdir)).c_str());
1095 AllowInput(owner->view());
1099 // --- version control -------------------------------
1100 case LFUN_VC_REGISTER:
1102 if (!owner->buffer()->lyxvc.inUse())
1103 owner->buffer()->lyxvc.registrer();
1107 case LFUN_VC_CHECKIN:
1109 if (owner->buffer()->lyxvc.inUse()
1110 && !owner->buffer()->isReadonly())
1111 owner->buffer()->lyxvc.checkIn();
1115 case LFUN_VC_CHECKOUT:
1117 if (owner->buffer()->lyxvc.inUse()
1118 && owner->buffer()->isReadonly())
1119 owner->buffer()->lyxvc.checkOut();
1123 case LFUN_VC_REVERT:
1125 owner->buffer()->lyxvc.revert();
1131 owner->buffer()->lyxvc.undoLast();
1135 case LFUN_VC_HISTORY:
1137 owner->getDialogs()->showVCLogFile();
1141 // --- buffers ----------------------------------------
1143 case LFUN_SWITCHBUFFER:
1144 owner->view()->buffer(bufferlist.getBuffer(argument));
1149 // servercmd: argument must be <file>:<template>
1150 Buffer * tmpbuf = NewLyxFile(argument);
1152 owner->view()->buffer(tmpbuf);
1156 case LFUN_FILE_OPEN:
1160 case LFUN_LATEX_LOG:
1161 owner->getDialogs()->showLogFile();
1166 lyxerr[Debug::INFO] << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1167 int sel = strToInt(argument);
1168 lyxerr[Debug::INFO] << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1170 // Should this give a setMessage instead?
1172 return string(); // illegal argument
1174 --sel; // sel 1..., but layout 0...
1176 // Pretend we got the name instead.
1177 Dispatch(int(LFUN_LAYOUT),
1178 textclasslist.NameOfLayout(owner->view()
1179 ->buffer()->params.textclass,
1184 case LFUN_LAYOUT_DOCUMENT:
1185 owner->getDialogs()->showDocument();
1188 case LFUN_LAYOUT_PARAGRAPH:
1189 owner->getDialogs()->showParagraph();
1192 case LFUN_LAYOUT_CHARACTER:
1193 owner->getDialogs()->showCharacter();
1196 case LFUN_LAYOUT_TABULAR:
1197 if (owner->view()->theLockingInset()) {
1198 if (owner->view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1199 InsetTabular * inset = static_cast<InsetTabular *>
1200 (owner->view()->theLockingInset());
1201 inset->openLayoutDialog(owner->view());
1202 } else if (owner->view()->theLockingInset()->
1203 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1204 InsetTabular * inset = static_cast<InsetTabular *>(
1205 owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1206 inset->openLayoutDialog(owner->view());
1211 case LFUN_LAYOUT_PREAMBLE:
1212 owner->getDialogs()->showPreamble();
1215 case LFUN_LAYOUT_SAVE_DEFAULT:
1216 MenuLayoutSave(owner->view());
1219 case LFUN_DROP_LAYOUTS_CHOICE:
1220 owner->getToolbar()->openLayoutList();
1223 case LFUN_MENU_OPEN_BY_NAME:
1224 owner->getMenubar()->openByName(argument);
1225 break; // RVDK_PATCH_5
1227 case LFUN_SPELLCHECK:
1228 if (lyxrc.isp_command != "none")
1229 ShowSpellChecker(owner->view());
1230 break; // RVDK_PATCH_5
1232 // --- lyxserver commands ----------------------------
1236 setMessage(owner->buffer()->fileName());
1237 lyxerr[Debug::INFO] << "FNAME["
1238 << owner->buffer()->fileName()
1246 dispatch_buffer = buf;
1247 lyxserver->notifyClient(dispatch_buffer);
1251 case LFUN_GOTOFILEROW:
1253 char file_name[100];
1255 ::sscanf(argument.c_str(), " %s %d", file_name, &row);
1257 // Must replace extension of the file to be .lyx and get full path
1258 string s = ChangeExtension(string(file_name), ".lyx");
1260 // Either change buffer or load the file
1261 if (bufferlist.exists(s))
1262 owner->view()->buffer(bufferlist.getBuffer(s));
1264 owner->view()->buffer(bufferlist.loadLyXFile(s));
1267 owner->view()->setCursorFromRow(row);
1270 owner->view()->center();
1274 case LFUN_GOTO_PARAGRAPH:
1276 istringstream istr(argument.c_str());
1280 Paragraph * par = TEXT()->getParFromID(id);
1285 TEXT()->setCursor(owner->view(), par, 0);
1286 owner->view()->setState();
1290 owner->view()->center();
1297 int const qa = lyxaction.LookupFunc(argument);
1298 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1302 // --- toolbar ----------------------------------
1303 case LFUN_PUSH_TOOLBAR:
1305 int nth = strToInt(argument);
1307 setErrorMessage(N_("Push-toolbar needs argument > 0"));
1309 owner->getToolbar()->push(nth);
1314 case LFUN_ADD_TO_TOOLBAR:
1316 if (lyxerr.debugging(Debug::GUI)) {
1317 lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1318 "argument = `" << argument << '\'' << endl;
1320 string tmp(argument);
1321 //lyxerr <<string("Argument: ") + argument);
1322 //lyxerr <<string("Tmp : ") + tmp);
1324 setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1326 owner->getToolbar()->add(argument, false);
1327 owner->getToolbar()->set();
1332 // --- insert characters ----------------------------------------
1334 // --- Mathed stuff. If we are here, there is no locked inset yet.
1339 if (!greek_kb_flag) {
1341 setMessage(N_("Math greek mode on"));
1348 case LFUN_GREEK_TOGGLE:
1350 greek_kb_flag = greek_kb_flag ? 0 : 2;
1351 if (greek_kb_flag) {
1352 setMessage(N_("Math greek keyboard on"));
1354 setMessage(N_("Math greek keyboard off"));
1359 case LFUN_MATH_EXTERN:
1360 case LFUN_MATH_NUMBER:
1361 case LFUN_MATH_NONUMBER:
1362 case LFUN_MATH_LIMITS:
1364 setErrorMessage(N_("This is only allowed in math mode!"));
1368 case LFUN_MATH_PANEL:
1369 owner->getDialogs()->showMathPanel();
1372 case LFUN_CITATION_CREATE:
1374 InsetCommandParams p( "cite" );
1376 if (!argument.empty()) {
1377 // This should be set at source, ie when typing
1378 // "citation-insert foo" in the minibuffer.
1379 // Question: would pybibliographer also need to be
1380 // changed. Suspect so. Leave as-is therefore.
1381 if (contains(argument, "|")) {
1382 p.setContents( token(argument, '|', 0) );
1383 p.setOptions( token(argument, '|', 1) );
1385 p.setContents( argument );
1387 Dispatch(LFUN_CITATION_INSERT, p.getAsString());
1389 owner->getDialogs()->createCitation( p.getAsString() );
1393 case LFUN_CHILDOPEN:
1395 string const filename =
1396 MakeAbsPath(argument,
1397 OnlyPath(owner->buffer()->fileName()));
1398 setMessage(N_("Opening child document ") +
1399 MakeDisplayPath(filename) + "...");
1400 owner->view()->savePosition(0);
1401 if (bufferlist.exists(filename))
1402 owner->view()->buffer(bufferlist.getBuffer(filename));
1404 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1408 case LFUN_TOGGLECURSORFOLLOW:
1409 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1412 case LFUN_KMAP_OFF: // keymap off
1413 owner->getIntl()->KeyMapOn(false);
1416 case LFUN_KMAP_PRIM: // primary keymap
1417 owner->getIntl()->KeyMapPrim();
1420 case LFUN_KMAP_SEC: // secondary keymap
1421 owner->getIntl()->KeyMapSec();
1424 case LFUN_KMAP_TOGGLE: // toggle keymap
1425 owner->getIntl()->ToggleKeyMap();
1430 // argument contains ';'-terminated commands
1431 while (argument.find(';') != string::npos) {
1433 argument = split(argument, first, ';');
1439 case LFUN_DIALOG_PREFERENCES:
1440 owner->getDialogs()->showPreferences();
1443 case LFUN_SAVEPREFERENCES:
1445 Path p(user_lyxdir);
1446 lyxrc.write("preferences");
1450 case LFUN_SCREEN_FONT_UPDATE:
1452 // handle the screen font changes.
1454 lyxrc.set_font_norm_type();
1455 fontloader.update();
1456 // Of course we should only do the resize and the textcache.clear
1457 // if values really changed...but not very important right now. (Lgb)
1458 // All visible buffers will need resize
1460 // We also need to empty the textcache so that
1461 // the buffer will be formatted correctly after
1467 case LFUN_SET_COLOR:
1470 string const x11_name = split(argument, lyx_name, ' ');
1471 if (lyx_name.empty() || x11_name.empty()) {
1472 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1477 if (!lcolor.setColor(lyx_name, x11_name)) {
1478 static string const err1 (N_("Set-color \""));
1479 static string const err2 (
1480 N_("\" failed - color is undefined "
1481 "or may not be redefined"));
1482 setErrorMessage(_(err1) + lyx_name + _(err2));
1485 lyxColorHandler->updateColor(lcolor.getFromLyXName(lyx_name));
1486 owner->view()->redraw();
1491 owner->message(argument);
1494 case LFUN_MESSAGE_PUSH:
1495 owner->messagePush(argument);
1498 case LFUN_MESSAGE_POP:
1499 owner->messagePop();
1503 // Then if it was none of the above
1504 if (!owner->view()->Dispatch(action, argument))
1505 lyxerr << "A truly unknown func ["
1506 << action << "]!" << endl;
1512 commandshortcut.erase();
1514 if (lyxrc.display_shortcuts && show_sc) {
1515 if (action != LFUN_SELFINSERT) {
1516 // Put name of command and list of shortcuts
1517 // for it in minibuffer
1518 string comname = lyxaction.getActionName(action);
1520 int pseudoaction = action;
1521 bool argsadded = false;
1523 if (!argument.empty()) {
1524 // If we have the command with argument,
1527 lyxaction.searchActionArg(action,
1530 if (pseudoaction == -1) {
1531 pseudoaction = action;
1533 comname += " " + argument;
1538 string const shortcuts =
1539 toplevel_keymap->findbinding(pseudoaction);
1541 if (!shortcuts.empty()) {
1542 comname += ": " + shortcuts;
1543 } else if (!argsadded) {
1544 comname += " " + argument;
1547 if (!comname.empty()) {
1548 comname = strip(comname);
1549 commandshortcut = "(" + comname + ')';
1551 // Here we could even add a small pause,
1552 // to annoy the user and make him learn
1554 // No! That will just annoy, not teach
1555 // anything. The user will read the messages
1556 // if they are interested. (Asger)
1561 string const res = getMessage();
1564 if (!commandshortcut.empty()) {
1565 owner->getMiniBuffer()->addSet(commandshortcut);
1568 string const msg(_(res) + ' ' + commandshortcut);
1569 owner->message(msg);
1576 void LyXFunc::setupLocalKeymap()
1578 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1579 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1583 void LyXFunc::MenuNew(bool fromTemplate)
1585 string initpath = lyxrc.document_path;
1587 if (owner->view()->available()) {
1588 string const trypath = owner->buffer()->filepath;
1589 // If directory is writeable, use this as default.
1590 if (IsDirWriteable(trypath) == 1)
1594 static int newfile_number;
1597 if (lyxrc.new_ask_filename) {
1598 FileDialog fileDlg(owner, _("Enter filename for new document"),
1599 LFUN_SELECT_FILE_SYNC,
1600 make_pair(string(_("Documents")),
1601 string(lyxrc.document_path)),
1602 make_pair(string(_("Templates")),
1603 string(lyxrc.template_path)));
1605 FileDialog::Result result =
1606 fileDlg.Select(initpath,
1607 _("*.lyx|LyX Documents (*.lyx)"),
1610 if (result.second.empty()) {
1611 owner->message(_("Canceled."));
1612 lyxerr[Debug::INFO] << "New Document Cancelled." << endl;
1616 // get absolute path of file and make sure the filename ends
1618 s = MakeAbsPath(result.second);
1619 if (!IsLyXFilename(s))
1622 // Check if the document already is open
1623 if (bufferlist.exists(s)) {
1624 switch (AskConfirmation(_("Document is already open:"),
1625 MakeDisplayPath(s, 50),
1626 _("Do you want to close that document now?\n"
1627 "('No' will just switch to the open version)")))
1629 case 1: // Yes: close the document
1630 if (!bufferlist.close(bufferlist.getBuffer(s)))
1631 // If close is canceled, we cancel here too.
1634 case 2: // No: switch to the open document
1635 owner->view()->buffer(bufferlist.getBuffer(s));
1637 case 3: // Cancel: Do nothing
1638 owner->message(_("Canceled."));
1642 // Check whether the file already exists
1644 if (fi.readable() &&
1645 AskQuestion(_("File already exists:"),
1646 MakeDisplayPath(s, 50),
1647 _("Do you want to open the document?"))) {
1649 string const disp_fn(MakeDisplayPath(s));
1652 str << _("Opening document") << ' '
1653 << disp_fn << "...";
1655 owner->message(str.str().c_str());
1656 //XFlush(fl_get_display());
1657 owner->view()->buffer(bufferlist.loadLyXFile(s));
1659 str2 << _("Document") << ' '
1660 << disp_fn << ' ' << _("opened.");
1662 owner->message(str2.str().c_str());
1667 s = AddName(lyxrc.document_path,
1668 "newfile" + tostr(++newfile_number) + ".lyx");
1670 while (bufferlist.exists(s) || fi.readable()) {
1672 s = AddName(lyxrc.document_path,
1673 "newfile" + tostr(newfile_number) +
1679 // The template stuff
1682 FileDialog fileDlg(owner, _("Select template file"),
1683 LFUN_SELECT_FILE_SYNC,
1684 make_pair(string(_("Documents")),
1685 string(lyxrc.document_path)),
1686 make_pair(string(_("Templates")),
1687 string(lyxrc.template_path)));
1689 FileDialog::Result result =
1690 fileDlg.Select(lyxrc.template_path,
1691 _("*.lyx|LyX Documents (*.lyx)"));
1693 if (result.first == FileDialog::Later)
1696 string const fname = result.second;
1703 // find a free buffer
1704 lyxerr[Debug::INFO] << "Find a free buffer." << endl;
1705 owner->view()->buffer(bufferlist.newFile(s, templname));
1709 void LyXFunc::Open(string const & fname)
1711 string initpath = lyxrc.document_path;
1713 if (owner->view()->available()) {
1714 string const trypath = owner->buffer()->filepath;
1715 // If directory is writeable, use this as default.
1716 if (IsDirWriteable(trypath) == 1)
1722 if (fname.empty()) {
1723 FileDialog fileDlg(owner, _("Select document to open"),
1725 make_pair(string(_("Documents")),
1726 string(lyxrc.document_path)),
1727 make_pair(string(_("Examples")),
1728 string(AddPath(system_lyxdir, "examples"))));
1730 FileDialog::Result result =
1731 fileDlg.Select(initpath,
1732 "*.lyx|LyX Documents (*.lyx)");
1734 if (result.first == FileDialog::Later)
1737 filename = result.second;
1739 // check selected filename
1740 if (filename.empty()) {
1741 owner->message(_("Canceled."));
1747 // get absolute path of file and add ".lyx" to the filename if
1749 filename = FileSearch(string(), filename, "lyx");
1752 string const disp_fn(MakeDisplayPath(filename));
1755 str << _("Opening document") << ' ' << disp_fn << "...";
1757 owner->message(str.str().c_str());
1759 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1761 owner->view()->buffer(openbuf);
1763 str << _("Document") << ' ' << disp_fn << ' ' << _("opened.");
1764 owner->message(str.str().c_str());
1767 str << _("Could not open docuent") << ' ' << disp_fn;
1768 owner->message(str.str().c_str());
1773 // checks for running without gui are missing.
1774 void LyXFunc::doImport(string const & argument)
1777 string filename = split(argument, format, ' ');
1778 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1779 << " file: " << filename << endl;
1781 if (filename.empty()) { // need user interaction
1782 string initpath = lyxrc.document_path;
1784 if (owner->view()->available()) {
1785 string const trypath = owner->buffer()->filepath;
1786 // If directory is writeable, use this as default.
1787 if (IsDirWriteable(trypath) == 1)
1791 string const text = _("Select ") + formats.PrettyName(format)
1792 + _(" file to import");
1794 FileDialog fileDlg(owner, text,
1796 make_pair(string(_("Documents")),
1797 string(lyxrc.document_path)),
1798 make_pair(string(_("Examples")),
1799 string(AddPath(system_lyxdir, "examples"))));
1801 string const extension = "*." + formats.Extension(format)
1802 + "| " + formats.PrettyName(format)
1803 + " (*." + formats.Extension(format) + ")";
1805 FileDialog::Result result = fileDlg.Select(initpath,
1808 if (result.first == FileDialog::Later)
1811 filename = result.second;
1813 // check selected filename
1814 if (filename.empty())
1815 owner->message(_("Canceled."));
1818 // still no filename? abort
1819 if (filename.empty())
1822 // get absolute path of file
1823 filename = MakeAbsPath(filename);
1825 string const lyxfile = ChangeExtension(filename, ".lyx");
1827 // Check if the document already is open
1828 if (bufferlist.exists(lyxfile)) {
1829 switch (AskConfirmation(_("Document is already open:"),
1830 MakeDisplayPath(lyxfile, 50),
1831 _("Do you want to close that document now?\n"
1832 "('No' will just switch to the open version)")))
1834 case 1: // Yes: close the document
1835 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1836 // If close is canceled, we cancel here too.
1839 case 2: // No: switch to the open document
1840 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1842 case 3: // Cancel: Do nothing
1843 owner->message(_("Canceled."));
1848 // Check if a LyX document by the same root exists in filesystem
1849 FileInfo const f(lyxfile, true);
1850 if (f.exist() && !AskQuestion(_("A document by the name"),
1851 MakeDisplayPath(lyxfile),
1852 _("already exists. Overwrite?"))) {
1853 owner->message(_("Canceled"));
1856 // filename should be valid now
1858 Importer::Import(owner, filename, format);
1862 void LyXFunc::reloadBuffer()
1864 string const fn = owner->buffer()->fileName();
1865 if (bufferlist.close(owner->buffer()))
1866 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1870 void LyXFunc::CloseBuffer()
1872 if (bufferlist.close(owner->buffer()) && !quitting) {
1873 if (bufferlist.empty()) {
1874 // need this otherwise SEGV may occur while trying to
1875 // set variables that don't exist
1876 // since there's no current buffer
1877 owner->getDialogs()->hideBufferDependent();
1879 owner->view()->buffer(bufferlist.first());
1885 // Each "owner" should have it's own message method. lyxview and
1886 // the minibuffer would use the minibuffer, but lyxserver would
1887 // send an ERROR signal to its client. Alejandro 970603
1888 // This func is bit problematic when it comes to NLS, to make the
1889 // lyx servers client be language indepenent we must not translate
1890 // strings sent to this func.
1891 void LyXFunc::setErrorMessage(string const & m) const
1893 dispatch_buffer = m;
1898 void LyXFunc::setMessage(string const & m)
1900 dispatch_buffer = m;
1904 void LyXFunc::initMiniBuffer()
1906 string text = _("Welcome to LyX!");
1908 // When meta-fake key is pressed, show the key sequence so far + "M-".
1914 // Else, when a non-complete key sequence is pressed,
1915 // show the available options.
1916 else if (keyseqUncomplete())
1917 text = keyseqOptions();
1919 // Else, show the buffer state.
1920 else if (owner->view()->available()) {
1921 Buffer * tmpbuf = owner->buffer();
1923 string const nicename =
1924 MakeDisplayPath(tmpbuf->fileName());
1925 // Should we do this instead? (kindo like emacs)
1926 // leaves more room for other information
1929 if (tmpbuf->lyxvc.inUse()) {
1931 text += tmpbuf->lyxvc.version();
1933 text += tmpbuf->lyxvc.locker();
1934 if (tmpbuf->isReadonly())
1937 } else if (tmpbuf->isReadonly())
1939 if (!tmpbuf->isLyxClean())
1940 text += _(" (Changed)");
1942 if (text != _("Welcome to LyX!")) // this is a hack
1943 text = _("* No document open *");
1946 owner->message(text);