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