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