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