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