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