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