]> git.lyx.org Git - lyx.git/blob - src/text3.C
211edad94c0c2ae6736a97efa1a986771e84f8e8
[lyx.git] / src / text3.C
1 /**
2  * \file text3.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Alfredo Braunstein
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "lyxtext.h"
19
20 #include "buffer.h"
21 #include "bufferparams.h"
22 #include "BufferView.h"
23 #include "cursor.h"
24 #include "debug.h"
25 #include "dispatchresult.h"
26 #include "factory.h"
27 #include "funcrequest.h"
28 #include "gettext.h"
29 #include "intl.h"
30 #include "language.h"
31 #include "lyxrc.h"
32 #include "lyxrow.h"
33 #include "paragraph.h"
34 #include "paragraph_funcs.h"
35 #include "ParagraphParameters.h"
36 #include "undo.h"
37 #include "vspace.h"
38
39 #include "frontends/Dialogs.h"
40 #include "frontends/LyXView.h"
41
42 #include "insets/insetcommand.h"
43 #include "insets/insetnewline.h"
44 #include "insets/insetspecialchar.h"
45 #include "insets/insettext.h"
46
47 #include "support/lstrings.h"
48 #include "support/tostr.h"
49 #include "support/std_sstream.h"
50
51 #include "mathed/math_hullinset.h"
52
53 #include <clocale>
54
55 using bv_funcs::replaceSelection;
56
57 using lyx::pos_type;
58
59 using lyx::support::isStrUnsignedInt;
60 using lyx::support::strToUnsignedInt;
61
62 using std::endl;
63 using std::find;
64 using std::string;
65 using std::istringstream;
66 using std::vector;
67
68
69 extern string current_layout;
70
71 // the selection possible is needed, that only motion events are
72 // used, where the button press event was on the drawing area too
73 bool selection_possible = false;
74
75
76 namespace {
77
78         // globals...
79         LyXFont freefont(LyXFont::ALL_IGNORE);
80         bool toggleall = false;
81
82
83         void toggleAndShow(BufferView * bv, LyXText * text,
84                 LyXFont const & font, bool toggleall = true)
85         {
86                 if (!bv->available())
87                         return;
88
89                 text->toggleFree(font, toggleall);
90                 bv->update();
91
92                 if (font.language() != ignore_language ||
93                                 font.number() != LyXFont::IGNORE) {
94                         CursorSlice & cur = text->cursor();
95                         Paragraph & par = *text->cursorPar();
96                         text->bidi.computeTables(par, *bv->buffer(),
97                                 *par.getRow(cur.pos()));
98                         if (cur.boundary() !=
99                                         text->bidi.isBoundary(*bv->buffer(), par,
100                                                         cur.pos(),
101                                                         text->real_current_font))
102                                 text->setCursor(cur.par(), cur.pos(),
103                                                 false, !cur.boundary());
104                 }
105         }
106
107
108         /// Apply the contents of freefont at the current cursor location.
109         void apply_freefont(BufferView * bv, LyXText * text)
110         {
111                 toggleAndShow(bv, text, freefont, toggleall);
112                 bv->owner()->view_state_changed();
113                 bv->owner()->message(_("Character set"));
114         }
115
116
117         /** Set the freefont using the contents of \param data dispatched from
118          *  the frontends and apply it at the current cursor location.
119          */
120         void update_and_apply_freefont(BufferView * bv, LyXText * text,
121                 string const & data)
122         {
123                 LyXFont font;
124                 bool toggle;
125                 if (bv_funcs::string2font(data, font, toggle)) {
126                         freefont = font;
127                         toggleall = toggle;
128                         apply_freefont(bv, text);
129                 }
130         }
131
132
133         void emph(BufferView * bv, LyXText * text)
134         {
135                 LyXFont font(LyXFont::ALL_IGNORE);
136                 font.setEmph(LyXFont::TOGGLE);
137                 toggleAndShow(bv, text, font);
138         }
139
140
141         void bold(BufferView * bv, LyXText * text)
142         {
143                 LyXFont font(LyXFont::ALL_IGNORE);
144                 font.setSeries(LyXFont::BOLD_SERIES);
145                 toggleAndShow(bv, text, font);
146         }
147
148
149         void noun(BufferView * bv, LyXText * text)
150         {
151                 LyXFont font(LyXFont::ALL_IGNORE);
152                 font.setNoun(LyXFont::TOGGLE);
153                 toggleAndShow(bv, text, font);
154         }
155
156
157         void lang(BufferView * bv, string const & l, LyXText * text)
158         {
159                 Language const * lang = languages.getLanguage(l);
160                 if (!lang)
161                         return;
162
163                 LyXFont font(LyXFont::ALL_IGNORE);
164                 font.setLanguage(lang);
165                 toggleAndShow(bv, text, font);
166         }
167
168
169         void code(BufferView * bv, LyXText * text)
170         {
171                 LyXFont font(LyXFont::ALL_IGNORE);
172                 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
173                 toggleAndShow(bv, text, font);
174         }
175
176
177         void sans(BufferView * bv, LyXText * text)
178         {
179                 LyXFont font(LyXFont::ALL_IGNORE);
180                 font.setFamily(LyXFont::SANS_FAMILY);
181                 toggleAndShow(bv, text, font);
182         }
183
184
185         void roman(BufferView * bv, LyXText * text)
186         {
187                 LyXFont font(LyXFont::ALL_IGNORE);
188                 font.setFamily(LyXFont::ROMAN_FAMILY);
189                 toggleAndShow(bv, text, font);
190         }
191
192
193         void styleReset(BufferView * bv, LyXText * text)
194         {
195                 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
196                 toggleAndShow(bv, text, font);
197         }
198
199
200         void underline(BufferView * bv, LyXText * text)
201         {
202                 LyXFont font(LyXFont::ALL_IGNORE);
203                 font.setUnderbar(LyXFont::TOGGLE);
204                 toggleAndShow(bv, text, font);
205         }
206
207
208         void fontSize(BufferView * bv, string const & size, LyXText * text)
209         {
210                 LyXFont font(LyXFont::ALL_IGNORE);
211                 font.setLyXSize(size);
212                 toggleAndShow(bv, text, font);
213         }
214
215
216         void moveCursor(LCursor & cur, bool selecting)
217         {
218                 if (selecting || cur.mark())
219                         cur.setSelection();
220                 if (!cur.selection())
221                         cur.bv().haveSelection(false);
222                 cur.bv().switchKeyMap();
223         }
224
225
226         void finishChange(BufferView * bv, bool selecting = false)
227         {
228                 finishUndo();
229                 moveCursor(bv->cursor(), selecting);
230                 bv->owner()->view_state_changed();
231         }
232
233 } // anon namespace
234
235
236 namespace bv_funcs {
237
238 string const freefont2string()
239 {
240         string data;
241         if (font2string(freefont, toggleall, data))
242                 return data;
243         return string();
244 }
245
246 }
247
248
249 InsetBase * LyXText::checkInsetHit(int x, int y)
250 {
251         ParagraphList::iterator pit;
252         ParagraphList::iterator end;
253
254         getParsInRange(paragraphs(),
255                        bv()->top_y(),
256                        bv()->top_y() + bv()->workHeight(),
257                        pit, end);
258
259         lyxerr << "checkInsetHit: x: " << x << " y: " << y << endl;
260         for ( ; pit != end; ++pit) {
261                 InsetList::iterator iit = pit->insetlist.begin();
262                 InsetList::iterator iend = pit->insetlist.end();
263                 for ( ; iit != iend; ++iit) {
264                         InsetBase * inset = iit->inset;
265 #if 1
266                         lyxerr << "examining inset " << inset
267                         //<< " xo/yo: " << inset->xo() << "/" << inset->yo()
268                                 << " xo: " << inset->xo() << "..." << inset->xo() + inset->width()
269                                 << " yo: " << inset->yo() - inset->ascent() << "..."
270                                 << inset->yo() + inset->descent() << endl;
271 #endif
272                         if (inset->covers(x, y - bv()->top_y())) {
273                                 lyxerr << "Hit inset: " << inset << endl;
274                                 return inset;
275                         }
276                 }
277         }
278         lyxerr << "No inset hit. " << endl;
279         return 0;
280 }
281
282
283 bool LyXText::gotoNextInset(vector<InsetOld_code> const & codes,
284                             string const & contents)
285 {
286         ParagraphList::iterator end = paragraphs().end();
287         ParagraphList::iterator pit = cursorPar();
288         pos_type pos = cursor().pos();
289
290         InsetBase * inset;
291         do {
292                 if (pos + 1 < pit->size()) {
293                         ++pos;
294                 } else  {
295                         ++pit;
296                         pos = 0;
297                 }
298
299         } while (pit != end &&
300                  !(pit->isInset(pos) &&
301                    (inset = pit->getInset(pos)) != 0 &&
302                    find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
303                    (contents.empty() ||
304                     static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
305                     == contents)));
306
307         if (pit == end)
308                 return false;
309
310         setCursor(parOffset(pit), pos, false);
311         return true;
312 }
313
314
315 void LyXText::gotoInset(vector<InsetOld_code> const & codes, bool same_content)
316 {
317         LCursor & cur = bv()->cursor();
318         cur.clearSelection();
319
320         string contents;
321         if (same_content
322             && cur.pos() < cur.lastpos()
323             && cur.paragraph().isInset(cur.pos())) {
324                 InsetBase const * inset = cur.paragraph().getInset(cur.pos());
325                 if (find(codes.begin(), codes.end(), inset->lyxCode())
326                     != codes.end())
327                         contents = static_cast<InsetCommand const *>(inset)->getContents();
328         }
329
330         if (!gotoNextInset(codes, contents)) {
331                 if (cur.pos() || cur.par() != 0) {
332                         CursorSlice tmp = cursor();
333                         cur.par() = 0;
334                         cur.pos() = 0;
335                         if (!gotoNextInset(codes, contents)) {
336                                 cursor() = tmp;
337                                 cur.bv().owner()->message(_("No more insets"));
338                         }
339                 } else {
340                         cur.bv().owner()->message(_("No more insets"));
341                 }
342         }
343         cur.bv().update();
344         cur.resetAnchor();
345 }
346
347
348 void LyXText::gotoInset(InsetOld_code code, bool same_content)
349 {
350         gotoInset(vector<InsetOld_code>(1, code), same_content);
351 }
352
353
354 void LyXText::cursorPrevious()
355 {
356         LCursor & cur = bv()->cursor();
357         pos_type cpos = cur.pos();
358         lyx::paroffset_type cpar = cur.par();
359
360         int x = bv()->cursor().x_target();
361         int y = bv()->top_y();
362         setCursorFromCoordinates(x, y);
363
364         if (cpar == cur.par() && cpos == cur.pos()) {
365                 // we have a row which is taller than the workarea. The
366                 // simplest solution is to move to the previous row instead.
367                 cursorUp(true);
368         }
369
370         bv()->updateScrollbar();
371         finishUndo();
372 }
373
374
375 void LyXText::cursorNext()
376 {
377         LCursor & cur = bv()->cursor();
378         pos_type cpos = cur.pos();
379         lyx::paroffset_type cpar = cur.par();
380
381         int x = cur.x_target();
382         int y = bv()->top_y() + bv()->workHeight();
383         setCursorFromCoordinates(x, y);
384
385         if (cpar == cur.par() && cpos == cur.pos()) {
386                 // we have a row which is taller than the workarea. The
387                 // simplest solution is to move to the next row instead.
388                 cursorDown(true);
389         }
390
391         bv()->updateScrollbar();
392         finishUndo();
393 }
394
395
396 namespace {
397
398 void specialChar(LyXText * text, BufferView * bv, InsetSpecialChar::Kind kind)
399 {
400         bv->update();
401         replaceSelection(text);
402         text->insertInset(new InsetSpecialChar(kind));
403         bv->update();
404 }
405
406
407 void doInsertInset(LyXText * text, BufferView * bv, FuncRequest const & cmd,
408         bool edit, bool pastesel)
409 {
410         InsetBase * inset = createInset(bv, cmd);
411         if (!inset)
412                 return;
413
414         bool gotsel = false;
415         if (bv->cursor().selection()) {
416                 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
417                 gotsel = true;
418         }
419         text->insertInset(inset);
420         if (edit)
421                 inset->edit(bv->cursor(), true);
422         if (gotsel && pastesel)
423                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
424 }
425
426 } // anon namespace
427
428
429 void LyXText::number()
430 {
431         LyXFont font(LyXFont::ALL_IGNORE);
432         font.setNumber(LyXFont::TOGGLE);
433         toggleAndShow(bv(), this, font);
434 }
435
436
437 bool LyXText::rtl() const
438 {
439         return cursorPar()->isRightToLeftPar(bv()->buffer()->params());
440 }
441
442
443 DispatchResult LyXText::dispatch(LCursor & cur, FuncRequest const & cmd)
444 {
445         lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
446         //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
447
448         BufferView * bv = &cur.bv();
449
450         switch (cmd.action) {
451
452         case LFUN_APPENDIX: {
453                 ParagraphList::iterator pit = cursorPar();
454                 bool start = !pit->params().startOfAppendix();
455
456                 // ensure that we have only one start_of_appendix in this document
457                 ParagraphList::iterator tmp = paragraphs().begin();
458                 ParagraphList::iterator end = paragraphs().end();
459
460                 for (; tmp != end; ++tmp) {
461                         if (tmp->params().startOfAppendix()) {
462                                 recUndo(parOffset(tmp));
463                                 tmp->params().startOfAppendix(false);
464                                 redoParagraph(tmp);
465                                 break;
466                         }
467                 }
468
469                 recUndo(parOffset(pit));
470                 pit->params().startOfAppendix(start);
471
472                 // we can set the refreshing parameters now
473                 updateCounters();
474                 redoParagraph(cursorPar());
475                 bv->update();
476                 break;
477         }
478
479         case LFUN_DELETE_WORD_FORWARD:
480                 cur.clearSelection();
481                 deleteWordForward();
482                 finishChange(bv);
483                 break;
484
485         case LFUN_DELETE_WORD_BACKWARD:
486                 cur.clearSelection();
487                 deleteWordBackward();
488                 finishChange(bv);
489                 break;
490
491         case LFUN_DELETE_LINE_FORWARD:
492                 cur.clearSelection();
493                 deleteLineForward();
494                 finishChange(bv);
495                 break;
496
497         case LFUN_WORDRIGHT:
498                 if (!cur.mark())
499                         cur.clearSelection();
500                 if (rtl())
501                         cursorLeftOneWord();
502                 else
503                         cursorRightOneWord();
504                 finishChange(bv);
505                 break;
506
507         case LFUN_WORDLEFT:
508                 if (!cur.mark())
509                         cur.clearSelection();
510                 if (rtl())
511                         cursorRightOneWord();
512                 else
513                         cursorLeftOneWord();
514                 finishChange(bv);
515                 break;
516
517         case LFUN_BEGINNINGBUF:
518                 if (!cur.mark())
519                         cur.clearSelection();
520                 cursorTop();
521                 finishChange(bv);
522                 break;
523
524         case LFUN_ENDBUF:
525                 if (!cur.mark())
526                         cur.clearSelection();
527                 cursorBottom();
528                 finishChange(bv);
529                 break;
530
531         case LFUN_RIGHTSEL:
532                 if (!cur.selection())
533                         cur.resetAnchor();
534                 if (rtl())
535                         cursorLeft(true);
536                 else
537                         cursorRight(true);
538                 finishChange(bv, true);
539                 break;
540
541         case LFUN_LEFTSEL:
542                 if (!cur.selection())
543                         cur.resetAnchor();
544                 if (rtl())
545                         cursorRight(true);
546                 else
547                         cursorLeft(true);
548                 finishChange(bv, true);
549                 break;
550
551         case LFUN_UPSEL:
552                 if (!cur.selection())
553                         cur.resetAnchor();
554                 cursorUp(true);
555                 finishChange(bv, true);
556                 break;
557
558         case LFUN_DOWNSEL:
559                 if (!cur.selection())
560                         cur.resetAnchor();
561                 cursorDown(true);
562                 finishChange(bv, true);
563                 break;
564
565         case LFUN_UP_PARAGRAPHSEL:
566                 if (!cur.selection())
567                         cur.resetAnchor();
568                 cursorUpParagraph();
569                 finishChange(bv, true);
570                 break;
571
572         case LFUN_DOWN_PARAGRAPHSEL:
573                 if (!cur.selection())
574                         cur.resetAnchor();
575                 cursorDownParagraph();
576                 finishChange(bv, true);
577                 break;
578
579         case LFUN_PRIORSEL:
580                 if (!cur.selection())
581                         cur.resetAnchor();
582                 cursorPrevious();
583                 finishChange(bv, true);
584                 break;
585
586         case LFUN_NEXTSEL:
587                 if (!cur.selection())
588                         cur.resetAnchor();
589                 cursorNext();
590                 finishChange(bv, true);
591                 break;
592
593         case LFUN_HOMESEL:
594                 if (!cur.selection())
595                         cur.resetAnchor();
596                 cursorHome();
597                 finishChange(bv, true);
598                 break;
599
600         case LFUN_ENDSEL:
601                 if (!cur.selection())
602                         cur.resetAnchor();
603                 cursorEnd();
604                 finishChange(bv, true);
605                 break;
606
607         case LFUN_WORDRIGHTSEL:
608                 if (!cur.selection())
609                         cur.resetAnchor();
610                 if (rtl())
611                         cursorLeftOneWord();
612                 else
613                         cursorRightOneWord();
614                 finishChange(bv, true);
615                 break;
616
617         case LFUN_WORDLEFTSEL:
618                 if (!cur.selection())
619                         cur.resetAnchor();
620                 if (rtl())
621                         cursorRightOneWord();
622                 else
623                         cursorLeftOneWord();
624                 finishChange(bv, true);
625                 break;
626
627         case LFUN_WORDSEL: {
628                 selectWord(lyx::WHOLE_WORD);
629                 finishChange(bv, true);
630                 break;
631         }
632
633         case LFUN_RIGHT:
634                 finishChange(bv);
635                 return moveRight();
636
637         case LFUN_LEFT:
638                 finishChange(bv);
639                 return moveLeft();
640
641         case LFUN_UP:
642                 finishChange(bv);
643                 return moveUp();
644
645         case LFUN_DOWN:
646                 finishChange(bv);
647                 return moveDown();
648
649         case LFUN_UP_PARAGRAPH:
650                 if (!cur.mark())
651                         cur.clearSelection();
652                 cursorUpParagraph();
653                 finishChange(bv);
654                 break;
655
656         case LFUN_DOWN_PARAGRAPH:
657                 if (!cur.mark())
658                         cur.clearSelection();
659                 cursorDownParagraph();
660                 finishChange(bv, false);
661                 break;
662
663         case LFUN_PRIOR:
664                 if (!cur.mark())
665                         cur.clearSelection();
666                 finishChange(bv, false);
667                 if (cur.par() == 0 && cursorRow() == firstRow())
668                         return DispatchResult(false, FINISHED_UP);
669                 cursorPrevious();
670                 break;
671
672         case LFUN_NEXT:
673                 if (!cur.mark())
674                         cur.clearSelection();
675                 finishChange(bv, false);
676                 if (cur.par() == cur.lastpar() && cursorRow() == lastRow())
677                         return DispatchResult(false, FINISHED_DOWN);
678                 cursorNext();
679                 break;
680
681         case LFUN_HOME:
682                 if (!cur.mark())
683                         cur.clearSelection();
684                 cursorHome();
685                 finishChange(bv, false);
686                 break;
687
688         case LFUN_END:
689                 if (!cur.mark())
690                         cur.clearSelection();
691                 cursorEnd();
692                 finishChange(bv, false);
693                 break;
694
695         case LFUN_BREAKLINE: {
696                 lyx::pos_type body = cursorPar()->beginOfBody();
697
698                 // Not allowed by LaTeX (labels or empty par)
699                 if (cursor().pos() <= body)
700                         break;
701
702                 replaceSelection(bv->getLyXText());
703                 insertInset(new InsetNewline);
704                 moveCursor(cur, false);
705                 break;
706         }
707
708         case LFUN_DELETE:
709                 if (!cur.selection()) {
710                         Delete();
711                         cur.resetAnchor();
712                         // It is possible to make it a lot faster still
713                         // just comment out the line below...
714                 } else {
715                         cutSelection(true, false);
716                 }
717                 moveCursor(cur, false);
718                 bv->owner()->view_state_changed();
719                 break;
720
721         case LFUN_DELETE_SKIP:
722                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
723                 if (!cur.selection()) {
724                         if (cursor().pos() == cursorPar()->size()) {
725                                 cursorRight(bv);
726                                 cursorLeft(bv);
727                                 Delete();
728                                 cur.resetAnchor();
729                         } else {
730                                 Delete();
731                                 cur.resetAnchor();
732                         }
733                 } else {
734                         cutSelection(true, false);
735                 }
736                 bv->update();
737                 break;
738
739
740         case LFUN_BACKSPACE:
741                 if (!cur.selection()) {
742                         if (bv->owner()->getIntl().getTransManager().backspace()) {
743                                 backspace();
744                                 cur.resetAnchor();
745                                 // It is possible to make it a lot faster still
746                                 // just comment out the line below...
747                         }
748                 } else {
749                         cutSelection(true, false);
750                 }
751                 bv->owner()->view_state_changed();
752                 bv->switchKeyMap();
753                 bv->update();
754                 break;
755
756         case LFUN_BACKSPACE_SKIP:
757                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
758                 if (!cur.selection()) {
759 #warning look here
760                         //CursorSlice cur = cursor();
761                         backspace();
762                         //anchor() = cur;
763                 } else {
764                         cutSelection(true, false);
765                 }
766                 bv->update();
767                 break;
768
769         case LFUN_BREAKPARAGRAPH:
770                 replaceSelection(bv->getLyXText());
771                 breakParagraph(bv->buffer()->paragraphs(), 0);
772                 bv->update();
773                 cur.resetAnchor();
774                 bv->switchKeyMap();
775                 bv->owner()->view_state_changed();
776                 break;
777
778         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
779                 replaceSelection(bv->getLyXText());
780                 breakParagraph(bv->buffer()->paragraphs(), 1);
781                 bv->update();
782                 cur.resetAnchor();
783                 bv->switchKeyMap();
784                 bv->owner()->view_state_changed();
785                 break;
786
787         case LFUN_BREAKPARAGRAPH_SKIP: {
788                 // When at the beginning of a paragraph, remove
789                 // indentation and add a "defskip" at the top.
790                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
791 #warning look here
792 //              CursorSlice cur = cursor();
793                 replaceSelection(bv->getLyXText());
794                 if (cur.pos() == 0) {
795                         ParagraphParameters & params = getPar(cur.current())->params();
796                         setParagraph(
797                                         params.spacing(),
798                                         params.align(),
799                                         params.labelWidthString(), 1);
800                 } else {
801                         breakParagraph(bv->buffer()->paragraphs(), 0);
802                 }
803                 bv->update();
804 //      anchor() = cur;
805                 bv->switchKeyMap();
806                 bv->owner()->view_state_changed();
807                 break;
808         }
809
810         case LFUN_PARAGRAPH_SPACING: {
811                 ParagraphList::iterator pit = cursorPar();
812                 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
813                 float cur_value = 1.0;
814                 if (cur_spacing == Spacing::Other)
815                         cur_value = pit->params().spacing().getValue();
816
817                 istringstream is(cmd.argument);
818                 string tmp;
819                 is >> tmp;
820                 Spacing::Space new_spacing = cur_spacing;
821                 float new_value = cur_value;
822                 if (tmp.empty()) {
823                         lyxerr << "Missing argument to `paragraph-spacing'"
824                                << endl;
825                 } else if (tmp == "single") {
826                         new_spacing = Spacing::Single;
827                 } else if (tmp == "onehalf") {
828                         new_spacing = Spacing::Onehalf;
829                 } else if (tmp == "double") {
830                         new_spacing = Spacing::Double;
831                 } else if (tmp == "other") {
832                         new_spacing = Spacing::Other;
833                         float tmpval = 0.0;
834                         is >> tmpval;
835                         lyxerr << "new_value = " << tmpval << endl;
836                         if (tmpval != 0.0)
837                                 new_value = tmpval;
838                 } else if (tmp == "default") {
839                         new_spacing = Spacing::Default;
840                 } else {
841                         lyxerr << _("Unknown spacing argument: ")
842                                << cmd.argument << endl;
843                 }
844                 if (cur_spacing != new_spacing || cur_value != new_value) {
845                         pit->params().spacing(Spacing(new_spacing, new_value));
846                         redoParagraph();
847                         bv->update();
848                 }
849                 break;
850         }
851
852         case LFUN_INSET_APPLY: {
853                 string const name = cmd.getArg(0);
854                 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
855                 if (inset)
856                         inset->dispatch(cur, FuncRequest(LFUN_INSET_MODIFY, cmd.argument));
857                 else
858                         dispatch(cur, FuncRequest(LFUN_INSET_INSERT, cmd.argument));
859                 break;
860         }
861
862         case LFUN_INSET_INSERT: {
863                 InsetBase * inset = createInset(bv, cmd);
864                 if (inset)
865                         insertInset(inset);
866                 break;
867         }
868
869         case LFUN_INSET_SETTINGS:
870                 if (cur.inset() && cur.inset()->asUpdatableInset())
871                         cur.inset()->asUpdatableInset()->showInsetDialog(bv);
872                 break;
873
874         case LFUN_INSET_TOGGLE:
875                 cur.clearSelection();
876                 if (!toggleInset())
877                         return DispatchResult(false);
878                 bv->update();
879                 bv->switchKeyMap();
880                 break;
881
882         case LFUN_SPACE_INSERT:
883                 if (cursorPar()->layout()->free_spacing)
884                         insertChar(' ');
885                 else
886                         doInsertInset(this, bv, cmd, false, false);
887                 moveCursor(cur, false);
888                 break;
889
890         case LFUN_HYPHENATION:
891                 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
892                 break;
893
894         case LFUN_LIGATURE_BREAK:
895                 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
896                 break;
897
898         case LFUN_LDOTS:
899                 specialChar(this, bv, InsetSpecialChar::LDOTS);
900                 break;
901
902         case LFUN_END_OF_SENTENCE:
903                 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
904                 break;
905
906         case LFUN_MENU_SEPARATOR:
907                 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
908                 break;
909
910         case LFUN_UPCASE_WORD:
911                 changeCase(LyXText::text_uppercase);
912                 bv->update();
913                 break;
914
915         case LFUN_LOWCASE_WORD:
916                 changeCase(LyXText::text_lowercase);
917                 bv->update();
918                 break;
919
920         case LFUN_CAPITALIZE_WORD:
921                 changeCase(LyXText::text_capitalization);
922                 bv->update();
923                 break;
924
925         case LFUN_TRANSPOSE_CHARS:
926                 recUndo(cursor().par());
927                 redoParagraph();
928                 bv->update();
929                 break;
930
931         case LFUN_PASTE:
932                 cur.message(_("Paste"));
933                 replaceSelection(bv->getLyXText());
934 #warning FIXME Check if the arg is in the domain of available selections.
935                 if (isStrUnsignedInt(cmd.argument))
936                         pasteSelection(strToUnsignedInt(cmd.argument));
937                 else
938                         pasteSelection(0);
939                 cur.clearSelection(); // bug 393
940                 bv->update();
941                 bv->switchKeyMap();
942                 finishUndo();
943                 break;
944
945         case LFUN_CUT:
946                 cutSelection(true, true);
947                 cur.message(_("Cut"));
948                 bv->update();
949                 break;
950
951         case LFUN_COPY:
952                 copySelection();
953                 cur.message(_("Copy"));
954                 break;
955
956         case LFUN_BEGINNINGBUFSEL:
957                 if (in_inset_)
958                         return DispatchResult(false);
959                 if (!cur.selection())
960                         cur.resetAnchor();
961                 cursorTop();
962                 finishChange(bv, true);
963                 break;
964
965         case LFUN_ENDBUFSEL:
966                 if (in_inset_)
967                         return DispatchResult(false);
968                 if (!cur.selection())
969                         cur.resetAnchor();
970                 cursorBottom();
971                 finishChange(bv, true);
972                 break;
973
974         case LFUN_GETXY:
975                 cur.message(tostr(cursorX(cur.current())) + ' '
976                           + tostr(cursorY(cur.current())));
977                 break;
978
979         case LFUN_SETXY: {
980                 int x = 0;
981                 int y = 0;
982                 istringstream is(cmd.argument);
983                 is >> x >> y;
984                 if (!is)
985                         lyxerr << "SETXY: Could not parse coordinates in '"
986                                << cmd.argument << std::endl;
987                 else
988                         setCursorFromCoordinates(x, y);
989                 break;
990         }
991
992         case LFUN_GETFONT:
993                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
994                         cur.message("E");
995                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
996                         cur.message("N");
997                 else
998                         cur.message("0");
999                 break;
1000
1001         case LFUN_GETLAYOUT:
1002                 cur.message(cursorPar()->layout()->name());
1003                 break;
1004
1005         case LFUN_LAYOUT: {
1006                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1007                   << cmd.argument << endl;
1008
1009                 // This is not the good solution to the empty argument
1010                 // problem, but it will hopefully suffice for 1.2.0.
1011                 // The correct solution would be to augument the
1012                 // function list/array with information about what
1013                 // functions needs arguments and their type.
1014                 if (cmd.argument.empty()) {
1015                         cur.errorMessage(_("LyX function 'layout' needs an argument."));
1016                         break;
1017                 }
1018
1019                 // Derive layout number from given argument (string)
1020                 // and current buffer's textclass (number)
1021                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1022                 bool hasLayout = tclass.hasLayout(cmd.argument);
1023                 string layout = cmd.argument;
1024
1025                 // If the entry is obsolete, use the new one instead.
1026                 if (hasLayout) {
1027                         string const & obs = tclass[layout]->obsoleted_by();
1028                         if (!obs.empty())
1029                                 layout = obs;
1030                 }
1031
1032                 if (!hasLayout) {
1033                         cur.errorMessage(string(N_("Layout ")) + cmd.argument +
1034                                 N_(" not known"));
1035                         break;
1036                 }
1037
1038                 bool change_layout = (current_layout != layout);
1039
1040                 if (!change_layout && cur.selection() &&
1041                         cur.selBegin().par() != cur.selEnd().par())
1042                 {
1043                         ParagraphList::iterator spit = getPar(cur.selBegin());
1044                         ParagraphList::iterator epit = boost::next(getPar(cur.selEnd()));
1045                         while (spit != epit) {
1046                                 if (spit->layout()->name() != current_layout) {
1047                                         change_layout = true;
1048                                         break;
1049                                 }
1050                                 ++spit;
1051                         }
1052                 }
1053
1054                 if (change_layout) {
1055                         current_layout = layout;
1056                         setLayout(layout);
1057                         bv->owner()->setLayout(layout);
1058                         bv->update();
1059                         bv->switchKeyMap();
1060                 }
1061                 break;
1062         }
1063
1064         case LFUN_PASTESELECTION: {
1065                 cur.clearSelection();
1066                 string const clip = bv->getClipboard();
1067                 if (!clip.empty()) {
1068                         if (cmd.argument == "paragraph")
1069                                 insertStringAsParagraphs(clip);
1070                         else
1071                                 insertStringAsLines(clip);
1072                         bv->update();
1073                 }
1074                 break;
1075         }
1076
1077         case LFUN_GOTOERROR:
1078                 gotoInset(InsetBase::ERROR_CODE, false);
1079                 break;
1080
1081         case LFUN_GOTONOTE:
1082                 gotoInset(InsetBase::NOTE_CODE, false);
1083                 break;
1084
1085         case LFUN_REFERENCE_GOTO: {
1086                 vector<InsetOld_code> tmp;
1087                 tmp.push_back(InsetBase::LABEL_CODE);
1088                 tmp.push_back(InsetBase::REF_CODE);
1089                 gotoInset(tmp, true);
1090                 break;
1091         }
1092
1093         case LFUN_QUOTE: {
1094                 replaceSelection(bv->getLyXText());
1095                 ParagraphList::iterator pit = cursorPar();
1096                 lyx::pos_type pos = cursor().pos();
1097                 char c;
1098                 if (!pos)
1099                         c = ' ';
1100                 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1101                         c = ' ';
1102                 else
1103                         c = pit->getChar(pos - 1);
1104
1105                 LyXLayout_ptr const & style = pit->layout();
1106
1107                 BufferParams const & bufparams = bv->buffer()->params();
1108                 if (style->pass_thru ||
1109                     pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew")
1110                   insertInset(new InsetQuotes(c, bufparams));
1111                 else
1112                         bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1113                 break;
1114         }
1115
1116         case LFUN_DATE_INSERT: {
1117                 replaceSelection(bv->getLyXText());
1118                 time_t now_time_t = time(NULL);
1119                 struct tm * now_tm = localtime(&now_time_t);
1120                 setlocale(LC_TIME, "");
1121                 string arg;
1122                 if (!cmd.argument.empty())
1123                         arg = cmd.argument;
1124                 else
1125                         arg = lyxrc.date_insert_format;
1126                 char datetmp[32];
1127                 int const datetmp_len =
1128                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1129
1130                 for (int i = 0; i < datetmp_len; i++)
1131                         insertChar(datetmp[i]);
1132
1133                 cur.resetAnchor();
1134                 moveCursor(cur, false);
1135                 break;
1136         }
1137
1138         case LFUN_MOUSE_TRIPLE:
1139                 if (bv->buffer() && cmd.button() == mouse_button::button1) {
1140                         selection_possible = true;
1141                         cursorHome();
1142                         cur.resetAnchor();
1143                         cursorEnd();
1144                         cur.setSelection();
1145                         bv->haveSelection(cur.selection());
1146                 }
1147                 break;
1148
1149         case LFUN_MOUSE_DOUBLE:
1150                 if (bv->buffer() && cmd.button() == mouse_button::button1) {
1151                         selection_possible = true;
1152                         selectWord(lyx::WHOLE_WORD_STRICT);
1153                         bv->haveSelection(cur.selection());
1154                 }
1155                 break;
1156
1157         case LFUN_MOUSE_MOTION: {
1158 #if 0
1159                 // Only use motion with button 1
1160                 //if (ev.button() != mouse_button::button1)
1161                 //      return false;
1162                 // don't set anchor_
1163                 bv->cursor().cursor_ = cur.cursor_;
1164
1165                 if (!bv->buffer())
1166                         break;
1167                 // The test for not selection possible is needed, that
1168                 // only motion events are used, where the bottom press
1169                 // event was on the drawing area too
1170                 if (!selection_possible) {
1171                         lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1172                                 "Dispatch: no selection possible\n";
1173                         break;
1174                 }
1175                 RowList::iterator cursorrow = cursorRow();
1176
1177                 setCursorFromCoordinates(cmd.x, cmd.y);
1178
1179                 // This is to allow jumping over large insets
1180                 // FIXME: shouldn't be top-text-specific
1181                 if (cursorrow == cursorRow() && !in_inset_) {
1182                         if (cmd.y - bv->top_y() >= bv->workHeight())
1183                                 cursorDown(true);
1184                         else if (cmd.y - bv->top_y() < 0)
1185                                 cursorUp(true);
1186                 }
1187                 cur.setSelection();
1188 #endif
1189                 break;
1190         }
1191
1192         // Single-click on work area
1193         case LFUN_MOUSE_PRESS: {
1194                 if (!bv->buffer())
1195                         break;
1196
1197                 // ok ok, this is a hack (for xforms)
1198                 // We shouldn't go further down as we really should only do the
1199                 // scrolling and be done with this. Otherwise we may open some
1200                 // dialogs (Jug 20020424).
1201                 if (cmd.button() == mouse_button::button4) {
1202                         bv->scroll(-lyxrc.wheel_jump);
1203                         break;
1204                 }
1205                 if (cmd.button() == mouse_button::button5) {
1206                         bv->scroll(lyxrc.wheel_jump);
1207                         break;
1208                 }
1209
1210                 // Middle button press pastes if we have a selection
1211                 // We do this here as if the selection was inside an inset
1212                 // it could get cleared on the unlocking of the inset so
1213                 // we have to check this first
1214                 bool paste_internally = false;
1215                 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1216                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1217                         paste_internally = true;
1218                 }
1219
1220                 selection_possible = true;
1221
1222                 // Clear the selection
1223                 cur.clearSelection();
1224
1225                 // Right click on a footnote flag opens float menu
1226                 if (cmd.button() == mouse_button::button3) {
1227                         selection_possible = false;
1228                         break;
1229                 }
1230
1231                 setCursorFromCoordinates(cur.current(), cmd.x, cmd.y);
1232                 cur.resetAnchor();
1233                 finishUndo();
1234                 cur.x_target() = cursorX(cur.current());
1235
1236                 // set cursor and anchor to this position
1237                 bv->cursor() = cur;
1238
1239                 if (bv->fitCursor())
1240                         selection_possible = false;
1241
1242                 // Insert primary selection with middle mouse
1243                 // if there is a local selection in the current buffer,
1244                 // insert this
1245                 if (cmd.button() == mouse_button::button2) {
1246                         if (paste_internally)
1247                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1248                         else
1249                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1250                         selection_possible = false;
1251                 }
1252                 break;
1253         }
1254
1255         case LFUN_MOUSE_RELEASE: {
1256                 if (!bv->buffer())
1257                         break;
1258
1259                 // do nothing if we used the mouse wheel
1260                 if (cmd.button() == mouse_button::button4
1261                  || cmd.button() == mouse_button::button5)
1262                         return DispatchResult(true, false);
1263
1264                 selection_possible = false;
1265
1266                 if (cmd.button() == mouse_button::button2)
1267                         break;
1268
1269                 // finish selection
1270                 if (cmd.button() == mouse_button::button1)
1271                         bv->haveSelection(cur.selection());
1272
1273                 bv->switchKeyMap();
1274                 bv->owner()->view_state_changed();
1275                 bv->owner()->updateMenubar();
1276                 bv->owner()->updateToolbar();
1277                 break;
1278         }
1279
1280         case LFUN_SELFINSERT: {
1281                 if (cmd.argument.empty())
1282                         break;
1283
1284                 // Automatically delete the currently selected
1285                 // text and replace it with what is being
1286                 // typed in now. Depends on lyxrc settings
1287                 // "auto_region_delete", which defaults to
1288                 // true (on).
1289
1290                 if (lyxrc.auto_region_delete) {
1291                         if (cur.selection())
1292                                 cutSelection(false, false);
1293                         bv->haveSelection(false);
1294                 }
1295
1296                 cur.clearSelection();
1297                 LyXFont const old_font = real_current_font;
1298
1299                 string::const_iterator cit = cmd.argument.begin();
1300                 string::const_iterator end = cmd.argument.end();
1301                 for (; cit != end; ++cit)
1302                         bv->owner()->getIntl().getTransManager().
1303                                 TranslateAndInsert(*cit, this);
1304
1305                 cur.resetAnchor();
1306                 moveCursor(cur, false);
1307
1308                 // real_current_font.number can change so we need to
1309                 // update the minibuffer
1310                 if (old_font != real_current_font)
1311                         bv->owner()->view_state_changed();
1312                 bv->updateScrollbar();
1313                 break;
1314         }
1315
1316         case LFUN_URL: {
1317                 InsetCommandParams p("url");
1318                 string const data = InsetCommandMailer::params2string("url", p);
1319                 bv->owner()->getDialogs().show("url", data, 0);
1320                 break;
1321         }
1322
1323         case LFUN_HTMLURL: {
1324                 InsetCommandParams p("htmlurl");
1325                 string const data = InsetCommandMailer::params2string("url", p);
1326                 bv->owner()->getDialogs().show("url", data, 0);
1327                 break;
1328         }
1329
1330         case LFUN_INSERT_LABEL: {
1331                 InsetCommandParams p("label");
1332                 string const data = InsetCommandMailer::params2string("label", p);
1333                 bv->owner()->getDialogs().show("label", data, 0);
1334                 break;
1335         }
1336
1337
1338 #if 0
1339         case LFUN_INSET_LIST:
1340         case LFUN_INSET_THEOREM:
1341         case LFUN_INSET_CAPTION:
1342 #endif
1343         case LFUN_INSERT_NOTE:
1344         case LFUN_INSERT_CHARSTYLE:
1345         case LFUN_INSERT_BOX:
1346         case LFUN_INSERT_BRANCH:
1347         case LFUN_INSERT_BIBITEM:
1348         case LFUN_INSET_ERT:
1349         case LFUN_INSET_FLOAT:
1350         case LFUN_INSET_FOOTNOTE:
1351         case LFUN_INSET_MARGINAL:
1352         case LFUN_INSET_OPTARG:
1353         case LFUN_INSET_WIDE_FLOAT:
1354         case LFUN_INSET_WRAP:
1355         case LFUN_TABULAR_INSERT:
1356         case LFUN_ENVIRONMENT_INSERT:
1357                 // Open the inset, and move the current selection
1358                 // inside it.
1359                 doInsertInset(this, bv, cmd, true, true);
1360                 break;
1361
1362         case LFUN_INDEX_INSERT:
1363                 // Just open the inset
1364                 doInsertInset(this, bv, cmd, true, false);
1365                 break;
1366
1367         case LFUN_INDEX_PRINT:
1368         case LFUN_TOC_INSERT:
1369         case LFUN_HFILL:
1370         case LFUN_INSERT_LINE:
1371         case LFUN_INSERT_PAGEBREAK:
1372                 // do nothing fancy
1373                 doInsertInset(this, bv, cmd, false, false);
1374                 break;
1375
1376         case LFUN_DEPTH_MIN:
1377                 bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH);
1378                 bv->update();
1379                 break;
1380
1381         case LFUN_DEPTH_PLUS:
1382                 bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH);
1383                 bv->update();
1384                 break;
1385
1386         case LFUN_MATH_DELIM:
1387         case LFUN_MATH_DISPLAY:
1388         case LFUN_INSERT_MATH:
1389         case LFUN_MATH_LIMITS:
1390         case LFUN_MATH_MACRO:
1391         case LFUN_MATH_MUTATE:
1392         case LFUN_MATH_SPACE:
1393         case LFUN_MATH_IMPORT_SELECTION:
1394         case LFUN_MATH_MODE:
1395         case LFUN_MATH_NONUMBER:
1396         case LFUN_MATH_NUMBER:
1397         case LFUN_MATH_EXTERN:
1398         case LFUN_MATH_SIZE:
1399                 mathDispatch(cur, cmd);
1400                 break;
1401
1402         case LFUN_EMPH:
1403                 emph(bv, this);
1404                 bv->owner()->view_state_changed();
1405                 break;
1406
1407         case LFUN_BOLD:
1408                 bold(bv, this);
1409                 bv->owner()->view_state_changed();
1410                 break;
1411
1412         case LFUN_NOUN:
1413                 noun(bv, this);
1414                 bv->owner()->view_state_changed();
1415                 break;
1416
1417         case LFUN_CODE:
1418                 code(bv, this);
1419                 bv->owner()->view_state_changed();
1420                 break;
1421
1422         case LFUN_SANS:
1423                 sans(bv, this);
1424                 bv->owner()->view_state_changed();
1425                 break;
1426
1427         case LFUN_ROMAN:
1428                 roman(bv, this);
1429                 bv->owner()->view_state_changed();
1430                 break;
1431
1432         case LFUN_DEFAULT:
1433                 styleReset(bv, this);
1434                 bv->owner()->view_state_changed();
1435                 break;
1436
1437         case LFUN_UNDERLINE:
1438                 underline(bv, this);
1439                 bv->owner()->view_state_changed();
1440                 break;
1441
1442         case LFUN_FONT_SIZE:
1443                 fontSize(bv, cmd.argument, this);
1444                 bv->owner()->view_state_changed();
1445                 break;
1446
1447         case LFUN_LANGUAGE:
1448                 lang(bv, cmd.argument, this);
1449                 bv->switchKeyMap();
1450                 bv->owner()->view_state_changed();
1451                 break;
1452
1453         case LFUN_FREEFONT_APPLY:
1454                 apply_freefont(bv, this);
1455                 break;
1456
1457         case LFUN_FREEFONT_UPDATE:
1458                 update_and_apply_freefont(bv, this, cmd.argument);
1459                 break;
1460
1461         case LFUN_FINISHED_LEFT:
1462                 lyxerr << "handle LFUN_FINISHED_LEFT" << endl;
1463                 cur.pop(cur.currentDepth());
1464                 cur.bv().cursor() = cur;
1465                 if (rtl())
1466                         cursorLeft(true);
1467                 break;
1468
1469         case LFUN_FINISHED_RIGHT:
1470                 lyxerr << "handle LFUN_FINISHED_RIGHT" << endl;
1471                 cur.pop(cur.currentDepth());
1472                 cur.bv().cursor() = cur;
1473                 if (!rtl())
1474                         cursorRight(true);
1475                 break;
1476
1477         case LFUN_FINISHED_UP:
1478                 lyxerr << "handle LFUN_FINISHED_UP" << endl;
1479                 cur.pop(cur.currentDepth());
1480                 cur.bv().cursor() = cur;
1481                 cursorUp(true);
1482                 break;
1483
1484         case LFUN_FINISHED_DOWN:
1485                 lyxerr << "handle LFUN_FINISHED_DOWN" << endl;
1486                 cur.pop(cur.currentDepth());
1487                 cur.bv().cursor() = cur;
1488                 cursorDown(true);
1489                 break;
1490
1491         default:
1492                 return DispatchResult(false);
1493         }
1494
1495         return DispatchResult(true, true);
1496 }