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