]> git.lyx.org Git - lyx.git/blob - src/lyxfunc.C
renaming, fix citation (still no search though)
[lyx.git] / src / lyxfunc.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "Lsstream.h"
14
15 #include <time.h>
16 #include <locale.h>
17 #include <utility> 
18 #include <algorithm> 
19
20 #include <cstdlib>
21 #include <cctype>
22 #include <cstring>
23
24 #ifdef __GNUG__
25 #pragma implementation
26 #endif
27
28 #include "version.h"
29 #include "kbmap.h"
30 #include "lyxfunc.h"
31 #include "bufferlist.h"
32 #include "ColorHandler.h"
33 #include "lyxserver.h"
34 #include "figure_form.h"
35 #include "intl.h"
36 #include "lyx_main.h"
37 #include "lyx_cb.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 "spellchecker.h" // RVDK_PATCH_5
62 #include "minibuffer.h"
63 #include "vspace.h"
64 #include "LyXView.h"
65 #include "lyx_gui_misc.h"
66 #include "support/filetools.h"
67 #include "support/FileInfo.h"
68 #include "support/syscall.h"
69 #include "support/lstrings.h"
70 #include "support/path.h"
71 #include "support/lyxfunctional.h"
72 #include "debug.h"
73 #include "lyxrc.h"
74 #include "lyxtext.h"
75 #include "gettext.h"
76 #include "trans_mgr.h"
77 #include "layout.h"
78 #include "WorkArea.h"
79 #include "bufferview_funcs.h"
80 #include "frontends/FileDialog.h"
81 #include "frontends/Dialogs.h"
82 #include "frontends/Toolbar.h"
83 #include "frontends/Menubar.h"
84 #include "FloatList.h"
85 #include "converter.h"
86 #include "exporter.h"
87 #include "importer.h"
88 #include "FontLoader.h"
89 #include "TextCache.h"
90 #include "lyxfind.h"
91
92 using std::pair;
93 using std::make_pair; 
94 using std::endl;
95 using std::find_if;
96
97 extern BufferList bufferlist;
98 extern LyXServer * lyxserver;
99 extern int greek_kb_flag;
100 extern bool selection_possible;
101 extern void MenuSendto();
102
103 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
104
105 extern void show_symbols_form(LyXFunc *);
106
107 extern LyXAction lyxaction;
108 // (alkis)
109 extern tex_accent_struct get_accent(kb_action action);
110
111 extern LyXTextClass::size_type current_layout;
112
113 extern void ShowLatexLog();
114
115 /* === globals =========================================================== */
116
117 bool LyXFunc::show_sc = true;
118
119 LyXFunc::LyXFunc(LyXView * o)
120         : owner(o)
121 {
122         meta_fake_bit = 0;
123         lyx_dead_action = LFUN_NOACTION;
124         lyx_calling_dead_action = LFUN_NOACTION;
125         setupLocalKeymap();
126 }
127
128 inline
129 LyXText * LyXFunc::TEXT(bool flag=true) const
130 {
131         if (flag)
132                 return owner->view()->text;
133         return owner->view()->getLyXText();
134 }
135
136 // I changed this func slightly. I commented out the ...FinishUndo(),
137 // this means that all places that used to have a moveCursorUpdate, now
138 // have a ...FinishUndo() as the preceeding statement. I have also added
139 // a moveCursorUpdate to some of the functions that updated the cursor, but
140 // that did not show its new position.
141 inline
142 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
143 {
144         if (selecting || TEXT(flag)->mark_set) {
145                 TEXT(flag)->SetSelection(owner->view());
146                 if (TEXT(flag)->bv_owner)
147                     owner->view()->toggleToggle();
148         }
149         owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
150         owner->view()->showCursor();
151         
152         /* ---> Everytime the cursor is moved, show the current font state. */
153         // should this too me moved out of this func?
154         //owner->showState();
155         owner->view()->setState();
156 }
157
158
159 void LyXFunc::handleKeyFunc(kb_action action)
160 {
161         char c = 0;
162
163         if (keyseq.length == -1 && keyseq.getiso() != 0) 
164                 c = keyseq.getiso();
165         owner->getIntl()->getTrans()
166                 .deadkey(c, get_accent(action).accent, TEXT(false));
167         // Need to reset, in case the minibuffer calls these
168         // actions
169         keyseq.reset();
170         keyseq.length = 0;
171         // copied verbatim from do_accent_char
172         owner->view()->update(TEXT(false),
173                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
174         TEXT(false)->sel_cursor = TEXT(false)->cursor;
175 }
176
177
178 int LyXFunc::processKeySym(KeySym keysym, unsigned int state) 
179 {
180         string argument;
181         
182         if (lyxerr.debugging(Debug::KEY)) {
183                 char * tmp = XKeysymToString(keysym);
184                 string const stm = (tmp ? tmp : "");
185                 lyxerr << "KeySym is "
186                        << stm
187                        << "["
188                        << keysym << "] State is ["
189                        << state << "]"
190                        << endl;
191         }
192         // Do nothing if we have nothing (JMarc)
193         if (keysym == NoSymbol) {
194                 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
195                                    << endl;
196                 //return 0;
197                 return FL_PREEMPT;
198         }
199
200         if (owner->view()->available()) {
201                 // this function should be used always [asierra060396]
202                 UpdatableInset * tli = owner->view()->theLockingInset();
203                 if (tli && (keysym == XK_Escape)) {
204                         if (tli == tli->GetLockingInset()) {
205                                 owner->view()->unlockInset(tli);
206                                 TEXT()->CursorRight(owner->view());
207                                 moveCursorUpdate(true, false);
208                                 owner->showState();
209                         } else {
210                                 tli->UnlockInsetInInset(owner->view(),
211                                                         tli->GetLockingInset(),true);
212                         }
213                         //return 0;
214                         return FL_PREEMPT;
215                 }
216         }
217         
218         // Can we be sure that this will work for all X-Windows
219         // implementations? (Lgb)
220         // This code snippet makes lyx ignore some keys. Perhaps
221         // all of them should be explictly mentioned?
222         if ((keysym >= XK_Shift_L && keysym <= XK_Hyper_R)
223            || keysym == XK_Mode_switch || keysym == 0x0)
224                 return 0;
225
226         // Do a one-deep top-level lookup for
227         // cancel and meta-fake keys. RVDK_PATCH_5
228         cancel_meta_seq.reset();
229
230         int action = cancel_meta_seq.addkey(keysym, state
231                                             &(ShiftMask|ControlMask
232                                               |Mod1Mask)); 
233         if (lyxerr.debugging(Debug::KEY)) {
234                 lyxerr << "action first set to [" << action << "]" << endl;
235         }
236         
237         // When not cancel or meta-fake, do the normal lookup. 
238         // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
239         // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
240         if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
241                 if (lyxerr.debugging(Debug::KEY)) {
242                         lyxerr << "meta_fake_bit is [" << meta_fake_bit << "]" << endl;
243                 }
244                 // remove Caps Lock and Mod2 as a modifiers
245                 action = keyseq.addkey(keysym,
246                                        (state | meta_fake_bit)
247                                        &(ShiftMask|ControlMask
248                                          |Mod1Mask));
249                 if (lyxerr.debugging(Debug::KEY)) {
250                         lyxerr << "action now set to [" << action << "]" << endl;
251                 }
252         }
253         // Dont remove this unless you know what you are doing.
254         meta_fake_bit = 0;
255                 
256         if (action == 0) action = LFUN_PREFIX;
257
258         if (lyxerr.debugging(Debug::KEY)) {
259                 string buf;
260                 keyseq.print(buf);
261                 lyxerr << "Key ["
262                        << action << "]["
263                        << buf << "]"
264                        << endl;
265         }
266
267         // already here we know if it any point in going further
268         // why not return already here if action == -1 and
269         // num_bytes == 0? (Lgb)
270
271         if (keyseq.length > 1 || keyseq.length < -1) {
272                 string buf;
273                 keyseq.print(buf);
274                 owner->getMiniBuffer()->Set(buf);
275         }
276
277         if (action == -1) {
278                 if (keyseq.length < -1) { // unknown key sequence...
279                         string buf;
280                         keyseq.print(buf);
281                         owner->getMiniBuffer()->Set(_("Unknown sequence:"), buf);
282                         return 0;
283                 }
284         
285                 char const isochar = keyseq.getiso();
286                 if (!(state & ControlMask) &&
287                     !(state & Mod1Mask) &&
288                     (isochar && keysym < 0xF000)) {
289                         argument += isochar;
290                 }
291                 if (argument.empty()) {
292                         lyxerr.debug() << "Empty argument!" << endl;
293                         // This can`t possibly be of any use
294                         // so we`ll skip the dispatch.
295                         return 0;
296                 }
297         } else if (action == LFUN_SELFINSERT) {
298                 // We must set the argument to the char looked up by
299                 // XKeysymToString
300                 XKeyEvent xke;
301                 xke.type = KeyPress;
302                 xke.serial = 0;
303                 xke.send_event = False;
304                 xke.display = fl_get_display();
305                 xke.window = 0;
306                 xke.root = 0;
307                 xke.subwindow = 0;
308                 xke.time = 0;
309                 xke.x = 0;
310                 xke.y = 0;
311                 xke.x_root = 0;
312                 xke.y_root = 0;
313                 xke.state = state;
314                 xke.keycode = XKeysymToKeycode(fl_get_display(), keysym);
315                 xke.same_screen = True;
316                 char ret[10];
317                 KeySym tmpkeysym;
318                 int res = XLookupString(&xke, ret, 10, &tmpkeysym, 0);
319                 //Assert(keysym == tmpkeysym);
320                 lyxerr[Debug::KEY] << "TmpKeysym ["
321                                    << tmpkeysym << "]" << endl;
322                 
323                 if (res > 0)
324                         argument = string(ret, res);
325                 lyxerr[Debug::KEY] << "SelfInsert arg["
326                                    << argument << "]" << endl;
327         }
328         
329
330         bool tmp_sc = show_sc;
331         show_sc = false;
332         Dispatch(action, argument);
333         show_sc = tmp_sc;
334         
335         return 0;
336
337
338 LyXFunc::func_status LyXFunc::getStatus(int ac) const
339 {
340         return getStatus(ac, string());
341 }
342
343 LyXFunc::func_status LyXFunc::getStatus(int ac, string const & not_to_use_arg) const
344 {
345         kb_action action;
346         func_status flag = LyXFunc::OK;
347         string argument;
348         Buffer * buf = owner->buffer();
349         
350         if (lyxaction.isPseudoAction(ac)) 
351                 action = lyxaction.retrieveActionArg(ac, argument);
352         else {
353                 action = static_cast<kb_action>(ac);
354                 if (!not_to_use_arg.empty())
355                         argument = not_to_use_arg; // exept here
356         }
357         
358         if (action == LFUN_UNKNOWN_ACTION) {
359                 setErrorMessage(N_("Unknown action"));
360                 return LyXFunc::Unknown;
361         }
362         
363         // Check whether we need a buffer
364         if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
365                 // Yes we need a buffer, do we have one?
366                 if (buf) {
367                         // yes
368                         // Can we use a readonly buffer?
369                         if (buf->isReadonly() && 
370                             !lyxaction.funcHasFlag(action,
371                                                    LyXAction::ReadOnly)) {
372                                 // no
373                                 setErrorMessage(N_("Document is read-only"));
374                                 flag |= LyXFunc::Disabled;
375                         }
376                 } else {
377                         // no
378                         setErrorMessage(N_("Command not allowed with"
379                                            "out any document open"));
380                         flag |= LyXFunc::Disabled;
381                         return flag;
382                 }
383         }
384
385         // I would really like to avoid having this switch and rather try to
386         // encode this in the function itself.
387         bool disable = false;
388         switch (action) {
389         case LFUN_MENUPRINT:
390                 disable = !Exporter::IsExportable(buf, "dvi")
391                         || lyxrc.print_command == "none";
392                 break;
393         case LFUN_EXPORT:
394                 disable = argument == "fax" &&
395                         !Exporter::IsExportable(buf, argument);
396                 break;
397         case LFUN_UNDO:
398                 disable = buf->undostack.empty();
399                 break;
400         case LFUN_REDO:
401                 disable = buf->redostack.empty();
402                 break;
403         case LFUN_SPELLCHECK:
404                 disable = lyxrc.isp_command == "none";
405                 break;
406         case LFUN_RUNCHKTEX:
407                 disable = lyxrc.chktex_command == "none";
408                 break;
409         case LFUN_BUILDPROG:
410                 disable = !Exporter::IsExportable(buf, "program");
411                 break;
412
413         case LFUN_INSERTFOOTNOTE:
414         case LFUN_FOOTMELT:
415         case LFUN_MARGINMELT:
416                 // Disable insertion of floats in a tabular.
417                 disable = false;
418                 if (owner->view()->theLockingInset()) {
419                         disable = (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) ||
420                                 owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
421                 }
422                 break;
423
424         case LFUN_LAYOUT_TABULAR:
425                 disable = true;
426                 if (owner->view()->theLockingInset()) {
427                         disable = (owner->view()->theLockingInset()->LyxCode() != Inset::TABULAR_CODE) &&
428                                 !owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
429                 }
430                 break;
431
432         case LFUN_TABULAR_FEATURE:
433                 disable = true;
434                 if (owner->view()->theLockingInset()) {
435                         func_status ret = LyXFunc::Disabled;
436                         if (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) {
437                                 ret = static_cast<InsetTabular *>
438                                         (owner->view()->theLockingInset())->
439                                         getStatus(argument);
440                         } else if (owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
441                                 ret = static_cast<InsetTabular *>
442                                         (owner->view()->theLockingInset()->
443                                         GetFirstLockingInsetOfType(Inset::TABULAR_CODE))->
444                                         getStatus(argument);
445                         }
446                         flag |= ret;
447                         disable = false;
448                 } else {
449                     static InsetTabular inset(*owner->buffer(), 1, 1);
450                     func_status ret;
451
452                     disable = true;
453                     ret = inset.getStatus(argument);
454                     if ((ret & LyXFunc::ToggleOn) ||
455                         (ret & LyXFunc::ToggleOff))
456                         flag |= LyXFunc::ToggleOff;
457                 }
458                 break;
459
460         case LFUN_VC_REGISTER:
461                 disable = buf->lyxvc.inUse();
462                 break;
463         case LFUN_VC_CHECKIN:
464                 disable = !buf->lyxvc.inUse() || buf->isReadonly();
465                 break;
466         case LFUN_VC_CHECKOUT:
467                 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
468                 break;
469         case LFUN_VC_REVERT:
470         case LFUN_VC_UNDO:
471         case LFUN_VC_HISTORY:
472                 disable = !buf->lyxvc.inUse();
473                 break;
474         case LFUN_BOOKMARK_GOTO:
475                 disable =  !owner->view()->
476                         isSavedPosition(strToUnsignedInt(argument));
477         default:
478                 break;
479         }
480         if (disable)
481                 flag |= LyXFunc::Disabled;
482
483         if (buf) {
484                 func_status box = LyXFunc::ToggleOff;
485                 LyXFont const & font =
486                         TEXT(false)->real_current_font;
487                 switch (action) {
488                 case LFUN_EMPH:
489                         if (font.emph() == LyXFont::ON)
490                                 box = LyXFunc::ToggleOn;
491                         break;
492                 case LFUN_NOUN:
493                         if (font.noun() == LyXFont::ON)
494                                 box = LyXFunc::ToggleOn;
495                         break;
496                 case LFUN_BOLD:
497                         if (font.series() == LyXFont::BOLD_SERIES)
498                                 box = LyXFunc::ToggleOn;
499                         break;
500                 case LFUN_TEX:
501                         if (font.latex() == LyXFont::ON)
502                                 box = LyXFunc::ToggleOn;
503                         break;
504                 default:
505                         box = LyXFunc::OK;
506                         break;
507                 }
508                 flag |= box;
509         }
510
511         return flag;
512 }
513
514
515 string const LyXFunc::Dispatch(string const & s) 
516 {
517         // Split command string into command and argument
518         string cmd;
519         string line = frontStrip(s);
520         string arg = strip(frontStrip(split(line, cmd, ' ')));
521
522         return Dispatch(lyxaction.LookupFunc(cmd), arg);
523 }
524
525
526 string const LyXFunc::Dispatch(int ac,
527                          string const & do_not_use_this_arg)
528 {
529         lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << ac
530                               <<"] arg[" << do_not_use_this_arg << "]" << endl;
531         
532         string argument;
533         kb_action action;
534         
535         // we have not done anything wrong yet.
536         errorstat = false;
537         dispatch_buffer.erase();
538         
539         // if action is a pseudo-action, we need the real action
540         if (lyxaction.isPseudoAction(ac)) {
541                 string tmparg;
542                 action = static_cast<kb_action>
543                         (lyxaction.retrieveActionArg(ac, tmparg));
544                 if (!tmparg.empty())
545                         argument = tmparg;
546         } else {
547                 action = static_cast<kb_action>(ac);
548                 if (!do_not_use_this_arg.empty())
549                         argument = do_not_use_this_arg; // except here
550         }
551     
552         selection_possible = false;
553         
554         if (owner->view()->available())
555                 owner->view()->hideCursor();
556
557         // We cannot use this function here
558         if (getStatus(ac, do_not_use_this_arg) & Disabled)
559                 goto exit_with_message;
560
561         commandshortcut.erase();
562         
563         if (lyxrc.display_shortcuts && show_sc) {
564                 if (action != LFUN_SELFINSERT) {
565                         // Put name of command and list of shortcuts
566                         // for it in minibuffer
567                         string comname = lyxaction.getActionName(action);
568
569                         int pseudoaction = action;
570                         bool argsadded = false;
571
572                         if (!argument.empty()) {
573                                 // If we have the command with argument, 
574                                 // this is better
575                                 pseudoaction = 
576                                         lyxaction.searchActionArg(action,
577                                                                   argument);
578
579                                 if (pseudoaction == -1) {
580                                         pseudoaction = action;
581                                 } else {
582                                         comname += " " + argument;
583                                         argsadded = true;
584                                 }
585                         }
586
587                         string const shortcuts =
588                                 toplevel_keymap->findbinding(pseudoaction);
589
590                         if (!shortcuts.empty()) {
591                                 comname += ": " + shortcuts;
592                         } else if (!argsadded) {
593                                 comname += " " + argument;
594                         }
595
596                         if (!comname.empty()) {
597                                 comname = strip(comname);
598                                 commandshortcut = "(" + comname + ')';
599                                 owner->getMiniBuffer()->Set(commandshortcut);
600                                 // Here we could even add a small pause,
601                                 // to annoy the user and make him learn
602                                 // the shortcuts.
603                                 // No! That will just annoy, not teach
604                                 // anything. The user will read the messages
605                                 // if they are interested. (Asger)
606                         }
607                 }
608         }
609
610         if (owner->view()->available() && owner->view()->theLockingInset()) {
611                 UpdatableInset::RESULT result;
612                 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
613                                      (keyseq.length >= -1)))
614                 {
615                         if ((action==LFUN_UNKNOWN_ACTION) && argument.empty()){
616                                 argument = keyseq.getiso();
617                         }
618                         // Undo/Redo pre 0.13 is a bit tricky for insets.
619                         if (action == LFUN_UNDO) {
620                                 int slx;
621                                 int sly;
622                                 UpdatableInset * inset = 
623                                         owner->view()->theLockingInset();
624                                 inset->GetCursorPos(owner->view(), slx, sly);
625                                 owner->view()->unlockInset(inset);
626                                 owner->view()->menuUndo();
627                                 if (TEXT()->cursor.par()->
628                                     IsInset(TEXT()->cursor.pos())) {
629                                         inset = static_cast<UpdatableInset*>(
630                                                 TEXT()->cursor.par()->
631                                                 GetInset(TEXT()->
632                                                          cursor.pos()));
633                                 } else {
634                                         inset = 0;
635                                 }
636                                 if (inset)
637                                         inset->Edit(owner->view(),slx,sly,0);
638                                 return string();
639                         } else if (action == LFUN_REDO) {
640                                 int slx;
641                                 int sly;
642                                 UpdatableInset * inset = owner->view()->
643                                         theLockingInset();
644                                 inset->GetCursorPos(owner->view(), slx, sly);
645                                 owner->view()->unlockInset(inset);
646                                 owner->view()->menuRedo();
647                                 inset = static_cast<UpdatableInset*>(
648                                         TEXT()->cursor.par()->
649                                         GetInset(TEXT()->
650                                                  cursor.pos()));
651                                 if (inset)
652                                         inset->Edit(owner->view(),slx,sly,0); 
653                                 return string();
654                         } else if (((result=owner->view()->theLockingInset()->
655                                    LocalDispatch(owner->view(), action,
656                                                  argument)) ==
657                                    UpdatableInset::DISPATCHED) ||
658                                    (result == UpdatableInset::DISPATCHED_NOUPDATE))
659                                 return string();
660                         else {
661                                 //setMessage(N_("Text mode"));
662                                 switch (action) {
663                                 case LFUN_UNKNOWN_ACTION:
664                                 case LFUN_BREAKPARAGRAPH:
665                                 case LFUN_BREAKLINE:
666                                         TEXT()->CursorRight(owner->view());
667                                         owner->view()->setState();
668                                         owner->showState();
669                                         break;
670                                 case LFUN_RIGHT:
671                                         if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
672                                                 TEXT()->CursorRight(owner->view());
673                                                 moveCursorUpdate(true, false);
674                                                 owner->showState();
675                                         }
676                                         return string();
677                                 case LFUN_LEFT: 
678                                         if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
679                                                 TEXT()->CursorRight(owner->view());
680                                                 moveCursorUpdate(true, false);
681                                                 owner->showState();
682                                         }
683                                         return string();
684                                 case LFUN_DOWN:
685                                         TEXT()->CursorDown(owner->view());
686                                         moveCursorUpdate(true, false);
687                                         owner->showState();
688                                         return string();
689                                 default:
690                                         break;
691                                 }
692                         }
693                 }
694         }
695
696         Assert(action != LFUN_SELECT_FILE_SYNC);
697
698         switch (action) {
699                 // --- Misc -------------------------------------------
700         case LFUN_WORDFINDFORWARD  : 
701         case LFUN_WORDFINDBACKWARD : {
702                 static string last_search;
703                 string searched_string;
704             
705                 if (!argument.empty()) {
706                         last_search = argument;
707                         searched_string = argument;
708                 } else {
709                         searched_string = last_search;
710                 }
711
712                 if (!searched_string.empty() &&
713                     ((action == LFUN_WORDFINDBACKWARD) ? 
714                      SearchBackward(owner->view(), searched_string) :
715                      SearchForward(owner->view(), searched_string))) {
716
717                         // ??? What is that ???
718                         owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
719
720                         // ??? Needed ???
721                         // clear the selection (if there is any) 
722                         owner->view()->toggleSelection();
723                         TEXT()->ClearSelection(owner->view());
724
725                         // Move cursor so that successive C-s 's will not stand in place. 
726                         if (action == LFUN_WORDFINDFORWARD ) 
727                                 TEXT()->CursorRightOneWord(owner->view());
728                         TEXT()->FinishUndo();
729                         moveCursorUpdate(true, false);
730
731                         // ??? Needed ???
732                         // set the new selection 
733                         // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
734                         owner->view()->toggleSelection(false);
735                 }
736          
737                 // REMOVED : if (owner->view()->getWorkArea()->focus)
738                 owner->view()->showCursor();
739         }
740         break;
741                 
742         case LFUN_PREFIX:
743         {
744                 if (owner->view()->available()) {
745                         owner->view()->update(TEXT(),
746                                               BufferView::SELECT|BufferView::FITCUR);
747                 }
748                 string buf;
749                 keyseq.print(buf, true);
750                 owner->getMiniBuffer()->Set(buf, string(), string(), 1);
751         }
752         break;
753
754         // --- Misc -------------------------------------------
755         case LFUN_EXEC_COMMAND:
756                 owner->getMiniBuffer()->PrepareForCommand(); 
757                 break;
758                 
759         case LFUN_CANCEL:                   // RVDK_PATCH_5
760                 keyseq.reset();
761                 meta_fake_bit = 0;
762                 if (owner->view()->available())
763                         // cancel any selection
764                         Dispatch(LFUN_MARK_OFF);
765                 setMessage(N_("Cancel"));
766                 break;
767
768         case LFUN_META_FAKE:                                 // RVDK_PATCH_5
769         {
770                 meta_fake_bit = Mod1Mask;
771                 string buf;
772                 keyseq.print(buf, true);
773                 setMessage(buf); // RVDK_PATCH_5
774         }
775         break;  
776
777         case LFUN_READ_ONLY_TOGGLE:
778                 if (owner->buffer()->lyxvc.inUse()) {
779                         owner->buffer()->lyxvc.toggleReadOnly();
780                 } else {
781                         owner->buffer()->setReadonly(
782                                 !owner->buffer()->isReadonly());
783                 }
784                 break;
785                 
786         case LFUN_CENTER: // this is center and redraw.
787                 owner->view()->center();
788                 break;
789                 
790                 // --- Menus -----------------------------------------------
791         case LFUN_MENUNEW:
792                 MenuNew(false);
793                 break;
794                 
795         case LFUN_MENUNEWTMPLT:
796                 MenuNew(true);
797                 break;
798                 
799         case LFUN_CLOSEBUFFER:
800                 CloseBuffer();
801                 break;
802                 
803         case LFUN_MENUWRITE:
804                 if (!owner->buffer()->isUnnamed()) {
805                         owner->getMiniBuffer()->Set(_("Saving document"),
806                                                     MakeDisplayPath(owner->buffer()->fileName()),
807                                                     "...");
808                         MenuWrite(owner->view(), owner->buffer());
809                 } else
810                         WriteAs(owner->view(), owner->buffer());
811                 break;
812                 
813         case LFUN_WRITEAS:
814                 WriteAs(owner->view(), owner->buffer(), argument);
815                 break;
816                 
817         case LFUN_MENURELOAD:
818                 reloadBuffer();
819                 break;
820                 
821         case LFUN_UPDATE:
822                 Exporter::Export(owner->buffer(), argument, true);
823                 break;
824
825         case LFUN_PREVIEW:
826                 Exporter::Preview(owner->buffer(), argument);
827                 break;
828                 
829         case LFUN_BUILDPROG:
830                 Exporter::Export(owner->buffer(), "program", true);
831                 break;
832                 
833         case LFUN_RUNCHKTEX:
834                 MenuRunChktex(owner->buffer());
835                 break;
836                                 
837         case LFUN_MENUPRINT:
838                 owner->getDialogs()->showPrint();
839                 break;
840
841         case LFUN_EXPORT:
842                 if (argument == "custom")
843                         MenuSendto();
844                 else
845                         Exporter::Export(owner->buffer(), argument, false);
846                 break;
847
848         case LFUN_IMPORT:
849                 doImport(argument);
850                 break;
851                 
852         case LFUN_QUIT:
853                 QuitLyX();
854                 break;
855                 
856         case LFUN_TOCVIEW:
857         case LFUN_LOFVIEW:
858         case LFUN_LOTVIEW:
859         case LFUN_LOAVIEW:
860         {
861                 InsetCommandParams p;
862                 
863                 if (action == LFUN_TOCVIEW )
864                         p.setCmdName( "tableofcontents" );
865                 else if (action == LFUN_LOAVIEW )
866                         p.setCmdName( "listofalgorithms" );
867                 else if (action == LFUN_LOFVIEW )
868                         p.setCmdName( "listoffigures" );
869                 else
870                         p.setCmdName( "listoftables" );
871
872                 owner->getDialogs()->createTOC( p.getAsString() );
873                 break;
874         }       
875
876         case LFUN_DIALOG_TABULAR_INSERT:
877                 owner->getDialogs()->showTabularCreate();
878                 break;
879                 
880         case LFUN_FIGURE:
881                 Figure();
882                 break;
883
884         case LFUN_AUTOSAVE:
885                 AutoSave(owner->view());
886                 break;
887                 
888         case LFUN_UNDO:
889                 owner->view()->menuUndo();
890                 break;
891                 
892         case LFUN_REDO:
893                 owner->view()->menuRedo();
894                 break;
895                 
896         case LFUN_MENUSEARCH:
897                 owner->getDialogs()->showSearch();
898                 break;
899                 
900         case LFUN_REMOVEERRORS:
901                 if (owner->view()->removeAutoInsets()) {
902                         owner->view()->redraw();
903                         owner->view()->fitCursor(TEXT());
904                 }
905                 break;
906                 
907 #ifndef NEW_INSETS
908         case LFUN_OPENSTUFF:
909                 owner->view()->openStuff();
910                 break;
911 #endif
912                 
913         case LFUN_HYPHENATION:
914                 owner->view()->hyphenationPoint();
915                 break;
916                 
917         case LFUN_LDOTS:
918                 owner->view()->ldots();
919                 break;
920                 
921         case LFUN_END_OF_SENTENCE:
922                 owner->view()->endOfSentenceDot();
923                 break;
924
925         case LFUN_MENU_SEPARATOR:
926                 owner->view()->menuSeparator();
927                 break;
928                 
929         case LFUN_HFILL:
930                 owner->view()->hfill();
931                 break;
932                 
933         case LFUN_DEPTH:
934                 changeDepth(owner->view(), TEXT(false), 0);
935                 break;
936                 
937         case LFUN_DEPTH_MIN:
938                 changeDepth(owner->view(), TEXT(false), -1);
939                 break;
940                 
941         case LFUN_DEPTH_PLUS:
942                 changeDepth(owner->view(), TEXT(false), 1);
943                 break;
944                 
945         case LFUN_FREE:
946                 owner->getDialogs()->setUserFreeFont();
947                 break;
948                 
949         case LFUN_TEX:
950                 Tex(owner->view());
951                 owner->view()->setState();
952                 owner->showState();
953                 break;
954 #ifndef NEW_INSETS              
955         case LFUN_MELT:
956                 Melt(owner->view());
957                 break;
958 #endif
959         case LFUN_RECONFIGURE:
960                 Reconfigure(owner->view());
961                 break;
962 #ifndef NEW_INSETS
963         case LFUN_FOOTMELT:
964                 if (owner->view()->available()
965                     && !TEXT()->selection
966                     && TEXT()->cursor.par()->footnoteflag
967                     != LyXParagraph::NO_FOOTNOTE)
968                         { // only melt footnotes with FOOTMELT, not margins etc
969                                 if (TEXT()->cursor.par()->footnotekind == LyXParagraph::FOOTNOTE)
970                                         Melt(owner->view());
971                         }
972                 else
973                         Foot(owner->view()); 
974                 owner->view()->setState();
975                 break;
976
977         case LFUN_MARGINMELT:
978                 if (owner->view()->available()
979                     && !TEXT()->selection
980                     && TEXT()->cursor.par()->footnoteflag
981                     != LyXParagraph::NO_FOOTNOTE) {
982                         // only melt margins
983                         if (TEXT()->cursor.par()->footnotekind == LyXParagraph::MARGIN)
984                                 Melt(owner->view());
985                 } else
986                         Margin(owner->view()); 
987                 owner->view()->setState();
988                 break;
989 #endif
990 #ifndef NEW_INSETS
991         case LFUN_FLOATSOPERATE:
992                 if (argument == "openfoot")
993                         owner->view()->allFloats(1,0);
994                 else if (argument == "closefoot")
995                         owner->view()->allFloats(0,0);
996                 else if (argument == "openfig")
997                         owner->view()->allFloats(1,1);
998                 else if (argument == "closefig")
999                         owner->view()->allFloats(0,1);
1000                 break;
1001 #else
1002 #warning Find another implementation here (or another lyxfunc)!
1003 #endif
1004         case LFUN_HELP_COPYRIGHT:
1005                 owner->getDialogs()->showCopyright();
1006                 break;
1007
1008         case LFUN_HELP_CREDITS:
1009                 owner->getDialogs()->showCredits();
1010                 break;
1011
1012         case LFUN_HELP_OPEN: {
1013                 string arg = argument;
1014                 if (arg.empty()) {
1015                         setErrorMessage(N_("Missing argument"));
1016                         break;
1017                 }
1018                 ProhibitInput(owner->view());
1019                 string fname = i18nLibFileSearch("doc", arg, "lyx");
1020                 if (fname.empty()) {
1021                         lyxerr << "LyX: unable to find documentation file `"
1022                                << arg << "'. Bad installation?" << endl;
1023                         AllowInput(owner->view());
1024                         break;
1025                 }
1026                 owner->getMiniBuffer()->Set(_("Opening help file"),
1027                                             MakeDisplayPath(fname),"...");
1028                 owner->view()->buffer(bufferlist.loadLyXFile(fname,false));
1029                 AllowInput(owner->view());
1030                 break;
1031         }
1032
1033         case LFUN_HELP_VERSION: {
1034                 ProhibitInput(owner->view());
1035                 string msg(_("LyX Version "));
1036                 msg += LYX_VERSION;
1037                 msg += " of ";
1038                 msg += LYX_RELEASE;
1039                 fl_show_message(msg.c_str(),
1040                                 (_("Library directory: ")
1041                                  + MakeDisplayPath(system_lyxdir)).c_str(),
1042                                 (_("User directory: ") 
1043                                  + MakeDisplayPath(user_lyxdir)).c_str());
1044                 AllowInput(owner->view());
1045                 break;
1046         }
1047         
1048                 // --- version control -------------------------------
1049         case LFUN_VC_REGISTER:
1050         {
1051                 if (!owner->buffer()->lyxvc.inUse())
1052                         owner->buffer()->lyxvc.registrer();
1053         }
1054         break;
1055                 
1056         case LFUN_VC_CHECKIN:
1057         {
1058                 if (owner->buffer()->lyxvc.inUse()
1059                     && !owner->buffer()->isReadonly())
1060                         owner->buffer()->lyxvc.checkIn();
1061         }
1062         break;
1063                 
1064         case LFUN_VC_CHECKOUT:
1065         {
1066                 if (owner->buffer()->lyxvc.inUse()
1067                     && owner->buffer()->isReadonly())
1068                         owner->buffer()->lyxvc.checkOut();
1069         }
1070         break;
1071         
1072         case LFUN_VC_REVERT:
1073         {
1074                 owner->buffer()->lyxvc.revert();
1075         }
1076         break;
1077                 
1078         case LFUN_VC_UNDO:
1079         {
1080                 owner->buffer()->lyxvc.undoLast();
1081         }
1082         break;
1083                 
1084         case LFUN_VC_HISTORY:
1085         {
1086                 owner->getDialogs()->showVCLogFile();
1087                 break;
1088         }
1089         
1090         // --- buffers ----------------------------------------
1091
1092         case LFUN_SWITCHBUFFER:
1093                 owner->view()->buffer(bufferlist.getBuffer(argument));
1094                 break;
1095
1096         case LFUN_FILE_NEW:
1097         {
1098                 // servercmd: argument must be <file>:<template>
1099                 Buffer * tmpbuf = NewLyxFile(argument);
1100                 if (tmpbuf)
1101                         owner->view()->buffer(tmpbuf);
1102         }
1103         break;
1104                         
1105         case LFUN_FILE_OPEN:
1106                 Open(argument);
1107                 break;
1108
1109         case LFUN_LATEX_LOG:
1110                 owner->getDialogs()->showLogFile();
1111                 break;
1112                 
1113         case LFUN_LAYOUTNO:
1114         {
1115                 lyxerr.debug() << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1116                 int sel = strToInt(argument);
1117                 lyxerr.debug() << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1118                 
1119                 // Should this give a setMessage instead?
1120                 if (sel == 0) 
1121                         return string(); // illegal argument
1122
1123                 --sel; // sel 1..., but layout 0...
1124
1125                 // Pretend we got the name instead.
1126                 Dispatch(int(LFUN_LAYOUT), 
1127                          textclasslist.NameOfLayout(owner->view()
1128                                                     ->buffer()->params.textclass,
1129                                                     sel));
1130                 return string();
1131         }
1132                 
1133         case LFUN_LAYOUT_DOCUMENT:
1134                 owner->getDialogs()->showLayoutDocument();
1135                 break;
1136                 
1137         case LFUN_LAYOUT_PARAGRAPH:
1138                 owner->getDialogs()->showLayoutParagraph();
1139                 break;
1140                 
1141         case LFUN_LAYOUT_CHARACTER:
1142                 owner->getDialogs()->showLayoutCharacter();
1143                 break;
1144
1145         case LFUN_LAYOUT_TABULAR:
1146             if (owner->view()->theLockingInset()) {
1147                 if (owner->view()->theLockingInset()->LyxCode()==Inset::TABULAR_CODE) {
1148                     InsetTabular * inset = static_cast<InsetTabular *>
1149                         (owner->view()->theLockingInset());
1150                     inset->OpenLayoutDialog(owner->view());
1151                 } else if (owner->view()->theLockingInset()->
1152                            GetFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1153                     InsetTabular * inset = static_cast<InsetTabular *>(
1154                         owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE));
1155                     inset->OpenLayoutDialog(owner->view());
1156                 }
1157             }
1158             break;
1159
1160         case LFUN_LAYOUT_PREAMBLE:
1161                 owner->getDialogs()->showPreamble();
1162                 break;
1163                 
1164         case LFUN_LAYOUT_SAVE_DEFAULT:
1165                 MenuLayoutSave(owner->view());
1166                 break;
1167                 
1168         case LFUN_DROP_LAYOUTS_CHOICE:
1169                 owner->getToolbar()->openLayoutList();
1170                 break;
1171
1172         case LFUN_MENU_OPEN_BY_NAME:
1173                 owner->getMenubar()->openByName(argument);
1174                 break; // RVDK_PATCH_5
1175                 
1176         case LFUN_SPELLCHECK:
1177                 if (lyxrc.isp_command != "none")
1178                         ShowSpellChecker(owner->view());
1179                 break; // RVDK_PATCH_5
1180                 
1181         // --- lyxserver commands ----------------------------
1182
1183
1184         case LFUN_GETNAME:
1185                 setMessage(owner->buffer()->fileName());
1186                 lyxerr.debug() << "FNAME["
1187                                << owner->buffer()->fileName()
1188                                << "] " << endl;
1189                 break;
1190                 
1191         case LFUN_NOTIFY:
1192         {
1193                 string buf;
1194                 keyseq.print(buf);
1195                 dispatch_buffer = buf;
1196                 lyxserver->notifyClient(dispatch_buffer);
1197         }
1198         break;
1199
1200         case LFUN_GOTOFILEROW:
1201         {
1202                 char file_name[100];
1203                 int row;
1204                 ::sscanf(argument.c_str(), " %s %d", file_name, &row);
1205
1206                 // Must replace extension of the file to be .lyx and get full path
1207                 string s = ChangeExtension(string(file_name), ".lyx");
1208
1209                 // Either change buffer or load the file
1210                 if (bufferlist.exists(s))
1211                         owner->view()->buffer(bufferlist.getBuffer(s));
1212                 else
1213                         owner->view()->buffer(bufferlist.loadLyXFile(s));
1214
1215                 // Set the cursor  
1216                 owner->view()->setCursorFromRow(row);
1217
1218                 // Recenter screen
1219                 owner->view()->center();
1220         }
1221         break;
1222
1223         case LFUN_GOTO_PARAGRAPH:
1224         {
1225                 istringstream istr(argument.c_str());
1226
1227                 int id;
1228                 istr >> id;
1229                 LyXParagraph * par = TEXT()->GetParFromID(id);
1230                 if (par == 0)
1231                         break;
1232
1233                 // Set the cursor
1234                 TEXT()->SetCursor(owner->view(), par, 0);
1235                 owner->view()->setState();
1236                 owner->showState();
1237
1238                 // Recenter screen
1239                 owner->view()->center();
1240         }
1241         break;
1242
1243         case LFUN_APROPOS:
1244         case LFUN_GETTIP:
1245         {
1246                 int const qa = lyxaction.LookupFunc(argument);
1247                 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1248         }
1249         break;
1250
1251         // --- toolbar ----------------------------------
1252         case LFUN_PUSH_TOOLBAR:
1253         {
1254                 int nth = strToInt(argument);
1255                 if (nth <= 0) {
1256                         setErrorMessage(N_("Push-toolbar needs argument > 0"));
1257                 } else {
1258                         owner->getToolbar()->push(nth);
1259                 }
1260         }
1261         break;
1262         
1263         case LFUN_ADD_TO_TOOLBAR:
1264         {
1265                 if (lyxerr.debugging(Debug::GUI)) {
1266                         lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1267                                 "argument = `" << argument << '\'' << endl;
1268                 }
1269                 string tmp(argument);
1270                 //lyxerr <<string("Argument: ") + argument);
1271                 //lyxerr <<string("Tmp     : ") + tmp);
1272                 if (tmp.empty()) {
1273                         setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1274                 } else {
1275                         owner->getToolbar()->add(argument, false);
1276                         owner->getToolbar()->set();
1277                 }
1278         }
1279         break;
1280         
1281         // --- insert characters ----------------------------------------
1282
1283         // ---  Mathed stuff. If we are here, there is no locked inset yet.
1284         
1285         // Greek mode     
1286         case LFUN_GREEK:
1287         {
1288                 if (!greek_kb_flag) {
1289                         greek_kb_flag = 1;
1290                         setMessage(N_("Math greek mode on"));
1291                 } else
1292                         greek_kb_flag = 0;
1293         }  
1294         break;
1295       
1296         // Greek keyboard      
1297         case LFUN_GREEK_TOGGLE:
1298         {
1299                 greek_kb_flag = greek_kb_flag ? 0 : 2;
1300                 if (greek_kb_flag) {
1301                         setMessage(N_("Math greek keyboard on"));
1302                 } else {
1303                         setMessage(N_("Math greek keyboard off"));
1304                 }
1305         }
1306         break;
1307         
1308         case LFUN_MATH_NUMBER:
1309         case LFUN_MATH_LIMITS:
1310         {
1311                 setErrorMessage(N_("This is only allowed in math mode!"));
1312         
1313         }
1314         break;
1315
1316         case LFUN_MATH_PANEL:
1317                 owner->getDialogs()->showMathPanel();
1318                 break;
1319         
1320         case LFUN_CITATION_CREATE:
1321         {
1322                 InsetCommandParams p( "cite" );
1323                 
1324                 if (!argument.empty()) {
1325                         // This should be set at source, ie when typing
1326                         // "citation-insert foo" in the minibuffer.
1327                         // Question: would pybibliographer also need to be
1328                         // changed. Suspect so. Leave as-is therefore.
1329                         if (contains(argument, "|")) {
1330                                 p.setContents( token(argument, '|', 0) );
1331                                 p.setOptions(  token(argument, '|', 1) );
1332                         } else {
1333                                 p.setContents( argument );
1334                         }
1335                         Dispatch(LFUN_CITATION_INSERT, p.getAsString());
1336                 } else
1337                         owner->getDialogs()->createCitation( p.getAsString() );
1338         }
1339         break;
1340                     
1341         case LFUN_CHILDOPEN:
1342         {
1343                 string const filename =
1344                         MakeAbsPath(argument, 
1345                                     OnlyPath(owner->buffer()->fileName()));
1346                 setMessage(N_("Opening child document ") +
1347                            MakeDisplayPath(filename) + "...");
1348                 owner->view()->savePosition(0);
1349                 if (bufferlist.exists(filename))
1350                         owner->view()->buffer(bufferlist.getBuffer(filename));
1351                 else
1352                         owner->view()->buffer(bufferlist.loadLyXFile(filename));
1353         }
1354         break;
1355
1356 #ifndef NEW_INSETS
1357         case LFUN_INSERTFOOTNOTE: 
1358         {
1359                 LyXParagraph::footnote_kind kind;
1360                 if (argument == "footnote") {
1361                         kind = LyXParagraph::FOOTNOTE;
1362                 } else if (argument == "margin") {
1363                         kind = LyXParagraph::MARGIN;
1364                 } else if (argument == "figure") {
1365                         kind = LyXParagraph::FIG;
1366                 } else if (argument == "table") {
1367                         kind = LyXParagraph::TAB;
1368                 } else if (argument == "wide-fig") {
1369                         kind = LyXParagraph::WIDE_FIG;
1370                 } else if (argument == "wide-tab") {
1371                         kind = LyXParagraph::WIDE_TAB;
1372                 } else if (argument == "algorithm") {
1373                         kind = LyXParagraph::ALGORITHM;
1374                 } else {
1375                         setErrorMessage(N_("Unknown kind of footnote"));
1376                         break;
1377                 }
1378                 TEXT()->InsertFootnoteEnvironment(owner->view(), kind);
1379                 owner->view()->update(TEXT(),
1380                                       BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1381                 owner->view()->setState();
1382         }
1383         break;
1384 #endif
1385         case LFUN_TOGGLECURSORFOLLOW:
1386                 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1387                 break;
1388                 
1389         case LFUN_KMAP_OFF:             // keymap off
1390                 owner->getIntl()->KeyMapOn(false);
1391                 break;
1392                 
1393         case LFUN_KMAP_PRIM:    // primary keymap
1394                 owner->getIntl()->KeyMapPrim();
1395                 break;
1396                 
1397         case LFUN_KMAP_SEC:             // secondary keymap
1398                 owner->getIntl()->KeyMapSec();
1399                 break;
1400                 
1401         case LFUN_KMAP_TOGGLE:  // toggle keymap
1402                 owner->getIntl()->ToggleKeyMap();
1403                 break;
1404
1405         case LFUN_SEQUENCE: 
1406         {
1407                 // argument contains ';'-terminated commands
1408                 while (argument.find(';') != string::npos) {
1409                         string first;
1410                         argument = split(argument, first, ';');
1411                         Dispatch(first);
1412                 }
1413         }
1414         break;
1415
1416         case LFUN_DIALOG_PREFERENCES:
1417                 owner->getDialogs()->showPreferences();
1418                 break;
1419                 
1420         case LFUN_SAVEPREFERENCES:
1421         {
1422                 Path p(user_lyxdir);
1423                 lyxrc.write("preferences");
1424         }
1425         break;
1426
1427         case LFUN_SCREEN_FONT_UPDATE:
1428         {
1429                 // handle the screen font changes.
1430                 // 
1431                 lyxrc.set_font_norm_type();
1432                 fontloader.update();
1433                 // Of course we should only do the resize and the textcache.clear
1434                 // if values really changed...but not very important right now. (Lgb)
1435                 // All buffers will need resize
1436                 bufferlist.resize();
1437                 // We also need to empty the textcache so that
1438                 // the buffer will be formatted correctly after
1439                 // a zoom change.
1440                 textcache.clear();
1441         }
1442         break;
1443
1444         case LFUN_SET_COLOR:
1445         {
1446                 string lyx_name;
1447                 string const x11_name = split(argument, lyx_name, ' ');
1448                 if (lyx_name.empty() || x11_name.empty()) {
1449                         setErrorMessage(N_("Syntax: set-color <lyx_name>"
1450                                                 " <x11_name>"));
1451                         break;
1452                         }
1453
1454                 if (!lcolor.setColor(lyx_name, x11_name)) {
1455                         static string const err1 (N_("Set-color \""));
1456                         static string const err2 (
1457                                 N_("\" failed - color is undefined "
1458                                    "or may not be redefined"));
1459                         setErrorMessage(_(err1) + lyx_name + _(err2));
1460                         break;
1461                 }
1462                 lyxColorHandler->updateColor(lcolor.getFromLyXName(lyx_name));
1463                 owner->view()->redraw();
1464                 break;
1465         }
1466
1467         default:
1468                 // Then if it was none of the above
1469                 if (!owner->view()->Dispatch(action, argument))
1470                         lyxerr << "A truly unknown func!" << endl;
1471                 break;
1472         } // end of switch
1473
1474 exit_with_message:
1475
1476         string const res = getMessage();
1477
1478         if (res.empty()) {
1479                 if (!commandshortcut.empty()) {
1480                         string const newbuf =
1481                                 owner->getMiniBuffer()->GetText();
1482                         if (newbuf != commandshortcut) {
1483                                 owner->getMiniBuffer()->Set(newbuf
1484                                                             + " " +
1485                                                             commandshortcut);
1486                         }
1487                 }
1488         } else {
1489                 string msg(_(res));
1490                 msg += " ";
1491                 msg += commandshortcut;
1492                 owner->getMiniBuffer()->Set(msg);
1493         }
1494
1495         return res;
1496 }
1497
1498
1499 void LyXFunc::setupLocalKeymap()
1500 {
1501         keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1502         cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1503 }
1504
1505
1506 void LyXFunc::MenuNew(bool fromTemplate)
1507 {
1508         string initpath = lyxrc.document_path;
1509
1510         if (owner->view()->available()) {
1511                 string const trypath = owner->buffer()->filepath;
1512                 // If directory is writeable, use this as default.
1513                 if (IsDirWriteable(trypath) == 1)
1514                         initpath = trypath;
1515         }
1516
1517         static int newfile_number = 0;
1518         string s;
1519         
1520         if (lyxrc.new_ask_filename) {
1521                 FileDialog fileDlg(owner, _("Enter filename for new document"),
1522                         LFUN_SELECT_FILE_SYNC,
1523                         make_pair(string(_("Documents")), string(lyxrc.document_path)),
1524                         make_pair(string(_("Templates")), string(lyxrc.template_path)));
1525
1526                 FileDialog::Result result = fileDlg.Select(initpath, _("*.lyx|LyX Documents (*.lyx)"), _("newfile"));
1527         
1528                 if (result.second.empty()) {
1529                         owner->getMiniBuffer()->Set(_("Canceled."));
1530                         lyxerr.debug() << "New Document Cancelled." << endl;
1531                         return;
1532                 }
1533         
1534                 // get absolute path of file and make sure the filename ends
1535                 // with .lyx
1536                 s = MakeAbsPath(result.second);
1537                 if (!IsLyXFilename(s))
1538                         s += ".lyx";
1539
1540                 // Check if the document already is open
1541                 if (bufferlist.exists(s)) {
1542                         switch (AskConfirmation(_("Document is already open:"), 
1543                                                MakeDisplayPath(s, 50),
1544                                                _("Do you want to close that document now?\n"
1545                                                  "('No' will just switch to the open version)")))
1546                         {
1547                         case 1: // Yes: close the document
1548                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
1549                                 // If close is canceled, we cancel here too.
1550                                         return;
1551                                 break;
1552                         case 2: // No: switch to the open document
1553                                 owner->view()->buffer(bufferlist.getBuffer(s));
1554                                 return;
1555                         case 3: // Cancel: Do nothing
1556                                 owner->getMiniBuffer()->Set(_("Canceled."));
1557                                 return;
1558                         }
1559                 }
1560                 // Check whether the file already exists
1561                 if (IsLyXFilename(s)) {
1562                         FileInfo fi(s);
1563                         if (fi.readable() &&
1564                             AskQuestion(_("File already exists:"), 
1565                                         MakeDisplayPath(s, 50),
1566                                         _("Do you want to open the document?"))) {
1567                                 // loads document
1568                                 owner->getMiniBuffer()->Set(_("Opening document"), 
1569                                                             MakeDisplayPath(s), "...");
1570                                 XFlush(fl_get_display());
1571                                 owner->view()->buffer(
1572                                         bufferlist.loadLyXFile(s));
1573                                 owner->getMiniBuffer()->Set(_("Document"),
1574                                                             MakeDisplayPath(s),
1575                                                             _("opened."));
1576                                 return;
1577                         }
1578                 }
1579         } else {
1580                 s = AddName(lyxrc.document_path,
1581                             "newfile" + tostr(++newfile_number) + ".lyx");
1582                 FileInfo fi(s);
1583                 while (bufferlist.exists(s) || fi.readable()) {
1584                         ++newfile_number;
1585                         s = AddName(lyxrc.document_path,
1586                                     "newfile" + tostr(newfile_number) +
1587                                     ".lyx");
1588                         fi.newFile(s);
1589                 }
1590         }
1591
1592         // The template stuff
1593         string templname;
1594         if (fromTemplate) {
1595                 FileDialog fileDlg(owner, _("Select template file"),
1596                         LFUN_SELECT_FILE_SYNC,
1597                         make_pair(string(_("Documents")), string(lyxrc.document_path)),
1598                         make_pair(string(_("Templates")), string(lyxrc.template_path)));
1599
1600                 FileDialog::Result result = fileDlg.Select(initpath, _("*.lyx|LyX Documents (*.lyx)"));
1601         
1602                 if (result.first == FileDialog::Later)
1603                         return;
1604
1605                 string const fname = result.second;
1606
1607                 if (fname.empty()) 
1608                         return;
1609                 templname = fname;
1610         }
1611   
1612         // find a free buffer
1613         lyxerr.debug() << "Find a free buffer." << endl;
1614         owner->view()->buffer(bufferlist.newFile(s, templname));
1615 }
1616
1617
1618 void LyXFunc::Open(string const & fname)
1619 {
1620         string initpath = lyxrc.document_path;
1621   
1622         if (owner->view()->available()) {
1623                 string const trypath = owner->buffer()->filepath;
1624                 // If directory is writeable, use this as default.
1625                 if (IsDirWriteable(trypath) == 1)
1626                         initpath = trypath;
1627         }
1628
1629         string filename;
1630  
1631         if (fname.empty()) {
1632                 FileDialog fileDlg(owner, _("Select document to open"),
1633                         LFUN_FILE_OPEN,
1634                         make_pair(string(_("Documents")), string(lyxrc.document_path)),
1635                         make_pair(string(_("Examples")), string(AddPath(system_lyxdir, "examples"))));
1636
1637                 FileDialog::Result result = fileDlg.Select(initpath, "*.lyx|LyX Documents (*.lyx)");
1638         
1639                 if (result.first == FileDialog::Later)
1640                         return;
1641
1642                 filename = result.second;
1643  
1644                 // check selected filename
1645                 if (filename.empty()) {
1646                         owner->getMiniBuffer()->Set(_("Canceled."));
1647                         return;
1648                 }
1649         } else
1650                 filename = fname;
1651
1652         // get absolute path of file and make sure the filename ends
1653         // with .lyx
1654         filename = MakeAbsPath(filename);
1655         if (!IsLyXFilename(filename))
1656                 filename += ".lyx";
1657
1658         // loads document
1659         owner->getMiniBuffer()->Set(_("Opening document"),
1660                                     MakeDisplayPath(filename), "...");
1661         Buffer * openbuf = bufferlist.loadLyXFile(filename);
1662         if (openbuf) {
1663                 owner->view()->buffer(openbuf);
1664                 owner->getMiniBuffer()->Set(_("Document"),
1665                                             MakeDisplayPath(filename),
1666                                             _("opened."));
1667         } else {
1668                 owner->getMiniBuffer()->Set(_("Could not open document"),
1669                                             MakeDisplayPath(filename));
1670         }
1671 }
1672
1673
1674 // checks for running without gui are missing.
1675 void LyXFunc::doImport(string const & argument)
1676 {
1677         string format;
1678         string filename = split(argument, format, ' ');
1679         lyxerr.debug() << "LyXFunc::doImport: " << format 
1680                        << " file: " << filename << endl;
1681
1682         if (filename.empty()) { // need user interaction
1683                 string initpath = lyxrc.document_path;
1684                 
1685                 if (owner->view()->available()) {
1686                         string const trypath = owner->buffer()->filepath;
1687                         // If directory is writeable, use this as default.
1688                         if (IsDirWriteable(trypath) == 1)
1689                                 initpath = trypath;
1690                 }
1691
1692                 string const text = _("Select ") + formats.PrettyName(format)
1693                         + _(" file to import");
1694
1695                 FileDialog fileDlg(owner, text, 
1696                         LFUN_IMPORT,
1697                         make_pair(string(_("Documents")), string(lyxrc.document_path)),
1698                         make_pair(string(_("Examples")), string(AddPath(system_lyxdir, "examples"))));
1699                         
1700                 string const extension = "*." + formats.Extension(format) + "| " +
1701                         formats.PrettyName(format) + " (*." + formats.Extension(format) + ")";
1702
1703                 FileDialog::Result result = fileDlg.Select(initpath, extension);
1704
1705                 if (result.first == FileDialog::Later)
1706                         return;
1707
1708                 filename = result.second;
1709  
1710                 // check selected filename
1711                 if (filename.empty()) 
1712                         owner->getMiniBuffer()->Set(_("Canceled."));
1713         }
1714
1715         // still no filename? abort
1716         if (filename.empty()) 
1717                 return;
1718
1719         // get absolute path of file
1720         filename = MakeAbsPath(filename);
1721
1722         string const lyxfile = ChangeExtension(filename, ".lyx");
1723
1724         // Check if the document already is open
1725         if (bufferlist.exists(lyxfile)) {
1726                 switch (AskConfirmation(_("Document is already open:"), 
1727                                        MakeDisplayPath(lyxfile, 50),
1728                                        _("Do you want to close that document now?\n"
1729                                          "('No' will just switch to the open version)")))
1730                         {
1731                         case 1: // Yes: close the document
1732                                 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1733                                 // If close is canceled, we cancel here too.
1734                                         return;
1735                                 break;
1736                         case 2: // No: switch to the open document
1737                                 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1738                                 return;
1739                         case 3: // Cancel: Do nothing
1740                                 owner->getMiniBuffer()->Set(_("Canceled."));
1741                                 return;
1742                         }
1743         }
1744
1745         // Check if a LyX document by the same root exists in filesystem
1746         FileInfo const f(lyxfile, true);
1747         if (f.exist() && !AskQuestion(_("A document by the name"), 
1748                                       MakeDisplayPath(lyxfile),
1749                                       _("already exists. Overwrite?"))) {
1750                 owner->getMiniBuffer()->Set(_("Canceled."));
1751                 return;
1752         }
1753         // filename should be valid now
1754
1755         Importer::Import(owner, filename, format);
1756 }
1757
1758
1759 void LyXFunc::reloadBuffer()
1760 {
1761         string const fn = owner->buffer()->fileName();
1762         if (bufferlist.close(owner->buffer()))
1763                 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1764 }
1765
1766
1767 void LyXFunc::CloseBuffer()
1768 {
1769         if (bufferlist.close(owner->buffer()) && !quitting) {
1770                 if (bufferlist.empty()) {
1771                         // need this otherwise SEGV may occur while trying to
1772                         // set variables that don't exist
1773                         // since there's no current buffer
1774                         owner->getDialogs()->hideBufferDependent();
1775                 } else {
1776                         owner->view()->buffer(bufferlist.first());
1777                 }
1778         }
1779 }
1780
1781
1782 // Each "owner" should have it's own message method. lyxview and
1783 // the minibuffer would use the minibuffer, but lyxserver would
1784 // send an ERROR signal to its client.  Alejandro 970603
1785 // This func is bit problematic when it comes to NLS, to make the
1786 // lyx servers client be language indepenent we must not translate
1787 // strings sent to this func.
1788 void LyXFunc::setErrorMessage(string const & m) const
1789 {
1790         dispatch_buffer = m;
1791         errorstat = true;
1792 }
1793
1794
1795 void LyXFunc::setMessage(string const & m)
1796 {
1797         dispatch_buffer = m;
1798 }