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