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