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