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