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