]> git.lyx.org Git - features.git/blob - src/lyxfunc.C
Added a preliminary NOT working insettabular. Lot's of fixes for the text
[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                 InsetTabular * new_inset = new InsetTabular(owner->buffer(),2,2);
2063                 owner->view()->insertInset(new_inset);
2064                 new_inset->Edit(owner->view(), 0, 0, 0);
2065         }
2066         break;
2067
2068         // --- lyxserver commands ----------------------------
2069
2070         case LFUN_CHARATCURSOR:
2071         {
2072                 LyXParagraph::size_type pos = 
2073                         owner->view()->text->cursor.pos;
2074                 if(pos < owner->view()->text->cursor.par->size())
2075                         //dispatch_buffer = owner->view()->text->
2076                         //      cursor.par->text[pos];
2077                         dispatch_buffer =
2078                                 owner->view()->text->
2079                                 cursor.par->GetChar(pos);
2080                 else
2081                         dispatch_buffer = "EOF";
2082         }
2083         break;
2084         
2085         case LFUN_GETXY:
2086                 dispatch_buffer = 
2087                         tostr(owner->view()->text->cursor.x) + ' '
2088                         + tostr(owner->view()->text->cursor.y);
2089                 break;
2090                 
2091         case LFUN_SETXY:
2092         {
2093                 int  x;
2094                 long y;
2095                 sscanf(argument.c_str(), " %d %ld", &x, &y);
2096                 owner->view()->text->SetCursorFromCoordinates(x, y);
2097         }
2098         break;
2099         
2100         case LFUN_GETLAYOUT:
2101                 dispatch_buffer =  
2102                         tostr(owner->view()->text->cursor.par->layout);
2103                 break;
2104                         
2105         case LFUN_GETFONT:
2106         {
2107                 LyXFont & font = owner->view()->text->current_font;
2108                 if(font.shape() == LyXFont::ITALIC_SHAPE)
2109                         dispatch_buffer = 'E';
2110                 else if(font.shape() == LyXFont::SMALLCAPS_SHAPE)
2111                         dispatch_buffer = 'N';
2112                 else
2113                         dispatch_buffer = '0';
2114
2115         }
2116         break;
2117
2118         case LFUN_GETLATEX:
2119         {
2120                 LyXFont & font = owner->view()->text->current_font;
2121                 if(font.latex() == LyXFont::ON)
2122                         dispatch_buffer = 'L';
2123                 else
2124                         dispatch_buffer = '0';
2125         }
2126         break;
2127
2128         case LFUN_GETNAME:
2129                 setMessage(owner->buffer()->fileName());
2130                 lyxerr.debug() << "FNAME["
2131                                << owner->buffer()->fileName()
2132                                << "] " << endl;
2133                 break;
2134                 
2135         case LFUN_NOTIFY:
2136         {
2137                 string buf;
2138                 keyseq.print(buf);
2139                 dispatch_buffer = buf;
2140                 lyxserver->notifyClient(dispatch_buffer);
2141         }
2142         break;
2143
2144         case LFUN_GOTOFILEROW:
2145         {
2146                 char file_name[100];
2147                 int  row;
2148                 sscanf(argument.c_str(), " %s %d", file_name, &row);
2149
2150                 // Must replace extension of the file to be .lyx and get full path
2151                 string s = ChangeExtension(string(file_name), ".lyx", false);
2152
2153                 // Either change buffer or load the file
2154                 if (bufferlist.exists(s))
2155                         owner->view()->buffer(bufferlist.getBuffer(s));
2156                 else
2157                         owner->view()->buffer(bufferlist.loadLyXFile(s));
2158
2159                 // Set the cursor  
2160                 owner->view()->setCursorFromRow(row);
2161
2162                 // Recenter screen
2163                 owner->view()->center();
2164         }
2165         break;
2166
2167         case LFUN_APROPOS:
2168         case LFUN_GETTIP:
2169         {
2170                 int qa = lyxaction.LookupFunc(argument.c_str());
2171                 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
2172         }
2173         break;
2174
2175         // --- accented characters ---------------------------
2176                 
2177         case LFUN_UMLAUT:
2178         case LFUN_CIRCUMFLEX:
2179         case LFUN_GRAVE:
2180         case LFUN_ACUTE:
2181         case LFUN_TILDE:
2182         case LFUN_CEDILLA:
2183         case LFUN_MACRON:
2184         case LFUN_DOT:
2185         case LFUN_UNDERDOT:
2186         case LFUN_UNDERBAR:
2187         case LFUN_CARON:
2188         case LFUN_SPECIAL_CARON:
2189         case LFUN_BREVE:
2190         case LFUN_TIE:
2191         case LFUN_HUNG_UMLAUT:
2192         case LFUN_CIRCLE:
2193         case LFUN_OGONEK:
2194         {
2195                 char c;
2196                 
2197                 if (keyseq.length == -1 && keyseq.getiso() != 0) 
2198                         c = keyseq.getiso();
2199                 else
2200                         c = 0;
2201                 
2202                 owner->getIntl()->getTrans()->
2203                         deadkey(c, get_accent(action).accent, 
2204                                 owner->view()->text);
2205                 
2206                 // Need to reset, in case the minibuffer calls these
2207                 // actions
2208                 keyseq.reset();
2209                 keyseq.length = 0;
2210                 
2211                 // copied verbatim from do_accent_char
2212                 owner->view()->smallUpdate(1);
2213                 SetUpdateTimer();
2214                 owner->view()->text->sel_cursor = 
2215                         owner->view()->text->cursor;
2216         }   
2217         break;
2218         
2219         // --- toolbar ----------------------------------
2220         case LFUN_PUSH_TOOLBAR:
2221         {
2222                 int nth = strToInt(argument);
2223                 if (lyxerr.debugging(Debug::TOOLBAR)) {
2224                         lyxerr << "LFUN_PUSH_TOOLBAR: argument = `"
2225                                << argument << "'\n"
2226                                << "LFUN_PUSH_TOOLBAR: nth = `"
2227                                << nth << "'" << endl;
2228                 }
2229                 
2230                 if (nth <= 0) {
2231                         LyXBell();
2232                         setErrorMessage(N_("Push-toolbar needs argument > 0"));
2233                 } else {
2234                         owner->getToolbar()->push(nth);
2235                 }
2236         }
2237         break;
2238         
2239         case LFUN_ADD_TO_TOOLBAR:
2240         {
2241                 if (lyxerr.debugging(Debug::TOOLBAR)) {
2242                         lyxerr << "LFUN_ADD_TO_TOOLBAR:"
2243                                 "argument = `" << argument << '\'' << endl;
2244                 }
2245                 string tmp(argument);
2246                 //lyxerr <<string("Argument: ") + argument);
2247                 //lyxerr <<string("Tmp     : ") + tmp);
2248                 if (tmp.empty()) {
2249                         LyXBell();
2250                         setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
2251                 } else {
2252                         owner->getToolbar()->add(argument, false);
2253                         owner->getToolbar()->set();
2254                 }
2255         }
2256         break;
2257         
2258         // --- insert characters ----------------------------------------
2259
2260         // ---  Mathed stuff. If we are here, there is no locked inset yet.
2261         
2262         // Greek mode     
2263         case LFUN_GREEK:
2264         {
2265                 if (!greek_kb_flag) {
2266                         greek_kb_flag = 1;
2267                         setMessage(N_("Math greek mode on"));
2268                 } else
2269                         greek_kb_flag = 0;
2270         }  
2271         break;
2272       
2273         // Greek keyboard      
2274         case LFUN_GREEK_TOGGLE:
2275         {
2276                 greek_kb_flag = greek_kb_flag ? 0 : 2;
2277                 if (greek_kb_flag) {
2278                         setMessage(N_("Math greek keyboard on"));
2279                 } else {
2280                         setMessage(N_("Math greek keyboard off"));
2281                 }
2282         }
2283         break;
2284         
2285         case LFUN_MATH_DELIM:     
2286         case LFUN_INSERT_MATRIX:
2287         {          
2288                 if (owner->view()->available()) { 
2289                         owner->view()->
2290                                 open_new_inset(new InsetFormula(false));
2291                         owner->view()
2292                                 ->the_locking_inset
2293                                 ->LocalDispatch(owner->view(),
2294                                                 action,
2295                                                 argument);
2296                 }
2297         }          
2298         break;
2299                
2300         case LFUN_INSERT_MATH:
2301         {
2302                 math_insert_symbol(argument.c_str());
2303         }
2304         break;
2305         
2306         case LFUN_MATH_DISPLAY:
2307         {
2308                 if (owner->view()->available())
2309                         owner->view()->open_new_inset(new InsetFormula(true));
2310                 break;
2311         }
2312                     
2313         case LFUN_MATH_MACRO:
2314         {
2315                 if (owner->view()->available()) {
2316                         string s(argument);
2317                         if (s.empty())
2318                                 setErrorMessage(N_("Missing argument"));
2319                         else {
2320                                 string s1 = token(s, ' ', 1);
2321                                 int na = s1.empty() ? 0: atoi(s1.c_str());
2322                                 owner->view()->
2323                                         open_new_inset(new InsetFormulaMacro(token(s, ' ', 0), na));
2324                         }
2325                 }
2326         }
2327         break;
2328
2329         case LFUN_MATH_MODE:   // Open or create a math inset
2330         {
2331                 
2332                 if (owner->view()->available())
2333                         owner->view()->open_new_inset(new InsetFormula);
2334                 setMessage(N_("Math editor mode"));
2335         }
2336         break;
2337           
2338         case LFUN_MATH_NUMBER:
2339         case LFUN_MATH_LIMITS:
2340         {
2341                 setErrorMessage(N_("This is only allowed in math mode!"));
2342         }
2343         break;
2344         
2345         case LFUN_INSERT_CITATION:
2346         {   
2347                 InsetCitation * new_inset = new InsetCitation();
2348                 // ale970405
2349                 // The note, if any, must be after the key, delimited
2350                 // by a | so both key and remark can have spaces.
2351                 if (!argument.empty()) {
2352                         string lsarg(argument);
2353                         if (contains(lsarg, "|")) {
2354                                 new_inset->setContents(token(lsarg, '|', 0));
2355                                 new_inset->setOptions(token(lsarg, '|', 1));
2356                         } else
2357                                 new_inset->setContents(lsarg);
2358                         owner->view()->insertInset(new_inset);
2359                 } else {
2360                         owner->view()->insertInset(new_inset);
2361                         new_inset->Edit(owner->view(), 0, 0, 0);
2362                 }
2363         }
2364         break;
2365                     
2366         case LFUN_INSERT_BIBTEX:
2367         {   
2368                 // ale970405+lasgoutt970425
2369                 // The argument can be up to two tokens separated 
2370                 // by a space. The first one is the bibstyle.
2371                 string lsarg(argument);
2372                 string bibstyle = token(lsarg, ' ', 1);
2373                 if (bibstyle.empty())
2374                         bibstyle = "plain";
2375                 InsetBibtex * new_inset 
2376                         = new InsetBibtex(token(lsarg, ' ', 0),
2377                                           bibstyle,
2378                                           owner->buffer());
2379                 
2380                 owner->view()->insertInset(new_inset);
2381                 if (lsarg.empty()) {
2382                         new_inset->Edit(owner->view(), 0, 0, 0);
2383                 }
2384         }
2385         break;
2386                 
2387         // BibTeX data bases
2388         case LFUN_BIBDB_ADD:
2389         {
2390                 InsetBibtex * inset = 
2391                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2392                 if (inset) {
2393                         inset->addDatabase(argument);
2394                 }
2395         }
2396         break;
2397                     
2398         case LFUN_BIBDB_DEL:
2399         {
2400                 InsetBibtex * inset = 
2401                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2402                 if (inset) {
2403                         inset->delDatabase(argument);
2404                 }
2405         }
2406         break;
2407         
2408         case LFUN_BIBTEX_STYLE:
2409         {
2410                 InsetBibtex * inset = 
2411                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2412                 if (inset) {
2413                         inset->setOptions(argument);
2414                 }
2415         }
2416         break;
2417                 
2418         case LFUN_INDEX_INSERT:
2419         case LFUN_INDEX_INSERT_LAST:
2420         {
2421                 // Can't do that at the beginning of a paragraph.
2422                 if (owner->view()->text->cursor.pos - 1 < 0)
2423                         break;
2424
2425                 InsetIndex * new_inset = new InsetIndex();
2426                 if (!argument.empty()) {
2427                         string lsarg(argument);
2428                         new_inset->setContents(lsarg);
2429                         owner->view()->insertInset(new_inset);
2430                 } else {
2431                         //reh 98/09/21
2432                         //get the current word for an argument
2433                         LyXParagraph::size_type lastpos = 
2434                                 owner->view()->text->cursor.pos - 1;
2435                         // Get the current word. note that this must be done
2436                         // before inserting the inset, or the inset will
2437                         // break the word
2438                         string curstring(owner->view()
2439                                          ->text->cursor.par->GetWord(lastpos));
2440
2441                         //make the new inset and write the current word into it
2442                         InsetIndex * new_inset = new InsetIndex();
2443
2444                         new_inset->setContents(curstring);
2445
2446                         //don't edit it if the call was to INSERT_LAST
2447                         if(action != LFUN_INDEX_INSERT_LAST) {
2448                                 new_inset->Edit(owner->view(), 0, 0, 0);
2449                         } else {
2450                                 //it looks blank on the screen unless
2451                                 //we do  something.  put it here.
2452
2453                                 // move the cursor to the returned value of lastpos
2454                                 // but only for the auto-insert
2455                                 owner->view()->text->cursor.pos = lastpos;
2456                         }
2457
2458                         //put the new inset into the buffer.
2459                         // there should be some way of knowing the user
2460                         //cancelled & avoiding this, but i don't know how
2461                         owner->view()->insertInset(new_inset);
2462                 }
2463         }
2464         break;
2465
2466         case LFUN_INDEX_PRINT:
2467         {
2468                 Inset * new_inset = new InsetPrintIndex(owner->buffer());
2469                 owner->view()->insertInset(new_inset, "Standard", true);
2470         }
2471         break;
2472
2473         case LFUN_PARENTINSERT:
2474         {
2475                 lyxerr << "arg " << argument << endl;
2476                 Inset * new_inset = new InsetParent(argument, owner->buffer());
2477                 owner->view()->insertInset(new_inset, "Standard", true);
2478         }
2479         break;
2480
2481         case LFUN_CHILDINSERT:
2482         {
2483                 Inset * new_inset = new InsetInclude(argument,
2484                                                      owner->buffer());
2485                 owner->view()->insertInset(new_inset, "Standard", true);
2486                 new_inset->Edit(owner->view(), 0, 0, 0);
2487         }
2488         break;
2489
2490         case LFUN_CHILDOPEN:
2491         {
2492                 string filename =
2493                         MakeAbsPath(argument, 
2494                                     OnlyPath(owner->buffer()->fileName()));
2495                 setMessage(N_("Opening child document ") +
2496                            MakeDisplayPath(filename) + "...");
2497                 owner->view()->savePosition();
2498                 if (bufferlist.exists(filename))
2499                         owner->view()->buffer(bufferlist.getBuffer(filename));
2500                 else
2501                         owner->view()->buffer(bufferlist.loadLyXFile(filename));
2502         }
2503         break;
2504
2505         case LFUN_INSERT_NOTE:
2506                 owner->view()->insertNote();
2507                 break;
2508                 
2509         case LFUN_INSERTFOOTNOTE: 
2510         {
2511                 LyXParagraph::footnote_kind kind;
2512                 if (argument == "footnote")
2513                         { kind = LyXParagraph::FOOTNOTE; }
2514                 else if (argument == "margin")
2515                         { kind = LyXParagraph::MARGIN; }
2516                 else if (argument == "figure")
2517                         { kind = LyXParagraph::FIG; }
2518                 else if (argument == "table")
2519                         { kind = LyXParagraph::TAB; }
2520                 else if (argument == "wide-fig")
2521                         { kind = LyXParagraph::WIDE_FIG; }
2522                 else if (argument == "wide-tab")
2523                         { kind = LyXParagraph::WIDE_TAB; }
2524                 else if (argument == "algorithm")
2525                         { kind = LyXParagraph::ALGORITHM; }
2526                 else {
2527                         setErrorMessage(N_("Unknown kind of footnote"));
2528                         break;
2529                 }
2530                 owner->view()->text->InsertFootnoteEnvironment(kind);
2531                 owner->view()->update(1);
2532                 owner->view()->setState();
2533         }
2534         break;
2535         
2536         case LFUN_BUFFERBULLETSSELECT:
2537                 bulletForm();
2538                 break;
2539                 
2540         case LFUN_TOGGLECURSORFOLLOW:
2541                 cursor_follows_scrollbar = !cursor_follows_scrollbar;
2542                 break;
2543                 
2544         case LFUN_KMAP_OFF:             // keymap off
2545                 owner->getIntl()->KeyMapOn(false);
2546                 break;
2547                 
2548         case LFUN_KMAP_PRIM:    // primary keymap
2549                 owner->getIntl()->KeyMapPrim();
2550                 break;
2551                 
2552         case LFUN_KMAP_SEC:             // secondary keymap
2553                 owner->getIntl()->KeyMapSec();
2554                 break;
2555                 
2556         case LFUN_KMAP_TOGGLE:  // toggle keymap
2557                 owner->getIntl()->ToggleKeyMap();
2558                 break;
2559
2560         case LFUN_SELFINSERT:
2561         {
2562                 for (string::size_type i = 0; i < argument.length(); ++i) {
2563                         owner->view()->text->InsertChar(argument[i]);
2564                         // This needs to be in the loop, or else we
2565                         // won't break lines correctly. (Asger)
2566                         owner->view()->smallUpdate(1);
2567                 }
2568                 SetUpdateTimer();
2569                 owner->view()->text->sel_cursor = 
2570                         owner->view()->text->cursor;
2571                 moveCursorUpdate(false);
2572         }
2573         break;
2574
2575         case LFUN_SEQUENCE: 
2576         {
2577                 // argument contains ';'-terminated commands
2578                 while (argument.find(';') != string::npos) {
2579                         string first;
2580                         argument = split(argument, first, ';');
2581                         Dispatch(first);
2582                 }
2583         }
2584         break;
2585
2586         case LFUN_DATE_INSERT:  // jdblair: date-insert cmd
2587         {
2588                 char datetmp[32];
2589                 int datetmp_len;
2590                 time_t now_time_t;
2591                 struct tm *now_tm;
2592                 static string arg;
2593                 
2594                 now_time_t = time(NULL);
2595                 now_tm = localtime(&now_time_t);
2596                 (void)setlocale(LC_TIME, "");
2597                 if (!argument.empty())
2598                         arg = argument;
2599                 else if (arg.empty())
2600                         arg = lyxrc.date_insert_format;
2601                 datetmp_len = (int) strftime(datetmp, 32, arg.c_str(), now_tm);
2602                 for (int i = 0; i < datetmp_len; i++) {
2603                         owner->view()->text->InsertChar(datetmp[i]);
2604                         owner->view()->smallUpdate(1);
2605                 }
2606                 SetUpdateTimer();
2607                 owner->view()->text->sel_cursor = owner->view()->text->cursor;
2608                 moveCursorUpdate(false);
2609         }
2610         break;
2611
2612         case LFUN_SAVEPREFERENCES:
2613         {
2614                 Path p(user_lyxdir);
2615                 lyxrc.write("preferences");
2616         }
2617         break;
2618         
2619         case LFUN_UNKNOWN_ACTION:
2620         {
2621                 if(!owner->buffer()) {
2622                         LyXBell();
2623                         setErrorMessage(N_("No document open"));
2624                         break;
2625                 }
2626
2627                 if (owner->buffer()->isReadonly()) {
2628                         LyXBell();
2629                         setErrorMessage(N_("Document is read only"));
2630                         break;
2631                 }
2632                          
2633                 if (!argument.empty()) {
2634                         
2635                         /* Automatically delete the currently selected
2636                          * text and replace it with what is being
2637                          * typed in now. Depends on lyxrc settings
2638                          * "auto_region_delete", which defaults to
2639                          * true (on). */
2640                 
2641                         if ( lyxrc.auto_region_delete ) {
2642                                 if (owner->view()->text->selection){
2643                                         owner->view()->text->CutSelection(false);
2644                                         owner->view()->update(-1);
2645                                 }
2646                         }
2647                         
2648                         owner->view()->beforeChange();
2649
2650 #if 0
2651                         if (isdigit(argument[0]) &&
2652                             (lyxrc.number_inset == "true" ||
2653                              (lyxrc.number_inset == "rtl" &&
2654                               owner->view()->text->real_current_font.isVisibleRightToLeft() 
2655                               ))) {
2656                                 UpdatableInset * tmpinset = new InsetNumber(owner->buffer());
2657                                 owner->view()->open_new_inset(tmpinset);
2658                                 tmpinset->LocalDispatch(owner->view(), action,
2659                                                         argument);
2660                                 return string();
2661                         }
2662 #endif
2663                         
2664                         for (string::size_type i = 0;
2665                              i < argument.length(); ++i) {
2666                                 if (greek_kb_flag) {
2667                                         if (!math_insert_greek(argument[i]))
2668                                                 owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2669                                 } else
2670                                         owner->getIntl()->getTrans()->TranslateAndInsert(argument[i], owner->view()->text);
2671                         }
2672                         
2673                         owner->view()->smallUpdate(1);
2674                         SetUpdateTimer();
2675
2676                         owner->view()->text->sel_cursor = 
2677                                 owner->view()->text->cursor;
2678                         moveCursorUpdate(false);
2679                         return string();
2680                 } else {
2681                         // why is an "Unknown action" with empty
2682                         // argument even dispatched in the first
2683                         // place? I`ll probably change that. (Lgb)
2684                         LyXBell();
2685                         setErrorMessage(N_("Unknown action"));
2686                 }
2687                 break;
2688         default:
2689                 lyxerr << "A truly unknown func!" << endl;
2690                 break;
2691         }
2692         } // end of switch
2693   exit_with_message:
2694
2695         string res = getMessage();
2696
2697         if (res.empty()) {
2698                 if (!commandshortcut.empty()) {
2699                         string newbuf = owner->getMiniBuffer()->GetText();
2700                         if (newbuf != commandshortcut) {
2701                                 owner->getMiniBuffer()->Set(newbuf
2702                                                             + " " +
2703                                                             commandshortcut);
2704                         }
2705                 }
2706         } else {
2707                 owner->getMiniBuffer()->Set(string(_(res.c_str()))
2708                                             + " " + commandshortcut);
2709         }
2710
2711         return res;
2712 }
2713
2714
2715 void LyXFunc::setupLocalKeymap()
2716 {
2717         keyseq.stdmap = keyseq.curmap = toplevel_keymap;
2718         cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap;
2719 }
2720
2721
2722 void LyXFunc::MenuNew(bool fromTemplate)
2723 {
2724         string fname, initpath = lyxrc.document_path;
2725         LyXFileDlg fileDlg;
2726
2727         if (owner->view()->available()) {
2728                 string trypath = owner->buffer()->filepath;
2729                 // If directory is writeable, use this as default.
2730                 if (IsDirWriteable(trypath) == 1)
2731                         initpath = trypath;
2732         }
2733
2734         ProhibitInput(owner->view());
2735         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2736         fileDlg.SetButton(1, _("Templates"), lyxrc.template_path);
2737         fname = fileDlg.Select(_("Enter Filename for new document"), 
2738                                initpath, "*.lyx", _("newfile"));
2739         AllowInput(owner->view());
2740         
2741         if (fname.empty()) {
2742                 owner->getMiniBuffer()->Set(_("Canceled."));
2743                 lyxerr.debug() << "New Document Cancelled." << endl;
2744                 return;
2745         }
2746         
2747         // get absolute path of file and make sure the filename ends
2748         // with .lyx
2749         string s = MakeAbsPath(fname);
2750         if (!IsLyXFilename(s))
2751                 s += ".lyx";
2752
2753         // Check if the document already is open
2754         if (bufferlist.exists(s)){
2755                 switch(AskConfirmation(_("Document is already open:"), 
2756                                        MakeDisplayPath(s, 50),
2757                                        _("Do you want to close that document now?\n"
2758                                          "('No' will just switch to the open version)")))
2759                         {
2760                         case 1: // Yes: close the document
2761                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
2762                                 // If close is canceled, we cancel here too.
2763                                         return;
2764                                 break;
2765                         case 2: // No: switch to the open document
2766                                 owner->view()->buffer(bufferlist.getBuffer(s));
2767                                 return;
2768                         case 3: // Cancel: Do nothing
2769                                 owner->getMiniBuffer()->Set(_("Canceled."));
2770                                 return;
2771                         }
2772         }
2773         
2774         // Check whether the file already exists
2775         if (IsLyXFilename(s)) {
2776                 FileInfo fi(s);
2777                 if (fi.readable() &&
2778                     AskQuestion(_("File already exists:"), 
2779                                 MakeDisplayPath(s, 50),
2780                                 _("Do you want to open the document?"))) {
2781                         // loads document
2782                         owner->getMiniBuffer()->Set(_("Opening document"), 
2783                                                     MakeDisplayPath(s), "...");
2784                         XFlush(fl_display);
2785                         owner->view()->buffer(
2786                                 bufferlist.loadLyXFile(s));
2787                         owner->getMiniBuffer()->Set(_("Document"),
2788                                                     MakeDisplayPath(s),
2789                                                     _("opened."));
2790                         return;
2791                 }
2792         }
2793
2794         // The template stuff
2795         string templname;
2796         if (fromTemplate) {
2797                 ProhibitInput(owner->view());
2798                 fname = fileDlg.Select(_("Choose template"),
2799                                        lyxrc.template_path,
2800                                        "*.lyx");
2801                 templname = fname;
2802                 AllowInput(owner->view());
2803         }
2804   
2805         // find a free buffer
2806         lyxerr.debug() << "Find a free buffer." << endl;
2807         owner->view()->buffer(bufferlist.newFile(s, templname));
2808 }
2809
2810
2811 void LyXFunc::MenuOpen()
2812 {
2813         string initpath = lyxrc.document_path;
2814         LyXFileDlg fileDlg;
2815   
2816         if (owner->view()->available()) {
2817                 string trypath = owner->buffer()->filepath;
2818                 // If directory is writeable, use this as default.
2819                 if (IsDirWriteable(trypath) == 1)
2820                         initpath = trypath;
2821         }
2822
2823         // launches dialog
2824         ProhibitInput(owner->view());
2825         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2826         fileDlg.SetButton(1, _("Examples"), 
2827                           AddPath(system_lyxdir, "examples"));
2828         string filename = fileDlg.Select(_("Select Document to Open"),
2829                                          initpath, "*.lyx");
2830         AllowInput(owner->view());
2831  
2832         // check selected filename
2833         if (filename.empty()) {
2834                 owner->getMiniBuffer()->Set(_("Canceled."));
2835                 return;
2836         }
2837
2838         // get absolute path of file and make sure the filename ends
2839         // with .lyx
2840         filename = MakeAbsPath(filename);
2841         if (!IsLyXFilename(filename))
2842                 filename += ".lyx";
2843
2844         // loads document
2845         owner->getMiniBuffer()->Set(_("Opening document"),
2846                                     MakeDisplayPath(filename), "...");
2847         Buffer * openbuf = bufferlist.loadLyXFile(filename);
2848         if (openbuf) {
2849                 owner->view()->buffer(openbuf);
2850                 owner->getMiniBuffer()->Set(_("Document"),
2851                                             MakeDisplayPath(filename),
2852                                             _("opened."));
2853         } else {
2854                 owner->getMiniBuffer()->Set(_("Could not open document"),
2855                                             MakeDisplayPath(filename));
2856         }
2857 }
2858
2859
2860 void LyXFunc::doImportASCII(bool linorpar)
2861 {
2862         string initpath = lyxrc.document_path;
2863         LyXFileDlg fileDlg;
2864   
2865         if (owner->view()->available()) {
2866                 string trypath = owner->buffer()->filepath;
2867                 // If directory is writeable, use this as default.
2868                 if (IsDirWriteable(trypath) == 1)
2869                         initpath = trypath;
2870         }
2871
2872         // launches dialog
2873         ProhibitInput(owner->view());
2874         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2875         fileDlg.SetButton(1, _("Examples"), 
2876                           AddPath(system_lyxdir, "examples"));
2877         string filename = fileDlg.Select(_("Select ASCII file to Import"),
2878                                          initpath, "*.txt");
2879         AllowInput(owner->view());
2880  
2881         // check selected filename
2882         if (filename.empty()) {
2883                 owner->getMiniBuffer()->Set(_("Canceled."));
2884                 return;
2885         }
2886
2887         // get absolute path of file
2888         filename = MakeAbsPath(filename);
2889
2890         string s = ChangeExtension(filename, ".lyx", false);
2891
2892         // Check if the document already is open
2893         if (bufferlist.exists(s)) {
2894                 switch(AskConfirmation(_("Document is already open:"), 
2895                                        MakeDisplayPath(s, 50),
2896                                        _("Do you want to close that document now?\n"
2897                                          "('No' will just switch to the open version)")))
2898                         {
2899                         case 1: // Yes: close the document
2900                                 if (!bufferlist.close(bufferlist.getBuffer(s)))
2901                                 // If close is canceled, we cancel here too.
2902                                         return;
2903                                 break;
2904                         case 2: // No: switch to the open document
2905                                 owner->view()->buffer(bufferlist.getBuffer(s));
2906                                 return;
2907                         case 3: // Cancel: Do nothing
2908                                 owner->getMiniBuffer()->Set(_("Canceled."));
2909                                 return;
2910                         }
2911         }
2912
2913         // Check if a LyX document by the same root exists in filesystem
2914         FileInfo f(s, true);
2915         if (f.exist() && !AskQuestion(_("A document by the name"), 
2916                                       MakeDisplayPath(s),
2917                                       _("already exists. Overwrite?"))) {
2918                 owner->getMiniBuffer()->Set(_("Canceled."));
2919                 return;
2920         }
2921
2922         owner->view()->buffer(bufferlist.newFile(s, string()));
2923         owner->getMiniBuffer()->Set(_("Importing ASCII file"),
2924                                     MakeDisplayPath(filename), "...");
2925         // Insert ASCII file
2926         InsertAsciiFile(owner->view(), filename, linorpar);
2927         owner->getMiniBuffer()->Set(_("ASCII file "),
2928                                     MakeDisplayPath(filename),
2929                                     _("imported."));
2930 }
2931
2932
2933 void LyXFunc::doImportLaTeX(bool isnoweb)
2934 {
2935         string initpath = lyxrc.document_path;
2936         LyXFileDlg fileDlg;
2937   
2938         if (owner->view()->available()) {
2939                 string trypath = owner->buffer()->filepath;
2940                 // If directory is writeable, use this as default.
2941                 if (IsDirWriteable(trypath) == 1)
2942                         initpath = trypath;
2943         }
2944
2945         // launches dialog
2946         ProhibitInput(owner->view());
2947         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
2948         fileDlg.SetButton(1, _("Examples"), 
2949                           AddPath(system_lyxdir, "examples"));
2950         string filename;
2951         if (isnoweb) {
2952                 filename = fileDlg.Select(_("Select Noweb file to Import"),
2953                                           initpath, "*.nw");
2954         } else {
2955                 filename = fileDlg.Select(_("Select LaTeX file to Import"),
2956                                           initpath, "*.tex");
2957         }
2958         
2959         AllowInput(owner->view());
2960  
2961         // check selected filename
2962         if (filename.empty()) {
2963                 owner->getMiniBuffer()->Set(_("Canceled."));
2964                 return;
2965         }
2966
2967         // get absolute path of file
2968         filename = MakeAbsPath(filename);
2969
2970         // Check if the document already is open
2971         string LyXfilename = ChangeExtension(filename, ".lyx", false);
2972         if (bufferlist.exists(LyXfilename)){
2973                 switch(AskConfirmation(_("Document is already open:"), 
2974                                        MakeDisplayPath(LyXfilename, 50),
2975                                        _("Do you want to close that document now?\n"
2976                                          "('No' will just switch to the open version)")))
2977                         {
2978                         case 1: // Yes: close the document
2979                                 if (!bufferlist.close(bufferlist.getBuffer(LyXfilename)))
2980                                 // If close is canceled, we cancel here too.
2981                                         return;
2982                                 break;
2983                         case 2: // No: switch to the open document
2984                                 owner->view()->buffer(
2985                                         bufferlist.getBuffer(LyXfilename));
2986                                 return;
2987                         case 3: // Cancel: Do nothing
2988                                 owner->getMiniBuffer()->Set(_("Canceled."));
2989                                 return;
2990                         }
2991         }
2992
2993         // Check if a LyX document by the same root exists in filesystem
2994         FileInfo f(LyXfilename, true);
2995         if (f.exist() && !AskQuestion(_("A document by the name"), 
2996                                       MakeDisplayPath(LyXfilename),
2997                                       _("already exists. Overwrite?"))) {
2998                 owner->getMiniBuffer()->Set(_("Canceled."));
2999                 return;
3000         }
3001
3002         // loads document
3003         Buffer * openbuf;
3004         if (!isnoweb) {
3005                 owner->getMiniBuffer()->Set(_("Importing LaTeX file"),
3006                                             MakeDisplayPath(filename), "...");
3007                 ImportLaTeX myImport(filename);
3008                 openbuf = myImport.run();
3009         } else {
3010                 owner->getMiniBuffer()->Set(_("Importing Noweb file"),
3011                                             MakeDisplayPath(filename), "...");
3012                 ImportNoweb myImport(filename);
3013                 openbuf = myImport.run();
3014         }
3015         if (openbuf) {
3016                 owner->view()->buffer(openbuf);
3017                 owner->getMiniBuffer()->Set(isnoweb ?
3018                                             _("Noweb file ") : _("LateX file "),
3019                                             MakeDisplayPath(filename),
3020                                             _("imported."));
3021         } else {
3022                 owner->getMiniBuffer()->Set(isnoweb ?
3023                                             _("Could not import Noweb file") :
3024                                             _("Could not import LaTeX file"),
3025                                             MakeDisplayPath(filename));
3026         }
3027 }
3028
3029
3030 void LyXFunc::doImportLinuxDoc()
3031 {
3032         string initpath = lyxrc.document_path;
3033         LyXFileDlg fileDlg;
3034   
3035         if (owner->view()->available()) {
3036                 string trypath = owner->buffer()->filepath;
3037                 // If directory is writeable, use this as default.
3038                 if (IsDirWriteable(trypath) == 1)
3039                         initpath = trypath;
3040         }
3041
3042         // launches dialog
3043         ProhibitInput(owner->view());
3044         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
3045         fileDlg.SetButton(1, _("Examples"), 
3046                           AddPath(system_lyxdir, "examples"));
3047
3048         string filename = fileDlg.Select(_("Select LinuxDoc file to Import"),
3049                                           initpath, "*.sgml");
3050         
3051         AllowInput(owner->view());
3052  
3053         // check selected filename
3054         if (filename.empty()) {
3055                 owner->getMiniBuffer()->Set(_("Canceled."));
3056                 return;
3057         }
3058
3059         // get absolute path of file
3060         filename = MakeAbsPath(filename);
3061
3062         // Check if the document already is open
3063         string LyXfilename = ChangeExtension(filename, ".lyx", false);
3064         if (bufferlist.exists(LyXfilename)){
3065                 switch(AskConfirmation(_("Document is already open:"), 
3066                                        MakeDisplayPath(LyXfilename, 50),
3067                                        _("Do you want to close that document now?\n"
3068                                          "('No' will just switch to the open version)")))
3069                         {
3070                         case 1: // Yes: close the document
3071                                 if (!bufferlist.close(bufferlist.getBuffer(LyXfilename)))
3072                                 // If close is canceled, we cancel here too.
3073                                         return;
3074                                 break;
3075                         case 2: // No: switch to the open document
3076                                 owner->view()->buffer(
3077                                         bufferlist.getBuffer(LyXfilename));
3078                                 return;
3079                         case 3: // Cancel: Do nothing
3080                                 owner->getMiniBuffer()->Set(_("Canceled."));
3081                                 return;
3082                         }
3083         }
3084
3085         // Check if a LyX document by the same root exists in filesystem
3086         FileInfo f(LyXfilename, true);
3087         if (f.exist() && !AskQuestion(_("A document by the name"), 
3088                                       MakeDisplayPath(LyXfilename),
3089                                       _("already exists. Overwrite?"))) {
3090                 owner->getMiniBuffer()->Set(_("Canceled."));
3091                 return;
3092         }
3093
3094         // loads document
3095         owner->getMiniBuffer()->Set(_("Importing LinuxDoc file"),
3096                                     MakeDisplayPath(filename), "...");
3097
3098         // run sgml2lyx
3099         string tmp = lyxrc.linuxdoc_to_lyx_command + filename;
3100         Systemcalls one;
3101         Buffer * buf = 0;
3102
3103         int result = one.startscript(Systemcalls::System, tmp);
3104         if (result == 0) {
3105                 string filename = ChangeExtension(filename, ".lyx", false);
3106                 // File was generated without problems. Load it.
3107                 buf = bufferlist.loadLyXFile(filename);
3108                 owner->view()->buffer(buf);
3109                 owner->getMiniBuffer()->Set(_("LinuxDoc file "),
3110                                             MakeDisplayPath(filename),
3111                                             _("imported."));
3112         } else {
3113                 owner->getMiniBuffer()->Set(_("Could not import LinuxDoc file"),
3114                                             MakeDisplayPath(filename));
3115         }
3116 }
3117
3118
3119 void LyXFunc::MenuInsertLyXFile(string const & filen)
3120 {
3121         string filename = filen;
3122
3123         if (filename.empty()) {
3124                 // Launch a file browser
3125                 string initpath = lyxrc.document_path;
3126                 LyXFileDlg fileDlg;
3127
3128                 if (owner->view()->available()) {
3129                         string trypath = owner->buffer()->filepath;
3130                         // If directory is writeable, use this as default.
3131                         if (IsDirWriteable(trypath) == 1)
3132                                 initpath = trypath;
3133                 }
3134
3135                 // launches dialog
3136                 ProhibitInput(owner->view());
3137                 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
3138                 fileDlg.SetButton(1, _("Examples"), 
3139                                   AddPath(system_lyxdir, "examples"));
3140                 filename = fileDlg.Select(_("Select Document to Insert"),
3141                                           initpath, "*.lyx");
3142                 AllowInput(owner->view());
3143
3144                 // check selected filename
3145                 if (filename.empty()) {
3146                         owner->getMiniBuffer()->Set(_("Canceled."));
3147                         return;
3148                 }
3149         } 
3150
3151         // get absolute path of file and make sure the filename ends
3152         // with .lyx
3153         filename = MakeAbsPath(filename);
3154         if (!IsLyXFilename(filename))
3155                 filename += ".lyx";
3156
3157         // Inserts document
3158         owner->getMiniBuffer()->Set(_("Inserting document"),
3159                                     MakeDisplayPath(filename), "...");
3160         bool res = owner->view()->insertLyXFile(filename);
3161         if (res) {
3162                 owner->getMiniBuffer()->Set(_("Document"),
3163                                             MakeDisplayPath(filename),
3164                                             _("inserted."));
3165         } else {
3166                 owner->getMiniBuffer()->Set(_("Could not insert document"),
3167                                             MakeDisplayPath(filename));
3168         }
3169 }
3170
3171
3172 void LyXFunc::reloadBuffer()
3173 {
3174         string fn = owner->buffer()->fileName();
3175         if (bufferlist.close(owner->buffer()))
3176                 owner->view()->buffer(bufferlist.loadLyXFile(fn));
3177 }
3178
3179
3180 void LyXFunc::CloseBuffer()
3181 {
3182         if (bufferlist.close(owner->buffer()) && !quitting) {
3183                 if (bufferlist.empty()) {
3184                         // need this otherwise SEGV may occur while trying to
3185                         // set variables that don't exist
3186                         // since there's no current buffer
3187                         CloseAllBufferRelatedPopups();
3188                 }
3189                 else {
3190                         owner->view()->buffer(bufferlist.first());
3191                 }
3192         }
3193 }
3194
3195
3196 Inset * LyXFunc::getInsetByCode(Inset::Code code)
3197 {
3198         bool found = false;
3199         Inset * inset = 0;
3200         LyXCursor cursor = owner->view()->text->cursor;
3201         LyXParagraph::size_type pos = cursor.pos;
3202         LyXParagraph * par = cursor.par;
3203         
3204         while (par && !found) {
3205                 while ((inset = par->ReturnNextInsetPointer(pos))){
3206                         if (inset->LyxCode() == code) {
3207                                 found = true;
3208                                 break;
3209                         }
3210                         ++pos;
3211                 } 
3212                 par = par->next;
3213         }
3214         return found ? inset : 0;
3215 }
3216
3217
3218 // Each "owner" should have it's own message method. lyxview and
3219 // the minibuffer would use the minibuffer, but lyxserver would
3220 // send an ERROR signal to its client.  Alejandro 970603
3221 // This func is bit problematic when it comes to NLS, to make the
3222 // lyx servers client be language indepenent we must not translate
3223 // strings sent to this func.
3224 void LyXFunc::setErrorMessage(string const & m) const
3225 {
3226         dispatch_buffer = m;
3227         errorstat = true;
3228 }
3229
3230
3231 void LyXFunc::setMessage(string const & m)
3232 {
3233         dispatch_buffer = m;
3234 }