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