]> git.lyx.org Git - features.git/blob - src/lyxfunc.C
applied the hebrew patch
[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-1999 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 #if 0
34 #include "insets/insetlatex.h"
35 #endif
36 #include "insets/inseturl.h"
37 #include "insets/insetlatexaccent.h"
38 #include "insets/insettoc.h"
39 #include "insets/insetlof.h"
40 #include "insets/insetloa.h"
41 #include "insets/insetlot.h"
42 #include "insets/insetref.h"
43 #include "insets/insetparent.h"
44 #include "insets/insetindex.h"
45 #include "insets/insetinclude.h"
46 #include "insets/insetbib.h"
47 #include "mathed/formulamacro.h"
48 #include "toolbar.h"
49 #include "spellchecker.h" // RVDK_PATCH_5
50 #include "minibuffer.h"
51 #include "vspace.h"
52 #include "LyXView.h"
53 #include "filedlg.h"
54 #include "lyx_gui_misc.h"
55 #include "support/filetools.h"
56 #include "support/FileInfo.h"
57 #include "support/syscall.h"
58 #include "support/lstrings.h"
59 #include "support/path.h"
60 #include "lyxscreen.h"
61 #include "debug.h"
62 #include "lyxrc.h"
63 #include "lyxtext.h"
64 #include "gettext.h"
65 #include "trans_mgr.h"
66 #include "ImportLaTeX.h"
67 #include "ImportNoweb.h"
68 #include "layout.h"
69
70 extern bool cursor_follows_scrollbar;
71
72 extern void InsertAsciiFile(string const &, bool);
73 extern void math_insert_symbol(char const *);
74 extern Bool math_insert_greek(char const); // why "Bool"?
75 extern BufferList bufferlist;
76 extern LyXServer * lyxserver;
77 extern short greek_kb_flag;
78 extern FD_form_toc * fd_form_toc;
79 extern bool selection_possible;
80
81 extern kb_keymap * toplevel_keymap;
82
83 extern void MenuWrite(Buffer *);
84 extern void MenuWriteAs(Buffer *);
85 extern int  MenuRunLaTeX(Buffer *);
86 extern int  MenuBuildProg(Buffer *);
87 extern int  MenuRunChktex(Buffer *);
88 extern bool CreatePostscript(Buffer *, bool);
89 extern void MenuPrint(Buffer *);
90 extern void MenuSendto();
91 extern void QuitLyX();
92 extern void MenuFax(Buffer *);
93 extern void MenuExport(Buffer *, string const &);
94 extern void MenuPasteSelection(char at);
95 extern LyXAction lyxaction;
96 // (alkis)
97 extern tex_accent_struct get_accent(kb_action action);
98
99 extern void AutoSave();
100 extern void MenuSearch();
101 extern void SetUpdateTimer(float timer = 0.3);
102 extern void FreeUpdateTimer();
103 extern bool PreviewDVI(Buffer *);
104 extern bool PreviewPostscript(Buffer *);
105 extern void MenuInsertLabel(char const *);
106 extern void MenuInsertRef();
107 extern void MenuLayoutCharacter();
108 extern void MenuLayoutParagraph();
109 extern void MenuLayoutDocument();
110 extern void MenuLayoutPaper();
111 extern void MenuLayoutTable(int flag);
112 extern void MenuLayoutQuotes();
113 extern void MenuLayoutPreamble();
114 extern void MenuLayoutSave();
115 extern void bulletForm();
116
117 extern Buffer * NewLyxFile(string const &);
118 extern void LoadLyXFile(string const &);
119 extern void Reconfigure();
120
121 extern int current_layout;
122 extern int getISOCodeFromLaTeX(char *);
123
124 //extern int UnlockInset(UpdatableInset *);
125
126 extern void ShowLatexLog();
127
128 extern void UpdateInset(Inset * inset, bool mark_dirty = true);
129
130 /* === globals =========================================================== */
131
132 bool LyXFunc::show_sc = true;
133
134
135 LyXFunc::LyXFunc(LyXView * o)
136         : owner(o)
137 {
138         meta_fake_bit = 0;
139         lyx_dead_action = LFUN_NOACTION;
140         lyx_calling_dead_action = LFUN_NOACTION;
141         setupLocalKeymap();
142 }
143
144
145 // I changed this func slightly. I commented out the ...FinishUndo(),
146 // this means that all places that used to have a moveCursorUpdate, now
147 // have a ...FinishUndo() as the preceeding statement. I have also added
148 // a moveCursorUpdate to some of the functions that updated the cursor, but
149 // that did not show its new position.
150 inline
151 void LyXFunc::moveCursorUpdate(bool selecting)
152 {
153         if (selecting || owner->view()->text->mark_set) {
154                 owner->view()->text->SetSelection();
155                 owner->view()->getScreen()->ToggleToggle();
156                 owner->view()->update(0);
157         } else {
158                 owner->view()->update(-2); // this IS necessary
159                 // (Matthias) 
160         }
161
162         owner->view()->getScreen()->ShowCursor();
163         
164         /* ---> Everytime the cursor is moved, show the current font state. */
165         // should this too me moved out of this func?
166         //owner->getMiniBuffer()->Set(CurrentState());
167         owner->view()->SetState();
168 }
169
170
171 int LyXFunc::processKeyEvent(XEvent * ev)
172 {
173         char s_r[10];
174         string argument;
175         XKeyEvent * keyevent = &ev->xkey;
176         KeySym keysym_return;
177
178         int num_bytes = LyXLookupString(ev, s_r, 10, &keysym_return);
179         s_r[num_bytes] = '\0';
180
181         if (lyxerr.debugging(Debug::KEY)) {
182                 char * tmp = XKeysymToString(keysym_return);
183                 string stm = (tmp ? tmp : "");
184                 lyxerr << "KeySym is "
185                        << stm
186                        << "["
187                        << keysym_return << "]"
188                        << " and num_bytes is "
189                        << num_bytes
190                        << " the string returned is \""
191                        << s_r << '\"' << endl;
192         }
193         // Do nothing if we have nothing (JMarc)
194         if (num_bytes == 0 && keysym_return == NoSymbol) {
195                 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
196                                    << endl;
197                 return 0;
198         }
199         
200         // this function should be used always [asierra060396]
201         if (owner->view()->available() &&
202             owner->view()->the_locking_inset &&
203             keysym_return == XK_Escape) {
204                 owner->view()->unlockInset(owner->view()->the_locking_inset);
205                 owner->view()->text->CursorRight();
206                 moveCursorUpdate(false);
207                 owner->getMiniBuffer()->Set(CurrentState());
208                 return 0;
209         }
210
211         // Can we be sure that this will work for all X-Windows
212         // implementations? (Lgb)
213         // This code snippet makes lyx ignore some keys. Perhaps
214         // all of them should be explictly mentioned?
215         if((keysym_return >= XK_Shift_L && keysym_return <= XK_Hyper_R)
216            || keysym_return == XK_Mode_switch || keysym_return == 0x0)
217                 return 0;
218
219         // Do a one-deep top-level lookup for
220         // cancel and meta-fake keys. RVDK_PATCH_5
221         cancel_meta_seq.reset();
222
223         int action = cancel_meta_seq.addkey(keysym_return, keyevent->state
224                                             &(ShiftMask|ControlMask
225                                               |Mod1Mask)); 
226
227         // When not cancel or meta-fake, do the normal lookup. 
228         // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
229         // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
230         if ( (action != LFUN_CANCEL) && (action != LFUN_META_FAKE) ) {
231
232                 // remove Caps Lock and Mod2 as a modifiers
233                 action = keyseq.addkey(keysym_return,
234                                        (keyevent->state | meta_fake_bit)
235                                        &(ShiftMask|ControlMask
236                                          |Mod1Mask));      
237         }
238         // Dont remove this unless you know what you are doing.
239         meta_fake_bit = 0;
240                 
241         if (action == 0) action = LFUN_PREFIX;
242
243         if (lyxerr.debugging(Debug::KEY)) {
244                 string buf;
245                 keyseq.print(buf);
246                 lyxerr << "Key ["
247                        << action << "]["
248                        << buf << "]["
249                        << num_bytes << "]" << endl;
250         }
251
252         // already here we know if it any point in going further
253         // why not return already here if action == -1 and
254         // num_bytes == 0? (Lgb)
255
256         if(keyseq.length > 1 || keyseq.length < -1){
257                 string buf;
258                 keyseq.print(buf);
259                 owner->getMiniBuffer()->Set(buf);
260         }
261
262         if (action == -1) {
263                 if (keyseq.length < -1) { // unknown key sequence...
264                         string buf;
265                         LyXBell();
266                         keyseq.print(buf);
267                         owner->getMiniBuffer()->Set(_("Unknown sequence:"), buf);
268                         return 0;
269                 }
270         
271                 char isochar = keyseq.getiso();
272                 if (!(keyevent->state&ControlMask) &&
273                     !(keyevent->state&Mod1Mask) &&
274                     (isochar && keysym_return < 0xF000)) {
275                         argument += isochar;
276                 }
277                 if (argument.empty()) {
278                         lyxerr.debug() << "Empty argument!" << endl;
279                         // This can`t possibly be of any use
280                         // so we`ll skip the dispatch.
281                         return 0;
282                 }
283         } else
284                 if (action == LFUN_SELFINSERT) {
285                         argument = s_r[0];
286                 }
287     
288         bool tmp_sc = show_sc;
289         show_sc = false;
290         Dispatch(action, argument.c_str());
291         show_sc = tmp_sc;
292         
293         return 0;
294
295
296
297 LyXFunc::func_status LyXFunc::getStatus(int ac) const
298 {
299         kb_action action;
300         func_status flag = LyXFunc::OK;
301         string argument;
302         Buffer * buf = owner->buffer();
303         
304         if (lyxaction.isPseudoAction(ac)) 
305                 action = lyxaction.retrieveActionArg(ac, argument);
306         else 
307                 action = static_cast<kb_action>(ac);
308         
309         if (action == LFUN_UNKNOWN_ACTION) {
310                 setErrorMessage(N_("Unknown action"));
311                 return LyXFunc::Unknown;
312         } 
313         
314         // Check whether we need a buffer
315         if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
316                 // Yes we need a buffer, do we have one?
317                 if (buf) {
318                         // yes
319                         // Can we use a readonly buffer?
320                         if (buf->isReadonly() && 
321                             !lyxaction.funcHasFlag(action,
322                                                    LyXAction::ReadOnly)) {
323                                 // no
324                                 setErrorMessage(N_("Document is read-only"));
325                                 flag |= LyXFunc::Disabled;
326                         }
327                 } else {
328                         // no
329                         setErrorMessage(N_("Command not allowed with"
330                                            "out any document open"));
331                         flag |= LyXFunc::Disabled;
332                 }
333         }
334
335         if (flag & LyXFunc::Disabled)
336                 return flag;
337
338         // I would really like to avoid having this switch and rather try to
339         // encode this in the function itself.
340         static bool noLaTeX = lyxrc->latex_command == "none";
341         bool disable = false;
342         switch (action) {
343         case LFUN_PREVIEW:
344                 disable = noLaTeX || lyxrc->view_dvi_command == "none";
345                 break;  
346         case LFUN_PREVIEWPS: 
347                 disable = noLaTeX || lyxrc->view_ps_command == "none";
348                 break;
349         case LFUN_RUNLATEX:
350         case LFUN_RUNDVIPS:
351                 disable = noLaTeX;
352                 break;
353         case LFUN_MENUPRINT:
354                 disable = noLaTeX || lyxrc->print_command == "none";
355                 break;
356         case LFUN_FAX:
357                 disable = noLaTeX || lyxrc->fax_command == "none"; 
358                 break;
359         case LFUN_IMPORT:
360                 if (argument == "latex")
361                         disable = lyxrc->relyx_command == "none";
362                 break;
363         case LFUN_EXPORT:
364                 if (argument == "dvi" || argument == "postscript")
365                         disable = noLaTeX;
366                 if (argument == "html")
367                         disable = lyxrc->html_command == "none";
368                 break;
369         case LFUN_UNDO:
370                 disable = buf->undostack.empty();
371                 break;
372         case LFUN_REDO:
373                 disable = buf->redostack.empty();
374                 break;
375         case LFUN_SPELLCHECK:
376                 disable = lyxrc->isp_command == "none";
377                 break;
378         case LFUN_RUNCHKTEX:
379                 disable = lyxrc->chktex_command == "none";
380                 break;
381         case LFUN_LAYOUT_TABLE:
382                 disable = ! owner->view()->text->cursor.par->table;
383                 break;
384         default:
385                 break;
386         }
387         if (disable)
388                 flag |= LyXFunc::Disabled;
389
390         if (buf) {
391                 func_status box = LyXFunc::ToggleOff;
392                 LyXFont font = owner->view()->text->real_current_font;
393                 switch (action) {
394                 case LFUN_EMPH:
395                         if (font.emph() == LyXFont::ON)
396                                 box = LyXFunc::ToggleOn;
397                         break;
398                 case LFUN_NOUN:
399                         if (font.noun() == LyXFont::ON)
400                                 box = LyXFunc::ToggleOn;
401                         break;
402                 case LFUN_BOLD:
403                         if (font.series() == LyXFont::BOLD_SERIES)
404                                 box = LyXFunc::ToggleOn;
405                         break;
406                 case LFUN_TEX:
407                         if (font.latex() == LyXFont::ON)
408                                 box = LyXFunc::ToggleOn;
409                         break;
410                 case LFUN_RTL:
411                         if (font.direction() == LyXFont::RTL_DIR)
412                                 box = LyXFunc::ToggleOn;
413                         break;
414                 default:
415                         box = LyXFunc::OK;
416                         break;
417                 }
418                 flag |= box;
419         }
420
421         return flag;
422 }
423
424
425 string LyXFunc::Dispatch(string const & s) 
426 {
427         // Split command string into command and argument
428         string cmd, line = frontStrip(s);
429         string arg = strip(frontStrip(split(line, cmd, ' ')));
430
431         return Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
432 }
433
434
435 string LyXFunc::Dispatch(int ac,
436                          char const * do_not_use_this_arg)
437 {
438         string argument;
439         kb_action action;
440         
441         FL_OBJECT * ob = 0;  // This will disapear soon
442     
443         // we have not done anything wrong yet.
444         errorstat = false;
445         dispatch_buffer.clear();
446         
447         // if action is a pseudo-action, we need the real action
448         if (lyxaction.isPseudoAction(ac)) {
449                 string tmparg;
450                 action = static_cast<kb_action>
451                         (lyxaction.retrieveActionArg(ac, tmparg));
452                 if (!tmparg.empty())
453                         argument = tmparg;
454         } else {
455                 action = static_cast<kb_action>(ac);
456                 if (do_not_use_this_arg)
457                         argument = do_not_use_this_arg; // except here
458         }
459     
460         selection_possible = false;
461         
462         if (owner->view()->available() 
463             && owner->view()->getScreen())
464                 owner->view()->getScreen()->HideCursor();
465
466         // We cannot use this function here
467         if (getStatus(action) & Disabled)
468                 goto exit_with_message;
469
470         commandshortcut.clear();
471         
472         if (lyxrc->display_shortcuts && show_sc) {
473                 if (action != LFUN_SELFINSERT) {
474                         // Put name of command and list of shortcuts
475                         // for it in minibuffer
476                         string comname = lyxaction.getActionName(action);
477
478                         int pseudoaction = action;
479                         bool argsadded = false;
480
481                         if (!argument.empty()) {
482                                 // If we have the command with argument, 
483                                 // this is better
484                                 pseudoaction = 
485                                         lyxaction.searchActionArg(action,
486                                                                   argument.c_str());
487
488                                 if (pseudoaction == -1) {
489                                         pseudoaction = action;
490                                 } else {
491                                         comname += " " + argument;
492                                         argsadded = true;
493                                 }
494                         }
495
496                         string shortcuts = toplevel_keymap->findbinding(pseudoaction);
497
498                         if (!shortcuts.empty()) {
499                                 comname += ": " + shortcuts;
500                         } else if (!argsadded) {
501                                 comname += " " + argument;
502                         }
503
504                         if (!comname.empty()) {
505                                 comname = strip(comname);
506                                 commandshortcut = "(" + comname + ')';
507                                 owner->getMiniBuffer()->Set(commandshortcut);
508                                 // Here we could even add a small pause,
509                                 // to annoy the user and make him learn
510                                 // the shortcuts.
511                                 // No! That will just annoy, not teach
512                                 // anything. The user will read the messages
513                                 // if they are interested. (Asger)
514                         }
515                 }
516         }
517
518         // If in math mode pass the control to
519         // the math inset [asierra060396]
520         if (owner->view()->available() &&
521             owner->view()->the_locking_inset) {
522                 if (action > 1
523                     || (action == LFUN_UNKNOWN_ACTION
524                         && keyseq.length >= -1)) {
525                         if (action == LFUN_UNKNOWN_ACTION
526                             && argument.empty()) {
527                                 argument = keyseq.getiso();
528                         }
529                         // Undo/Redo pre 0.13 is a bit tricky for insets.
530                         if (action == LFUN_UNDO) {
531                                 int slx, sly;
532                                 UpdatableInset * inset = 
533                                         owner->view()->the_locking_inset;
534                                 inset->GetCursorPos(slx, sly);
535                                 owner->view()->unlockInset(inset);
536                                 owner->view()->menuUndo();
537                                 inset = static_cast<UpdatableInset*>(owner->view()->text->cursor.par->GetInset(owner->view()->text->cursor.pos));
538                                 if (inset) 
539                                         inset->Edit(slx, sly);
540                                 return string();
541                         } else 
542                                 if (action == LFUN_REDO) {
543                                         int slx, sly;
544                                         UpdatableInset * inset = owner->view()->the_locking_inset;
545                                         inset->GetCursorPos(slx, sly);
546                                         owner->view()->unlockInset(inset);
547                                         owner->view()->menuRedo();
548                                         inset = static_cast<UpdatableInset*>(owner->view()->text->cursor.par->GetInset(owner->view()->text->cursor.pos));
549                                         if (inset)
550                                                 inset->Edit(slx, sly);
551                                         return string();
552                                 } else
553                                         if (owner->view()->the_locking_inset->LocalDispatch(action, argument.c_str()))
554                                                 return string();
555                                         else {
556                                                 setMessage(N_("Text mode"));
557                                                 LyXDirection direction = owner->view()->text->GetParDirection(owner->view()->text->cursor.par);
558                                                 if ( action == -1 ||
559                                                      (action == LFUN_RIGHT
560                                                       && direction == LYX_DIR_LEFT_TO_RIGHT)) {
561                                                         owner->view()->text->CursorRight();
562                                                         moveCursorUpdate(false);
563                                                         owner->getMiniBuffer()->Set(CurrentState());
564                                                 }
565                                                 if ( action == LFUN_LEFT 
566                                                      && direction == LYX_DIR_RIGHT_TO_LEFT) {
567                                                         owner->view()->text->CursorRight();
568                                                         moveCursorUpdate(false);
569                                                         owner->getMiniBuffer()->Set(CurrentState());
570                                                 }
571                                                 if (action == LFUN_LEFT || action == LFUN_RIGHT)
572                                                         return string();
573                                         }
574                 }
575         }
576
577         switch(action) {
578                 // --- Misc -------------------------------------------
579         case LFUN_WORDFINDFORWARD  : 
580         case LFUN_WORDFINDBACKWARD : {
581                 static string last_search;
582                 string searched_string;
583             
584                 if (!argument.empty()) {
585                         last_search = argument;
586                         searched_string = argument;
587                 } else {
588                         searched_string = last_search;
589                 }
590
591                 LyXText * ltCur = owner->view()->text ;
592
593                 if (!searched_string.empty() &&
594                     ((action == LFUN_WORDFINDBACKWARD) ? 
595                      ltCur->SearchBackward(searched_string.c_str()) :
596                      ltCur->SearchForward(searched_string.c_str()))) {
597
598                         // ??? What is that ???
599                         owner->view()->update(-2);
600
601                         // ??? Needed ???
602                         // clear the selection (if there is any) 
603                         owner->view()->getScreen()->ToggleSelection();
604                         owner->view()->text->ClearSelection();
605
606                         // Move cursor so that successive C-s 's will not stand in place. 
607                         if( action == LFUN_WORDFINDFORWARD ) 
608                                 owner->view()->text->CursorRightOneWord();
609                         owner->view()->text->FinishUndo();
610                         moveCursorUpdate(false);
611
612                         // ??? Needed ???
613                         // set the new selection 
614                         // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
615                         owner->view()->getScreen()->ToggleSelection(false);
616                 } else 
617                         LyXBell();      
618          
619                 // REMOVED : if (owner->view()->getWorkArea()->focus)
620                 owner->view()->getScreen()->ShowCursor();
621         }
622         break;
623
624         case LFUN_PREFIX:
625         {
626                 if (owner->view()->available()
627                     && owner->view()->getScreen()) {
628                         owner->view()->update(-2);
629                 }
630                 string buf;
631                 keyseq.print(buf, true);
632                 owner->getMiniBuffer()->Set(buf, string(), string(), 1);
633         }
634         break;
635
636         // --- Misc -------------------------------------------
637         case LFUN_EXEC_COMMAND:
638                 owner->getMiniBuffer()->ExecCommand(); 
639                 break;
640                 
641         case LFUN_CANCEL:                   // RVDK_PATCH_5
642                 keyseq.reset();
643                 meta_fake_bit = 0;
644                 if(owner->view()->available())
645                         // cancel any selection
646                         Dispatch(LFUN_MARK_OFF, 0);
647                 setMessage(N_("Cancel"));
648                 break;
649
650         case LFUN_META_FAKE:                                 // RVDK_PATCH_5
651         {
652                 meta_fake_bit = Mod1Mask;
653                 string buf;
654                 keyseq.print(buf, true);
655                 string res = string("M-") + buf;
656                 setMessage(buf); // RVDK_PATCH_5
657         }
658         break;  
659
660         case LFUN_READ_ONLY_TOGGLE:
661                 if (owner->buffer()->lyxvc.inUse()) {
662                         owner->buffer()->lyxvc.toggleReadOnly();
663                 } else {
664                         owner->buffer()->setReadonly(
665                                 !owner->buffer()->isReadonly());
666                 }
667                 break;
668                 
669         case LFUN_CENTER: // this is center and redraw.
670                 owner->view()->beforeChange();
671                 if (owner->view()->text->cursor.y >
672                     owner->view()->getWorkArea()->h / 2)        {
673                         owner->view()->getScreen()->
674                                 Draw(owner->view()->text->cursor.y -
675                                      owner->view()->getWorkArea()->h / 2);
676                 } else { // <= 
677                         owner->view()->getScreen()->
678                                 Draw(0);
679                 }
680                 owner->view()->update(0);
681                 owner->view()->redraw();
682                 break;
683                 
684         case LFUN_APPENDIX:
685                 if (owner->view()->available()) {
686                         owner->view()->text->toggleAppendix();
687                         owner->view()->update(1);
688                 }
689                 break;
690
691                 // --- Menus -----------------------------------------------
692         case LFUN_MENUNEW:
693                 MenuNew(false);
694                 break;
695                 
696         case LFUN_MENUNEWTMPLT:
697                 MenuNew(true);
698                 break;
699                 
700         case LFUN_MENUOPEN:
701                 MenuOpen();
702                 break;
703                 
704         case LFUN_CLOSEBUFFER:
705                 CloseBuffer();
706                 break;
707                 
708         case LFUN_MENUWRITE:
709                 MenuWrite(owner->buffer());
710                 break;
711                 
712         case LFUN_MENUWRITEAS:
713                 MenuWriteAs(owner->buffer());
714                 break;
715                 
716         case LFUN_MENURELOAD:
717                 reloadBuffer();
718                 break;
719                 
720         case LFUN_PREVIEW:
721                 PreviewDVI(owner->buffer());
722                 break;
723                         
724         case LFUN_PREVIEWPS:
725                 PreviewPostscript(owner->buffer());
726                 break;
727                 
728         case LFUN_RUNLATEX:
729                 MenuRunLaTeX(owner->buffer());
730                 break;
731                 
732         case LFUN_BUILDPROG:
733                 MenuBuildProg(owner->buffer());
734                 break;
735                 
736         case LFUN_RUNCHKTEX:
737                 MenuRunChktex(owner->buffer());
738                 break;
739                 
740         case LFUN_RUNDVIPS:
741                 CreatePostscript(owner->buffer(), false);
742                 break;
743                 
744         case LFUN_MENUPRINT:
745                 MenuPrint(owner->buffer());
746                 break;
747                 
748         case LFUN_FAX:
749                 MenuFax(owner->buffer());
750                 break;
751                         
752         case LFUN_EXPORT:
753                 MenuExport(owner->buffer(), argument);
754                 break;
755
756         case LFUN_IMPORT:
757         {
758                 //needs argument as string
759                 string imtyp = argument;
760                 
761                 // latex
762                 if (imtyp == "latex") {
763                         doImportLaTeX(false);
764                 }
765                 // ascii
766                 else if (imtyp == "ascii") {
767                         doImportASCII(false);
768                 } else if (imtyp == "asciiparagraph") {
769                         doImportASCII(true);
770                         // noweb
771                 } else if (imtyp == "noweb") {
772                         doImportLaTeX(true);
773                 } else {
774                         setErrorMessage(string(N_("Unknown import type: "))
775                                         + imtyp);
776                 }
777                 break;
778         }
779                 
780         case LFUN_QUIT:
781                 QuitLyX();
782                 break;
783                 
784         case LFUN_TOCVIEW:
785                 TocUpdateCB(ob, 0);
786                 if (fd_form_toc->form_toc->visible) {
787                         fl_raise_form(fd_form_toc->form_toc);
788                 } else {
789                         static int ow = -1, oh;
790                         fl_show_form(fd_form_toc->form_toc,
791                                      FL_PLACE_MOUSE |
792                                      FL_FREE_SIZE, FL_FULLBORDER,
793                                      _("Table of Contents"));
794                         if (ow < 0) {
795                                 ow = fd_form_toc->form_toc->w;
796                                 oh = fd_form_toc->form_toc->h;
797                         }
798                         fl_set_form_minsize(fd_form_toc->form_toc, ow, oh);
799                 }
800                 break;
801                 
802         case LFUN_TOC_INSERT:
803         {
804                 Inset * new_inset = new InsetTOC(owner->buffer());
805                 owner->view()->insertInset(new_inset, "Standard", true);
806                 break;
807         }
808         
809         case LFUN_LOF_INSERT:
810         {
811                 Inset * new_inset = new InsetLOF(owner->buffer());
812                 owner->view()->insertInset(new_inset, "Standard", true);
813                 break;
814         }
815         
816         case LFUN_LOA_INSERT:
817         {
818                 Inset * new_inset = new InsetLOA(owner->buffer());
819                 owner->view()->insertInset(new_inset, "Standard", true);
820                 break;
821         }
822
823         case LFUN_LOT_INSERT:
824         {
825                 Inset * new_inset = new InsetLOT(owner->buffer());
826                 owner->view()->insertInset(new_inset, "Standard", true);
827                 break;
828         }
829                 
830         case LFUN_TABLE:
831                 TableCB(ob, 0);
832                 break;
833                 
834         case LFUN_FIGURE:
835                 FigureCB(ob, 0);
836                 break;
837                 
838         case LFUN_AUTOSAVE:
839                 AutoSave();
840                 break;
841                 
842         case LFUN_UNDO:
843                 owner->view()->menuUndo();
844                 break;
845                 
846         case LFUN_REDO:
847                 owner->view()->menuRedo();
848                 break;
849                 
850         case LFUN_MENUSEARCH:
851                 MenuSearch();
852                 break;
853                 
854         case LFUN_PASTE:
855                 owner->view()->paste();
856                 break;
857                 
858         case LFUN_PASTESELECTION:
859         {
860                 bool asPara = false;
861                 if (argument == "paragraph") asPara = true;
862                 MenuPasteSelection(asPara);
863                 break;
864         }
865
866         case LFUN_CUT:
867                 owner->view()->cut();
868                 break;
869                 
870         case LFUN_COPY:
871                 owner->view()->copy();
872                 break;
873                 
874         case LFUN_LAYOUT_COPY:
875                 owner->view()->copyEnvironment();
876                 break;
877                 
878         case LFUN_LAYOUT_PASTE:
879                 owner->view()->pasteEnvironment();
880                 break;
881                 
882         case LFUN_GOTOERROR:
883                 owner->view()->gotoError();
884                 break;
885                 
886         case LFUN_REMOVEERRORS:
887                 if (owner->view()->removeAutoInsets()) {
888                         owner->view()->redraw();
889                         owner->view()->fitCursor();
890                         owner->view()->updateScrollbar();
891                 }
892                 break;
893                 
894         case LFUN_GOTONOTE:
895                 owner->view()->gotoNote();
896                 break;
897                 
898         case LFUN_OPENSTUFF:
899                 owner->view()->openStuff();
900                 break;
901                 
902         case LFUN_HYPHENATION:
903                 owner->view()->hyphenationPoint();
904                 break;
905                 
906         case LFUN_LDOTS:
907                 owner->view()->ldots();
908                 break;
909                 
910         case LFUN_END_OF_SENTENCE:
911                 owner->view()->endOfSentenceDot();
912                 break;
913
914         case LFUN_MENU_SEPARATOR:
915                 owner->view()->menuSeparator();
916                 break;
917                 
918         case LFUN_HFILL:
919                 owner->view()->hfill();
920                 break;
921                 
922         case LFUN_DEPTH:
923                 DepthCB(ob, 0);
924                 break;
925                 
926         case LFUN_DEPTH_MIN:
927                 DepthCB(ob, -1);
928                 break;
929                 
930         case LFUN_DEPTH_PLUS:
931                 DepthCB(ob, 1);
932                 break;
933                 
934         case LFUN_FREE:
935                 FreeCB();
936                 break;
937                 
938         case LFUN_TEX:
939                 TexCB();
940                 owner->view()->SetState();
941                 owner->getMiniBuffer()->Set(CurrentState());
942                 break;
943                 
944         case LFUN_MELT:
945                 MeltCB(ob, 0);
946                 break;
947                 
948         case LFUN_RECONFIGURE:
949                 Reconfigure();
950                 break;
951
952         case LFUN_FOOTMELT:
953                 if (owner->view()->available()
954                     && !owner->view()->text->selection
955                     && owner->view()->text->cursor.par->footnoteflag
956                     != LyXParagraph::NO_FOOTNOTE)
957                         { // only melt footnotes with FOOTMELT, not margins etc
958                                 if(owner->view()->text->cursor.par->footnotekind == LyXParagraph::FOOTNOTE)
959                                         MeltCB(ob, 0);
960                         }
961                 else
962                         FootCB(ob, 0);
963                 owner->view()->SetState();
964                 break;
965
966         case LFUN_MARGINMELT:
967                 if (owner->view()->available()
968                     && !owner->view()->text->selection
969                     && owner->view()->text->cursor.par->footnoteflag
970                     != LyXParagraph::NO_FOOTNOTE) {
971                         // only melt margins
972                         if(owner->view()->text->cursor.par->footnotekind == LyXParagraph::MARGIN)
973                                 MeltCB(ob, 0);
974                 }
975                 else
976                         MarginCB(ob, 0); 
977                 owner->view()->SetState();
978                 break;
979                 
980                 // --- version control -------------------------------
981         case LFUN_VC_REGISTER:
982         {
983                 if (!owner->buffer()->lyxvc.inUse())
984                         owner->buffer()->lyxvc.registrer();
985         }
986         break;
987                 
988         case LFUN_VC_CHECKIN:
989         {
990                 if (owner->buffer()->lyxvc.inUse()
991                     && !owner->buffer()->isReadonly())
992                         owner->buffer()->lyxvc.checkIn();
993         }
994         break;
995                 
996         case LFUN_VC_CHECKOUT:
997         {
998                 if (owner->buffer()->lyxvc.inUse()
999                     && owner->buffer()->isReadonly())
1000                         owner->buffer()->lyxvc.checkOut();
1001         }
1002         break;
1003         
1004         case LFUN_VC_REVERT:
1005         {
1006                 owner->buffer()->lyxvc.revert();
1007         }
1008         break;
1009                 
1010         case LFUN_VC_UNDO:
1011         {
1012                 owner->buffer()->lyxvc.undoLast();
1013         }
1014         break;
1015                 
1016         case LFUN_VC_HISTORY:
1017         {
1018                 owner->buffer()->lyxvc.showLog();
1019                 break;
1020         }
1021         
1022         // --- buffers ----------------------------------------
1023         case LFUN_PREVBUFFER:
1024 #ifdef WITH_WARNINGS
1025 #warning fix this please
1026 #endif
1027                 // it is the LyXView or the BufferView that should
1028                 // remember the previous buffer, not bufferlist.
1029 //                      if (owner->view()->available()){          
1030 //                              owner->view()->beforeChange();
1031 //                              owner->buffer()->update(-2);
1032 //                      }
1033 //                      owner->view()->setBuffer(bufferlist.prev());
1034
1035 //                      owner->view()->
1036 //                              resizeCurrentBufferPseudoExpose();
1037                 break;
1038                         
1039         case LFUN_FILE_INSERT:
1040         {
1041                 MenuInsertLyXFile(argument);
1042         }
1043         break;
1044         
1045         case LFUN_FILE_INSERT_ASCII:
1046         {
1047                 bool asPara = (argument == "paragraph");
1048                 InsertAsciiFile(string(), asPara);
1049         }
1050         break;
1051         
1052         case LFUN_FILE_NEW:
1053         {
1054                 // servercmd: argument must be <file>:<template>
1055                 Buffer * tmpbuf = NewLyxFile(argument);
1056                 if (tmpbuf)
1057                         owner->view()->buffer(tmpbuf);
1058         }
1059         break;
1060                         
1061         case LFUN_FILE_OPEN:
1062                 owner->view()->buffer(bufferlist.loadLyXFile(argument));
1063                 break;
1064
1065         case LFUN_LATEX_LOG:
1066                 ShowLatexLog();
1067                 break;
1068                 
1069         case LFUN_LAYOUTNO:
1070         {
1071                 lyxerr.debug() << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1072                 int sel = strToInt(argument);
1073                 lyxerr.debug() << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1074                 
1075                 // Should this give a setMessage instead?
1076                 if (sel == 0) 
1077                         return string(); // illegal argument
1078
1079                 --sel; // sel 1..., but layout 0...
1080
1081                 // Pretend we got the name instead.
1082                 Dispatch(int(LFUN_LAYOUT), 
1083                          textclasslist.NameOfLayout(owner->view()->
1084                                                     text->parameters->
1085                                                     textclass,
1086                                                     sel).c_str());
1087                 return string();
1088         }
1089                 
1090         case LFUN_LAYOUT:
1091         {
1092                 lyxerr.debug() << "LFUN_LAYOUT: (arg) "
1093                                << argument << endl;
1094                 
1095                 // Derive layout number from given argument (string)
1096                 // and current buffer's textclass (number). */    
1097                 int layoutno = 
1098                         textclasslist.NumberOfLayout(owner->
1099                                                      view()->
1100                                                      text->parameters->
1101                                                      textclass,
1102                                                      argument).second;
1103
1104                 // see if we found the layout number:
1105                 if (layoutno == -1) {
1106                         setErrorMessage(string(N_("Layout ")) + argument + 
1107                                         N_(" not known"));
1108                         break;
1109                 }
1110                         
1111                 if (current_layout != layoutno) {
1112                         owner->view()->getScreen()->HideCursor();
1113                         current_layout = layoutno;
1114                         owner->view()->update(-2);
1115                         owner->view()->text->
1116                                 SetLayout(layoutno);
1117                         owner->getToolbar()->combox->
1118                                 select(owner->view()->
1119                                        text->cursor.par->
1120                                        GetLayout() + 1);
1121                         owner->view()->update(1);
1122                 }
1123         }
1124         break;
1125
1126         case LFUN_LAYOUT_DOCUMENT:
1127                 MenuLayoutDocument();
1128                 break;
1129                 
1130         case LFUN_LAYOUT_PARAGRAPH:
1131                 MenuLayoutParagraph();
1132                 break;
1133                 
1134         case LFUN_LAYOUT_CHARACTER:
1135                 MenuLayoutCharacter();
1136                 break;
1137                 
1138         case LFUN_LAYOUT_TABLE:
1139         {
1140                 int flag = 0;
1141                 if (argument == "true") flag = 1;
1142                 MenuLayoutTable(flag);
1143         }
1144         break;
1145                 
1146         case LFUN_LAYOUT_PAPER:
1147                 MenuLayoutPaper();
1148                 break;
1149                 
1150         case LFUN_LAYOUT_QUOTES:
1151                 MenuLayoutQuotes();
1152                 break;
1153                 
1154         case LFUN_LAYOUT_PREAMBLE:
1155                 MenuLayoutPreamble();
1156                 break;
1157                 
1158         case LFUN_LAYOUT_SAVE_DEFAULT:
1159                 MenuLayoutSave();
1160                 break;
1161                 
1162         case LFUN_DROP_LAYOUTS_CHOICE:
1163                 owner->getToolbar()->combox->Show();
1164                 break;
1165
1166         case LFUN_RTL:
1167         {
1168                 RTLCB();
1169                 owner->view()->SetState();
1170                 owner->getMiniBuffer()->Set(CurrentState());
1171         }
1172                 break;
1173
1174         case LFUN_EMPH:
1175                 EmphCB();
1176                 break;
1177                 
1178         case LFUN_BOLD:
1179                 BoldCB();
1180                 break;
1181                 
1182         case LFUN_NOUN:
1183                 NounCB();
1184                 break;
1185                 
1186         case LFUN_CODE:
1187                 CodeCB();
1188                 break;
1189                 
1190         case LFUN_SANS:
1191                 SansCB();
1192                 break;
1193                 
1194         case LFUN_ROMAN:
1195                 RomanCB();
1196                 break;
1197                 
1198         case LFUN_DEFAULT:
1199                 StyleResetCB();
1200                 break;
1201                 
1202         case LFUN_UNDERLINE:
1203                 UnderlineCB();
1204                 break;
1205                 
1206         case LFUN_FONT_SIZE:
1207                 FontSizeCB(argument);
1208                 break;
1209                 
1210         case LFUN_FONT_STATE:
1211                 setMessage(CurrentState());
1212                 break;
1213                 
1214         case LFUN_UPCASE_WORD:
1215                 owner->view()->update(-2);
1216                 FreeUpdateTimer();
1217                 owner->view()->text->ChangeWordCase(LyXText::text_uppercase);
1218                 owner->view()->update(1);
1219                 SetUpdateTimer();
1220                 break;
1221                 
1222         case LFUN_LOWCASE_WORD:
1223                 owner->view()->update(-2);
1224                 FreeUpdateTimer();
1225                 owner->view()->text->ChangeWordCase(LyXText::text_lowercase);
1226                 owner->view()->update(1);
1227                 SetUpdateTimer();
1228                 break;
1229                 
1230         case LFUN_CAPITALIZE_WORD:
1231                 owner->view()->update(-2);
1232                 FreeUpdateTimer();
1233                 owner->view()->text->ChangeWordCase(LyXText::text_capitalization);
1234                 owner->view()->update(1);
1235                 SetUpdateTimer();
1236                 break;
1237                 
1238         case LFUN_INSERT_LABEL:
1239                 MenuInsertLabel(argument.c_str());
1240                 break;
1241                 
1242         case LFUN_INSERT_REF:
1243                 MenuInsertRef();
1244                 break;
1245                 
1246         case LFUN_REFTOGGLE:
1247         {
1248                 InsetRef * inset = 
1249                         static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1250                 if (inset) {
1251                         if (inset->getFlag() == InsetRef::REF)
1252                                 inset->setFlag(InsetRef::PAGE_REF);
1253                         else
1254                                 inset->setFlag(InsetRef::REF);
1255                         UpdateInset(inset);
1256                 } else {
1257                         setErrorMessage(N_("No cross-reference to toggle"));
1258                 }
1259         }
1260         break;
1261         
1262         case LFUN_REFBACK:
1263         {
1264                 owner->view()->restorePosition();
1265         }
1266         break;
1267
1268         case LFUN_REFGOTO:
1269         {
1270                 string label(argument);
1271                 if (label.empty()) {
1272                         InsetRef * inset = 
1273                                 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1274                         if (inset)
1275                                 label = inset->getContents();
1276                 }
1277                 
1278                 if (!label.empty()) {
1279                         owner->view()->savePosition();
1280                         owner->view()->gotoLabel(label.c_str());
1281                 }
1282         }
1283         break;
1284                 
1285         case LFUN_MENU_OPEN_BY_NAME:
1286                 owner->getMenus()->openByName(argument);
1287                 break; // RVDK_PATCH_5
1288                 
1289         case LFUN_SPELLCHECK:
1290                 if (lyxrc->isp_command != "none")
1291                         ShowSpellChecker();
1292                 break; // RVDK_PATCH_5
1293                 
1294                 // --- Cursor Movements -----------------------------
1295         case LFUN_RIGHT:
1296         {
1297                 LyXText * tmptext = owner->view()->text;
1298                 LyXDirection direction = tmptext->GetParDirection(tmptext->cursor.par);
1299                 if(!tmptext->mark_set)
1300                         owner->view()->beforeChange();
1301                 owner->view()->update(-2);
1302                 if (direction == LYX_DIR_RIGHT_TO_LEFT)
1303                         tmptext->CursorLeft();
1304                 if (tmptext->cursor.pos < tmptext->cursor.par->Last()
1305                     && tmptext->cursor.par->GetChar(tmptext->cursor.pos)
1306                     == LyXParagraph::META_INSET
1307                     && tmptext->cursor.par->GetInset(tmptext->cursor.pos)
1308                     && tmptext->cursor.par->GetInset(tmptext->cursor.pos)->Editable() == 2){
1309                         Inset * tmpinset = tmptext->cursor.par->GetInset(tmptext->cursor.pos);
1310                         setMessage(tmpinset->EditMessage());
1311                         tmpinset->Edit(0, 0);
1312                         break;
1313                 }
1314                 if (direction == LYX_DIR_LEFT_TO_RIGHT)
1315                         tmptext->CursorRight();
1316                 owner->view()->text->FinishUndo();
1317                 moveCursorUpdate(false);
1318                 owner->getMiniBuffer()->Set(CurrentState());
1319         }
1320         break;
1321                 
1322         case LFUN_LEFT:
1323         {
1324                 // This is soooo ugly. Isn`t it possible to make
1325                 // it simpler? (Lgb)
1326                 LyXText * txt = owner->view()->text;
1327                 LyXDirection direction = txt->GetParDirection(txt->cursor.par);
1328                 if(!txt->mark_set) owner->view()->beforeChange();
1329                 owner->view()->update(-2);
1330                 if (direction == LYX_DIR_LEFT_TO_RIGHT)
1331                         txt->CursorLeft();
1332                 if (txt->cursor.pos < txt->cursor.par->Last()
1333                     && txt->cursor.par->GetChar(txt->cursor.pos)
1334                     == LyXParagraph::META_INSET
1335                     && txt->cursor.par->GetInset(txt->cursor.pos)
1336                     && txt->cursor.par->GetInset(txt->cursor.pos)->Editable() == 2) {
1337                         Inset * tmpinset = txt->cursor.par->GetInset(txt->cursor.pos);
1338                         setMessage(tmpinset->EditMessage());
1339                         tmpinset->Edit(tmpinset->Width(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->GetParDirection(owner->view()->text->cursor.par) 
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->GetParDirection(owner->view()->text->cursor.par) 
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->GetParDirection(owner->view()->text->cursor.par)
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->GetParDirection(owner->view()->text->cursor.par)
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->GetParDirection(owner->view()->text->cursor.par)
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->GetParDirection(owner->view()->text->cursor.par) 
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()->getScreen()->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()->getScreen()->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(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()->beforeChange();
1994                 if (owner->view()->text->cursor.y >
1995                     owner->view()->getWorkArea()->h / 2)        {
1996                         owner->view()->getScreen()->
1997                                 Draw(owner->view()->text->cursor.y -
1998                                      owner->view()->getWorkArea()->h/2);
1999                 } else { // <= 
2000                         owner->view()->getScreen()->
2001                                 Draw(0);
2002                 }
2003                 owner->view()->update(0);
2004                 owner->view()->redraw();
2005         }
2006         break;
2007
2008         case LFUN_APROPOS:
2009         case LFUN_GETTIP:
2010         {
2011                 int qa = lyxaction.LookupFunc(argument.c_str());
2012                 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
2013         }
2014         break;
2015
2016         // --- accented characters ---------------------------
2017                 
2018         case LFUN_UMLAUT:
2019         case LFUN_CIRCUMFLEX:
2020         case LFUN_GRAVE:
2021         case LFUN_ACUTE:
2022         case LFUN_TILDE:
2023         case LFUN_CEDILLA:
2024         case LFUN_MACRON:
2025         case LFUN_DOT:
2026         case LFUN_UNDERDOT:
2027         case LFUN_UNDERBAR:
2028         case LFUN_CARON:
2029         case LFUN_SPECIAL_CARON:
2030         case LFUN_BREVE:
2031         case LFUN_TIE:
2032         case LFUN_HUNG_UMLAUT:
2033         case LFUN_CIRCLE:
2034         case LFUN_OGONEK:
2035         {
2036                 char c;
2037                 
2038                 if (keyseq.length == -1 && keyseq.getiso() != 0) 
2039                         c = keyseq.getiso();
2040                 else
2041                         c = 0;
2042                 
2043                 owner->getIntl()->getTrans()->
2044                         deadkey(c, get_accent(action).accent, 
2045                                 owner->view()->text);
2046                 
2047                 // Need to reset, in case the minibuffer calls these
2048                 // actions
2049                 keyseq.reset();
2050                 keyseq.length = 0;
2051                 
2052                 // copied verbatim from do_accent_char
2053                 owner->view()->smallUpdate(1);
2054                 SetUpdateTimer();
2055                 owner->view()->text->sel_cursor = 
2056                         owner->view()->text->cursor;
2057         }   
2058         break;
2059         
2060         // --- toolbar ----------------------------------
2061         case LFUN_PUSH_TOOLBAR:
2062         {
2063                 int nth = strToInt(argument);
2064                 if (lyxerr.debugging(Debug::TOOLBAR)) {
2065                         lyxerr << "LFUN_PUSH_TOOLBAR: argument = `"
2066                                << argument << "'\n"
2067                                << "LFUN_PUSH_TOOLBAR: nth = `"
2068                                << nth << "'" << endl;
2069                 }
2070                 
2071                 if (nth <= 0) {
2072                         LyXBell();
2073                         setErrorMessage(N_("Push-toolbar needs argument > 0"));
2074                 } else {
2075                         owner->getToolbar()->push(nth);
2076                 }
2077         }
2078         break;
2079         
2080         case LFUN_ADD_TO_TOOLBAR:
2081         {
2082                 if (lyxerr.debugging(Debug::TOOLBAR)) {
2083                         lyxerr << "LFUN_ADD_TO_TOOLBAR:"
2084                                 "argument = `" << argument << '\'' << endl;
2085                 }
2086                 string tmp(argument);
2087                 //lyxerr <<string("Argument: ") + argument);
2088                 //lyxerr <<string("Tmp     : ") + tmp);
2089                 if (tmp.empty()) {
2090                         LyXBell();
2091                         setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
2092                 } else {
2093                         owner->getToolbar()->add(argument, false);
2094                         owner->getToolbar()->set();
2095                 }
2096         }
2097         break;
2098         
2099         // --- insert characters ----------------------------------------
2100 #if 0
2101         case LFUN_INSERT_INSET_LATEX:
2102         {
2103                 Inset * new_inset = new InsetLatex(argument);
2104                 owner->buffer()->insertInset(new_inset);
2105         }
2106         break;
2107 #endif
2108         // ---  Mathed stuff. If we are here, there is no locked inset yet.
2109         
2110         // Greek mode     
2111         case LFUN_GREEK:
2112         {
2113                 if (!greek_kb_flag) {
2114                         greek_kb_flag = 1;
2115                         setMessage(N_("Math greek mode on"));
2116                 } else
2117                         greek_kb_flag = 0;
2118         }  
2119         break;
2120       
2121         // Greek keyboard      
2122         case LFUN_GREEK_TOGGLE:
2123         {
2124                 greek_kb_flag = greek_kb_flag ? 0 : 2;
2125                 if (greek_kb_flag) {
2126                         setMessage(N_("Math greek keyboard on"));
2127                 } else {
2128                         setMessage(N_("Math greek keyboard off"));
2129                 }
2130         }
2131         break;
2132         
2133         case LFUN_MATH_DELIM:     
2134         case LFUN_INSERT_MATRIX:
2135         {          
2136                 if (owner->view()->available()) { 
2137                         owner->view()->
2138                                 open_new_inset(new InsetFormula(false));
2139                         owner->view()->
2140                                 the_locking_inset->LocalDispatch(action, argument.c_str());
2141                 }
2142         }          
2143         break;
2144                
2145         case LFUN_INSERT_MATH:
2146         {
2147                 math_insert_symbol(argument.c_str());
2148         }
2149         break;
2150         
2151         case LFUN_MATH_DISPLAY:
2152         {
2153                 if (owner->view()->available())
2154                         owner->view()->open_new_inset(new InsetFormula(true));
2155                 break;
2156         }
2157                     
2158         case LFUN_MATH_MACRO:
2159         {
2160                 if (owner->view()->available()) {
2161                         string s(argument);
2162                         if (s.empty())
2163                                 setErrorMessage(N_("Missing argument"));
2164                         else {
2165                                 string s1 = token(s, ' ', 1);
2166                                 int na = s1.empty() ? 0: atoi(s1.c_str());
2167                                 owner->view()->
2168                                         open_new_inset(new InsetFormulaMacro(token(s, ' ', 0), na));
2169                         }
2170                 }
2171         }
2172         break;
2173
2174         case LFUN_MATH_MODE:   // Open or create a math inset
2175         {
2176                 
2177                 if (owner->view()->available())
2178                         owner->view()->open_new_inset(new InsetFormula);
2179                 setMessage(N_("Math editor mode"));
2180         }
2181         break;
2182           
2183         case LFUN_MATH_NUMBER:
2184         case LFUN_MATH_LIMITS:
2185         {
2186                 setErrorMessage(N_("This is only allowed in math mode!"));
2187         }
2188         break;
2189         
2190         case LFUN_INSERT_CITATION:
2191         {   
2192                 InsetCitation * new_inset = new InsetCitation();
2193                 // ale970405
2194                 // The note, if any, must be after the key, delimited
2195                 // by a | so both key and remark can have spaces.
2196                 if (!argument.empty()) {
2197                         string lsarg(argument);
2198                         if (contains(lsarg, "|")) {
2199                                 new_inset->setContents(token(lsarg, '|', 0));
2200                                 new_inset->setOptions(token(lsarg, '|', 1));
2201                         } else
2202                                 new_inset->setContents(lsarg);
2203                         owner->view()->insertInset(new_inset);
2204                 } else {
2205                         owner->view()->insertInset(new_inset);
2206                         new_inset->Edit(0, 0);
2207                 }
2208         }
2209         break;
2210                     
2211         case LFUN_INSERT_BIBTEX:
2212         {   
2213                 // ale970405+lasgoutt970425
2214                 // The argument can be up to two tokens separated 
2215                 // by a space. The first one is the bibstyle.
2216                 string lsarg(argument);
2217                 string bibstyle = token(lsarg, ' ', 1);
2218                 if (bibstyle.empty())
2219                         bibstyle = "plain";
2220                 InsetBibtex * new_inset 
2221                         = new InsetBibtex(token(lsarg, ' ', 0),
2222                                           bibstyle,
2223                                           owner->buffer());
2224                 
2225                 owner->view()->insertInset(new_inset);
2226                 if (lsarg.empty()) {
2227                         new_inset->Edit(0, 0);
2228                 }
2229         }
2230         break;
2231                 
2232         // BibTeX data bases
2233         case LFUN_BIBDB_ADD:
2234         {
2235                 InsetBibtex * inset = 
2236                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2237                 if (inset) {
2238                         inset->addDatabase(argument);
2239                 }
2240         }
2241         break;
2242                     
2243         case LFUN_BIBDB_DEL:
2244         {
2245                 InsetBibtex * inset = 
2246                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2247                 if (inset) {
2248                         inset->delDatabase(argument);
2249                 }
2250         }
2251         break;
2252         
2253         case LFUN_BIBTEX_STYLE:
2254         {
2255                 InsetBibtex * inset = 
2256                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2257                 if (inset) {
2258                         inset->setOptions(argument);
2259                 }
2260         }
2261         break;
2262                 
2263         case LFUN_INDEX_INSERT:
2264         case LFUN_INDEX_INSERT_LAST:
2265         {
2266                 // Can't do that at the beginning of a paragraph.
2267                 if (owner->view()->text->cursor.pos - 1 < 0)
2268                         break;
2269
2270                 InsetIndex * new_inset = new InsetIndex();
2271                 if (!argument.empty()) {
2272                         string lsarg(argument);
2273                         new_inset->setContents(lsarg);
2274                         owner->view()->insertInset(new_inset);
2275                 } else {
2276                         //reh 98/09/21
2277                         //get the current word for an argument
2278                         LyXParagraph::size_type lastpos = 
2279                                 owner->view()->text->cursor.pos - 1;
2280                         // Get the current word. note that this must be done
2281                         // before inserting the inset, or the inset will
2282                         // break the word
2283                         string curstring(owner->view()
2284                                          ->text->cursor.par->GetWord(lastpos));
2285
2286                         //make the new inset and write the current word into it
2287                         InsetIndex * new_inset = new InsetIndex();
2288
2289                         new_inset->setContents(curstring);
2290
2291                         //don't edit it if the call was to INSERT_LAST
2292                         if(action != LFUN_INDEX_INSERT_LAST) {
2293                                 new_inset->Edit(0, 0);
2294                         } else {
2295                                 //it looks blank on the screen unless
2296                                 //we do  something.  put it here.
2297
2298                                 // move the cursor to the returned value of lastpos
2299                                 // but only for the auto-insert
2300                                 owner->view()->text->cursor.pos = lastpos;
2301                         }
2302
2303                         //put the new inset into the buffer.
2304                         // there should be some way of knowing the user
2305                         //cancelled & avoiding this, but i don't know how
2306                         owner->view()->insertInset(new_inset);
2307                 }
2308         }
2309         break;
2310
2311         case LFUN_INDEX_PRINT:
2312         {
2313                 Inset * new_inset = new InsetPrintIndex(owner->buffer());
2314                 owner->view()->insertInset(new_inset, "Standard", true);
2315         }
2316         break;
2317
2318         case LFUN_PARENTINSERT:
2319         {
2320                 lyxerr << "arg " << argument << endl;
2321                 Inset * new_inset = new InsetParent(argument, owner->buffer());
2322                 owner->view()->insertInset(new_inset, "Standard", true);
2323         }
2324         break;
2325
2326         case LFUN_CHILDINSERT:
2327         {
2328                 Inset * new_inset = new InsetInclude(argument,
2329                                                      owner->buffer());
2330                 owner->view()->insertInset(new_inset, "Standard", true);
2331                 new_inset->Edit(0, 0);
2332         }
2333         break;
2334
2335         case LFUN_CHILDOPEN:
2336         {
2337                 string filename =
2338                         MakeAbsPath(argument, 
2339                                     OnlyPath(owner->buffer()->fileName()));
2340                 setMessage(N_("Opening child document ") +
2341                            MakeDisplayPath(filename) + "...");
2342                 owner->view()->savePosition();
2343                 if (bufferlist.exists(filename))
2344                         owner->view()->buffer(bufferlist.getBuffer(filename));
2345                 else
2346                         owner->view()->buffer(bufferlist.loadLyXFile(filename));
2347         }
2348         break;
2349
2350         case LFUN_INSERT_NOTE:
2351                 owner->view()->insertNote();
2352                 break;
2353                 
2354         case LFUN_INSERTFOOTNOTE: 
2355         {
2356                 LyXParagraph::footnote_kind kind;
2357                 if (argument == "footnote")
2358                         { kind = LyXParagraph::FOOTNOTE; }
2359                 else if (argument == "margin")
2360                         { kind = LyXParagraph::MARGIN; }
2361                 else if (argument == "figure")
2362                         { kind = LyXParagraph::FIG; }
2363                 else if (argument == "table")
2364                         { kind = LyXParagraph::TAB; }
2365                 else if (argument == "wide-fig")
2366                         { kind = LyXParagraph::WIDE_FIG; }
2367                 else if (argument == "wide-tab")
2368                         { kind = LyXParagraph::WIDE_TAB; }
2369                 else if (argument == "algorithm")
2370                         { kind = LyXParagraph::ALGORITHM; }
2371                 else {
2372                         setErrorMessage(N_("Unknown kind of footnote"));
2373                         break;
2374                 }
2375                 owner->view()->text->InsertFootnoteEnvironment(kind);
2376                 owner->view()->update(1);
2377                 owner->view()->SetState();
2378         }
2379         break;
2380         
2381         case LFUN_BUFFERBULLETSSELECT:
2382                 bulletForm();
2383                 break;
2384                 
2385         case LFUN_TOGGLECURSORFOLLOW:
2386                 cursor_follows_scrollbar = !cursor_follows_scrollbar;
2387                 break;
2388                 
2389         case LFUN_KMAP_OFF:             // keymap off
2390                 owner->getIntl()->KeyMapOn(false);
2391                 break;
2392                 
2393         case LFUN_KMAP_PRIM:    // primary keymap
2394                 owner->getIntl()->KeyMapPrim();
2395                 break;
2396                 
2397         case LFUN_KMAP_SEC:             // secondary keymap
2398                 owner->getIntl()->KeyMapSec();
2399                 break;
2400                 
2401         case LFUN_KMAP_TOGGLE:  // toggle keymap
2402                 owner->getIntl()->ToggleKeyMap();
2403                 break;
2404
2405         case LFUN_SELFINSERT:
2406         {
2407                 for (string::size_type i = 0; i < argument.length(); ++i) {
2408                         owner->view()->text->InsertChar(argument[i]);
2409                         // This needs to be in the loop, or else we
2410                         // won't break lines correctly. (Asger)
2411                         owner->view()->smallUpdate(1);
2412                 }
2413                 SetUpdateTimer();
2414                 owner->view()->text->sel_cursor = 
2415                         owner->view()->text->cursor;
2416                 moveCursorUpdate(false);
2417         }
2418         break;
2419
2420         case LFUN_SEQUENCE: 
2421         {
2422                 // argument contains ';'-terminated commands
2423                 while (argument.find(';') != string::npos) {
2424                         string first;
2425                         argument = split(argument, first, ';');
2426                         Dispatch(first);
2427                 }
2428         }
2429         break;
2430
2431         case LFUN_DATE_INSERT:  // jdblair: date-insert cmd
2432         {
2433                 char datetmp[32];
2434                 int datetmp_len;
2435                 time_t now_time_t;
2436                 struct tm *now_tm;
2437                 static string arg;
2438                 
2439                 now_time_t = time(NULL);
2440                 now_tm = localtime(&now_time_t);
2441                 (void)setlocale(LC_TIME, "");
2442                 if (!argument.empty())
2443                         arg = argument;
2444                 else if (arg.empty())
2445                         arg = lyxrc->date_insert_format;
2446                 datetmp_len = (int) strftime(datetmp, 32, arg.c_str(), now_tm);
2447                 for (int i = 0; i < datetmp_len; i++) {
2448                         owner->view()->text->InsertChar(datetmp[i]);
2449                         owner->view()->smallUpdate(1);
2450                 }
2451                 SetUpdateTimer();
2452                 owner->view()->text->sel_cursor = owner->view()->text->cursor;
2453                 moveCursorUpdate(false);
2454         }
2455         break;
2456
2457         case LFUN_SAVEPREFERENCES:
2458         {
2459                 Path p(user_lyxdir);
2460                 lyxrc->write("preferences");
2461         }
2462         break;
2463         
2464         case LFUN_UNKNOWN_ACTION:
2465         {
2466                 if(!owner->buffer()) {
2467                         LyXBell();
2468                         setErrorMessage(N_("No document open"));
2469                         break;
2470                 }
2471
2472                 if (owner->buffer()->isReadonly()) {
2473                         LyXBell();
2474                         setErrorMessage(N_("Document is read only"));
2475                         break;
2476                 }
2477                          
2478                 if (!argument.empty()) {
2479                         
2480                         /* Automatically delete the currently selected
2481                          * text and replace it with what is being
2482                          * typed in now. Depends on lyxrc settings
2483                          * "auto_region_delete", which defaults to
2484                          * true (on). */
2485                 
2486                         if ( lyxrc->auto_region_delete ) {
2487                                 if (owner->view()->text->selection){
2488                                         owner->view()->text->CutSelection(false);
2489                                         owner->view()->update(-1);
2490                                 }
2491                         }
2492                         
2493                         owner->view()->beforeChange();
2494                         for (string::size_type i = 0;
2495                              i < argument.length(); ++i) {
2496                                 if (greek_kb_flag) {
2497                                         if (!math_insert_greek(argument[i]))
2498                                                 owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2499                                 } else
2500                                         owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2501                         }
2502                         
2503                         owner->view()->smallUpdate(1);
2504                         SetUpdateTimer();
2505
2506                         owner->view()->text->sel_cursor = 
2507                                 owner->view()->text->cursor;
2508                         moveCursorUpdate(false);
2509                         return string();
2510                 } else {
2511                         // why is an "Unknown action" with empty
2512                         // argument even dispatched in the first
2513                         // place? I`ll probably change that. (Lgb)
2514                         LyXBell();
2515                         setErrorMessage(N_("Unknown action"));
2516                 }
2517                 break;
2518         default:
2519                 lyxerr << "A truly unknown func!" << endl;
2520                 break;
2521         }
2522         } // end of switch
2523   exit_with_message:
2524
2525         string res = getMessage();
2526
2527         if (res.empty()) {
2528                 if (!commandshortcut.empty()) {
2529                         string newbuf = owner->getMiniBuffer()->GetText();
2530                         if (newbuf != commandshortcut) {
2531                                 owner->getMiniBuffer()->Set(newbuf
2532                                                             + " " +
2533                                                             commandshortcut);
2534                         }
2535                 }
2536         } else {
2537                 owner->getMiniBuffer()->Set(string(_(res.c_str()))
2538                                             + " " + commandshortcut);
2539         }
2540
2541         return res;
2542 }
2543
2544
2545 void LyXFunc::setupLocalKeymap()
2546 {
2547         keyseq.stdmap = keyseq.curmap = toplevel_keymap;
2548         cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap;
2549 }
2550
2551
2552 void LyXFunc::MenuNew(bool fromTemplate)
2553 {
2554         string fname, initpath = lyxrc->document_path;
2555         LyXFileDlg fileDlg;
2556
2557         if (owner->view()->available()) {
2558                 string trypath = owner->buffer()->filepath;
2559                 // If directory is writeable, use this as default.
2560                 if (IsDirWriteable(trypath) == 1)
2561                         initpath = trypath;
2562         }
2563
2564         ProhibitInput();
2565         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2566         fileDlg.SetButton(1, _("Templates"), lyxrc->template_path);
2567         fname = fileDlg.Select(_("Enter Filename for new document"), 
2568                                initpath, "*.lyx", _("newfile"));
2569         AllowInput();
2570         
2571         if (fname.empty()) {
2572                 owner->getMiniBuffer()->Set(_("Canceled."));
2573                 lyxerr.debug() << "New Document Cancelled." << endl;
2574                 return;
2575         }
2576         
2577         // get absolute path of file and make sure the filename ends
2578         // with .lyx
2579         string s = MakeAbsPath(fname);
2580         if (!IsLyXFilename(s))
2581                 s += ".lyx";
2582
2583         // Check if the document already is open
2584         if (bufferlist.exists(s)){
2585                 switch(AskConfirmation(_("Document is already open:"), 
2586                                        MakeDisplayPath(s, 50),
2587                                        _("Do you want to close that document now?\n"
2588                                          "('No' will just switch to the open version)")))
2589                         {
2590                         case 1: // Yes: close the document
2591                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
2592                                 // If close is canceled, we cancel here too.
2593                                         return;
2594                                 break;
2595                         case 2: // No: switch to the open document
2596                                 owner->view()->buffer(bufferlist.getBuffer(s));
2597                                 return;
2598                         case 3: // Cancel: Do nothing
2599                                 owner->getMiniBuffer()->Set(_("Canceled."));
2600                                 return;
2601                         }
2602         }
2603         
2604         // Check whether the file already exists
2605         if (IsLyXFilename(s)) {
2606                 FileInfo fi(s);
2607                 if (fi.readable() &&
2608                     AskQuestion(_("File already exists:"), 
2609                                 MakeDisplayPath(s, 50),
2610                                 _("Do you want to open the document?"))) {
2611                         // loads document
2612                         owner->getMiniBuffer()->Set(_("Opening document"), 
2613                                                     MakeDisplayPath(s), "...");
2614                         XFlush(fl_display);
2615                         owner->view()->buffer(
2616                                 bufferlist.loadLyXFile(s));
2617                         owner->getMiniBuffer()->Set(_("Document"),
2618                                                     MakeDisplayPath(s),
2619                                                     _("opened."));
2620                         return;
2621                 }
2622         }
2623
2624         // The template stuff
2625         string templname;
2626         if (fromTemplate) {
2627                 ProhibitInput();
2628                 fname = fileDlg.Select(_("Choose template"),
2629                                        lyxrc->template_path,
2630                                        "*.lyx");
2631                 templname = fname;
2632                 AllowInput();
2633         }
2634   
2635         // find a free buffer
2636         lyxerr.debug() << "Find a free buffer." << endl;
2637         owner->view()->buffer(bufferlist.newFile(s, templname));
2638 }
2639
2640
2641 void LyXFunc::MenuOpen()
2642 {
2643         string initpath = lyxrc->document_path;
2644         LyXFileDlg fileDlg;
2645   
2646         if (owner->view()->available()) {
2647                 string trypath = owner->buffer()->filepath;
2648                 // If directory is writeable, use this as default.
2649                 if (IsDirWriteable(trypath) == 1)
2650                         initpath = trypath;
2651         }
2652
2653         // launches dialog
2654         ProhibitInput();
2655         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2656         fileDlg.SetButton(1, _("Examples"), 
2657                           AddPath(system_lyxdir, "examples"));
2658         string filename = fileDlg.Select(_("Select Document to Open"),
2659                                          initpath, "*.lyx");
2660         AllowInput();
2661  
2662         // check selected filename
2663         if (filename.empty()) {
2664                 owner->getMiniBuffer()->Set(_("Canceled."));
2665                 return;
2666         }
2667
2668         // get absolute path of file and make sure the filename ends
2669         // with .lyx
2670         filename = MakeAbsPath(filename);
2671         if (!IsLyXFilename(filename))
2672                 filename += ".lyx";
2673
2674         // loads document
2675         owner->getMiniBuffer()->Set(_("Opening document"),
2676                                     MakeDisplayPath(filename), "...");
2677         Buffer * openbuf = bufferlist.loadLyXFile(filename);
2678         if (openbuf) {
2679                 owner->view()->buffer(openbuf);
2680                 owner->getMiniBuffer()->Set(_("Document"),
2681                                             MakeDisplayPath(filename),
2682                                             _("opened."));
2683         } else {
2684                 owner->getMiniBuffer()->Set(_("Could not open document"),
2685                                             MakeDisplayPath(filename));
2686         }
2687 }
2688
2689
2690 void LyXFunc::doImportASCII(bool linorpar)
2691 {
2692         string initpath = lyxrc->document_path;
2693         LyXFileDlg fileDlg;
2694   
2695         if (owner->view()->available()) {
2696                 string trypath = owner->buffer()->filepath;
2697                 // If directory is writeable, use this as default.
2698                 if (IsDirWriteable(trypath) == 1)
2699                         initpath = trypath;
2700         }
2701
2702         // launches dialog
2703         ProhibitInput();
2704         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2705         fileDlg.SetButton(1, _("Examples"), 
2706                           AddPath(system_lyxdir, "examples"));
2707         string filename = fileDlg.Select(_("Select ASCII file to Import"),
2708                                          initpath, "*.txt");
2709         AllowInput();
2710  
2711         // check selected filename
2712         if (filename.empty()) {
2713                 owner->getMiniBuffer()->Set(_("Canceled."));
2714                 return;
2715         }
2716
2717         // get absolute path of file
2718         filename = MakeAbsPath(filename);
2719
2720         string s = ChangeExtension(filename, ".lyx", false);
2721
2722         // Check if the document already is open
2723         if (bufferlist.exists(s)) {
2724                 switch(AskConfirmation(_("Document is already open:"), 
2725                                        MakeDisplayPath(s, 50),
2726                                        _("Do you want to close that document now?\n"
2727                                          "('No' will just switch to the open version)")))
2728                         {
2729                         case 1: // Yes: close the document
2730                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
2731                                 // If close is canceled, we cancel here too.
2732                                         return;
2733                                 break;
2734                         case 2: // No: switch to the open document
2735                                 owner->view()->buffer(bufferlist.getBuffer(s));
2736                                 return;
2737                         case 3: // Cancel: Do nothing
2738                                 owner->getMiniBuffer()->Set(_("Canceled."));
2739                                 return;
2740                         }
2741         }
2742
2743         // Check if a LyX document by the same root exists in filesystem
2744         FileInfo f(s, true);
2745         if (f.exist() && !AskQuestion(_("A document by the name"), 
2746                                       MakeDisplayPath(s),
2747                                       _("already exists. Overwrite?"))) {
2748                 owner->getMiniBuffer()->Set(_("Canceled."));
2749                 return;
2750         }
2751
2752         owner->view()->buffer(bufferlist.newFile(s, string()));
2753         owner->getMiniBuffer()->Set(_("Importing ASCII file"),
2754                                     MakeDisplayPath(filename), "...");
2755         // Insert ASCII file
2756         InsertAsciiFile(filename, linorpar);
2757         owner->getMiniBuffer()->Set(_("ASCII file "),
2758                                     MakeDisplayPath(filename),
2759                                     _("imported."));
2760 }
2761
2762
2763 void LyXFunc::doImportLaTeX(bool isnoweb)
2764 {
2765         string initpath = lyxrc->document_path;
2766         LyXFileDlg fileDlg;
2767   
2768         if (owner->view()->available()) {
2769                 string trypath = owner->buffer()->filepath;
2770                 // If directory is writeable, use this as default.
2771                 if (IsDirWriteable(trypath) == 1)
2772                         initpath = trypath;
2773         }
2774
2775         // launches dialog
2776         ProhibitInput();
2777         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2778         fileDlg.SetButton(1, _("Examples"), 
2779                           AddPath(system_lyxdir, "examples"));
2780         string filename;
2781         if (isnoweb) {
2782                 filename = fileDlg.Select(_("Select Noweb file to Import"),
2783                                           initpath, "*.nw");
2784         } else {
2785                 filename = fileDlg.Select(_("Select LaTeX file to Import"),
2786                                           initpath, "*.tex");
2787         }
2788         
2789         AllowInput();
2790  
2791         // check selected filename
2792         if (filename.empty()) {
2793                 owner->getMiniBuffer()->Set(_("Canceled."));
2794                 return;
2795         }
2796
2797         // get absolute path of file
2798         filename = MakeAbsPath(filename);
2799
2800         // Check if the document already is open
2801         string LyXfilename = ChangeExtension(filename, ".lyx", false);
2802         if (bufferlist.exists(LyXfilename)){
2803                 switch(AskConfirmation(_("Document is already open:"), 
2804                                        MakeDisplayPath(LyXfilename, 50),
2805                                        _("Do you want to close that document now?\n"
2806                                          "('No' will just switch to the open version)")))
2807                         {
2808                         case 1: // Yes: close the document
2809                                 if (!bufferlist.close(bufferlist.getBuffer(LyXfilename)))
2810                                 // If close is canceled, we cancel here too.
2811                                         return;
2812                                 break;
2813                         case 2: // No: switch to the open document
2814                                 owner->view()->buffer(
2815                                         bufferlist.getBuffer(LyXfilename));
2816                                 return;
2817                         case 3: // Cancel: Do nothing
2818                                 owner->getMiniBuffer()->Set(_("Canceled."));
2819                                 return;
2820                         }
2821         }
2822
2823         // Check if a LyX document by the same root exists in filesystem
2824         FileInfo f(LyXfilename, true);
2825         if (f.exist() && !AskQuestion(_("A document by the name"), 
2826                                       MakeDisplayPath(LyXfilename),
2827                                       _("already exists. Overwrite?"))) {
2828                 owner->getMiniBuffer()->Set(_("Canceled."));
2829                 return;
2830         }
2831
2832         // loads document
2833         Buffer * openbuf;
2834         if (!isnoweb) {
2835                 owner->getMiniBuffer()->Set(_("Importing LaTeX file"),
2836                                             MakeDisplayPath(filename), "...");
2837                 ImportLaTeX myImport(filename);
2838                 openbuf = myImport.run();
2839         } else {
2840                 owner->getMiniBuffer()->Set(_("Importing Noweb file"),
2841                                             MakeDisplayPath(filename), "...");
2842                 ImportNoweb myImport(filename);
2843                 openbuf = myImport.run();
2844         }
2845         if (openbuf) {
2846                 owner->view()->buffer(openbuf);
2847                 owner->getMiniBuffer()->Set(isnoweb ?
2848                                             _("Noweb file ") : _("LateX file "),
2849                                             MakeDisplayPath(filename),
2850                                             _("imported."));
2851         } else {
2852                 owner->getMiniBuffer()->Set(isnoweb ?
2853                                             _("Could not import Noweb file") :
2854                                             _("Could not import LaTeX file"),
2855                                             MakeDisplayPath(filename));
2856         }
2857 }
2858
2859
2860 void LyXFunc::MenuInsertLyXFile(string const & filen)
2861 {
2862         string filename = filen;
2863
2864         if (filename.empty()) {
2865                 // Launch a file browser
2866                 string initpath = lyxrc->document_path;
2867                 LyXFileDlg fileDlg;
2868
2869                 if (owner->view()->available()) {
2870                         string trypath = owner->buffer()->filepath;
2871                         // If directory is writeable, use this as default.
2872                         if (IsDirWriteable(trypath) == 1)
2873                                 initpath = trypath;
2874                 }
2875
2876                 // launches dialog
2877                 ProhibitInput();
2878                 fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
2879                 fileDlg.SetButton(1, _("Examples"), 
2880                                   AddPath(system_lyxdir, "examples"));
2881                 filename = fileDlg.Select(_("Select Document to Insert"),
2882                                           initpath, "*.lyx");
2883                 AllowInput();
2884
2885                 // check selected filename
2886                 if (filename.empty()) {
2887                         owner->getMiniBuffer()->Set(_("Canceled."));
2888                         return;
2889                 }
2890         } 
2891
2892         // get absolute path of file and make sure the filename ends
2893         // with .lyx
2894         filename = MakeAbsPath(filename);
2895         if (!IsLyXFilename(filename))
2896                 filename += ".lyx";
2897
2898         // Inserts document
2899         owner->getMiniBuffer()->Set(_("Inserting document"),
2900                                     MakeDisplayPath(filename), "...");
2901         bool res = owner->view()->insertLyXFile(filename);
2902         if (res) {
2903                 owner->getMiniBuffer()->Set(_("Document"),
2904                                             MakeDisplayPath(filename),
2905                                             _("inserted."));
2906         } else {
2907                 owner->getMiniBuffer()->Set(_("Could not insert document"),
2908                                             MakeDisplayPath(filename));
2909         }
2910 }
2911
2912
2913 void LyXFunc::reloadBuffer()
2914 {
2915         string fn = owner->buffer()->fileName();
2916         if (bufferlist.close(owner->buffer()))
2917                 owner->view()->buffer(bufferlist.loadLyXFile(fn));
2918 }
2919
2920
2921 void LyXFunc::CloseBuffer()
2922 {
2923         if (bufferlist.close(owner->buffer()) && !quitting) {
2924                 if (bufferlist.empty()) {
2925                         // need this otherwise SEGV may occur while trying to
2926                         // set variables that don't exist
2927                         // since there's no current buffer
2928                         CloseAllBufferRelatedPopups();
2929                 }
2930                 else {
2931                         owner->view()->buffer(bufferlist.first());
2932                 }
2933         }
2934 }
2935
2936
2937 Inset * LyXFunc::getInsetByCode(Inset::Code code)
2938 {
2939         bool found = false;
2940         Inset * inset = 0;
2941         LyXCursor cursor = owner->view()->text->cursor;
2942         LyXParagraph::size_type pos = cursor.pos;
2943         LyXParagraph * par = cursor.par;
2944         
2945         while (par && !found) {
2946                 while ((inset = par->ReturnNextInsetPointer(pos))){
2947                         if (inset->LyxCode() == code) {
2948                                 found = true;
2949                                 break;
2950                         }
2951                         ++pos;
2952                 } 
2953                 par = par->next;
2954         }
2955         return found ? inset : 0;
2956 }
2957
2958
2959 // Each "owner" should have it's own message method. lyxview and
2960 // the minibuffer would use the minibuffer, but lyxserver would
2961 // send an ERROR signal to its client.  Alejandro 970603
2962 // This func is bit problematic when it comes to NLS, to make the
2963 // lyx servers client be language indepenent we must not translate
2964 // strings sent to this func.
2965 void LyXFunc::setErrorMessage(string const & m) const
2966 {
2967         dispatch_buffer = m;
2968         errorstat = true;
2969 }
2970
2971
2972 void LyXFunc::setMessage(string const & m)
2973 {
2974         dispatch_buffer = m;
2975 }