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