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