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