]> git.lyx.org Git - lyx.git/blob - src/lyxfunc.C
Only changed insert-date to date-insert conforming to standard :)
[lyx.git] / src / lyxfunc.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-1999 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12 #include <time.h>
13 #include <locale.h>
14
15 #include <cstdlib>
16 #include <cctype>
17 #include <cstring>
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #endif
22
23 #include "lyxlookup.h"
24 #include "kbmap.h"
25 #include "lyxfunc.h"
26 #include "bufferlist.h"
27 #include "lyxserver.h"
28 #include "lyx.h"
29 #include "intl.h"
30 #include "lyx_main.h"
31 #include "lyx_cb.h"
32 #include "LyXAction.h"
33 #if 0
34 #include "insets/insetlatex.h"
35 #endif
36 #include "insets/inseturl.h"
37 #include "insets/insetlatexaccent.h"
38 #include "insets/insettoc.h"
39 #include "insets/insetlof.h"
40 #include "insets/insetloa.h"
41 #include "insets/insetlot.h"
42 #include "insets/insetref.h"
43 #include "insets/insetparent.h"
44 #include "insets/insetindex.h"
45 #include "insets/insetinclude.h"
46 #include "insets/insetbib.h"
47 #include "mathed/formulamacro.h"
48 #include "toolbar.h"
49 #include "spellchecker.h" // RVDK_PATCH_5
50 #include "minibuffer.h"
51 #include "vspace.h"
52 #include "LyXView.h"
53 #include "filedlg.h"
54 #include "lyx_gui_misc.h"
55 #include "support/filetools.h"
56 #include "support/FileInfo.h"
57 #include "support/syscall.h"
58 #include "support/lstrings.h"
59 #include "support/path.h"
60 #include "lyxscreen.h"
61 #include "debug.h"
62 #include "lyxrc.h"
63 #include "lyxtext.h"
64 #include "gettext.h"
65 #include "trans_mgr.h"
66 #include "ImportLaTeX.h"
67 #include "ImportNoweb.h"
68 #include "layout.h"
69
70 extern bool cursor_follows_scrollbar;
71
72 extern void InsertAsciiFile(string const &, bool);
73 extern void math_insert_symbol(char const *);
74 extern Bool math_insert_greek(char const); // why "Bool"?
75 extern BufferList bufferlist;
76 extern LyXServer * lyxserver;
77 extern short greek_kb_flag;
78 extern FD_form_toc * fd_form_toc;
79 extern bool selection_possible;
80
81 extern kb_keymap * toplevel_keymap;
82
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 CreatePostscript(Buffer *, bool);
89 extern void MenuPrint(Buffer *);
90 extern void MenuSendto();
91 extern void QuitLyX();
92 extern void MenuFax(Buffer *);
93 extern void MenuExport(Buffer *, string const &);
94 extern void MenuPasteSelection(char at);
95 extern LyXAction lyxaction;
96 // (alkis)
97 extern tex_accent_struct get_accent(kb_action action);
98
99 extern void AutoSave();
100 extern void MenuSearch();
101 extern void SetUpdateTimer(float timer = 0.3);
102 extern void FreeUpdateTimer();
103 extern bool PreviewDVI(Buffer *);
104 extern bool PreviewPostscript(Buffer *);
105 extern void MenuInsertLabel(char const *);
106 extern void MenuInsertRef();
107 extern void MenuLayoutCharacter();
108 extern void MenuLayoutParagraph();
109 extern void MenuLayoutDocument();
110 extern void MenuLayoutPaper();
111 extern void MenuLayoutTable(int flag);
112 extern void MenuLayoutQuotes();
113 extern void MenuLayoutPreamble();
114 extern void MenuLayoutSave();
115 extern void bulletForm();
116
117 extern Buffer * NewLyxFile(string const &);
118 extern void LoadLyXFile(string const &);
119 extern void Reconfigure();
120
121 extern int current_layout;
122 extern int getISOCodeFromLaTeX(char *);
123
124 //extern int UnlockInset(UpdatableInset *);
125
126 extern void ShowLatexLog();
127
128 extern void UpdateInset(Inset * inset, bool mark_dirty = true);
129
130 /* === globals =========================================================== */
131
132 bool LyXFunc::show_sc = true;
133
134
135 LyXFunc::LyXFunc(LyXView * o)
136         : owner(o)
137 {
138         meta_fake_bit = 0;
139         lyx_dead_action = LFUN_NOACTION;
140         lyx_calling_dead_action = LFUN_NOACTION;
141         setupLocalKeymap();
142 }
143
144
145 // I changed this func slightly. I commented out the ...FinishUndo(),
146 // this means that all places that used to have a moveCursorUpdate, now
147 // have a ...FinishUndo() as the preceeding statement. I have also added
148 // a moveCursorUpdate to some of the functions that updated the cursor, but
149 // that did not show its new position.
150 inline
151 void LyXFunc::moveCursorUpdate(bool selecting)
152 {
153         if (selecting || owner->view()->text->mark_set) {
154                 owner->view()->text->SetSelection();
155                 owner->view()->getScreen()->ToggleToggle();
156                 owner->view()->update(0);
157         } else {
158                 owner->view()->update(-2); // this IS necessary
159                 // (Matthias) 
160         }
161
162         owner->view()->getScreen()->ShowCursor();
163         
164         /* ---> Everytime the cursor is moved, show the current font state. */
165         // should this too me moved out of this func?
166         //owner->getMiniBuffer()->Set(CurrentState());
167 }
168
169
170 int LyXFunc::processKeyEvent(XEvent * ev)
171 {
172         char s_r[10];
173         string argument;
174         XKeyEvent * keyevent = &ev->xkey;
175         KeySym keysym_return;
176
177         int num_bytes = LyXLookupString(ev, s_r, 10, &keysym_return);
178         s_r[num_bytes] = '\0';
179
180         if (lyxerr.debugging(Debug::KEY)) {
181                 char * tmp = XKeysymToString(keysym_return);
182                 string stm = (tmp ? tmp : "");
183                 lyxerr << "KeySym is "
184                        << stm
185                        << "["
186                        << keysym_return << "]"
187                        << " and num_bytes is "
188                        << num_bytes
189                        << " the string returned is \""
190                        << s_r << '\"' << endl;
191         }
192         // Do nothing if we have nothing (JMarc)
193         if (num_bytes == 0 && keysym_return == NoSymbol) {
194                 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
195                                    << endl;
196                 return 0;
197         }
198         
199         // this function should be used always [asierra060396]
200         if (owner->view()->available() &&
201             owner->view()->the_locking_inset &&
202             keysym_return == XK_Escape) {
203                 owner->view()->unlockInset(owner->view()->the_locking_inset);
204                 owner->view()->text->CursorRight();
205                 return 0;
206         }
207
208         // Can we be sure that this will work for all X-Windows
209         // implementations? (Lgb)
210         // This code snippet makes lyx ignore some keys. Perhaps
211         // all of them should be explictly mentioned?
212         if((keysym_return >= XK_Shift_L && keysym_return <= XK_Hyper_R)
213            || keysym_return == XK_Mode_switch || keysym_return == 0x0)
214                 return 0;
215
216         // Do a one-deep top-level lookup for
217         // cancel and meta-fake keys. RVDK_PATCH_5
218         cancel_meta_seq.reset();
219
220         int action = cancel_meta_seq.addkey(keysym_return, keyevent->state
221                                             &(ShiftMask|ControlMask
222                                               |Mod1Mask)); 
223
224         // When not cancel or meta-fake, do the normal lookup. 
225         // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
226         // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
227         if ( (action != LFUN_CANCEL) && (action != LFUN_META_FAKE) ) {
228
229                 // remove Caps Lock and Mod2 as a modifiers
230                 action = keyseq.addkey(keysym_return,
231                                        (keyevent->state | meta_fake_bit)
232                                        &(ShiftMask|ControlMask
233                                          |Mod1Mask));      
234         }
235         // Dont remove this unless you know what you are doing.
236         meta_fake_bit = 0;
237                 
238         if (action == 0) action = LFUN_PREFIX;
239
240         if (lyxerr.debugging(Debug::KEY)) {
241                 string buf;
242                 keyseq.print(buf);
243                 lyxerr << "Key ["
244                        << action << "]["
245                        << buf << "]["
246                        << num_bytes << "]" << endl;
247         }
248
249         // already here we know if it any point in going further
250         // why not return already here if action == -1 and
251         // num_bytes == 0? (Lgb)
252
253         if(keyseq.length > 1 || keyseq.length < -1){
254                 string buf;
255                 keyseq.print(buf);
256                 owner->getMiniBuffer()->Set(buf);
257         }
258
259         if (action == -1) {
260                 if (keyseq.length < -1) { // unknown key sequence...
261                         string buf;
262                         LyXBell();
263                         keyseq.print(buf);
264                         owner->getMiniBuffer()->Set(_("Unknown sequence:"), buf);
265                         return 0;
266                 }
267         
268                 char isochar = keyseq.getiso();
269                 if (!(keyevent->state&ControlMask) &&
270                     !(keyevent->state&Mod1Mask) &&
271                     (isochar && keysym_return < 0xF000)) {
272                         argument += isochar;
273                 }
274                 if (argument.empty()) {
275                         lyxerr.debug() << "Empty argument!" << endl;
276                         // This can`t possibly be of any use
277                         // so we`ll skip the dispatch.
278                         return 0;
279                 }
280         } else
281                 if (action == LFUN_SELFINSERT) {
282                         argument = s_r[0];
283                 }
284     
285         bool tmp_sc = show_sc;
286         show_sc = false;
287         Dispatch(action, argument.c_str());
288         show_sc = tmp_sc;
289         
290         return 0;
291
292
293
294 LyXFunc::func_status LyXFunc::getStatus(int ac) const
295 {
296         kb_action action;
297         func_status flag = LyXFunc::OK;
298         string argument;
299         Buffer * buf = owner->buffer();
300         
301         if (lyxaction.isPseudoAction(ac)) 
302                 action = lyxaction.retrieveActionArg(ac, argument);
303         else 
304                 action = static_cast<kb_action>(ac);
305         
306         if (action == LFUN_UNKNOWN_ACTION) {
307                 setErrorMessage(N_("Unknown action"));
308                 return LyXFunc::Unknown;
309         } 
310         
311         // Check whether we need a buffer
312         if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
313                 // Yes we need a buffer, do we have one?
314                 if (buf) {
315                         // yes
316                         // Can we use a readonly buffer?
317                         if (buf->isReadonly() && 
318                             !lyxaction.funcHasFlag(action,
319                                                    LyXAction::ReadOnly)) {
320                                 // no
321                                 setErrorMessage(N_("Document is read-only"));
322                                 flag |= LyXFunc::Disabled;
323                         }
324                 } else {
325                         // no
326                         setErrorMessage(N_("Command not allowed with"
327                                            "out any document open"));
328                         flag |= LyXFunc::Disabled;
329                 }
330         }
331
332         if (flag & LyXFunc::Disabled)
333                 return flag;
334
335         // I would really like to avoid having this switch and rather try to
336         // encode this in the function itself.
337         static bool noLaTeX = lyxrc->latex_command == "none";
338         bool disable = false;
339         switch (action) {
340         case LFUN_PREVIEW:
341                 disable = noLaTeX || lyxrc->view_dvi_command == "none";
342                 break;  
343         case LFUN_PREVIEWPS: 
344                 disable = noLaTeX || lyxrc->view_ps_command == "none";
345                 break;
346         case LFUN_RUNLATEX:
347         case LFUN_RUNDVIPS:
348                 disable = noLaTeX;
349                 break;
350         case LFUN_MENUPRINT:
351                 disable = noLaTeX || lyxrc->print_command == "none";
352                 break;
353         case LFUN_FAX:
354                 disable = noLaTeX || lyxrc->fax_command == "none"; 
355                 break;
356         case LFUN_IMPORT:
357                 if (argument == "latex")
358                         disable = lyxrc->relyx_command == "none";
359                 break;
360         case LFUN_EXPORT:
361                 if (argument == "dvi" || argument == "postscript")
362                         disable = noLaTeX;
363                 if (argument == "html")
364                         disable = lyxrc->html_command == "none";
365                 break;
366         case LFUN_UNDO:
367                 disable = buf->undostack.empty();
368                 break;
369         case LFUN_REDO:
370                 disable = buf->redostack.empty();
371                 break;
372         case LFUN_SPELLCHECK:
373                 disable = lyxrc->isp_command == "none";
374                 break;
375         case LFUN_RUNCHKTEX:
376                 disable = lyxrc->chktex_command == "none";
377                 break;
378         case LFUN_LAYOUT_TABLE:
379                 disable = ! owner->view()->text->cursor.par->table;
380                 break;
381         default:
382                 break;
383         }
384         if (disable)
385                 flag |= LyXFunc::Disabled;
386
387         if (buf) {
388                 func_status box = LyXFunc::ToggleOff;
389                 LyXFont font = owner->view()->text->real_current_font;
390                 switch (action) {
391                 case LFUN_EMPH:
392                         if (font.emph() == LyXFont::ON)
393                                 box = LyXFunc::ToggleOn;
394                         break;
395                 case LFUN_NOUN:
396                         if (font.noun() == LyXFont::ON)
397                                 box = LyXFunc::ToggleOn;
398                         break;
399                 case LFUN_BOLD:
400                         if (font.series() == LyXFont::BOLD_SERIES)
401                                 box = LyXFunc::ToggleOn;
402                         break;
403                 case LFUN_TEX:
404                         if (font.latex() == LyXFont::ON)
405                                 box = LyXFunc::ToggleOn;
406                         break;
407                 default:
408                         box = LyXFunc::OK;
409                         break;
410                 }
411                 flag |= box;
412         }
413
414         return flag;
415 }
416
417
418 string LyXFunc::Dispatch(string const & s) 
419 {
420         // Split command string into command and argument
421         string cmd, line = frontStrip(s);
422         string arg = strip(frontStrip(split(line, cmd, ' ')));
423
424         return Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
425 }
426
427
428 string LyXFunc::Dispatch(int ac,
429                          char const * do_not_use_this_arg)
430 {
431         string argument;
432         kb_action action;
433         
434         FL_OBJECT * ob = 0;  // This will disapear soon
435     
436         // we have not done anything wrong yet.
437         errorstat = false;
438         dispatch_buffer.clear();
439         
440         // if action is a pseudo-action, we need the real action
441         if (lyxaction.isPseudoAction(ac)) {
442                 string tmparg;
443                 action = static_cast<kb_action>
444                         (lyxaction.retrieveActionArg(ac, tmparg));
445                 if (!tmparg.empty())
446                         argument = tmparg;
447         } else {
448                 action = static_cast<kb_action>(ac);
449                 if (do_not_use_this_arg)
450                         argument = do_not_use_this_arg; // except here
451         }
452     
453         selection_possible = false;
454         
455         if (owner->view()->available() 
456             && owner->view()->getScreen())
457                 owner->view()->getScreen()->HideCursor();
458
459         // We cannot use this function here
460         if (getStatus(action) & Disabled)
461                 goto exit_with_message;
462
463         commandshortcut.clear();
464         
465         if (lyxrc->display_shortcuts && show_sc) {
466                 if (action != LFUN_SELFINSERT) {
467                         // Put name of command and list of shortcuts
468                         // for it in minibuffer
469                         string comname = lyxaction.getActionName(action);
470
471                         int pseudoaction = action;
472                         bool argsadded = false;
473
474                         if (!argument.empty()) {
475                                 // If we have the command with argument, 
476                                 // this is better
477                                 pseudoaction = 
478                                         lyxaction.searchActionArg(action,
479                                                                   argument.c_str());
480
481                                 if (pseudoaction == -1) {
482                                         pseudoaction = action;
483                                 } else {
484                                         comname += " " + argument;
485                                         argsadded = true;
486                                 }
487                         }
488
489                         string shortcuts = toplevel_keymap->findbinding(pseudoaction);
490
491                         if (!shortcuts.empty()) {
492                                 comname += ": " + shortcuts;
493                         } else if (!argsadded) {
494                                 comname += " " + argument;
495                         }
496
497                         if (!comname.empty()) {
498                                 comname = strip(comname);
499                                 commandshortcut = "(" + comname + ')';
500                                 owner->getMiniBuffer()->Set(commandshortcut);
501                                 // Here we could even add a small pause,
502                                 // to annoy the user and make him learn
503                                 // the shortcuts.
504                                 // No! That will just annoy, not teach
505                                 // anything. The user will read the messages
506                                 // if they are interested. (Asger)
507                         }
508                 }
509         }
510
511         // If in math mode pass the control to
512         // the math inset [asierra060396]
513         if (owner->view()->available() &&
514             owner->view()->the_locking_inset) {
515                 if (action > 1
516                     || (action == LFUN_UNKNOWN_ACTION
517                         && keyseq.length >= -1)) {
518                         if (action == LFUN_UNKNOWN_ACTION
519                             && argument.empty()) {
520                                 argument = keyseq.getiso();
521                         }
522                         // Undo/Redo pre 0.13 is a bit tricky for insets.
523                         if (action == LFUN_UNDO) {
524                                 int slx, sly;
525                                 UpdatableInset * inset = 
526                                         owner->view()->the_locking_inset;
527                                 inset->GetCursorPos(slx, sly);
528                                 owner->view()->unlockInset(inset);
529                                 owner->view()->menuUndo();
530                                 inset = static_cast<UpdatableInset*>(owner->view()->text->cursor.par->GetInset(owner->view()->text->cursor.pos));
531                                 if (inset) 
532                                         inset->Edit(slx, sly);
533                                 return string();
534                         } else 
535                                 if (action == LFUN_REDO) {
536                                         int slx, sly;
537                                         UpdatableInset * inset = owner->view()->the_locking_inset;
538                                         inset->GetCursorPos(slx, sly);
539                                         owner->view()->unlockInset(inset);
540                                         owner->view()->menuRedo();
541                                         inset = static_cast<UpdatableInset*>(owner->view()->text->cursor.par->GetInset(owner->view()->text->cursor.pos));
542                                         if (inset)
543                                                 inset->Edit(slx, sly);
544                                         return string();
545                                 } else
546                                         if (owner->view()->the_locking_inset->LocalDispatch(action, argument.c_str()))
547                                                 return string();
548                                         else {
549                                                 setMessage(N_("Text mode"));
550                                                 if (action == LFUN_RIGHT || action == -1)
551                                                         owner->view()->text->CursorRight();
552                                                 if (action == LFUN_LEFT || action == LFUN_RIGHT)
553                                                         return string();
554                                         }
555                 }
556         }
557
558         switch(action) {
559                 // --- Misc -------------------------------------------
560         case LFUN_WORDFINDFORWARD  : 
561         case LFUN_WORDFINDBACKWARD : {
562                 static string last_search;
563                 string searched_string;
564             
565                 if (!argument.empty()) {
566                         last_search = argument;
567                         searched_string = argument;
568                 } else {
569                         searched_string = last_search;
570                 }
571
572                 LyXText * ltCur = owner->view()->text ;
573
574                 if (!searched_string.empty() &&
575                     ((action == LFUN_WORDFINDBACKWARD) ? 
576                      ltCur->SearchBackward(searched_string.c_str()) :
577                      ltCur->SearchForward(searched_string.c_str()))) {
578
579                         // ??? What is that ???
580                         owner->view()->update(-2);
581
582                         // ??? Needed ???
583                         // clear the selection (if there is any) 
584                         owner->view()->getScreen()->ToggleSelection();
585                         owner->view()->text->ClearSelection();
586
587                         // Move cursor so that successive C-s 's will not stand in place. 
588                         if( action == LFUN_WORDFINDFORWARD ) 
589                                 owner->view()->text->CursorRightOneWord();
590                         owner->view()->text->FinishUndo();
591                         moveCursorUpdate(false);
592
593                         // ??? Needed ???
594                         // set the new selection 
595                         // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
596                         owner->view()->getScreen()->ToggleSelection(false);
597                 } else 
598                         LyXBell();      
599          
600                 // REMOVED : if (owner->view()->getWorkArea()->focus)
601                 owner->view()->getScreen()->ShowCursor();
602         }
603         break;
604
605         case LFUN_PREFIX:
606         {
607                 if (owner->view()->available()
608                     && owner->view()->getScreen()) {
609                         owner->view()->update(-2);
610                 }
611                 string buf;
612                 keyseq.print(buf, true);
613                 owner->getMiniBuffer()->Set(buf, string(), string(), 1);
614         }
615         break;
616
617         // --- Misc -------------------------------------------
618         case LFUN_EXEC_COMMAND:
619                 owner->getMiniBuffer()->ExecCommand(); 
620                 break;
621                 
622         case LFUN_CANCEL:                   // RVDK_PATCH_5
623                 keyseq.reset();
624                 meta_fake_bit = 0;
625                 if(owner->view()->available())
626                         // cancel any selection
627                         Dispatch(LFUN_MARK_OFF, 0);
628                 setMessage(N_("Cancel"));
629                 break;
630
631         case LFUN_META_FAKE:                                 // RVDK_PATCH_5
632         {
633                 meta_fake_bit = Mod1Mask;
634                 string buf;
635                 keyseq.print(buf, true);
636                 string res = string("M-") + buf;
637                 setMessage(buf); // RVDK_PATCH_5
638         }
639         break;  
640
641         case LFUN_READ_ONLY_TOGGLE:
642                 if (owner->buffer()->lyxvc.inUse()) {
643                         owner->buffer()->lyxvc.toggleReadOnly();
644                 } else {
645                         owner->buffer()->setReadonly(
646                                 !owner->buffer()->isReadonly());
647                 }
648                 break;
649                 
650         case LFUN_CENTER: // this is center and redraw.
651                 owner->view()->beforeChange();
652                 if (owner->view()->text->cursor.y >
653                     owner->view()->getWorkArea()->h / 2)        {
654                         owner->view()->getScreen()->
655                                 Draw(owner->view()->text->cursor.y -
656                                      owner->view()->getWorkArea()->h / 2);
657                 } else { // <= 
658                         owner->view()->getScreen()->
659                                 Draw(0);
660                 }
661                 owner->view()->update(0);
662                 owner->view()->redraw();
663                 break;
664                 
665         case LFUN_APPENDIX:
666                 if (owner->view()->available()) {
667                         owner->view()->text->toggleAppendix();
668                         owner->view()->update(1);
669                 }
670                 break;
671
672                 // --- Menus -----------------------------------------------
673         case LFUN_MENUNEW:
674                 MenuNew(false);
675                 break;
676                 
677         case LFUN_MENUNEWTMPLT:
678                 MenuNew(true);
679                 break;
680                 
681         case LFUN_MENUOPEN:
682                 MenuOpen();
683                 break;
684                 
685         case LFUN_CLOSEBUFFER:
686                 CloseBuffer();
687                 break;
688                 
689         case LFUN_MENUWRITE:
690                 MenuWrite(owner->buffer());
691                 break;
692                 
693         case LFUN_MENUWRITEAS:
694                 MenuWriteAs(owner->buffer());
695                 break;
696                 
697         case LFUN_MENURELOAD:
698                 reloadBuffer();
699                 break;
700                 
701         case LFUN_PREVIEW:
702                 PreviewDVI(owner->buffer());
703                 break;
704                         
705         case LFUN_PREVIEWPS:
706                 PreviewPostscript(owner->buffer());
707                 break;
708                 
709         case LFUN_RUNLATEX:
710                 MenuRunLaTeX(owner->buffer());
711                 break;
712                 
713         case LFUN_BUILDPROG:
714                 MenuBuildProg(owner->buffer());
715                 break;
716                 
717         case LFUN_RUNCHKTEX:
718                 MenuRunChktex(owner->buffer());
719                 break;
720                 
721         case LFUN_RUNDVIPS:
722                 CreatePostscript(owner->buffer(), false);
723                 break;
724                 
725         case LFUN_MENUPRINT:
726                 MenuPrint(owner->buffer());
727                 break;
728                 
729         case LFUN_FAX:
730                 MenuFax(owner->buffer());
731                 break;
732                         
733         case LFUN_EXPORT:
734                 MenuExport(owner->buffer(), argument);
735                 break;
736
737         case LFUN_IMPORT:
738         {
739                 //needs argument as string
740                 string imtyp = argument;
741                 
742                 // latex
743                 if (imtyp == "latex") {
744                         doImportLaTeX(false);
745                 }
746                 // ascii
747                 else if (imtyp == "ascii") {
748                         doImportASCII(false);
749                 } else if (imtyp == "asciiparagraph") {
750                         doImportASCII(true);
751                         // noweb
752                 } else if (imtyp == "noweb") {
753                         doImportLaTeX(true);
754                 } else {
755                         setErrorMessage(string(N_("Unknown import type: "))
756                                         + imtyp);
757                 }
758                 break;
759         }
760                 
761         case LFUN_QUIT:
762                 QuitLyX();
763                 break;
764                 
765         case LFUN_TOCVIEW:
766                 TocUpdateCB(ob, 0);
767                 if (fd_form_toc->form_toc->visible) {
768                         fl_raise_form(fd_form_toc->form_toc);
769                 } else {
770                         static int ow = -1, oh;
771                         fl_show_form(fd_form_toc->form_toc,
772                                      FL_PLACE_MOUSE |
773                                      FL_FREE_SIZE, FL_FULLBORDER,
774                                      _("Table of Contents"));
775                         if (ow < 0) {
776                                 ow = fd_form_toc->form_toc->w;
777                                 oh = fd_form_toc->form_toc->h;
778                         }
779                         fl_set_form_minsize(fd_form_toc->form_toc, ow, oh);
780                 }
781                 break;
782                 
783         case LFUN_TOC_INSERT:
784         {
785                 Inset * new_inset = new InsetTOC(owner->buffer());
786                 owner->view()->insertInset(new_inset, "Standard", true);
787                 break;
788         }
789         
790         case LFUN_LOF_INSERT:
791         {
792                 Inset * new_inset = new InsetLOF(owner->buffer());
793                 owner->view()->insertInset(new_inset, "Standard", true);
794                 break;
795         }
796         
797         case LFUN_LOA_INSERT:
798         {
799                 Inset * new_inset = new InsetLOA(owner->buffer());
800                 owner->view()->insertInset(new_inset, "Standard", true);
801                 break;
802         }
803
804         case LFUN_LOT_INSERT:
805         {
806                 Inset * new_inset = new InsetLOT(owner->buffer());
807                 owner->view()->insertInset(new_inset, "Standard", true);
808                 break;
809         }
810                 
811         case LFUN_TABLE:
812                 TableCB(ob, 0);
813                 break;
814                 
815         case LFUN_FIGURE:
816                 FigureCB(ob, 0);
817                 break;
818                 
819         case LFUN_AUTOSAVE:
820                 AutoSave();
821                 break;
822                 
823         case LFUN_UNDO:
824                 owner->view()->menuUndo();
825                 break;
826                 
827         case LFUN_REDO:
828                 owner->view()->menuRedo();
829                 break;
830                 
831         case LFUN_MENUSEARCH:
832                 MenuSearch();
833                 break;
834                 
835         case LFUN_PASTE:
836                 owner->view()->paste();
837                 break;
838                 
839         case LFUN_PASTESELECTION:
840         {
841                 bool asPara = false;
842                 if (argument == "paragraph") asPara = true;
843                 MenuPasteSelection(asPara);
844                 break;
845         }
846
847         case LFUN_CUT:
848                 owner->view()->cut();
849                 break;
850                 
851         case LFUN_COPY:
852                 owner->view()->copy();
853                 break;
854                 
855         case LFUN_LAYOUT_COPY:
856                 owner->view()->copyEnvironment();
857                 break;
858                 
859         case LFUN_LAYOUT_PASTE:
860                 owner->view()->pasteEnvironment();
861                 break;
862                 
863         case LFUN_GOTOERROR:
864                 owner->view()->gotoError();
865                 break;
866                 
867         case LFUN_REMOVEERRORS:
868                 if (owner->view()->removeAutoInsets()) {
869                         owner->view()->redraw();
870                         owner->view()->fitCursor();
871                         owner->view()->updateScrollbar();
872                 }
873                 break;
874                 
875         case LFUN_GOTONOTE:
876                 owner->view()->gotoNote();
877                 break;
878                 
879         case LFUN_OPENSTUFF:
880                 owner->view()->openStuff();
881                 break;
882                 
883         case LFUN_HYPHENATION:
884                 owner->view()->hyphenationPoint();
885                 break;
886                 
887         case LFUN_LDOTS:
888                 owner->view()->ldots();
889                 break;
890                 
891         case LFUN_END_OF_SENTENCE:
892                 owner->view()->endOfSentenceDot();
893                 break;
894
895         case LFUN_MENU_SEPARATOR:
896                 owner->view()->menuSeparator();
897                 break;
898                 
899         case LFUN_HFILL:
900                 owner->view()->hfill();
901                 break;
902                 
903         case LFUN_DEPTH:
904                 DepthCB(ob, 0);
905                 break;
906                 
907         case LFUN_DEPTH_MIN:
908                 DepthCB(ob, -1);
909                 break;
910                 
911         case LFUN_DEPTH_PLUS:
912                 DepthCB(ob, 1);
913                 break;
914                 
915         case LFUN_FREE:
916                 FreeCB();
917                 break;
918                 
919         case LFUN_TEX:
920                 TexCB();
921                 break;
922                 
923         case LFUN_MELT:
924                 MeltCB(ob, 0);
925                 break;
926                 
927         case LFUN_RECONFIGURE:
928                 Reconfigure();
929                 break;
930
931         case LFUN_FOOTMELT:
932                 if (owner->view()->available()
933                     && !owner->view()->text->selection
934                     && owner->view()->text->cursor.par->footnoteflag
935                     != LyXParagraph::NO_FOOTNOTE)
936                         { // only melt footnotes with FOOTMELT, not margins etc
937                                 if(owner->view()->text->cursor.par->footnotekind == LyXParagraph::FOOTNOTE)
938                                         MeltCB(ob, 0);
939                         }
940                 else
941                         FootCB(ob, 0); 
942                 break;
943
944         case LFUN_MARGINMELT:
945                 if (owner->view()->available()
946                     && !owner->view()->text->selection
947                     && owner->view()->text->cursor.par->footnoteflag
948                     != LyXParagraph::NO_FOOTNOTE) {
949                         // only melt margins
950                         if(owner->view()->text->cursor.par->footnotekind == LyXParagraph::MARGIN)
951                                 MeltCB(ob, 0);
952                 }
953                 else
954                         MarginCB(ob, 0); 
955                 break;
956                 
957                 // --- version control -------------------------------
958         case LFUN_VC_REGISTER:
959         {
960                 if (!owner->buffer()->lyxvc.inUse())
961                         owner->buffer()->lyxvc.registrer();
962         }
963         break;
964                 
965         case LFUN_VC_CHECKIN:
966         {
967                 if (owner->buffer()->lyxvc.inUse()
968                     && !owner->buffer()->isReadonly())
969                         owner->buffer()->lyxvc.checkIn();
970         }
971         break;
972                 
973         case LFUN_VC_CHECKOUT:
974         {
975                 if (owner->buffer()->lyxvc.inUse()
976                     && owner->buffer()->isReadonly())
977                         owner->buffer()->lyxvc.checkOut();
978         }
979         break;
980         
981         case LFUN_VC_REVERT:
982         {
983                 owner->buffer()->lyxvc.revert();
984         }
985         break;
986                 
987         case LFUN_VC_UNDO:
988         {
989                 owner->buffer()->lyxvc.undoLast();
990         }
991         break;
992                 
993         case LFUN_VC_HISTORY:
994         {
995                 owner->buffer()->lyxvc.showLog();
996                 break;
997         }
998         
999         // --- buffers ----------------------------------------
1000         case LFUN_PREVBUFFER:
1001 #ifdef WITH_WARNINGS
1002 #warning fix this please
1003 #endif
1004                 // it is the LyXView or the BufferView that should
1005                 // remember the previous buffer, not bufferlist.
1006 //                      if (owner->view()->available()){          
1007 //                              owner->view()->beforeChange();
1008 //                              owner->buffer()->update(-2);
1009 //                      }
1010 //                      owner->view()->setBuffer(bufferlist.prev());
1011
1012 //                      owner->view()->
1013 //                              resizeCurrentBufferPseudoExpose();
1014                 break;
1015                         
1016         case LFUN_FILE_INSERT:
1017         {
1018                 MenuInsertLyXFile(argument);
1019         }
1020         break;
1021         
1022         case LFUN_FILE_INSERT_ASCII:
1023         {
1024                 bool asPara = (argument == "paragraph");
1025                 InsertAsciiFile(string(), asPara);
1026         }
1027         break;
1028         
1029         case LFUN_FILE_NEW:
1030         {
1031                 // servercmd: argument must be <file>:<template>
1032                 Buffer * tmpbuf = NewLyxFile(argument);
1033                 if (tmpbuf)
1034                         owner->view()->buffer(tmpbuf);
1035         }
1036         break;
1037                         
1038         case LFUN_FILE_OPEN:
1039                 owner->view()->buffer(bufferlist.loadLyXFile(argument));
1040                 break;
1041
1042         case LFUN_LATEX_LOG:
1043                 ShowLatexLog();
1044                 break;
1045                 
1046         case LFUN_LAYOUTNO:
1047         {
1048                 lyxerr.debug() << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1049                 int sel = strToInt(argument);
1050                 lyxerr.debug() << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1051                 
1052                 // Should this give a setMessage instead?
1053                 if (sel == 0) 
1054                         return string(); // illegal argument
1055
1056                 --sel; // sel 1..., but layout 0...
1057
1058                 // Pretend we got the name instead.
1059                 Dispatch(int(LFUN_LAYOUT), 
1060                          textclasslist.NameOfLayout(owner->view()->
1061                                                     text->parameters->
1062                                                     textclass,
1063                                                     sel).c_str());
1064                 return string();
1065         }
1066                 
1067         case LFUN_LAYOUT:
1068         {
1069                 lyxerr.debug() << "LFUN_LAYOUT: (arg) "
1070                                << argument << endl;
1071                 
1072                 // Derive layout number from given argument (string)
1073                 // and current buffer's textclass (number). */    
1074                 int layoutno = 
1075                         textclasslist.NumberOfLayout(owner->
1076                                                      view()->
1077                                                      text->parameters->
1078                                                      textclass,
1079                                                      argument).second;
1080
1081                 // see if we found the layout number:
1082                 if (layoutno == -1) {
1083                         setErrorMessage(string(N_("Layout ")) + argument + 
1084                                         N_(" not known"));
1085                         break;
1086                 }
1087                         
1088                 if (current_layout != layoutno) {
1089                         owner->view()->getScreen()->HideCursor();
1090                         current_layout = layoutno;
1091                         owner->view()->update(-2);
1092                         owner->view()->text->
1093                                 SetLayout(layoutno);
1094                         owner->getToolbar()->combox->
1095                                 select(owner->view()->
1096                                        text->cursor.par->
1097                                        GetLayout() + 1);
1098                         owner->view()->update(1);
1099                 }
1100         }
1101         break;
1102
1103         case LFUN_LAYOUT_DOCUMENT:
1104                 MenuLayoutDocument();
1105                 break;
1106                 
1107         case LFUN_LAYOUT_PARAGRAPH:
1108                 MenuLayoutParagraph();
1109                 break;
1110                 
1111         case LFUN_LAYOUT_CHARACTER:
1112                 MenuLayoutCharacter();
1113                 break;
1114                 
1115         case LFUN_LAYOUT_TABLE:
1116         {
1117                 int flag = 0;
1118                 if (argument == "true") flag = 1;
1119                 MenuLayoutTable(flag);
1120         }
1121         break;
1122                 
1123         case LFUN_LAYOUT_PAPER:
1124                 MenuLayoutPaper();
1125                 break;
1126                 
1127         case LFUN_LAYOUT_QUOTES:
1128                 MenuLayoutQuotes();
1129                 break;
1130                 
1131         case LFUN_LAYOUT_PREAMBLE:
1132                 MenuLayoutPreamble();
1133                 break;
1134                 
1135         case LFUN_LAYOUT_SAVE_DEFAULT:
1136                 MenuLayoutSave();
1137                 break;
1138                 
1139         case LFUN_DROP_LAYOUTS_CHOICE:
1140                 owner->getToolbar()->combox->Show();
1141                 break;
1142
1143         case LFUN_EMPH:
1144                 EmphCB();
1145                 break;
1146                 
1147         case LFUN_BOLD:
1148                 BoldCB();
1149                 break;
1150                 
1151         case LFUN_NOUN:
1152                 NounCB();
1153                 break;
1154                 
1155         case LFUN_CODE:
1156                 CodeCB();
1157                 break;
1158                 
1159         case LFUN_SANS:
1160                 SansCB();
1161                 break;
1162                 
1163         case LFUN_ROMAN:
1164                 RomanCB();
1165                 break;
1166                 
1167         case LFUN_DEFAULT:
1168                 StyleResetCB();
1169                 break;
1170                 
1171         case LFUN_UNDERLINE:
1172                 UnderlineCB();
1173                 break;
1174                 
1175         case LFUN_FONT_SIZE:
1176                 FontSizeCB(argument);
1177                 break;
1178                 
1179         case LFUN_FONT_STATE:
1180                 setMessage(CurrentState());
1181                 break;
1182                 
1183         case LFUN_UPCASE_WORD:
1184                 owner->view()->update(-2);
1185                 FreeUpdateTimer();
1186                 owner->view()->text->ChangeWordCase(LyXText::text_uppercase);
1187                 owner->view()->update(1);
1188                 SetUpdateTimer();
1189                 break;
1190                 
1191         case LFUN_LOWCASE_WORD:
1192                 owner->view()->update(-2);
1193                 FreeUpdateTimer();
1194                 owner->view()->text->ChangeWordCase(LyXText::text_lowercase);
1195                 owner->view()->update(1);
1196                 SetUpdateTimer();
1197                 break;
1198                 
1199         case LFUN_CAPITALIZE_WORD:
1200                 owner->view()->update(-2);
1201                 FreeUpdateTimer();
1202                 owner->view()->text->ChangeWordCase(LyXText::text_capitalization);
1203                 owner->view()->update(1);
1204                 SetUpdateTimer();
1205                 break;
1206                 
1207         case LFUN_INSERT_LABEL:
1208                 MenuInsertLabel(argument.c_str());
1209                 break;
1210                 
1211         case LFUN_INSERT_REF:
1212                 MenuInsertRef();
1213                 break;
1214                 
1215         case LFUN_REFTOGGLE:
1216         {
1217                 InsetRef * inset = 
1218                         static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1219                 if (inset) {
1220                         if (inset->getFlag() == InsetRef::REF)
1221                                 inset->setFlag(InsetRef::PAGE_REF);
1222                         else
1223                                 inset->setFlag(InsetRef::REF);
1224                         UpdateInset(inset);
1225                 } else {
1226                         setErrorMessage(N_("No cross-reference to toggle"));
1227                 }
1228         }
1229         break;
1230         
1231         case LFUN_REFBACK:
1232         {
1233                 owner->view()->restorePosition();
1234         }
1235         break;
1236
1237         case LFUN_REFGOTO:
1238         {
1239                 string label(argument);
1240                 if (label.empty()) {
1241                         InsetRef * inset = 
1242                                 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1243                         if (inset)
1244                                 label = inset->getContents();
1245                 }
1246                 
1247                 if (!label.empty()) {
1248                         owner->view()->savePosition();
1249                         owner->view()->gotoLabel(label.c_str());
1250                 }
1251         }
1252         break;
1253                 
1254         case LFUN_MENU_OPEN_BY_NAME:
1255                 owner->getMenus()->openByName(argument);
1256                 break; // RVDK_PATCH_5
1257                 
1258         case LFUN_SPELLCHECK:
1259                 if (lyxrc->isp_command != "none")
1260                         ShowSpellChecker();
1261                 break; // RVDK_PATCH_5
1262                 
1263                 // --- Cursor Movements -----------------------------
1264         case LFUN_RIGHT:
1265         {
1266                 LyXText * tmptext = owner->view()->text;
1267                 if(!tmptext->mark_set)
1268                         owner->view()->beforeChange();
1269                 owner->view()->update(-2);
1270                 if (tmptext->cursor.pos < tmptext->cursor.par->Last()
1271                     && tmptext->cursor.par->GetChar(tmptext->cursor.pos)
1272                     == LyXParagraph::META_INSET
1273                     && tmptext->cursor.par->GetInset(tmptext->cursor.pos)
1274                     && tmptext->cursor.par->GetInset(tmptext->cursor.pos)->Editable() == 2){
1275                         Inset * tmpinset = tmptext->cursor.par->GetInset(tmptext->cursor.pos);
1276                         setMessage(tmpinset->EditMessage());
1277                         tmpinset->Edit(0, 0);
1278                         break;
1279                 }
1280                 tmptext->CursorRight();
1281                 owner->view()->text->FinishUndo();
1282                 moveCursorUpdate(false);
1283                 owner->getMiniBuffer()->Set(CurrentState());
1284         }
1285         break;
1286                 
1287         case LFUN_LEFT:
1288         {
1289                 // This is soooo ugly. Isn`t it possible to make
1290                 // it simpler? (Lgb)
1291                 LyXText * txt = owner->view()->text;
1292                 if(!txt->mark_set) owner->view()->beforeChange();
1293                 owner->view()->update(-2);
1294                 txt->CursorLeft();
1295                 if (txt->cursor.pos < txt->cursor.par->Last()
1296                     && txt->cursor.par->GetChar(txt->cursor.pos)
1297                     == LyXParagraph::META_INSET
1298                     && txt->cursor.par->GetInset(txt->cursor.pos)
1299                     && txt->cursor.par->GetInset(txt->cursor.pos)->Editable() == 2) {
1300                         Inset * tmpinset = txt->cursor.par->GetInset(txt->cursor.pos);
1301                         setMessage(tmpinset->EditMessage());
1302                         tmpinset->Edit(tmpinset->Width(txt->GetFont(txt->cursor.par,
1303                                                                     txt->cursor.pos)), 0);
1304                         break;
1305                 }
1306                 owner->view()->text->FinishUndo();
1307                 moveCursorUpdate(false);
1308                 owner->getMiniBuffer()->Set(CurrentState());
1309         }
1310         break;
1311                 
1312         case LFUN_UP:
1313                 if(!owner->view()->text->mark_set) owner->view()->beforeChange();
1314                 owner->view()->update(-3);
1315                 owner->view()->text->CursorUp();
1316                 owner->view()->text->FinishUndo();
1317                 moveCursorUpdate(false);
1318                 owner->getMiniBuffer()->Set(CurrentState());
1319                 break;
1320                 
1321         case LFUN_DOWN:
1322                 if(!owner->view()->text->mark_set)
1323                         owner->view()->beforeChange();
1324                 owner->view()->update(-3);
1325                 owner->view()->text->CursorDown();
1326                 owner->view()->text->FinishUndo();
1327                 moveCursorUpdate(false);
1328                 owner->getMiniBuffer()->Set(CurrentState());
1329                 break;
1330
1331         case LFUN_UP_PARAGRAPH:
1332                 if(!owner->view()->text->mark_set)
1333                         owner->view()->beforeChange();
1334                 owner->view()->update(-3);
1335                 owner->view()->text->CursorUpParagraph();
1336                 owner->view()->text->FinishUndo();
1337                 moveCursorUpdate(false);
1338                 owner->getMiniBuffer()->Set(CurrentState());
1339                 break;
1340                 
1341         case LFUN_DOWN_PARAGRAPH:
1342                 if(!owner->view()->text->mark_set)
1343                         owner->view()->beforeChange();
1344                 owner->view()->update(-3);
1345                 owner->view()->text->CursorDownParagraph();
1346                 owner->view()->text->FinishUndo();
1347                 moveCursorUpdate(false);
1348                 owner->getMiniBuffer()->Set(CurrentState());
1349                 break;
1350                 
1351         case LFUN_PRIOR:
1352                 if(!owner->view()->text->mark_set)
1353                         owner->view()->beforeChange();
1354                 owner->view()->update(-3);
1355                 owner->view()->cursorPrevious();
1356                 owner->view()->text->FinishUndo();
1357                 moveCursorUpdate(false);
1358                 owner->getMiniBuffer()->Set(CurrentState());
1359                 break;
1360                 
1361         case LFUN_NEXT:
1362                 if(!owner->view()->text->mark_set)
1363                         owner->view()->beforeChange();
1364                 owner->view()->update(-3);
1365                 owner->view()->cursorNext();
1366                 owner->view()->text->FinishUndo();
1367                 moveCursorUpdate(false);
1368                 owner->getMiniBuffer()->Set(CurrentState());
1369                 break;
1370                 
1371         case LFUN_HOME:
1372                 if(!owner->view()->text->mark_set)
1373                         owner->view()->beforeChange();
1374                 owner->view()->update(-2);
1375                 owner->view()->text->CursorHome();
1376                 owner->view()->text->FinishUndo();
1377                 moveCursorUpdate(false);
1378                 owner->getMiniBuffer()->Set(CurrentState());
1379                 break;
1380                 
1381         case LFUN_END:
1382                 if(!owner->view()->text->mark_set)
1383                         owner->view()->beforeChange();
1384                 owner->view()->update(-2);
1385                 owner->view()->text->CursorEnd();
1386                 owner->view()->text->FinishUndo();
1387                 moveCursorUpdate(false);
1388                 owner->getMiniBuffer()->Set(CurrentState());
1389                 break;
1390                 
1391         case LFUN_TAB:
1392                 if(!owner->view()->text->mark_set)
1393                         owner->view()->beforeChange();
1394                 owner->view()->update(-2);
1395                 owner->view()->text->CursorTab();
1396                 owner->view()->text->FinishUndo();
1397                 moveCursorUpdate(false);
1398                 owner->getMiniBuffer()->Set(CurrentState());
1399                 break;
1400                 
1401         case LFUN_WORDRIGHT:
1402                 if(!owner->view()->text->mark_set)
1403                         owner->view()->beforeChange();
1404                 owner->view()->update(-2);
1405                 owner->view()->text->CursorRightOneWord();
1406                 owner->view()->text->FinishUndo();
1407                 moveCursorUpdate(false);
1408                 owner->getMiniBuffer()->Set(CurrentState());
1409                 break;
1410                 
1411         case LFUN_WORDLEFT:
1412                 if(!owner->view()->text->mark_set)
1413                         owner->view()->beforeChange();
1414                 owner->view()->update(-2);
1415                 owner->view()->text->CursorLeftOneWord();
1416                 owner->view()->text->FinishUndo();
1417                 moveCursorUpdate(false);
1418                 owner->getMiniBuffer()->Set(CurrentState());
1419                 break;
1420                 
1421         case LFUN_BEGINNINGBUF:
1422                 if(!owner->view()->text->mark_set)
1423                         owner->view()->beforeChange();
1424                 owner->view()->update(-2);
1425                 owner->view()->text->CursorTop();
1426                 owner->view()->text->FinishUndo();
1427                 moveCursorUpdate(false);
1428                 owner->getMiniBuffer()->Set(CurrentState());
1429                 break;
1430                 
1431         case LFUN_ENDBUF:
1432                 if(!owner->view()->text->mark_set)
1433                         owner->view()->beforeChange();
1434                 owner->view()->update(-2);
1435                 owner->view()->text->CursorBottom();
1436                 owner->view()->text->FinishUndo();
1437                 moveCursorUpdate(false);
1438                 owner->getMiniBuffer()->Set(CurrentState());
1439                 break;
1440
1441       
1442                 /* cursor selection ---------------------------- */
1443         case LFUN_RIGHTSEL:
1444                 owner->view()->update(-2);
1445                 owner->view()->text->CursorRight();
1446                 owner->view()->text->FinishUndo();
1447                 moveCursorUpdate(true);
1448                 owner->getMiniBuffer()->Set(CurrentState());
1449                 break;
1450                 
1451         case LFUN_LEFTSEL:
1452                 owner->view()->update(-2);
1453                 owner->view()->text->CursorLeft();
1454                 owner->view()->text->FinishUndo();
1455                 moveCursorUpdate(true);
1456                 owner->getMiniBuffer()->Set(CurrentState());
1457                 break;
1458                 
1459         case LFUN_UPSEL:
1460                 owner->view()->update(-2);
1461                 owner->view()->text->CursorUp();
1462                 owner->view()->text->FinishUndo();
1463                 moveCursorUpdate(true);
1464                 owner->getMiniBuffer()->Set(CurrentState());
1465                 break;
1466                 
1467         case LFUN_DOWNSEL:
1468                 owner->view()->update(-2);
1469                 owner->view()->text->CursorDown();
1470                 owner->view()->text->FinishUndo();
1471                 moveCursorUpdate(true);
1472                 owner->getMiniBuffer()->Set(CurrentState());
1473                 break;
1474
1475         case LFUN_UP_PARAGRAPHSEL:
1476                 owner->view()->update(-2);
1477                 owner->view()->text->CursorUpParagraph();
1478                 owner->view()->text->FinishUndo();
1479                 moveCursorUpdate(true);
1480                 owner->getMiniBuffer()->Set(CurrentState());
1481                 break;
1482                 
1483         case LFUN_DOWN_PARAGRAPHSEL:
1484                 owner->view()->update(-2);
1485                 owner->view()->text->CursorDownParagraph();
1486                 owner->view()->text->FinishUndo();
1487                 moveCursorUpdate(true);
1488                 owner->getMiniBuffer()->Set(CurrentState());
1489                 break;
1490                 
1491         case LFUN_PRIORSEL:
1492                 owner->view()->update(-2);
1493                 owner->view()->cursorPrevious();
1494                 owner->view()->text->FinishUndo();
1495                 moveCursorUpdate(true);
1496                 owner->getMiniBuffer()->Set(CurrentState());
1497                 break;
1498                 
1499         case LFUN_NEXTSEL:
1500                 owner->view()->update(-2);
1501                 owner->view()->cursorNext();
1502                 owner->view()->text->FinishUndo();
1503                 moveCursorUpdate(true);
1504                 owner->getMiniBuffer()->Set(CurrentState());
1505                 break;
1506                 
1507         case LFUN_HOMESEL:
1508                 owner->view()->update(-2);
1509                 owner->view()->text->CursorHome();
1510                 owner->view()->text->FinishUndo();
1511                 moveCursorUpdate(true);
1512                 owner->getMiniBuffer()->Set(CurrentState());
1513                 break;
1514                 
1515         case LFUN_ENDSEL:
1516                 owner->view()->update(-2);
1517                 owner->view()->text->CursorEnd();
1518                 owner->view()->text->FinishUndo();
1519                 moveCursorUpdate(true);
1520                 owner->getMiniBuffer()->Set(CurrentState());
1521                 break;
1522                 
1523         case LFUN_WORDRIGHTSEL:
1524                 owner->view()->update(-2);
1525                 owner->view()->text->CursorRightOneWord();
1526                 owner->view()->text->FinishUndo();
1527                 moveCursorUpdate(true);
1528                 owner->getMiniBuffer()->Set(CurrentState());
1529                 break;
1530                 
1531         case LFUN_WORDLEFTSEL:
1532                 owner->view()->update(-2);
1533                 owner->view()->text->CursorLeftOneWord();
1534                 owner->view()->text->FinishUndo();
1535                 moveCursorUpdate(true);
1536                 owner->getMiniBuffer()->Set(CurrentState());
1537                 break;
1538                 
1539         case LFUN_BEGINNINGBUFSEL:
1540                 owner->view()->update(-2);
1541                 owner->view()->text->CursorTop();
1542                 owner->view()->text->FinishUndo();
1543                 moveCursorUpdate(true);
1544                 owner->getMiniBuffer()->Set(CurrentState());
1545                 break;
1546                 
1547         case LFUN_ENDBUFSEL:
1548                 owner->view()->update(-2);
1549                 owner->view()->text->CursorBottom();
1550                 owner->view()->text->FinishUndo();
1551                 moveCursorUpdate(true);
1552                 owner->getMiniBuffer()->Set(CurrentState());
1553                 break;
1554
1555                 // --- text changing commands ------------------------
1556         case LFUN_BREAKLINE:
1557                 owner->view()->beforeChange();
1558                 owner->view()->text->InsertChar(LyXParagraph::META_NEWLINE);
1559                 owner->view()->smallUpdate(1);
1560                 SetUpdateTimer(0.01);
1561                 moveCursorUpdate(false);
1562                 break;
1563                 
1564         case LFUN_PROTECTEDSPACE:
1565                 owner->view()->beforeChange();
1566                 owner->view()->text->
1567                         InsertChar(LyXParagraph::META_PROTECTED_SEPARATOR);
1568                 owner->view()->smallUpdate(1);
1569                 SetUpdateTimer();
1570                 moveCursorUpdate(false);
1571                 break;
1572                 
1573         case LFUN_SETMARK:
1574                 if(owner->view()->text->mark_set) {
1575                         owner->view()->beforeChange();
1576                         owner->view()->update(0);
1577                         setMessage(N_("Mark removed"));
1578                 } else {
1579                         owner->view()->beforeChange();
1580                         owner->view()->text->mark_set = 1;
1581                         owner->view()->update(0);
1582                         setMessage(N_("Mark set"));
1583                 }
1584                 owner->view()->text->sel_cursor = 
1585                         owner->view()->text->cursor;
1586                 break;
1587                 
1588         case LFUN_DELETE:
1589                 FreeUpdateTimer();
1590                 if (!owner->view()->text->selection) {
1591                         owner->view()->text->Delete();
1592                         owner->view()->text->sel_cursor = 
1593                                 owner->view()->text->cursor;
1594                         owner->view()->smallUpdate(1);
1595                         // It is possible to make it a lot faster still
1596                         // just comment out the lone below...
1597                         owner->view()->getScreen()->ShowCursor();
1598                 } else {
1599                         owner->view()->cut();
1600                 }
1601                 SetUpdateTimer();
1602                 break;
1603
1604         case LFUN_DELETE_SKIP:
1605         {
1606                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
1607                 
1608                 LyXCursor cursor = owner->view()->text->cursor;
1609
1610                 FreeUpdateTimer();
1611                 if (!owner->view()->text->selection) {
1612                         if (cursor.pos == cursor.par->Last()) {
1613                                 owner->view()->text->CursorRight();
1614                                 cursor = owner->view()->text->cursor;
1615                                 if (cursor.pos == 0
1616                                     && !(cursor.par->added_space_top 
1617                                          == VSpace (VSpace::NONE))) {
1618                                         owner->view()->text->SetParagraph
1619                                                 (cursor.par->line_top,
1620                                                  cursor.par->line_bottom,
1621                                                  cursor.par->pagebreak_top, 
1622                                                  cursor.par->pagebreak_bottom,
1623                                                  VSpace(VSpace::NONE), 
1624                                                  cursor.par->added_space_bottom,
1625                                                  cursor.par->align, 
1626                                                  cursor.par->labelwidthstring, 0);
1627                                         owner->view()->text->CursorLeft();
1628                                         owner->view()->update (1);
1629                                 } else {
1630                                         owner->view()->text->CursorLeft();
1631                                         owner->view()->text->Delete();
1632                                         owner->view()->text->sel_cursor = 
1633                                                 owner->view()->text->cursor;
1634                                         owner->view()->smallUpdate(1);
1635                                 }
1636                         } else {
1637                                 owner->view()->text->Delete();
1638                                 owner->view()->text->sel_cursor = 
1639                                         owner->view()->text->cursor;
1640                                 owner->view()->smallUpdate(1);
1641                         }
1642                 } else {
1643                         owner->view()->cut();
1644                 }
1645                 SetUpdateTimer();
1646         }
1647         break;
1648
1649         /* -------> Delete word forward. */
1650         case LFUN_DELETE_WORD_FORWARD:
1651                 owner->view()->update(-2);
1652                 FreeUpdateTimer();
1653                 owner->view()->text->DeleteWordForward();
1654                 owner->view()->update( 1 );
1655                 SetUpdateTimer();
1656                 moveCursorUpdate(false);
1657                 break;
1658
1659                 /* -------> Delete word backward. */
1660         case LFUN_DELETE_WORD_BACKWARD:
1661                 owner->view()->update(-2);
1662                 FreeUpdateTimer();
1663                 owner->view()->text->DeleteWordBackward();
1664                 owner->view()->update( 1 );
1665                 SetUpdateTimer();
1666                 moveCursorUpdate(false);
1667                 break;
1668                 
1669                 /* -------> Kill to end of line. */
1670         case LFUN_DELETE_LINE_FORWARD:
1671                 FreeUpdateTimer();
1672                 owner->view()->update(-2);
1673                 owner->view()->text->DeleteLineForward();
1674                 owner->view()->update( 1 );
1675                 SetUpdateTimer();
1676                 moveCursorUpdate(false);
1677                 break;
1678                 
1679                 /* -------> Set mark off. */
1680         case LFUN_MARK_OFF:
1681                 owner->view()->beforeChange();
1682                 owner->view()->update(0);
1683                 owner->view()->text->sel_cursor = 
1684                         owner->view()->text->cursor;
1685                 setMessage(N_("Mark off"));
1686                 break;
1687
1688                 /* -------> Set mark on. */
1689         case LFUN_MARK_ON:
1690                 owner->view()->beforeChange();
1691                 owner->view()->text->mark_set = 1;
1692                 owner->view()->update( 0 );
1693                 owner->view()->text->sel_cursor = 
1694                         owner->view()->text->cursor;
1695                 setMessage(N_("Mark on"));
1696                 break;
1697                 
1698         case LFUN_BACKSPACE:
1699         {
1700                 FreeUpdateTimer();
1701                 if (!owner->view()->text->selection) {
1702                         if (owner->getIntl()->getTrans()->backspace()) {
1703                                 owner->view()->text->Backspace();
1704                                 owner->view()->text->sel_cursor = 
1705                                         owner->view()->text->cursor;
1706                                 owner->view()->smallUpdate(1);
1707                                 // It is possible to make it a lot faster still
1708                                 // just comment out the lone below...
1709                                 owner->view()->getScreen()->ShowCursor();
1710                         }
1711                 } else {
1712                         owner->view()->cut();
1713                 }
1714                 SetUpdateTimer();
1715         }
1716         break;
1717
1718         case LFUN_BACKSPACE_SKIP:
1719         {
1720                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
1721                 
1722                 LyXCursor cursor = owner->view()->text->cursor;
1723                 
1724                 FreeUpdateTimer();
1725                 if (!owner->view()->text->selection) {
1726                         if (cursor.pos == 0 
1727                             && !(cursor.par->added_space_top 
1728                                  == VSpace (VSpace::NONE))) {
1729                                 owner->view()->text->SetParagraph 
1730                                         (cursor.par->line_top,      
1731                                          cursor.par->line_bottom,
1732                                          cursor.par->pagebreak_top, 
1733                                          cursor.par->pagebreak_bottom,
1734                                          VSpace(VSpace::NONE), cursor.par->added_space_bottom,
1735                                          cursor.par->align, 
1736                                          cursor.par->labelwidthstring, 0);
1737                                 owner->view()->update (1);
1738                         } else {
1739                                 owner->view()->text->Backspace();
1740                                 owner->view()->text->sel_cursor 
1741                                         = cursor;
1742                                 owner->view()->smallUpdate (1);
1743                         }
1744                 } else
1745                         owner->view()->cut();
1746                 SetUpdateTimer();
1747         }
1748         break;
1749
1750         case LFUN_BREAKPARAGRAPH:
1751         {
1752                 owner->view()->beforeChange();
1753                 owner->view()->text->BreakParagraph(0);
1754                 owner->view()->smallUpdate(1);
1755                 SetUpdateTimer(0.01);
1756                 owner->view()->text->sel_cursor = 
1757                         owner->view()->text->cursor;
1758                 break;
1759         }
1760
1761         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1762         {
1763                 owner->view()->beforeChange();
1764                 owner->view()->text->BreakParagraph(1);
1765                 owner->view()->smallUpdate(1);
1766                 SetUpdateTimer(0.01);
1767                 owner->view()->text->sel_cursor = 
1768                         owner->view()->text->cursor;
1769                 break;
1770         }
1771         
1772         case LFUN_BREAKPARAGRAPH_SKIP:
1773         {
1774                 // When at the beginning of a paragraph, remove
1775                 // indentation and add a "defskip" at the top.
1776                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
1777                 
1778                 LyXCursor cursor = owner->view()->text->cursor;
1779                 
1780                 owner->view()->beforeChange();
1781                 if (cursor.pos == 0) {
1782                         if (cursor.par->added_space_top == VSpace(VSpace::NONE)) {
1783                                 owner->view()->text->SetParagraph
1784                                         (cursor.par->line_top,      
1785                                          cursor.par->line_bottom,
1786                                          cursor.par->pagebreak_top, 
1787                                          cursor.par->pagebreak_bottom,
1788                                          VSpace(VSpace::DEFSKIP), cursor.par->added_space_bottom,
1789                                          cursor.par->align, 
1790                                          cursor.par->labelwidthstring, 1);
1791                                 owner->view()->update(1);
1792                         } 
1793                 }
1794                 else {
1795                         owner->view()->text->BreakParagraph(0);
1796                         owner->view()->smallUpdate(1);
1797                 }
1798                 SetUpdateTimer(0.01);
1799                 owner->view()->text->sel_cursor = cursor;
1800         }
1801         break;
1802         
1803         case LFUN_QUOTE:
1804                 owner->view()->beforeChange();
1805                 owner->view()->text->InsertChar('\"');  // This " matches the single quote in the code
1806                 owner->view()->smallUpdate(1);
1807                 SetUpdateTimer();
1808                 moveCursorUpdate(false);
1809                 break;
1810
1811         case LFUN_HTMLURL:
1812         case LFUN_URL:
1813         {
1814                 InsetCommand * new_inset;
1815                 if (action == LFUN_HTMLURL)
1816                         new_inset = new InsetUrl("htmlurl", "", "");
1817                 else
1818                         new_inset = new InsetUrl("url", "", "");
1819                 owner->view()->insertInset(new_inset);
1820                 new_inset->Edit(0, 0);
1821         }
1822         break;
1823
1824         // --- lyxserver commands ----------------------------
1825
1826         case LFUN_CHARATCURSOR:
1827         {
1828                 LyXParagraph::size_type pos = 
1829                         owner->view()->text->cursor.pos;
1830                 if(pos < owner->view()->text->cursor.par->size())
1831                         dispatch_buffer = owner->view()->text->
1832                                 cursor.par->text[pos];
1833                 else
1834                         dispatch_buffer = "EOF";
1835         }
1836         break;
1837         
1838         case LFUN_GETXY:
1839                 dispatch_buffer = 
1840                         tostr(owner->view()->text->cursor.x) + ' '
1841                         + tostr(owner->view()->text->cursor.y);
1842                 break;
1843                 
1844         case LFUN_SETXY:
1845         {
1846                 int  x;
1847                 long y;
1848                 sscanf(argument.c_str(), " %d %ld", &x, &y);
1849                 owner->view()->text->SetCursorFromCoordinates(x, y);
1850         }
1851         break;
1852         
1853         case LFUN_GETLAYOUT:
1854                 dispatch_buffer =  
1855                         tostr(owner->view()->text->cursor.par->layout);
1856                 break;
1857                         
1858         case LFUN_GETFONT:
1859         {
1860                 LyXFont * font = &(owner->view()->text->current_font);
1861                 if(font->shape() == LyXFont::ITALIC_SHAPE)
1862                         dispatch_buffer = 'E';
1863                 else if(font->shape() == LyXFont::SMALLCAPS_SHAPE)
1864                         dispatch_buffer = 'N';
1865                 else
1866                         dispatch_buffer = '0';
1867
1868         }
1869         break;
1870
1871         case LFUN_GETLATEX:
1872         {
1873                 LyXFont * font = &(owner->view()->text->current_font);
1874                 if(font->latex() == LyXFont::ON)
1875                         dispatch_buffer = 'L';
1876                 else
1877                         dispatch_buffer = '0';
1878         }
1879         break;
1880
1881         case LFUN_GETNAME:
1882                 setMessage(owner->buffer()->fileName());
1883                 lyxerr.debug() << "FNAME["
1884                                << owner->buffer()->fileName()
1885                                << "] " << endl;
1886                 break;
1887                 
1888         case LFUN_NOTIFY:
1889         {
1890                 string buf;
1891                 keyseq.print(buf);
1892                 dispatch_buffer = buf;
1893                 lyxserver->notifyClient(dispatch_buffer);
1894         }
1895         break;
1896
1897         case LFUN_GOTOFILEROW:
1898         {
1899                 char file_name[100];
1900                 int  row;
1901                 sscanf(argument.c_str(), " %s %d", file_name, &row);
1902
1903                 // Must replace extension of the file to be .lyx and get full path
1904                 string s = ChangeExtension(string(file_name), ".lyx", false);
1905
1906                 // Either change buffer or load the file
1907                 if (bufferlist.exists(s))
1908                         owner->view()->buffer(bufferlist.getBuffer(s));
1909                 else
1910                         owner->view()->buffer(bufferlist.loadLyXFile(s));
1911
1912                 // Set the cursor  
1913                 owner->view()->setCursorFromRow(row);
1914
1915                 // Recenter screen
1916                 owner->view()->beforeChange();
1917                 if (owner->view()->text->cursor.y >
1918                     owner->view()->getWorkArea()->h / 2)        {
1919                         owner->view()->getScreen()->
1920                                 Draw(owner->view()->text->cursor.y -
1921                                      owner->view()->getWorkArea()->h/2);
1922                 } else { // <= 
1923                         owner->view()->getScreen()->
1924                                 Draw(0);
1925                 }
1926                 owner->view()->update(0);
1927                 owner->view()->redraw();
1928         }
1929         break;
1930
1931         case LFUN_APROPOS:
1932         case LFUN_GETTIP:
1933         {
1934                 int qa = lyxaction.LookupFunc(argument.c_str());
1935                 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1936         }
1937         break;
1938
1939         // --- accented characters ---------------------------
1940                 
1941         case LFUN_UMLAUT:
1942         case LFUN_CIRCUMFLEX:
1943         case LFUN_GRAVE:
1944         case LFUN_ACUTE:
1945         case LFUN_TILDE:
1946         case LFUN_CEDILLA:
1947         case LFUN_MACRON:
1948         case LFUN_DOT:
1949         case LFUN_UNDERDOT:
1950         case LFUN_UNDERBAR:
1951         case LFUN_CARON:
1952         case LFUN_SPECIAL_CARON:
1953         case LFUN_BREVE:
1954         case LFUN_TIE:
1955         case LFUN_HUNG_UMLAUT:
1956         case LFUN_CIRCLE:
1957         case LFUN_OGONEK:
1958         {
1959                 char c;
1960                 
1961                 if (keyseq.length == -1 && keyseq.getiso() != 0) 
1962                         c = keyseq.getiso();
1963                 else
1964                         c = 0;
1965                 
1966                 owner->getIntl()->getTrans()->
1967                         deadkey(c, get_accent(action).accent, 
1968                                 owner->view()->text);
1969                 
1970                 // Need to reset, in case the minibuffer calls these
1971                 // actions
1972                 keyseq.reset();
1973                 keyseq.length = 0;
1974                 
1975                 // copied verbatim from do_accent_char
1976                 owner->view()->smallUpdate(1);
1977                 SetUpdateTimer();
1978                 owner->view()->text->sel_cursor = 
1979                         owner->view()->text->cursor;
1980         }   
1981         break;
1982         
1983         // --- toolbar ----------------------------------
1984         case LFUN_PUSH_TOOLBAR:
1985         {
1986                 int nth = strToInt(argument);
1987                 if (lyxerr.debugging(Debug::TOOLBAR)) {
1988                         lyxerr << "LFUN_PUSH_TOOLBAR: argument = `"
1989                                << argument << "'\n"
1990                                << "LFUN_PUSH_TOOLBAR: nth = `"
1991                                << nth << "'" << endl;
1992                 }
1993                 
1994                 if (nth <= 0) {
1995                         LyXBell();
1996                         setErrorMessage(N_("Push-toolbar needs argument > 0"));
1997                 } else {
1998                         owner->getToolbar()->push(nth);
1999                 }
2000         }
2001         break;
2002         
2003         case LFUN_ADD_TO_TOOLBAR:
2004         {
2005                 if (lyxerr.debugging(Debug::TOOLBAR)) {
2006                         lyxerr << "LFUN_ADD_TO_TOOLBAR:"
2007                                 "argument = `" << argument << '\'' << endl;
2008                 }
2009                 string tmp(argument);
2010                 //lyxerr <<string("Argument: ") + argument);
2011                 //lyxerr <<string("Tmp     : ") + tmp);
2012                 if (tmp.empty()) {
2013                         LyXBell();
2014                         setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
2015                 } else {
2016                         owner->getToolbar()->add(argument, false);
2017                         owner->getToolbar()->set();
2018                 }
2019         }
2020         break;
2021         
2022         // --- insert characters ----------------------------------------
2023 #if 0
2024         case LFUN_INSERT_INSET_LATEX:
2025         {
2026                 Inset * new_inset = new InsetLatex(argument);
2027                 owner->buffer()->insertInset(new_inset);
2028         }
2029         break;
2030 #endif
2031         // ---  Mathed stuff. If we are here, there is no locked inset yet.
2032         
2033         // Greek mode     
2034         case LFUN_GREEK:
2035         {
2036                 if (!greek_kb_flag) {
2037                         greek_kb_flag = 1;
2038                         setMessage(N_("Math greek mode on"));
2039                 } else
2040                         greek_kb_flag = 0;
2041         }  
2042         break;
2043       
2044         // Greek keyboard      
2045         case LFUN_GREEK_TOGGLE:
2046         {
2047                 greek_kb_flag = greek_kb_flag ? 0 : 2;
2048                 if (greek_kb_flag) {
2049                         setMessage(N_("Math greek keyboard on"));
2050                 } else {
2051                         setMessage(N_("Math greek keyboard off"));
2052                 }
2053         }
2054         break;
2055         
2056         case LFUN_MATH_DELIM:     
2057         case LFUN_INSERT_MATRIX:
2058         {          
2059                 if (owner->view()->available()) { 
2060                         owner->view()->
2061                                 open_new_inset(new InsetFormula(false));
2062                         owner->view()->
2063                                 the_locking_inset->LocalDispatch(action, argument.c_str());
2064                 }
2065         }          
2066         break;
2067                
2068         case LFUN_INSERT_MATH:
2069         {
2070                 math_insert_symbol(argument.c_str());
2071         }
2072         break;
2073         
2074         case LFUN_MATH_DISPLAY:
2075         {
2076                 if (owner->view()->available())
2077                         owner->view()->open_new_inset(new InsetFormula(true));
2078                 break;
2079         }
2080                     
2081         case LFUN_MATH_MACRO:
2082         {
2083                 if (owner->view()->available()) {
2084                         string s(argument);
2085                         if (s.empty())
2086                                 setErrorMessage(N_("Missing argument"));
2087                         else {
2088                                 string s1 = token(s, ' ', 1);
2089                                 int na = s1.empty() ? 0: atoi(s1.c_str());
2090                                 owner->view()->
2091                                         open_new_inset(new InsetFormulaMacro(token(s, ' ', 0), na));
2092                         }
2093                 }
2094         }
2095         break;
2096
2097         case LFUN_MATH_MODE:   // Open or create a math inset
2098         {
2099                 
2100                 if (owner->view()->available())
2101                         owner->view()->open_new_inset(new InsetFormula);
2102                 setMessage(N_("Math editor mode"));
2103         }
2104         break;
2105           
2106         case LFUN_MATH_NUMBER:
2107         case LFUN_MATH_LIMITS:
2108         {
2109                 setErrorMessage(N_("This is only allowed in math mode!"));
2110         }
2111         break;
2112         
2113         case LFUN_INSERT_CITATION:
2114         {   
2115                 InsetCitation * new_inset = new InsetCitation();
2116                 // ale970405
2117                 // The note, if any, must be after the key, delimited
2118                 // by a | so both key and remark can have spaces.
2119                 if (!argument.empty()) {
2120                         string lsarg(argument);
2121                         if (contains(lsarg, "|")) {
2122                                 new_inset->setContents(token(lsarg, '|', 0));
2123                                 new_inset->setOptions(token(lsarg, '|', 1));
2124                         } else
2125                                 new_inset->setContents(lsarg);
2126                         owner->view()->insertInset(new_inset);
2127                 } else {
2128                         owner->view()->insertInset(new_inset);
2129                         new_inset->Edit(0, 0);
2130                 }
2131         }
2132         break;
2133                     
2134         case LFUN_INSERT_BIBTEX:
2135         {   
2136                 // ale970405+lasgoutt970425
2137                 // The argument can be up to two tokens separated 
2138                 // by a space. The first one is the bibstyle.
2139                 string lsarg(argument);
2140                 string bibstyle = token(lsarg, ' ', 1);
2141                 if (bibstyle.empty())
2142                         bibstyle = "plain";
2143                 InsetBibtex * new_inset 
2144                         = new InsetBibtex(token(lsarg, ' ', 0),
2145                                           bibstyle,
2146                                           owner->buffer());
2147                 
2148                 owner->view()->insertInset(new_inset);
2149                 if (lsarg.empty()) {
2150                         new_inset->Edit(0, 0);
2151                 }
2152         }
2153         break;
2154                 
2155         // BibTeX data bases
2156         case LFUN_BIBDB_ADD:
2157         {
2158                 InsetBibtex * inset = 
2159                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2160                 if (inset) {
2161                         inset->addDatabase(argument);
2162                 }
2163         }
2164         break;
2165                     
2166         case LFUN_BIBDB_DEL:
2167         {
2168                 InsetBibtex * inset = 
2169                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2170                 if (inset) {
2171                         inset->delDatabase(argument);
2172                 }
2173         }
2174         break;
2175         
2176         case LFUN_BIBTEX_STYLE:
2177         {
2178                 InsetBibtex * inset = 
2179                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2180                 if (inset) {
2181                         inset->setOptions(argument);
2182                 }
2183         }
2184         break;
2185                 
2186         case LFUN_INDEX_INSERT:
2187         case LFUN_INDEX_INSERT_LAST:
2188         {
2189                 // Can't do that at the beginning of a paragraph.
2190                 if (owner->view()->text->cursor.pos - 1 < 0)
2191                         break;
2192
2193                 InsetIndex * new_inset = new InsetIndex();
2194                 if (!argument.empty()) {
2195                         string lsarg(argument);
2196                         new_inset->setContents(lsarg);
2197                         owner->view()->insertInset(new_inset);
2198                 } else {
2199                         //reh 98/09/21
2200                         //get the current word for an argument
2201                         LyXParagraph::size_type lastpos = 
2202                                 owner->view()->text->cursor.pos - 1;
2203                         // Get the current word. note that this must be done
2204                         // before inserting the inset, or the inset will
2205                         // break the word
2206                         string curstring(owner->view()
2207                                          ->text->cursor.par->GetWord(lastpos));
2208
2209                         //make the new inset and write the current word into it
2210                         InsetIndex * new_inset = new InsetIndex();
2211
2212                         new_inset->setContents(curstring);
2213
2214                         //don't edit it if the call was to INSERT_LAST
2215                         if(action != LFUN_INDEX_INSERT_LAST) {
2216                                 new_inset->Edit(0, 0);
2217                         } else {
2218                                 //it looks blank on the screen unless
2219                                 //we do  something.  put it here.
2220
2221                                 // move the cursor to the returned value of lastpos
2222                                 // but only for the auto-insert
2223                                 owner->view()->text->cursor.pos = lastpos;
2224                         }
2225
2226                         //put the new inset into the buffer.
2227                         // there should be some way of knowing the user
2228                         //cancelled & avoiding this, but i don't know how
2229                         owner->view()->insertInset(new_inset);
2230                 }
2231         }
2232         break;
2233
2234         case LFUN_INDEX_PRINT:
2235         {
2236                 Inset * new_inset = new InsetPrintIndex(owner->buffer());
2237                 owner->view()->insertInset(new_inset, "Standard", true);
2238         }
2239         break;
2240
2241         case LFUN_PARENTINSERT:
2242         {
2243                 lyxerr << "arg " << argument << endl;
2244                 Inset * new_inset = new InsetParent(argument, owner->buffer());
2245                 owner->view()->insertInset(new_inset, "Standard", true);
2246         }
2247         break;
2248
2249         case LFUN_CHILDINSERT:
2250         {
2251                 Inset * new_inset = new InsetInclude(argument,
2252                                                      owner->buffer());
2253                 owner->view()->insertInset(new_inset, "Standard", true);
2254                 new_inset->Edit(0, 0);
2255         }
2256         break;
2257
2258         case LFUN_CHILDOPEN:
2259         {
2260                 string filename =
2261                         MakeAbsPath(argument, 
2262                                     OnlyPath(owner->buffer()->fileName()));
2263                 setMessage(N_("Opening child document ") +
2264                            MakeDisplayPath(filename) + "...");
2265                 owner->view()->savePosition();
2266                 if (bufferlist.exists(filename))
2267                         owner->view()->buffer(bufferlist.getBuffer(filename));
2268                 else
2269                         owner->view()->buffer(bufferlist.loadLyXFile(filename));
2270         }
2271         break;
2272
2273         case LFUN_INSERT_NOTE:
2274                 owner->view()->insertNote();
2275                 break;
2276                 
2277         case LFUN_INSERTFOOTNOTE: 
2278         {
2279                 LyXParagraph::footnote_kind kind;
2280                 if (argument == "footnote")
2281                         { kind = LyXParagraph::FOOTNOTE; }
2282                 else if (argument == "margin")
2283                         { kind = LyXParagraph::MARGIN; }
2284                 else if (argument == "figure")
2285                         { kind = LyXParagraph::FIG; }
2286                 else if (argument == "table")
2287                         { kind = LyXParagraph::TAB; }
2288                 else if (argument == "wide-fig")
2289                         { kind = LyXParagraph::WIDE_FIG; }
2290                 else if (argument == "wide-tab")
2291                         { kind = LyXParagraph::WIDE_TAB; }
2292                 else if (argument == "algorithm")
2293                         { kind = LyXParagraph::ALGORITHM; }
2294                 else {
2295                         setErrorMessage(N_("Unknown kind of footnote"));
2296                         break;
2297                 }
2298                 owner->view()->text->InsertFootnoteEnvironment(kind);
2299                 owner->view()->update(1);
2300         }
2301         break;
2302         
2303         case LFUN_BUFFERBULLETSSELECT:
2304                 bulletForm();
2305                 break;
2306                 
2307         case LFUN_TOGGLECURSORFOLLOW:
2308                 cursor_follows_scrollbar = !cursor_follows_scrollbar;
2309                 break;
2310                 
2311         case LFUN_KMAP_OFF:             // keymap off
2312                 owner->getIntl()->KeyMapOn(false);
2313                 break;
2314                 
2315         case LFUN_KMAP_PRIM:    // primary keymap
2316                 owner->getIntl()->KeyMapPrim();
2317                 break;
2318                 
2319         case LFUN_KMAP_SEC:             // secondary keymap
2320                 owner->getIntl()->KeyMapSec();
2321                 break;
2322                 
2323         case LFUN_KMAP_TOGGLE:  // toggle keymap
2324                 owner->getIntl()->ToggleKeyMap();
2325                 break;
2326
2327         case LFUN_SELFINSERT:
2328         {
2329                 for (string::size_type i = 0; i < argument.length(); ++i) {
2330                         owner->view()->text->InsertChar(argument[i]);
2331                         // This needs to be in the loop, or else we
2332                         // won't break lines correctly. (Asger)
2333                         owner->view()->smallUpdate(1);
2334                 }
2335                 SetUpdateTimer();
2336                 owner->view()->text->sel_cursor = 
2337                         owner->view()->text->cursor;
2338                 moveCursorUpdate(false);
2339         }
2340         break;
2341
2342         case LFUN_SEQUENCE: 
2343         {
2344                 // argument contains ';'-terminated commands
2345                 while (argument.find(';') != string::npos) {
2346                         string first;
2347                         argument = split(argument, first, ';');
2348                         Dispatch(first);
2349                 }
2350         }
2351         break;
2352
2353         case LFUN_DATE_INSERT:  // jdblair: date-insert cmd
2354         {
2355                 char datetmp[32];
2356                 int datetmp_len;
2357                 time_t now_time_t;
2358                 struct tm *now_tm;
2359                 static string arg;
2360                 
2361                 now_time_t = time(NULL);
2362                 now_tm = localtime(&now_time_t);
2363                 (void)setlocale(LC_TIME, "");
2364                 if (!argument.empty())
2365                         arg = argument;
2366                 else if (arg.empty())
2367                         arg = lyxrc->date_insert_format;
2368                 datetmp_len = (int) strftime(datetmp, 32, arg.c_str(), now_tm);
2369                 for (int i = 0; i < datetmp_len; i++) {
2370                         owner->view()->text->InsertChar(datetmp[i]);
2371                         owner->view()->smallUpdate(1);
2372                 }
2373                 SetUpdateTimer();
2374                 owner->view()->text->sel_cursor = owner->view()->text->cursor;
2375                 moveCursorUpdate(false);
2376         }
2377         break;
2378
2379         case LFUN_SAVEPREFERENCES:
2380         {
2381                 Path p(user_lyxdir);
2382                 lyxrc->write("preferences");
2383         }
2384         break;
2385         
2386         case LFUN_UNKNOWN_ACTION:
2387         {
2388                 if(!owner->buffer()) {
2389                         LyXBell();
2390                         setErrorMessage(N_("No document open"));
2391                         break;
2392                 }
2393
2394                 if (owner->buffer()->isReadonly()) {
2395                         LyXBell();
2396                         setErrorMessage(N_("Document is read only"));
2397                         break;
2398                 }
2399                          
2400                 if (!argument.empty()) {
2401                         
2402                         /* Automatically delete the currently selected
2403                          * text and replace it with what is being
2404                          * typed in now. Depends on lyxrc settings
2405                          * "auto_region_delete", which defaults to
2406                          * true (on). */
2407                 
2408                         if ( lyxrc->auto_region_delete ) {
2409                                 if (owner->view()->text->selection){
2410                                         owner->view()->text->CutSelection(false);
2411                                         owner->view()->update(-1);
2412                                 }
2413                         }
2414                         
2415                         owner->view()->beforeChange();
2416                         for (string::size_type i = 0;
2417                              i < argument.length(); ++i) {
2418                                 if (greek_kb_flag) {
2419                                         if (!math_insert_greek(argument[i]))
2420                                                 owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2421                                 } else
2422                                         owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2423                         }
2424                         
2425                         owner->view()->smallUpdate(1);
2426                         SetUpdateTimer();
2427
2428                         owner->view()->text->sel_cursor = 
2429                                 owner->view()->text->cursor;
2430                         moveCursorUpdate(false);
2431                         return string();
2432                 } else {
2433                         // why is an "Unknown action" with empty
2434                         // argument even dispatched in the first
2435                         // place? I`ll probably change that. (Lgb)
2436                         LyXBell();
2437                         setErrorMessage(N_("Unknown action"));
2438                 }
2439                 break;
2440         default:
2441                 lyxerr << "A truly unknown func!" << endl;
2442                 break;
2443         }
2444         } // end of switch
2445   exit_with_message:
2446
2447         string res = getMessage();
2448
2449         if (res.empty()) {
2450                 if (!commandshortcut.empty()) {
2451                         string newbuf = owner->getMiniBuffer()->GetText();
2452                         if (newbuf != commandshortcut) {
2453                                 owner->getMiniBuffer()->Set(newbuf
2454                                                             + " " +
2455                                                             commandshortcut);
2456                         }
2457                 }
2458         } else {
2459                 owner->getMiniBuffer()->Set(string(_(res.c_str()))
2460                                             + " " + commandshortcut);
2461         }
2462
2463         return res;
2464 }
2465
2466
2467 void LyXFunc::setupLocalKeymap()
2468 {
2469         keyseq.stdmap = keyseq.curmap = toplevel_keymap;
2470         cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap;
2471 }
2472
2473
2474 void LyXFunc::MenuNew(bool fromTemplate)
2475 {
2476         string fname, initpath = lyxrc->document_path;
2477         LyXFileDlg fileDlg;
2478
2479         if (owner->view()->available()) {
2480                 string trypath = owner->buffer()->filepath;
2481                 // If directory is writeable, use this as default.
2482                 if (IsDirWriteable(trypath) == 1)
2483                         initpath = trypath;
2484         }
2485
2486         ProhibitInput();
2487         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2488         fileDlg.SetButton(1, _("Templates"), lyxrc->template_path);
2489         fname = fileDlg.Select(_("Enter Filename for new document"), 
2490                                initpath, "*.lyx", _("newfile"));
2491         AllowInput();
2492         
2493         if (fname.empty()) {
2494                 owner->getMiniBuffer()->Set(_("Canceled."));
2495                 lyxerr.debug() << "New Document Cancelled." << endl;
2496                 return;
2497         }
2498         
2499         // get absolute path of file and make sure the filename ends
2500         // with .lyx
2501         string s = MakeAbsPath(fname);
2502         if (!IsLyXFilename(s))
2503                 s += ".lyx";
2504
2505         // Check if the document already is open
2506         if (bufferlist.exists(s)){
2507                 switch(AskConfirmation(_("Document is already open:"), 
2508                                        MakeDisplayPath(s, 50),
2509                                        _("Do you want to close that document now?\n"
2510                                          "('No' will just switch to the open version)")))
2511                         {
2512                         case 1: // Yes: close the document
2513                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
2514                                 // If close is canceled, we cancel here too.
2515                                         return;
2516                                 break;
2517                         case 2: // No: switch to the open document
2518                                 owner->view()->buffer(bufferlist.getBuffer(s));
2519                                 return;
2520                         case 3: // Cancel: Do nothing
2521                                 owner->getMiniBuffer()->Set(_("Canceled."));
2522                                 return;
2523                         }
2524         }
2525         
2526         // Check whether the file already exists
2527         if (IsLyXFilename(s)) {
2528                 FileInfo fi(s);
2529                 if (fi.readable() &&
2530                     AskQuestion(_("File already exists:"), 
2531                                 MakeDisplayPath(s, 50),
2532                                 _("Do you want to open the document?"))) {
2533                         // loads document
2534                         owner->getMiniBuffer()->Set(_("Opening document"), 
2535                                                     MakeDisplayPath(s), "...");
2536                         XFlush(fl_display);
2537                         owner->view()->buffer(
2538                                 bufferlist.loadLyXFile(s));
2539                         owner->getMiniBuffer()->Set(_("Document"),
2540                                                     MakeDisplayPath(s),
2541                                                     _("opened."));
2542                         return;
2543                 }
2544         }
2545
2546         // The template stuff
2547         string templname;
2548         if (fromTemplate) {
2549                 ProhibitInput();
2550                 fname = fileDlg.Select(_("Choose template"),
2551                                        lyxrc->template_path,
2552                                        "*.lyx");
2553                 templname = fname;
2554                 AllowInput();
2555         }
2556   
2557         // find a free buffer
2558         lyxerr.debug() << "Find a free buffer." << endl;
2559         owner->view()->buffer(bufferlist.newFile(s, templname));
2560 }
2561
2562
2563 void LyXFunc::MenuOpen()
2564 {
2565         string initpath = lyxrc->document_path;
2566         LyXFileDlg fileDlg;
2567   
2568         if (owner->view()->available()) {
2569                 string trypath = owner->buffer()->filepath;
2570                 // If directory is writeable, use this as default.
2571                 if (IsDirWriteable(trypath) == 1)
2572                         initpath = trypath;
2573         }
2574
2575         // launches dialog
2576         ProhibitInput();
2577         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2578         fileDlg.SetButton(1, _("Examples"), 
2579                           AddPath(system_lyxdir, "examples"));
2580         string filename = fileDlg.Select(_("Select Document to Open"),
2581                                          initpath, "*.lyx");
2582         AllowInput();
2583  
2584         // check selected filename
2585         if (filename.empty()) {
2586                 owner->getMiniBuffer()->Set(_("Canceled."));
2587                 return;
2588         }
2589
2590         // get absolute path of file and make sure the filename ends
2591         // with .lyx
2592         filename = MakeAbsPath(filename);
2593         if (!IsLyXFilename(filename))
2594                 filename += ".lyx";
2595
2596         // loads document
2597         owner->getMiniBuffer()->Set(_("Opening document"),
2598                                     MakeDisplayPath(filename), "...");
2599         Buffer * openbuf = bufferlist.loadLyXFile(filename);
2600         if (openbuf) {
2601                 owner->view()->buffer(openbuf);
2602                 owner->getMiniBuffer()->Set(_("Document"),
2603                                             MakeDisplayPath(filename),
2604                                             _("opened."));
2605         } else {
2606                 owner->getMiniBuffer()->Set(_("Could not open document"),
2607                                             MakeDisplayPath(filename));
2608         }
2609 }
2610
2611
2612 void LyXFunc::doImportASCII(bool linorpar)
2613 {
2614         string initpath = lyxrc->document_path;
2615         LyXFileDlg fileDlg;
2616   
2617         if (owner->view()->available()) {
2618                 string trypath = owner->buffer()->filepath;
2619                 // If directory is writeable, use this as default.
2620                 if (IsDirWriteable(trypath) == 1)
2621                         initpath = trypath;
2622         }
2623
2624         // launches dialog
2625         ProhibitInput();
2626         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2627         fileDlg.SetButton(1, _("Examples"), 
2628                           AddPath(system_lyxdir, "examples"));
2629         string filename = fileDlg.Select(_("Select ASCII file to Import"),
2630                                          initpath, "*.txt");
2631         AllowInput();
2632  
2633         // check selected filename
2634         if (filename.empty()) {
2635                 owner->getMiniBuffer()->Set(_("Canceled."));
2636                 return;
2637         }
2638
2639         // get absolute path of file
2640         filename = MakeAbsPath(filename);
2641
2642         string s = ChangeExtension(filename, ".lyx", false);
2643
2644         // Check if the document already is open
2645         if (bufferlist.exists(s)) {
2646                 switch(AskConfirmation(_("Document is already open:"), 
2647                                        MakeDisplayPath(s, 50),
2648                                        _("Do you want to close that document now?\n"
2649                                          "('No' will just switch to the open version)")))
2650                         {
2651                         case 1: // Yes: close the document
2652                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
2653                                 // If close is canceled, we cancel here too.
2654                                         return;
2655                                 break;
2656                         case 2: // No: switch to the open document
2657                                 owner->view()->buffer(bufferlist.getBuffer(s));
2658                                 return;
2659                         case 3: // Cancel: Do nothing
2660                                 owner->getMiniBuffer()->Set(_("Canceled."));
2661                                 return;
2662                         }
2663         }
2664
2665         // Check if a LyX document by the same root exists in filesystem
2666         FileInfo f(s, true);
2667         if (f.exist() && !AskQuestion(_("A document by the name"), 
2668                                       MakeDisplayPath(s),
2669                                       _("already exists. Overwrite?"))) {
2670                 owner->getMiniBuffer()->Set(_("Canceled."));
2671                 return;
2672         }
2673
2674         owner->view()->buffer(bufferlist.newFile(s, string()));
2675         owner->getMiniBuffer()->Set(_("Importing ASCII file"),
2676                                     MakeDisplayPath(filename), "...");
2677         // Insert ASCII file
2678         InsertAsciiFile(filename, linorpar);
2679         owner->getMiniBuffer()->Set(_("ASCII file "),
2680                                     MakeDisplayPath(filename),
2681                                     _("imported."));
2682 }
2683
2684
2685 void LyXFunc::doImportLaTeX(bool isnoweb)
2686 {
2687         string initpath = lyxrc->document_path;
2688         LyXFileDlg fileDlg;
2689   
2690         if (owner->view()->available()) {
2691                 string trypath = owner->buffer()->filepath;
2692                 // If directory is writeable, use this as default.
2693                 if (IsDirWriteable(trypath) == 1)
2694                         initpath = trypath;
2695         }
2696
2697         // launches dialog
2698         ProhibitInput();
2699         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2700         fileDlg.SetButton(1, _("Examples"), 
2701                           AddPath(system_lyxdir, "examples"));
2702         string filename;
2703         if (isnoweb) {
2704                 filename = fileDlg.Select(_("Select Noweb file to Import"),
2705                                           initpath, "*.nw");
2706         } else {
2707                 filename = fileDlg.Select(_("Select LaTeX file to Import"),
2708                                           initpath, "*.tex");
2709         }
2710         
2711         AllowInput();
2712  
2713         // check selected filename
2714         if (filename.empty()) {
2715                 owner->getMiniBuffer()->Set(_("Canceled."));
2716                 return;
2717         }
2718
2719         // get absolute path of file
2720         filename = MakeAbsPath(filename);
2721
2722         // Check if the document already is open
2723         string LyXfilename = ChangeExtension(filename, ".lyx", false);
2724         if (bufferlist.exists(LyXfilename)){
2725                 switch(AskConfirmation(_("Document is already open:"), 
2726                                        MakeDisplayPath(LyXfilename, 50),
2727                                        _("Do you want to close that document now?\n"
2728                                          "('No' will just switch to the open version)")))
2729                         {
2730                         case 1: // Yes: close the document
2731                                 if (!bufferlist.close(bufferlist.getBuffer(LyXfilename)))
2732                                 // If close is canceled, we cancel here too.
2733                                         return;
2734                                 break;
2735                         case 2: // No: switch to the open document
2736                                 owner->view()->buffer(
2737                                         bufferlist.getBuffer(LyXfilename));
2738                                 return;
2739                         case 3: // Cancel: Do nothing
2740                                 owner->getMiniBuffer()->Set(_("Canceled."));
2741                                 return;
2742                         }
2743         }
2744
2745         // Check if a LyX document by the same root exists in filesystem
2746         FileInfo f(LyXfilename, true);
2747         if (f.exist() && !AskQuestion(_("A document by the name"), 
2748                                       MakeDisplayPath(LyXfilename),
2749                                       _("already exists. Overwrite?"))) {
2750                 owner->getMiniBuffer()->Set(_("Canceled."));
2751                 return;
2752         }
2753
2754         // loads document
2755         Buffer * openbuf;
2756         if (!isnoweb) {
2757                 owner->getMiniBuffer()->Set(_("Importing LaTeX file"),
2758                                             MakeDisplayPath(filename), "...");
2759                 ImportLaTeX myImport(filename);
2760                 openbuf = myImport.run();
2761         } else {
2762                 owner->getMiniBuffer()->Set(_("Importing Noweb file"),
2763                                             MakeDisplayPath(filename), "...");
2764                 ImportNoweb myImport(filename);
2765                 openbuf = myImport.run();
2766         }
2767         if (openbuf) {
2768                 owner->view()->buffer(openbuf);
2769                 owner->getMiniBuffer()->Set(isnoweb ?
2770                                             _("Noweb file ") : _("LateX file "),
2771                                             MakeDisplayPath(filename),
2772                                             _("imported."));
2773         } else {
2774                 owner->getMiniBuffer()->Set(isnoweb ?
2775                                             _("Could not import Noweb file") :
2776                                             _("Could not import LaTeX file"),
2777                                             MakeDisplayPath(filename));
2778         }
2779 }
2780
2781
2782 void LyXFunc::MenuInsertLyXFile(string const & filen)
2783 {
2784         string filename = filen;
2785
2786         if (filename.empty()) {
2787                 // Launch a file browser
2788                 string initpath = lyxrc->document_path;
2789                 LyXFileDlg fileDlg;
2790
2791                 if (owner->view()->available()) {
2792                         string trypath = owner->buffer()->filepath;
2793                         // If directory is writeable, use this as default.
2794                         if (IsDirWriteable(trypath) == 1)
2795                                 initpath = trypath;
2796                 }
2797
2798                 // launches dialog
2799                 ProhibitInput();
2800                 fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2801                 fileDlg.SetButton(1, _("Examples"), 
2802                                   AddPath(system_lyxdir, "examples"));
2803                 filename = fileDlg.Select(_("Select Document to Insert"),
2804                                           initpath, "*.lyx");
2805                 AllowInput();
2806
2807                 // check selected filename
2808                 if (filename.empty()) {
2809                         owner->getMiniBuffer()->Set(_("Canceled."));
2810                         return;
2811                 }
2812         } 
2813
2814         // get absolute path of file and make sure the filename ends
2815         // with .lyx
2816         filename = MakeAbsPath(filename);
2817         if (!IsLyXFilename(filename))
2818                 filename += ".lyx";
2819
2820         // Inserts document
2821         owner->getMiniBuffer()->Set(_("Inserting document"),
2822                                     MakeDisplayPath(filename), "...");
2823         bool res = owner->view()->insertLyXFile(filename);
2824         if (res) {
2825                 owner->getMiniBuffer()->Set(_("Document"),
2826                                             MakeDisplayPath(filename),
2827                                             _("inserted."));
2828         } else {
2829                 owner->getMiniBuffer()->Set(_("Could not insert document"),
2830                                             MakeDisplayPath(filename));
2831         }
2832 }
2833
2834
2835 void LyXFunc::reloadBuffer()
2836 {
2837         string fn = owner->buffer()->fileName();
2838         if (bufferlist.close(owner->buffer()))
2839                 owner->view()->buffer(bufferlist.loadLyXFile(fn));
2840 }
2841
2842
2843 void LyXFunc::CloseBuffer()
2844 {
2845         if (bufferlist.close(owner->buffer()) && !quitting) {
2846                 if (bufferlist.empty()) {
2847                         // need this otherwise SEGV may occur while trying to
2848                         // set variables that don't exist
2849                         // since there's no current buffer
2850                         CloseAllBufferRelatedPopups();
2851                 }
2852                 else {
2853                         owner->view()->buffer(bufferlist.first());
2854                 }
2855         }
2856 }
2857
2858
2859 Inset * LyXFunc::getInsetByCode(Inset::Code code)
2860 {
2861         bool found = false;
2862         Inset * inset = 0;
2863         LyXCursor cursor = owner->view()->text->cursor;
2864         LyXParagraph::size_type pos = cursor.pos;
2865         LyXParagraph * par = cursor.par;
2866         
2867         while (par && !found) {
2868                 while ((inset = par->ReturnNextInsetPointer(pos))){
2869                         if (inset->LyxCode() == code) {
2870                                 found = true;
2871                                 break;
2872                         }
2873                         ++pos;
2874                 } 
2875                 par = par->next;
2876         }
2877         return found ? inset : 0;
2878 }
2879
2880
2881 // Each "owner" should have it's own message method. lyxview and
2882 // the minibuffer would use the minibuffer, but lyxserver would
2883 // send an ERROR signal to its client.  Alejandro 970603
2884 // This func is bit problematic when it comes to NLS, to make the
2885 // lyx servers client be language indepenent we must not translate
2886 // strings sent to this func.
2887 void LyXFunc::setErrorMessage(string const & m) const
2888 {
2889         dispatch_buffer = m;
2890         errorstat = true;
2891 }
2892
2893
2894 void LyXFunc::setMessage(string const & m)
2895 {
2896         dispatch_buffer = m;
2897 }