]> git.lyx.org Git - lyx.git/blob - src/text3.C
move tabular creation to the factory
[lyx.git] / src / text3.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2002 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "lyxtext.h"
14 #include "lyxrow.h"
15 #include "paragraph.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
18 #include "lyxrc.h"
19 #include "debug.h"
20 #include "bufferparams.h"
21 #include "buffer.h"
22 #include "ParagraphParameters.h"
23 #include "gettext.h"
24 #include "factory.h"
25 #include "intl.h"
26 #include "language.h"
27 #include "support/lstrings.h"
28 #include "frontends/LyXView.h"
29 #include "frontends/screen.h"
30 #include "frontends/WorkArea.h"
31 #include "insets/insetspecialchar.h"
32 #include "insets/insettext.h"
33 #include "insets/insetquotes.h"
34 #include "insets/insetcommand.h"
35 #include "undo_funcs.h"
36
37 #include <ctime>
38 #include <clocale>
39
40 using std::endl;
41
42 extern string current_layout;
43
44 namespace {
45
46         void moveCursorUpdate(BufferView * bv, bool selecting)
47         {
48                 LyXText * lt = bv->getLyXText();
49
50                 if (selecting || lt->selection.mark()) {
51                         lt->setSelection(bv);
52                         if (lt->bv_owner)
53                                 bv->toggleToggle();
54                         else
55                                 bv->updateInset(lt->inset_owner, false);
56                 }
57                 if (lt->bv_owner) {
58                         //if (fitcur)
59                         //      bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
60                         //else
61                                 bv->update(lt, BufferView::SELECT);
62                         bv->showCursor();
63                 } else if (bv->text->status() != LyXText::UNCHANGED) {
64                         bv->theLockingInset()->hideInsetCursor(bv);
65                         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
66                         bv->showCursor();
67                 }
68
69                 if (!lt->selection.set())
70                         bv->workarea().haveSelection(false);
71
72                 bv->switchKeyMap();
73         }
74
75
76         void finishChange(BufferView * bv, bool selecting = false)
77         {
78                 finishUndo();
79                 moveCursorUpdate(bv, selecting);
80                 bv->owner()->view_state_changed();
81         }
82
83 }
84
85
86 bool LyXText::gotoNextInset(BufferView * bv,
87         vector<Inset::Code> const & codes, string const & contents) const
88 {
89         LyXCursor res = cursor;
90         Inset * inset;
91         do {
92                 if (res.pos() < res.par()->size() - 1) {
93                         res.pos(res.pos() + 1);
94                 } else  {
95                         res.par(res.par()->next());
96                         res.pos(0);
97                 }
98
99         } while (res.par() &&
100                  !(res.par()->isInset(res.pos())
101                    && (inset = res.par()->getInset(res.pos())) != 0
102                    && find(codes.begin(), codes.end(), inset->lyxCode())
103                    != codes.end()
104                    && (contents.empty() ||
105                        static_cast<InsetCommand *>(
106                                                         res.par()->getInset(res.pos()))->getContents()
107                        == contents)));
108
109         if (res.par()) {
110                 setCursor(bv, res.par(), res.pos(), false);
111                 return true;
112         }
113         return false;
114 }
115
116
117 void LyXText::gotoInset(BufferView * bv, vector<Inset::Code> const & codes,
118                                   bool same_content)
119 {
120         bv->hideCursor();
121         bv->beforeChange(this);
122         update(bv, false);
123
124         string contents;
125         if (same_content && cursor.par()->isInset(cursor.pos())) {
126                 Inset const * inset = cursor.par()->getInset(cursor.pos());
127                 if (find(codes.begin(), codes.end(), inset->lyxCode())
128                     != codes.end())
129                         contents = static_cast<InsetCommand const *>(inset)->getContents();
130         }
131
132         if (!gotoNextInset(bv, codes, contents)) {
133                 if (cursor.pos() || cursor.par() != ownerParagraph()) {
134                         LyXCursor tmp = cursor;
135                         cursor.par(ownerParagraph());
136                         cursor.pos(0);
137                         if (!gotoNextInset(bv, codes, contents)) {
138                                 cursor = tmp;
139                                 bv->owner()->message(_("No more insets"));
140                         }
141                 } else {
142                         bv->owner()->message(_("No more insets"));
143                 }
144         }
145         update(bv, false);
146         selection.cursor = cursor;
147 }
148
149
150 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
151 {
152         gotoInset(bv, vector<Inset::Code>(1, code), same_content);
153 }
154
155
156 void LyXText::cursorPrevious(BufferView * bv)
157 {
158         if (!cursor.row()->previous()) {
159                 if (first_y > 0) {
160                         int new_y = bv->text->first_y - bv->workarea().workHeight();
161                         bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
162                         bv->updateScrollbar();
163                 }
164                 return;
165         }
166
167         int y = first_y;
168         Row * cursorrow = cursor.row();
169
170         setCursorFromCoordinates(bv, cursor.x_fix(), y);
171         finishUndo();
172
173         int new_y;
174         if (cursorrow == bv->text->cursor.row()) {
175                 // we have a row which is higher than the workarea so we leave the
176                 // cursor on the start of the row and move only the draw up as soon
177                 // as we move the cursor or do something while inside the row (it may
178                 // span several workarea-heights) we'll move to the top again, but this
179                 // is better than just jump down and only display part of the row.
180                 new_y = bv->text->first_y - bv->workarea().workHeight();
181         } else {
182                 if (inset_owner) {
183                         new_y = bv->text->cursor.iy()
184                                 + bv->theLockingInset()->insetInInsetY() + y
185                                 + cursor.row()->height()
186                                 - bv->workarea().workHeight() + 1;
187                 } else {
188                         new_y = cursor.y()
189                                 - cursor.row()->baseline()
190                                 + cursor.row()->height()
191                                 - bv->workarea().workHeight() + 1;
192                 }
193         }
194         bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
195         if (cursor.row()->previous()) {
196                 LyXCursor cur;
197                 setCursor(bv, cur, cursor.row()->previous()->par(),
198                                                 cursor.row()->previous()->pos(), false);
199                 if (cur.y() > first_y) {
200                         cursorUp(bv, true);
201                 }
202         }
203         bv->updateScrollbar();
204 }
205
206
207 void LyXText::cursorNext(BufferView * bv)
208 {
209         if (!cursor.row()->next()) {
210                 int y = cursor.y() - cursor.row()->baseline() +
211                         cursor.row()->height();
212                 if (y > int(first_y + bv->workarea().workHeight())) {
213                         bv->screen().draw(bv->text, bv,
214                                                   bv->text->first_y + bv->workarea().workHeight());
215                         bv->updateScrollbar();
216                 }
217                 return;
218         }
219
220         int y = first_y + bv->workarea().workHeight();
221         if (inset_owner && !first_y) {
222                 y -= (bv->text->cursor.iy()
223                           - bv->text->first_y
224                           + bv->theLockingInset()->insetInInsetY());
225         }
226
227         getRowNearY(y);
228
229         Row * cursorrow = cursor.row();
230         setCursorFromCoordinates(bv, cursor.x_fix(), y);
231         // + workarea().workHeight());
232         finishUndo();
233
234         int new_y;
235         if (cursorrow == bv->text->cursor.row()) {
236                 // we have a row which is higher than the workarea so we leave the
237                 // cursor on the start of the row and move only the draw down as soon
238                 // as we move the cursor or do something while inside the row (it may
239                 // span several workarea-heights) we'll move to the top again, but this
240                 // is better than just jump down and only display part of the row.
241                 new_y = bv->text->first_y + bv->workarea().workHeight();
242         } else {
243                 if (inset_owner) {
244                         new_y = bv->text->cursor.iy()
245                                 + bv->theLockingInset()->insetInInsetY()
246                                 + y - cursor.row()->baseline();
247                 } else {
248                         new_y =  cursor.y() - cursor.row()->baseline();
249                 }
250         }
251         bv->screen().draw(bv->text, bv, new_y);
252         if (cursor.row()->next()) {
253                 LyXCursor cur;
254                 setCursor(bv, cur, cursor.row()->next()->par(),
255                                                 cursor.row()->next()->pos(), false);
256                 if (cur.y() < int(first_y + bv->workarea().workHeight())) {
257                         cursorDown(bv, true);
258                 }
259         }
260         bv->updateScrollbar();
261 }
262
263
264 void LyXText::update(BufferView * bv, bool changed)
265 {
266         BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
267         if (changed)
268                 bv->update(this, c | BufferView::CHANGE);
269         else
270                 bv->update(this, c);
271 }
272
273
274 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
275 {
276         bv->hideCursor();
277         lt->update(bv);
278         InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
279         if (!bv->insertInset(new_inset))
280                 delete new_inset;
281         else
282                 bv->updateInset(new_inset, true);
283 }
284
285
286 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
287 {
288         lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
289                               <<"] arg[" << cmd.argument << "]" << endl;
290
291         BufferView * bv = cmd.view();
292
293         switch (cmd.action) {
294
295         case LFUN_APPENDIX: {
296                 Paragraph * par = cursor.par();
297                 bool start = !par->params().startOfAppendix();
298
299                 // ensure that we have only one start_of_appendix in this document
300                 Paragraph * tmp = ownerParagraph();
301                 for (; tmp; tmp = tmp->next())
302                         tmp->params().startOfAppendix(false);
303
304                 par->params().startOfAppendix(start);
305
306                 // we can set the refreshing parameters now
307                 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
308                 refresh_y = 0;
309                 refresh_row = 0; // not needed for full update
310                 updateCounters(cmd.view());
311                 setCursor(cmd.view(), cursor.par(), cursor.pos());
312                 update(bv);
313                 break;
314         }
315
316         case LFUN_DELETE_WORD_FORWARD:
317                 bv->beforeChange(this);
318                 update(bv, false);
319                 deleteWordForward(bv);
320                 update(bv);
321                 finishChange(bv);
322                 break;
323
324         case LFUN_DELETE_WORD_BACKWARD:
325                 bv->beforeChange(this);
326                 update(bv, false);
327                 deleteWordBackward(bv);
328                 update(bv, true);
329                 finishChange(bv);
330                 break;
331
332         case LFUN_DELETE_LINE_FORWARD:
333                 bv->beforeChange(this);
334                 update(bv, false);
335                 deleteLineForward(bv);
336                 update(bv);
337                 finishChange(bv);
338                 break;
339
340         case LFUN_SHIFT_TAB:
341         case LFUN_TAB:
342                 if (!selection.mark())
343                         bv->beforeChange(this);
344                 update(bv, false);
345                 cursorTab(bv);
346                 finishChange(bv);
347                 break;
348
349         case LFUN_WORDRIGHT:
350                 if (!selection.mark())
351                         bv->beforeChange(this);
352                 update(bv, false);
353                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
354                         cursorLeftOneWord(bv);
355                 else
356                         cursorRightOneWord(bv);
357                 finishChange(bv);
358                 break;
359
360         case LFUN_WORDLEFT:
361                 if (!selection.mark())
362                         bv->beforeChange(this);
363                 update(bv, false);
364                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
365                         cursorRightOneWord(bv);
366                 else
367                         cursorLeftOneWord(bv);
368                 finishChange(bv);
369                 break;
370
371         case LFUN_BEGINNINGBUF:
372                 if (!selection.mark())
373                         bv->beforeChange(this);
374                 update(bv, false);
375                 cursorTop(bv);
376                 finishChange(bv);
377                 break;
378
379         case LFUN_ENDBUF:
380                 if (selection.mark())
381                         bv->beforeChange(this);
382                 update(bv, false);
383                 cursorBottom(bv);
384                 finishChange(bv);
385                 break;
386
387         case LFUN_RIGHTSEL:
388                 update(bv, false);
389                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
390                         cursorLeft(bv);
391                 else
392                         cursorRight(bv);
393                 finishChange(bv, true);
394                 break;
395
396         case LFUN_LEFTSEL:
397                 update(bv, false);
398                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
399                         cursorRight(bv);
400                 else
401                         cursorLeft(bv);
402                 finishChange(bv, true);
403                 break;
404
405         case LFUN_UPSEL:
406                 update(bv, false);
407                 cursorUp(bv, true);
408                 finishChange(bv, true);
409                 break;
410
411         case LFUN_DOWNSEL:
412                 update(bv, false);
413                 cursorDown(bv, true);
414                 finishChange(bv, true);
415                 break;
416
417         case LFUN_UP_PARAGRAPHSEL:
418                 update(bv, false);
419                 cursorUpParagraph(bv);
420                 finishChange(bv, true);
421                 break;
422
423         case LFUN_DOWN_PARAGRAPHSEL:
424                 update(bv, false);
425                 cursorDownParagraph(bv);
426                 finishChange(bv, true);
427                 break;
428
429         case LFUN_PRIORSEL:
430                 update(bv, false);
431                 cursorPrevious(bv);
432                 finishChange(bv, true);
433                 break;
434
435         case LFUN_NEXTSEL:
436                 update(bv, false);
437                 cursorNext(bv);
438                 finishChange(bv, true);
439                 break;
440
441         case LFUN_HOMESEL:
442                 update(bv, false);
443                 cursorHome(bv);
444                 finishChange(bv, true);
445                 break;
446
447         case LFUN_ENDSEL:
448                 update(bv, false);
449                 cursorEnd(bv);
450                 finishChange(bv, true);
451                 break;
452
453         case LFUN_WORDRIGHTSEL:
454                 update(bv, false);
455                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
456                         cursorLeftOneWord(bv);
457                 else
458                         cursorRightOneWord(bv);
459                 finishChange(bv, true);
460                 break;
461
462         case LFUN_WORDLEFTSEL:
463                 update(bv, false);
464                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
465                         cursorRightOneWord(bv);
466                 else
467                         cursorLeftOneWord(bv);
468                 finishChange(bv, true);
469                 break;
470
471         case LFUN_RIGHT: {
472                 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
473                 if (!selection.mark())
474                         bv->beforeChange(this);
475                 update(bv);
476                 if (is_rtl)
477                         cursorLeft(bv, false);
478                 if (cursor.pos() < cursor.par()->size()
479                     && cursor.par()->isInset(cursor.pos())
480                     && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
481                         Inset * tmpinset = cursor.par()->getInset(cursor.pos());
482                         cmd.message(tmpinset->editMessage());
483                         tmpinset->edit(bv, !is_rtl);
484                         break;
485                 }
486                 if (!is_rtl)
487                         cursorRight(bv, false);
488                 finishChange(bv);
489                 break;
490         }
491
492         case LFUN_LEFT: {
493                 // This is soooo ugly. Isn`t it possible to make
494                 // it simpler? (Lgb)
495                 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
496                 if (!selection.mark())
497                         bv->beforeChange(this);
498                 update(bv);
499                 LyXCursor const cur = cursor;
500                 if (!is_rtl)
501                         cursorLeft(bv, false);
502                 if ((is_rtl || cur != cursor) && // only if really moved!
503                     cursor.pos() < cursor.par()->size() &&
504                     cursor.par()->isInset(cursor.pos()) &&
505                     isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
506                         Inset * tmpinset = cursor.par()->getInset(cursor.pos());
507                         cmd.message(tmpinset->editMessage());
508                         tmpinset->edit(bv, is_rtl);
509                         break;
510                 }
511                 if (is_rtl)
512                         cursorRight(bv, false);
513                 finishChange(bv);
514                 break;
515         }
516
517         case LFUN_UP:
518                 if (!selection.mark())
519                         bv->beforeChange(this);
520                 bv->update(this, BufferView::UPDATE);
521                 cursorUp(bv);
522                 finishChange(bv);
523                 break;
524
525         case LFUN_DOWN:
526                 if (!selection.mark())
527                         bv->beforeChange(this);
528                 bv->update(this, BufferView::UPDATE);
529                 cursorDown(bv);
530                 finishChange(bv);
531                 break;
532
533         case LFUN_UP_PARAGRAPH:
534                 if (!selection.mark())
535                         bv->beforeChange(this);
536                 bv->update(this, BufferView::UPDATE);
537                 cursorUpParagraph(bv);
538                 finishChange(bv);
539                 break;
540
541         case LFUN_DOWN_PARAGRAPH:
542                 if (!selection.mark())
543                         bv->beforeChange(this);
544                 bv->update(this, BufferView::UPDATE);
545                 cursorDownParagraph(bv);
546                 finishChange(bv, false);
547                 break;
548
549         case LFUN_PRIOR:
550                 if (!selection.mark())
551                         bv->beforeChange(this);
552                 bv->update(this, BufferView::UPDATE);
553                 cursorPrevious(bv);
554                 finishChange(bv, false);
555                 // was:
556                 // finishUndo();
557                 // moveCursorUpdate(bv, false, false);
558                 // owner_->view_state_changed();
559                 break;
560
561         case LFUN_NEXT:
562                 if (!selection.mark())
563                         bv->beforeChange(this);
564                 bv->update(this, BufferView::UPDATE);
565                 cursorNext(bv);
566                 finishChange(bv, false);
567                 break;
568
569         case LFUN_HOME:
570                 if (!selection.mark())
571                         bv->beforeChange(this);
572                 update(bv);
573                 cursorHome(bv);
574                 finishChange(bv, false);
575                 break;
576
577         case LFUN_END:
578                 if (!selection.mark())
579                         bv->beforeChange(this);
580                 update(bv);
581                 cursorEnd(bv);
582                 finishChange(bv, false);
583                 break;
584
585         case LFUN_BREAKLINE:
586                 bv->beforeChange(this);
587                 insertChar(bv, Paragraph::META_NEWLINE);
588                 update(bv, true);
589                 setCursor(bv, cursor.par(), cursor.pos());
590                 moveCursorUpdate(bv, false);
591                 break;
592
593         case LFUN_DELETE:
594                 if (!selection.set()) {
595                         Delete(bv);
596                         selection.cursor = cursor;
597                         update(bv);
598                         // It is possible to make it a lot faster still
599                         // just comment out the line below...
600                         bv->showCursor();
601                 } else {
602                         update(bv, false);
603                         cutSelection(bv, true);
604                         update(bv);
605                 }
606                 moveCursorUpdate(bv, false);
607                 bv->owner()->view_state_changed();
608                 bv->switchKeyMap();
609                 break;
610
611         case LFUN_DELETE_SKIP:
612                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
613                 if (!selection.set()) {
614                         LyXCursor cur = cursor;
615                         if (cur.pos() == cur.par()->size()) {
616                                 cursorRight(bv);
617                                 cur = cursor;
618                                 if (cur.pos() == 0
619                                     && !(cur.par()->params().spaceTop()
620                                          == VSpace (VSpace::NONE))) {
621                                         setParagraph(bv,
622                                                  cur.par()->params().lineTop(),
623                                                  cur.par()->params().lineBottom(),
624                                                  cur.par()->params().pagebreakTop(),
625                                                  cur.par()->params().pagebreakBottom(),
626                                                  VSpace(VSpace::NONE),
627                                                  cur.par()->params().spaceBottom(),
628                                                  cur.par()->params().spacing(),
629                                                  cur.par()->params().align(),
630                                                  cur.par()->params().labelWidthString(), 0);
631                                         cursorLeft(bv);
632                                         update(bv);
633                                 } else {
634                                         cursorLeft(bv);
635                                         Delete(bv);
636                                         selection.cursor = cursor;
637                                 }
638                         } else {
639                                 Delete(bv);
640                                 selection.cursor = cursor;
641                         }
642                 } else {
643                         update(bv, false);
644                         cutSelection(bv, true);
645                 }
646                 update(bv);
647                 break;
648
649
650         case LFUN_BACKSPACE:
651                 if (!selection.set()) {
652                         if (bv->owner()->getIntl().getTransManager().backspace()) {
653                                 backspace(bv);
654                                 selection.cursor = cursor;
655                                 update(bv);
656                                 // It is possible to make it a lot faster still
657                                 // just comment out the line below...
658                                 bv->showCursor();
659                         }
660                 } else {
661                         update(bv, false);
662                         cutSelection(bv, true);
663                         update(bv);
664                 }
665                 bv->owner()->view_state_changed();
666                 bv->switchKeyMap();
667                 break;
668
669         case LFUN_BACKSPACE_SKIP:
670                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
671                 if (!selection.set()) {
672                         LyXCursor cur = cursor;
673                         if (cur.pos() == 0
674                             && !(cur.par()->params().spaceTop()
675                                  == VSpace (VSpace::NONE))) {
676                                 setParagraph(bv,
677                                          cur.par()->params().lineTop(),
678                                          cur.par()->params().lineBottom(),
679                                          cur.par()->params().pagebreakTop(),
680                                          cur.par()->params().pagebreakBottom(),
681                                          VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
682                                          cur.par()->params().spacing(),
683                                          cur.par()->params().align(),
684                                          cur.par()->params().labelWidthString(), 0);
685                         } else {
686                                 backspace(bv);
687                                 selection.cursor = cur;
688                         }
689                 } else {
690                         update(bv, false);
691                         cutSelection(bv, true);
692                 }
693                 update(bv);
694                 break;
695
696         case LFUN_BREAKPARAGRAPH:
697                 bv->beforeChange(this);
698                 breakParagraph(bv, 0);
699                 update(bv);
700                 selection.cursor = cursor;
701                 bv->switchKeyMap();
702                 bv->owner()->view_state_changed();
703                 break;
704
705         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
706                 bv->beforeChange(this);
707                 breakParagraph(bv, 1);
708                 update(bv);
709                 selection.cursor = cursor;
710                 bv->switchKeyMap();
711                 bv->owner()->view_state_changed();
712                 break;
713
714         case LFUN_BREAKPARAGRAPH_SKIP: {
715                 // When at the beginning of a paragraph, remove
716                 // indentation and add a "defskip" at the top.
717                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
718                 LyXCursor cur = cursor;
719                 bv->beforeChange(this);
720                 if (cur.pos() == 0) {
721                         if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
722                                 setParagraph(bv,
723                                          cur.par()->params().lineTop(),
724                                          cur.par()->params().lineBottom(),
725                                          cur.par()->params().pagebreakTop(),
726                                          cur.par()->params().pagebreakBottom(),
727                                          VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
728                                          cur.par()->params().spacing(),
729                                          cur.par()->params().align(),
730                                          cur.par()->params().labelWidthString(), 1);
731                                 //update(bv);
732                         }
733                 }
734                 else {
735                         breakParagraph(bv, 0);
736                         //update(bv);
737                 }
738                 update(bv);
739                 selection.cursor = cur;
740                 bv->switchKeyMap();
741                 bv->owner()->view_state_changed();
742                 break;
743         }
744
745         case LFUN_PARAGRAPH_SPACING: {
746                 Paragraph * par = cursor.par();
747                 Spacing::Space cur_spacing = par->params().spacing().getSpace();
748                 float cur_value = 1.0;
749                 if (cur_spacing == Spacing::Other)
750                         cur_value = par->params().spacing().getValue();
751
752                 istringstream is(cmd.argument.c_str());
753                 string tmp;
754                 is >> tmp;
755                 Spacing::Space new_spacing = cur_spacing;
756                 float new_value = cur_value;
757                 if (tmp.empty()) {
758                         lyxerr << "Missing argument to `paragraph-spacing'"
759                                << endl;
760                 } else if (tmp == "single") {
761                         new_spacing = Spacing::Single;
762                 } else if (tmp == "onehalf") {
763                         new_spacing = Spacing::Onehalf;
764                 } else if (tmp == "double") {
765                         new_spacing = Spacing::Double;
766                 } else if (tmp == "other") {
767                         new_spacing = Spacing::Other;
768                         float tmpval = 0.0;
769                         is >> tmpval;
770                         lyxerr << "new_value = " << tmpval << endl;
771                         if (tmpval != 0.0)
772                                 new_value = tmpval;
773                 } else if (tmp == "default") {
774                         new_spacing = Spacing::Default;
775                 } else {
776                         lyxerr << _("Unknown spacing argument: ")
777                                << cmd.argument << endl;
778                 }
779                 if (cur_spacing != new_spacing || cur_value != new_value) {
780                         par->params().spacing(Spacing(new_spacing, new_value));
781                         redoParagraph(bv);
782                         update(bv);
783                 }
784                 break;
785         }
786
787         case LFUN_INSET_TOGGLE:
788                 bv->hideCursor();
789                 bv->beforeChange(this);
790                 update(bv, false);
791                 toggleInset(bv);
792                 update(bv, false);
793                 bv->switchKeyMap();
794                 break;
795
796         case LFUN_PROTECTEDSPACE:
797                 if (cursor.par()->layout()->free_spacing) {
798                         insertChar(bv, ' ');
799                         update(bv);
800                 } else {
801                         specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
802                 }
803                 moveCursorUpdate(bv, false);
804                 break;
805
806         case LFUN_HYPHENATION:
807                 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
808                 break;
809
810         case LFUN_LIGATURE_BREAK:
811                 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
812                 break;
813
814         case LFUN_LDOTS:
815                 specialChar(this, bv, InsetSpecialChar::LDOTS);
816                 break;
817
818         case LFUN_HFILL:
819                 bv->hideCursor();
820                 update(bv, false);
821                 insertChar(bv, Paragraph::META_HFILL);
822                 update(bv);
823                 break;
824
825         case LFUN_END_OF_SENTENCE:
826                 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
827                 break;
828
829         case LFUN_MENU_SEPARATOR:
830                 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
831                 break;
832
833         case LFUN_MARK_OFF:
834                 bv->beforeChange(this);
835                 update(bv, false);
836                 selection.cursor = cursor;
837                 cmd.message(N_("Mark off"));
838                 break;
839
840         case LFUN_MARK_ON:
841                 bv->beforeChange(this);
842                 selection.mark(true);
843                 update(bv, false);
844                 selection.cursor = cursor;
845                 cmd.message(N_("Mark on"));
846                 break;
847
848         case LFUN_SETMARK:
849                 bv->beforeChange(this);
850                 if (selection.mark()) {
851                         update(bv);
852                         cmd.message(N_("Mark removed"));
853                 } else {
854                         selection.mark(true);
855                         update(bv);
856                         cmd.message(N_("Mark set"));
857                 }
858                 selection.cursor = cursor;
859                 break;
860
861         case LFUN_UPCASE_WORD:
862                 update(bv, false);
863                 changeCase(bv, LyXText::text_uppercase);
864                 if (inset_owner)
865                         bv->updateInset(inset_owner, true);
866                 update(bv);
867                 break;
868
869         case LFUN_LOWCASE_WORD:
870                 update(bv, false);
871                 changeCase(bv, LyXText::text_lowercase);
872                 if (inset_owner)
873                         bv->updateInset(inset_owner, true);
874                 update(bv);
875                 break;
876
877         case LFUN_CAPITALIZE_WORD:
878                 update(bv, false);
879                 changeCase(bv, LyXText::text_capitalization);
880                 if (inset_owner)
881                         bv->updateInset(inset_owner, true);
882                 update(bv);
883                 break;
884
885         case LFUN_TRANSPOSE_CHARS:
886                 update(bv, false);
887                 transposeChars(*bv);
888                 if (inset_owner)
889                         bv->updateInset(inset_owner, true);
890                 update(bv);
891                 break;
892
893         case LFUN_PASTE:
894                 cmd.message(_("Paste"));
895                 bv->hideCursor();
896                 // clear the selection
897                 bv->toggleSelection();
898                 clearSelection();
899                 update(bv, false);
900                 pasteSelection(bv);
901                 clearSelection(); // bug 393
902                 update(bv, false);
903                 update(bv);
904                 bv->switchKeyMap();
905                 break;
906
907         case LFUN_CUT:
908                 bv->hideCursor();
909                 update(bv, false);
910                 cutSelection(bv, true);
911                 update(bv);
912                 cmd.message(_("Cut"));
913                 break;
914
915         case LFUN_COPY:
916                 copySelection(bv);
917                 cmd.message(_("Copy"));
918                 break;
919
920         case LFUN_BEGINNINGBUFSEL:
921                 if (inset_owner)
922                         return Inset::UNDISPATCHED;
923                 update(bv, false);
924                 cursorTop(bv);
925                 finishChange(bv, true);
926                 break;
927
928         case LFUN_ENDBUFSEL:
929                 if (inset_owner)
930                         return Inset::UNDISPATCHED;
931                 update(bv, false);
932                 cursorBottom(bv);
933                 finishChange(bv, true);
934                 break;
935
936         case LFUN_GETXY:
937                 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
938                 break;
939
940         case LFUN_SETXY: {
941                 int x = 0;
942                 int y = 0;
943                 istringstream is(cmd.argument.c_str());
944                 is >> x >> y;
945                 if (!is)
946                         lyxerr << "SETXY: Could not parse coordinates in '"
947                                << cmd.argument << std::endl;
948                 else 
949                         setCursorFromCoordinates(bv, x, y);
950                 break;
951         }
952
953         case LFUN_GETFONT:
954                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
955                         cmd.message("E");
956                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
957                         cmd.message("N");
958                 else
959                         cmd.message("0");
960                 break;
961
962         case LFUN_GETLAYOUT:
963                 cmd.message(tostr(cursor.par()->layout()));
964                 break;
965
966         case LFUN_LAYOUT: {
967                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
968                   << cmd.argument << endl;
969
970                 // This is not the good solution to the empty argument
971                 // problem, but it will hopefully suffice for 1.2.0.
972                 // The correct solution would be to augument the
973                 // function list/array with information about what
974                 // functions needs arguments and their type.
975                 if (cmd.argument.empty()) {
976                         cmd.errorMessage(_("LyX function 'layout' needs an argument."));
977                         break;
978                 }
979
980                 // Derive layout number from given argument (string)
981                 // and current buffer's textclass (number)
982                 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
983                 bool hasLayout = tclass.hasLayout(cmd.argument);
984                 string layout = cmd.argument;
985
986                 // If the entry is obsolete, use the new one instead.
987                 if (hasLayout) {
988                         string const & obs = tclass[layout]->obsoleted_by();
989                         if (!obs.empty())
990                                 layout = obs;
991                 }
992
993                 if (!hasLayout) {
994                         cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
995                                 N_(" not known"));
996                         break;
997                 }
998
999                 bool change_layout = (current_layout != layout);
1000                 if (!change_layout && selection.set() &&
1001                         selection.start.par() != selection.end.par())
1002                 {
1003                         Paragraph * spar = selection.start.par();
1004                         Paragraph * epar = selection.end.par()->next();
1005                         while (spar != epar) {
1006                                 if (spar->layout()->name() != current_layout) {
1007                                         change_layout = true;
1008                                         break;
1009                                 }
1010                         }
1011                 }
1012                 if (change_layout) {
1013                         bv->hideCursor();
1014                         current_layout = layout;
1015                         update(bv, false);
1016                         setLayout(bv, layout);
1017                         bv->owner()->setLayout(layout);
1018                         update(bv);
1019                         bv->switchKeyMap();
1020                 }
1021                 break;
1022         }
1023
1024         case LFUN_PASTESELECTION: {
1025                 if (!bv->buffer())
1026                         break;
1027                 bv->hideCursor();
1028                 // this was originally a beforeChange(bv->text), i.e
1029                 // the outermost LyXText!
1030                 bv->beforeChange(this);
1031                 string const clip = bv->workarea().getClipboard();
1032                 if (!clip.empty()) {
1033                         if (cmd.argument == "paragraph")
1034                                 insertStringAsParagraphs(bv, clip);
1035                         else
1036                                 insertStringAsLines(bv, clip);
1037                         clearSelection();
1038                         update(bv);
1039                 }
1040                 break;
1041         }
1042
1043         case LFUN_GOTOERROR:
1044                 gotoInset(bv, Inset::ERROR_CODE, false);
1045                 break;
1046
1047         case LFUN_GOTONOTE:
1048                 gotoInset(bv, Inset::NOTE_CODE, false);
1049                 break;
1050
1051         case LFUN_REFERENCE_GOTO:
1052         {
1053                 vector<Inset::Code> tmp;
1054                 tmp.push_back(Inset::LABEL_CODE);
1055                 tmp.push_back(Inset::REF_CODE);
1056                 gotoInset(bv, tmp, true);
1057                 break;
1058         }
1059
1060 #if 0
1061         case LFUN_INSET_LIST:
1062         case LFUN_INSET_THEOREM:
1063 #endif
1064         case LFUN_INSERT_NOTE:
1065         case LFUN_INSET_ERT:
1066         case LFUN_INSET_EXTERNAL:
1067         case LFUN_INSET_FLOAT:
1068         case LFUN_INSET_FOOTNOTE:
1069         case LFUN_INSET_MARGINAL:
1070         case LFUN_INSET_MINIPAGE:
1071         case LFUN_INSET_OPTARG:
1072         case LFUN_INSET_WIDE_FLOAT:
1073         case LFUN_TABULAR_INSERT:
1074         {
1075                 Inset * inset = createInset(cmd);
1076                 if (inset) {
1077                         bool gotsel = false;
1078                         if (selection.set()) {
1079                                 cutSelection(bv, true, false);
1080                                 gotsel = true;
1081                         }
1082                         if (bv->insertInset(inset)) {
1083                                 inset->edit(bv);
1084                                 if (gotsel)
1085                                         bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1086                         }
1087                         else
1088                                 delete inset;
1089                 }
1090                 break;
1091         }
1092
1093
1094         case LFUN_QUOTE: {
1095                 Paragraph const * par = cursor.par();
1096                 lyx::pos_type pos = cursor.pos();
1097                 char c;
1098                 if (!pos)
1099                         c = ' ';
1100                 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1101                         c = ' ';
1102                 else
1103                         c = par->getChar(pos - 1);
1104
1105                 bv->hideCursor();
1106                 LyXLayout_ptr const & style = par->layout();
1107
1108                 if (style->pass_thru ||
1109                                 par->getFontSettings(bv->buffer()->params,
1110                                          pos).language()->lang() == "hebrew" ||
1111                         (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1112                         bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1113                 break;
1114         }
1115
1116         case LFUN_DATE_INSERT:  { // jdblair: date-insert cmd
1117                 time_t now_time_t = time(NULL);
1118                 struct tm * now_tm = localtime(&now_time_t);
1119                 setlocale(LC_TIME, "");
1120                 string arg;
1121                 if (!cmd.argument.empty())
1122                         arg = cmd.argument;
1123                 else
1124                         arg = lyxrc.date_insert_format;
1125                 char datetmp[32];
1126                 int const datetmp_len =
1127                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1128
1129                 for (int i = 0; i < datetmp_len; i++) {
1130                         insertChar(bv, datetmp[i]);
1131                         update(bv, true);
1132                 }
1133                 selection.cursor = cursor;
1134                 moveCursorUpdate(bv, false);
1135                 break;
1136         }
1137
1138         case LFUN_SELFINSERT: {
1139                 if (cmd.argument.empty())
1140                         break;
1141
1142                 // Automatically delete the currently selected
1143                 // text and replace it with what is being
1144                 // typed in now. Depends on lyxrc settings
1145                 // "auto_region_delete", which defaults to
1146                 // true (on).
1147
1148                 if (lyxrc.auto_region_delete) {
1149                         if (selection.set()) {
1150                                 cutSelection(bv, false, false);
1151                                 update(bv);
1152                         }
1153                         bv->workarea().haveSelection(false);
1154                 }
1155
1156                 bv->beforeChange(this);
1157                 LyXFont const old_font(real_current_font);
1158
1159                 string::const_iterator cit = cmd.argument.begin();
1160                 string::const_iterator end = cmd.argument.end();
1161                 for (; cit != end; ++cit)
1162                         bv->owner()->getIntl().getTransManager().
1163                                 TranslateAndInsert(*cit, this);
1164
1165                 update(bv);
1166                 selection.cursor = cursor;
1167                 moveCursorUpdate(bv, false);
1168
1169                 // real_current_font.number can change so we need to
1170                 // update the minibuffer
1171                 if (old_font != real_current_font)
1172                         bv->owner()->view_state_changed();
1173                 break;
1174         }
1175
1176         default:
1177                 return Inset::UNDISPATCHED;
1178         }
1179
1180         return Inset::DISPATCHED;
1181 }