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