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