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