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