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