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