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