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