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