]> git.lyx.org Git - features.git/blob - src/lyxfunc.C
Remove the optional bool argument to ChangeExtension().
[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.erase();
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.erase();
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                 owner->view()->update(1);
1662                 SetUpdateTimer(0.01);
1663                 moveCursorUpdate(false);
1664                 break;
1665                 
1666         case LFUN_PROTECTEDSPACE:
1667         {
1668                 LyXLayout const & style =
1669                         textclasslist.Style(owner->view()->buffer()->params.textclass,
1670                                             owner->view()->text->cursor.par->GetLayout());
1671
1672                 if (style.free_spacing) {
1673                         owner->view()->text->InsertChar(' ');
1674                         owner->view()->update(-1);
1675                 } else {
1676                         owner->view()->protectedBlank();
1677                 }
1678                 moveCursorUpdate(false);
1679         }
1680         break;
1681                 
1682         case LFUN_SETMARK:
1683                 if(owner->view()->text->mark_set) {
1684                         owner->view()->beforeChange();
1685                         owner->view()->update(0);
1686                         setMessage(N_("Mark removed"));
1687                 } else {
1688                         owner->view()->beforeChange();
1689                         owner->view()->text->mark_set = 1;
1690                         owner->view()->update(0);
1691                         setMessage(N_("Mark set"));
1692                 }
1693                 owner->view()->text->sel_cursor = 
1694                         owner->view()->text->cursor;
1695                 break;
1696                 
1697         case LFUN_DELETE:
1698                 FreeUpdateTimer();
1699                 if (!owner->view()->text->selection) {
1700                         owner->view()->text->Delete();
1701                         owner->view()->text->sel_cursor = 
1702                                 owner->view()->text->cursor;
1703                         owner->view()->update(1);
1704                         // It is possible to make it a lot faster still
1705                         // just comment out the lone below...
1706                         owner->view()->showCursor();
1707                 } else {
1708                         owner->view()->cut();
1709                 }
1710                 SetUpdateTimer();
1711                 moveCursorUpdate(false);
1712                 owner->getMiniBuffer()->Set(CurrentState(owner->view()));
1713                 owner->view()->setState();
1714                 break;
1715
1716         case LFUN_DELETE_SKIP:
1717         {
1718                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
1719                 
1720                 LyXCursor cursor = owner->view()->text->cursor;
1721
1722                 FreeUpdateTimer();
1723                 if (!owner->view()->text->selection) {
1724                         if (cursor.pos == cursor.par->Last()) {
1725                                 owner->view()->text->CursorRight();
1726                                 cursor = owner->view()->text->cursor;
1727                                 if (cursor.pos == 0
1728                                     && !(cursor.par->added_space_top 
1729                                          == VSpace (VSpace::NONE))) {
1730                                         owner->view()->text->SetParagraph
1731                                                 (cursor.par->line_top,
1732                                                  cursor.par->line_bottom,
1733                                                  cursor.par->pagebreak_top, 
1734                                                  cursor.par->pagebreak_bottom,
1735                                                  VSpace(VSpace::NONE), 
1736                                                  cursor.par->added_space_bottom,
1737                                                  cursor.par->align, 
1738                                                  cursor.par->labelwidthstring, 0);
1739                                         owner->view()->text->CursorLeft();
1740                                         owner->view()->update (1);
1741                                 } else {
1742                                         owner->view()->text->CursorLeft();
1743                                         owner->view()->text->Delete();
1744                                         owner->view()->text->sel_cursor = 
1745                                                 owner->view()->text->cursor;
1746                                         owner->view()->update(1);
1747                                 }
1748                         } else {
1749                                 owner->view()->text->Delete();
1750                                 owner->view()->text->sel_cursor = 
1751                                         owner->view()->text->cursor;
1752                                 owner->view()->update(1);
1753                         }
1754                 } else {
1755                         owner->view()->cut();
1756                 }
1757                 SetUpdateTimer();
1758         }
1759         break;
1760
1761         /* -------> Delete word forward. */
1762         case LFUN_DELETE_WORD_FORWARD:
1763                 owner->view()->update(-2);
1764                 FreeUpdateTimer();
1765                 owner->view()->text->DeleteWordForward();
1766                 owner->view()->update( 1 );
1767                 SetUpdateTimer();
1768                 moveCursorUpdate(false);
1769                 owner->getMiniBuffer()->Set(CurrentState(owner->view()));
1770                 break;
1771
1772                 /* -------> Delete word backward. */
1773         case LFUN_DELETE_WORD_BACKWARD:
1774                 owner->view()->update(-2);
1775                 FreeUpdateTimer();
1776                 owner->view()->text->DeleteWordBackward();
1777                 owner->view()->update( 1 );
1778                 SetUpdateTimer();
1779                 moveCursorUpdate(false);
1780                 owner->getMiniBuffer()->Set(CurrentState(owner->view()));
1781                 break;
1782                 
1783                 /* -------> Kill to end of line. */
1784         case LFUN_DELETE_LINE_FORWARD:
1785                 FreeUpdateTimer();
1786                 owner->view()->update(-2);
1787                 owner->view()->text->DeleteLineForward();
1788                 owner->view()->update( 1 );
1789                 SetUpdateTimer();
1790                 moveCursorUpdate(false);
1791                 break;
1792                 
1793                 /* -------> Set mark off. */
1794         case LFUN_MARK_OFF:
1795                 owner->view()->beforeChange();
1796                 owner->view()->update(0);
1797                 owner->view()->text->sel_cursor = 
1798                         owner->view()->text->cursor;
1799                 setMessage(N_("Mark off"));
1800                 break;
1801
1802                 /* -------> Set mark on. */
1803         case LFUN_MARK_ON:
1804                 owner->view()->beforeChange();
1805                 owner->view()->text->mark_set = 1;
1806                 owner->view()->update( 0 );
1807                 owner->view()->text->sel_cursor = 
1808                         owner->view()->text->cursor;
1809                 setMessage(N_("Mark on"));
1810                 break;
1811                 
1812         case LFUN_BACKSPACE:
1813         {
1814                 FreeUpdateTimer();
1815                 if (!owner->view()->text->selection) {
1816                         if (owner->getIntl()->getTrans()->backspace()) {
1817                                 owner->view()->text->Backspace();
1818                                 owner->view()->text->sel_cursor = 
1819                                         owner->view()->text->cursor;
1820                                 owner->view()->update(1);
1821                                 // It is possible to make it a lot faster still
1822                                 // just comment out the lone below...
1823                                 owner->view()->showCursor();
1824                         }
1825                 } else {
1826                         owner->view()->cut();
1827                 }
1828                 SetUpdateTimer();
1829                 owner->getMiniBuffer()->Set(CurrentState(owner->view()));
1830                 owner->view()->setState();
1831         }
1832         break;
1833
1834         case LFUN_BACKSPACE_SKIP:
1835         {
1836                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
1837                 
1838                 LyXCursor cursor = owner->view()->text->cursor;
1839                 
1840                 FreeUpdateTimer();
1841                 if (!owner->view()->text->selection) {
1842                         if (cursor.pos == 0 
1843                             && !(cursor.par->added_space_top 
1844                                  == VSpace (VSpace::NONE))) {
1845                                 owner->view()->text->SetParagraph 
1846                                         (cursor.par->line_top,      
1847                                          cursor.par->line_bottom,
1848                                          cursor.par->pagebreak_top, 
1849                                          cursor.par->pagebreak_bottom,
1850                                          VSpace(VSpace::NONE), cursor.par->added_space_bottom,
1851                                          cursor.par->align, 
1852                                          cursor.par->labelwidthstring, 0);
1853                                 owner->view()->update (1);
1854                         } else {
1855                                 owner->view()->text->Backspace();
1856                                 owner->view()->text->sel_cursor 
1857                                         = cursor;
1858                                 owner->view()->update(1);
1859                         }
1860                 } else
1861                         owner->view()->cut();
1862                 SetUpdateTimer();
1863         }
1864         break;
1865
1866         case LFUN_BREAKPARAGRAPH:
1867         {
1868                 owner->view()->beforeChange();
1869                 owner->view()->text->BreakParagraph(0);
1870                 owner->view()->update(1);
1871                 SetUpdateTimer(0.01);
1872                 owner->view()->text->sel_cursor = 
1873                         owner->view()->text->cursor;
1874                 owner->view()->setState();
1875                 owner->getMiniBuffer()->Set(CurrentState(owner->view()));
1876                 break;
1877         }
1878
1879         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1880         {
1881                 owner->view()->beforeChange();
1882                 owner->view()->text->BreakParagraph(1);
1883                 owner->view()->update(1);
1884                 SetUpdateTimer(0.01);
1885                 owner->view()->text->sel_cursor = 
1886                         owner->view()->text->cursor;
1887                 owner->view()->setState();
1888                 owner->getMiniBuffer()->Set(CurrentState(owner->view()));
1889                 break;
1890         }
1891         
1892         case LFUN_BREAKPARAGRAPH_SKIP:
1893         {
1894                 // When at the beginning of a paragraph, remove
1895                 // indentation and add a "defskip" at the top.
1896                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
1897                 
1898                 LyXCursor cursor = owner->view()->text->cursor;
1899                 
1900                 owner->view()->beforeChange();
1901                 if (cursor.pos == 0) {
1902                         if (cursor.par->added_space_top == VSpace(VSpace::NONE)) {
1903                                 owner->view()->text->SetParagraph
1904                                         (cursor.par->line_top,      
1905                                          cursor.par->line_bottom,
1906                                          cursor.par->pagebreak_top, 
1907                                          cursor.par->pagebreak_bottom,
1908                                          VSpace(VSpace::DEFSKIP), cursor.par->added_space_bottom,
1909                                          cursor.par->align, 
1910                                          cursor.par->labelwidthstring, 1);
1911                                 owner->view()->update(1);
1912                         } 
1913                 }
1914                 else {
1915                         owner->view()->text->BreakParagraph(0);
1916                         owner->view()->update(1);
1917                 }
1918                 SetUpdateTimer(0.01);
1919                 owner->view()->text->sel_cursor = cursor;
1920                 owner->view()->setState();
1921                 owner->getMiniBuffer()->Set(CurrentState(owner->view()));
1922         }
1923         break;
1924
1925         case LFUN_PARAGRAPH_SPACING:
1926         {
1927                 LyXParagraph * par = owner->view()->text->cursor.par;
1928                 Spacing::Space cur_spacing = par->spacing.getSpace();
1929                 float cur_value = 1.0;
1930                 if (cur_spacing == Spacing::Other) {
1931                         cur_value = par->spacing.getValue();
1932                 }
1933                 
1934 #ifdef HAVE_SSTREAM
1935                 istringstream istr(argument);
1936 #else
1937                 istrstream istr(argument.c_str());
1938 #endif
1939                 string tmp;
1940                 istr >> tmp;
1941                 Spacing::Space new_spacing = cur_spacing;
1942                 float new_value = cur_value;
1943                 if (tmp.empty()) {
1944                         lyxerr << "Missing argument to `paragraph-spacing'"
1945                                << endl;
1946                 } else if (tmp == "single") {
1947                         new_spacing = Spacing::Single;
1948                 } else if (tmp == "onehalf") {
1949                         new_spacing = Spacing::Onehalf;
1950                 } else if (tmp == "double") {
1951                         new_spacing = Spacing::Double;
1952                 } else if (tmp == "other") {
1953                         new_spacing = Spacing::Other;
1954                         float tmpval = 0.0;
1955                         istr >> tmpval;
1956                         lyxerr << "new_value = " << tmpval << endl;
1957                         if (tmpval != 0.0)
1958                                 new_value = tmpval;
1959                 } else if (tmp == "default") {
1960                         new_spacing = Spacing::Default;
1961                 } else {
1962                         lyxerr << _("Unknown spacing argument: ")
1963                                << argument << endl;
1964                 }
1965                 if (cur_spacing != new_spacing || cur_value != new_value) {
1966                         par->spacing.set(new_spacing, new_value);
1967                         owner->view()->text->RedoParagraph();
1968                         owner->view()->update(-1);
1969                 }
1970         }
1971         break;
1972         
1973         case LFUN_QUOTE:
1974                 owner->view()->beforeChange();
1975                 owner->view()->text->InsertChar('\"');  // This " matches the single quote in the code
1976                 owner->view()->update(1);
1977                 SetUpdateTimer();
1978                 moveCursorUpdate(false);
1979                 break;
1980
1981         case LFUN_HTMLURL:
1982         case LFUN_URL:
1983         {
1984                 InsetCommand * new_inset;
1985                 if (action == LFUN_HTMLURL)
1986                         new_inset = new InsetUrl("htmlurl", "", "");
1987                 else
1988                         new_inset = new InsetUrl("url", "", "");
1989                 owner->view()->insertInset(new_inset);
1990                 new_inset->Edit(owner->view(), 0, 0, 0);
1991         }
1992         break;
1993         
1994         case LFUN_INSET_TEXT:
1995         {
1996                 InsetText * new_inset = new InsetText(owner->buffer());
1997                 owner->view()->insertInset(new_inset);
1998                 new_inset->Edit(owner->view(), 0, 0, 0);
1999         }
2000         break;
2001         case LFUN_INSET_ERT:
2002         {
2003                 InsetERT * new_inset = new InsetERT(owner->buffer());
2004                 owner->view()->insertInset(new_inset);
2005                 new_inset->Edit(owner->view(), 0, 0, 0);
2006         }
2007         break;
2008         
2009         case LFUN_INSET_FOOTNOTE:
2010         {
2011                 InsetFoot * new_inset = new InsetFoot(owner->buffer());
2012                 owner->view()->insertInset(new_inset);
2013                 new_inset->Edit(owner->view(), 0, 0, 0);
2014         }
2015         break;
2016
2017         case LFUN_INSET_TABULAR:
2018         {
2019                 int r = 2, c = 2;
2020                 if (!argument.empty())
2021                         sscanf(argument.c_str(),"%d%d",&r,&c);
2022                 InsetTabular * new_inset = new InsetTabular(owner->buffer(),r,c);
2023                 owner->view()->insertInset(new_inset);
2024                 new_inset->Edit(owner->view(), 0, 0, 0);
2025         }
2026         break;
2027
2028         // --- lyxserver commands ----------------------------
2029
2030         case LFUN_CHARATCURSOR:
2031         {
2032                 LyXParagraph::size_type pos = 
2033                         owner->view()->text->cursor.pos;
2034                 if(pos < owner->view()->text->cursor.par->size())
2035                         //dispatch_buffer = owner->view()->text->
2036                         //      cursor.par->text[pos];
2037                         dispatch_buffer =
2038                                 owner->view()->text->
2039                                 cursor.par->GetChar(pos);
2040                 else
2041                         dispatch_buffer = "EOF";
2042         }
2043         break;
2044         
2045         case LFUN_GETXY:
2046                 dispatch_buffer = 
2047                         tostr(owner->view()->text->cursor.x) + ' '
2048                         + tostr(owner->view()->text->cursor.y);
2049                 break;
2050                 
2051         case LFUN_SETXY:
2052         {
2053                 int  x;
2054                 long y;
2055                 sscanf(argument.c_str(), " %d %ld", &x, &y);
2056                 owner->view()->text->SetCursorFromCoordinates(x, y);
2057         }
2058         break;
2059         
2060         case LFUN_GETLAYOUT:
2061                 dispatch_buffer =  
2062                         tostr(owner->view()->text->cursor.par->layout);
2063                 break;
2064                         
2065         case LFUN_GETFONT:
2066         {
2067                 LyXFont & font = owner->view()->text->current_font;
2068                 if(font.shape() == LyXFont::ITALIC_SHAPE)
2069                         dispatch_buffer = 'E';
2070                 else if(font.shape() == LyXFont::SMALLCAPS_SHAPE)
2071                         dispatch_buffer = 'N';
2072                 else
2073                         dispatch_buffer = '0';
2074
2075         }
2076         break;
2077
2078         case LFUN_GETLATEX:
2079         {
2080                 LyXFont & font = owner->view()->text->current_font;
2081                 if(font.latex() == LyXFont::ON)
2082                         dispatch_buffer = 'L';
2083                 else
2084                         dispatch_buffer = '0';
2085         }
2086         break;
2087
2088         case LFUN_GETNAME:
2089                 setMessage(owner->buffer()->fileName());
2090                 lyxerr.debug() << "FNAME["
2091                                << owner->buffer()->fileName()
2092                                << "] " << endl;
2093                 break;
2094                 
2095         case LFUN_NOTIFY:
2096         {
2097                 string buf;
2098                 keyseq.print(buf);
2099                 dispatch_buffer = buf;
2100                 lyxserver->notifyClient(dispatch_buffer);
2101         }
2102         break;
2103
2104         case LFUN_GOTOFILEROW:
2105         {
2106                 char file_name[100];
2107                 int  row;
2108                 sscanf(argument.c_str(), " %s %d", file_name, &row);
2109
2110                 // Must replace extension of the file to be .lyx and get full path
2111                 string s = ChangeExtension(string(file_name), ".lyx");
2112
2113                 // Either change buffer or load the file
2114                 if (bufferlist.exists(s))
2115                         owner->view()->buffer(bufferlist.getBuffer(s));
2116                 else
2117                         owner->view()->buffer(bufferlist.loadLyXFile(s));
2118
2119                 // Set the cursor  
2120                 owner->view()->setCursorFromRow(row);
2121
2122                 // Recenter screen
2123                 owner->view()->center();
2124         }
2125         break;
2126
2127         case LFUN_APROPOS:
2128         case LFUN_GETTIP:
2129         {
2130                 int qa = lyxaction.LookupFunc(argument.c_str());
2131                 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
2132         }
2133         break;
2134
2135         // --- accented characters ---------------------------
2136                 
2137         case LFUN_UMLAUT:
2138         case LFUN_CIRCUMFLEX:
2139         case LFUN_GRAVE:
2140         case LFUN_ACUTE:
2141         case LFUN_TILDE:
2142         case LFUN_CEDILLA:
2143         case LFUN_MACRON:
2144         case LFUN_DOT:
2145         case LFUN_UNDERDOT:
2146         case LFUN_UNDERBAR:
2147         case LFUN_CARON:
2148         case LFUN_SPECIAL_CARON:
2149         case LFUN_BREVE:
2150         case LFUN_TIE:
2151         case LFUN_HUNG_UMLAUT:
2152         case LFUN_CIRCLE:
2153         case LFUN_OGONEK:
2154         {
2155                 char c = 0;
2156                 
2157                 if (keyseq.length == -1 && keyseq.getiso() != 0) 
2158                         c = keyseq.getiso();
2159                 
2160                 owner->getIntl()->getTrans()->
2161                         deadkey(c, get_accent(action).accent, 
2162                                 owner->view()->text);
2163                 
2164                 // Need to reset, in case the minibuffer calls these
2165                 // actions
2166                 keyseq.reset();
2167                 keyseq.length = 0;
2168                 
2169                 // copied verbatim from do_accent_char
2170                 owner->view()->update(1);
2171
2172                 SetUpdateTimer();
2173                 owner->view()->text->sel_cursor = 
2174                         owner->view()->text->cursor;
2175         }   
2176         break;
2177         
2178         // --- toolbar ----------------------------------
2179         case LFUN_PUSH_TOOLBAR:
2180         {
2181                 int nth = strToInt(argument);
2182                 if (lyxerr.debugging(Debug::TOOLBAR)) {
2183                         lyxerr << "LFUN_PUSH_TOOLBAR: argument = `"
2184                                << argument << "'\n"
2185                                << "LFUN_PUSH_TOOLBAR: nth = `"
2186                                << nth << "'" << endl;
2187                 }
2188                 
2189                 if (nth <= 0) {
2190                         LyXBell();
2191                         setErrorMessage(N_("Push-toolbar needs argument > 0"));
2192                 } else {
2193                         owner->getToolbar()->push(nth);
2194                 }
2195         }
2196         break;
2197         
2198         case LFUN_ADD_TO_TOOLBAR:
2199         {
2200                 if (lyxerr.debugging(Debug::TOOLBAR)) {
2201                         lyxerr << "LFUN_ADD_TO_TOOLBAR:"
2202                                 "argument = `" << argument << '\'' << endl;
2203                 }
2204                 string tmp(argument);
2205                 //lyxerr <<string("Argument: ") + argument);
2206                 //lyxerr <<string("Tmp     : ") + tmp);
2207                 if (tmp.empty()) {
2208                         LyXBell();
2209                         setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
2210                 } else {
2211                         owner->getToolbar()->add(argument, false);
2212                         owner->getToolbar()->set();
2213                 }
2214         }
2215         break;
2216         
2217         // --- insert characters ----------------------------------------
2218
2219         // ---  Mathed stuff. If we are here, there is no locked inset yet.
2220         
2221         // Greek mode     
2222         case LFUN_GREEK:
2223         {
2224                 if (!greek_kb_flag) {
2225                         greek_kb_flag = 1;
2226                         setMessage(N_("Math greek mode on"));
2227                 } else
2228                         greek_kb_flag = 0;
2229         }  
2230         break;
2231       
2232         // Greek keyboard      
2233         case LFUN_GREEK_TOGGLE:
2234         {
2235                 greek_kb_flag = greek_kb_flag ? 0 : 2;
2236                 if (greek_kb_flag) {
2237                         setMessage(N_("Math greek keyboard on"));
2238                 } else {
2239                         setMessage(N_("Math greek keyboard off"));
2240                 }
2241         }
2242         break;
2243         
2244         case LFUN_MATH_DELIM:     
2245         case LFUN_INSERT_MATRIX:
2246         {          
2247                 if (owner->view()->available()) { 
2248                         owner->view()->
2249                                 open_new_inset(new InsetFormula(false));
2250                         owner->view()
2251                                 ->the_locking_inset
2252                                 ->LocalDispatch(owner->view(),
2253                                                 action,
2254                                                 argument);
2255                 }
2256         }          
2257         break;
2258                
2259         case LFUN_INSERT_MATH:
2260         {
2261                 math_insert_symbol(argument.c_str());
2262         }
2263         break;
2264         
2265         case LFUN_MATH_DISPLAY:
2266         {
2267                 if (owner->view()->available())
2268                         owner->view()->open_new_inset(new InsetFormula(true));
2269                 break;
2270         }
2271                     
2272         case LFUN_MATH_MACRO:
2273         {
2274                 if (owner->view()->available()) {
2275                         string s(argument);
2276                         if (s.empty())
2277                                 setErrorMessage(N_("Missing argument"));
2278                         else {
2279                                 string s1 = token(s, ' ', 1);
2280                                 int na = s1.empty() ? 0: atoi(s1.c_str());
2281                                 owner->view()->
2282                                         open_new_inset(new InsetFormulaMacro(token(s, ' ', 0), na));
2283                         }
2284                 }
2285         }
2286         break;
2287
2288         case LFUN_MATH_MODE:   // Open or create a math inset
2289         {
2290                 
2291                 if (owner->view()->available())
2292                         owner->view()->open_new_inset(new InsetFormula);
2293                 setMessage(N_("Math editor mode"));
2294         }
2295         break;
2296           
2297         case LFUN_MATH_NUMBER:
2298         case LFUN_MATH_LIMITS:
2299         {
2300                 setErrorMessage(N_("This is only allowed in math mode!"));
2301         }
2302         break;
2303         
2304         case LFUN_INSERT_CITATION:
2305         {   
2306                 InsetCitation * new_inset = new InsetCitation();
2307                 // ale970405
2308                 // The note, if any, must be after the key, delimited
2309                 // by a | so both key and remark can have spaces.
2310                 if (!argument.empty()) {
2311                         string lsarg(argument);
2312                         if (contains(lsarg, "|")) {
2313                                 new_inset->setContents(token(lsarg, '|', 0));
2314                                 new_inset->setOptions(token(lsarg, '|', 1));
2315                         } else
2316                                 new_inset->setContents(lsarg);
2317                         owner->view()->insertInset(new_inset);
2318                 } else {
2319                         owner->view()->insertInset(new_inset);
2320                         new_inset->Edit(owner->view(), 0, 0, 0);
2321                 }
2322         }
2323         break;
2324                     
2325         case LFUN_INSERT_BIBTEX:
2326         {   
2327                 // ale970405+lasgoutt970425
2328                 // The argument can be up to two tokens separated 
2329                 // by a space. The first one is the bibstyle.
2330                 string lsarg(argument);
2331                 string bibstyle = token(lsarg, ' ', 1);
2332                 if (bibstyle.empty())
2333                         bibstyle = "plain";
2334                 InsetBibtex * new_inset 
2335                         = new InsetBibtex(token(lsarg, ' ', 0),
2336                                           bibstyle,
2337                                           owner->buffer());
2338                 
2339                 owner->view()->insertInset(new_inset);
2340                 if (lsarg.empty()) {
2341                         new_inset->Edit(owner->view(), 0, 0, 0);
2342                 }
2343         }
2344         break;
2345                 
2346         // BibTeX data bases
2347         case LFUN_BIBDB_ADD:
2348         {
2349                 InsetBibtex * inset = 
2350                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2351                 if (inset) {
2352                         inset->addDatabase(argument);
2353                 }
2354         }
2355         break;
2356                     
2357         case LFUN_BIBDB_DEL:
2358         {
2359                 InsetBibtex * inset = 
2360                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2361                 if (inset) {
2362                         inset->delDatabase(argument);
2363                 }
2364         }
2365         break;
2366         
2367         case LFUN_BIBTEX_STYLE:
2368         {
2369                 InsetBibtex * inset = 
2370                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2371                 if (inset) {
2372                         inset->setOptions(argument);
2373                 }
2374         }
2375         break;
2376                 
2377         case LFUN_INDEX_INSERT:
2378         case LFUN_INDEX_INSERT_LAST:
2379         {
2380                 // Can't do that at the beginning of a paragraph.
2381                 if (owner->view()->text->cursor.pos - 1 < 0)
2382                         break;
2383
2384                 InsetIndex * new_inset = new InsetIndex();
2385                 if (!argument.empty()) {
2386                         string lsarg(argument);
2387                         new_inset->setContents(lsarg);
2388                         owner->view()->insertInset(new_inset);
2389                 } else {
2390                         //reh 98/09/21
2391                         //get the current word for an argument
2392                         LyXParagraph::size_type lastpos = 
2393                                 owner->view()->text->cursor.pos - 1;
2394                         // Get the current word. note that this must be done
2395                         // before inserting the inset, or the inset will
2396                         // break the word
2397                         string curstring(owner->view()
2398                                          ->text->cursor.par->GetWord(lastpos));
2399
2400                         //make the new inset and write the current word into it
2401                         InsetIndex * new_inset = new InsetIndex();
2402
2403                         new_inset->setContents(curstring);
2404
2405                         //don't edit it if the call was to INSERT_LAST
2406                         if(action != LFUN_INDEX_INSERT_LAST) {
2407                                 new_inset->Edit(owner->view(), 0, 0, 0);
2408                         } else {
2409                                 //it looks blank on the screen unless
2410                                 //we do  something.  put it here.
2411
2412                                 // move the cursor to the returned value of lastpos
2413                                 // but only for the auto-insert
2414                                 owner->view()->text->cursor.pos = lastpos;
2415                         }
2416
2417                         //put the new inset into the buffer.
2418                         // there should be some way of knowing the user
2419                         //cancelled & avoiding this, but i don't know how
2420                         owner->view()->insertInset(new_inset);
2421                 }
2422         }
2423         break;
2424
2425         case LFUN_INDEX_PRINT:
2426         {
2427                 Inset * new_inset = new InsetPrintIndex(owner->buffer());
2428                 owner->view()->insertInset(new_inset, "Standard", true);
2429         }
2430         break;
2431
2432         case LFUN_PARENTINSERT:
2433         {
2434                 lyxerr << "arg " << argument << endl;
2435                 Inset * new_inset = new InsetParent(argument, owner->buffer());
2436                 owner->view()->insertInset(new_inset, "Standard", true);
2437         }
2438         break;
2439
2440         case LFUN_CHILDINSERT:
2441         {
2442                 Inset * new_inset = new InsetInclude(argument,
2443                                                      owner->buffer());
2444                 owner->view()->insertInset(new_inset, "Standard", true);
2445                 new_inset->Edit(owner->view(), 0, 0, 0);
2446         }
2447         break;
2448
2449         case LFUN_CHILDOPEN:
2450         {
2451                 string filename =
2452                         MakeAbsPath(argument, 
2453                                     OnlyPath(owner->buffer()->fileName()));
2454                 setMessage(N_("Opening child document ") +
2455                            MakeDisplayPath(filename) + "...");
2456                 owner->view()->savePosition();
2457                 if (bufferlist.exists(filename))
2458                         owner->view()->buffer(bufferlist.getBuffer(filename));
2459                 else
2460                         owner->view()->buffer(bufferlist.loadLyXFile(filename));
2461         }
2462         break;
2463
2464         case LFUN_INSERT_NOTE:
2465                 owner->view()->insertNote();
2466                 break;
2467                 
2468         case LFUN_INSERTFOOTNOTE: 
2469         {
2470                 LyXParagraph::footnote_kind kind;
2471                 if (argument == "footnote")
2472                         { kind = LyXParagraph::FOOTNOTE; }
2473                 else if (argument == "margin")
2474                         { kind = LyXParagraph::MARGIN; }
2475                 else if (argument == "figure")
2476                         { kind = LyXParagraph::FIG; }
2477                 else if (argument == "table")
2478                         { kind = LyXParagraph::TAB; }
2479                 else if (argument == "wide-fig")
2480                         { kind = LyXParagraph::WIDE_FIG; }
2481                 else if (argument == "wide-tab")
2482                         { kind = LyXParagraph::WIDE_TAB; }
2483                 else if (argument == "algorithm")
2484                         { kind = LyXParagraph::ALGORITHM; }
2485                 else {
2486                         setErrorMessage(N_("Unknown kind of footnote"));
2487                         break;
2488                 }
2489                 owner->view()->text->InsertFootnoteEnvironment(kind);
2490                 owner->view()->update(1);
2491                 owner->view()->setState();
2492         }
2493         break;
2494         
2495         case LFUN_BUFFERBULLETSSELECT:
2496                 bulletForm();
2497                 break;
2498                 
2499         case LFUN_TOGGLECURSORFOLLOW:
2500                 cursor_follows_scrollbar = !cursor_follows_scrollbar;
2501                 break;
2502                 
2503         case LFUN_KMAP_OFF:             // keymap off
2504                 owner->getIntl()->KeyMapOn(false);
2505                 break;
2506                 
2507         case LFUN_KMAP_PRIM:    // primary keymap
2508                 owner->getIntl()->KeyMapPrim();
2509                 break;
2510                 
2511         case LFUN_KMAP_SEC:             // secondary keymap
2512                 owner->getIntl()->KeyMapSec();
2513                 break;
2514                 
2515         case LFUN_KMAP_TOGGLE:  // toggle keymap
2516                 owner->getIntl()->ToggleKeyMap();
2517                 break;
2518
2519         case LFUN_SELFINSERT:
2520         {
2521                 for (string::size_type i = 0; i < argument.length(); ++i) {
2522                         owner->view()->text->InsertChar(argument[i]);
2523                         // This needs to be in the loop, or else we
2524                         // won't break lines correctly. (Asger)
2525                         owner->view()->update(1);
2526                 }
2527                 SetUpdateTimer();
2528                 owner->view()->text->sel_cursor = 
2529                         owner->view()->text->cursor;
2530                 moveCursorUpdate(false);
2531         }
2532         break;
2533
2534         case LFUN_SEQUENCE: 
2535         {
2536                 // argument contains ';'-terminated commands
2537                 while (argument.find(';') != string::npos) {
2538                         string first;
2539                         argument = split(argument, first, ';');
2540                         Dispatch(first);
2541                 }
2542         }
2543         break;
2544
2545         case LFUN_DATE_INSERT:  // jdblair: date-insert cmd
2546         {
2547                 char datetmp[32];
2548                 int datetmp_len;
2549                 time_t now_time_t;
2550                 struct tm *now_tm;
2551                 static string arg;
2552                 
2553                 now_time_t = time(NULL);
2554                 now_tm = localtime(&now_time_t);
2555                 (void)setlocale(LC_TIME, "");
2556                 if (!argument.empty())
2557                         arg = argument;
2558                 else if (arg.empty())
2559                         arg = lyxrc.date_insert_format;
2560                 datetmp_len = (int) strftime(datetmp, 32, arg.c_str(), now_tm);
2561                 for (int i = 0; i < datetmp_len; i++) {
2562                         owner->view()->text->InsertChar(datetmp[i]);
2563                         owner->view()->update(1);
2564                 }
2565                 SetUpdateTimer();
2566                 owner->view()->text->sel_cursor = owner->view()->text->cursor;
2567                 moveCursorUpdate(false);
2568         }
2569         break;
2570
2571         case LFUN_SAVEPREFERENCES:
2572         {
2573                 Path p(user_lyxdir);
2574                 lyxrc.write("preferences");
2575         }
2576         break;
2577         
2578         case LFUN_UNKNOWN_ACTION:
2579         {
2580                 if(!owner->buffer()) {
2581                         LyXBell();
2582                         setErrorMessage(N_("No document open"));
2583                         break;
2584                 }
2585
2586                 if (owner->buffer()->isReadonly()) {
2587                         LyXBell();
2588                         setErrorMessage(N_("Document is read only"));
2589                         break;
2590                 }
2591                          
2592                 if (!argument.empty()) {
2593                         
2594                         /* Automatically delete the currently selected
2595                          * text and replace it with what is being
2596                          * typed in now. Depends on lyxrc settings
2597                          * "auto_region_delete", which defaults to
2598                          * true (on). */
2599                 
2600                         if ( lyxrc.auto_region_delete ) {
2601                                 if (owner->view()->text->selection){
2602                                         owner->view()->text->CutSelection(false);
2603                                         owner->view()->update(-1);
2604                                 }
2605                         }
2606                         
2607                         owner->view()->beforeChange();
2608                         
2609                         for (string::size_type i = 0;
2610                              i < argument.length(); ++i) {
2611                                 if (greek_kb_flag) {
2612                                         if (!math_insert_greek(argument[i]))
2613                                                 owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2614                                 } else
2615                                         owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2616                         }
2617
2618                         owner->view()->update(1);
2619                         SetUpdateTimer();
2620
2621                         owner->view()->text->sel_cursor = 
2622                                 owner->view()->text->cursor;
2623                         moveCursorUpdate(false);
2624                         return string();
2625                 } else {
2626                         // why is an "Unknown action" with empty
2627                         // argument even dispatched in the first
2628                         // place? I`ll probably change that. (Lgb)
2629                         LyXBell();
2630                         setErrorMessage(N_("Unknown action"));
2631                 }
2632                 break;
2633         default:
2634                 lyxerr << "A truly unknown func!" << endl;
2635                 break;
2636         }
2637         } // end of switch
2638   exit_with_message:
2639
2640         string res = getMessage();
2641
2642         if (res.empty()) {
2643                 if (!commandshortcut.empty()) {
2644                         string newbuf = owner->getMiniBuffer()->GetText();
2645                         if (newbuf != commandshortcut) {
2646                                 owner->getMiniBuffer()->Set(newbuf
2647                                                             + " " +
2648                                                             commandshortcut);
2649                         }
2650                 }
2651         } else {
2652                 owner->getMiniBuffer()->Set(string(_(res.c_str()))
2653                                             + " " + commandshortcut);
2654         }
2655
2656         return res;
2657 }
2658
2659
2660 void LyXFunc::setupLocalKeymap()
2661 {
2662         keyseq.stdmap = keyseq.curmap = toplevel_keymap;
2663         cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap;
2664 }
2665
2666
2667 void LyXFunc::MenuNew(bool fromTemplate)
2668 {
2669         string fname, initpath = lyxrc.document_path;
2670         LyXFileDlg fileDlg;
2671
2672         if (owner->view()->available()) {
2673                 string trypath = owner->buffer()->filepath;
2674                 // If directory is writeable, use this as default.
2675                 if (IsDirWriteable(trypath) == 1)
2676                         initpath = trypath;
2677         }
2678
2679         ProhibitInput(owner->view());
2680         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2681         fileDlg.SetButton(1, _("Templates"), lyxrc.template_path);
2682         fname = fileDlg.Select(_("Enter Filename for new document"), 
2683                                initpath, "*.lyx", _("newfile"));
2684         AllowInput(owner->view());
2685         
2686         if (fname.empty()) {
2687                 owner->getMiniBuffer()->Set(_("Canceled."));
2688                 lyxerr.debug() << "New Document Cancelled." << endl;
2689                 return;
2690         }
2691         
2692         // get absolute path of file and make sure the filename ends
2693         // with .lyx
2694         string s = MakeAbsPath(fname);
2695         if (!IsLyXFilename(s))
2696                 s += ".lyx";
2697
2698         // Check if the document already is open
2699         if (bufferlist.exists(s)){
2700                 switch(AskConfirmation(_("Document is already open:"), 
2701                                        MakeDisplayPath(s, 50),
2702                                        _("Do you want to close that document now?\n"
2703                                          "('No' will just switch to the open version)")))
2704                         {
2705                         case 1: // Yes: close the document
2706                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
2707                                 // If close is canceled, we cancel here too.
2708                                         return;
2709                                 break;
2710                         case 2: // No: switch to the open document
2711                                 owner->view()->buffer(bufferlist.getBuffer(s));
2712                                 return;
2713                         case 3: // Cancel: Do nothing
2714                                 owner->getMiniBuffer()->Set(_("Canceled."));
2715                                 return;
2716                         }
2717         }
2718         
2719         // Check whether the file already exists
2720         if (IsLyXFilename(s)) {
2721                 FileInfo fi(s);
2722                 if (fi.readable() &&
2723                     AskQuestion(_("File already exists:"), 
2724                                 MakeDisplayPath(s, 50),
2725                                 _("Do you want to open the document?"))) {
2726                         // loads document
2727                         owner->getMiniBuffer()->Set(_("Opening document"), 
2728                                                     MakeDisplayPath(s), "...");
2729                         XFlush(fl_display);
2730                         owner->view()->buffer(
2731                                 bufferlist.loadLyXFile(s));
2732                         owner->getMiniBuffer()->Set(_("Document"),
2733                                                     MakeDisplayPath(s),
2734                                                     _("opened."));
2735                         return;
2736                 }
2737         }
2738
2739         // The template stuff
2740         string templname;
2741         if (fromTemplate) {
2742                 ProhibitInput(owner->view());
2743                 fname = fileDlg.Select(_("Choose template"),
2744                                        lyxrc.template_path,
2745                                        "*.lyx");
2746                 templname = fname;
2747                 AllowInput(owner->view());
2748         }
2749   
2750         // find a free buffer
2751         lyxerr.debug() << "Find a free buffer." << endl;
2752         owner->view()->buffer(bufferlist.newFile(s, templname));
2753 }
2754
2755
2756 void LyXFunc::MenuOpen()
2757 {
2758         string initpath = lyxrc.document_path;
2759         LyXFileDlg fileDlg;
2760   
2761         if (owner->view()->available()) {
2762                 string trypath = owner->buffer()->filepath;
2763                 // If directory is writeable, use this as default.
2764                 if (IsDirWriteable(trypath) == 1)
2765                         initpath = trypath;
2766         }
2767
2768         // launches dialog
2769         ProhibitInput(owner->view());
2770         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2771         fileDlg.SetButton(1, _("Examples"), 
2772                           AddPath(system_lyxdir, "examples"));
2773         string filename = fileDlg.Select(_("Select Document to Open"),
2774                                          initpath, "*.lyx");
2775         AllowInput(owner->view());
2776  
2777         // check selected filename
2778         if (filename.empty()) {
2779                 owner->getMiniBuffer()->Set(_("Canceled."));
2780                 return;
2781         }
2782
2783         // get absolute path of file and make sure the filename ends
2784         // with .lyx
2785         filename = MakeAbsPath(filename);
2786         if (!IsLyXFilename(filename))
2787                 filename += ".lyx";
2788
2789         // loads document
2790         owner->getMiniBuffer()->Set(_("Opening document"),
2791                                     MakeDisplayPath(filename), "...");
2792         Buffer * openbuf = bufferlist.loadLyXFile(filename);
2793         if (openbuf) {
2794                 owner->view()->buffer(openbuf);
2795                 owner->getMiniBuffer()->Set(_("Document"),
2796                                             MakeDisplayPath(filename),
2797                                             _("opened."));
2798         } else {
2799                 owner->getMiniBuffer()->Set(_("Could not open document"),
2800                                             MakeDisplayPath(filename));
2801         }
2802 }
2803
2804 // returns filename if file must be imported,
2805 // empty string if either file not found or already loaded
2806 // checks for running without gui are missing.
2807
2808 void LyXFunc::doImportHelper(
2809         string const & file,          // filename (possibly empty)
2810         string const & text,          // info when asking for filename
2811         string const & pattern,       // filetype
2812         bool func(BufferView *, string const &)     // the real import function
2813 )
2814 {
2815         string filename = file;
2816
2817         if (filename.empty()) { // need user interaction
2818                 string initpath = lyxrc.document_path;
2819                 LyXFileDlg fileDlg;
2820                 
2821                 if (owner->view()->available()) {
2822                         string trypath = owner->buffer()->filepath;
2823                         // If directory is writeable, use this as default.
2824                         if (IsDirWriteable(trypath) == 1)
2825                                 initpath = trypath;
2826                 }
2827
2828                 // launches dialog
2829                 ProhibitInput(owner->view());
2830                 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2831                 fileDlg.SetButton(1, _("Examples"), 
2832                                         AddPath(system_lyxdir, "examples"));
2833                 filename = fileDlg.Select(text, initpath, pattern);
2834                 AllowInput(owner->view());
2835  
2836                 // check selected filename
2837                 if (filename.empty()) 
2838                         owner->getMiniBuffer()->Set(_("Canceled."));
2839         }
2840
2841         // still no filename? abort
2842         if (filename.empty()) 
2843                 return;
2844
2845         // get absolute path of file
2846         filename = MakeAbsPath(filename);
2847
2848         string lyxfile = ChangeExtension(filename, ".lyx");
2849
2850         // Check if the document already is open
2851         if (bufferlist.exists(lyxfile)) {
2852                 switch(AskConfirmation(_("Document is already open:"), 
2853                                        MakeDisplayPath(lyxfile, 50),
2854                                        _("Do you want to close that document now?\n"
2855                                          "('No' will just switch to the open version)")))
2856                         {
2857                         case 1: // Yes: close the document
2858                                 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
2859                                 // If close is canceled, we cancel here too.
2860                                         return;
2861                                 break;
2862                         case 2: // No: switch to the open document
2863                                 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
2864                                 return;
2865                         case 3: // Cancel: Do nothing
2866                                 owner->getMiniBuffer()->Set(_("Canceled."));
2867                                 return;
2868                         }
2869         }
2870
2871         // Check if a LyX document by the same root exists in filesystem
2872         FileInfo f(lyxfile, true);
2873         if (f.exist() && !AskQuestion(_("A document by the name"), 
2874                                       MakeDisplayPath(lyxfile),
2875                                       _("already exists. Overwrite?"))) {
2876                 owner->getMiniBuffer()->Set(_("Canceled."));
2877                 return;
2878         }
2879         // filename should be valid now
2880
2881         // notify user of import ahead
2882         string displaypath = MakeDisplayPath(filename);
2883         owner->view()->buffer(bufferlist.newFile(lyxfile, string()));
2884         owner->getMiniBuffer()->Set(_("Importing"), displaypath, "...");
2885
2886         // call real importer
2887         bool result = func(owner->view(), filename);
2888
2889         // we are done
2890         if (result)
2891                 owner->getMiniBuffer()->Set(displaypath, _("imported."));
2892         else
2893                 owner->getMiniBuffer()->Set(displaypath, _(": import failed."));
2894 }
2895
2896 static
2897 bool doImportASCIIasLines(BufferView * view, string const & filename)
2898 {
2899         InsertAsciiFile(view, filename, false);
2900         return true;
2901 }
2902
2903 static
2904 bool doImportASCIIasParagraphs(BufferView * view, string const & filename)
2905 {
2906         InsertAsciiFile(view, filename, true);
2907         return true;
2908 }
2909
2910 static
2911 bool doImportLaTeX(BufferView *, string const & filename)
2912 {
2913         ImportLaTeX myImport(filename);
2914         Buffer * openbuf = myImport.run();
2915         return openbuf != NULL;         
2916 }
2917
2918 static
2919 bool doImportNoweb(BufferView *, string const & filename)
2920 {
2921         ImportNoweb myImport(filename);
2922         Buffer * openbuf = myImport.run();
2923         return openbuf != NULL;         
2924 }
2925
2926 static
2927 bool doImportLinuxDoc(BufferView *, string const & filename)
2928 {
2929         // run sgml2lyx
2930         string tmp = lyxrc.linuxdoc_to_lyx_command + filename;
2931         Systemcalls one;
2932         Buffer * buf = 0;
2933
2934         int result = one.startscript(Systemcalls::System, tmp);
2935         if (result == 0) {
2936                 string filename = ChangeExtension(filename, ".lyx");
2937                 // File was generated without problems. Load it.
2938                 buf = bufferlist.loadLyXFile(filename);
2939         }
2940
2941         return result == 0;
2942 }
2943
2944
2945 void LyXFunc::MenuInsertLyXFile(string const & filen)
2946 {
2947         string filename = filen;
2948
2949         if (filename.empty()) {
2950                 // Launch a file browser
2951                 string initpath = lyxrc.document_path;
2952                 LyXFileDlg fileDlg;
2953
2954                 if (owner->view()->available()) {
2955                         string trypath = owner->buffer()->filepath;
2956                         // If directory is writeable, use this as default.
2957                         if (IsDirWriteable(trypath) == 1)
2958                                 initpath = trypath;
2959                 }
2960
2961                 // launches dialog
2962                 ProhibitInput(owner->view());
2963                 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2964                 fileDlg.SetButton(1, _("Examples"), 
2965                                   AddPath(system_lyxdir, "examples"));
2966                 filename = fileDlg.Select(_("Select Document to Insert"),
2967                                           initpath, "*.lyx");
2968                 AllowInput(owner->view());
2969
2970                 // check selected filename
2971                 if (filename.empty()) {
2972                         owner->getMiniBuffer()->Set(_("Canceled."));
2973                         return;
2974                 }
2975         } 
2976
2977         // get absolute path of file and make sure the filename ends
2978         // with .lyx
2979         filename = MakeAbsPath(filename);
2980         if (!IsLyXFilename(filename))
2981                 filename += ".lyx";
2982
2983         // Inserts document
2984         owner->getMiniBuffer()->Set(_("Inserting document"),
2985                                     MakeDisplayPath(filename), "...");
2986         bool res = owner->view()->insertLyXFile(filename);
2987         if (res) {
2988                 owner->getMiniBuffer()->Set(_("Document"),
2989                                             MakeDisplayPath(filename),
2990                                             _("inserted."));
2991         } else {
2992                 owner->getMiniBuffer()->Set(_("Could not insert document"),
2993                                             MakeDisplayPath(filename));
2994         }
2995 }
2996
2997 void LyXFunc::doImport(string const & argument)
2998 {
2999         string type;
3000         string filename = split(argument, type, ' ');
3001         lyxerr.debug() << "LyXFunc::doImport: " << type 
3002                        << " file: " << filename << endl;
3003
3004         if (type == "latex") 
3005                 doImportHelper(filename,
3006                                _("Select LaTeX file to import"), "*.tex", 
3007                                doImportLaTeX);
3008         else if (type == "ascii") 
3009                 doImportHelper(filename,
3010                                _("Select ASCII file to import"), "*.txt", 
3011                                doImportASCIIasLines);
3012         else if (type == "asciiparagraph") 
3013                 doImportHelper(filename,
3014                                _("Select ASCII file to import"), "*.txt", 
3015                                doImportASCIIasParagraphs);
3016         else if (type == "noweb") 
3017                 doImportHelper(filename,
3018                                _("Select NoWeb file to import"), "*.nw", 
3019                                doImportNoweb);
3020         else if (type == "linuxdoc") 
3021                 doImportHelper(filename,
3022                                _("Select LinuxDoc file to import"), "*.doc", 
3023                                doImportLinuxDoc);
3024         else 
3025                 setErrorMessage(string(N_("Unknown import type: ")) + type);
3026 }
3027
3028 void LyXFunc::reloadBuffer()
3029 {
3030         string fn = owner->buffer()->fileName();
3031         if (bufferlist.close(owner->buffer()))
3032                 owner->view()->buffer(bufferlist.loadLyXFile(fn));
3033 }
3034
3035
3036 void LyXFunc::CloseBuffer()
3037 {
3038         if (bufferlist.close(owner->buffer()) && !quitting) {
3039                 if (bufferlist.empty()) {
3040                         // need this otherwise SEGV may occur while trying to
3041                         // set variables that don't exist
3042                         // since there's no current buffer
3043                         CloseAllBufferRelatedPopups();
3044                 }
3045                 else {
3046                         owner->view()->buffer(bufferlist.first());
3047                 }
3048         }
3049 }
3050
3051
3052 Inset * LyXFunc::getInsetByCode(Inset::Code code)
3053 {
3054         bool found = false;
3055         Inset * inset = 0;
3056         LyXCursor cursor = owner->view()->text->cursor;
3057         LyXParagraph::size_type pos = cursor.pos;
3058         LyXParagraph * par = cursor.par;
3059         
3060         while (par && !found) {
3061                 while ((inset = par->ReturnNextInsetPointer(pos))){
3062                         if (inset->LyxCode() == code) {
3063                                 found = true;
3064                                 break;
3065                         }
3066                         ++pos;
3067                 } 
3068                 par = par->next;
3069         }
3070         return found ? inset : 0;
3071 }
3072
3073
3074 // Each "owner" should have it's own message method. lyxview and
3075 // the minibuffer would use the minibuffer, but lyxserver would
3076 // send an ERROR signal to its client.  Alejandro 970603
3077 // This func is bit problematic when it comes to NLS, to make the
3078 // lyx servers client be language indepenent we must not translate
3079 // strings sent to this func.
3080 void LyXFunc::setErrorMessage(string const & m) const
3081 {
3082         dispatch_buffer = m;
3083         errorstat = true;
3084 }
3085
3086
3087 void LyXFunc::setMessage(string const & m)
3088 {
3089         dispatch_buffer = m;
3090 }