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