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