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