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