]> git.lyx.org Git - lyx.git/blob - src/lyxfunc.C
31411e05936dd0862b6ad16e8e19678ef4d49c47
[lyx.git] / src / lyxfunc.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12 #include <time.h>
13 #include <locale.h>
14
15 #include <cstdlib>
16 #include <cctype>
17 #include <cstring>
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #endif
22
23 #include "lyxlookup.h"
24 #include "kbmap.h"
25 #include "lyxfunc.h"
26 #include "bufferlist.h"
27 #include "lyxserver.h"
28 #include "lyx.h"
29 #include "intl.h"
30 #include "lyx_main.h"
31 #include "lyx_cb.h"
32 #include "LyXAction.h"
33 #include "insets/inseturl.h"
34 #include "insets/insetlatexaccent.h"
35 #include "insets/insettoc.h"
36 #include "insets/insetlof.h"
37 #include "insets/insetloa.h"
38 #include "insets/insetlot.h"
39 #include "insets/insetref.h"
40 #include "insets/insetparent.h"
41 #include "insets/insetindex.h"
42 #include "insets/insetinclude.h"
43 #include "insets/insetbib.h"
44 #include "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
2133                                 ->LocalDispatch(owner->view(),
2134                                                 action,
2135                                                 argument);
2136                 }
2137         }          
2138         break;
2139                
2140         case LFUN_INSERT_MATH:
2141         {
2142                 math_insert_symbol(argument.c_str());
2143         }
2144         break;
2145         
2146         case LFUN_MATH_DISPLAY:
2147         {
2148                 if (owner->view()->available())
2149                         owner->view()->open_new_inset(new InsetFormula(true));
2150                 break;
2151         }
2152                     
2153         case LFUN_MATH_MACRO:
2154         {
2155                 if (owner->view()->available()) {
2156                         string s(argument);
2157                         if (s.empty())
2158                                 setErrorMessage(N_("Missing argument"));
2159                         else {
2160                                 string s1 = token(s, ' ', 1);
2161                                 int na = s1.empty() ? 0: atoi(s1.c_str());
2162                                 owner->view()->
2163                                         open_new_inset(new InsetFormulaMacro(token(s, ' ', 0), na));
2164                         }
2165                 }
2166         }
2167         break;
2168
2169         case LFUN_MATH_MODE:   // Open or create a math inset
2170         {
2171                 
2172                 if (owner->view()->available())
2173                         owner->view()->open_new_inset(new InsetFormula);
2174                 setMessage(N_("Math editor mode"));
2175         }
2176         break;
2177           
2178         case LFUN_MATH_NUMBER:
2179         case LFUN_MATH_LIMITS:
2180         {
2181                 setErrorMessage(N_("This is only allowed in math mode!"));
2182         }
2183         break;
2184         
2185         case LFUN_INSERT_CITATION:
2186         {   
2187                 InsetCitation * new_inset = new InsetCitation();
2188                 // ale970405
2189                 // The note, if any, must be after the key, delimited
2190                 // by a | so both key and remark can have spaces.
2191                 if (!argument.empty()) {
2192                         string lsarg(argument);
2193                         if (contains(lsarg, "|")) {
2194                                 new_inset->setContents(token(lsarg, '|', 0));
2195                                 new_inset->setOptions(token(lsarg, '|', 1));
2196                         } else
2197                                 new_inset->setContents(lsarg);
2198                         owner->view()->insertInset(new_inset);
2199                 } else {
2200                         owner->view()->insertInset(new_inset);
2201                         new_inset->Edit(owner->view(), 0, 0, 0);
2202                 }
2203         }
2204         break;
2205                     
2206         case LFUN_INSERT_BIBTEX:
2207         {   
2208                 // ale970405+lasgoutt970425
2209                 // The argument can be up to two tokens separated 
2210                 // by a space. The first one is the bibstyle.
2211                 string lsarg(argument);
2212                 string bibstyle = token(lsarg, ' ', 1);
2213                 if (bibstyle.empty())
2214                         bibstyle = "plain";
2215                 InsetBibtex * new_inset 
2216                         = new InsetBibtex(token(lsarg, ' ', 0),
2217                                           bibstyle,
2218                                           owner->buffer());
2219                 
2220                 owner->view()->insertInset(new_inset);
2221                 if (lsarg.empty()) {
2222                         new_inset->Edit(owner->view(), 0, 0, 0);
2223                 }
2224         }
2225         break;
2226                 
2227         // BibTeX data bases
2228         case LFUN_BIBDB_ADD:
2229         {
2230                 InsetBibtex * inset = 
2231                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2232                 if (inset) {
2233                         inset->addDatabase(argument);
2234                 }
2235         }
2236         break;
2237                     
2238         case LFUN_BIBDB_DEL:
2239         {
2240                 InsetBibtex * inset = 
2241                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2242                 if (inset) {
2243                         inset->delDatabase(argument);
2244                 }
2245         }
2246         break;
2247         
2248         case LFUN_BIBTEX_STYLE:
2249         {
2250                 InsetBibtex * inset = 
2251                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2252                 if (inset) {
2253                         inset->setOptions(argument);
2254                 }
2255         }
2256         break;
2257                 
2258         case LFUN_INDEX_INSERT:
2259         case LFUN_INDEX_INSERT_LAST:
2260         {
2261                 // Can't do that at the beginning of a paragraph.
2262                 if (owner->view()->text->cursor.pos - 1 < 0)
2263                         break;
2264
2265                 InsetIndex * new_inset = new InsetIndex();
2266                 if (!argument.empty()) {
2267                         string lsarg(argument);
2268                         new_inset->setContents(lsarg);
2269                         owner->view()->insertInset(new_inset);
2270                 } else {
2271                         //reh 98/09/21
2272                         //get the current word for an argument
2273                         LyXParagraph::size_type lastpos = 
2274                                 owner->view()->text->cursor.pos - 1;
2275                         // Get the current word. note that this must be done
2276                         // before inserting the inset, or the inset will
2277                         // break the word
2278                         string curstring(owner->view()
2279                                          ->text->cursor.par->GetWord(lastpos));
2280
2281                         //make the new inset and write the current word into it
2282                         InsetIndex * new_inset = new InsetIndex();
2283
2284                         new_inset->setContents(curstring);
2285
2286                         //don't edit it if the call was to INSERT_LAST
2287                         if(action != LFUN_INDEX_INSERT_LAST) {
2288                                 new_inset->Edit(owner->view(), 0, 0, 0);
2289                         } else {
2290                                 //it looks blank on the screen unless
2291                                 //we do  something.  put it here.
2292
2293                                 // move the cursor to the returned value of lastpos
2294                                 // but only for the auto-insert
2295                                 owner->view()->text->cursor.pos = lastpos;
2296                         }
2297
2298                         //put the new inset into the buffer.
2299                         // there should be some way of knowing the user
2300                         //cancelled & avoiding this, but i don't know how
2301                         owner->view()->insertInset(new_inset);
2302                 }
2303         }
2304         break;
2305
2306         case LFUN_INDEX_PRINT:
2307         {
2308                 Inset * new_inset = new InsetPrintIndex(owner->buffer());
2309                 owner->view()->insertInset(new_inset, "Standard", true);
2310         }
2311         break;
2312
2313         case LFUN_PARENTINSERT:
2314         {
2315                 lyxerr << "arg " << argument << endl;
2316                 Inset * new_inset = new InsetParent(argument, owner->buffer());
2317                 owner->view()->insertInset(new_inset, "Standard", true);
2318         }
2319         break;
2320
2321         case LFUN_CHILDINSERT:
2322         {
2323                 Inset * new_inset = new InsetInclude(argument,
2324                                                      owner->buffer());
2325                 owner->view()->insertInset(new_inset, "Standard", true);
2326                 new_inset->Edit(owner->view(), 0, 0, 0);
2327         }
2328         break;
2329
2330         case LFUN_CHILDOPEN:
2331         {
2332                 string filename =
2333                         MakeAbsPath(argument, 
2334                                     OnlyPath(owner->buffer()->fileName()));
2335                 setMessage(N_("Opening child document ") +
2336                            MakeDisplayPath(filename) + "...");
2337                 owner->view()->savePosition();
2338                 if (bufferlist.exists(filename))
2339                         owner->view()->buffer(bufferlist.getBuffer(filename));
2340                 else
2341                         owner->view()->buffer(bufferlist.loadLyXFile(filename));
2342         }
2343         break;
2344
2345         case LFUN_INSERT_NOTE:
2346                 owner->view()->insertNote();
2347                 break;
2348                 
2349         case LFUN_INSERTFOOTNOTE: 
2350         {
2351                 LyXParagraph::footnote_kind kind;
2352                 if (argument == "footnote")
2353                         { kind = LyXParagraph::FOOTNOTE; }
2354                 else if (argument == "margin")
2355                         { kind = LyXParagraph::MARGIN; }
2356                 else if (argument == "figure")
2357                         { kind = LyXParagraph::FIG; }
2358                 else if (argument == "table")
2359                         { kind = LyXParagraph::TAB; }
2360                 else if (argument == "wide-fig")
2361                         { kind = LyXParagraph::WIDE_FIG; }
2362                 else if (argument == "wide-tab")
2363                         { kind = LyXParagraph::WIDE_TAB; }
2364                 else if (argument == "algorithm")
2365                         { kind = LyXParagraph::ALGORITHM; }
2366                 else {
2367                         setErrorMessage(N_("Unknown kind of footnote"));
2368                         break;
2369                 }
2370                 owner->view()->text->InsertFootnoteEnvironment(kind);
2371                 owner->view()->update(1);
2372                 owner->view()->setState();
2373         }
2374         break;
2375         
2376         case LFUN_BUFFERBULLETSSELECT:
2377                 bulletForm();
2378                 break;
2379                 
2380         case LFUN_TOGGLECURSORFOLLOW:
2381                 cursor_follows_scrollbar = !cursor_follows_scrollbar;
2382                 break;
2383                 
2384         case LFUN_KMAP_OFF:             // keymap off
2385                 owner->getIntl()->KeyMapOn(false);
2386                 break;
2387                 
2388         case LFUN_KMAP_PRIM:    // primary keymap
2389                 owner->getIntl()->KeyMapPrim();
2390                 break;
2391                 
2392         case LFUN_KMAP_SEC:             // secondary keymap
2393                 owner->getIntl()->KeyMapSec();
2394                 break;
2395                 
2396         case LFUN_KMAP_TOGGLE:  // toggle keymap
2397                 owner->getIntl()->ToggleKeyMap();
2398                 break;
2399
2400         case LFUN_SELFINSERT:
2401         {
2402                 for (string::size_type i = 0; i < argument.length(); ++i) {
2403                         owner->view()->text->InsertChar(argument[i]);
2404                         // This needs to be in the loop, or else we
2405                         // won't break lines correctly. (Asger)
2406                         owner->view()->smallUpdate(1);
2407                 }
2408                 SetUpdateTimer();
2409                 owner->view()->text->sel_cursor = 
2410                         owner->view()->text->cursor;
2411                 moveCursorUpdate(false);
2412         }
2413         break;
2414
2415         case LFUN_SEQUENCE: 
2416         {
2417                 // argument contains ';'-terminated commands
2418                 while (argument.find(';') != string::npos) {
2419                         string first;
2420                         argument = split(argument, first, ';');
2421                         Dispatch(first);
2422                 }
2423         }
2424         break;
2425
2426         case LFUN_DATE_INSERT:  // jdblair: date-insert cmd
2427         {
2428                 char datetmp[32];
2429                 int datetmp_len;
2430                 time_t now_time_t;
2431                 struct tm *now_tm;
2432                 static string arg;
2433                 
2434                 now_time_t = time(NULL);
2435                 now_tm = localtime(&now_time_t);
2436                 (void)setlocale(LC_TIME, "");
2437                 if (!argument.empty())
2438                         arg = argument;
2439                 else if (arg.empty())
2440                         arg = lyxrc->date_insert_format;
2441                 datetmp_len = (int) strftime(datetmp, 32, arg.c_str(), now_tm);
2442                 for (int i = 0; i < datetmp_len; i++) {
2443                         owner->view()->text->InsertChar(datetmp[i]);
2444                         owner->view()->smallUpdate(1);
2445                 }
2446                 SetUpdateTimer();
2447                 owner->view()->text->sel_cursor = owner->view()->text->cursor;
2448                 moveCursorUpdate(false);
2449         }
2450         break;
2451
2452         case LFUN_SAVEPREFERENCES:
2453         {
2454                 Path p(user_lyxdir);
2455                 lyxrc->write("preferences");
2456         }
2457         break;
2458         
2459         case LFUN_UNKNOWN_ACTION:
2460         {
2461                 if(!owner->buffer()) {
2462                         LyXBell();
2463                         setErrorMessage(N_("No document open"));
2464                         break;
2465                 }
2466
2467                 if (owner->buffer()->isReadonly()) {
2468                         LyXBell();
2469                         setErrorMessage(N_("Document is read only"));
2470                         break;
2471                 }
2472                          
2473                 if (!argument.empty()) {
2474                         
2475                         /* Automatically delete the currently selected
2476                          * text and replace it with what is being
2477                          * typed in now. Depends on lyxrc settings
2478                          * "auto_region_delete", which defaults to
2479                          * true (on). */
2480                 
2481                         if ( lyxrc->auto_region_delete ) {
2482                                 if (owner->view()->text->selection){
2483                                         owner->view()->text->CutSelection(false);
2484                                         owner->view()->update(-1);
2485                                 }
2486                         }
2487                         
2488                         owner->view()->beforeChange();
2489                         for (string::size_type i = 0;
2490                              i < argument.length(); ++i) {
2491                                 if (greek_kb_flag) {
2492                                         if (!math_insert_greek(argument[i]))
2493                                                 owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2494                                 } else
2495                                         owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2496                         }
2497                         
2498                         owner->view()->smallUpdate(1);
2499                         SetUpdateTimer();
2500
2501                         owner->view()->text->sel_cursor = 
2502                                 owner->view()->text->cursor;
2503                         moveCursorUpdate(false);
2504                         return string();
2505                 } else {
2506                         // why is an "Unknown action" with empty
2507                         // argument even dispatched in the first
2508                         // place? I`ll probably change that. (Lgb)
2509                         LyXBell();
2510                         setErrorMessage(N_("Unknown action"));
2511                 }
2512                 break;
2513         default:
2514                 lyxerr << "A truly unknown func!" << endl;
2515                 break;
2516         }
2517         } // end of switch
2518   exit_with_message:
2519
2520         string res = getMessage();
2521
2522         if (res.empty()) {
2523                 if (!commandshortcut.empty()) {
2524                         string newbuf = owner->getMiniBuffer()->GetText();
2525                         if (newbuf != commandshortcut) {
2526                                 owner->getMiniBuffer()->Set(newbuf
2527                                                             + " " +
2528                                                             commandshortcut);
2529                         }
2530                 }
2531         } else {
2532                 owner->getMiniBuffer()->Set(string(_(res.c_str()))
2533                                             + " " + commandshortcut);
2534         }
2535
2536         return res;
2537 }
2538
2539
2540 void LyXFunc::setupLocalKeymap()
2541 {
2542         keyseq.stdmap = keyseq.curmap = toplevel_keymap;
2543         cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap;
2544 }
2545
2546
2547 void LyXFunc::MenuNew(bool fromTemplate)
2548 {
2549         string fname, initpath = lyxrc->document_path;
2550         LyXFileDlg fileDlg;
2551
2552         if (owner->view()->available()) {
2553                 string trypath = owner->buffer()->filepath;
2554                 // If directory is writeable, use this as default.
2555                 if (IsDirWriteable(trypath) == 1)
2556                         initpath = trypath;
2557         }
2558
2559         ProhibitInput();
2560         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2561         fileDlg.SetButton(1, _("Templates"), lyxrc->template_path);
2562         fname = fileDlg.Select(_("Enter Filename for new document"), 
2563                                initpath, "*.lyx", _("newfile"));
2564         AllowInput();
2565         
2566         if (fname.empty()) {
2567                 owner->getMiniBuffer()->Set(_("Canceled."));
2568                 lyxerr.debug() << "New Document Cancelled." << endl;
2569                 return;
2570         }
2571         
2572         // get absolute path of file and make sure the filename ends
2573         // with .lyx
2574         string s = MakeAbsPath(fname);
2575         if (!IsLyXFilename(s))
2576                 s += ".lyx";
2577
2578         // Check if the document already is open
2579         if (bufferlist.exists(s)){
2580                 switch(AskConfirmation(_("Document is already open:"), 
2581                                        MakeDisplayPath(s, 50),
2582                                        _("Do you want to close that document now?\n"
2583                                          "('No' will just switch to the open version)")))
2584                         {
2585                         case 1: // Yes: close the document
2586                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
2587                                 // If close is canceled, we cancel here too.
2588                                         return;
2589                                 break;
2590                         case 2: // No: switch to the open document
2591                                 owner->view()->buffer(bufferlist.getBuffer(s));
2592                                 return;
2593                         case 3: // Cancel: Do nothing
2594                                 owner->getMiniBuffer()->Set(_("Canceled."));
2595                                 return;
2596                         }
2597         }
2598         
2599         // Check whether the file already exists
2600         if (IsLyXFilename(s)) {
2601                 FileInfo fi(s);
2602                 if (fi.readable() &&
2603                     AskQuestion(_("File already exists:"), 
2604                                 MakeDisplayPath(s, 50),
2605                                 _("Do you want to open the document?"))) {
2606                         // loads document
2607                         owner->getMiniBuffer()->Set(_("Opening document"), 
2608                                                     MakeDisplayPath(s), "...");
2609                         XFlush(fl_display);
2610                         owner->view()->buffer(
2611                                 bufferlist.loadLyXFile(s));
2612                         owner->getMiniBuffer()->Set(_("Document"),
2613                                                     MakeDisplayPath(s),
2614                                                     _("opened."));
2615                         return;
2616                 }
2617         }
2618
2619         // The template stuff
2620         string templname;
2621         if (fromTemplate) {
2622                 ProhibitInput();
2623                 fname = fileDlg.Select(_("Choose template"),
2624                                        lyxrc->template_path,
2625                                        "*.lyx");
2626                 templname = fname;
2627                 AllowInput();
2628         }
2629   
2630         // find a free buffer
2631         lyxerr.debug() << "Find a free buffer." << endl;
2632         owner->view()->buffer(bufferlist.newFile(s, templname));
2633 }
2634
2635
2636 void LyXFunc::MenuOpen()
2637 {
2638         string initpath = lyxrc->document_path;
2639         LyXFileDlg fileDlg;
2640   
2641         if (owner->view()->available()) {
2642                 string trypath = owner->buffer()->filepath;
2643                 // If directory is writeable, use this as default.
2644                 if (IsDirWriteable(trypath) == 1)
2645                         initpath = trypath;
2646         }
2647
2648         // launches dialog
2649         ProhibitInput();
2650         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2651         fileDlg.SetButton(1, _("Examples"), 
2652                           AddPath(system_lyxdir, "examples"));
2653         string filename = fileDlg.Select(_("Select Document to Open"),
2654                                          initpath, "*.lyx");
2655         AllowInput();
2656  
2657         // check selected filename
2658         if (filename.empty()) {
2659                 owner->getMiniBuffer()->Set(_("Canceled."));
2660                 return;
2661         }
2662
2663         // get absolute path of file and make sure the filename ends
2664         // with .lyx
2665         filename = MakeAbsPath(filename);
2666         if (!IsLyXFilename(filename))
2667                 filename += ".lyx";
2668
2669         // loads document
2670         owner->getMiniBuffer()->Set(_("Opening document"),
2671                                     MakeDisplayPath(filename), "...");
2672         Buffer * openbuf = bufferlist.loadLyXFile(filename);
2673         if (openbuf) {
2674                 owner->view()->buffer(openbuf);
2675                 owner->getMiniBuffer()->Set(_("Document"),
2676                                             MakeDisplayPath(filename),
2677                                             _("opened."));
2678         } else {
2679                 owner->getMiniBuffer()->Set(_("Could not open document"),
2680                                             MakeDisplayPath(filename));
2681         }
2682 }
2683
2684
2685 void LyXFunc::doImportASCII(bool linorpar)
2686 {
2687         string initpath = lyxrc->document_path;
2688         LyXFileDlg fileDlg;
2689   
2690         if (owner->view()->available()) {
2691                 string trypath = owner->buffer()->filepath;
2692                 // If directory is writeable, use this as default.
2693                 if (IsDirWriteable(trypath) == 1)
2694                         initpath = trypath;
2695         }
2696
2697         // launches dialog
2698         ProhibitInput();
2699         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2700         fileDlg.SetButton(1, _("Examples"), 
2701                           AddPath(system_lyxdir, "examples"));
2702         string filename = fileDlg.Select(_("Select ASCII file to Import"),
2703                                          initpath, "*.txt");
2704         AllowInput();
2705  
2706         // check selected filename
2707         if (filename.empty()) {
2708                 owner->getMiniBuffer()->Set(_("Canceled."));
2709                 return;
2710         }
2711
2712         // get absolute path of file
2713         filename = MakeAbsPath(filename);
2714
2715         string s = ChangeExtension(filename, ".lyx", false);
2716
2717         // Check if the document already is open
2718         if (bufferlist.exists(s)) {
2719                 switch(AskConfirmation(_("Document is already open:"), 
2720                                        MakeDisplayPath(s, 50),
2721                                        _("Do you want to close that document now?\n"
2722                                          "('No' will just switch to the open version)")))
2723                         {
2724                         case 1: // Yes: close the document
2725                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
2726                                 // If close is canceled, we cancel here too.
2727                                         return;
2728                                 break;
2729                         case 2: // No: switch to the open document
2730                                 owner->view()->buffer(bufferlist.getBuffer(s));
2731                                 return;
2732                         case 3: // Cancel: Do nothing
2733                                 owner->getMiniBuffer()->Set(_("Canceled."));
2734                                 return;
2735                         }
2736         }
2737
2738         // Check if a LyX document by the same root exists in filesystem
2739         FileInfo f(s, true);
2740         if (f.exist() && !AskQuestion(_("A document by the name"), 
2741                                       MakeDisplayPath(s),
2742                                       _("already exists. Overwrite?"))) {
2743                 owner->getMiniBuffer()->Set(_("Canceled."));
2744                 return;
2745         }
2746
2747         owner->view()->buffer(bufferlist.newFile(s, string()));
2748         owner->getMiniBuffer()->Set(_("Importing ASCII file"),
2749                                     MakeDisplayPath(filename), "...");
2750         // Insert ASCII file
2751         InsertAsciiFile(filename, linorpar);
2752         owner->getMiniBuffer()->Set(_("ASCII file "),
2753                                     MakeDisplayPath(filename),
2754                                     _("imported."));
2755 }
2756
2757
2758 void LyXFunc::doImportLaTeX(bool isnoweb)
2759 {
2760         string initpath = lyxrc->document_path;
2761         LyXFileDlg fileDlg;
2762   
2763         if (owner->view()->available()) {
2764                 string trypath = owner->buffer()->filepath;
2765                 // If directory is writeable, use this as default.
2766                 if (IsDirWriteable(trypath) == 1)
2767                         initpath = trypath;
2768         }
2769
2770         // launches dialog
2771         ProhibitInput();
2772         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2773         fileDlg.SetButton(1, _("Examples"), 
2774                           AddPath(system_lyxdir, "examples"));
2775         string filename;
2776         if (isnoweb) {
2777                 filename = fileDlg.Select(_("Select Noweb file to Import"),
2778                                           initpath, "*.nw");
2779         } else {
2780                 filename = fileDlg.Select(_("Select LaTeX file to Import"),
2781                                           initpath, "*.tex");
2782         }
2783         
2784         AllowInput();
2785  
2786         // check selected filename
2787         if (filename.empty()) {
2788                 owner->getMiniBuffer()->Set(_("Canceled."));
2789                 return;
2790         }
2791
2792         // get absolute path of file
2793         filename = MakeAbsPath(filename);
2794
2795         // Check if the document already is open
2796         string LyXfilename = ChangeExtension(filename, ".lyx", false);
2797         if (bufferlist.exists(LyXfilename)){
2798                 switch(AskConfirmation(_("Document is already open:"), 
2799                                        MakeDisplayPath(LyXfilename, 50),
2800                                        _("Do you want to close that document now?\n"
2801                                          "('No' will just switch to the open version)")))
2802                         {
2803                         case 1: // Yes: close the document
2804                                 if (!bufferlist.close(bufferlist.getBuffer(LyXfilename)))
2805                                 // If close is canceled, we cancel here too.
2806                                         return;
2807                                 break;
2808                         case 2: // No: switch to the open document
2809                                 owner->view()->buffer(
2810                                         bufferlist.getBuffer(LyXfilename));
2811                                 return;
2812                         case 3: // Cancel: Do nothing
2813                                 owner->getMiniBuffer()->Set(_("Canceled."));
2814                                 return;
2815                         }
2816         }
2817
2818         // Check if a LyX document by the same root exists in filesystem
2819         FileInfo f(LyXfilename, true);
2820         if (f.exist() && !AskQuestion(_("A document by the name"), 
2821                                       MakeDisplayPath(LyXfilename),
2822                                       _("already exists. Overwrite?"))) {
2823                 owner->getMiniBuffer()->Set(_("Canceled."));
2824                 return;
2825         }
2826
2827         // loads document
2828         Buffer * openbuf;
2829         if (!isnoweb) {
2830                 owner->getMiniBuffer()->Set(_("Importing LaTeX file"),
2831                                             MakeDisplayPath(filename), "...");
2832                 ImportLaTeX myImport(filename);
2833                 openbuf = myImport.run();
2834         } else {
2835                 owner->getMiniBuffer()->Set(_("Importing Noweb file"),
2836                                             MakeDisplayPath(filename), "...");
2837                 ImportNoweb myImport(filename);
2838                 openbuf = myImport.run();
2839         }
2840         if (openbuf) {
2841                 owner->view()->buffer(openbuf);
2842                 owner->getMiniBuffer()->Set(isnoweb ?
2843                                             _("Noweb file ") : _("LateX file "),
2844                                             MakeDisplayPath(filename),
2845                                             _("imported."));
2846         } else {
2847                 owner->getMiniBuffer()->Set(isnoweb ?
2848                                             _("Could not import Noweb file") :
2849                                             _("Could not import LaTeX file"),
2850                                             MakeDisplayPath(filename));
2851         }
2852 }
2853
2854
2855 void LyXFunc::MenuInsertLyXFile(string const & filen)
2856 {
2857         string filename = filen;
2858
2859         if (filename.empty()) {
2860                 // Launch a file browser
2861                 string initpath = lyxrc->document_path;
2862                 LyXFileDlg fileDlg;
2863
2864                 if (owner->view()->available()) {
2865                         string trypath = owner->buffer()->filepath;
2866                         // If directory is writeable, use this as default.
2867                         if (IsDirWriteable(trypath) == 1)
2868                                 initpath = trypath;
2869                 }
2870
2871                 // launches dialog
2872                 ProhibitInput();
2873                 fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2874                 fileDlg.SetButton(1, _("Examples"), 
2875                                   AddPath(system_lyxdir, "examples"));
2876                 filename = fileDlg.Select(_("Select Document to Insert"),
2877                                           initpath, "*.lyx");
2878                 AllowInput();
2879
2880                 // check selected filename
2881                 if (filename.empty()) {
2882                         owner->getMiniBuffer()->Set(_("Canceled."));
2883                         return;
2884                 }
2885         } 
2886
2887         // get absolute path of file and make sure the filename ends
2888         // with .lyx
2889         filename = MakeAbsPath(filename);
2890         if (!IsLyXFilename(filename))
2891                 filename += ".lyx";
2892
2893         // Inserts document
2894         owner->getMiniBuffer()->Set(_("Inserting document"),
2895                                     MakeDisplayPath(filename), "...");
2896         bool res = owner->view()->insertLyXFile(filename);
2897         if (res) {
2898                 owner->getMiniBuffer()->Set(_("Document"),
2899                                             MakeDisplayPath(filename),
2900                                             _("inserted."));
2901         } else {
2902                 owner->getMiniBuffer()->Set(_("Could not insert document"),
2903                                             MakeDisplayPath(filename));
2904         }
2905 }
2906
2907
2908 void LyXFunc::reloadBuffer()
2909 {
2910         string fn = owner->buffer()->fileName();
2911         if (bufferlist.close(owner->buffer()))
2912                 owner->view()->buffer(bufferlist.loadLyXFile(fn));
2913 }
2914
2915
2916 void LyXFunc::CloseBuffer()
2917 {
2918         if (bufferlist.close(owner->buffer()) && !quitting) {
2919                 if (bufferlist.empty()) {
2920                         // need this otherwise SEGV may occur while trying to
2921                         // set variables that don't exist
2922                         // since there's no current buffer
2923                         CloseAllBufferRelatedPopups();
2924                 }
2925                 else {
2926                         owner->view()->buffer(bufferlist.first());
2927                 }
2928         }
2929 }
2930
2931
2932 Inset * LyXFunc::getInsetByCode(Inset::Code code)
2933 {
2934         bool found = false;
2935         Inset * inset = 0;
2936         LyXCursor cursor = owner->view()->text->cursor;
2937         LyXParagraph::size_type pos = cursor.pos;
2938         LyXParagraph * par = cursor.par;
2939         
2940         while (par && !found) {
2941                 while ((inset = par->ReturnNextInsetPointer(pos))){
2942                         if (inset->LyxCode() == code) {
2943                                 found = true;
2944                                 break;
2945                         }
2946                         ++pos;
2947                 } 
2948                 par = par->next;
2949         }
2950         return found ? inset : 0;
2951 }
2952
2953
2954 // Each "owner" should have it's own message method. lyxview and
2955 // the minibuffer would use the minibuffer, but lyxserver would
2956 // send an ERROR signal to its client.  Alejandro 970603
2957 // This func is bit problematic when it comes to NLS, to make the
2958 // lyx servers client be language indepenent we must not translate
2959 // strings sent to this func.
2960 void LyXFunc::setErrorMessage(string const & m) const
2961 {
2962         dispatch_buffer = m;
2963         errorstat = true;
2964 }
2965
2966
2967 void LyXFunc::setMessage(string const & m)
2968 {
2969         dispatch_buffer = m;
2970 }