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