]> git.lyx.org Git - features.git/blob - src/lyxfunc.C
66c417974674144d1bfd9ecec93aae94fa002a48
[features.git] / src / lyxfunc.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef HAVE_SSTREAM
14 #include <sstream>
15 using std::istringstream;
16 #else
17 #include <strstream>
18 #endif
19
20 #include <time.h>
21 #include <locale.h>
22
23 #include <cstdlib>
24 #include <cctype>
25 #include <cstring>
26
27 #ifdef __GNUG__
28 #pragma implementation
29 #endif
30
31 #include "lyxlookup.h"
32 #include "kbmap.h"
33 #include "lyxfunc.h"
34 #include "bufferlist.h"
35 #include "lyxserver.h"
36 #include "lyx.h"
37 #include "intl.h"
38 #include "lyx_main.h"
39 #include "lyx_cb.h"
40 #include "LyXAction.h"
41 #include "insets/inseturl.h"
42 #include "insets/insetlatexaccent.h"
43 #include "insets/insettoc.h"
44 #include "insets/insetlof.h"
45 #include "insets/insetloa.h"
46 #include "insets/insetlot.h"
47 #include "insets/insetref.h"
48 #include "insets/insetparent.h"
49 #include "insets/insetindex.h"
50 #include "insets/insetinclude.h"
51 #include "insets/insetbib.h"
52 #include "insets/insettext.h"
53 //#include "insets/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
82 using std::pair;
83 using std::endl;
84
85 extern bool cursor_follows_scrollbar;
86
87 extern void InsertAsciiFile(BufferView *, string const &, bool);
88 extern void math_insert_symbol(char const *);
89 extern Bool math_insert_greek(char const); // why "Bool"?
90 extern BufferList bufferlist;
91 extern LyXServer * lyxserver;
92 extern short greek_kb_flag;
93 extern FD_form_toc * fd_form_toc;
94 extern bool selection_possible;
95
96 extern kb_keymap * toplevel_keymap;
97
98 extern void MenuWrite(Buffer *);
99 extern void MenuWriteAs(Buffer *);
100 extern int  MenuRunLaTeX(Buffer *);
101 extern int  MenuBuildProg(Buffer *);
102 extern int  MenuRunChktex(Buffer *);
103 extern bool CreatePostscript(Buffer *, bool);
104 extern void MenuPrint(Buffer *);
105 extern void MenuSendto();
106 extern void QuitLyX();
107 extern void MenuFax(Buffer *);
108 extern void MenuExport(Buffer *, string const &);
109 extern void MenuPasteSelection(char at);
110 extern LyXAction lyxaction;
111 // (alkis)
112 extern tex_accent_struct get_accent(kb_action action);
113
114 extern void AutoSave(BufferView *);
115 extern void SetUpdateTimer(float timer = 0.3);
116 extern void FreeUpdateTimer();
117 extern bool PreviewDVI(Buffer *);
118 extern bool PreviewPostscript(Buffer *);
119 extern void MenuInsertLabel(char const *);
120 extern void MenuInsertRef();
121 extern void MenuLayoutCharacter();
122 extern void MenuLayoutParagraph();
123 extern void MenuLayoutDocument();
124 extern void MenuLayoutPaper();
125 extern void MenuLayoutTable(int flag);
126 extern void MenuLayoutQuotes();
127 extern void MenuLayoutPreamble();
128 extern void MenuLayoutSave();
129 extern void bulletForm();
130
131 extern Buffer * NewLyxFile(string const &);
132 extern void LoadLyXFile(string const &);
133 extern void Reconfigure(BufferView *);
134
135 extern LyXTextClass::size_type current_layout;
136 extern int getISOCodeFromLaTeX(char *);
137
138 extern void ShowLatexLog();
139
140 /* === globals =========================================================== */
141
142 bool LyXFunc::show_sc = true;
143
144
145 LyXFunc::LyXFunc(LyXView * o)
146         : owner(o)
147 {
148         meta_fake_bit = 0;
149         lyx_dead_action = LFUN_NOACTION;
150         lyx_calling_dead_action = LFUN_NOACTION;
151         setupLocalKeymap();
152 }
153
154
155 // I changed this func slightly. I commented out the ...FinishUndo(),
156 // this means that all places that used to have a moveCursorUpdate, now
157 // have a ...FinishUndo() as the preceeding statement. I have also added
158 // a moveCursorUpdate to some of the functions that updated the cursor, but
159 // that did not show its new position.
160 inline
161 void LyXFunc::moveCursorUpdate(bool selecting)
162 {
163         if (selecting || owner->view()->text->mark_set) {
164                 owner->view()->text->SetSelection();
165                 owner->view()->toggleToggle();
166                 owner->view()->update(0);
167         } else {
168                 owner->view()->update(-2); // this IS necessary
169                 // (Matthias) 
170         }
171
172         owner->view()->showCursor();
173         
174         /* ---> Everytime the cursor is moved, show the current font state. */
175         // should this too me moved out of this func?
176         //owner->getMiniBuffer()->Set(CurrentState());
177         owner->view()->setState();
178 }
179
180
181 int LyXFunc::processKeyEvent(XEvent * ev)
182 {
183         char s_r[10];
184         string argument;
185         XKeyEvent * keyevent = &ev->xkey;
186         KeySym keysym_return;
187
188         int num_bytes = LyXLookupString(ev, s_r, 10, &keysym_return);
189         s_r[num_bytes] = '\0';
190
191         if (lyxerr.debugging(Debug::KEY)) {
192                 char * tmp = XKeysymToString(keysym_return);
193                 string stm = (tmp ? tmp : "");
194                 lyxerr << "KeySym is "
195                        << stm
196                        << "["
197                        << keysym_return << "]"
198                        << " and num_bytes is "
199                        << num_bytes
200                        << " the string returned is \""
201                        << s_r << '\"' << endl;
202         }
203         // Do nothing if we have nothing (JMarc)
204         if (num_bytes == 0 && keysym_return == NoSymbol) {
205                 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
206                                    << endl;
207                 return 0;
208         }
209         
210         // this function should be used always [asierra060396]
211         UpdatableInset * tli = owner->view()->the_locking_inset;
212         if (owner->view()->available() && tli && (keysym_return==XK_Escape)) {
213                 if (tli == tli->GetLockingInset()) {
214                         owner->view()->unlockInset(tli);
215                         owner->view()->text->CursorRight();
216                         moveCursorUpdate(false);
217                         owner->getMiniBuffer()->Set(CurrentState(owner->view()));
218                 } else {
219                         tli->UnlockInsetInInset(owner->view(),
220                                                 tli->GetLockingInset());
221                 }
222                 return 0;
223         }
224
225         // Can we be sure that this will work for all X-Windows
226         // implementations? (Lgb)
227         // This code snippet makes lyx ignore some keys. Perhaps
228         // all of them should be explictly mentioned?
229         if((keysym_return >= XK_Shift_L && keysym_return <= XK_Hyper_R)
230            || keysym_return == XK_Mode_switch || keysym_return == 0x0)
231                 return 0;
232
233         // Do a one-deep top-level lookup for
234         // cancel and meta-fake keys. RVDK_PATCH_5
235         cancel_meta_seq.reset();
236
237         int action = cancel_meta_seq.addkey(keysym_return, keyevent->state
238                                             &(ShiftMask|ControlMask
239                                               |Mod1Mask)); 
240
241         // When not cancel or meta-fake, do the normal lookup. 
242         // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
243         // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
244         if ( (action != LFUN_CANCEL) && (action != LFUN_META_FAKE) ) {
245
246                 // remove Caps Lock and Mod2 as a modifiers
247                 action = keyseq.addkey(keysym_return,
248                                        (keyevent->state | meta_fake_bit)
249                                        &(ShiftMask|ControlMask
250                                          |Mod1Mask));      
251         }
252         // Dont remove this unless you know what you are doing.
253         meta_fake_bit = 0;
254                 
255         if (action == 0) action = LFUN_PREFIX;
256
257         if (lyxerr.debugging(Debug::KEY)) {
258                 string buf;
259                 keyseq.print(buf);
260                 lyxerr << "Key ["
261                        << action << "]["
262                        << buf << "]["
263                        << num_bytes << "]" << endl;
264         }
265
266         // already here we know if it any point in going further
267         // why not return already here if action == -1 and
268         // num_bytes == 0? (Lgb)
269
270         if(keyseq.length > 1 || keyseq.length < -1) {
271                 string buf;
272                 keyseq.print(buf);
273                 owner->getMiniBuffer()->Set(buf);
274         }
275
276         if (action == -1) {
277                 if (keyseq.length < -1) { // unknown key sequence...
278                         string buf;
279                         LyXBell();
280                         keyseq.print(buf);
281                         owner->getMiniBuffer()->Set(_("Unknown sequence:"), buf);
282                         return 0;
283                 }
284         
285                 char isochar = keyseq.getiso();
286                 if (!(keyevent->state&ControlMask) &&
287                     !(keyevent->state&Mod1Mask) &&
288                     (isochar && keysym_return < 0xF000)) {
289                         argument += isochar;
290                 }
291                 if (argument.empty()) {
292                         lyxerr.debug() << "Empty argument!" << endl;
293                         // This can`t possibly be of any use
294                         // so we`ll skip the dispatch.
295                         return 0;
296                 }
297         } else
298                 if (action == LFUN_SELFINSERT) {
299                         argument = s_r[0];
300                 }
301     
302         bool tmp_sc = show_sc;
303         show_sc = false;
304         Dispatch(action, argument.c_str());
305         show_sc = tmp_sc;
306         
307         return 0;
308
309
310
311 LyXFunc::func_status LyXFunc::getStatus(int ac) const
312 {
313         kb_action action;
314         func_status flag = LyXFunc::OK;
315         string argument;
316         Buffer * buf = owner->buffer();
317         
318         if (lyxaction.isPseudoAction(ac)) 
319                 action = lyxaction.retrieveActionArg(ac, argument);
320         else 
321                 action = static_cast<kb_action>(ac);
322         
323         if (action == LFUN_UNKNOWN_ACTION) {
324                 setErrorMessage(N_("Unknown action"));
325                 return LyXFunc::Unknown;
326         } 
327         
328         // Check whether we need a buffer
329         if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
330                 // Yes we need a buffer, do we have one?
331                 if (buf) {
332                         // yes
333                         // Can we use a readonly buffer?
334                         if (buf->isReadonly() && 
335                             !lyxaction.funcHasFlag(action,
336                                                    LyXAction::ReadOnly)) {
337                                 // no
338                                 setErrorMessage(N_("Document is read-only"));
339                                 flag |= LyXFunc::Disabled;
340                         }
341                 } else {
342                         // no
343                         setErrorMessage(N_("Command not allowed with"
344                                            "out any document open"));
345                         flag |= LyXFunc::Disabled;
346                 }
347         }
348
349         if (flag & LyXFunc::Disabled)
350                 return flag;
351
352         // I would really like to avoid having this switch and rather try to
353         // encode this in the function itself.
354         static bool noLaTeX = lyxrc.latex_command == "none";
355         bool disable = false;
356         switch (action) {
357         case LFUN_PREVIEW:
358                 disable = noLaTeX || lyxrc.view_dvi_command == "none";
359                 break;  
360         case LFUN_PREVIEWPS: 
361                 disable = noLaTeX || lyxrc.view_ps_command == "none";
362                 break;
363         case LFUN_RUNLATEX:
364         case LFUN_RUNDVIPS:
365                 disable = noLaTeX;
366                 break;
367         case LFUN_MENUPRINT:
368                 disable = noLaTeX || lyxrc.print_command == "none";
369                 break;
370         case LFUN_FAX:
371                 disable = noLaTeX || lyxrc.fax_command == "none"; 
372                 break;
373         case LFUN_IMPORT:
374                 if (argument == "latex")
375                         disable = lyxrc.relyx_command == "none";
376                 if (argument == "linuxdoc")
377                         disable = lyxrc.linuxdoc_to_lyx_command == "none";
378                 break;
379         case LFUN_EXPORT:
380                 if (argument == "dvi" || argument == "postscript")
381                         disable = noLaTeX;
382                 if (argument == "html")
383                         disable = lyxrc.html_command == "none";
384                 if (argument == "html-linuxdoc")
385                         disable = lyxrc.linuxdoc_to_html_command == "none";
386                 if (argument == "html-docbook")
387                         disable = lyxrc.docbook_to_html_command == "none";
388                 break;
389         case LFUN_UNDO:
390                 disable = buf->undostack.empty();
391                 break;
392         case LFUN_REDO:
393                 disable = buf->redostack.empty();
394                 break;
395         case LFUN_SPELLCHECK:
396                 disable = lyxrc.isp_command == "none";
397                 break;
398         case LFUN_RUNCHKTEX:
399                 disable = lyxrc.chktex_command == "none";
400                 break;
401         case LFUN_LAYOUT_TABLE:
402                 disable = ! owner->view()->text->cursor.par->table;
403                 break;
404         default:
405                 break;
406         }
407         if (disable)
408                 flag |= LyXFunc::Disabled;
409
410         if (buf) {
411                 func_status box = LyXFunc::ToggleOff;
412                 LyXFont font = owner->view()->text->real_current_font;
413                 switch (action) {
414                 case LFUN_EMPH:
415                         if (font.emph() == LyXFont::ON)
416                                 box = LyXFunc::ToggleOn;
417                         break;
418                 case LFUN_NOUN:
419                         if (font.noun() == LyXFont::ON)
420                                 box = LyXFunc::ToggleOn;
421                         break;
422                 case LFUN_BOLD:
423                         if (font.series() == LyXFont::BOLD_SERIES)
424                                 box = LyXFunc::ToggleOn;
425                         break;
426                 case LFUN_TEX:
427                         if (font.latex() == LyXFont::ON)
428                                 box = LyXFunc::ToggleOn;
429                         break;
430                 default:
431                         box = LyXFunc::OK;
432                         break;
433                 }
434                 flag |= box;
435         }
436
437         return flag;
438 }
439
440
441 string LyXFunc::Dispatch(string const & s) 
442 {
443         // Split command string into command and argument
444         string cmd, line = frontStrip(s);
445         string arg = strip(frontStrip(split(line, cmd, ' ')));
446
447         return Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
448 }
449
450
451 string LyXFunc::Dispatch(int ac,
452                          char const * do_not_use_this_arg)
453 {
454         string argument;
455         kb_action action;
456         
457         // we have not done anything wrong yet.
458         errorstat = false;
459         dispatch_buffer.clear();
460         
461         // if action is a pseudo-action, we need the real action
462         if (lyxaction.isPseudoAction(ac)) {
463                 string tmparg;
464                 action = static_cast<kb_action>
465                         (lyxaction.retrieveActionArg(ac, tmparg));
466                 if (!tmparg.empty())
467                         argument = tmparg;
468         } else {
469                 action = static_cast<kb_action>(ac);
470                 if (do_not_use_this_arg)
471                         argument = do_not_use_this_arg; // except here
472         }
473     
474         selection_possible = false;
475         
476         if (owner->view()->available())
477                 owner->view()->hideCursor();
478
479         // We cannot use this function here
480         if (getStatus(action) & Disabled)
481                 goto exit_with_message;
482
483         commandshortcut.clear();
484         
485         if (lyxrc.display_shortcuts && show_sc) {
486                 if (action != LFUN_SELFINSERT) {
487                         // Put name of command and list of shortcuts
488                         // for it in minibuffer
489                         string comname = lyxaction.getActionName(action);
490
491                         int pseudoaction = action;
492                         bool argsadded = false;
493
494                         if (!argument.empty()) {
495                                 // If we have the command with argument, 
496                                 // this is better
497                                 pseudoaction = 
498                                         lyxaction.searchActionArg(action,
499                                                                   argument.c_str());
500
501                                 if (pseudoaction == -1) {
502                                         pseudoaction = action;
503                                 } else {
504                                         comname += " " + argument;
505                                         argsadded = true;
506                                 }
507                         }
508
509                         string shortcuts = toplevel_keymap->findbinding(pseudoaction);
510
511                         if (!shortcuts.empty()) {
512                                 comname += ": " + shortcuts;
513                         } else if (!argsadded) {
514                                 comname += " " + argument;
515                         }
516
517                         if (!comname.empty()) {
518                                 comname = strip(comname);
519                                 commandshortcut = "(" + comname + ')';
520                                 owner->getMiniBuffer()->Set(commandshortcut);
521                                 // Here we could even add a small pause,
522                                 // to annoy the user and make him learn
523                                 // the shortcuts.
524                                 // No! That will just annoy, not teach
525                                 // anything. The user will read the messages
526                                 // if they are interested. (Asger)
527                         }
528                 }
529         }
530
531         // If in math mode pass the control to
532         // the math inset [asierra060396]
533         if (owner->view()->available() &&
534             owner->view()->the_locking_inset) {
535                 if (action > 1
536                     || (action == LFUN_UNKNOWN_ACTION
537                         && keyseq.length >= -1)) {
538                         if (action == LFUN_UNKNOWN_ACTION
539                             && argument.empty()) {
540                                 argument = keyseq.getiso();
541                         }
542                         // Undo/Redo pre 0.13 is a bit tricky for insets.
543                         if (action == LFUN_UNDO) {
544                                 int slx, sly;
545                                 UpdatableInset * inset = 
546                                         owner->view()->the_locking_inset;
547                                 inset->GetCursorPos(slx, sly);
548                                 owner->view()->unlockInset(inset);
549                                 owner->view()->menuUndo();
550                                 if (owner->view()->text->cursor.par->
551                                     IsInset(owner->view()->text->cursor.pos)) {
552                                         inset = static_cast<UpdatableInset*>(
553                                                 owner->view()->text->cursor.par->
554                                                 GetInset(owner->view()->text->
555                                                          cursor.pos));
556                                 } else {
557                                         inset = 0;
558                                 }
559                                 if (inset)
560                                         inset->Edit(owner->view(),slx,sly,0);
561                                 return string();
562                         } else if (action == LFUN_REDO) {
563                                 int slx, sly;
564                                 UpdatableInset * inset = owner->view()->
565                                         the_locking_inset;
566                                 inset->GetCursorPos(slx, sly);
567                                 owner->view()->unlockInset(inset);
568                                 owner->view()->menuRedo();
569                                 inset = static_cast<UpdatableInset*>(
570                                         owner->view()->text->cursor.par->
571                                         GetInset(owner->view()->text->
572                                                  cursor.pos));
573                                 if (inset)
574                                         inset->Edit(owner->view(),slx,sly,0); 
575                                 return string();
576                         } else if (owner->view()->the_locking_inset->
577                                    LocalDispatch(owner->view(), action,
578                                                  argument) ==
579                                    UpdatableInset::DISPATCHED)
580                                 return string();
581                         else {
582                                 setMessage(N_("Text mode"));
583                                 switch(action) {
584                                 case LFUN_UNKNOWN_ACTION:
585                                 case LFUN_BREAKPARAGRAPH:
586                                 case LFUN_BREAKLINE:
587                                         owner->view()->text->CursorRight();
588                                         owner->view()->setState();
589                                         owner->getMiniBuffer()->Set(CurrentState(owner->view()));
590                                         break;
591                                 case LFUN_RIGHT:
592                                         if (!owner->view()->text->cursor.par->isRightToLeftPar()) {
593                                                 owner->view()->text->CursorRight();
594                                                 moveCursorUpdate(false);
595                                                 owner->getMiniBuffer()->
596                                                         Set(CurrentState(owner->view()));
597                                         }
598                                         return string();
599                                 case LFUN_LEFT: 
600                                         if (owner->view()->text->cursor.par->isRightToLeftPar()) {
601                                                 owner->view()->text->CursorRight();
602                                                 moveCursorUpdate(false);
603                                                 owner->getMiniBuffer()->
604                                                         Set(CurrentState(owner->view()));
605                                         }
606                                         return string();
607                                 default:
608                                         break;
609                                 }
610                         }
611                 }
612         }
613
614         switch(action) {
615                 // --- Misc -------------------------------------------
616         case LFUN_WORDFINDFORWARD  : 
617         case LFUN_WORDFINDBACKWARD : {
618                 static string last_search;
619                 string searched_string;
620             
621                 if (!argument.empty()) {
622                         last_search = argument;
623                         searched_string = argument;
624                 } else {
625                         searched_string = last_search;
626                 }
627
628                 LyXText * ltCur = owner->view()->text ;
629
630                 if (!searched_string.empty() &&
631                     ((action == LFUN_WORDFINDBACKWARD) ? 
632                      ltCur->SearchBackward(searched_string.c_str()) :
633                      ltCur->SearchForward(searched_string.c_str()))) {
634
635                         // ??? What is that ???
636                         owner->view()->update(-2);
637
638                         // ??? Needed ???
639                         // clear the selection (if there is any) 
640                         owner->view()->toggleSelection();
641                         owner->view()->text->ClearSelection();
642
643                         // Move cursor so that successive C-s 's will not stand in place. 
644                         if( action == LFUN_WORDFINDFORWARD ) 
645                                 owner->view()->text->CursorRightOneWord();
646                         owner->view()->text->FinishUndo();
647                         moveCursorUpdate(false);
648
649                         // ??? Needed ???
650                         // set the new selection 
651                         // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
652                         owner->view()->toggleSelection(false);
653                 } else 
654                         LyXBell();      
655          
656                 // REMOVED : if (owner->view()->getWorkArea()->focus)
657                 owner->view()->showCursor();
658         }
659         break;
660
661         case LFUN_PREFIX:
662         {
663                 if (owner->view()->available()) {
664                         owner->view()->update(-2);
665                 }
666                 string buf;
667                 keyseq.print(buf, true);
668                 owner->getMiniBuffer()->Set(buf, string(), string(), 1);
669         }
670         break;
671
672         // --- Misc -------------------------------------------
673         case LFUN_EXEC_COMMAND:
674                 owner->getMiniBuffer()->ExecCommand(); 
675                 break;
676                 
677         case LFUN_CANCEL:                   // RVDK_PATCH_5
678                 keyseq.reset();
679                 meta_fake_bit = 0;
680                 if(owner->view()->available())
681                         // cancel any selection
682                         Dispatch(LFUN_MARK_OFF, 0);
683                 setMessage(N_("Cancel"));
684                 break;
685
686         case LFUN_META_FAKE:                                 // RVDK_PATCH_5
687         {
688                 meta_fake_bit = Mod1Mask;
689                 string buf;
690                 keyseq.print(buf, true);
691                 string res = string("M-") + buf;
692                 setMessage(buf); // RVDK_PATCH_5
693         }
694         break;  
695
696         case LFUN_READ_ONLY_TOGGLE:
697                 if (owner->buffer()->lyxvc.inUse()) {
698                         owner->buffer()->lyxvc.toggleReadOnly();
699                 } else {
700                         owner->buffer()->setReadonly(
701                                 !owner->buffer()->isReadonly());
702                 }
703                 break;
704                 
705         case LFUN_CENTER: // this is center and redraw.
706                 owner->view()->center();
707                 break;
708                 
709         case LFUN_APPENDIX:
710                 if (owner->view()->available()) {
711                         owner->view()->text->toggleAppendix();
712                         owner->view()->update(1);
713                 }
714                 break;
715
716                 // --- Menus -----------------------------------------------
717         case LFUN_MENUNEW:
718                 MenuNew(false);
719                 break;
720                 
721         case LFUN_MENUNEWTMPLT:
722                 MenuNew(true);
723                 break;
724                 
725         case LFUN_MENUOPEN:
726                 MenuOpen();
727                 break;
728                 
729         case LFUN_CLOSEBUFFER:
730                 CloseBuffer();
731                 break;
732                 
733         case LFUN_MENUWRITE:
734                 owner->getMiniBuffer()->Set(_("Saving document"),
735                                             MakeDisplayPath(owner->buffer()->fileName()),
736                                             "...");
737                 MenuWrite(owner->buffer());
738                 //owner->getMiniBuffer()->
739                 //      Set(_("Document saved as"),
740                 //          MakeDisplayPath(owner->buffer()->fileName()));
741                 //} else {
742                 //owner->getMiniBuffer()->Set(_("Save failed!"));
743                 //}
744                 break;
745                 
746         case LFUN_MENUWRITEAS:
747                 MenuWriteAs(owner->buffer());
748                 break;
749                 
750         case LFUN_MENURELOAD:
751                 reloadBuffer();
752                 break;
753                 
754         case LFUN_PREVIEW:
755                 PreviewDVI(owner->buffer());
756                 break;
757                         
758         case LFUN_PREVIEWPS:
759                 PreviewPostscript(owner->buffer());
760                 break;
761                 
762         case LFUN_RUNLATEX:
763                 MenuRunLaTeX(owner->buffer());
764                 break;
765                 
766         case LFUN_BUILDPROG:
767                 MenuBuildProg(owner->buffer());
768                 break;
769                 
770         case LFUN_RUNCHKTEX:
771                 MenuRunChktex(owner->buffer());
772                 break;
773                 
774         case LFUN_RUNDVIPS:
775                 CreatePostscript(owner->buffer(), false);
776                 break;
777                 
778         case LFUN_MENUPRINT:
779                 MenuPrint(owner->buffer());
780                 break;
781                 
782         case LFUN_FAX:
783                 MenuFax(owner->buffer());
784                 break;
785                         
786         case LFUN_EXPORT:
787                 MenuExport(owner->buffer(), argument);
788                 break;
789
790         case LFUN_IMPORT:
791         {
792                 //needs argument as string
793                 string imtyp = argument;
794                 
795                 // latex
796                 if (imtyp == "latex") {
797                         doImportLaTeX(false);
798                 }
799                 // ascii
800                 else if (imtyp == "ascii") {
801                         doImportASCII(false);
802                 } else if (imtyp == "asciiparagraph") {
803                         doImportASCII(true);
804                         // noweb
805                 } else if (imtyp == "noweb") {
806                         doImportLaTeX(true);
807                 } else if (imtyp == "linuxdoc") {
808                         doImportLinuxDoc();
809                 } else {
810                         setErrorMessage(string(N_("Unknown import type: "))
811                                         + imtyp);
812                 }
813                 break;
814         }
815                 
816         case LFUN_QUIT:
817                 QuitLyX();
818                 break;
819                 
820         case LFUN_TOCVIEW:
821                 TocUpdateCB(0, 0);
822                 if (fd_form_toc->form_toc->visible) {
823                         fl_raise_form(fd_form_toc->form_toc);
824                 } else {
825                         static int ow = -1, oh;
826                         fl_show_form(fd_form_toc->form_toc,
827                                      FL_PLACE_MOUSE |
828                                      FL_FREE_SIZE, FL_FULLBORDER,
829                                      _("Table of Contents"));
830                         if (ow < 0) {
831                                 ow = fd_form_toc->form_toc->w;
832                                 oh = fd_form_toc->form_toc->h;
833                         }
834                         fl_set_form_minsize(fd_form_toc->form_toc, ow, oh);
835                 }
836                 break;
837                 
838         case LFUN_TOC_INSERT:
839         {
840                 Inset * new_inset = new InsetTOC(owner->buffer());
841                 owner->view()->insertInset(new_inset, "Standard", true);
842                 break;
843         }
844         
845         case LFUN_LOF_INSERT:
846         {
847                 Inset * new_inset = new InsetLOF(owner->buffer());
848                 owner->view()->insertInset(new_inset, "Standard", true);
849                 break;
850         }
851         
852         case LFUN_LOA_INSERT:
853         {
854                 Inset * new_inset = new InsetLOA(owner->buffer());
855                 owner->view()->insertInset(new_inset, "Standard", true);
856                 break;
857         }
858
859         case LFUN_LOT_INSERT:
860         {
861                 Inset * new_inset = new InsetLOT(owner->buffer());
862                 owner->view()->insertInset(new_inset, "Standard", true);
863                 break;
864         }
865                 
866         case LFUN_TABLE:
867                 Table();
868                 break;
869                 
870         case LFUN_FIGURE:
871                 Figure();
872                 break;
873
874         case LFUN_INSERT_GRAPHICS:
875         {
876                 Inset * new_inset = new InsetGraphics;
877                 owner->view()->insertInset(new_inset);
878                 break;
879         }
880         
881         case LFUN_AUTOSAVE:
882                 AutoSave(owner->view());
883                 break;
884                 
885         case LFUN_UNDO:
886                 owner->view()->menuUndo();
887                 break;
888                 
889         case LFUN_REDO:
890                 owner->view()->menuRedo();
891                 break;
892                 
893         case LFUN_MENUSEARCH:
894         {
895                 // Ok this is one _very_ bad solution, but I think that some
896                 // of this will be rewritten as part of GUI indep anyway.
897                 // Lgb
898                 static LyXFindReplace FR_;
899                 FR_.StartSearch(owner->view());
900         }
901         
902                 break;
903                 
904         case LFUN_PASTE:
905                 owner->view()->paste();
906                 owner->view()->setState();
907                 break;
908                 
909         case LFUN_PASTESELECTION:
910         {
911                 bool asPara = false;
912                 if (argument == "paragraph") asPara = true;
913                 MenuPasteSelection(asPara);
914                 break;
915         }
916
917         case LFUN_CUT:
918                 owner->view()->cut();
919                 break;
920                 
921         case LFUN_COPY:
922                 owner->view()->copy();
923                 break;
924                 
925         case LFUN_LAYOUT_COPY:
926                 owner->view()->copyEnvironment();
927                 break;
928                 
929         case LFUN_LAYOUT_PASTE:
930                 owner->view()->pasteEnvironment();
931                 owner->view()->setState();
932                 break;
933                 
934         case LFUN_GOTOERROR:
935                 owner->view()->gotoError();
936                 break;
937                 
938         case LFUN_REMOVEERRORS:
939                 if (owner->view()->removeAutoInsets()) {
940                         owner->view()->redraw();
941                         owner->view()->fitCursor();
942                         owner->view()->updateScrollbar();
943                 }
944                 break;
945                 
946         case LFUN_GOTONOTE:
947                 owner->view()->gotoNote();
948                 break;
949                 
950         case LFUN_OPENSTUFF:
951                 owner->view()->openStuff();
952                 break;
953                 
954         case LFUN_HYPHENATION:
955                 owner->view()->hyphenationPoint();
956                 break;
957                 
958         case LFUN_LDOTS:
959                 owner->view()->ldots();
960                 break;
961                 
962         case LFUN_END_OF_SENTENCE:
963                 owner->view()->endOfSentenceDot();
964                 break;
965
966         case LFUN_MENU_SEPARATOR:
967                 owner->view()->menuSeparator();
968                 break;
969                 
970         case LFUN_HFILL:
971                 owner->view()->hfill();
972                 break;
973                 
974         case LFUN_DEPTH:
975                 changeDepth(owner->view(), 0);
976                 break;
977                 
978         case LFUN_DEPTH_MIN:
979                 changeDepth(owner->view(), -1);
980                 break;
981                 
982         case LFUN_DEPTH_PLUS:
983                 changeDepth(owner->view(), 1);
984                 break;
985                 
986         case LFUN_FREE:
987                 Free(owner->view());
988                 break;
989                 
990         case LFUN_TEX:
991                 Tex(owner->view());
992                 owner->view()->setState();
993                 owner->getMiniBuffer()->Set(CurrentState(owner->view()));
994                 break;
995                 
996         case LFUN_MELT:
997                 Melt(owner->view());
998                 break;
999                 
1000         case LFUN_RECONFIGURE:
1001                 Reconfigure(owner->view());
1002                 break;
1003
1004         case LFUN_FOOTMELT:
1005                 if (owner->view()->available()
1006                     && !owner->view()->text->selection
1007                     && owner->view()->text->cursor.par->footnoteflag
1008                     != LyXParagraph::NO_FOOTNOTE)
1009                         { // only melt footnotes with FOOTMELT, not margins etc
1010                                 if(owner->view()->text->cursor.par->footnotekind == LyXParagraph::FOOTNOTE)
1011                                         Melt(owner->view());
1012                         }
1013                 else
1014                         Foot(owner->view()); 
1015                 owner->view()->setState();
1016                 break;
1017
1018         case LFUN_MARGINMELT:
1019                 if (owner->view()->available()
1020                     && !owner->view()->text->selection
1021                     && owner->view()->text->cursor.par->footnoteflag
1022                     != LyXParagraph::NO_FOOTNOTE) {
1023                         // only melt margins
1024                         if(owner->view()->text->cursor.par->footnotekind == LyXParagraph::MARGIN)
1025                                 Melt(owner->view());
1026                 } else
1027                         Margin(owner->view()); 
1028                 owner->view()->setState();
1029                 break;
1030                 
1031                 // --- version control -------------------------------
1032         case LFUN_VC_REGISTER:
1033         {
1034                 if (!owner->buffer()->lyxvc.inUse())
1035                         owner->buffer()->lyxvc.registrer();
1036         }
1037         break;
1038                 
1039         case LFUN_VC_CHECKIN:
1040         {
1041                 if (owner->buffer()->lyxvc.inUse()
1042                     && !owner->buffer()->isReadonly())
1043                         owner->buffer()->lyxvc.checkIn();
1044         }
1045         break;
1046                 
1047         case LFUN_VC_CHECKOUT:
1048         {
1049                 if (owner->buffer()->lyxvc.inUse()
1050                     && owner->buffer()->isReadonly())
1051                         owner->buffer()->lyxvc.checkOut();
1052         }
1053         break;
1054         
1055         case LFUN_VC_REVERT:
1056         {
1057                 owner->buffer()->lyxvc.revert();
1058         }
1059         break;
1060                 
1061         case LFUN_VC_UNDO:
1062         {
1063                 owner->buffer()->lyxvc.undoLast();
1064         }
1065         break;
1066                 
1067         case LFUN_VC_HISTORY:
1068         {
1069                 owner->buffer()->lyxvc.showLog();
1070                 break;
1071         }
1072         
1073         // --- buffers ----------------------------------------
1074
1075         case LFUN_FILE_INSERT:
1076         {
1077                 MenuInsertLyXFile(argument);
1078         }
1079         break;
1080         
1081         case LFUN_FILE_INSERT_ASCII:
1082         {
1083                 bool asPara = (argument == "paragraph");
1084                 InsertAsciiFile(owner->view(), string(), asPara);
1085         }
1086         break;
1087         
1088         case LFUN_FILE_NEW:
1089         {
1090                 // servercmd: argument must be <file>:<template>
1091                 Buffer * tmpbuf = NewLyxFile(argument);
1092                 if (tmpbuf)
1093                         owner->view()->buffer(tmpbuf);
1094         }
1095         break;
1096                         
1097         case LFUN_FILE_OPEN:
1098                 owner->view()->buffer(bufferlist.loadLyXFile(argument));
1099                 break;
1100
1101         case LFUN_LATEX_LOG:
1102                 ShowLatexLog();
1103                 break;
1104                 
1105         case LFUN_LAYOUTNO:
1106         {
1107                 lyxerr.debug() << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1108                 int sel = strToInt(argument);
1109                 lyxerr.debug() << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1110                 
1111                 // Should this give a setMessage instead?
1112                 if (sel == 0) 
1113                         return string(); // illegal argument
1114
1115                 --sel; // sel 1..., but layout 0...
1116
1117                 // Pretend we got the name instead.
1118                 Dispatch(int(LFUN_LAYOUT), 
1119                          textclasslist.NameOfLayout(owner->view()->
1120                                                     text->bparams->
1121                                                     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()->text->bparams->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                 lyxerr << "all spacing arguments: " << argument << endl;
1960                 istringstream istr(argument);
1961                 string tmp;
1962                 istr >> tmp;
1963                 lyxerr << "spacing = " << tmp << endl;
1964                 Spacing::Space new_spacing = cur_spacing;
1965                 float new_value = cur_value;
1966                 if (tmp == "single") {
1967                         new_spacing = Spacing::Single;
1968                 } else if (tmp == "onehalf") {
1969                         new_spacing = Spacing::Onehalf;
1970                 } else if (tmp == "double") {
1971                         new_spacing = Spacing::Double;
1972                 } else if (tmp == "other") {
1973                         new_spacing = Spacing::Other;
1974                         float tmpval = 0.0;
1975                         istr >> tmpval;
1976                         lyxerr << "new_value = " << tmpval << endl;
1977                         if (tmpval != 0.0)
1978                                 new_value = tmpval;
1979                 } else if (tmp == "default") {
1980                         new_spacing = Spacing::Default;
1981                 } else {
1982                         lyxerr << _("Unknown spacing argument: ")
1983                                << argument << endl;
1984                 }
1985                 if (cur_spacing != new_spacing || cur_value != new_value) {
1986                         par->spacing.set(new_spacing, new_value);
1987                         owner->view()->text->RedoParagraph();
1988                         owner->view()->update(-1);
1989                 }
1990         }
1991         break;
1992         
1993         case LFUN_QUOTE:
1994                 owner->view()->beforeChange();
1995                 owner->view()->text->InsertChar('\"');  // This " matches the single quote in the code
1996                 owner->view()->smallUpdate(1);
1997                 SetUpdateTimer();
1998                 moveCursorUpdate(false);
1999                 break;
2000
2001         case LFUN_HTMLURL:
2002         case LFUN_URL:
2003         {
2004                 InsetCommand * new_inset;
2005                 if (action == LFUN_HTMLURL)
2006                         new_inset = new InsetUrl("htmlurl", "", "");
2007                 else
2008                         new_inset = new InsetUrl("url", "", "");
2009                 owner->view()->insertInset(new_inset);
2010                 new_inset->Edit(owner->view(), 0, 0, 0);
2011         }
2012         break;
2013         
2014         case LFUN_INSET_TEXT:
2015         {
2016                 InsetText * new_inset = new InsetText(owner->buffer());
2017                 owner->view()->insertInset(new_inset);
2018                 new_inset->Edit(owner->view(), 0, 0, 0);
2019         }
2020         break;
2021 #if 0
2022         case LFUN_INSET_NUMBER:
2023         {
2024                 InsetNumber * new_inset = new InsetNumber(owner->buffer());
2025                 owner->view()->insertInset(new_inset);
2026                 new_inset->Edit(owner->view(), 0, 0, 0);
2027         }
2028         break;
2029 #endif
2030         case LFUN_INSET_ERT:
2031         {
2032                 InsetERT * new_inset = new InsetERT(owner->buffer());
2033                 owner->view()->insertInset(new_inset);
2034                 new_inset->Edit(owner->view(), 0, 0, 0);
2035         }
2036         break;
2037         
2038         case LFUN_INSET_FOOTNOTE:
2039         {
2040                 InsetFoot * new_inset = new InsetFoot(owner->buffer());
2041                 owner->view()->insertInset(new_inset);
2042                 new_inset->Edit(owner->view(), 0, 0, 0);
2043         }
2044         break;
2045
2046         // --- lyxserver commands ----------------------------
2047
2048         case LFUN_CHARATCURSOR:
2049         {
2050                 LyXParagraph::size_type pos = 
2051                         owner->view()->text->cursor.pos;
2052                 if(pos < owner->view()->text->cursor.par->size())
2053                         //dispatch_buffer = owner->view()->text->
2054                         //      cursor.par->text[pos];
2055                         dispatch_buffer =
2056                                 owner->view()->text->
2057                                 cursor.par->GetChar(pos);
2058                 else
2059                         dispatch_buffer = "EOF";
2060         }
2061         break;
2062         
2063         case LFUN_GETXY:
2064                 dispatch_buffer = 
2065                         tostr(owner->view()->text->cursor.x) + ' '
2066                         + tostr(owner->view()->text->cursor.y);
2067                 break;
2068                 
2069         case LFUN_SETXY:
2070         {
2071                 int  x;
2072                 long y;
2073                 sscanf(argument.c_str(), " %d %ld", &x, &y);
2074                 owner->view()->text->SetCursorFromCoordinates(x, y);
2075         }
2076         break;
2077         
2078         case LFUN_GETLAYOUT:
2079                 dispatch_buffer =  
2080                         tostr(owner->view()->text->cursor.par->layout);
2081                 break;
2082                         
2083         case LFUN_GETFONT:
2084         {
2085                 LyXFont & font = owner->view()->text->current_font;
2086                 if(font.shape() == LyXFont::ITALIC_SHAPE)
2087                         dispatch_buffer = 'E';
2088                 else if(font.shape() == LyXFont::SMALLCAPS_SHAPE)
2089                         dispatch_buffer = 'N';
2090                 else
2091                         dispatch_buffer = '0';
2092
2093         }
2094         break;
2095
2096         case LFUN_GETLATEX:
2097         {
2098                 LyXFont & font = owner->view()->text->current_font;
2099                 if(font.latex() == LyXFont::ON)
2100                         dispatch_buffer = 'L';
2101                 else
2102                         dispatch_buffer = '0';
2103         }
2104         break;
2105
2106         case LFUN_GETNAME:
2107                 setMessage(owner->buffer()->fileName());
2108                 lyxerr.debug() << "FNAME["
2109                                << owner->buffer()->fileName()
2110                                << "] " << endl;
2111                 break;
2112                 
2113         case LFUN_NOTIFY:
2114         {
2115                 string buf;
2116                 keyseq.print(buf);
2117                 dispatch_buffer = buf;
2118                 lyxserver->notifyClient(dispatch_buffer);
2119         }
2120         break;
2121
2122         case LFUN_GOTOFILEROW:
2123         {
2124                 char file_name[100];
2125                 int  row;
2126                 sscanf(argument.c_str(), " %s %d", file_name, &row);
2127
2128                 // Must replace extension of the file to be .lyx and get full path
2129                 string s = ChangeExtension(string(file_name), ".lyx", false);
2130
2131                 // Either change buffer or load the file
2132                 if (bufferlist.exists(s))
2133                         owner->view()->buffer(bufferlist.getBuffer(s));
2134                 else
2135                         owner->view()->buffer(bufferlist.loadLyXFile(s));
2136
2137                 // Set the cursor  
2138                 owner->view()->setCursorFromRow(row);
2139
2140                 // Recenter screen
2141                 owner->view()->center();
2142         }
2143         break;
2144
2145         case LFUN_APROPOS:
2146         case LFUN_GETTIP:
2147         {
2148                 int qa = lyxaction.LookupFunc(argument.c_str());
2149                 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
2150         }
2151         break;
2152
2153         // --- accented characters ---------------------------
2154                 
2155         case LFUN_UMLAUT:
2156         case LFUN_CIRCUMFLEX:
2157         case LFUN_GRAVE:
2158         case LFUN_ACUTE:
2159         case LFUN_TILDE:
2160         case LFUN_CEDILLA:
2161         case LFUN_MACRON:
2162         case LFUN_DOT:
2163         case LFUN_UNDERDOT:
2164         case LFUN_UNDERBAR:
2165         case LFUN_CARON:
2166         case LFUN_SPECIAL_CARON:
2167         case LFUN_BREVE:
2168         case LFUN_TIE:
2169         case LFUN_HUNG_UMLAUT:
2170         case LFUN_CIRCLE:
2171         case LFUN_OGONEK:
2172         {
2173                 char c;
2174                 
2175                 if (keyseq.length == -1 && keyseq.getiso() != 0) 
2176                         c = keyseq.getiso();
2177                 else
2178                         c = 0;
2179                 
2180                 owner->getIntl()->getTrans()->
2181                         deadkey(c, get_accent(action).accent, 
2182                                 owner->view()->text);
2183                 
2184                 // Need to reset, in case the minibuffer calls these
2185                 // actions
2186                 keyseq.reset();
2187                 keyseq.length = 0;
2188                 
2189                 // copied verbatim from do_accent_char
2190                 owner->view()->smallUpdate(1);
2191                 SetUpdateTimer();
2192                 owner->view()->text->sel_cursor = 
2193                         owner->view()->text->cursor;
2194         }   
2195         break;
2196         
2197         // --- toolbar ----------------------------------
2198         case LFUN_PUSH_TOOLBAR:
2199         {
2200                 int nth = strToInt(argument);
2201                 if (lyxerr.debugging(Debug::TOOLBAR)) {
2202                         lyxerr << "LFUN_PUSH_TOOLBAR: argument = `"
2203                                << argument << "'\n"
2204                                << "LFUN_PUSH_TOOLBAR: nth = `"
2205                                << nth << "'" << endl;
2206                 }
2207                 
2208                 if (nth <= 0) {
2209                         LyXBell();
2210                         setErrorMessage(N_("Push-toolbar needs argument > 0"));
2211                 } else {
2212                         owner->getToolbar()->push(nth);
2213                 }
2214         }
2215         break;
2216         
2217         case LFUN_ADD_TO_TOOLBAR:
2218         {
2219                 if (lyxerr.debugging(Debug::TOOLBAR)) {
2220                         lyxerr << "LFUN_ADD_TO_TOOLBAR:"
2221                                 "argument = `" << argument << '\'' << endl;
2222                 }
2223                 string tmp(argument);
2224                 //lyxerr <<string("Argument: ") + argument);
2225                 //lyxerr <<string("Tmp     : ") + tmp);
2226                 if (tmp.empty()) {
2227                         LyXBell();
2228                         setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
2229                 } else {
2230                         owner->getToolbar()->add(argument, false);
2231                         owner->getToolbar()->set();
2232                 }
2233         }
2234         break;
2235         
2236         // --- insert characters ----------------------------------------
2237
2238         // ---  Mathed stuff. If we are here, there is no locked inset yet.
2239         
2240         // Greek mode     
2241         case LFUN_GREEK:
2242         {
2243                 if (!greek_kb_flag) {
2244                         greek_kb_flag = 1;
2245                         setMessage(N_("Math greek mode on"));
2246                 } else
2247                         greek_kb_flag = 0;
2248         }  
2249         break;
2250       
2251         // Greek keyboard      
2252         case LFUN_GREEK_TOGGLE:
2253         {
2254                 greek_kb_flag = greek_kb_flag ? 0 : 2;
2255                 if (greek_kb_flag) {
2256                         setMessage(N_("Math greek keyboard on"));
2257                 } else {
2258                         setMessage(N_("Math greek keyboard off"));
2259                 }
2260         }
2261         break;
2262         
2263         case LFUN_MATH_DELIM:     
2264         case LFUN_INSERT_MATRIX:
2265         {          
2266                 if (owner->view()->available()) { 
2267                         owner->view()->
2268                                 open_new_inset(new InsetFormula(false));
2269                         owner->view()
2270                                 ->the_locking_inset
2271                                 ->LocalDispatch(owner->view(),
2272                                                 action,
2273                                                 argument);
2274                 }
2275         }          
2276         break;
2277                
2278         case LFUN_INSERT_MATH:
2279         {
2280                 math_insert_symbol(argument.c_str());
2281         }
2282         break;
2283         
2284         case LFUN_MATH_DISPLAY:
2285         {
2286                 if (owner->view()->available())
2287                         owner->view()->open_new_inset(new InsetFormula(true));
2288                 break;
2289         }
2290                     
2291         case LFUN_MATH_MACRO:
2292         {
2293                 if (owner->view()->available()) {
2294                         string s(argument);
2295                         if (s.empty())
2296                                 setErrorMessage(N_("Missing argument"));
2297                         else {
2298                                 string s1 = token(s, ' ', 1);
2299                                 int na = s1.empty() ? 0: atoi(s1.c_str());
2300                                 owner->view()->
2301                                         open_new_inset(new InsetFormulaMacro(token(s, ' ', 0), na));
2302                         }
2303                 }
2304         }
2305         break;
2306
2307         case LFUN_MATH_MODE:   // Open or create a math inset
2308         {
2309                 
2310                 if (owner->view()->available())
2311                         owner->view()->open_new_inset(new InsetFormula);
2312                 setMessage(N_("Math editor mode"));
2313         }
2314         break;
2315           
2316         case LFUN_MATH_NUMBER:
2317         case LFUN_MATH_LIMITS:
2318         {
2319                 setErrorMessage(N_("This is only allowed in math mode!"));
2320         }
2321         break;
2322         
2323         case LFUN_INSERT_CITATION:
2324         {   
2325                 InsetCitation * new_inset = new InsetCitation();
2326                 // ale970405
2327                 // The note, if any, must be after the key, delimited
2328                 // by a | so both key and remark can have spaces.
2329                 if (!argument.empty()) {
2330                         string lsarg(argument);
2331                         if (contains(lsarg, "|")) {
2332                                 new_inset->setContents(token(lsarg, '|', 0));
2333                                 new_inset->setOptions(token(lsarg, '|', 1));
2334                         } else
2335                                 new_inset->setContents(lsarg);
2336                         owner->view()->insertInset(new_inset);
2337                 } else {
2338                         owner->view()->insertInset(new_inset);
2339                         new_inset->Edit(owner->view(), 0, 0, 0);
2340                 }
2341         }
2342         break;
2343                     
2344         case LFUN_INSERT_BIBTEX:
2345         {   
2346                 // ale970405+lasgoutt970425
2347                 // The argument can be up to two tokens separated 
2348                 // by a space. The first one is the bibstyle.
2349                 string lsarg(argument);
2350                 string bibstyle = token(lsarg, ' ', 1);
2351                 if (bibstyle.empty())
2352                         bibstyle = "plain";
2353                 InsetBibtex * new_inset 
2354                         = new InsetBibtex(token(lsarg, ' ', 0),
2355                                           bibstyle,
2356                                           owner->buffer());
2357                 
2358                 owner->view()->insertInset(new_inset);
2359                 if (lsarg.empty()) {
2360                         new_inset->Edit(owner->view(), 0, 0, 0);
2361                 }
2362         }
2363         break;
2364                 
2365         // BibTeX data bases
2366         case LFUN_BIBDB_ADD:
2367         {
2368                 InsetBibtex * inset = 
2369                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2370                 if (inset) {
2371                         inset->addDatabase(argument);
2372                 }
2373         }
2374         break;
2375                     
2376         case LFUN_BIBDB_DEL:
2377         {
2378                 InsetBibtex * inset = 
2379                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2380                 if (inset) {
2381                         inset->delDatabase(argument);
2382                 }
2383         }
2384         break;
2385         
2386         case LFUN_BIBTEX_STYLE:
2387         {
2388                 InsetBibtex * inset = 
2389                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2390                 if (inset) {
2391                         inset->setOptions(argument);
2392                 }
2393         }
2394         break;
2395                 
2396         case LFUN_INDEX_INSERT:
2397         case LFUN_INDEX_INSERT_LAST:
2398         {
2399                 // Can't do that at the beginning of a paragraph.
2400                 if (owner->view()->text->cursor.pos - 1 < 0)
2401                         break;
2402
2403                 InsetIndex * new_inset = new InsetIndex();
2404                 if (!argument.empty()) {
2405                         string lsarg(argument);
2406                         new_inset->setContents(lsarg);
2407                         owner->view()->insertInset(new_inset);
2408                 } else {
2409                         //reh 98/09/21
2410                         //get the current word for an argument
2411                         LyXParagraph::size_type lastpos = 
2412                                 owner->view()->text->cursor.pos - 1;
2413                         // Get the current word. note that this must be done
2414                         // before inserting the inset, or the inset will
2415                         // break the word
2416                         string curstring(owner->view()
2417                                          ->text->cursor.par->GetWord(lastpos));
2418
2419                         //make the new inset and write the current word into it
2420                         InsetIndex * new_inset = new InsetIndex();
2421
2422                         new_inset->setContents(curstring);
2423
2424                         //don't edit it if the call was to INSERT_LAST
2425                         if(action != LFUN_INDEX_INSERT_LAST) {
2426                                 new_inset->Edit(owner->view(), 0, 0, 0);
2427                         } else {
2428                                 //it looks blank on the screen unless
2429                                 //we do  something.  put it here.
2430
2431                                 // move the cursor to the returned value of lastpos
2432                                 // but only for the auto-insert
2433                                 owner->view()->text->cursor.pos = lastpos;
2434                         }
2435
2436                         //put the new inset into the buffer.
2437                         // there should be some way of knowing the user
2438                         //cancelled & avoiding this, but i don't know how
2439                         owner->view()->insertInset(new_inset);
2440                 }
2441         }
2442         break;
2443
2444         case LFUN_INDEX_PRINT:
2445         {
2446                 Inset * new_inset = new InsetPrintIndex(owner->buffer());
2447                 owner->view()->insertInset(new_inset, "Standard", true);
2448         }
2449         break;
2450
2451         case LFUN_PARENTINSERT:
2452         {
2453                 lyxerr << "arg " << argument << endl;
2454                 Inset * new_inset = new InsetParent(argument, owner->buffer());
2455                 owner->view()->insertInset(new_inset, "Standard", true);
2456         }
2457         break;
2458
2459         case LFUN_CHILDINSERT:
2460         {
2461                 Inset * new_inset = new InsetInclude(argument,
2462                                                      owner->buffer());
2463                 owner->view()->insertInset(new_inset, "Standard", true);
2464                 new_inset->Edit(owner->view(), 0, 0, 0);
2465         }
2466         break;
2467
2468         case LFUN_CHILDOPEN:
2469         {
2470                 string filename =
2471                         MakeAbsPath(argument, 
2472                                     OnlyPath(owner->buffer()->fileName()));
2473                 setMessage(N_("Opening child document ") +
2474                            MakeDisplayPath(filename) + "...");
2475                 owner->view()->savePosition();
2476                 if (bufferlist.exists(filename))
2477                         owner->view()->buffer(bufferlist.getBuffer(filename));
2478                 else
2479                         owner->view()->buffer(bufferlist.loadLyXFile(filename));
2480         }
2481         break;
2482
2483         case LFUN_INSERT_NOTE:
2484                 owner->view()->insertNote();
2485                 break;
2486                 
2487         case LFUN_INSERTFOOTNOTE: 
2488         {
2489                 LyXParagraph::footnote_kind kind;
2490                 if (argument == "footnote")
2491                         { kind = LyXParagraph::FOOTNOTE; }
2492                 else if (argument == "margin")
2493                         { kind = LyXParagraph::MARGIN; }
2494                 else if (argument == "figure")
2495                         { kind = LyXParagraph::FIG; }
2496                 else if (argument == "table")
2497                         { kind = LyXParagraph::TAB; }
2498                 else if (argument == "wide-fig")
2499                         { kind = LyXParagraph::WIDE_FIG; }
2500                 else if (argument == "wide-tab")
2501                         { kind = LyXParagraph::WIDE_TAB; }
2502                 else if (argument == "algorithm")
2503                         { kind = LyXParagraph::ALGORITHM; }
2504                 else {
2505                         setErrorMessage(N_("Unknown kind of footnote"));
2506                         break;
2507                 }
2508                 owner->view()->text->InsertFootnoteEnvironment(kind);
2509                 owner->view()->update(1);
2510                 owner->view()->setState();
2511         }
2512         break;
2513         
2514         case LFUN_BUFFERBULLETSSELECT:
2515                 bulletForm();
2516                 break;
2517                 
2518         case LFUN_TOGGLECURSORFOLLOW:
2519                 cursor_follows_scrollbar = !cursor_follows_scrollbar;
2520                 break;
2521                 
2522         case LFUN_KMAP_OFF:             // keymap off
2523                 owner->getIntl()->KeyMapOn(false);
2524                 break;
2525                 
2526         case LFUN_KMAP_PRIM:    // primary keymap
2527                 owner->getIntl()->KeyMapPrim();
2528                 break;
2529                 
2530         case LFUN_KMAP_SEC:             // secondary keymap
2531                 owner->getIntl()->KeyMapSec();
2532                 break;
2533                 
2534         case LFUN_KMAP_TOGGLE:  // toggle keymap
2535                 owner->getIntl()->ToggleKeyMap();
2536                 break;
2537
2538         case LFUN_SELFINSERT:
2539         {
2540                 for (string::size_type i = 0; i < argument.length(); ++i) {
2541                         owner->view()->text->InsertChar(argument[i]);
2542                         // This needs to be in the loop, or else we
2543                         // won't break lines correctly. (Asger)
2544                         owner->view()->smallUpdate(1);
2545                 }
2546                 SetUpdateTimer();
2547                 owner->view()->text->sel_cursor = 
2548                         owner->view()->text->cursor;
2549                 moveCursorUpdate(false);
2550         }
2551         break;
2552
2553         case LFUN_SEQUENCE: 
2554         {
2555                 // argument contains ';'-terminated commands
2556                 while (argument.find(';') != string::npos) {
2557                         string first;
2558                         argument = split(argument, first, ';');
2559                         Dispatch(first);
2560                 }
2561         }
2562         break;
2563
2564         case LFUN_DATE_INSERT:  // jdblair: date-insert cmd
2565         {
2566                 char datetmp[32];
2567                 int datetmp_len;
2568                 time_t now_time_t;
2569                 struct tm *now_tm;
2570                 static string arg;
2571                 
2572                 now_time_t = time(NULL);
2573                 now_tm = localtime(&now_time_t);
2574                 (void)setlocale(LC_TIME, "");
2575                 if (!argument.empty())
2576                         arg = argument;
2577                 else if (arg.empty())
2578                         arg = lyxrc.date_insert_format;
2579                 datetmp_len = (int) strftime(datetmp, 32, arg.c_str(), now_tm);
2580                 for (int i = 0; i < datetmp_len; i++) {
2581                         owner->view()->text->InsertChar(datetmp[i]);
2582                         owner->view()->smallUpdate(1);
2583                 }
2584                 SetUpdateTimer();
2585                 owner->view()->text->sel_cursor = owner->view()->text->cursor;
2586                 moveCursorUpdate(false);
2587         }
2588         break;
2589
2590         case LFUN_SAVEPREFERENCES:
2591         {
2592                 Path p(user_lyxdir);
2593                 lyxrc.write("preferences");
2594         }
2595         break;
2596         
2597         case LFUN_UNKNOWN_ACTION:
2598         {
2599                 if(!owner->buffer()) {
2600                         LyXBell();
2601                         setErrorMessage(N_("No document open"));
2602                         break;
2603                 }
2604
2605                 if (owner->buffer()->isReadonly()) {
2606                         LyXBell();
2607                         setErrorMessage(N_("Document is read only"));
2608                         break;
2609                 }
2610                          
2611                 if (!argument.empty()) {
2612                         
2613                         /* Automatically delete the currently selected
2614                          * text and replace it with what is being
2615                          * typed in now. Depends on lyxrc settings
2616                          * "auto_region_delete", which defaults to
2617                          * true (on). */
2618                 
2619                         if ( lyxrc.auto_region_delete ) {
2620                                 if (owner->view()->text->selection){
2621                                         owner->view()->text->CutSelection(false);
2622                                         owner->view()->update(-1);
2623                                 }
2624                         }
2625                         
2626                         owner->view()->beforeChange();
2627
2628 #if 0
2629                         if (isdigit(argument[0]) &&
2630                             (lyxrc.number_inset == "true" ||
2631                              (lyxrc.number_inset == "rtl" &&
2632                               owner->view()->text->real_current_font.isVisibleRightToLeft() 
2633                               ))) {
2634                                 UpdatableInset * tmpinset = new InsetNumber(owner->buffer());
2635                                 owner->view()->open_new_inset(tmpinset);
2636                                 tmpinset->LocalDispatch(owner->view(), action,
2637                                                         argument);
2638                                 return string();
2639                         }
2640 #endif
2641                         
2642                         for (string::size_type i = 0;
2643                              i < argument.length(); ++i) {
2644                                 if (greek_kb_flag) {
2645                                         if (!math_insert_greek(argument[i]))
2646                                                 owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2647                                 } else
2648                                         owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2649                         }
2650                         
2651                         owner->view()->smallUpdate(1);
2652                         SetUpdateTimer();
2653
2654                         owner->view()->text->sel_cursor = 
2655                                 owner->view()->text->cursor;
2656                         moveCursorUpdate(false);
2657                         return string();
2658                 } else {
2659                         // why is an "Unknown action" with empty
2660                         // argument even dispatched in the first
2661                         // place? I`ll probably change that. (Lgb)
2662                         LyXBell();
2663                         setErrorMessage(N_("Unknown action"));
2664                 }
2665                 break;
2666         default:
2667                 lyxerr << "A truly unknown func!" << endl;
2668                 break;
2669         }
2670         } // end of switch
2671   exit_with_message:
2672
2673         string res = getMessage();
2674
2675         if (res.empty()) {
2676                 if (!commandshortcut.empty()) {
2677                         string newbuf = owner->getMiniBuffer()->GetText();
2678                         if (newbuf != commandshortcut) {
2679                                 owner->getMiniBuffer()->Set(newbuf
2680                                                             + " " +
2681                                                             commandshortcut);
2682                         }
2683                 }
2684         } else {
2685                 owner->getMiniBuffer()->Set(string(_(res.c_str()))
2686                                             + " " + commandshortcut);
2687         }
2688
2689         return res;
2690 }
2691
2692
2693 void LyXFunc::setupLocalKeymap()
2694 {
2695         keyseq.stdmap = keyseq.curmap = toplevel_keymap;
2696         cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap;
2697 }
2698
2699
2700 void LyXFunc::MenuNew(bool fromTemplate)
2701 {
2702         string fname, initpath = lyxrc.document_path;
2703         LyXFileDlg fileDlg;
2704
2705         if (owner->view()->available()) {
2706                 string trypath = owner->buffer()->filepath;
2707                 // If directory is writeable, use this as default.
2708                 if (IsDirWriteable(trypath) == 1)
2709                         initpath = trypath;
2710         }
2711
2712         ProhibitInput(owner->view());
2713         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2714         fileDlg.SetButton(1, _("Templates"), lyxrc.template_path);
2715         fname = fileDlg.Select(_("Enter Filename for new document"), 
2716                                initpath, "*.lyx", _("newfile"));
2717         AllowInput(owner->view());
2718         
2719         if (fname.empty()) {
2720                 owner->getMiniBuffer()->Set(_("Canceled."));
2721                 lyxerr.debug() << "New Document Cancelled." << endl;
2722                 return;
2723         }
2724         
2725         // get absolute path of file and make sure the filename ends
2726         // with .lyx
2727         string s = MakeAbsPath(fname);
2728         if (!IsLyXFilename(s))
2729                 s += ".lyx";
2730
2731         // Check if the document already is open
2732         if (bufferlist.exists(s)){
2733                 switch(AskConfirmation(_("Document is already open:"), 
2734                                        MakeDisplayPath(s, 50),
2735                                        _("Do you want to close that document now?\n"
2736                                          "('No' will just switch to the open version)")))
2737                         {
2738                         case 1: // Yes: close the document
2739                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
2740                                 // If close is canceled, we cancel here too.
2741                                         return;
2742                                 break;
2743                         case 2: // No: switch to the open document
2744                                 owner->view()->buffer(bufferlist.getBuffer(s));
2745                                 return;
2746                         case 3: // Cancel: Do nothing
2747                                 owner->getMiniBuffer()->Set(_("Canceled."));
2748                                 return;
2749                         }
2750         }
2751         
2752         // Check whether the file already exists
2753         if (IsLyXFilename(s)) {
2754                 FileInfo fi(s);
2755                 if (fi.readable() &&
2756                     AskQuestion(_("File already exists:"), 
2757                                 MakeDisplayPath(s, 50),
2758                                 _("Do you want to open the document?"))) {
2759                         // loads document
2760                         owner->getMiniBuffer()->Set(_("Opening document"), 
2761                                                     MakeDisplayPath(s), "...");
2762                         XFlush(fl_display);
2763                         owner->view()->buffer(
2764                                 bufferlist.loadLyXFile(s));
2765                         owner->getMiniBuffer()->Set(_("Document"),
2766                                                     MakeDisplayPath(s),
2767                                                     _("opened."));
2768                         return;
2769                 }
2770         }
2771
2772         // The template stuff
2773         string templname;
2774         if (fromTemplate) {
2775                 ProhibitInput(owner->view());
2776                 fname = fileDlg.Select(_("Choose template"),
2777                                        lyxrc.template_path,
2778                                        "*.lyx");
2779                 templname = fname;
2780                 AllowInput(owner->view());
2781         }
2782   
2783         // find a free buffer
2784         lyxerr.debug() << "Find a free buffer." << endl;
2785         owner->view()->buffer(bufferlist.newFile(s, templname));
2786 }
2787
2788
2789 void LyXFunc::MenuOpen()
2790 {
2791         string initpath = lyxrc.document_path;
2792         LyXFileDlg fileDlg;
2793   
2794         if (owner->view()->available()) {
2795                 string trypath = owner->buffer()->filepath;
2796                 // If directory is writeable, use this as default.
2797                 if (IsDirWriteable(trypath) == 1)
2798                         initpath = trypath;
2799         }
2800
2801         // launches dialog
2802         ProhibitInput(owner->view());
2803         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2804         fileDlg.SetButton(1, _("Examples"), 
2805                           AddPath(system_lyxdir, "examples"));
2806         string filename = fileDlg.Select(_("Select Document to Open"),
2807                                          initpath, "*.lyx");
2808         AllowInput(owner->view());
2809  
2810         // check selected filename
2811         if (filename.empty()) {
2812                 owner->getMiniBuffer()->Set(_("Canceled."));
2813                 return;
2814         }
2815
2816         // get absolute path of file and make sure the filename ends
2817         // with .lyx
2818         filename = MakeAbsPath(filename);
2819         if (!IsLyXFilename(filename))
2820                 filename += ".lyx";
2821
2822         // loads document
2823         owner->getMiniBuffer()->Set(_("Opening document"),
2824                                     MakeDisplayPath(filename), "...");
2825         Buffer * openbuf = bufferlist.loadLyXFile(filename);
2826         if (openbuf) {
2827                 owner->view()->buffer(openbuf);
2828                 owner->getMiniBuffer()->Set(_("Document"),
2829                                             MakeDisplayPath(filename),
2830                                             _("opened."));
2831         } else {
2832                 owner->getMiniBuffer()->Set(_("Could not open document"),
2833                                             MakeDisplayPath(filename));
2834         }
2835 }
2836
2837
2838 void LyXFunc::doImportASCII(bool linorpar)
2839 {
2840         string initpath = lyxrc.document_path;
2841         LyXFileDlg fileDlg;
2842   
2843         if (owner->view()->available()) {
2844                 string trypath = owner->buffer()->filepath;
2845                 // If directory is writeable, use this as default.
2846                 if (IsDirWriteable(trypath) == 1)
2847                         initpath = trypath;
2848         }
2849
2850         // launches dialog
2851         ProhibitInput(owner->view());
2852         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2853         fileDlg.SetButton(1, _("Examples"), 
2854                           AddPath(system_lyxdir, "examples"));
2855         string filename = fileDlg.Select(_("Select ASCII file to Import"),
2856                                          initpath, "*.txt");
2857         AllowInput(owner->view());
2858  
2859         // check selected filename
2860         if (filename.empty()) {
2861                 owner->getMiniBuffer()->Set(_("Canceled."));
2862                 return;
2863         }
2864
2865         // get absolute path of file
2866         filename = MakeAbsPath(filename);
2867
2868         string s = ChangeExtension(filename, ".lyx", false);
2869
2870         // Check if the document already is open
2871         if (bufferlist.exists(s)) {
2872                 switch(AskConfirmation(_("Document is already open:"), 
2873                                        MakeDisplayPath(s, 50),
2874                                        _("Do you want to close that document now?\n"
2875                                          "('No' will just switch to the open version)")))
2876                         {
2877                         case 1: // Yes: close the document
2878                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
2879                                 // If close is canceled, we cancel here too.
2880                                         return;
2881                                 break;
2882                         case 2: // No: switch to the open document
2883                                 owner->view()->buffer(bufferlist.getBuffer(s));
2884                                 return;
2885                         case 3: // Cancel: Do nothing
2886                                 owner->getMiniBuffer()->Set(_("Canceled."));
2887                                 return;
2888                         }
2889         }
2890
2891         // Check if a LyX document by the same root exists in filesystem
2892         FileInfo f(s, true);
2893         if (f.exist() && !AskQuestion(_("A document by the name"), 
2894                                       MakeDisplayPath(s),
2895                                       _("already exists. Overwrite?"))) {
2896                 owner->getMiniBuffer()->Set(_("Canceled."));
2897                 return;
2898         }
2899
2900         owner->view()->buffer(bufferlist.newFile(s, string()));
2901         owner->getMiniBuffer()->Set(_("Importing ASCII file"),
2902                                     MakeDisplayPath(filename), "...");
2903         // Insert ASCII file
2904         InsertAsciiFile(owner->view(), filename, linorpar);
2905         owner->getMiniBuffer()->Set(_("ASCII file "),
2906                                     MakeDisplayPath(filename),
2907                                     _("imported."));
2908 }
2909
2910
2911 void LyXFunc::doImportLaTeX(bool isnoweb)
2912 {
2913         string initpath = lyxrc.document_path;
2914         LyXFileDlg fileDlg;
2915   
2916         if (owner->view()->available()) {
2917                 string trypath = owner->buffer()->filepath;
2918                 // If directory is writeable, use this as default.
2919                 if (IsDirWriteable(trypath) == 1)
2920                         initpath = trypath;
2921         }
2922
2923         // launches dialog
2924         ProhibitInput(owner->view());
2925         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2926         fileDlg.SetButton(1, _("Examples"), 
2927                           AddPath(system_lyxdir, "examples"));
2928         string filename;
2929         if (isnoweb) {
2930                 filename = fileDlg.Select(_("Select Noweb file to Import"),
2931                                           initpath, "*.nw");
2932         } else {
2933                 filename = fileDlg.Select(_("Select LaTeX file to Import"),
2934                                           initpath, "*.tex");
2935         }
2936         
2937         AllowInput(owner->view());
2938  
2939         // check selected filename
2940         if (filename.empty()) {
2941                 owner->getMiniBuffer()->Set(_("Canceled."));
2942                 return;
2943         }
2944
2945         // get absolute path of file
2946         filename = MakeAbsPath(filename);
2947
2948         // Check if the document already is open
2949         string LyXfilename = ChangeExtension(filename, ".lyx", false);
2950         if (bufferlist.exists(LyXfilename)){
2951                 switch(AskConfirmation(_("Document is already open:"), 
2952                                        MakeDisplayPath(LyXfilename, 50),
2953                                        _("Do you want to close that document now?\n"
2954                                          "('No' will just switch to the open version)")))
2955                         {
2956                         case 1: // Yes: close the document
2957                                 if (!bufferlist.close(bufferlist.getBuffer(LyXfilename)))
2958                                 // If close is canceled, we cancel here too.
2959                                         return;
2960                                 break;
2961                         case 2: // No: switch to the open document
2962                                 owner->view()->buffer(
2963                                         bufferlist.getBuffer(LyXfilename));
2964                                 return;
2965                         case 3: // Cancel: Do nothing
2966                                 owner->getMiniBuffer()->Set(_("Canceled."));
2967                                 return;
2968                         }
2969         }
2970
2971         // Check if a LyX document by the same root exists in filesystem
2972         FileInfo f(LyXfilename, true);
2973         if (f.exist() && !AskQuestion(_("A document by the name"), 
2974                                       MakeDisplayPath(LyXfilename),
2975                                       _("already exists. Overwrite?"))) {
2976                 owner->getMiniBuffer()->Set(_("Canceled."));
2977                 return;
2978         }
2979
2980         // loads document
2981         Buffer * openbuf;
2982         if (!isnoweb) {
2983                 owner->getMiniBuffer()->Set(_("Importing LaTeX file"),
2984                                             MakeDisplayPath(filename), "...");
2985                 ImportLaTeX myImport(filename);
2986                 openbuf = myImport.run();
2987         } else {
2988                 owner->getMiniBuffer()->Set(_("Importing Noweb file"),
2989                                             MakeDisplayPath(filename), "...");
2990                 ImportNoweb myImport(filename);
2991                 openbuf = myImport.run();
2992         }
2993         if (openbuf) {
2994                 owner->view()->buffer(openbuf);
2995                 owner->getMiniBuffer()->Set(isnoweb ?
2996                                             _("Noweb file ") : _("LateX file "),
2997                                             MakeDisplayPath(filename),
2998                                             _("imported."));
2999         } else {
3000                 owner->getMiniBuffer()->Set(isnoweb ?
3001                                             _("Could not import Noweb file") :
3002                                             _("Could not import LaTeX file"),
3003                                             MakeDisplayPath(filename));
3004         }
3005 }
3006
3007
3008 void LyXFunc::doImportLinuxDoc()
3009 {
3010         string initpath = lyxrc.document_path;
3011         LyXFileDlg fileDlg;
3012   
3013         if (owner->view()->available()) {
3014                 string trypath = owner->buffer()->filepath;
3015                 // If directory is writeable, use this as default.
3016                 if (IsDirWriteable(trypath) == 1)
3017                         initpath = trypath;
3018         }
3019
3020         // launches dialog
3021         ProhibitInput(owner->view());
3022         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
3023         fileDlg.SetButton(1, _("Examples"), 
3024                           AddPath(system_lyxdir, "examples"));
3025
3026         string filename = fileDlg.Select(_("Select LinuxDoc file to Import"),
3027                                           initpath, "*.sgml");
3028         
3029         AllowInput(owner->view());
3030  
3031         // check selected filename
3032         if (filename.empty()) {
3033                 owner->getMiniBuffer()->Set(_("Canceled."));
3034                 return;
3035         }
3036
3037         // get absolute path of file
3038         filename = MakeAbsPath(filename);
3039
3040         // Check if the document already is open
3041         string LyXfilename = ChangeExtension(filename, ".lyx", false);
3042         if (bufferlist.exists(LyXfilename)){
3043                 switch(AskConfirmation(_("Document is already open:"), 
3044                                        MakeDisplayPath(LyXfilename, 50),
3045                                        _("Do you want to close that document now?\n"
3046                                          "('No' will just switch to the open version)")))
3047                         {
3048                         case 1: // Yes: close the document
3049                                 if (!bufferlist.close(bufferlist.getBuffer(LyXfilename)))
3050                                 // If close is canceled, we cancel here too.
3051                                         return;
3052                                 break;
3053                         case 2: // No: switch to the open document
3054                                 owner->view()->buffer(
3055                                         bufferlist.getBuffer(LyXfilename));
3056                                 return;
3057                         case 3: // Cancel: Do nothing
3058                                 owner->getMiniBuffer()->Set(_("Canceled."));
3059                                 return;
3060                         }
3061         }
3062
3063         // Check if a LyX document by the same root exists in filesystem
3064         FileInfo f(LyXfilename, true);
3065         if (f.exist() && !AskQuestion(_("A document by the name"), 
3066                                       MakeDisplayPath(LyXfilename),
3067                                       _("already exists. Overwrite?"))) {
3068                 owner->getMiniBuffer()->Set(_("Canceled."));
3069                 return;
3070         }
3071
3072         // loads document
3073         owner->getMiniBuffer()->Set(_("Importing LinuxDoc file"),
3074                                     MakeDisplayPath(filename), "...");
3075
3076         // run sgml2lyx
3077         string tmp = lyxrc.linuxdoc_to_lyx_command + filename;
3078         Systemcalls one;
3079         Buffer * buf = 0;
3080
3081         int result = one.startscript(Systemcalls::System, tmp);
3082         if (result == 0) {
3083                 string filename = ChangeExtension(filename, ".lyx", false);
3084                 // File was generated without problems. Load it.
3085                 buf = bufferlist.loadLyXFile(filename);
3086                 owner->view()->buffer(buf);
3087                 owner->getMiniBuffer()->Set(_("LinuxDoc file "),
3088                                             MakeDisplayPath(filename),
3089                                             _("imported."));
3090         } else {
3091                 owner->getMiniBuffer()->Set(_("Could not import LinuxDoc file"),
3092                                             MakeDisplayPath(filename));
3093         }
3094 }
3095
3096
3097 void LyXFunc::MenuInsertLyXFile(string const & filen)
3098 {
3099         string filename = filen;
3100
3101         if (filename.empty()) {
3102                 // Launch a file browser
3103                 string initpath = lyxrc.document_path;
3104                 LyXFileDlg fileDlg;
3105
3106                 if (owner->view()->available()) {
3107                         string trypath = owner->buffer()->filepath;
3108                         // If directory is writeable, use this as default.
3109                         if (IsDirWriteable(trypath) == 1)
3110                                 initpath = trypath;
3111                 }
3112
3113                 // launches dialog
3114                 ProhibitInput(owner->view());
3115                 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
3116                 fileDlg.SetButton(1, _("Examples"), 
3117                                   AddPath(system_lyxdir, "examples"));
3118                 filename = fileDlg.Select(_("Select Document to Insert"),
3119                                           initpath, "*.lyx");
3120                 AllowInput(owner->view());
3121
3122                 // check selected filename
3123                 if (filename.empty()) {
3124                         owner->getMiniBuffer()->Set(_("Canceled."));
3125                         return;
3126                 }
3127         } 
3128
3129         // get absolute path of file and make sure the filename ends
3130         // with .lyx
3131         filename = MakeAbsPath(filename);
3132         if (!IsLyXFilename(filename))
3133                 filename += ".lyx";
3134
3135         // Inserts document
3136         owner->getMiniBuffer()->Set(_("Inserting document"),
3137                                     MakeDisplayPath(filename), "...");
3138         bool res = owner->view()->insertLyXFile(filename);
3139         if (res) {
3140                 owner->getMiniBuffer()->Set(_("Document"),
3141                                             MakeDisplayPath(filename),
3142                                             _("inserted."));
3143         } else {
3144                 owner->getMiniBuffer()->Set(_("Could not insert document"),
3145                                             MakeDisplayPath(filename));
3146         }
3147 }
3148
3149
3150 void LyXFunc::reloadBuffer()
3151 {
3152         string fn = owner->buffer()->fileName();
3153         if (bufferlist.close(owner->buffer()))
3154                 owner->view()->buffer(bufferlist.loadLyXFile(fn));
3155 }
3156
3157
3158 void LyXFunc::CloseBuffer()
3159 {
3160         if (bufferlist.close(owner->buffer()) && !quitting) {
3161                 if (bufferlist.empty()) {
3162                         // need this otherwise SEGV may occur while trying to
3163                         // set variables that don't exist
3164                         // since there's no current buffer
3165                         CloseAllBufferRelatedPopups();
3166                 }
3167                 else {
3168                         owner->view()->buffer(bufferlist.first());
3169                 }
3170         }
3171 }
3172
3173
3174 Inset * LyXFunc::getInsetByCode(Inset::Code code)
3175 {
3176         bool found = false;
3177         Inset * inset = 0;
3178         LyXCursor cursor = owner->view()->text->cursor;
3179         LyXParagraph::size_type pos = cursor.pos;
3180         LyXParagraph * par = cursor.par;
3181         
3182         while (par && !found) {
3183                 while ((inset = par->ReturnNextInsetPointer(pos))){
3184                         if (inset->LyxCode() == code) {
3185                                 found = true;
3186                                 break;
3187                         }
3188                         ++pos;
3189                 } 
3190                 par = par->next;
3191         }
3192         return found ? inset : 0;
3193 }
3194
3195
3196 // Each "owner" should have it's own message method. lyxview and
3197 // the minibuffer would use the minibuffer, but lyxserver would
3198 // send an ERROR signal to its client.  Alejandro 970603
3199 // This func is bit problematic when it comes to NLS, to make the
3200 // lyx servers client be language indepenent we must not translate
3201 // strings sent to this func.
3202 void LyXFunc::setErrorMessage(string const & m) const
3203 {
3204         dispatch_buffer = m;
3205         errorstat = true;
3206 }
3207
3208
3209 void LyXFunc::setMessage(string const & m)
3210 {
3211         dispatch_buffer = m;
3212 }