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