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