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