]> git.lyx.org Git - features.git/blob - src/lyxfunc.C
Factor the export code between the two Dispatch methods, update .lyx and .sgml file...
[features.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-1999 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include <cstdlib>
14 #include <cctype>
15 #include <cstring>
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "lyxlookup.h"
22 #include "kbmap.h"
23 #include "lyxfunc.h"
24 #include "bufferlist.h"
25 #include "lyxserver.h"
26 #include "lyx.h"
27 #include "intl.h"
28 #include "lyx_main.h"
29 #include "lyx_cb.h"
30 #include "LyXAction.h"
31 #if 0
32 #include "insets/insetlatex.h"
33 #endif
34 #include "insets/inseturl.h"
35 #include "insets/insetlatexaccent.h"
36 #include "insets/insettoc.h"
37 #include "insets/insetlof.h"
38 #include "insets/insetloa.h"
39 #include "insets/insetlot.h"
40 #include "insets/insetref.h"
41 #include "insets/insetparent.h"
42 #include "insets/insetindex.h"
43 #include "insets/insetinclude.h"
44 #include "insets/insetbib.h"
45 #include "mathed/formulamacro.h"
46 #include "toolbar.h"
47 #include "spellchecker.h" // RVDK_PATCH_5
48 #include "minibuffer.h"
49 #include "vspace.h"
50 #include "LyXView.h"
51 #include "filedlg.h"
52 #include "lyx_gui_misc.h"
53 #include "support/filetools.h"
54 #include "support/FileInfo.h"
55 #include "support/LAssert.h"
56 #include "support/syscall.h"
57 #include "support/lstrings.h"
58 #include "support/path.h"
59 #include "lyxscreen.h"
60 #include "debug.h"
61 #include "lyxrc.h"
62 #include "lyxtext.h"
63 #include "gettext.h"
64 #include "trans_mgr.h"
65 #include "ImportLaTeX.h"
66 #include "ImportNoweb.h"
67 #include "layout.h"
68
69 extern bool cursor_follows_scrollbar;
70
71 extern void InsertAsciiFile(string const &, bool);
72 extern void math_insert_symbol(char const*);
73 extern Bool math_insert_greek(char const); // why "Bool"?
74 extern BufferList bufferlist;
75 extern LyXServer * lyxserver;
76 extern short greek_kb_flag;
77 extern FD_form_toc * fd_form_toc;
78 extern bool selection_possible;
79
80 extern kb_keymap * toplevel_keymap;
81
82 extern void BeforeChange();
83 extern void MenuWrite(Buffer*);
84 extern void MenuWriteAs(Buffer*);
85 extern int  MenuRunLaTeX(Buffer*);
86 extern int  MenuBuildProg(Buffer*);
87 extern int  MenuRunChktex(Buffer*);
88 extern bool MenuRunDvips(Buffer *, bool);
89 extern void MenuPrint(Buffer*);
90 extern void MenuSendto();
91 extern void QuitLyX();
92 extern void MenuFax(Buffer *);
93 extern void MenuExport(Buffer *,string const &);
94 extern void MenuPasteSelection(char at);
95 extern LyXAction lyxaction;
96 // (alkis)
97 extern tex_accent_struct get_accent(kb_action action);
98
99 extern void AutoSave();
100 extern void MenuSearch();
101 extern void CopyCB();
102 extern void CopyEnvironmentCB();
103 extern void PasteEnvironmentCB();
104 extern void GotoNote();
105 extern void NoteCB();
106 extern void OpenStuff();
107 extern void HyphenationPoint();
108 extern void Ldots();
109 extern void EndOfSentenceDot();
110 extern void MenuSeparator();
111 extern void HFill();
112 extern void MenuUndo();
113 extern void MenuRedo();
114 extern void SmallUpdate(signed char);
115 extern void SetUpdateTimer(float timer= 0.3);
116 extern void FreeUpdateTimer();
117 extern bool MenuPreview(Buffer*);
118 extern bool MenuPreviewPS(Buffer*);
119 extern void MenuInsertLabel(char const *);
120 extern void MenuInsertRef();
121 extern void MenuLayoutCharacter();
122 extern void MenuLayoutParagraph();
123 extern void MenuLayoutDocument();
124 extern void MenuLayoutPaper();
125 extern void MenuLayoutTable(int flag);
126 extern void MenuLayoutQuotes();
127 extern void MenuLayoutPreamble();
128 extern void MenuLayoutSave();
129 extern void bulletForm();
130
131 extern Buffer * NewLyxFile(string const &);
132 extern void LoadLyXFile(string const &);
133 extern void Reconfigure();
134
135 extern int current_layout;
136 extern int getISOCodeFromLaTeX(char *);
137
138 extern int UnlockInset(UpdatableInset *);
139
140 extern void ShowLatexLog();
141
142 extern void UpdateInset(Inset * inset, bool mark_dirty = true);
143
144 /* === globals =========================================================== */
145
146 bool LyXFunc::show_sc = true;
147
148
149 LyXFunc::LyXFunc(LyXView * o)
150         :owner(o)
151 {
152         meta_fake_bit = 0;
153         lyx_dead_action = LFUN_NOACTION;
154         lyx_calling_dead_action = LFUN_NOACTION;
155         setupLocalKeymap();
156 }
157
158
159 // I changed this func slightly. I commented out the ...FinishUndo(),
160 // this means that all places that used to have a moveCursorUpdate, now
161 // have a ...FinishUndo() as the preceeding statement. I have also added
162 // a moveCursorUpdate to some of the functions that updated the cursor, but
163 // that did not show its new position.
164 inline
165 void LyXFunc::moveCursorUpdate(bool selecting)
166 {
167 #ifdef MOVE_TEXT
168         if (selecting || owner->view()->text->mark_set) {
169                 owner->view()->text->SetSelection();
170                 owner->view()->getScreen()->ToggleToggle();
171                 owner->view()->update(0);
172         } else {
173                 owner->view()->update(-2); // this IS necessary
174                 // (Matthias) 
175         }
176 #else
177         if (selecting || owner->buffer()->text->mark_set) {
178                 owner->buffer()->text->SetSelection();
179                 owner->view()->getScreen()->ToggleToggle();
180                 owner->buffer()->update(0);
181         } else {
182                 owner->buffer()->update(-2); // this IS necessary
183                 // (Matthias) 
184         }
185 #endif
186         owner->view()->getScreen()->ShowCursor();
187         
188         /* ---> Everytime the cursor is moved, show the current font state. */
189         // should this too me moved out of this func?
190         //owner->getMiniBuffer()->Set(CurrentState());
191 }
192
193
194 int LyXFunc::processKeyEvent(XEvent * ev)
195 {
196         char s_r[10];
197         s_r[9] = '\0';
198         int num_bytes;
199         int action; 
200         string argument;
201         XKeyEvent * keyevent = &ev->xkey;
202         KeySym keysym_return;
203
204         num_bytes = LyXLookupString(ev, s_r, 10, &keysym_return);
205
206         if (lyxerr.debugging(Debug::KEY)) {
207                 lyxerr << "KeySym is "
208                        << XKeysymToString(keysym_return)
209                        << "["
210                        << keysym_return << "]"
211                        << " and num_bytes is "
212                        << num_bytes
213                        << " the string returned is \""
214                        << s_r << '\"' << endl;
215         }
216         // Do nothing if we have nothing (JMarc)
217         if (num_bytes == 0 && keysym_return == NoSymbol) {
218                 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
219                                    << endl;
220                 return 0;
221         }
222         
223         // this function should be used always [asierra060396]
224         if (owner->view()->available() &&
225             owner->buffer()->the_locking_inset &&
226             keysym_return == XK_Escape) {
227                 UnlockInset(owner->buffer()->the_locking_inset);
228 #ifdef MOVE_TEXT
229                 owner->view()->text->CursorRight();
230 #else
231                 owner->buffer()->text->CursorRight();
232 #endif
233                 return 0;
234         }
235
236         // Can we be sure that this will work for all X-Windows
237         // implementations? (Lgb)
238         // This code snippet makes lyx ignore some keys. Perhaps
239         // all of them should be explictly mentioned?
240         if((keysym_return >= XK_Shift_L && keysym_return <= XK_Hyper_R)
241            || keysym_return == XK_Mode_switch || keysym_return == 0x0)
242                 return 0;
243
244         // Do a one-deep top-level lookup for
245         // cancel and meta-fake keys. RVDK_PATCH_5
246         cancel_meta_seq.reset();
247
248         action = cancel_meta_seq.addkey(keysym_return, keyevent->state
249                                         &(ShiftMask|ControlMask
250                                           |Mod1Mask)); 
251
252         // When not cancel or meta-fake, do the normal lookup. 
253         // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
254         // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
255         if ( (action != LFUN_CANCEL) && (action != LFUN_META_FAKE) ) {
256
257                 // remove Caps Lock and Mod2 as a modifiers
258                 action = keyseq.addkey(keysym_return,
259                                        (keyevent->state | meta_fake_bit)
260                                        &(ShiftMask|ControlMask
261                                          |Mod1Mask));      
262         }
263         // Dont remove this unless you know what you are doing.
264         meta_fake_bit = 0;
265                 
266         if (action == 0) action = LFUN_PREFIX;
267
268         if (lyxerr.debugging(Debug::KEY)) {
269                 char buf[100];
270                 keyseq.print(buf, 100);
271                 lyxerr << "Key ["
272                        << action << "]["
273                        << buf << "]["
274                        << num_bytes << "]" << endl;
275         }
276
277         // already here we know if it any point in going further
278         // why not return already here if action == -1 and
279         // num_bytes == 0? (Lgb)
280
281         if(keyseq.length>1 || keyseq.length<-1){
282                 char buf[100];
283                 keyseq.print(buf, 100);
284                 owner->getMiniBuffer()->Set(buf);
285         }
286
287         if (action == -1) {
288                 if (keyseq.length<-1) { // unknown key sequence...
289                         char buf[100];
290                         LyXBell();
291                         keyseq.print(buf, 100);
292                         owner->getMiniBuffer()->Set(_("Unknown sequence:"), buf);
293                         return 0;
294                 }
295         
296                 char isochar = keyseq.getiso();
297                 if (!(keyevent->state&ControlMask) &&
298                     !(keyevent->state&Mod1Mask) &&
299                     (isochar && keysym_return < 0xF000)) {
300                         argument += isochar;
301                 }
302                 if (argument.empty()) {
303                         lyxerr.debug() << "Empty argument!" << endl;
304                         // This can`t possibly be of any use
305                         // so we`ll skip the dispatch.
306                         return 0;
307                 }
308         } else
309                 if (action == LFUN_SELFINSERT) {
310                         argument = s_r[0];
311                 }
312     
313         bool tmp_sc = show_sc;
314         show_sc = false;
315         Dispatch(action, argument.c_str());
316         show_sc = tmp_sc;
317         
318         return 0;
319
320
321
322 LyXFunc::func_status LyXFunc::getStatus(int ac) const
323 {
324         kb_action action;
325         func_status flag = LyXFunc::OK;
326         string argument;
327         Buffer * buf = owner->buffer();
328         
329         if (lyxaction.isPseudoAction(ac)) 
330                 action = lyxaction.retrieveActionArg(ac, argument);
331         else 
332                 action = static_cast<kb_action>(ac);
333         
334         if (action == LFUN_UNKNOWN_ACTION) {
335                 setErrorMessage(N_("Unknown action"));
336                 return LyXFunc::Unknown;
337         } 
338         
339         // Check whether we need a buffer
340         if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
341                 // Yes we need a buffer, do we have one?
342                 if (buf) {
343                         // yes
344                         // Can we use a readonly buffer?
345                         if (buf->isReadonly() && 
346                             !lyxaction.funcHasFlag(action,
347                                                    LyXAction::ReadOnly)) {
348                                 // no
349                                 setErrorMessage(N_("Document is read-only"));
350                                 flag = func_status(flag | LyXFunc::Disabled);
351                         }
352                 } else {
353                         // no
354                         setErrorMessage(N_("Command not allowed with"
355                                            "out any document open"));
356                         flag = func_status(flag | LyXFunc::Disabled);
357                 }
358         }
359
360         if (flag & LyXFunc::Disabled)
361                 return flag;
362
363         static bool noLaTeX = lyxrc->latex_command == "none";
364         bool disable = false;
365         switch (action) {
366           case LFUN_PREVIEW:
367                   disable = noLaTeX || lyxrc->view_dvi_command == "none";
368                   break;        
369           case LFUN_PREVIEWPS: 
370                   disable = noLaTeX || lyxrc->view_ps_command == "none";
371                   break;
372           case LFUN_RUNLATEX:
373           case LFUN_RUNDVIPS:
374                   disable = noLaTeX;
375                   break;
376           case LFUN_MENUPRINT:
377                   disable = noLaTeX || lyxrc->print_command == "none";
378                   break;
379           case LFUN_FAX:
380                   disable = noLaTeX || lyxrc->fax_command == "none"; 
381                   break;
382           case LFUN_IMPORT:
383                   if (argument == "latex")
384                           disable = lyxrc->relyx_command == "none";
385                   break;
386           case LFUN_EXPORT:
387                   if (argument == "dvi" || argument == "postscript")
388                           disable = noLaTeX;
389                   if (argument == "html")
390                           disable = lyxrc->html_command == "none";
391                   break;
392           case LFUN_UNDO:
393                   disable = buf->undostack.empty();
394                   break;
395           case LFUN_REDO:
396                   disable = buf->redostack.empty();
397                   break;
398           case LFUN_SPELLCHECK:
399                   disable = lyxrc->isp_command == "none";
400                   break;
401           case LFUN_RUNCHKTEX:
402                   disable = lyxrc->chktex_command == "none";
403                   break;
404           case LFUN_LAYOUT_TABLE:
405 #ifdef MOVE_TEXT
406                   disable = ! owner->view()->text->cursor.par->table;
407 #else
408                   disable = ! buf->text->cursor.par->table;
409 #endif
410                   break;
411           default:
412                   break;
413         }
414         if (disable)
415                 flag |= LyXFunc::Disabled;
416
417         if (buf) {
418                 func_status box = LyXFunc::ToggleOff;
419 #ifdef MOVE_TEXT
420                 LyXFont font = owner->view()->text->real_current_font;
421 #else
422                 LyXFont font = buf->text->real_current_font;
423 #endif
424                 switch (action) {
425                 case LFUN_EMPH:
426                         if (font.emph() == LyXFont::ON)
427                                 box = LyXFunc::ToggleOn;
428                         break;
429                 case LFUN_NOUN:
430                         if (font.noun() == LyXFont::ON)
431                                 box = LyXFunc::ToggleOn;
432                         break;
433                 case LFUN_BOLD:
434                         if (font.series() == LyXFont::BOLD_SERIES)
435                                 box = LyXFunc::ToggleOn;
436                         break;
437                 case LFUN_TEX:
438                         if (font.latex() == LyXFont::ON)
439                                 box = LyXFunc::ToggleOn;
440                         break;
441                 default:
442                         box = LyXFunc::OK;
443                         break;
444                 }
445                 flag |= box;
446         }
447
448
449         return flag;
450 }
451
452
453 string LyXFunc::Dispatch(string const & s) 
454 {
455         // Split command string into command and argument
456         string cmd, line = frontStrip(s);
457         string arg = strip(frontStrip(split(line, cmd, ' ')));
458
459         return Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
460 }
461
462
463 #ifdef MOVE_TEXT
464 string LyXFunc::Dispatch(int ac,
465                           char const * do_not_use_this_arg)
466 {
467         string argument;
468         kb_action action;
469         
470         FL_OBJECT * ob = 0;  // This will disapear soon
471     
472         // we have not done anything wrong yet.
473         errorstat = false;
474         dispatch_buffer.clear();
475         
476         // if action is a pseudo-action, we need the real action
477         if (lyxaction.isPseudoAction(ac)) {
478                 string tmparg;
479                 action = static_cast<kb_action>
480                         (lyxaction.retrieveActionArg(ac, tmparg));
481                 if (!tmparg.empty())
482                         argument = tmparg;
483         } else {
484                 action = static_cast<kb_action>(ac);
485                 if (do_not_use_this_arg)
486                         argument = do_not_use_this_arg; // except here
487         }
488     
489         selection_possible = false;
490         
491         if (owner->view()->available() 
492             && owner->view()->getScreen())
493                 owner->view()->getScreen()->HideCursor();
494
495         // We cannot use this function here
496         if (getStatus(action) & Disabled)
497                 goto exit_with_message;
498
499         commandshortcut.clear();
500         
501         if (lyxrc->display_shortcuts && show_sc) {
502                 if (action != LFUN_SELFINSERT) {
503                         // Put name of command and list of shortcuts
504                         // for it in minibuffer
505                         string comname = lyxaction.getActionName(action);
506
507                         int pseudoaction = action;
508                         bool argsadded = false;
509
510                         if (!argument.empty()) {
511                                 // If we have the command with argument, 
512                                 // this is better
513                                 pseudoaction = 
514                                         lyxaction.searchActionArg(action,
515                                                                   argument.c_str());
516
517                                 if (pseudoaction == -1) {
518                                         pseudoaction = action;
519                                 } else {
520                                         comname += " " + argument;
521                                         argsadded = true;
522                                 }
523                         }
524
525                         string shortcuts = toplevel_keymap->findbinding(pseudoaction);
526
527                         if (!shortcuts.empty()) {
528                                 comname += ": " + shortcuts;
529                         } else if (!argsadded) {
530                                 comname += " " + argument;
531                         }
532
533                         if (!comname.empty()) {
534                                 comname = strip(comname);
535                                 commandshortcut = "(" + comname + ')';
536                                 owner->getMiniBuffer()->Set(commandshortcut);
537                                 // Here we could even add a small pause,
538                                 // to annoy the user and make him learn
539                                 // the shortcuts.
540                                 // No! That will just annoy, not teach
541                                 // anything. The user will read the messages
542                                 // if they are interested. (Asger)
543                         }
544                 }
545         }
546
547         // If in math mode pass the control to
548         // the math inset [asierra060396]
549         if (owner->view()->available() &&
550             owner->buffer()->the_locking_inset) {
551                 if (action > 1
552                     || (action == LFUN_UNKNOWN_ACTION && keyseq.length>= -1)) {
553                         if (action == LFUN_UNKNOWN_ACTION && argument.empty()) {
554                                 argument = keyseq.getiso();
555                         }
556                         // Undo/Redo pre 0.13 is a bit tricky for insets.
557                         if (action == LFUN_UNDO) {
558                                 int slx, sly;
559                                 UpdatableInset * inset = 
560                                         owner->buffer()->the_locking_inset;
561                                 inset->GetCursorPos(slx, sly);
562                                 UnlockInset(inset);
563                                 MenuUndo();
564                                 inset = static_cast<UpdatableInset*>(owner->view()->text->cursor.par->GetInset(owner->view()->text->cursor.pos));
565                                 if (inset) 
566                                         inset->Edit(slx, sly);
567                                 return string();
568                         } else 
569                                 if (action == LFUN_REDO) {
570                                         int slx, sly;
571                                         UpdatableInset * inset = owner->buffer()->the_locking_inset;
572                                         inset->GetCursorPos(slx, sly);
573                                         UnlockInset(inset);
574                                         MenuRedo();
575                                         inset = static_cast<UpdatableInset*>(owner->view()->text->cursor.par->GetInset(owner->view()->text->cursor.pos));
576                                         if (inset)
577                                                 inset->Edit(slx, sly);
578                                         return string();
579                                 } else
580                                         if (owner->buffer()->the_locking_inset->LocalDispatch(action, argument.c_str()))
581                                                 return string();
582                                         else {
583                                                 setMessage(N_("Text mode"));
584                                                 if (action == LFUN_RIGHT || action == -1)
585                                                         owner->view()->text->CursorRight();
586                                                 if (action == LFUN_LEFT || action == LFUN_RIGHT)
587                                                         return string();
588                                         }
589                 }
590         }
591
592         switch(action) {
593                 // --- Misc -------------------------------------------
594         case LFUN_WORDFINDFORWARD  : 
595         case LFUN_WORDFINDBACKWARD : {
596                 static string last_search;
597                 string searched_string;
598             
599                 if (!argument.empty()) {
600                         last_search = argument;
601                         searched_string = argument;
602                 } else {
603                         searched_string = last_search;
604                 }
605
606                 LyXText * ltCur = owner->view()->text ;
607
608                 if (!searched_string.empty() &&
609                     ((action == LFUN_WORDFINDBACKWARD) ? 
610                      ltCur->SearchBackward(searched_string.c_str()) :
611                      ltCur->SearchForward(searched_string.c_str()))) {
612
613                         // ??? What is that ???
614                         owner->view()->update(-2);
615
616                         // ??? Needed ???
617                         // clear the selection (if there is any) 
618                         owner->view()->getScreen()->ToggleSelection();
619                         owner->view()->text->ClearSelection();
620
621                         // Move cursor so that successive C-s 's will not stand in place. 
622                         if( action == LFUN_WORDFINDFORWARD ) 
623                                 owner->view()->text->CursorRightOneWord();
624                         owner->view()->text->FinishUndo();
625                         moveCursorUpdate(false);
626
627                         // ??? Needed ???
628                         // set the new selection 
629                         // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
630                         owner->view()->getScreen()->ToggleSelection(false);
631                 } else 
632                         LyXBell();      
633          
634                 // REMOVED : if (owner->view()->getWorkArea()->focus)
635                 owner->view()->getScreen()->ShowCursor();
636         }
637         break;
638
639         case LFUN_PREFIX:
640         {
641                 if (owner->view()->available()
642                     && owner->view()->getScreen()) {
643                         owner->view()->update(-2);
644                 }
645                 char buf[100];
646                 keyseq.print(buf, 100, true);
647                 owner->getMiniBuffer()->Set(buf, string(), string(), 1);
648         }
649         break;
650
651         // --- Misc -------------------------------------------
652         case LFUN_EXEC_COMMAND:
653                 owner->getMiniBuffer()->ExecCommand(); 
654                 break;
655                 
656         case LFUN_CANCEL:                   // RVDK_PATCH_5
657                 keyseq.reset();
658                 meta_fake_bit = 0;
659                 if(owner->view()->available())
660                         // cancel any selection
661                         Dispatch(LFUN_MARK_OFF, 0);
662                 setMessage(N_("Cancel"));
663                 break;
664
665         case LFUN_META_FAKE:                                 // RVDK_PATCH_5
666         {
667                 meta_fake_bit = Mod1Mask;
668                 char buf[100];
669                 keyseq.print(buf, 98, true);
670                 string res = string("M-") + buf;
671                 setMessage(buf); // RVDK_PATCH_5
672         }
673         break;  
674
675         case LFUN_READ_ONLY_TOGGLE:
676                 if (owner->buffer()->lyxvc.inUse()) {
677                         owner->buffer()->lyxvc.toggleReadOnly();
678                 } else {
679                         owner->buffer()->setReadonly(
680                                 !owner->buffer()->isReadonly());
681                 }
682                 break;
683                 
684         case LFUN_CENTER: // this is center and redraw.
685                 BeforeChange();
686                 if (owner->view()->text->cursor.y >
687                     owner->view()->getWorkArea()->h / 2)        {
688                         owner->view()->getScreen()->
689                                 Draw(owner->view()->text->cursor.y -
690                                      owner->view()->getWorkArea()->h/2);
691                 } else { // <= 
692                         owner->view()->getScreen()->
693                                 Draw(0);
694                 }
695                 owner->view()->update(0);
696                 owner->view()->redraw();
697                 break;
698                 
699         case LFUN_APPENDIX:
700                 if (owner->view()->available()) {
701                         owner->view()->text->toggleAppendix();
702                         owner->view()->update(1);
703                 }
704                 break;
705
706         // --- Menus -----------------------------------------------
707         case LFUN_MENUNEW:
708                 MenuNew(false);
709                 break;
710                 
711         case LFUN_MENUNEWTMPLT:
712                 MenuNew(true);
713                 break;
714                 
715         case LFUN_MENUOPEN:
716                 MenuOpen();
717                 break;
718                 
719         case LFUN_CLOSEBUFFER:
720                 CloseBuffer();
721                 break;
722                 
723         case LFUN_MENUWRITE:
724                 MenuWrite(owner->buffer());
725                 break;
726                 
727         case LFUN_MENUWRITEAS:
728                 MenuWriteAs(owner->buffer());
729                 break;
730                 
731         case LFUN_MENURELOAD:
732                 reloadBuffer();
733                 break;
734                 
735         case LFUN_PREVIEW:
736                 MenuPreview(owner->buffer());
737                 break;
738                         
739         case LFUN_PREVIEWPS:
740                 MenuPreviewPS(owner->buffer());
741                 break;
742                 
743         case LFUN_RUNLATEX:
744                 MenuRunLaTeX(owner->buffer());
745                 break;
746                 
747         case LFUN_BUILDPROG:
748                 MenuBuildProg(owner->buffer());
749                 break;
750                 
751         case LFUN_RUNCHKTEX:
752                 MenuRunChktex(owner->buffer());
753                 break;
754                 
755         case LFUN_RUNDVIPS:
756                 MenuRunDvips(owner->buffer(), false);
757                 break;
758                 
759         case LFUN_MENUPRINT:
760                 MenuPrint(owner->buffer());
761                 break;
762                 
763         case LFUN_FAX:
764                 MenuFax(owner->buffer());
765                 break;
766                         
767         case LFUN_EXPORT:
768                 MenuExport(owner->buffer(), argument);
769                 break;
770
771         case LFUN_IMPORT:
772         {
773                 //needs argument as string
774                 string imtyp = argument;
775                 
776                 // latex
777                 if (imtyp == "latex") {
778                         doImportLaTeX(false);
779                 }
780                 // ascii
781                 else if (imtyp == "ascii") {
782                         doImportASCII(false);
783                 } else if (imtyp == "asciiparagraph") {
784                         doImportASCII(true);
785                 // noweb
786                 } else if (imtyp == "noweb") {
787                         doImportLaTeX(true);
788                 } else {
789                         setErrorMessage(string(N_("Unknown import type: "))
790                                         + imtyp);
791                 }
792                 break;
793         }
794                 
795         case LFUN_QUIT:
796                 QuitLyX();
797                 break;
798                 
799         case LFUN_TOCVIEW:
800                 TocUpdateCB(ob, 0);
801                 if (fd_form_toc->form_toc->visible) {
802                         fl_raise_form(fd_form_toc->form_toc);
803                 } else {
804                         static int ow = -1, oh;
805                         fl_show_form(fd_form_toc->form_toc,
806                                      FL_PLACE_MOUSE |
807                                      FL_FREE_SIZE, FL_FULLBORDER,
808                                      _("Table of Contents"));
809                         if (ow < 0) {
810                                 ow = fd_form_toc->form_toc->w;
811                                 oh = fd_form_toc->form_toc->h;
812                         }
813                         fl_set_form_minsize(fd_form_toc->form_toc, ow, oh);
814                 }
815                 break;
816                 
817         case LFUN_TOC_INSERT:
818         {
819                 Inset * new_inset = new InsetTOC(owner->buffer());
820                 owner->buffer()->insertInset(new_inset, "Standard", true);
821                 break;
822         }
823         
824         case LFUN_LOF_INSERT:
825         {
826                 Inset * new_inset = new InsetLOF(owner->buffer());
827                 owner->buffer()->insertInset(new_inset, "Standard", true);
828                 break;
829         }
830         
831         case LFUN_LOA_INSERT:
832         {
833                 Inset * new_inset = new InsetLOA(owner->buffer());
834                 owner->buffer()->insertInset(new_inset, "Standard", true);
835                 break;
836         }
837
838         case LFUN_LOT_INSERT:
839         {
840                 Inset * new_inset = new InsetLOT(owner->buffer());
841                 owner->buffer()->insertInset(new_inset, "Standard", true);
842                 break;
843         }
844                 
845         case LFUN_TABLE:
846                 TableCB(ob, 0);
847                 break;
848                 
849         case LFUN_FIGURE:
850                 FigureCB(ob, 0);
851                 break;
852                 
853         case LFUN_AUTOSAVE:
854                 AutoSave();
855                 break;
856                 
857         case LFUN_UNDO:
858                 MenuUndo();
859                 break;
860                 
861         case LFUN_REDO:
862                 MenuRedo();
863                 break;
864                 
865         case LFUN_MENUSEARCH:
866                 MenuSearch();
867                 break;
868                 
869         case LFUN_PASTE:
870                 PasteCB();
871                 break;
872                 
873         case LFUN_PASTESELECTION:
874         {
875                 bool asPara = false;
876                 if (argument == "paragraph") asPara = true;
877                 MenuPasteSelection(asPara);
878                 break;
879         }
880
881         case LFUN_CUT:
882                 CutCB();
883                 break;
884                 
885         case LFUN_COPY:
886                 CopyCB();
887                 break;
888                 
889         case LFUN_LAYOUT_COPY:
890                 CopyEnvironmentCB();
891                 break;
892                 
893         case LFUN_LAYOUT_PASTE:
894                 PasteEnvironmentCB();
895                 break;
896                 
897         case LFUN_GOTOERROR:
898                 owner->view()->gotoError();
899                 break;
900                 
901         case LFUN_REMOVEERRORS:
902                 if (owner->buffer()->removeAutoInsets()) {
903                         owner->view()->redraw();
904                         owner->view()->fitCursor();
905                         owner->view()->updateScrollbar();
906                 }
907                 break;
908                 
909         case LFUN_GOTONOTE:
910                 GotoNote();
911                 break;
912                 
913         case LFUN_OPENSTUFF:
914                 OpenStuff();
915                 break;
916                 
917         case LFUN_HYPHENATION:
918                 HyphenationPoint();
919                 break;
920                 
921         case LFUN_LDOTS:
922                 Ldots();
923                 break;
924                 
925         case LFUN_END_OF_SENTENCE:
926                 EndOfSentenceDot();
927                 break;
928
929         case LFUN_MENU_SEPARATOR:
930                 MenuSeparator();
931                 break;
932                 
933         case LFUN_HFILL:
934                 HFill();
935                 break;
936                 
937         case LFUN_DEPTH:
938                 DepthCB(ob, 0);
939                 break;
940                 
941         case LFUN_DEPTH_MIN:
942                 DepthCB(ob, -1);
943                 break;
944                 
945         case LFUN_DEPTH_PLUS:
946                 DepthCB(ob, 1);
947                 break;
948                 
949         case LFUN_FREE:
950                 FreeCB();
951                 break;
952                 
953         case LFUN_TEX:
954                 TexCB();
955                 break;
956                 
957         case LFUN_MELT:
958                 MeltCB(ob, 0);
959                 break;
960                 
961         case LFUN_RECONFIGURE:
962                 Reconfigure();
963                 break;
964
965         case LFUN_FOOTMELT:
966                 if (owner->view()->available()
967                     && !owner->view()->text->selection
968                     && owner->view()->text->cursor.par->footnoteflag
969                     != LyXParagraph::NO_FOOTNOTE)
970                 { // only melt footnotes with FOOTMELT, not margins etc
971                   if(owner->view()->text->cursor.par->footnotekind == LyXParagraph::FOOTNOTE)
972                         MeltCB(ob, 0);
973                 }
974                 else
975                         FootCB(ob, 0); 
976                 break;
977
978         case LFUN_MARGINMELT:
979                 if (owner->view()->available()
980                     && !owner->view()->text->selection
981                     && owner->view()->text->cursor.par->footnoteflag
982                     != LyXParagraph::NO_FOOTNOTE) {
983                         // only melt margins
984                         if(owner->view()->text->cursor.par->footnotekind == LyXParagraph::MARGIN)
985                                 MeltCB(ob, 0);
986                 }
987                 else
988                         MarginCB(ob, 0); 
989                 break;
990                 
991                 // --- version control -------------------------------
992         case LFUN_VC_REGISTER:
993         {
994                 if (!owner->buffer()->lyxvc.inUse())
995                         owner->buffer()->lyxvc.registrer();
996         }
997         break;
998                 
999         case LFUN_VC_CHECKIN:
1000         {
1001                 if (owner->buffer()->lyxvc.inUse()
1002                     && !owner->buffer()->isReadonly())
1003                         owner->buffer()->lyxvc.checkIn();
1004         }
1005         break;
1006                 
1007         case LFUN_VC_CHECKOUT:
1008         {
1009                 if (owner->buffer()->lyxvc.inUse()
1010                     && owner->buffer()->isReadonly())
1011                         owner->buffer()->lyxvc.checkOut();
1012         }
1013         break;
1014         
1015         case LFUN_VC_REVERT:
1016         {
1017                 owner->buffer()->lyxvc.revert();
1018         }
1019         break;
1020                 
1021         case LFUN_VC_UNDO:
1022         {
1023                 owner->buffer()->lyxvc.undoLast();
1024         }
1025         break;
1026                 
1027         case LFUN_VC_HISTORY:
1028         {
1029                 owner->buffer()->lyxvc.showLog();
1030                 break;
1031         }
1032         
1033         // --- buffers ----------------------------------------
1034         case LFUN_PREVBUFFER:
1035 #ifdef WITH_WARNINGS
1036 #warning fix this please
1037 #endif
1038                 // it is the LyXView or the BufferView that should
1039                 // remember the previous buffer, not bufferlist.
1040 //                      if (owner->view()->available()){          
1041 //                              BeforeChange();
1042 //                              owner->buffer()->update(-2);
1043 //                      }
1044 //                      owner->view()->setBuffer(bufferlist.prev());
1045
1046 //                      owner->view()->
1047 //                              resizeCurrentBufferPseudoExpose();
1048                 break;
1049                         
1050         case LFUN_FILE_INSERT:
1051         {
1052                 MenuInsertLyXFile(argument);
1053         }
1054         break;
1055         
1056         case LFUN_FILE_INSERT_ASCII:
1057         {
1058                 bool asPara = (argument == "paragraph");
1059                 InsertAsciiFile(string(), asPara);
1060         }
1061         break;
1062         
1063         case LFUN_FILE_NEW:
1064         {
1065                 // servercmd: argument must be <file>:<template>
1066                 Buffer * tmpbuf = NewLyxFile(argument);
1067                 if (tmpbuf)
1068                         owner->view()->buffer(tmpbuf);
1069         }
1070                 break;
1071                         
1072         case LFUN_FILE_OPEN:
1073                 owner->view()->buffer(
1074                         bufferlist.loadLyXFile(argument));
1075                 break;
1076
1077         case LFUN_LATEX_LOG:
1078                 ShowLatexLog();
1079                 break;
1080                 
1081         case LFUN_LAYOUTNO:
1082         {
1083                 lyxerr.debug() << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1084                 int sel = strToInt(argument);
1085                 lyxerr.debug() << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1086                 
1087                 // Should this give a setMessage instead?
1088                 if (sel == 0) 
1089                         return string(); // illegal argument
1090
1091                 sel--; // sel 1..., but layout 0...
1092
1093                 // Pretend we got the name instead.
1094                 Dispatch(int(LFUN_LAYOUT), 
1095                          textclasslist.NameOfLayout(owner->view()->
1096                                                text->parameters->
1097                                                textclass,
1098                                                sel).c_str());
1099                 return string();
1100         }
1101                 
1102         case LFUN_LAYOUT:
1103         {
1104                 lyxerr.debug() << "LFUN_LAYOUT: (arg) "
1105                                << argument << endl;
1106                 
1107                 // Derive layout number from given argument (string)
1108                 // and current buffer's textclass (number). */    
1109                 int layoutno = 
1110                         textclasslist.NumberOfLayout(owner->
1111                                                 view()->
1112                                                 text->parameters->
1113                                                 textclass,
1114                                                 argument).second;
1115
1116                 // see if we found the layout number:
1117                 if (layoutno == -1) {
1118                         setErrorMessage(string(N_("Layout ")) + argument + 
1119                                         N_(" not known"));
1120                         break;
1121                 }
1122                         
1123                 if (current_layout != layoutno) {
1124                         owner->view()->getScreen()->HideCursor();
1125                         current_layout = layoutno;
1126                         owner->view()->update(-2);
1127                         owner->view()->text->
1128                                 SetLayout(layoutno);
1129                         owner->getToolbar()->combox->
1130                                 select(owner->view()->
1131                                        text->cursor.par->
1132                                        GetLayout() + 1);
1133                         owner->view()->update(1);
1134                 }
1135         }
1136         break;
1137
1138         case LFUN_LAYOUT_DOCUMENT:
1139                 MenuLayoutDocument();
1140                 break;
1141                 
1142         case LFUN_LAYOUT_PARAGRAPH:
1143                 MenuLayoutParagraph();
1144                 break;
1145                 
1146         case LFUN_LAYOUT_CHARACTER:
1147                 MenuLayoutCharacter();
1148                 break;
1149                 
1150         case LFUN_LAYOUT_TABLE:
1151         {
1152                 int flag = 0;
1153                 if (argument == "true") flag = 1;
1154                 MenuLayoutTable(flag);
1155         }
1156         break;
1157                 
1158         case LFUN_LAYOUT_PAPER:
1159                 MenuLayoutPaper();
1160                 break;
1161                 
1162         case LFUN_LAYOUT_QUOTES:
1163                 MenuLayoutQuotes();
1164                 break;
1165                 
1166         case LFUN_LAYOUT_PREAMBLE:
1167                 MenuLayoutPreamble();
1168                 break;
1169                 
1170         case LFUN_LAYOUT_SAVE_DEFAULT:
1171                 MenuLayoutSave();
1172                 break;
1173                 
1174         case LFUN_DROP_LAYOUTS_CHOICE:
1175                 owner->getToolbar()->combox->Show();
1176                 break;
1177
1178         case LFUN_EMPH:
1179                 EmphCB();
1180                 break;
1181                 
1182         case LFUN_BOLD:
1183                 BoldCB();
1184                 break;
1185                 
1186         case LFUN_NOUN:
1187                 NounCB();
1188                 break;
1189                 
1190         case LFUN_CODE:
1191                 CodeCB();
1192                 break;
1193                 
1194         case LFUN_SANS:
1195                 SansCB();
1196                 break;
1197                 
1198         case LFUN_ROMAN:
1199                 RomanCB();
1200                 break;
1201                 
1202         case LFUN_DEFAULT:
1203                 StyleResetCB();
1204                 break;
1205                 
1206         case LFUN_UNDERLINE:
1207                 UnderlineCB();
1208                 break;
1209                 
1210         case LFUN_FONT_SIZE:
1211                 FontSizeCB(argument);
1212                 break;
1213                 
1214         case LFUN_FONT_STATE:
1215                 setMessage(CurrentState());
1216                 break;
1217                 
1218         case LFUN_UPCASE_WORD:
1219                 owner->view()->update(-2);
1220                 FreeUpdateTimer();
1221                 owner->view()->text->ChangeWordCase(LyXText::text_uppercase);
1222                 owner->view()->update(1);
1223                 SetUpdateTimer();
1224                 break;
1225                 
1226         case LFUN_LOWCASE_WORD:
1227                 owner->view()->update(-2);
1228                 FreeUpdateTimer();
1229                 owner->view()->text->ChangeWordCase(LyXText::text_lowercase);
1230                 owner->view()->update(1);
1231                 SetUpdateTimer();
1232                 break;
1233                 
1234         case LFUN_CAPITALIZE_WORD:
1235                 owner->view()->update(-2);
1236                 FreeUpdateTimer();
1237                 owner->view()->text->ChangeWordCase(LyXText::text_capitalization);
1238                 owner->view()->update(1);
1239                 SetUpdateTimer();
1240                 break;
1241                 
1242         case LFUN_INSERT_LABEL:
1243                 MenuInsertLabel(argument.c_str());
1244                 break;
1245                 
1246         case LFUN_INSERT_REF:
1247                 MenuInsertRef();
1248                 break;
1249                 
1250         case LFUN_REFTOGGLE:
1251         {
1252                 InsetRef * inset = 
1253                         static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1254                 if (inset) {
1255                         if (inset->getFlag() == InsetRef::REF)
1256                                 inset->setFlag(InsetRef::PAGE_REF);
1257                         else
1258                                 inset->setFlag(InsetRef::REF);
1259                         UpdateInset(inset);
1260                 } else {
1261                         setErrorMessage(N_("No cross-reference to toggle"));
1262                 }
1263         }
1264         break;
1265         
1266         case LFUN_REFBACK:
1267         {
1268                 owner->view()->restorePosition();
1269         }
1270         break;
1271
1272         case LFUN_REFGOTO:
1273         {
1274                 string label(argument);
1275                 if (label.empty()) {
1276                         InsetRef * inset = 
1277                                 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1278                         if (inset)
1279                                 label = inset->getContents();
1280                 }
1281                 
1282                 if (!label.empty()) {
1283                         owner->view()->savePosition();
1284                         owner->buffer()->gotoLabel(label.c_str());
1285                 }
1286         }
1287         break;
1288                 
1289         case LFUN_MENU_OPEN_BY_NAME:
1290                 owner->getMenus()->openByName(argument);
1291                 break; // RVDK_PATCH_5
1292                 
1293         case LFUN_SPELLCHECK:
1294                 if (lyxrc->isp_command != "none")
1295                         ShowSpellChecker();
1296                 break; // RVDK_PATCH_5
1297                 
1298                 // --- Cursor Movements -----------------------------
1299         case LFUN_RIGHT:
1300         {
1301                 LyXText * tmptext = owner->view()->text;
1302                 if(!tmptext->mark_set)
1303                         BeforeChange();
1304                 owner->view()->update(-2);
1305                 if (tmptext->cursor.pos < tmptext->cursor.par->Last()
1306                     && tmptext->cursor.par->GetChar(tmptext->cursor.pos)
1307                     == LyXParagraph::META_INSET
1308                     && tmptext->cursor.par->GetInset(tmptext->cursor.pos)
1309                     && tmptext->cursor.par->GetInset(tmptext->cursor.pos)->Editable() == 2){
1310                         Inset * tmpinset = tmptext->cursor.par->GetInset(tmptext->cursor.pos);
1311                         setMessage(tmpinset->EditMessage());
1312                         tmpinset->Edit(0, 0);
1313                         break;
1314                 }
1315                 tmptext->CursorRight();
1316                 owner->view()->text->FinishUndo();
1317                 moveCursorUpdate(false);
1318                 owner->getMiniBuffer()->Set(CurrentState());
1319         }
1320         break;
1321                 
1322         case LFUN_LEFT:
1323         {
1324                 // This is soooo ugly. Isn`t it possible to make
1325                 // it simpler? (Lgb)
1326                 LyXText * txt = owner->view()->text;
1327                 if(!txt->mark_set) BeforeChange();
1328                 owner->view()->update(-2);
1329                 txt->CursorLeft();
1330                 if (txt->cursor.pos < txt->cursor.par->Last()
1331                     && txt->cursor.par->GetChar(txt->cursor.pos)
1332                     == LyXParagraph::META_INSET
1333                     && txt->cursor.par->GetInset(txt->cursor.pos)
1334                     && txt->cursor.par->GetInset(txt->cursor.pos)->Editable() == 2) {
1335                         Inset * tmpinset = txt->cursor.par->GetInset(txt->cursor.pos);
1336                         setMessage(tmpinset->EditMessage());
1337                         tmpinset->Edit(tmpinset->Width(txt->GetFont(txt->cursor.par,
1338                                                                     txt->cursor.pos)), 0);
1339                         break;
1340                 }
1341                 owner->view()->text->FinishUndo();
1342                 moveCursorUpdate(false);
1343                 owner->getMiniBuffer()->Set(CurrentState());
1344         }
1345         break;
1346                 
1347         case LFUN_UP:
1348                 if(!owner->view()->text->mark_set) BeforeChange();
1349                 owner->view()->update(-3);
1350                 owner->view()->text->CursorUp();
1351                 owner->view()->text->FinishUndo();
1352                 moveCursorUpdate(false);
1353                 owner->getMiniBuffer()->Set(CurrentState());
1354                 break;
1355                 
1356         case LFUN_DOWN:
1357                 if(!owner->view()->text->mark_set)
1358                         BeforeChange();
1359                 owner->view()->update(-3);
1360                 owner->view()->text->CursorDown();
1361                 owner->view()->text->FinishUndo();
1362                 moveCursorUpdate(false);
1363                 owner->getMiniBuffer()->Set(CurrentState());
1364                 break;
1365
1366         case LFUN_UP_PARAGRAPH:
1367                 if(!owner->view()->text->mark_set)
1368                         BeforeChange();
1369                 owner->view()->update(-3);
1370                 owner->view()->text->CursorUpParagraph();
1371                 owner->view()->text->FinishUndo();
1372                 moveCursorUpdate(false);
1373                 owner->getMiniBuffer()->Set(CurrentState());
1374                 break;
1375                 
1376         case LFUN_DOWN_PARAGRAPH:
1377                 if(!owner->view()->text->mark_set)
1378                         BeforeChange();
1379                 owner->view()->update(-3);
1380                 owner->view()->text->CursorDownParagraph();
1381                 owner->view()->text->FinishUndo();
1382                 moveCursorUpdate(false);
1383                 owner->getMiniBuffer()->Set(CurrentState());
1384                 break;
1385                 
1386         case LFUN_PRIOR:
1387                 if(!owner->view()->text->mark_set)
1388                         BeforeChange();
1389                 owner->view()->update(-3);
1390                 owner->view()->cursorPrevious();
1391                 owner->view()->text->FinishUndo();
1392                 moveCursorUpdate(false);
1393                 owner->getMiniBuffer()->Set(CurrentState());
1394                 break;
1395                 
1396         case LFUN_NEXT:
1397                 if(!owner->view()->text->mark_set)
1398                         BeforeChange();
1399                 owner->view()->update(-3);
1400                 owner->view()->cursorNext();
1401                 owner->view()->text->FinishUndo();
1402                 moveCursorUpdate(false);
1403                 owner->getMiniBuffer()->Set(CurrentState());
1404                 break;
1405                 
1406         case LFUN_HOME:
1407                 if(!owner->view()->text->mark_set)
1408                         BeforeChange();
1409                 owner->view()->update(-2);
1410                 owner->view()->text->CursorHome();
1411                 owner->view()->text->FinishUndo();
1412                 moveCursorUpdate(false);
1413                 owner->getMiniBuffer()->Set(CurrentState());
1414                 break;
1415                 
1416         case LFUN_END:
1417                 if(!owner->view()->text->mark_set)
1418                         BeforeChange();
1419                 owner->view()->update(-2);
1420                 owner->view()->text->CursorEnd();
1421                 owner->view()->text->FinishUndo();
1422                 moveCursorUpdate(false);
1423                 owner->getMiniBuffer()->Set(CurrentState());
1424                 break;
1425                 
1426         case LFUN_TAB:
1427                 if(!owner->view()->text->mark_set)
1428                         BeforeChange();
1429                 owner->view()->update(-2);
1430                 owner->view()->text->CursorTab();
1431                 owner->view()->text->FinishUndo();
1432                 moveCursorUpdate(false);
1433                 owner->getMiniBuffer()->Set(CurrentState());
1434                 break;
1435                 
1436         case LFUN_WORDRIGHT:
1437                 if(!owner->view()->text->mark_set)
1438                         BeforeChange();
1439                 owner->view()->update(-2);
1440                 owner->view()->text->CursorRightOneWord();
1441                 owner->view()->text->FinishUndo();
1442                 moveCursorUpdate(false);
1443                 owner->getMiniBuffer()->Set(CurrentState());
1444                 break;
1445                 
1446         case LFUN_WORDLEFT:
1447                 if(!owner->view()->text->mark_set)
1448                         BeforeChange();
1449                 owner->view()->update(-2);
1450                 owner->view()->text->CursorLeftOneWord();
1451                 owner->view()->text->FinishUndo();
1452                 moveCursorUpdate(false);
1453                 owner->getMiniBuffer()->Set(CurrentState());
1454                 break;
1455                 
1456         case LFUN_BEGINNINGBUF:
1457                 if(!owner->view()->text->mark_set)
1458                         BeforeChange();
1459                 owner->view()->update(-2);
1460                 owner->view()->text->CursorTop();
1461                 owner->view()->text->FinishUndo();
1462                 moveCursorUpdate(false);
1463                 owner->getMiniBuffer()->Set(CurrentState());
1464                 break;
1465                 
1466         case LFUN_ENDBUF:
1467                 if(!owner->view()->text->mark_set)
1468                         BeforeChange();
1469                 owner->view()->update(-2);
1470                 owner->view()->text->CursorBottom();
1471                 owner->view()->text->FinishUndo();
1472                 moveCursorUpdate(false);
1473                 owner->getMiniBuffer()->Set(CurrentState());
1474                 break;
1475
1476       
1477                 /* cursor selection ---------------------------- */
1478         case LFUN_RIGHTSEL:
1479                 owner->view()->update(-2);
1480                 owner->view()->text->CursorRight();
1481                 owner->view()->text->FinishUndo();
1482                 moveCursorUpdate(true);
1483                 owner->getMiniBuffer()->Set(CurrentState());
1484                 break;
1485                 
1486         case LFUN_LEFTSEL:
1487                 owner->view()->update(-2);
1488                 owner->view()->text->CursorLeft();
1489                 owner->view()->text->FinishUndo();
1490                 moveCursorUpdate(true);
1491                 owner->getMiniBuffer()->Set(CurrentState());
1492                 break;
1493                 
1494         case LFUN_UPSEL:
1495                 owner->view()->update(-2);
1496                 owner->view()->text->CursorUp();
1497                 owner->view()->text->FinishUndo();
1498                 moveCursorUpdate(true);
1499                 owner->getMiniBuffer()->Set(CurrentState());
1500                 break;
1501                 
1502         case LFUN_DOWNSEL:
1503                 owner->view()->update(-2);
1504                 owner->view()->text->CursorDown();
1505                 owner->view()->text->FinishUndo();
1506                 moveCursorUpdate(true);
1507                 owner->getMiniBuffer()->Set(CurrentState());
1508                 break;
1509
1510         case LFUN_UP_PARAGRAPHSEL:
1511                 owner->view()->update(-2);
1512                 owner->view()->text->CursorUpParagraph();
1513                 owner->view()->text->FinishUndo();
1514                 moveCursorUpdate(true);
1515                 owner->getMiniBuffer()->Set(CurrentState());
1516                 break;
1517                 
1518         case LFUN_DOWN_PARAGRAPHSEL:
1519                 owner->view()->update(-2);
1520                 owner->view()->text->CursorDownParagraph();
1521                 owner->view()->text->FinishUndo();
1522                 moveCursorUpdate(true);
1523                 owner->getMiniBuffer()->Set(CurrentState());
1524                 break;
1525                 
1526         case LFUN_PRIORSEL:
1527                 owner->view()->update(-2);
1528                 owner->view()->cursorPrevious();
1529                 owner->view()->text->FinishUndo();
1530                 moveCursorUpdate(true);
1531                 owner->getMiniBuffer()->Set(CurrentState());
1532                 break;
1533                 
1534         case LFUN_NEXTSEL:
1535                 owner->view()->update(-2);
1536                 owner->view()->cursorNext();
1537                 owner->view()->text->FinishUndo();
1538                 moveCursorUpdate(true);
1539                 owner->getMiniBuffer()->Set(CurrentState());
1540                 break;
1541                 
1542         case LFUN_HOMESEL:
1543                 owner->view()->update(-2);
1544                 owner->view()->text->CursorHome();
1545                 owner->view()->text->FinishUndo();
1546                 moveCursorUpdate(true);
1547                 owner->getMiniBuffer()->Set(CurrentState());
1548                 break;
1549                 
1550         case LFUN_ENDSEL:
1551                 owner->view()->update(-2);
1552                 owner->view()->text->CursorEnd();
1553                 owner->view()->text->FinishUndo();
1554                 moveCursorUpdate(true);
1555                 owner->getMiniBuffer()->Set(CurrentState());
1556                 break;
1557                 
1558         case LFUN_WORDRIGHTSEL:
1559                 owner->view()->update(-2);
1560                 owner->view()->text->CursorRightOneWord();
1561                 owner->view()->text->FinishUndo();
1562                 moveCursorUpdate(true);
1563                 owner->getMiniBuffer()->Set(CurrentState());
1564                 break;
1565                 
1566         case LFUN_WORDLEFTSEL:
1567                 owner->view()->update(-2);
1568                 owner->view()->text->CursorLeftOneWord();
1569                 owner->view()->text->FinishUndo();
1570                 moveCursorUpdate(true);
1571                 owner->getMiniBuffer()->Set(CurrentState());
1572                 break;
1573                 
1574         case LFUN_BEGINNINGBUFSEL:
1575                 owner->view()->update(-2);
1576                 owner->view()->text->CursorTop();
1577                 owner->view()->text->FinishUndo();
1578                 moveCursorUpdate(true);
1579                 owner->getMiniBuffer()->Set(CurrentState());
1580                 break;
1581                 
1582         case LFUN_ENDBUFSEL:
1583                 owner->view()->update(-2);
1584                 owner->view()->text->CursorBottom();
1585                 owner->view()->text->FinishUndo();
1586                 moveCursorUpdate(true);
1587                 owner->getMiniBuffer()->Set(CurrentState());
1588                 break;
1589
1590                 // --- text changing commands ------------------------
1591         case LFUN_BREAKLINE:
1592                 BeforeChange();
1593                 owner->view()->text->InsertChar(LyXParagraph::META_NEWLINE);
1594                 SmallUpdate(1);
1595                 SetUpdateTimer(0.01);
1596                 moveCursorUpdate(false);
1597                 break;
1598                 
1599         case LFUN_PROTECTEDSPACE:
1600                 BeforeChange();
1601                 owner->view()->text->
1602                         InsertChar(LyXParagraph::META_PROTECTED_SEPARATOR);
1603                 SmallUpdate(1);
1604                 SetUpdateTimer();
1605                 moveCursorUpdate(false);
1606                 break;
1607                 
1608         case LFUN_SETMARK:
1609                 if(owner->view()->text->mark_set) {
1610                         BeforeChange();
1611                         owner->view()->update(0);
1612                         setMessage(N_("Mark removed"));
1613                 } else {
1614                         BeforeChange();
1615                         owner->view()->text->mark_set = 1;
1616                         owner->view()->update(0);
1617                         setMessage(N_("Mark set"));
1618                 }
1619                 owner->view()->text->sel_cursor = 
1620                         owner->view()->text->cursor;
1621                 break;
1622                 
1623         case LFUN_DELETE:
1624                 FreeUpdateTimer();
1625                 if (!owner->view()->text->selection) {
1626                         owner->view()->text->Delete();
1627                         owner->view()->text->sel_cursor = 
1628                                 owner->view()->text->cursor;
1629                         SmallUpdate(1);
1630                         // It is possible to make it a lot faster still
1631                         // just comment out the lone below...
1632                         owner->view()->getScreen()->ShowCursor();
1633                 } else {
1634                         CutCB();
1635                 }
1636                 SetUpdateTimer();
1637                 break;
1638
1639         case LFUN_DELETE_SKIP:
1640         {
1641                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
1642                 
1643                 LyXCursor cursor = owner->view()->text->cursor;
1644
1645                 FreeUpdateTimer();
1646                 if (!owner->view()->text->selection) {
1647                         if (cursor.pos == cursor.par->Last()) {
1648                                 owner->view()->text->CursorRight();
1649                                 cursor = owner->view()->text->cursor;
1650                                 if (cursor.pos == 0
1651                                     && !(cursor.par->added_space_top 
1652                                          == VSpace (VSpace::NONE))) {
1653                                         owner->view()->text->SetParagraph
1654                                                 (cursor.par->line_top,
1655                                                  cursor.par->line_bottom,
1656                                                  cursor.par->pagebreak_top, 
1657                                                  cursor.par->pagebreak_bottom,
1658                                                  VSpace(VSpace::NONE), 
1659                                                  cursor.par->added_space_bottom,
1660                                                  cursor.par->align, 
1661                                                  cursor.par->labelwidthstring, 0);
1662                                         owner->view()->text->CursorLeft();
1663                                         owner->view()->update (1);
1664                                 } else {
1665                                         owner->view()->text->CursorLeft();
1666                                         owner->view()->text->Delete();
1667                                         owner->view()->text->sel_cursor = 
1668                                                 owner->view()->text->cursor;
1669                                         SmallUpdate(1);
1670                                 }
1671                         } else {
1672                                 owner->view()->text->Delete();
1673                                 owner->view()->text->sel_cursor = 
1674                                         owner->view()->text->cursor;
1675                                 SmallUpdate(1);
1676                         }
1677                 } else {
1678                         CutCB();
1679                 }
1680                 SetUpdateTimer();
1681         }
1682         break;
1683
1684         /* -------> Delete word forward. */
1685         case LFUN_DELETE_WORD_FORWARD:
1686                 owner->view()->update(-2);
1687                 FreeUpdateTimer();
1688                 owner->view()->text->DeleteWordForward();
1689                 owner->view()->update( 1 );
1690                 SetUpdateTimer();
1691                 moveCursorUpdate(false);
1692                 break;
1693
1694                 /* -------> Delete word backward. */
1695         case LFUN_DELETE_WORD_BACKWARD:
1696                 owner->view()->update(-2);
1697                 FreeUpdateTimer();
1698                 owner->view()->text->DeleteWordBackward();
1699                 owner->view()->update( 1 );
1700                 SetUpdateTimer();
1701                 moveCursorUpdate(false);
1702                 break;
1703                 
1704                 /* -------> Kill to end of line. */
1705         case LFUN_DELETE_LINE_FORWARD:
1706                 FreeUpdateTimer();
1707                 owner->view()->update(-2);
1708                 owner->view()->text->DeleteLineForward();
1709                 owner->view()->update( 1 );
1710                 SetUpdateTimer();
1711                 moveCursorUpdate(false);
1712                 break;
1713                 
1714                 /* -------> Set mark off. */
1715         case LFUN_MARK_OFF:
1716                 BeforeChange();
1717                 owner->view()->update(0);
1718                 owner->view()->text->sel_cursor = 
1719                         owner->view()->text->cursor;
1720                 setMessage(N_("Mark off"));
1721                 break;
1722
1723                 /* -------> Set mark on. */
1724         case LFUN_MARK_ON:
1725                 BeforeChange();
1726                 owner->view()->text->mark_set = 1;
1727                 owner->view()->update( 0 );
1728                 owner->view()->text->sel_cursor = 
1729                         owner->view()->text->cursor;
1730                 setMessage(N_("Mark on"));
1731                 break;
1732                 
1733         case LFUN_BACKSPACE:
1734         {
1735                 FreeUpdateTimer();
1736                 if (!owner->view()->text->selection) {
1737                         if (owner->getIntl()->getTrans()->backspace()) {
1738                                 owner->view()->text->Backspace();
1739                                 owner->view()->text->sel_cursor = 
1740                                         owner->view()->text->cursor;
1741                                 SmallUpdate(1);
1742                                 // It is possible to make it a lot faster still
1743                                 // just comment out the lone below...
1744                                 owner->view()->getScreen()->ShowCursor();
1745                         }
1746                 } else {
1747                         CutCB();
1748                 }
1749                 SetUpdateTimer();
1750         }
1751         break;
1752
1753         case LFUN_BACKSPACE_SKIP:
1754         {
1755                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
1756                 
1757                 LyXCursor cursor = owner->view()->text->cursor;
1758                 
1759                 FreeUpdateTimer();
1760                 if (!owner->view()->text->selection) {
1761                         if (cursor.pos == 0 
1762                             && !(cursor.par->added_space_top 
1763                                  == VSpace (VSpace::NONE))) {
1764                                 owner->view()->text->SetParagraph 
1765                                         (cursor.par->line_top,      
1766                                          cursor.par->line_bottom,
1767                                          cursor.par->pagebreak_top, 
1768                                          cursor.par->pagebreak_bottom,
1769                                          VSpace(VSpace::NONE), cursor.par->added_space_bottom,
1770                                          cursor.par->align, 
1771                                          cursor.par->labelwidthstring, 0);
1772                                 owner->view()->update (1);
1773                         } else {
1774                                 owner->view()->text->Backspace();
1775                                 owner->view()->text->sel_cursor 
1776                                         = cursor;
1777                                 SmallUpdate (1);
1778                         }
1779                 } else
1780                         CutCB();
1781                 SetUpdateTimer();
1782         }
1783         break;
1784
1785         case LFUN_BREAKPARAGRAPH:
1786         {
1787                 BeforeChange();
1788                 owner->view()->text->BreakParagraph(0);
1789                 SmallUpdate(1);
1790                 SetUpdateTimer(0.01);
1791                 owner->view()->text->sel_cursor = 
1792                         owner->view()->text->cursor;
1793                 break;
1794         }
1795
1796         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1797         {
1798                 BeforeChange();
1799                 owner->view()->text->BreakParagraph(1);
1800                 SmallUpdate(1);
1801                 SetUpdateTimer(0.01);
1802                 owner->view()->text->sel_cursor = 
1803                         owner->view()->text->cursor;
1804                 break;
1805         }
1806         
1807         case LFUN_BREAKPARAGRAPH_SKIP:
1808         {
1809                 // When at the beginning of a paragraph, remove
1810                 // indentation and add a "defskip" at the top.
1811                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
1812                 
1813                 LyXCursor cursor = owner->view()->text->cursor;
1814                 
1815                 BeforeChange();
1816                 if (cursor.pos == 0) {
1817                         if (cursor.par->added_space_top == VSpace(VSpace::NONE)) {
1818                                 owner->view()->text->SetParagraph
1819                                         (cursor.par->line_top,      
1820                                          cursor.par->line_bottom,
1821                                          cursor.par->pagebreak_top, 
1822                                          cursor.par->pagebreak_bottom,
1823                                          VSpace(VSpace::DEFSKIP), cursor.par->added_space_bottom,
1824                                          cursor.par->align, 
1825                                          cursor.par->labelwidthstring, 1);
1826                                 owner->view()->update(1);
1827                         } 
1828                 }
1829                 else {
1830                         owner->view()->text->BreakParagraph(0);
1831                         SmallUpdate(1);
1832                 }
1833                 SetUpdateTimer(0.01);
1834                 owner->view()->text->sel_cursor = cursor;
1835         }
1836         break;
1837         
1838         case LFUN_QUOTE:
1839                 BeforeChange();
1840                 owner->view()->text->InsertChar('\"');  // This " matches the single quote in the code
1841                 SmallUpdate(1);
1842                 SetUpdateTimer();
1843                 moveCursorUpdate(false);
1844                 break;
1845
1846         case LFUN_HTMLURL:
1847         case LFUN_URL:
1848         {
1849                 InsetCommand * new_inset;
1850                 if (action == LFUN_HTMLURL)
1851                         new_inset = new InsetUrl("htmlurl", "", "");
1852                 else
1853                         new_inset = new InsetUrl("url", "", "");
1854                 owner->buffer()->insertInset(new_inset);
1855                 new_inset->Edit(0, 0);
1856         }
1857         break;
1858
1859         // --- lyxserver commands ----------------------------
1860
1861         case LFUN_CHARATCURSOR:
1862         {
1863                 LyXParagraph::size_type pos = 
1864                   owner->view()->text->cursor.pos;
1865                 if(pos < owner->view()->text->cursor.par->size())
1866                         dispatch_buffer = owner->view()->text->
1867                                 cursor.par->text[pos];
1868                 else
1869                         dispatch_buffer = "EOF";
1870         }
1871         break;
1872         
1873         case LFUN_GETXY:
1874                 dispatch_buffer = 
1875                          tostr(owner->view()->text->cursor.x) + ' '
1876                         + tostr(owner->view()->text->cursor.y);
1877                 break;
1878                 
1879         case LFUN_SETXY:
1880         {
1881                 int  x;
1882                 long y;
1883                 sscanf(argument.c_str(), " %d %ld", &x, &y);
1884                 owner->view()->text->SetCursorFromCoordinates(x, y);
1885         }
1886         break;
1887         
1888         case LFUN_GETLAYOUT:
1889                 dispatch_buffer =  
1890                         tostr(owner->view()->text->cursor.par->layout);
1891                 break;
1892                         
1893         case LFUN_GETFONT:
1894         {
1895                 LyXFont *font = &(owner->view()->text->current_font);
1896                 if(font->shape() == LyXFont::ITALIC_SHAPE)
1897                         dispatch_buffer = 'E';
1898                 else if(font->shape() == LyXFont::SMALLCAPS_SHAPE)
1899                         dispatch_buffer = 'N';
1900                 else
1901                         dispatch_buffer = '0';
1902
1903         }
1904         break;
1905
1906         case LFUN_GETLATEX:
1907         {
1908                 LyXFont *font = &(owner->view()->text->current_font);
1909                 if(font->latex() == LyXFont::ON)
1910                         dispatch_buffer = 'L';
1911                 else
1912                         dispatch_buffer = '0';
1913         }
1914         break;
1915
1916         case LFUN_GETNAME:
1917                 setMessage(owner->buffer()->fileName());
1918                 lyxerr.debug() << "FNAME["
1919                                << owner->buffer()->fileName()
1920                                << "] " << endl;
1921                 break;
1922                 
1923         case LFUN_NOTIFY:
1924         {
1925                 char buf[100];
1926                 keyseq.print(buf, 100);
1927                 dispatch_buffer = buf;
1928                 lyxserver->notifyClient(dispatch_buffer);
1929         }
1930         break;
1931
1932         case LFUN_GOTOFILEROW:
1933         {
1934                 char file_name[100];
1935                 int  row;
1936                 sscanf(argument.c_str(), " %s %d", file_name, &row);
1937
1938                 // Must replace extension of the file to be .lyx and get full path
1939                 string s = ChangeExtension(string(file_name), ".lyx", false);
1940
1941                 // Either change buffer or load the file
1942                 if (bufferlist.exists(s))
1943                         owner->view()->buffer(bufferlist.getBuffer(s));
1944                 else
1945                         owner->view()->buffer(bufferlist.loadLyXFile(s));
1946
1947                 // Set the cursor  
1948                 owner->buffer()->setCursorFromRow(row);
1949
1950                 // Recenter screen
1951                 BeforeChange();
1952                 if (owner->view()->text->cursor.y >
1953                     owner->view()->getWorkArea()->h / 2)        {
1954                         owner->view()->getScreen()->
1955                                 Draw(owner->view()->text->cursor.y -
1956                                      owner->view()->getWorkArea()->h/2);
1957                 } else { // <= 
1958                         owner->view()->getScreen()->
1959                                 Draw(0);
1960                 }
1961                 owner->view()->update(0);
1962                 owner->view()->redraw();
1963         }
1964         break;
1965
1966         case LFUN_APROPOS:
1967         case LFUN_GETTIP:
1968         {
1969                 int qa = lyxaction.LookupFunc(argument.c_str());
1970                 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1971         }
1972         break;
1973
1974         // --- accented characters ---------------------------
1975                 
1976         case LFUN_UMLAUT:
1977         case LFUN_CIRCUMFLEX:
1978         case LFUN_GRAVE:
1979         case LFUN_ACUTE:
1980         case LFUN_TILDE:
1981         case LFUN_CEDILLA:
1982         case LFUN_MACRON:
1983         case LFUN_DOT:
1984         case LFUN_UNDERDOT:
1985         case LFUN_UNDERBAR:
1986         case LFUN_CARON:
1987         case LFUN_SPECIAL_CARON:
1988         case LFUN_BREVE:
1989         case LFUN_TIE:
1990         case LFUN_HUNG_UMLAUT:
1991         case LFUN_CIRCLE:
1992         case LFUN_OGONEK:
1993         {
1994                 char c;
1995                 
1996                 if (keyseq.length == -1 && keyseq.getiso()!= 0) 
1997                         c= keyseq.getiso();
1998                 else
1999                         c= 0;
2000                 
2001                 owner->getIntl()->getTrans()->
2002                         deadkey(c, get_accent(action).accent, 
2003                                 owner->view()->text);
2004                 
2005                 // Need to reset, in case the minibuffer calls these
2006                 // actions
2007                 keyseq.reset();
2008                 keyseq.length= 0;
2009                 
2010                 // copied verbatim from do_accent_char
2011                 SmallUpdate(1);
2012                 SetUpdateTimer();
2013                 owner->view()->text->sel_cursor = 
2014                         owner->view()->text->cursor;
2015         }   
2016         break;
2017         
2018         // --- toolbar ----------------------------------
2019         case LFUN_PUSH_TOOLBAR:
2020         {
2021                 int nth = strToInt(argument);
2022                 if (lyxerr.debugging(Debug::TOOLBAR)) {
2023                         lyxerr << "LFUN_PUSH_TOOLBAR: argument = `"
2024                                << argument << "'\n"
2025                                << "LFUN_PUSH_TOOLBAR: nth = `"
2026                                << nth << "'" << endl;
2027                 }
2028                 
2029                 if (nth <= 0) {
2030                         LyXBell();
2031                         setErrorMessage(N_("Push-toolbar needs argument > 0"));
2032                 } else {
2033                         owner->getToolbar()->push(nth);
2034                 }
2035         }
2036         break;
2037         
2038         case LFUN_ADD_TO_TOOLBAR:
2039         {
2040                 if (lyxerr.debugging(Debug::TOOLBAR)) {
2041                         lyxerr << "LFUN_ADD_TO_TOOLBAR:"
2042                                 "argument = `" << argument << '\'' << endl;
2043                 }
2044                 string tmp(argument);
2045                 //lyxerr <<string("Argument: ") + argument);
2046                 //lyxerr <<string("Tmp     : ") + tmp);
2047                 if (tmp.empty()) {
2048                         LyXBell();
2049                         setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
2050                 } else {
2051                         owner->getToolbar()->add(argument, false);
2052                         owner->getToolbar()->set();
2053                 }
2054         }
2055         break;
2056         
2057         // --- insert characters ----------------------------------------
2058 #if 0
2059         case LFUN_INSERT_INSET_LATEX:
2060         {
2061                 Inset *new_inset = new InsetLatex(argument);
2062                 owner->buffer()->insertInset(new_inset);
2063         }
2064         break;
2065 #endif
2066         // ---  Mathed stuff. If we are here, there is no locked inset yet.
2067         
2068         // Greek mode     
2069         case LFUN_GREEK:
2070         {
2071                 if (!greek_kb_flag) {
2072                         greek_kb_flag = 1;
2073                         setMessage(N_("Math greek mode on"));
2074                 } else
2075                         greek_kb_flag = 0;
2076         }  
2077         break;
2078       
2079         // Greek keyboard      
2080         case LFUN_GREEK_TOGGLE:
2081         {
2082                 greek_kb_flag = (greek_kb_flag) ? 0: 2;
2083                 if (greek_kb_flag) {
2084                         setMessage(N_("Math greek keyboard on"));
2085                 } else {
2086                         setMessage(N_("Math greek keyboard off"));
2087                 }
2088         }
2089         break;
2090         
2091         case LFUN_MATH_DELIM:     
2092         case LFUN_INSERT_MATRIX:
2093         {          
2094                 if (owner->view()->available()) { 
2095                         owner->buffer()->
2096                                 open_new_inset(new InsetFormula(false));
2097                         owner->buffer()->
2098                                 the_locking_inset->LocalDispatch(action, argument.c_str());
2099                 }
2100         }          
2101         break;
2102                
2103         case LFUN_INSERT_MATH:
2104         {
2105                 math_insert_symbol(argument.c_str());
2106         }
2107         break;
2108         
2109         case LFUN_MATH_DISPLAY:
2110         {
2111                 if (owner->view()->available())
2112                         owner->buffer()->open_new_inset(new InsetFormula(true));
2113                 break;
2114         }
2115                     
2116         case LFUN_MATH_MACRO:
2117         {
2118                 if (owner->view()->available()) {
2119                         string s(argument);
2120                         if (s.empty())
2121                             setErrorMessage(N_("Missing argument"));
2122                         else {
2123                             string s1 = token(s, ' ', 1);
2124                             int na = s1.empty() ? 0: atoi(s1.c_str());
2125                             owner->buffer()->
2126                               open_new_inset(new InsetFormulaMacro(token(s, ' ', 0), na));
2127                         }
2128                 }
2129         }
2130         break;
2131
2132         case LFUN_MATH_MODE:   // Open or create a math inset
2133         {
2134                 
2135                 if (owner->view()->available())
2136                         owner->buffer()->open_new_inset(new InsetFormula);
2137                 setMessage(N_("Math editor mode"));
2138         }
2139         break;
2140           
2141         case LFUN_MATH_NUMBER:
2142         case LFUN_MATH_LIMITS:
2143         {
2144                 setErrorMessage(N_("This is only allowed in math mode!"));
2145         }
2146         break;
2147         
2148         case LFUN_INSERT_CITATION:
2149         {   
2150                 InsetCitation * new_inset = new InsetCitation();
2151                 // ale970405
2152                 // The note, if any, must be after the key, delimited
2153                 // by a | so both key and remark can have spaces.
2154                 if (!argument.empty()) {
2155                         string lsarg(argument);
2156                         if (contains(lsarg, "|")) {
2157                                 new_inset->setContents(token(lsarg, '|', 0));
2158                                 new_inset->setOptions(token(lsarg, '|', 1));
2159                         } else
2160                                 new_inset->setContents(lsarg);
2161                         owner->buffer()->insertInset(new_inset);
2162                 } else {
2163                         owner->buffer()->insertInset(new_inset);
2164                         new_inset->Edit(0, 0);
2165                 }
2166         }
2167         break;
2168                     
2169         case LFUN_INSERT_BIBTEX:
2170         {   
2171                 // ale970405+lasgoutt970425
2172                 // The argument can be up to two tokens separated 
2173                 // by a space. The first one is the bibstyle.
2174                 string lsarg(argument);
2175                 string bibstyle = token(lsarg, ' ', 1);
2176                 if (bibstyle.empty())
2177                         bibstyle = "plain";
2178                 InsetBibtex * new_inset 
2179                         = new InsetBibtex(token(lsarg, ' ', 0),
2180                                           bibstyle,
2181                                           owner->buffer());
2182                 
2183                 owner->buffer()->insertInset(new_inset);
2184                 if (lsarg.empty()) {
2185                         new_inset->Edit(0, 0);
2186                 }
2187         }
2188         break;
2189                 
2190         // BibTeX data bases
2191         case LFUN_BIBDB_ADD:
2192         {
2193                 InsetBibtex * inset = 
2194                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2195                 if (inset) {
2196                         inset->addDatabase(argument);
2197                 }
2198         }
2199         break;
2200                     
2201         case LFUN_BIBDB_DEL:
2202         {
2203                 InsetBibtex * inset = 
2204                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2205                 if (inset) {
2206                         inset->delDatabase(argument);
2207                 }
2208         }
2209         break;
2210         
2211         case LFUN_BIBTEX_STYLE:
2212         {
2213                 InsetBibtex * inset = 
2214                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2215                 if (inset) {
2216                         inset->setOptions(argument);
2217                 }
2218         }
2219         break;
2220                 
2221         case LFUN_INDEX_INSERT:
2222         case LFUN_INDEX_INSERT_LAST:
2223         {
2224                 // Can't do that at the beginning of a paragraph.
2225                 if (owner->view()->text->cursor.pos - 1 <0)
2226                         break;
2227
2228                 InsetIndex * new_inset = new InsetIndex();
2229                 if (!argument.empty()) {
2230                         string lsarg(argument);
2231                         new_inset->setContents(lsarg);
2232                         owner->buffer()->insertInset(new_inset);
2233                 } else {
2234                   //reh 98/09/21
2235                   //get the current word for an argument
2236                   LyXParagraph::size_type lastpos = 
2237                           owner->view()->text->cursor.pos - 1;
2238                   // Get the current word. note that this must be done
2239                   // before inserting the inset, or the inset will
2240                   // break the word
2241                   string curstring(owner->view()
2242                                    ->text->cursor.par->GetWord(lastpos));
2243
2244                   //make the new inset and write the current word into it
2245                   InsetIndex * new_inset = new InsetIndex();
2246
2247                   new_inset->setContents(curstring);
2248
2249                   //don't edit it if the call was to INSERT_LAST
2250                   if(action!= LFUN_INDEX_INSERT_LAST) {
2251                       new_inset->Edit(0, 0);
2252                   } else {
2253                       //it looks blank on the screen unless
2254                       //we do  something.  put it here.
2255
2256                       // move the cursor to the returned value of lastpos
2257                       // but only for the auto-insert
2258                       owner->view()->text->cursor.pos= lastpos;
2259                   }
2260
2261                   //put the new inset into the buffer.
2262                   // there should be some way of knowing the user
2263                   //cancelled & avoiding this, but i don't know how
2264                   owner->buffer()->insertInset(new_inset);
2265                 }
2266         }
2267         break;
2268
2269         case LFUN_INDEX_PRINT:
2270         {
2271                 Inset * new_inset = new InsetPrintIndex(owner->buffer());
2272                 owner->buffer()->insertInset(new_inset, "Standard", true);
2273         }
2274         break;
2275
2276         case LFUN_PARENTINSERT:
2277         {
2278                 lyxerr << "arg " << argument << endl;
2279                 Inset * new_inset = new InsetParent(argument, owner->buffer());
2280                 owner->buffer()->insertInset(new_inset, "Standard", true);
2281         }
2282         break;
2283
2284         case LFUN_CHILDINSERT:
2285         {
2286                 Inset * new_inset = new InsetInclude(argument,
2287                                                      owner->buffer());
2288                 owner->buffer()->insertInset(new_inset, "Standard", true);
2289                 new_inset->Edit(0, 0);
2290         }
2291         break;
2292
2293         case LFUN_CHILDOPEN:
2294         {
2295                 string filename =
2296                         MakeAbsPath(argument, 
2297                                     OnlyPath(owner->buffer()->fileName()));
2298                 setMessage(N_("Opening child document ") +
2299                            MakeDisplayPath(filename) + "...");
2300                 owner->view()->savePosition();
2301                 if (bufferlist.exists(filename))
2302                   owner->view()->buffer(bufferlist.getBuffer(filename));
2303                 else
2304                   owner->view()->buffer(bufferlist.loadLyXFile(filename));
2305         }
2306         break;
2307
2308         case LFUN_INSERT_NOTE:
2309                 NoteCB();
2310                 break;
2311                 
2312         case LFUN_INSERTFOOTNOTE: 
2313         {
2314                 LyXParagraph::footnote_kind kind;
2315                 if (argument == "footnote")
2316                         { kind = LyXParagraph::FOOTNOTE; }
2317                 else if (argument == "margin")
2318                         { kind = LyXParagraph::MARGIN; }
2319                 else if (argument == "figure")
2320                         { kind = LyXParagraph::FIG; }
2321                 else if (argument == "table")
2322                         { kind = LyXParagraph::TAB; }
2323                 else if (argument == "wide-fig")
2324                         { kind = LyXParagraph::WIDE_FIG; }
2325                 else if (argument == "wide-tab")
2326                         { kind = LyXParagraph::WIDE_TAB; }
2327                 else if (argument == "algorithm")
2328                         { kind = LyXParagraph::ALGORITHM; }
2329                 else {
2330                         setErrorMessage(N_("Unknown kind of footnote"));
2331                         break;
2332                 }
2333                 owner->view()->text->InsertFootnoteEnvironment(kind);
2334                 owner->view()->update(1);
2335         }
2336         break;
2337         
2338         case LFUN_BUFFERBULLETSSELECT:
2339                 bulletForm();
2340                 break;
2341                 
2342         case LFUN_TOGGLECURSORFOLLOW:
2343                 cursor_follows_scrollbar = !cursor_follows_scrollbar;
2344                 break;
2345                 
2346         case LFUN_KMAP_OFF:             // keymap off
2347                 owner->getIntl()->KeyMapOn(false);
2348                 break;
2349                 
2350         case LFUN_KMAP_PRIM:    // primary keymap
2351                 owner->getIntl()->KeyMapPrim();
2352                 break;
2353                 
2354         case LFUN_KMAP_SEC:             // secondary keymap
2355                 owner->getIntl()->KeyMapSec();
2356                 break;
2357                 
2358         case LFUN_KMAP_TOGGLE:  // toggle keymap
2359                 owner->getIntl()->ToggleKeyMap();
2360                 break;
2361
2362         case LFUN_SELFINSERT:
2363         {
2364                 for (string::size_type i = 0; i < argument.length(); ++i) {
2365                         owner->view()->text->InsertChar(argument[i]);
2366                         // This needs to be in the loop, or else we
2367                         // won't break lines correctly. (Asger)
2368                         SmallUpdate(1);
2369                 }
2370                 SetUpdateTimer();
2371                 owner->view()->text->sel_cursor = 
2372                         owner->view()->text->cursor;
2373                 moveCursorUpdate(false);
2374         }
2375         break;
2376
2377         case LFUN_SEQUENCE: 
2378         {
2379                 // argument contains ';'-terminated commands
2380                 while (argument.find(';') != string::npos) {
2381                         string first;
2382                         argument = split(argument, first, ';');
2383                         Dispatch(first);
2384                 }
2385         }
2386         break;
2387
2388         case LFUN_SAVEPREFERENCES:
2389         {
2390                 Path p(user_lyxdir);
2391                 lyxrc->write("preferences");
2392         }
2393         break;
2394         
2395         case LFUN_UNKNOWN_ACTION:
2396         {
2397                 if (owner->buffer()->isReadonly()) {
2398                         LyXBell();
2399                         setErrorMessage(N_("Document is read only"));
2400                         break;
2401                 }
2402                          
2403                 if (!argument.empty()) {
2404                         
2405                         /* Automatically delete the currently selected
2406                          * text and replace it with what is being
2407                          * typed in now. Depends on lyxrc settings
2408                          * "auto_region_delete", which defaults to
2409                          * true (on). */
2410                 
2411                         if ( lyxrc->auto_region_delete ) {
2412                                 if (owner->view()->text->selection){
2413                                         owner->view()->text->CutSelection(false);
2414                                         owner->view()->update(-1);
2415                                 }
2416                         }
2417                         
2418                         BeforeChange();
2419                         for (string::size_type i = 0;
2420                              i < argument.length(); ++i) {
2421                                 if (greek_kb_flag) {
2422                                         if (!math_insert_greek(argument[i]))
2423                                                 owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2424                                 } else
2425                                         owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2426                         }
2427                         
2428                         SmallUpdate(1);
2429                         SetUpdateTimer();
2430
2431                         owner->view()->text->sel_cursor = 
2432                                 owner->view()->text->cursor;
2433                         moveCursorUpdate(false);
2434                         return string();
2435                 } else {
2436                         // why is an "Unknown action" with empty
2437                         // argument even dispatched in the first
2438                         // place? I`ll probably change that. (Lgb)
2439                         LyXBell();
2440                         setErrorMessage(N_("Unknown action"));
2441                 }
2442                 break;
2443         default:
2444                 lyxerr << "A truly unknown func!" << endl;
2445                 break;
2446         }
2447         } // end of switch
2448   exit_with_message:
2449
2450         string res = getMessage();
2451
2452         if (res.empty()) {
2453                 if (!commandshortcut.empty()) {
2454                         string newbuf = owner->getMiniBuffer()->GetText();
2455                         if (newbuf != commandshortcut) {
2456                                 owner->getMiniBuffer()->Set(newbuf
2457                                                             + " " +
2458                                                             commandshortcut);
2459                         }
2460                 }
2461         } else {
2462                 owner->getMiniBuffer()->Set(string(_(res.c_str()))
2463                                             + " " + commandshortcut);
2464         }
2465
2466         return res;
2467 }
2468 #else
2469 string LyXFunc::Dispatch(int ac,
2470                           char const * do_not_use_this_arg)
2471 {
2472         string argument;
2473         kb_action action;
2474         
2475         FL_OBJECT * ob = 0;  // This will disapear soon
2476     
2477         // we have not done anything wrong yet.
2478         errorstat = false;
2479         dispatch_buffer.clear();
2480         
2481         // if action is a pseudo-action, we need the real action
2482         if (lyxaction.isPseudoAction(ac)) {
2483                 string tmparg;
2484                 action = static_cast<kb_action>
2485                         (lyxaction.retrieveActionArg(ac, tmparg));
2486                 if (!tmparg.empty())
2487                         argument = tmparg;
2488         } else {
2489                 action = static_cast<kb_action>(ac);
2490                 if (do_not_use_this_arg)
2491                         argument = do_not_use_this_arg; // except here
2492         }
2493     
2494         selection_possible = false;
2495         
2496         if (owner->view()->available() 
2497             && owner->view()->getScreen())
2498                 owner->view()->getScreen()->HideCursor();
2499
2500         // We cannot use this function here
2501         if (getStatus(action) & Disabled)
2502                 goto exit_with_message;
2503
2504         commandshortcut.clear();
2505         
2506         if (lyxrc->display_shortcuts && show_sc) {
2507                 if (action != LFUN_SELFINSERT) {
2508                         // Put name of command and list of shortcuts
2509                         // for it in minibuffer
2510                         string comname = lyxaction.getActionName(action);
2511
2512                         int pseudoaction = action;
2513                         bool argsadded = false;
2514
2515                         if (!argument.empty()) {
2516                                 // If we have the command with argument, 
2517                                 // this is better
2518                                 pseudoaction = 
2519                                         lyxaction.searchActionArg(action,
2520                                                                   argument.c_str());
2521
2522                                 if (pseudoaction == -1) {
2523                                         pseudoaction = action;
2524                                 } else {
2525                                         comname += " " + argument;
2526                                         argsadded = true;
2527                                 }
2528                         }
2529
2530                         string shortcuts = toplevel_keymap->findbinding(pseudoaction);
2531
2532                         if (!shortcuts.empty()) {
2533                                 comname += ": " + shortcuts;
2534                         } else if (!argsadded) {
2535                                 comname += " " + argument;
2536                         }
2537
2538                         if (!comname.empty()) {
2539                                 comname = strip(comname);
2540                                 commandshortcut = "(" + comname + ')';
2541                                 owner->getMiniBuffer()->Set(commandshortcut);
2542                                 // Here we could even add a small pause,
2543                                 // to annoy the user and make him learn
2544                                 // the shortcuts.
2545                                 // No! That will just annoy, not teach
2546                                 // anything. The user will read the messages
2547                                 // if they are interested. (Asger)
2548                         }
2549                 }
2550         }
2551
2552         // If in math mode pass the control to
2553         // the math inset [asierra060396]
2554         if (owner->view()->available() &&
2555             owner->buffer()->the_locking_inset) {
2556                 if (action > 1
2557                     || (action == LFUN_UNKNOWN_ACTION && keyseq.length>= -1)) {
2558                         if (action == LFUN_UNKNOWN_ACTION && argument.empty()) {
2559                                 argument = keyseq.getiso();
2560                         }
2561                         // Undo/Redo pre 0.13 is a bit tricky for insets.
2562                         if (action == LFUN_UNDO) {
2563                                 int slx, sly;
2564                                 UpdatableInset * inset = 
2565                                         owner->buffer()->the_locking_inset;
2566                                 inset->GetCursorPos(slx, sly);
2567                                 UnlockInset(inset);
2568                                 MenuUndo();
2569                                 inset = static_cast<UpdatableInset*>(owner->buffer()->text->cursor.par->GetInset(owner->buffer()->text->cursor.pos));
2570                                 if (inset) 
2571                                         inset->Edit(slx, sly);
2572                                 return string();
2573                         } else 
2574                                 if (action == LFUN_REDO) {
2575                                         int slx, sly;
2576                                         UpdatableInset * inset = owner->buffer()->the_locking_inset;
2577                                         inset->GetCursorPos(slx, sly);
2578                                         UnlockInset(inset);
2579                                         MenuRedo();
2580                                         inset = static_cast<UpdatableInset*>(owner->buffer()->text->cursor.par->GetInset(owner->buffer()->text->cursor.pos));
2581                                         if (inset)
2582                                                 inset->Edit(slx, sly);
2583                                         return string();
2584                                 } else
2585                                         if (owner->buffer()->the_locking_inset->LocalDispatch(action, argument.c_str()))
2586                                                 return string();
2587                                         else {
2588                                                 setMessage(N_("Text mode"));
2589                                                 if (action == LFUN_RIGHT || action == -1)
2590                                                         owner->buffer()->text->CursorRight();
2591                                                 if (action == LFUN_LEFT || action == LFUN_RIGHT)
2592                                                         return string();
2593                                         }
2594                 }
2595         }
2596
2597         switch(action) {
2598                 // --- Misc -------------------------------------------
2599         case LFUN_WORDFINDFORWARD  : 
2600         case LFUN_WORDFINDBACKWARD : {
2601                 static string last_search;
2602                 string searched_string;
2603             
2604                 if (!argument.empty()) {
2605                         last_search = argument;
2606                         searched_string = argument;
2607                 } else {
2608                         searched_string = last_search;
2609                 }
2610
2611                 LyXText * ltCur = owner->view()->buffer()->text ;
2612
2613                 if (!searched_string.empty() &&
2614                     ((action == LFUN_WORDFINDBACKWARD) ? 
2615                      ltCur->SearchBackward(searched_string.c_str()) :
2616                      ltCur->SearchForward(searched_string.c_str()))) {
2617
2618                         // ??? What is that ???
2619                         owner->view()->buffer()->update(-2);
2620
2621                         // ??? Needed ???
2622                         // clear the selection (if there is any) 
2623                         owner->view()->getScreen()->ToggleSelection();
2624                         owner->view()->buffer()->text->ClearSelection();
2625
2626                         // Move cursor so that successive C-s 's will not stand in place. 
2627                         if( action == LFUN_WORDFINDFORWARD ) 
2628                                 owner->buffer()->text->CursorRightOneWord();
2629                         owner->buffer()->text->FinishUndo();
2630                         moveCursorUpdate(false);
2631
2632                         // ??? Needed ???
2633                         // set the new selection 
2634                         // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
2635                         owner->view()->getScreen()->ToggleSelection(false);
2636                 } else 
2637                         LyXBell();      
2638          
2639                 // REMOVED : if (owner->view()->getWorkArea()->focus)
2640                 owner->view()->getScreen()->ShowCursor();
2641         }
2642         break;
2643
2644         case LFUN_PREFIX:
2645         {
2646                 if (owner->view()->available()
2647                     && owner->view()->getScreen()) {
2648                         owner->buffer()->update(-2);
2649                 }
2650                 char buf[100];
2651                 keyseq.print(buf, 100, true);
2652                 owner->getMiniBuffer()->Set(buf, string(), string(), 1);
2653         }
2654         break;
2655
2656         // --- Misc -------------------------------------------
2657         case LFUN_EXEC_COMMAND:
2658                 owner->getMiniBuffer()->ExecCommand(); 
2659                 break;
2660                 
2661         case LFUN_CANCEL:                   // RVDK_PATCH_5
2662                 keyseq.reset();
2663                 meta_fake_bit = 0;
2664                 if(owner->view()->available())
2665                         // cancel any selection
2666                         Dispatch(LFUN_MARK_OFF, 0);
2667                 setMessage(N_("Cancel"));
2668                 break;
2669
2670         case LFUN_META_FAKE:                                 // RVDK_PATCH_5
2671         {
2672                 meta_fake_bit = Mod1Mask;
2673                 char buf[100];
2674                 keyseq.print(buf, 98, true);
2675                 string res = string("M-") + buf;
2676                 setMessage(buf); // RVDK_PATCH_5
2677         }
2678         break;  
2679
2680         case LFUN_READ_ONLY_TOGGLE:
2681                 if (owner->buffer()->lyxvc.inUse()) {
2682                         owner->buffer()->lyxvc.toggleReadOnly();
2683                 } else {
2684                         owner->buffer()->setReadonly(
2685                                 !owner->buffer()->isReadonly());
2686                 }
2687                 break;
2688                 
2689         case LFUN_CENTER: // this is center and redraw.
2690                 BeforeChange();
2691                 if (owner->buffer()->text->cursor.y >
2692                     owner->view()->getWorkArea()->h / 2)        {
2693                         owner->view()->getScreen()->
2694                                 Draw(owner->buffer()->text->cursor.y -
2695                                      owner->view()->getWorkArea()->h/2);
2696                 } else { // <= 
2697                         owner->view()->getScreen()->
2698                                 Draw(0);
2699                 }
2700                 owner->buffer()->update(0);
2701                 owner->view()->redraw();
2702                 break;
2703                 
2704         case LFUN_APPENDIX:
2705                 if (owner->view()->available()) {
2706                         owner->buffer()->text->toggleAppendix();
2707                         owner->buffer()->update(1);
2708                 }
2709                 break;
2710
2711         // --- Menus -----------------------------------------------
2712         case LFUN_MENUNEW:
2713                 MenuNew(false);
2714                 break;
2715                 
2716         case LFUN_MENUNEWTMPLT:
2717                 MenuNew(true);
2718                 break;
2719                 
2720         case LFUN_MENUOPEN:
2721                 MenuOpen();
2722                 break;
2723                 
2724         case LFUN_CLOSEBUFFER:
2725                 CloseBuffer();
2726                 break;
2727                 
2728         case LFUN_MENUWRITE:
2729                 MenuWrite(owner->buffer());
2730                 break;
2731                 
2732         case LFUN_MENUWRITEAS:
2733                 MenuWriteAs(owner->buffer());
2734                 break;
2735                 
2736         case LFUN_MENURELOAD:
2737                 reloadBuffer();
2738                 break;
2739                 
2740         case LFUN_PREVIEW:
2741                 MenuPreview(owner->buffer());
2742                 break;
2743                         
2744         case LFUN_PREVIEWPS:
2745                 MenuPreviewPS(owner->buffer());
2746                 break;
2747                 
2748         case LFUN_RUNLATEX:
2749                 MenuRunLaTeX(owner->buffer());
2750                 break;
2751                 
2752         case LFUN_BUILDPROG:
2753                 MenuBuildProg(owner->buffer());
2754                 break;
2755                 
2756         case LFUN_RUNCHKTEX:
2757                 MenuRunChktex(owner->buffer());
2758                 break;
2759                 
2760         case LFUN_RUNDVIPS:
2761                 MenuRunDvips(owner->buffer(), false);
2762                 break;
2763                 
2764         case LFUN_MENUPRINT:
2765                 MenuPrint(owner->buffer());
2766                 break;
2767                 
2768         case LFUN_FAX:
2769                 MenuFax(owner->buffer());
2770                 break;
2771                         
2772         case LFUN_EXPORT:
2773         {
2774                 //needs argument as string
2775                 string extyp = argument;
2776                 
2777                 // latex
2778                 if (extyp == "latex") {
2779                         // make sure that this buffer is not linuxdoc
2780                         MenuMakeLaTeX(owner->buffer());
2781                 }
2782                 // linuxdoc
2783                 else if (extyp == "linuxdoc") {
2784                         // make sure that this buffer is not latex
2785                         MenuMakeLinuxDoc(owner->buffer());
2786                 }
2787                 // docbook
2788                 else if (extyp == "docbook") {
2789                         // make sure that this buffer is not latex or linuxdoc
2790                         MenuMakeDocBook(owner->buffer());
2791                 }
2792                 // dvi
2793                 else if (extyp == "dvi") {
2794                         // Run LaTeX as "Update dvi..." Bernhard.
2795                         // We want the dvi in the current directory. This
2796                         // is achieved by temporarily disabling use of
2797                         // temp directory. As a side-effect, we get
2798                         // *.log and *.aux files also. (Asger)
2799                         bool flag = lyxrc->use_tempdir;
2800                         lyxrc->use_tempdir = false;
2801                         MenuRunLaTeX(owner->buffer());
2802                         lyxrc->use_tempdir = flag;
2803                 }
2804                 // postscript
2805                 else if (extyp == "postscript") {
2806                         // Start Print-dialog. Not as good as dvi... Bernhard.
2807                         MenuPrint(owner->buffer());
2808                         // Since the MenuPrint is a pop-up, we can't use
2809                         // the same trick as above. (Asger)
2810                         // MISSING: Move of ps-file :-(
2811                 }
2812                 // ascii
2813                 else if (extyp == "ascii") {
2814                         MenuMakeAscii(owner->buffer());
2815                 }
2816                 else if (extyp == "custom") {
2817                         MenuSendto();
2818                         break;
2819                 }
2820                 // HTML
2821                 else if (extyp == "html" && lyxrc->html_command != "none") {
2822                         // First, create LaTeX file
2823                         MenuMakeLaTeX(owner->buffer());
2824
2825                         // And now, run the converter
2826                         string file = owner->buffer()->getFileName();
2827                         Path path(OnlyPath(file));
2828                         // the tex file name has to be correct for
2829                         // latex, but the html file name can be
2830                         // anything.
2831                         string result = ChangeExtension(file, ".html", false);
2832                         string infile = owner->buffer()->getLatexName();
2833                         string tmp = lyxrc->html_command;
2834                         tmp = subst(tmp, "$$FName", infile);
2835                         tmp = subst(tmp, "$$OutName", result);
2836                         Systemcalls one;
2837                         int res = one.startscript(Systemcalls::System, tmp);
2838                         if (res == 0) {
2839                                 setMessage(N_("Document exported as HTML to file `")
2840                                            + MakeDisplayPath(result) +'\'');
2841                         } else {
2842                                 setErrorMessage(N_("Unable to convert to HTML the file `")
2843                                                 + MakeDisplayPath(infile) 
2844                                                 + '\'');
2845                         }
2846                 }
2847                 else {
2848                         setErrorMessage(N_("Unknown export type: ")
2849                                         + extyp);
2850                 }
2851         }
2852         break;
2853
2854         case LFUN_IMPORT:
2855         {
2856                 //needs argument as string
2857                 string imtyp = argument;
2858                 
2859                 // latex
2860                 if (imtyp == "latex") {
2861                         doImportLaTeX(false);
2862                 }
2863                 // ascii
2864                 else if (imtyp == "ascii") {
2865                         doImportASCII(false);
2866                 } else if (imtyp == "asciiparagraph") {
2867                         doImportASCII(true);
2868                 // noweb
2869                 } else if (imtyp == "noweb") {
2870                         doImportLaTeX(true);
2871                 } else {
2872                         setErrorMessage(string(N_("Unknown import type: "))
2873                                         + imtyp);
2874                 }
2875                 break;
2876         }
2877                 
2878         case LFUN_QUIT:
2879                 QuitLyX();
2880                 break;
2881                 
2882         case LFUN_TOCVIEW:
2883                 TocUpdateCB(ob, 0);
2884                 if (fd_form_toc->form_toc->visible) {
2885                         fl_raise_form(fd_form_toc->form_toc);
2886                 } else {
2887                         static int ow = -1, oh;
2888                         fl_show_form(fd_form_toc->form_toc,
2889                                      FL_PLACE_MOUSE |
2890                                      FL_FREE_SIZE, FL_FULLBORDER,
2891                                      _("Table of Contents"));
2892                         if (ow < 0) {
2893                                 ow = fd_form_toc->form_toc->w;
2894                                 oh = fd_form_toc->form_toc->h;
2895                         }
2896                         fl_set_form_minsize(fd_form_toc->form_toc, ow, oh);
2897                 }
2898                 break;
2899                 
2900         case LFUN_TOC_INSERT:
2901         {
2902                 Inset * new_inset = new InsetTOC(owner->buffer());
2903                 owner->buffer()->insertInset(new_inset, "Standard", true);
2904                 break;
2905         }
2906         
2907         case LFUN_LOF_INSERT:
2908         {
2909                 Inset * new_inset = new InsetLOF(owner->buffer());
2910                 owner->buffer()->insertInset(new_inset, "Standard", true);
2911                 break;
2912         }
2913         
2914         case LFUN_LOA_INSERT:
2915         {
2916                 Inset * new_inset = new InsetLOA(owner->buffer());
2917                 owner->buffer()->insertInset(new_inset, "Standard", true);
2918                 break;
2919         }
2920
2921         case LFUN_LOT_INSERT:
2922         {
2923                 Inset * new_inset = new InsetLOT(owner->buffer());
2924                 owner->buffer()->insertInset(new_inset, "Standard", true);
2925                 break;
2926         }
2927                 
2928         case LFUN_TABLE:
2929                 TableCB(ob, 0);
2930                 break;
2931                 
2932         case LFUN_FIGURE:
2933                 FigureCB(ob, 0);
2934                 break;
2935                 
2936         case LFUN_AUTOSAVE:
2937                 AutoSave();
2938                 break;
2939                 
2940         case LFUN_UNDO:
2941                 MenuUndo();
2942                 break;
2943                 
2944         case LFUN_REDO:
2945                 MenuRedo();
2946                 break;
2947                 
2948         case LFUN_MENUSEARCH:
2949                 MenuSearch();
2950                 break;
2951                 
2952         case LFUN_PASTE:
2953                 PasteCB();
2954                 break;
2955                 
2956         case LFUN_PASTESELECTION:
2957         {
2958                 bool asPara = false;
2959                 if (argument == "paragraph") asPara = true;
2960                 MenuPasteSelection(asPara);
2961                 break;
2962         }
2963
2964         case LFUN_CUT:
2965                 CutCB();
2966                 break;
2967                 
2968         case LFUN_COPY:
2969                 CopyCB();
2970                 break;
2971                 
2972         case LFUN_LAYOUT_COPY:
2973                 CopyEnvironmentCB();
2974                 break;
2975                 
2976         case LFUN_LAYOUT_PASTE:
2977                 PasteEnvironmentCB();
2978                 break;
2979                 
2980         case LFUN_GOTOERROR:
2981                 owner->view()->gotoError();
2982                 break;
2983                 
2984         case LFUN_REMOVEERRORS:
2985                 if (owner->buffer()->removeAutoInsets()) {
2986                         owner->view()->redraw();
2987                         owner->view()->fitCursor();
2988                         owner->view()->updateScrollbar();
2989                 }
2990                 break;
2991                 
2992         case LFUN_GOTONOTE:
2993                 GotoNote();
2994                 break;
2995                 
2996         case LFUN_OPENSTUFF:
2997                 OpenStuff();
2998                 break;
2999                 
3000         case LFUN_HYPHENATION:
3001                 HyphenationPoint();
3002                 break;
3003                 
3004         case LFUN_LDOTS:
3005                 Ldots();
3006                 break;
3007                 
3008         case LFUN_END_OF_SENTENCE:
3009                 EndOfSentenceDot();
3010                 break;
3011
3012         case LFUN_MENU_SEPARATOR:
3013                 MenuSeparator();
3014                 break;
3015                 
3016         case LFUN_HFILL:
3017                 HFill();
3018                 break;
3019                 
3020         case LFUN_DEPTH:
3021                 DepthCB(ob, 0);
3022                 break;
3023                 
3024         case LFUN_DEPTH_MIN:
3025                 DepthCB(ob, -1);
3026                 break;
3027                 
3028         case LFUN_DEPTH_PLUS:
3029                 DepthCB(ob, 1);
3030                 break;
3031                 
3032         case LFUN_FREE:
3033                 FreeCB();
3034                 break;
3035                 
3036         case LFUN_TEX:
3037                 TexCB();
3038                 break;
3039                 
3040         case LFUN_MELT:
3041                 MeltCB(ob, 0);
3042                 break;
3043                 
3044         case LFUN_RECONFIGURE:
3045                 Reconfigure();
3046                 break;
3047
3048         case LFUN_FOOTMELT:
3049                 if (owner->view()->available()
3050                     && !owner->buffer()->text->selection
3051                     && owner->buffer()->text->cursor.par->footnoteflag
3052                     != LyXParagraph::NO_FOOTNOTE)
3053                 { // only melt footnotes with FOOTMELT, not margins etc
3054                   if(owner->buffer()->text->cursor.par->footnotekind == LyXParagraph::FOOTNOTE)
3055                         MeltCB(ob, 0);
3056                 }
3057                 else
3058                         FootCB(ob, 0); 
3059                 break;
3060
3061         case LFUN_MARGINMELT:
3062                 if (owner->view()->available()
3063                     && !owner->buffer()->text->selection
3064                     && owner->buffer()->text->cursor.par->footnoteflag
3065                     != LyXParagraph::NO_FOOTNOTE) {
3066                         // only melt margins
3067                         if(owner->buffer()->text->cursor.par->footnotekind == LyXParagraph::MARGIN)
3068                                 MeltCB(ob, 0);
3069                 }
3070                 else
3071                         MarginCB(ob, 0); 
3072                 break;
3073                 
3074                 // --- version control -------------------------------
3075         case LFUN_VC_REGISTER:
3076         {
3077                 if (!owner->buffer()->lyxvc.inUse())
3078                         owner->buffer()->lyxvc.registrer();
3079         }
3080         break;
3081                 
3082         case LFUN_VC_CHECKIN:
3083         {
3084                 if (owner->buffer()->lyxvc.inUse()
3085                     && !owner->buffer()->isReadonly())
3086                         owner->buffer()->lyxvc.checkIn();
3087         }
3088         break;
3089                 
3090         case LFUN_VC_CHECKOUT:
3091         {
3092                 if (owner->buffer()->lyxvc.inUse()
3093                     && owner->buffer()->isReadonly())
3094                         owner->buffer()->lyxvc.checkOut();
3095         }
3096         break;
3097         
3098         case LFUN_VC_REVERT:
3099         {
3100                 owner->buffer()->lyxvc.revert();
3101         }
3102         break;
3103                 
3104         case LFUN_VC_UNDO:
3105         {
3106                 owner->buffer()->lyxvc.undoLast();
3107         }
3108         break;
3109                 
3110         case LFUN_VC_HISTORY:
3111         {
3112                 owner->buffer()->lyxvc.showLog();
3113                 break;
3114         }
3115         
3116         // --- buffers ----------------------------------------
3117         case LFUN_PREVBUFFER:
3118 #ifdef WITH_WARNINGS
3119 #warning fix this please
3120 #endif
3121                 // it is the LyXView or the BufferView that should
3122                 // remember the previous buffer, not bufferlist.
3123 //                      if (owner->view()->available()){          
3124 //                              BeforeChange();
3125 //                              owner->buffer()->update(-2);
3126 //                      }
3127 //                      owner->view()->setBuffer(bufferlist.prev());
3128
3129 //                      owner->view()->
3130 //                              resizeCurrentBufferPseudoExpose();
3131                 break;
3132                         
3133         case LFUN_FILE_INSERT:
3134         {
3135                 MenuInsertLyXFile(argument);
3136         }
3137         break;
3138         
3139         case LFUN_FILE_INSERT_ASCII:
3140         {
3141                 bool asPara = (argument == "paragraph");
3142                 InsertAsciiFile(string(), asPara);
3143         }
3144         break;
3145         
3146         case LFUN_FILE_NEW:
3147         {
3148                 // servercmd: argument must be <file>:<template>
3149                 Buffer * tmpbuf = NewLyxFile(argument);
3150                 if (tmpbuf)
3151                         owner->view()->buffer(tmpbuf);
3152         }
3153                 break;
3154                         
3155         case LFUN_FILE_OPEN:
3156                 owner->view()->buffer(
3157                         bufferlist.loadLyXFile(argument));
3158                 break;
3159
3160         case LFUN_LATEX_LOG:
3161                 ShowLatexLog();
3162                 break;
3163                 
3164         case LFUN_LAYOUTNO:
3165         {
3166                 lyxerr.debug() << "LFUN_LAYOUTNO: (arg) " << argument << endl;
3167                 int sel = strToInt(argument);
3168                 lyxerr.debug() << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
3169                 
3170                 // Should this give a setMessage instead?
3171                 if (sel == 0) 
3172                         return string(); // illegal argument
3173
3174                 sel--; // sel 1..., but layout 0...
3175
3176                 // Pretend we got the name instead.
3177                 Dispatch(int(LFUN_LAYOUT), 
3178                          textclasslist.NameOfLayout(owner->buffer()->
3179                                                text->parameters->
3180                                                textclass,
3181                                                sel).c_str());
3182                 return string();
3183         }
3184                 
3185         case LFUN_LAYOUT:
3186         {
3187                 lyxerr.debug() << "LFUN_LAYOUT: (arg) "
3188                                << argument << endl;
3189                 
3190                 // Derive layout number from given argument (string)
3191                 // and current buffer's textclass (number). */    
3192                 int layoutno = 
3193                         textclasslist.NumberOfLayout(owner->
3194                                                 buffer()->
3195                                                 text->parameters->
3196                                                 textclass,
3197                                                 argument).second;
3198
3199                 // see if we found the layout number:
3200                 if (layoutno == -1) {
3201                         setErrorMessage(string(N_("Layout ")) + argument + 
3202                                         N_(" not known"));
3203                         break;
3204                 }
3205                         
3206                 if (current_layout != layoutno) {
3207                         owner->view()->getScreen()->HideCursor();
3208                         current_layout = layoutno;
3209                         owner->buffer()->update(-2);
3210                         owner->buffer()->text->
3211                                 SetLayout(layoutno);
3212                         owner->getToolbar()->combox->
3213                                 select(owner->buffer()->
3214                                        text->cursor.par->
3215                                        GetLayout() + 1);
3216                         owner->buffer()->update(1);
3217                 }
3218         }
3219         break;
3220
3221         case LFUN_LAYOUT_DOCUMENT:
3222                 MenuLayoutDocument();
3223                 break;
3224                 
3225         case LFUN_LAYOUT_PARAGRAPH:
3226                 MenuLayoutParagraph();
3227                 break;
3228                 
3229         case LFUN_LAYOUT_CHARACTER:
3230                 MenuLayoutCharacter();
3231                 break;
3232                 
3233         case LFUN_LAYOUT_TABLE:
3234         {
3235                 int flag = 0;
3236                 if (argument == "true") flag = 1;
3237                 MenuLayoutTable(flag);
3238         }
3239         break;
3240                 
3241         case LFUN_LAYOUT_PAPER:
3242                 MenuLayoutPaper();
3243                 break;
3244                 
3245         case LFUN_LAYOUT_QUOTES:
3246                 MenuLayoutQuotes();
3247                 break;
3248                 
3249         case LFUN_LAYOUT_PREAMBLE:
3250                 MenuLayoutPreamble();
3251                 break;
3252                 
3253         case LFUN_LAYOUT_SAVE_DEFAULT:
3254                 MenuLayoutSave();
3255                 break;
3256                 
3257         case LFUN_DROP_LAYOUTS_CHOICE:
3258                 owner->getToolbar()->combox->Show();
3259                 break;
3260
3261         case LFUN_EMPH:
3262                 EmphCB();
3263                 break;
3264                 
3265         case LFUN_BOLD:
3266                 BoldCB();
3267                 break;
3268                 
3269         case LFUN_NOUN:
3270                 NounCB();
3271                 break;
3272                 
3273         case LFUN_CODE:
3274                 CodeCB();
3275                 break;
3276                 
3277         case LFUN_SANS:
3278                 SansCB();
3279                 break;
3280                 
3281         case LFUN_ROMAN:
3282                 RomanCB();
3283                 break;
3284                 
3285         case LFUN_DEFAULT:
3286                 StyleResetCB();
3287                 break;
3288                 
3289         case LFUN_UNDERLINE:
3290                 UnderlineCB();
3291                 break;
3292                 
3293         case LFUN_FONT_SIZE:
3294                 FontSizeCB(argument);
3295                 break;
3296                 
3297         case LFUN_FONT_STATE:
3298                 setMessage(CurrentState());
3299                 break;
3300                 
3301         case LFUN_UPCASE_WORD:
3302                 owner->buffer()->update(-2);
3303                 FreeUpdateTimer();
3304                 owner->buffer()->text->ChangeWordCase(LyXText::text_uppercase);
3305                 owner->buffer()->update(1);
3306                 SetUpdateTimer();
3307                 break;
3308                 
3309         case LFUN_LOWCASE_WORD:
3310                 owner->buffer()->update(-2);
3311                 FreeUpdateTimer();
3312                 owner->buffer()->text->ChangeWordCase(LyXText::text_lowercase);
3313                 owner->buffer()->update(1);
3314                 SetUpdateTimer();
3315                 break;
3316                 
3317         case LFUN_CAPITALIZE_WORD:
3318                 owner->buffer()->update(-2);
3319                 FreeUpdateTimer();
3320                 owner->buffer()->text->ChangeWordCase(LyXText::text_capitalization);
3321                 owner->buffer()->update(1);
3322                 SetUpdateTimer();
3323                 break;
3324                 
3325         case LFUN_INSERT_LABEL:
3326                 MenuInsertLabel(argument.c_str());
3327                 break;
3328                 
3329         case LFUN_INSERT_REF:
3330                 MenuInsertRef();
3331                 break;
3332                 
3333         case LFUN_REFTOGGLE:
3334         {
3335                 InsetRef * inset = 
3336                         static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
3337                 if (inset) {
3338                         if (inset->getFlag() == InsetRef::REF)
3339                                 inset->setFlag(InsetRef::PAGE_REF);
3340                         else
3341                                 inset->setFlag(InsetRef::REF);
3342                         UpdateInset(inset);
3343                 } else {
3344                         setErrorMessage(N_("No cross-reference to toggle"));
3345                 }
3346         }
3347         break;
3348         
3349         case LFUN_REFBACK:
3350         {
3351                 owner->view()->restorePosition();
3352         }
3353         break;
3354
3355         case LFUN_REFGOTO:
3356         {
3357                 string label(argument);
3358                 if (label.empty()) {
3359                         InsetRef * inset = 
3360                                 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
3361                         if (inset)
3362                                 label = inset->getContents();
3363                 }
3364                 
3365                 if (!label.empty()) {
3366                         owner->view()->savePosition();
3367                         owner->buffer()->gotoLabel(label.c_str());
3368                 }
3369         }
3370         break;
3371                 
3372         case LFUN_MENU_OPEN_BY_NAME:
3373                 owner->getMenus()->openByName(argument);
3374                 break; // RVDK_PATCH_5
3375                 
3376         case LFUN_SPELLCHECK:
3377                 if (lyxrc->isp_command != "none")
3378                         ShowSpellChecker();
3379                 break; // RVDK_PATCH_5
3380                 
3381                 // --- Cursor Movements -----------------------------
3382         case LFUN_RIGHT:
3383         {
3384                 Buffer * tmpbuffer = owner->buffer();
3385                 LyXText * tmptext = owner->buffer()->text;
3386                 if(!tmptext->mark_set)
3387                         BeforeChange();
3388                 tmpbuffer->update(-2);
3389                 if (tmptext->cursor.pos < tmptext->cursor.par->Last()
3390                     && tmptext->cursor.par->GetChar(tmptext->cursor.pos)
3391                     == LyXParagraph::META_INSET
3392                     && tmptext->cursor.par->GetInset(tmptext->cursor.pos)
3393                     && tmptext->cursor.par->GetInset(tmptext->cursor.pos)->Editable() == 2){
3394                         Inset * tmpinset = tmptext->cursor.par->GetInset(tmptext->cursor.pos);
3395                         setMessage(tmpinset->EditMessage());
3396                         tmpinset->Edit(0, 0);
3397                         break;
3398                 }
3399                 tmptext->CursorRight();
3400                 owner->buffer()->text->FinishUndo();
3401                 moveCursorUpdate(false);
3402                 owner->getMiniBuffer()->Set(CurrentState());
3403         }
3404         break;
3405                 
3406         case LFUN_LEFT:
3407         {
3408                 // This is soooo ugly. Isn`t it possible to make
3409                 // it simpler? (Lgb)
3410                 LyXText * txt = owner->buffer()->text;
3411                 if(!txt->mark_set) BeforeChange();
3412                 owner->buffer()->update(-2);
3413                 txt->CursorLeft();
3414                 if (txt->cursor.pos < txt->cursor.par->Last()
3415                     && txt->cursor.par->GetChar(txt->cursor.pos)
3416                     == LyXParagraph::META_INSET
3417                     && txt->cursor.par->GetInset(txt->cursor.pos)
3418                     && txt->cursor.par->GetInset(txt->cursor.pos)->Editable() == 2) {
3419                         Inset * tmpinset = txt->cursor.par->GetInset(txt->cursor.pos);
3420                         setMessage(tmpinset->EditMessage());
3421                         tmpinset->Edit(tmpinset->Width(txt->GetFont(txt->cursor.par,
3422                                                                     txt->cursor.pos)), 0);
3423                         break;
3424                 }
3425                 owner->buffer()->text->FinishUndo();
3426                 moveCursorUpdate(false);
3427                 owner->getMiniBuffer()->Set(CurrentState());
3428         }
3429         break;
3430                 
3431         case LFUN_UP:
3432                 if(!owner->buffer()->text->mark_set) BeforeChange();
3433                 owner->buffer()->update(-3);
3434                 owner->buffer()->text->CursorUp();
3435                 owner->buffer()->text->FinishUndo();
3436                 moveCursorUpdate(false);
3437                 owner->getMiniBuffer()->Set(CurrentState());
3438                 break;
3439                 
3440         case LFUN_DOWN:
3441                 if(!owner->buffer()->text->mark_set)
3442                         BeforeChange();
3443                 owner->buffer()->update(-3);
3444                 owner->buffer()->text->CursorDown();
3445                 owner->buffer()->text->FinishUndo();
3446                 moveCursorUpdate(false);
3447                 owner->getMiniBuffer()->Set(CurrentState());
3448                 break;
3449
3450         case LFUN_UP_PARAGRAPH:
3451                 if(!owner->buffer()->text->mark_set)
3452                         BeforeChange();
3453                 owner->buffer()->update(-3);
3454                 owner->buffer()->text->CursorUpParagraph();
3455                 owner->buffer()->text->FinishUndo();
3456                 moveCursorUpdate(false);
3457                 owner->getMiniBuffer()->Set(CurrentState());
3458                 break;
3459                 
3460         case LFUN_DOWN_PARAGRAPH:
3461                 if(!owner->buffer()->text->mark_set)
3462                         BeforeChange();
3463                 owner->buffer()->update(-3);
3464                 owner->buffer()->text->CursorDownParagraph();
3465                 owner->buffer()->text->FinishUndo();
3466                 moveCursorUpdate(false);
3467                 owner->getMiniBuffer()->Set(CurrentState());
3468                 break;
3469                 
3470         case LFUN_PRIOR:
3471                 if(!owner->buffer()->text->mark_set)
3472                         BeforeChange();
3473                 owner->buffer()->update(-3);
3474                 owner->view()->cursorPrevious();
3475                 owner->buffer()->text->FinishUndo();
3476                 moveCursorUpdate(false);
3477                 owner->getMiniBuffer()->Set(CurrentState());
3478                 break;
3479                 
3480         case LFUN_NEXT:
3481                 if(!owner->buffer()->text->mark_set)
3482                         BeforeChange();
3483                 owner->buffer()->update(-3);
3484                 owner->view()->cursorNext();
3485                 owner->buffer()->text->FinishUndo();
3486                 moveCursorUpdate(false);
3487                 owner->getMiniBuffer()->Set(CurrentState());
3488                 break;
3489                 
3490         case LFUN_HOME:
3491                 if(!owner->buffer()->text->mark_set)
3492                         BeforeChange();
3493                 owner->buffer()->update(-2);
3494                 owner->buffer()->text->CursorHome();
3495                 owner->buffer()->text->FinishUndo();
3496                 moveCursorUpdate(false);
3497                 owner->getMiniBuffer()->Set(CurrentState());
3498                 break;
3499                 
3500         case LFUN_END:
3501                 if(!owner->buffer()->text->mark_set)
3502                         BeforeChange();
3503                 owner->buffer()->update(-2);
3504                 owner->buffer()->text->CursorEnd();
3505                 owner->buffer()->text->FinishUndo();
3506                 moveCursorUpdate(false);
3507                 owner->getMiniBuffer()->Set(CurrentState());
3508                 break;
3509                 
3510         case LFUN_TAB:
3511                 if(!owner->buffer()->text->mark_set)
3512                         BeforeChange();
3513                 owner->buffer()->update(-2);
3514                 owner->buffer()->text->CursorTab();
3515                 owner->buffer()->text->FinishUndo();
3516                 moveCursorUpdate(false);
3517                 owner->getMiniBuffer()->Set(CurrentState());
3518                 break;
3519                 
3520         case LFUN_WORDRIGHT:
3521                 if(!owner->buffer()->text->mark_set)
3522                         BeforeChange();
3523                 owner->buffer()->update(-2);
3524                 owner->buffer()->text->CursorRightOneWord();
3525                 owner->buffer()->text->FinishUndo();
3526                 moveCursorUpdate(false);
3527                 owner->getMiniBuffer()->Set(CurrentState());
3528                 break;
3529                 
3530         case LFUN_WORDLEFT:
3531                 if(!owner->buffer()->text->mark_set)
3532                         BeforeChange();
3533                 owner->buffer()->update(-2);
3534                 owner->buffer()->text->CursorLeftOneWord();
3535                 owner->buffer()->text->FinishUndo();
3536                 moveCursorUpdate(false);
3537                 owner->getMiniBuffer()->Set(CurrentState());
3538                 break;
3539                 
3540         case LFUN_BEGINNINGBUF:
3541                 if(!owner->buffer()->text->mark_set)
3542                         BeforeChange();
3543                 owner->buffer()->update(-2);
3544                 owner->buffer()->text->CursorTop();
3545                 owner->buffer()->text->FinishUndo();
3546                 moveCursorUpdate(false);
3547                 owner->getMiniBuffer()->Set(CurrentState());
3548                 break;
3549                 
3550         case LFUN_ENDBUF:
3551                 if(!owner->buffer()->text->mark_set)
3552                         BeforeChange();
3553                 owner->buffer()->update(-2);
3554                 owner->buffer()->text->CursorBottom();
3555                 owner->buffer()->text->FinishUndo();
3556                 moveCursorUpdate(false);
3557                 owner->getMiniBuffer()->Set(CurrentState());
3558                 break;
3559
3560       
3561                 /* cursor selection ---------------------------- */
3562         case LFUN_RIGHTSEL:
3563                 owner->buffer()->update(-2);
3564                 owner->buffer()->text->CursorRight();
3565                 owner->buffer()->text->FinishUndo();
3566                 moveCursorUpdate(true);
3567                 owner->getMiniBuffer()->Set(CurrentState());
3568                 break;
3569                 
3570         case LFUN_LEFTSEL:
3571                 owner->buffer()->update(-2);
3572                 owner->buffer()->text->CursorLeft();
3573                 owner->buffer()->text->FinishUndo();
3574                 moveCursorUpdate(true);
3575                 owner->getMiniBuffer()->Set(CurrentState());
3576                 break;
3577                 
3578         case LFUN_UPSEL:
3579                 owner->buffer()->update(-2);
3580                 owner->buffer()->text->CursorUp();
3581                 owner->buffer()->text->FinishUndo();
3582                 moveCursorUpdate(true);
3583                 owner->getMiniBuffer()->Set(CurrentState());
3584                 break;
3585                 
3586         case LFUN_DOWNSEL:
3587                 owner->buffer()->update(-2);
3588                 owner->buffer()->text->CursorDown();
3589                 owner->buffer()->text->FinishUndo();
3590                 moveCursorUpdate(true);
3591                 owner->getMiniBuffer()->Set(CurrentState());
3592                 break;
3593
3594         case LFUN_UP_PARAGRAPHSEL:
3595                 owner->buffer()->update(-2);
3596                 owner->buffer()->text->CursorUpParagraph();
3597                 owner->buffer()->text->FinishUndo();
3598                 moveCursorUpdate(true);
3599                 owner->getMiniBuffer()->Set(CurrentState());
3600                 break;
3601                 
3602         case LFUN_DOWN_PARAGRAPHSEL:
3603                 owner->buffer()->update(-2);
3604                 owner->buffer()->text->CursorDownParagraph();
3605                 owner->buffer()->text->FinishUndo();
3606                 moveCursorUpdate(true);
3607                 owner->getMiniBuffer()->Set(CurrentState());
3608                 break;
3609                 
3610         case LFUN_PRIORSEL:
3611                 owner->buffer()->update(-2);
3612                 owner->view()->cursorPrevious();
3613                 owner->buffer()->text->FinishUndo();
3614                 moveCursorUpdate(true);
3615                 owner->getMiniBuffer()->Set(CurrentState());
3616                 break;
3617                 
3618         case LFUN_NEXTSEL:
3619                 owner->buffer()->update(-2);
3620                 owner->view()->cursorNext();
3621                 owner->buffer()->text->FinishUndo();
3622                 moveCursorUpdate(true);
3623                 owner->getMiniBuffer()->Set(CurrentState());
3624                 break;
3625                 
3626         case LFUN_HOMESEL:
3627                 owner->buffer()->update(-2);
3628                 owner->buffer()->text->CursorHome();
3629                 owner->buffer()->text->FinishUndo();
3630                 moveCursorUpdate(true);
3631                 owner->getMiniBuffer()->Set(CurrentState());
3632                 break;
3633                 
3634         case LFUN_ENDSEL:
3635                 owner->buffer()->update(-2);
3636                 owner->buffer()->text->CursorEnd();
3637                 owner->buffer()->text->FinishUndo();
3638                 moveCursorUpdate(true);
3639                 owner->getMiniBuffer()->Set(CurrentState());
3640                 break;
3641                 
3642         case LFUN_WORDRIGHTSEL:
3643                 owner->buffer()->update(-2);
3644                 owner->buffer()->text->CursorRightOneWord();
3645                 owner->buffer()->text->FinishUndo();
3646                 moveCursorUpdate(true);
3647                 owner->getMiniBuffer()->Set(CurrentState());
3648                 break;
3649                 
3650         case LFUN_WORDLEFTSEL:
3651                 owner->buffer()->update(-2);
3652                 owner->buffer()->text->CursorLeftOneWord();
3653                 owner->buffer()->text->FinishUndo();
3654                 moveCursorUpdate(true);
3655                 owner->getMiniBuffer()->Set(CurrentState());
3656                 break;
3657                 
3658         case LFUN_BEGINNINGBUFSEL:
3659                 owner->buffer()->update(-2);
3660                 owner->buffer()->text->CursorTop();
3661                 owner->buffer()->text->FinishUndo();
3662                 moveCursorUpdate(true);
3663                 owner->getMiniBuffer()->Set(CurrentState());
3664                 break;
3665                 
3666         case LFUN_ENDBUFSEL:
3667                 owner->buffer()->update(-2);
3668                 owner->buffer()->text->CursorBottom();
3669                 owner->buffer()->text->FinishUndo();
3670                 moveCursorUpdate(true);
3671                 owner->getMiniBuffer()->Set(CurrentState());
3672                 break;
3673
3674                 // --- text changing commands ------------------------
3675         case LFUN_BREAKLINE:
3676                 BeforeChange();
3677                 owner->buffer()->text->InsertChar(LyXParagraph::META_NEWLINE);
3678                 SmallUpdate(1);
3679                 SetUpdateTimer(0.01);
3680                 moveCursorUpdate(false);
3681                 break;
3682                 
3683         case LFUN_PROTECTEDSPACE:
3684                 BeforeChange();
3685                 owner->buffer()->text->
3686                         InsertChar(LyXParagraph::META_PROTECTED_SEPARATOR);
3687                 SmallUpdate(1);
3688                 SetUpdateTimer();
3689                 moveCursorUpdate(false);
3690                 break;
3691                 
3692         case LFUN_SETMARK:
3693                 if(owner->buffer()->text->mark_set) {
3694                         BeforeChange();
3695                         owner->buffer()->update(0);
3696                         setMessage(N_("Mark removed"));
3697                 } else {
3698                         BeforeChange();
3699                         owner->buffer()->text->mark_set = 1;
3700                         owner->buffer()->update(0);
3701                         setMessage(N_("Mark set"));
3702                 }
3703                 owner->buffer()->text->sel_cursor = 
3704                         owner->buffer()->text->cursor;
3705                 break;
3706                 
3707         case LFUN_DELETE:
3708                 FreeUpdateTimer();
3709                 if (!owner->buffer()->text->selection) {
3710                         owner->buffer()->text->Delete();
3711                         owner->buffer()->text->sel_cursor = 
3712                                 owner->buffer()->text->cursor;
3713                         SmallUpdate(1);
3714                         // It is possible to make it a lot faster still
3715                         // just comment out the lone below...
3716                         owner->view()->getScreen()->ShowCursor();
3717                 } else {
3718                         CutCB();
3719                 }
3720                 SetUpdateTimer();
3721                 break;
3722
3723         case LFUN_DELETE_SKIP:
3724         {
3725                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
3726                 
3727                 LyXCursor cursor = owner->buffer()->text->cursor;
3728
3729                 FreeUpdateTimer();
3730                 if (!owner->buffer()->text->selection) {
3731                         if (cursor.pos == cursor.par->Last()) {
3732                                 owner->buffer()->text->CursorRight();
3733                                 cursor = owner->buffer()->text->cursor;
3734                                 if (cursor.pos == 0
3735                                     && !(cursor.par->added_space_top 
3736                                          == VSpace (VSpace::NONE))) {
3737                                         owner->buffer()->text->SetParagraph
3738                                                 (cursor.par->line_top,
3739                                                  cursor.par->line_bottom,
3740                                                  cursor.par->pagebreak_top, 
3741                                                  cursor.par->pagebreak_bottom,
3742                                                  VSpace(VSpace::NONE), 
3743                                                  cursor.par->added_space_bottom,
3744                                                  cursor.par->align, 
3745                                                  cursor.par->labelwidthstring, 0);
3746                                         owner->buffer()->text->CursorLeft();
3747                                         owner->buffer()->update (1);
3748                                 } else {
3749                                         owner->buffer()->text->CursorLeft();
3750                                         owner->buffer()->text->Delete();
3751                                         owner->buffer()->text->sel_cursor = 
3752                                                 owner->buffer()->text->cursor;
3753                                         SmallUpdate(1);
3754                                 }
3755                         } else {
3756                                 owner->buffer()->text->Delete();
3757                                 owner->buffer()->text->sel_cursor = 
3758                                         owner->buffer()->text->cursor;
3759                                 SmallUpdate(1);
3760                         }
3761                 } else {
3762                         CutCB();
3763                 }
3764                 SetUpdateTimer();
3765         }
3766         break;
3767
3768         /* -------> Delete word forward. */
3769         case LFUN_DELETE_WORD_FORWARD:
3770                 owner->buffer()->update(-2);
3771                 FreeUpdateTimer();
3772                 owner->buffer()->text->DeleteWordForward();
3773                 owner->buffer()->update( 1 );
3774                 SetUpdateTimer();
3775                 moveCursorUpdate(false);
3776                 break;
3777
3778                 /* -------> Delete word backward. */
3779         case LFUN_DELETE_WORD_BACKWARD:
3780                 owner->buffer()->update(-2);
3781                 FreeUpdateTimer();
3782                 owner->buffer()->text->DeleteWordBackward();
3783                 owner->buffer()->update( 1 );
3784                 SetUpdateTimer();
3785                 moveCursorUpdate(false);
3786                 break;
3787                 
3788                 /* -------> Kill to end of line. */
3789         case LFUN_DELETE_LINE_FORWARD:
3790                 FreeUpdateTimer();
3791                 owner->buffer()->update(-2);
3792                 owner->buffer()->text->DeleteLineForward();
3793                 owner->buffer()->update( 1 );
3794                 SetUpdateTimer();
3795                 moveCursorUpdate(false);
3796                 break;
3797                 
3798                 /* -------> Set mark off. */
3799         case LFUN_MARK_OFF:
3800                 BeforeChange();
3801                 owner->buffer()->update(0);
3802                 owner->buffer()->text->sel_cursor = 
3803                         owner->buffer()->text->cursor;
3804                 setMessage(N_("Mark off"));
3805                 break;
3806
3807                 /* -------> Set mark on. */
3808         case LFUN_MARK_ON:
3809                 BeforeChange();
3810                 owner->buffer()->text->mark_set = 1;
3811                 owner->buffer()->update( 0 );
3812                 owner->buffer()->text->sel_cursor = 
3813                         owner->buffer()->text->cursor;
3814                 setMessage(N_("Mark on"));
3815                 break;
3816                 
3817         case LFUN_BACKSPACE:
3818         {
3819                 FreeUpdateTimer();
3820                 if (!owner->buffer()->text->selection) {
3821                         if (owner->getIntl()->getTrans()->backspace()) {
3822                                 owner->buffer()->text->Backspace();
3823                                 owner->buffer()->text->sel_cursor = 
3824                                         owner->buffer()->text->cursor;
3825                                 SmallUpdate(1);
3826                                 // It is possible to make it a lot faster still
3827                                 // just comment out the lone below...
3828                                 owner->view()->getScreen()->ShowCursor();
3829                         }
3830                 } else {
3831                         CutCB();
3832                 }
3833                 SetUpdateTimer();
3834         }
3835         break;
3836
3837         case LFUN_BACKSPACE_SKIP:
3838         {
3839                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
3840                 
3841                 LyXCursor cursor = owner->buffer()->text->cursor;
3842                 
3843                 FreeUpdateTimer();
3844                 if (!owner->buffer()->text->selection) {
3845                         if (cursor.pos == 0 
3846                             && !(cursor.par->added_space_top 
3847                                  == VSpace (VSpace::NONE))) {
3848                                 owner->buffer()->text->SetParagraph 
3849                                         (cursor.par->line_top,      
3850                                          cursor.par->line_bottom,
3851                                          cursor.par->pagebreak_top, 
3852                                          cursor.par->pagebreak_bottom,
3853                                          VSpace(VSpace::NONE), cursor.par->added_space_bottom,
3854                                          cursor.par->align, 
3855                                          cursor.par->labelwidthstring, 0);
3856                                 owner->buffer()->update (1);
3857                         } else {
3858                                 owner->buffer()->text->Backspace();
3859                                 owner->buffer()->text->sel_cursor 
3860                                         = cursor;
3861                                 SmallUpdate (1);
3862                         }
3863                 } else
3864                         CutCB();
3865                 SetUpdateTimer();
3866         }
3867         break;
3868
3869         case LFUN_BREAKPARAGRAPH:
3870         {
3871                 BeforeChange();
3872                 owner->buffer()->text->BreakParagraph(0);
3873                 SmallUpdate(1);
3874                 SetUpdateTimer(0.01);
3875                 owner->buffer()->text->sel_cursor = 
3876                         owner->buffer()->text->cursor;
3877                 break;
3878         }
3879
3880         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
3881         {
3882                 BeforeChange();
3883                 owner->buffer()->text->BreakParagraph(1);
3884                 SmallUpdate(1);
3885                 SetUpdateTimer(0.01);
3886                 owner->buffer()->text->sel_cursor = 
3887                         owner->buffer()->text->cursor;
3888                 break;
3889         }
3890         
3891         case LFUN_BREAKPARAGRAPH_SKIP:
3892         {
3893                 // When at the beginning of a paragraph, remove
3894                 // indentation and add a "defskip" at the top.
3895                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
3896                 
3897                 LyXCursor cursor = owner->buffer()->text->cursor;
3898                 
3899                 BeforeChange();
3900                 if (cursor.pos == 0) {
3901                         if (cursor.par->added_space_top == VSpace(VSpace::NONE)) {
3902                                 owner->buffer()->text->SetParagraph
3903                                         (cursor.par->line_top,      
3904                                          cursor.par->line_bottom,
3905                                          cursor.par->pagebreak_top, 
3906                                          cursor.par->pagebreak_bottom,
3907                                          VSpace(VSpace::DEFSKIP), cursor.par->added_space_bottom,
3908                                          cursor.par->align, 
3909                                          cursor.par->labelwidthstring, 1);
3910                                 owner->buffer()->update(1);
3911                         } 
3912                 }
3913                 else {
3914                         owner->buffer()->text->BreakParagraph(0);
3915                         SmallUpdate(1);
3916                 }
3917                 SetUpdateTimer(0.01);
3918                 owner->buffer()->text->sel_cursor = cursor;
3919         }
3920         break;
3921         
3922         case LFUN_QUOTE:
3923                 BeforeChange();
3924                 owner->buffer()->text->InsertChar('\"');  // This " matches the single quote in the code
3925                 SmallUpdate(1);
3926                 SetUpdateTimer();
3927                 moveCursorUpdate(false);
3928                 break;
3929
3930         case LFUN_HTMLURL:
3931         case LFUN_URL:
3932         {
3933                 InsetCommand * new_inset;
3934                 if (action == LFUN_HTMLURL)
3935                         new_inset = new InsetUrl("htmlurl", "", "");
3936                 else
3937                         new_inset = new InsetUrl("url", "", "");
3938                 owner->buffer()->insertInset(new_inset);
3939                 new_inset->Edit(0, 0);
3940         }
3941         break;
3942
3943         // --- lyxserver commands ----------------------------
3944
3945         case LFUN_CHARATCURSOR:
3946         {
3947                 LyXParagraph::size_type pos = 
3948                   owner->buffer()->text->cursor.pos;
3949                 if(pos < owner->buffer()->text->cursor.par->size())
3950                         dispatch_buffer = owner->buffer()->text->
3951                                 cursor.par->text[pos];
3952                 else
3953                         dispatch_buffer = "EOF";
3954         }
3955         break;
3956         
3957         case LFUN_GETXY:
3958                 dispatch_buffer = 
3959                          tostr(owner->buffer()->text->cursor.x) + ' '
3960                         + tostr(owner->buffer()->text->cursor.y);
3961                 break;
3962                 
3963         case LFUN_SETXY:
3964         {
3965                 int  x;
3966                 long y;
3967                 sscanf(argument.c_str(), " %d %ld", &x, &y);
3968                 owner->buffer()->text->SetCursorFromCoordinates(x, y);
3969         }
3970         break;
3971         
3972         case LFUN_GETLAYOUT:
3973                 dispatch_buffer =  
3974                         tostr(owner->buffer()->text->cursor.par->layout);
3975                 break;
3976                         
3977         case LFUN_GETFONT:
3978         {
3979                 LyXFont *font = &(owner->buffer()->text->current_font);
3980                 if(font->shape() == LyXFont::ITALIC_SHAPE)
3981                         dispatch_buffer = 'E';
3982                 else if(font->shape() == LyXFont::SMALLCAPS_SHAPE)
3983                         dispatch_buffer = 'N';
3984                 else
3985                         dispatch_buffer = '0';
3986
3987         }
3988         break;
3989
3990         case LFUN_GETLATEX:
3991         {
3992                 LyXFont *font = &(owner->buffer()->text->current_font);
3993                 if(font->latex() == LyXFont::ON)
3994                         dispatch_buffer = 'L';
3995                 else
3996                         dispatch_buffer = '0';
3997         }
3998         break;
3999
4000         case LFUN_GETNAME:
4001                 setMessage(owner->buffer()->getFileName());
4002                 lyxerr.debug() << "FNAME["
4003                                << owner->buffer()->getFileName()
4004                                << "] " << endl;
4005                 break;
4006                 
4007         case LFUN_NOTIFY:
4008         {
4009                 char buf[100];
4010                 keyseq.print(buf, 100);
4011                 dispatch_buffer = buf;
4012                 lyxserver->notifyClient(dispatch_buffer);
4013         }
4014         break;
4015
4016         case LFUN_GOTOFILEROW:
4017         {
4018                 char file_name[100];
4019                 int  row;
4020                 sscanf(argument.c_str(), " %s %d", file_name, &row);
4021
4022                 // Must replace extension of the file to be .lyx and get full path
4023                 string s = ChangeExtension(string(file_name), ".lyx", false);
4024
4025                 // Either change buffer or load the file
4026                 if (bufferlist.exists(s))
4027                         owner->view()->buffer(bufferlist.getBuffer(s));
4028                 else
4029                         owner->view()->buffer(bufferlist.loadLyXFile(s));
4030
4031                 // Set the cursor  
4032                 owner->buffer()->setCursorFromRow(row);
4033
4034                 // Recenter screen
4035                 BeforeChange();
4036                 if (owner->buffer()->text->cursor.y >
4037                     owner->view()->getWorkArea()->h / 2)        {
4038                         owner->view()->getScreen()->
4039                                 Draw(owner->buffer()->text->cursor.y -
4040                                      owner->view()->getWorkArea()->h/2);
4041                 } else { // <= 
4042                         owner->view()->getScreen()->
4043                                 Draw(0);
4044                 }
4045                 owner->buffer()->update(0);
4046                 owner->view()->redraw();
4047         }
4048         break;
4049
4050         case LFUN_APROPOS:
4051         case LFUN_GETTIP:
4052         {
4053                 int qa = lyxaction.LookupFunc(argument.c_str());
4054                 setMessage(lyxaction.helpText((kb_action)qa));
4055         }
4056         break;
4057
4058         // --- accented characters ---------------------------
4059                 
4060         case LFUN_UMLAUT:
4061         case LFUN_CIRCUMFLEX:
4062         case LFUN_GRAVE:
4063         case LFUN_ACUTE:
4064         case LFUN_TILDE:
4065         case LFUN_CEDILLA:
4066         case LFUN_MACRON:
4067         case LFUN_DOT:
4068         case LFUN_UNDERDOT:
4069         case LFUN_UNDERBAR:
4070         case LFUN_CARON:
4071         case LFUN_SPECIAL_CARON:
4072         case LFUN_BREVE:
4073         case LFUN_TIE:
4074         case LFUN_HUNG_UMLAUT:
4075         case LFUN_CIRCLE:
4076         case LFUN_OGONEK:
4077         {
4078                 char c;
4079                 
4080                 if (keyseq.length == -1 && keyseq.getiso()!= 0) 
4081                         c= keyseq.getiso();
4082                 else
4083                         c= 0;
4084                 
4085                 owner->getIntl()->getTrans()->
4086                         deadkey(c, get_accent(action).accent, 
4087                                 owner->buffer()->text);
4088                 
4089                 // Need to reset, in case the minibuffer calls these
4090                 // actions
4091                 keyseq.reset();
4092                 keyseq.length= 0;
4093                 
4094                 // copied verbatim from do_accent_char
4095                 SmallUpdate(1);
4096                 SetUpdateTimer();
4097                 owner->buffer()->text->sel_cursor = 
4098                         owner->buffer()->text->cursor;
4099         }   
4100         break;
4101         
4102         // --- toolbar ----------------------------------
4103         case LFUN_PUSH_TOOLBAR:
4104         {
4105                 int nth = strToInt(argument);
4106                 if (lyxerr.debugging(Debug::TOOLBAR)) {
4107                         lyxerr << "LFUN_PUSH_TOOLBAR: argument = `"
4108                                << argument << "'\n"
4109                                << "LFUN_PUSH_TOOLBAR: nth = `"
4110                                << nth << "'" << endl;
4111                 }
4112                 
4113                 if (nth <= 0) {
4114                         LyXBell();
4115                         setErrorMessage(N_("Push-toolbar needs argument > 0"));
4116                 } else {
4117                         owner->getToolbar()->push(nth);
4118                 }
4119         }
4120         break;
4121         
4122         case LFUN_ADD_TO_TOOLBAR:
4123         {
4124                 if (lyxerr.debugging(Debug::TOOLBAR)) {
4125                         lyxerr << "LFUN_ADD_TO_TOOLBAR:"
4126                                 "argument = `" << argument << '\'' << endl;
4127                 }
4128                 string tmp(argument);
4129                 //lyxerr <<string("Argument: ") + argument);
4130                 //lyxerr <<string("Tmp     : ") + tmp);
4131                 if (tmp.empty()) {
4132                         LyXBell();
4133                         setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
4134                 } else {
4135                         owner->getToolbar()->add(argument, false);
4136                         owner->getToolbar()->set();
4137                 }
4138         }
4139         break;
4140         
4141         // --- insert characters ----------------------------------------
4142 #if 0
4143         case LFUN_INSERT_INSET_LATEX:
4144         {
4145                 Inset *new_inset = new InsetLatex(argument);
4146                 owner->buffer()->insertInset(new_inset);
4147         }
4148         break;
4149 #endif
4150         // ---  Mathed stuff. If we are here, there is no locked inset yet.
4151         
4152         // Greek mode     
4153         case LFUN_GREEK:
4154         {
4155                 if (!greek_kb_flag) {
4156                         greek_kb_flag = 1;
4157                         setMessage(N_("Math greek mode on"));
4158                 } else
4159                         greek_kb_flag = 0;
4160         }  
4161         break;
4162       
4163         // Greek keyboard      
4164         case LFUN_GREEK_TOGGLE:
4165         {
4166                 greek_kb_flag = (greek_kb_flag) ? 0: 2;
4167                 if (greek_kb_flag) {
4168                         setMessage(N_("Math greek keyboard on"));
4169                 } else {
4170                         setMessage(N_("Math greek keyboard off"));
4171                 }
4172         }
4173         break;
4174         
4175         case LFUN_MATH_DELIM:     
4176         case LFUN_INSERT_MATRIX:
4177         {          
4178                 if (owner->view()->available()) { 
4179                         owner->buffer()->
4180                                 open_new_inset(new InsetFormula(false));
4181                         owner->buffer()->
4182                                 the_locking_inset->LocalDispatch(action, argument.c_str());
4183                 }
4184         }          
4185         break;
4186                
4187         case LFUN_INSERT_MATH:
4188         {
4189                 math_insert_symbol(argument.c_str());
4190         }
4191         break;
4192         
4193         case LFUN_MATH_DISPLAY:
4194         {
4195                 if (owner->view()->available())
4196                         owner->buffer()->open_new_inset(new InsetFormula(true));
4197                 break;
4198         }
4199                     
4200         case LFUN_MATH_MACRO:
4201         {
4202                 if (owner->view()->available()) {
4203                         string s(argument);
4204                         if (s.empty())
4205                             setErrorMessage(N_("Missing argument"));
4206                         else {
4207                             string s1 = token(s, ' ', 1);
4208                             int na = s1.empty() ? 0: atoi(s1.c_str());
4209                             owner->buffer()->
4210                               open_new_inset(new InsetFormulaMacro(token(s, ' ', 0), na));
4211                         }
4212                 }
4213         }
4214         break;
4215
4216         case LFUN_MATH_MODE:   // Open or create a math inset
4217         {
4218                 
4219                 if (owner->view()->available())
4220                         owner->buffer()->open_new_inset(new InsetFormula);
4221                 setMessage(N_("Math editor mode"));
4222         }
4223         break;
4224           
4225         case LFUN_MATH_NUMBER:
4226         case LFUN_MATH_LIMITS:
4227         {
4228                 setErrorMessage(N_("This is only allowed in math mode!"));
4229         }
4230         break;
4231         
4232         case LFUN_INSERT_CITATION:
4233         {   
4234                 InsetCitation * new_inset = new InsetCitation();
4235                 // ale970405
4236                 // The note, if any, must be after the key, delimited
4237                 // by a | so both key and remark can have spaces.
4238                 if (!argument.empty()) {
4239                         string lsarg(argument);
4240                         if (contains(lsarg, "|")) {
4241                                 new_inset->setContents(token(lsarg, '|', 0));
4242                                 new_inset->setOptions(token(lsarg, '|', 1));
4243                         } else
4244                                 new_inset->setContents(lsarg);
4245                         owner->buffer()->insertInset(new_inset);
4246                 } else {
4247                         owner->buffer()->insertInset(new_inset);
4248                         new_inset->Edit(0, 0);
4249                 }
4250         }
4251         break;
4252                     
4253         case LFUN_INSERT_BIBTEX:
4254         {   
4255                 // ale970405+lasgoutt970425
4256                 // The argument can be up to two tokens separated 
4257                 // by a space. The first one is the bibstyle.
4258                 string lsarg(argument);
4259                 string bibstyle = token(lsarg, ' ', 1);
4260                 if (bibstyle.empty())
4261                         bibstyle = "plain";
4262                 InsetBibtex * new_inset 
4263                         = new InsetBibtex(token(lsarg, ' ', 0),
4264                                           bibstyle,
4265                                           owner->buffer());
4266                 
4267                 owner->buffer()->insertInset(new_inset);
4268                 if (lsarg.empty()) {
4269                         new_inset->Edit(0, 0);
4270                 }
4271         }
4272         break;
4273                 
4274         // BibTeX data bases
4275         case LFUN_BIBDB_ADD:
4276         {
4277                 InsetBibtex * inset = 
4278                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
4279                 if (inset) {
4280                         inset->addDatabase(argument);
4281                 }
4282         }
4283         break;
4284                     
4285         case LFUN_BIBDB_DEL:
4286         {
4287                 InsetBibtex * inset = 
4288                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
4289                 if (inset) {
4290                         inset->delDatabase(argument);
4291                 }
4292         }
4293         break;
4294         
4295         case LFUN_BIBTEX_STYLE:
4296         {
4297                 InsetBibtex * inset = 
4298                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
4299                 if (inset) {
4300                         inset->setOptions(argument);
4301                 }
4302         }
4303         break;
4304                 
4305         case LFUN_INDEX_INSERT:
4306         case LFUN_INDEX_INSERT_LAST:
4307         {
4308                 // Can't do that at the beginning of a paragraph.
4309                 if (owner->buffer()->text->cursor.pos - 1 <0)
4310                         break;
4311
4312                 InsetIndex * new_inset = new InsetIndex();
4313                 if (!argument.empty()) {
4314                         string lsarg(argument);
4315                         new_inset->setContents(lsarg);
4316                         owner->buffer()->insertInset(new_inset);
4317                 } else {
4318                   //reh 98/09/21
4319                   //get the current word for an argument
4320                   LyXParagraph::size_type lastpos = 
4321                           owner->buffer()->text->cursor.pos - 1;
4322                   // Get the current word. note that this must be done
4323                   // before inserting the inset, or the inset will
4324                   // break the word
4325                   string curstring(owner->buffer()
4326                                    ->text->cursor.par->GetWord(lastpos));
4327
4328                   //make the new inset and write the current word into it
4329                   InsetIndex * new_inset = new InsetIndex();
4330
4331                   new_inset->setContents(curstring);
4332
4333                   //don't edit it if the call was to INSERT_LAST
4334                   if(action!= LFUN_INDEX_INSERT_LAST) {
4335                       new_inset->Edit(0, 0);
4336                   } else {
4337                       //it looks blank on the screen unless
4338                       //we do  something.  put it here.
4339
4340                       // move the cursor to the returned value of lastpos
4341                       // but only for the auto-insert
4342                       owner->buffer()->text->cursor.pos= lastpos;
4343                   }
4344
4345                   //put the new inset into the buffer.
4346                   // there should be some way of knowing the user
4347                   //cancelled & avoiding this, but i don't know how
4348                   owner->buffer()->insertInset(new_inset);
4349                 }
4350         }
4351         break;
4352
4353         case LFUN_INDEX_PRINT:
4354         {
4355                 Inset * new_inset = new InsetPrintIndex(owner->buffer());
4356                 owner->buffer()->insertInset(new_inset, "Standard", true);
4357         }
4358         break;
4359
4360         case LFUN_PARENTINSERT:
4361         {
4362                 lyxerr << "arg " << argument << endl;
4363                 Inset * new_inset = new InsetParent(argument, owner->buffer());
4364                 owner->buffer()->insertInset(new_inset, "Standard", true);
4365         }
4366         break;
4367
4368         case LFUN_CHILDINSERT:
4369         {
4370                 Inset * new_inset = new InsetInclude(argument,
4371                                                      owner->buffer());
4372                 owner->buffer()->insertInset(new_inset, "Standard", true);
4373                 new_inset->Edit(0, 0);
4374         }
4375         break;
4376
4377         case LFUN_CHILDOPEN:
4378         {
4379                 string filename =
4380                         MakeAbsPath(argument, 
4381                                     OnlyPath(owner->buffer()->getFileName()));
4382                 setMessage(N_("Opening child document ") +
4383                            MakeDisplayPath(filename) + "...");
4384                 owner->view()->savePosition();
4385                 if (bufferlist.exists(filename))
4386                   owner->view()->buffer(bufferlist.getBuffer(filename));
4387                 else
4388                   owner->view()->buffer(bufferlist.loadLyXFile(filename));
4389         }
4390         break;
4391
4392         case LFUN_INSERT_NOTE:
4393                 NoteCB();
4394                 break;
4395                 
4396         case LFUN_INSERTFOOTNOTE: 
4397         {
4398                 LyXParagraph::footnote_kind kind;
4399                 if (argument == "footnote")
4400                         { kind = LyXParagraph::FOOTNOTE; }
4401                 else if (argument == "margin")
4402                         { kind = LyXParagraph::MARGIN; }
4403                 else if (argument == "figure")
4404                         { kind = LyXParagraph::FIG; }
4405                 else if (argument == "table")
4406                         { kind = LyXParagraph::TAB; }
4407                 else if (argument == "wide-fig")
4408                         { kind = LyXParagraph::WIDE_FIG; }
4409                 else if (argument == "wide-tab")
4410                         { kind = LyXParagraph::WIDE_TAB; }
4411                 else if (argument == "algorithm")
4412                         { kind = LyXParagraph::ALGORITHM; }
4413                 else {
4414                         setErrorMessage(N_("Unknown kind of footnote"));
4415                         break;
4416                 }
4417                 owner->buffer()->text->InsertFootnoteEnvironment(kind);
4418                 owner->buffer()->update(1);
4419         }
4420         break;
4421         
4422         case LFUN_BUFFERBULLETSSELECT:
4423                 bulletForm();
4424                 break;
4425                 
4426         case LFUN_TOGGLECURSORFOLLOW:
4427                 cursor_follows_scrollbar = !cursor_follows_scrollbar;
4428                 break;
4429                 
4430         case LFUN_KMAP_OFF:             // keymap off
4431                 owner->getIntl()->KeyMapOn(false);
4432                 break;
4433                 
4434         case LFUN_KMAP_PRIM:    // primary keymap
4435                 owner->getIntl()->KeyMapPrim();
4436                 break;
4437                 
4438         case LFUN_KMAP_SEC:             // secondary keymap
4439                 owner->getIntl()->KeyMapSec();
4440                 break;
4441                 
4442         case LFUN_KMAP_TOGGLE:  // toggle keymap
4443                 owner->getIntl()->ToggleKeyMap();
4444                 break;
4445
4446         case LFUN_SELFINSERT:
4447         {
4448                 for (string::size_type i = 0; i < argument.length(); ++i) {
4449                         owner->buffer()->text->InsertChar(argument[i]);
4450                         // This needs to be in the loop, or else we
4451                         // won't break lines correctly. (Asger)
4452                         SmallUpdate(1);
4453                 }
4454                 SetUpdateTimer();
4455                 owner->buffer()->text->sel_cursor = 
4456                         owner->buffer()->text->cursor;
4457                 moveCursorUpdate(false);
4458         }
4459         break;
4460
4461         case LFUN_SEQUENCE: 
4462         {
4463                 // argument contains ';'-terminated commands
4464                 while (argument.find(';') != string::npos) {
4465                         string first;
4466                         argument = split(argument, first, ';');
4467                         Dispatch(first);
4468                 }
4469         }
4470         break;
4471
4472         case LFUN_SAVEPREFERENCES:
4473         {
4474                 Path p(user_lyxdir);
4475                 lyxrc->write("preferences");
4476         }
4477         break;
4478         
4479         case LFUN_UNKNOWN_ACTION:
4480         {
4481                 if (owner->buffer()->isReadonly()) {
4482                         LyXBell();
4483                         setErrorMessage(N_("Document is read only"));
4484                         break;
4485                 }
4486                          
4487                 if (!argument.empty()) {
4488                         
4489                         /* Automatically delete the currently selected
4490                          * text and replace it with what is being
4491                          * typed in now. Depends on lyxrc settings
4492                          * "auto_region_delete", which defaults to
4493                          * true (on). */
4494                 
4495                         if ( lyxrc->auto_region_delete ) {
4496                                 if (owner->buffer()->text->selection){
4497                                         owner->buffer()->text->CutSelection(false);
4498                                         owner->buffer()->update(-1);
4499                                 }
4500                         }
4501                         
4502                         BeforeChange();
4503                         for (string::size_type i = 0;
4504                              i < argument.length(); ++i) {
4505                                 if (greek_kb_flag) {
4506                                         if (!math_insert_greek(argument[i]))
4507                                                 owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->buffer()->text);
4508                                 } else
4509                                         owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->buffer()->text);
4510                         }
4511                         
4512                         SmallUpdate(1);
4513                         SetUpdateTimer();
4514
4515                         owner->buffer()->text->sel_cursor = 
4516                                 owner->buffer()->text->cursor;
4517                         moveCursorUpdate(false);
4518                         return string();
4519                 } else {
4520                         // why is an "Unknown action" with empty
4521                         // argument even dispatched in the first
4522                         // place? I`ll probably change that. (Lgb)
4523                         LyXBell();
4524                         setErrorMessage(N_("Unknown action"));
4525                 }
4526                 break;
4527         default:
4528                 lyxerr << "A truly unknown func!" << endl;
4529                 break;
4530         }
4531         } // end of switch
4532   exit_with_message:
4533
4534         string res = getMessage();
4535
4536         if (res.empty()) {
4537                 if (!commandshortcut.empty()) {
4538                         string newbuf = owner->getMiniBuffer()->GetText();
4539                         if (newbuf != commandshortcut) {
4540                                 owner->getMiniBuffer()->Set(newbuf
4541                                                             + " " +
4542                                                             commandshortcut);
4543                         }
4544                 }
4545         } else {
4546                 owner->getMiniBuffer()->Set(string(_(res.c_str()))
4547                                             + " " + commandshortcut);
4548         }
4549
4550         return res;
4551 }
4552 #endif      
4553
4554 void LyXFunc::setupLocalKeymap()
4555 {
4556         keyseq.stdmap = keyseq.curmap = toplevel_keymap;
4557         cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap;
4558 }
4559
4560
4561 void LyXFunc::MenuNew(bool fromTemplate)
4562 {
4563         string fname, initpath = lyxrc->document_path;
4564         LyXFileDlg fileDlg;
4565
4566         if (owner->view()->available()) {
4567                 string trypath = owner->buffer()->filepath;
4568                 // If directory is writeable, use this as default.
4569                 if (IsDirWriteable(trypath) == 1)
4570                         initpath = trypath;
4571         }
4572
4573         ProhibitInput();
4574         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
4575         fileDlg.SetButton(1, _("Templates"), lyxrc->template_path);
4576         fname = fileDlg.Select(_("Enter Filename for new document"), 
4577                                initpath, "*.lyx", _("newfile"));
4578         AllowInput();
4579         
4580         if (fname.empty()) {
4581                 owner->getMiniBuffer()->Set(_("Canceled."));
4582                 lyxerr.debug() << "New Document Cancelled." << endl;
4583                 return;
4584         }
4585         
4586         // get absolute path of file and make sure the filename ends
4587         // with .lyx
4588         string s = MakeAbsPath(fname);
4589         if (!IsLyXFilename(s))
4590                 s += ".lyx";
4591
4592         // Check if the document already is open
4593         if (bufferlist.exists(s)){
4594                 switch(AskConfirmation(_("Document is already open:"), 
4595                                        MakeDisplayPath(s, 50),
4596                                        _("Do you want to close that document now?\n"
4597                                          "('No' will just switch to the open version)")))
4598                 {
4599                 case 1: // Yes: close the document
4600                         if (!bufferlist.close(bufferlist.getBuffer(s)))
4601                                 // If close is canceled, we cancel here too.
4602                                 return;
4603                         break;
4604                 case 2: // No: switch to the open document
4605                         owner->view()->buffer(bufferlist.getBuffer(s));
4606                         return;
4607                 case 3: // Cancel: Do nothing
4608                         owner->getMiniBuffer()->Set(_("Canceled."));
4609                         return;
4610                 }
4611         }
4612         
4613         // Check whether the file already exists
4614         if (IsLyXFilename(s)) {
4615                 FileInfo fi(s);
4616                 if (fi.readable() &&
4617                     AskQuestion(_("File already exists:"), 
4618                                 MakeDisplayPath(s, 50),
4619                                 _("Do you want to open the document?"))) {
4620                         // loads document
4621                         owner->getMiniBuffer()->Set(_("Opening document"), 
4622                                                     MakeDisplayPath(s), "...");
4623                         XFlush(fl_display);
4624                         owner->view()->buffer(
4625                                 bufferlist.loadLyXFile(s));
4626                         owner->getMiniBuffer()->Set(_("Document"),
4627                                                     MakeDisplayPath(s),
4628                                                     _("opened."));
4629                         return;
4630                 }
4631         }
4632
4633         // The template stuff
4634         string templname;
4635         if (fromTemplate) {
4636                 ProhibitInput();
4637                 fname = fileDlg.Select(_("Choose template"),
4638                                        lyxrc->template_path,
4639                                        "*.lyx");
4640                 templname = fname;
4641                 AllowInput();
4642         }
4643   
4644         // find a free buffer
4645         lyxerr.debug() << "Find a free buffer." << endl;
4646         owner->view()->buffer(bufferlist.newFile(s, templname));
4647 }
4648
4649
4650 void LyXFunc::MenuOpen()
4651 {
4652         string initpath = lyxrc->document_path;
4653         LyXFileDlg fileDlg;
4654   
4655         if (owner->view()->available()) {
4656                 string trypath = owner->buffer()->filepath;
4657                 // If directory is writeable, use this as default.
4658                 if (IsDirWriteable(trypath) == 1)
4659                         initpath = trypath;
4660         }
4661
4662         // launches dialog
4663         ProhibitInput();
4664         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
4665         fileDlg.SetButton(1, _("Examples"), 
4666                           AddPath(system_lyxdir, "examples"));
4667         string filename = fileDlg.Select(_("Select Document to Open"),
4668                                           initpath, "*.lyx");
4669         AllowInput();
4670  
4671         // check selected filename
4672         if (filename.empty()) {
4673                 owner->getMiniBuffer()->Set(_("Canceled."));
4674                 return;
4675         }
4676
4677         // get absolute path of file and make sure the filename ends
4678         // with .lyx
4679         filename = MakeAbsPath(filename);
4680         if (!IsLyXFilename(filename))
4681                 filename += ".lyx";
4682
4683         // loads document
4684         owner->getMiniBuffer()->Set(_("Opening document"),
4685                                     MakeDisplayPath(filename), "...");
4686         Buffer * openbuf = bufferlist.loadLyXFile(filename);
4687         if (openbuf) {
4688                 owner->view()->buffer(openbuf);
4689                 owner->getMiniBuffer()->Set(_("Document"),
4690                                             MakeDisplayPath(filename),
4691                                             _("opened."));
4692         } else {
4693                 owner->getMiniBuffer()->Set(_("Could not open document"),
4694                                             MakeDisplayPath(filename));
4695         }
4696 }
4697
4698
4699 void LyXFunc::doImportASCII(bool linorpar)
4700 {
4701         string initpath = lyxrc->document_path;
4702         LyXFileDlg fileDlg;
4703   
4704         if (owner->view()->available()) {
4705                 string trypath = owner->buffer()->filepath;
4706                 // If directory is writeable, use this as default.
4707                 if (IsDirWriteable(trypath) == 1)
4708                         initpath = trypath;
4709         }
4710
4711         // launches dialog
4712         ProhibitInput();
4713         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
4714         fileDlg.SetButton(1, _("Examples"), 
4715                           AddPath(system_lyxdir, "examples"));
4716         string filename = fileDlg.Select(_("Select ASCII file to Import"),
4717                                           initpath, "*.txt");
4718         AllowInput();
4719  
4720         // check selected filename
4721         if (filename.empty()) {
4722                 owner->getMiniBuffer()->Set(_("Canceled."));
4723                 return;
4724         }
4725
4726         // get absolute path of file
4727         filename = MakeAbsPath(filename);
4728
4729         string s = ChangeExtension(filename, ".lyx", false);
4730
4731         // Check if the document already is open
4732         if (bufferlist.exists(s)){
4733                 switch(AskConfirmation(_("Document is already open:"), 
4734                                        MakeDisplayPath(s, 50),
4735                                        _("Do you want to close that document now?\n"
4736                                          "('No' will just switch to the open version)")))
4737                 {
4738                 case 1: // Yes: close the document
4739                         if (!bufferlist.close(bufferlist.getBuffer(s)))
4740                                 // If close is canceled, we cancel here too.
4741                                 return;
4742                         break;
4743                 case 2: // No: switch to the open document
4744                         owner->view()->buffer(bufferlist.getBuffer(s));
4745                         return;
4746                 case 3: // Cancel: Do nothing
4747                         owner->getMiniBuffer()->Set(_("Canceled."));
4748                         return;
4749                 }
4750         }
4751
4752         // Check if a LyX document by the same root exists in filesystem
4753         FileInfo f(s, true);
4754         if (f.exist() && !AskQuestion(_("A document by the name"), 
4755                                       MakeDisplayPath(s),
4756                                       _("already exists. Overwrite?"))) {
4757                 owner->getMiniBuffer()->Set(_("Canceled."));
4758                 return;
4759         }
4760
4761         owner->view()->buffer(bufferlist.newFile(s, string()));
4762         owner->getMiniBuffer()->Set(_("Importing ASCII file"),
4763                                     MakeDisplayPath(filename), "...");
4764         // Insert ASCII file
4765         InsertAsciiFile(filename, linorpar);
4766         owner->getMiniBuffer()->Set(_("ASCII file "),
4767                                     MakeDisplayPath(filename),
4768                                     _("imported."));
4769 }
4770
4771
4772 void LyXFunc::doImportLaTeX(bool isnoweb)
4773 {
4774         string initpath = lyxrc->document_path;
4775         LyXFileDlg fileDlg;
4776   
4777         if (owner->view()->available()) {
4778                 string trypath = owner->buffer()->filepath;
4779                 // If directory is writeable, use this as default.
4780                 if (IsDirWriteable(trypath) == 1)
4781                         initpath = trypath;
4782         }
4783
4784         // launches dialog
4785         ProhibitInput();
4786         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
4787         fileDlg.SetButton(1, _("Examples"), 
4788                           AddPath(system_lyxdir, "examples"));
4789         string filename;
4790         if (isnoweb) {
4791                 filename = fileDlg.Select(_("Select Noweb file to Import"),
4792                                           initpath, "*.nw");
4793         } else {
4794                 filename = fileDlg.Select(_("Select LaTeX file to Import"),
4795                                           initpath, "*.tex");
4796         }
4797         
4798         AllowInput();
4799  
4800         // check selected filename
4801         if (filename.empty()) {
4802                 owner->getMiniBuffer()->Set(_("Canceled."));
4803                 return;
4804         }
4805
4806         // get absolute path of file
4807         filename = MakeAbsPath(filename);
4808
4809         // Check if the document already is open
4810         string LyXfilename = ChangeExtension(filename, ".lyx", false);
4811         if (bufferlist.exists(LyXfilename)){
4812                 switch(AskConfirmation(_("Document is already open:"), 
4813                                        MakeDisplayPath(LyXfilename, 50),
4814                                        _("Do you want to close that document now?\n"
4815                                          "('No' will just switch to the open version)")))
4816                 {
4817                 case 1: // Yes: close the document
4818                         if (!bufferlist.close(bufferlist.getBuffer(LyXfilename)))
4819                                 // If close is canceled, we cancel here too.
4820                                 return;
4821                         break;
4822                 case 2: // No: switch to the open document
4823                         owner->view()->buffer(
4824                                 bufferlist.getBuffer(LyXfilename));
4825                         return;
4826                 case 3: // Cancel: Do nothing
4827                         owner->getMiniBuffer()->Set(_("Canceled."));
4828                         return;
4829                 }
4830         }
4831
4832         // Check if a LyX document by the same root exists in filesystem
4833         FileInfo f(LyXfilename, true);
4834         if (f.exist() && !AskQuestion(_("A document by the name"), 
4835                                       MakeDisplayPath(LyXfilename),
4836                                       _("already exists. Overwrite?"))) {
4837                 owner->getMiniBuffer()->Set(_("Canceled."));
4838                 return;
4839         }
4840
4841         // loads document
4842         Buffer * openbuf;
4843         if (!isnoweb) {
4844                 owner->getMiniBuffer()->Set(_("Importing LaTeX file"),
4845                                             MakeDisplayPath(filename), "...");
4846                 ImportLaTeX myImport(filename);
4847                 openbuf = myImport.run();
4848         } else {
4849                 owner->getMiniBuffer()->Set(_("Importing Noweb file"),
4850                                             MakeDisplayPath(filename), "...");
4851                 ImportNoweb myImport(filename);
4852                 openbuf = myImport.run();
4853         }
4854         if (openbuf) {
4855                 owner->view()->buffer(openbuf);
4856                 owner->getMiniBuffer()->Set(isnoweb ?
4857                                             _("Noweb file ") : _("LateX file "),
4858                                             MakeDisplayPath(filename),
4859                                             _("imported."));
4860         } else {
4861                 owner->getMiniBuffer()->Set(isnoweb ?
4862                                             _("Could not import Noweb file") :
4863                                             _("Could not import LaTeX file"),
4864                                             MakeDisplayPath(filename));
4865         }
4866 }
4867
4868
4869 void LyXFunc::MenuInsertLyXFile(string const & filen)
4870 {
4871         string filename = filen;
4872
4873         if (filename.empty()) {
4874                 // Launch a file browser
4875                 string initpath = lyxrc->document_path;
4876                 LyXFileDlg fileDlg;
4877
4878                 if (owner->view()->available()) {
4879                         string trypath = owner->buffer()->filepath;
4880                         // If directory is writeable, use this as default.
4881                         if (IsDirWriteable(trypath) == 1)
4882                                 initpath = trypath;
4883                 }
4884
4885                 // launches dialog
4886                 ProhibitInput();
4887                 fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
4888                 fileDlg.SetButton(1, _("Examples"), 
4889                                   AddPath(system_lyxdir, "examples"));
4890                 filename = fileDlg.Select(_("Select Document to Insert"),
4891                                           initpath, "*.lyx");
4892                 AllowInput();
4893
4894                 // check selected filename
4895                 if (filename.empty()) {
4896                         owner->getMiniBuffer()->Set(_("Canceled."));
4897                         return;
4898                 }
4899         } 
4900
4901         // get absolute path of file and make sure the filename ends
4902         // with .lyx
4903         filename = MakeAbsPath(filename);
4904         if (!IsLyXFilename(filename))
4905                 filename += ".lyx";
4906
4907         // Inserts document
4908         owner->getMiniBuffer()->Set(_("Inserting document"),
4909                                     MakeDisplayPath(filename), "...");
4910         bool res = owner->buffer()->insertLyXFile(filename);
4911         if (res) {
4912                 owner->getMiniBuffer()->Set(_("Document"),
4913                                             MakeDisplayPath(filename),
4914                                             _("inserted."));
4915         } else {
4916                 owner->getMiniBuffer()->Set(_("Could not insert document"),
4917                                             MakeDisplayPath(filename));
4918         }
4919 }
4920
4921
4922 void LyXFunc::reloadBuffer()
4923 {
4924         string fn = owner->buffer()->fileName();
4925         if (bufferlist.close(owner->buffer()))
4926                 owner->view()->buffer(bufferlist.loadLyXFile(fn));
4927 }
4928
4929
4930 void LyXFunc::CloseBuffer()
4931 {
4932         if (bufferlist.close(owner->buffer()) && !quitting) {
4933                 if (bufferlist.empty()) {
4934                         // need this otherwise SEGV may occur while trying to
4935                         // set variables that don't exist
4936                         // since there's no current buffer
4937                         CloseAllBufferRelatedPopups();
4938                 }
4939                 else {
4940                         owner->view()->buffer(bufferlist.first());
4941                 }
4942         }
4943 }
4944
4945
4946 Inset * LyXFunc::getInsetByCode(Inset::Code code)
4947 {
4948         bool found = false;
4949         Inset * inset = 0;
4950 #ifdef MOVE_TEXT
4951         LyXCursor cursor = owner->view()->text->cursor;
4952 #else
4953         LyXCursor cursor = owner->buffer()->text->cursor;
4954 #endif
4955         LyXParagraph::size_type pos = cursor.pos;
4956         LyXParagraph * par = cursor.par;
4957         
4958         while (par && !found) {
4959                 while ((inset = par->ReturnNextInsetPointer(pos))){
4960                         if (inset->LyxCode() == code) {
4961                                 found = true;
4962                                 break;
4963                         }
4964                         pos++;
4965                 } 
4966                 par = par->next;
4967         }
4968         return (found) ? inset: 0;
4969 }
4970
4971
4972 // Each "owner" should have it's own message method. lyxview and
4973 // the minibuffer would use the minibuffer, but lyxserver would
4974 // send an ERROR signal to its client.  Alejandro 970603
4975 // This func is bit problematic when it comes to NLS, to make the
4976 // lyx servers client be language indepenent we must not translate
4977 // strings sent to this func.
4978 void LyXFunc::setErrorMessage(string const & m) const
4979 {
4980         dispatch_buffer = m;
4981         errorstat = true;
4982 }
4983
4984
4985 void LyXFunc::setMessage(string const & m)
4986 {
4987         dispatch_buffer = m;
4988 }