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