]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
setUndo2 interface change
[lyx.git] / src / BufferView.C
1 /**
2  * \file BufferView.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author unknown
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #include "BufferView.h"
13 #include "BufferView_pimpl.h"
14
15 #include "LaTeX.h"
16 #include "ParagraphParameters.h"
17 #include "WordLangTuple.h"
18 #include "buffer.h"
19 #include "bufferlist.h"
20 #include "debug.h"
21 #include "gettext.h"
22 #include "errorlist.h"
23 #include "iterators.h"
24 #include "language.h"
25 #include "lyxcursor.h"
26 #include "lyxlex.h"
27 #include "lyxtext.h"
28 #include "undo_funcs.h"
29 #include "changes.h"
30 #include "paragraph_funcs.h"
31
32 #include "frontends/Alert.h"
33 #include "frontends/Dialogs.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/WorkArea.h"
36 #include "frontends/screen.h"
37
38 #include "insets/insetcommand.h" // ChangeRefs
39 #include "insets/inseterror.h"
40 #include "insets/updatableinset.h"
41
42 #include "support/FileInfo.h"
43 #include "support/filetools.h"
44 #include "support/lyxfunctional.h" // equal_1st_in_pair
45 #include "support/types.h"
46 #include "support/lyxalgo.h" // lyx_count
47
48 #include <fstream>
49
50 extern BufferList bufferlist;
51
52 using lyx::pos_type;
53
54 using std::pair;
55 using std::endl;
56 using std::ifstream;
57 using std::vector;
58 using std::find;
59 using std::count_if;
60
61
62 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
63                        int width, int height)
64         : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
65 {
66         text = 0;
67 }
68
69
70 BufferView::~BufferView()
71 {
72         delete text;
73         delete pimpl_;
74 }
75
76
77 Buffer * BufferView::buffer() const
78 {
79         return pimpl_->buffer_;
80 }
81
82
83 LyXScreen & BufferView::screen() const
84 {
85         return pimpl_->screen();
86 }
87
88
89 LyXView * BufferView::owner() const
90 {
91         return pimpl_->owner_;
92 }
93
94
95 Painter & BufferView::painter() const
96 {
97         return pimpl_->painter();
98 }
99
100
101 void BufferView::buffer(Buffer * b)
102 {
103         pimpl_->buffer(b);
104 }
105
106
107 void BufferView::reload()
108 {
109         string const fn = buffer()->fileName();
110         if (bufferlist.close(buffer(), false))
111                 buffer(bufferlist.loadLyXFile(fn));
112 }
113
114
115 void BufferView::resize()
116 {
117         if (pimpl_->buffer_) {
118                 pimpl_->resizeCurrentBuffer();
119         }
120 }
121
122
123 void BufferView::repaint()
124 {
125         pimpl_->repaint();
126 }
127
128
129 bool BufferView::fitCursor()
130 {
131         return pimpl_->fitCursor();
132 }
133
134
135 void BufferView::update()
136 {
137         pimpl_->update();
138 }
139
140
141 void BufferView::updateScrollbar()
142 {
143         pimpl_->updateScrollbar();
144 }
145
146
147 void BufferView::scrollDocView(int value)
148 {
149         pimpl_->scrollDocView(value);
150 }
151
152
153 void BufferView::redoCurrentBuffer()
154 {
155         pimpl_->redoCurrentBuffer();
156 }
157
158
159 bool BufferView::available() const
160 {
161         return pimpl_->available();
162 }
163
164
165 Change const BufferView::getCurrentChange()
166 {
167         return pimpl_->getCurrentChange();
168 }
169
170
171 void BufferView::beforeChange(LyXText * text)
172 {
173         pimpl_->beforeChange(text);
174 }
175
176
177 void BufferView::savePosition(unsigned int i)
178 {
179         pimpl_->savePosition(i);
180 }
181
182
183 void BufferView::restorePosition(unsigned int i)
184 {
185         pimpl_->restorePosition(i);
186 }
187
188
189 bool BufferView::isSavedPosition(unsigned int i)
190 {
191         return pimpl_->isSavedPosition(i);
192 }
193
194
195 void BufferView::update(LyXText * text, UpdateCodes f)
196 {
197         pimpl_->update(text, f);
198 }
199
200
201 void BufferView::update(UpdateCodes f)
202 {
203         pimpl_->update(f);
204 }
205
206
207 void BufferView::switchKeyMap()
208 {
209         pimpl_->switchKeyMap();
210 }
211
212
213 void BufferView::insetUnlock()
214 {
215         pimpl_->insetUnlock();
216 }
217
218
219 int BufferView::workWidth() const
220 {
221         return pimpl_->workarea().workWidth();
222 }
223
224
225 void BufferView::toggleSelection(bool b)
226 {
227         pimpl_->toggleSelection(b);
228 }
229
230
231 void BufferView::toggleToggle()
232 {
233         pimpl_->toggleToggle();
234 }
235
236
237 void BufferView::center()
238 {
239         pimpl_->center();
240 }
241
242
243 string const BufferView::getClipboard() const
244 {
245         return pimpl_->workarea().getClipboard();
246 }
247
248
249 void BufferView::stuffClipboard(string const & stuff) const
250 {
251         pimpl_->stuffClipboard(stuff);
252 }
253
254
255 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
256                                   BufferView::UpdateCodes uc2)
257 {
258         return static_cast<BufferView::UpdateCodes>
259                 (static_cast<int>(uc1) | static_cast<int>(uc2));
260 }
261
262
263 bool BufferView::dispatch(FuncRequest const & ev)
264 {
265         return pimpl_->dispatch(ev);
266 }
267
268
269 void BufferView::scroll(int lines)
270 {
271         pimpl_->scroll(lines);
272 }
273
274
275 // Inserts a file into current document
276 bool BufferView::insertLyXFile(string const & filen)
277         //
278         // Copyright CHT Software Service GmbH
279         // Uwe C. Schroeder
280         //
281         // Insert a LyXformat - file into current buffer
282         //
283         // Moved from lyx_cb.C (Lgb)
284 {
285         if (filen.empty())
286                 return false;
287
288         string const fname = MakeAbsPath(filen);
289
290         // check if file exist
291         FileInfo const fi(fname);
292
293         if (!fi.readable()) {
294                 string const file = MakeDisplayPath(fname, 50);
295                 string const text =
296                         bformat(_("The specified document\n%1$s\ncould not be read."), file);
297                 Alert::error(_("Could not read document"), text);
298                 return false;
299         }
300
301         beforeChange(text);
302
303         ifstream ifs(fname.c_str());
304         if (!ifs) {
305                 string const file = MakeDisplayPath(fname, 50);
306                 string const text =
307                         bformat(_("Could not open the specified document %1$s\n"), file);
308                 Alert::error(_("Could not open file"), text);
309                 return false;
310         }
311
312         int const c = ifs.peek();
313
314         LyXLex lex(0, 0);
315         lex.setStream(ifs);
316
317         bool res = true;
318
319         if (c == '#') {
320                 // FIXME: huh ? No we won't !
321                 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
322                 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
323         } else {
324                 lyxerr[Debug::INFO] << "Will insert file without header"
325                                     << endl;
326                 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
327         }
328
329         resize();
330         return res;
331 }
332
333
334 void BufferView::resetErrorList()
335 {
336         pimpl_->errorlist_.clear();
337 }
338
339
340 void BufferView::setErrorList(ErrorList const & el)
341 {
342         pimpl_->errorlist_ = el;
343 }
344
345
346 void BufferView::addError(ErrorItem const & ei)
347 {
348         pimpl_->errorlist_.push_back(ei);
349
350 }
351
352
353 void BufferView::showErrorList(string const & action) const
354 {
355         if (getErrorList().size()) {
356                 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
357                 owner()->getDialogs().show("errorlist", title);
358         }
359 }
360
361
362 ErrorList const &
363 BufferView::getErrorList() const
364 {
365         return pimpl_->errorlist_;
366 }
367
368
369 void BufferView::setCursorFromRow(int row)
370 {
371         int tmpid = -1;
372         int tmppos = -1;
373
374         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
375
376         ParagraphList::iterator texrowpar;
377
378         if (tmpid == -1) {
379                 texrowpar = text->ownerParagraphs().begin();
380                 tmppos = 0;
381         } else {
382                 texrowpar = *buffer()->getParFromID(tmpid);
383         }
384         text->setCursor(texrowpar, tmppos);
385 }
386
387
388 bool BufferView::insertInset(Inset * inset, string const & lout)
389 {
390         return pimpl_->insertInset(inset, lout);
391 }
392
393
394 void BufferView::gotoLabel(string const & label)
395 {
396         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
397              it != buffer()->inset_iterator_end(); ++it) {
398                 vector<string> labels = it->getLabelList();
399                 if (find(labels.begin(),labels.end(),label)
400                      != labels.end()) {
401                         beforeChange(text);
402                         text->setCursor(it.getPar(), it.getPos());
403                         text->selection.cursor = text->cursor;
404                         update(text, BufferView::SELECT);
405                         return;
406                 }
407         }
408 }
409
410
411 void BufferView::undo()
412 {
413         if (!available())
414                 return;
415
416         owner()->message(_("Undo"));
417         beforeChange(text);
418         update(text, BufferView::SELECT);
419         if (!textUndo(this))
420                 owner()->message(_("No further undo information"));
421         else
422                 update(text, BufferView::SELECT);
423         switchKeyMap();
424 }
425
426
427 void BufferView::redo()
428 {
429         if (!available())
430                 return;
431
432         owner()->message(_("Redo"));
433         beforeChange(text);
434         update(text, BufferView::SELECT);
435         if (!textRedo(this))
436                 owner()->message(_("No further redo information"));
437         else
438                 update(text, BufferView::SELECT);
439         switchKeyMap();
440 }
441
442
443 // these functions are for the spellchecker
444 WordLangTuple const BufferView::nextWord(float & value)
445 {
446         if (!available()) {
447                 value = 1;
448                 return WordLangTuple();
449         }
450
451         return text->selectNextWordToSpellcheck(value);
452 }
453
454
455 void BufferView::selectLastWord()
456 {
457         if (!available())
458                 return;
459
460         LyXCursor cur = text->selection.cursor;
461         beforeChange(text);
462         text->selection.cursor = cur;
463         text->selectSelectedWord();
464         toggleSelection(false);
465         update(text, BufferView::SELECT);
466 }
467
468
469 void BufferView::endOfSpellCheck()
470 {
471         if (!available()) return;
472
473         beforeChange(text);
474         text->selectSelectedWord();
475         text->clearSelection();
476         update(text, BufferView::SELECT);
477 }
478
479
480 void BufferView::replaceWord(string const & replacestring)
481 {
482         if (!available())
483                 return;
484
485         LyXText * tt = getLyXText();
486         update(tt, BufferView::SELECT);
487
488         // clear the selection (if there is any)
489         toggleSelection(false);
490         update(tt, BufferView::SELECT);
491
492         // clear the selection (if there is any)
493         toggleSelection(false);
494         tt->replaceSelectionWithString(replacestring);
495
496         tt->setSelectionRange(replacestring.length());
497
498         // Go back so that replacement string is also spellchecked
499         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
500                 tt->cursorLeft(this);
501         }
502         update(tt, BufferView::SELECT);
503
504         // FIXME: should be done through LFUN
505         buffer()->markDirty();
506         fitCursor();
507 }
508 // End of spellchecker stuff
509
510
511 bool BufferView::lockInset(UpdatableInset * inset)
512 {
513         if (!inset)
514                 return false;
515         // don't relock if we're already locked
516         if (theLockingInset() == inset)
517                 return true;
518         if (!theLockingInset()) {
519                 // first check if it's the inset under the cursor we want lock
520                 // should be most of the time
521                 if (text->cursor.pos() < text->cursor.par()->size()
522                     && text->cursor.par()->getChar(text->cursor.pos()) ==
523                     Paragraph::META_INSET) {
524                         Inset * in = text->cursor.par()->getInset(text->cursor.pos());
525                         if (inset == in) {
526                                 theLockingInset(inset);
527                                 return true;
528                         }
529                 }
530                 // Then do a deep look of the inset and lock the right one
531                 int const id = inset->id();
532                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
533                 ParagraphList::iterator pend = buffer()->paragraphs.end();
534                 for (; pit != pend; ++pit) {
535                         InsetList::iterator it = pit->insetlist.begin();
536                         InsetList::iterator end = pit->insetlist.end();
537                         for (; it != end; ++it) {
538                                 if (it.getInset() == inset) {
539                                         text->setCursorIntern(pit, it.getPos());
540                                         theLockingInset(inset);
541                                         return true;
542                                 }
543                                 if (it.getInset()->getInsetFromID(id)) {
544                                         text->setCursorIntern(pit, it.getPos());
545                                         FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
546                                         it.getInset()->localDispatch(cmd);
547                                         return theLockingInset()->lockInsetInInset(this, inset);
548                                 }
549                         }
550                 }
551                 return false;
552         }
553         return theLockingInset()->lockInsetInInset(this, inset);
554 }
555
556
557 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
558 {
559         if (theLockingInset() && available()) {
560                 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
561                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
562                         updateScrollbar();
563                         return true;
564                 }
565         }
566         return false;
567 }
568
569
570 void BufferView::hideCursor()
571 {
572         screen().hideCursor();
573 }
574
575
576 int BufferView::unlockInset(UpdatableInset * inset)
577 {
578         if (!inset)
579                 return 0;
580         if (inset && theLockingInset() == inset) {
581                 inset->insetUnlock(this);
582                 theLockingInset(0);
583                 // make sure we update the combo !
584                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
585                 // Tell the paragraph dialog that we changed paragraph
586                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
587                 finishUndo();
588                 return 0;
589         } else if (inset && theLockingInset() &&
590                    theLockingInset()->unlockInsetInInset(this, inset)) {
591                 // Tell the paragraph dialog that we changed paragraph
592                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
593                 // owner inset has updated the layout combo
594                 finishUndo();
595                 return 0;
596         }
597         return 1;
598 }
599
600
601 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
602 {
603         if (!theLockingInset())
604                 return; // shouldn't happen
605         if (kind == Undo::EDIT) // in this case insets would not be stored!
606                 kind = Undo::FINISH;
607         setUndo(this, kind, text->cursor.par());
608 }
609
610
611 void BufferView::updateInset(Inset * inset)
612 {
613         pimpl_->updateInset(inset);
614 }
615
616
617 bool BufferView::ChangeInsets(Inset::Code code,
618                               string const & from, string const & to)
619 {
620         bool need_update = false;
621         LyXCursor cursor = text->cursor;
622         LyXCursor tmpcursor = cursor;
623         cursor.par(tmpcursor.par());
624         cursor.pos(tmpcursor.pos());
625
626         ParIterator end = buffer()->par_iterator_end();
627         for (ParIterator it = buffer()->par_iterator_begin();
628              it != end; ++it) {
629                 bool changed_inset = false;
630                 for (InsetList::iterator it2 = it->insetlist.begin();
631                      it2 != it->insetlist.end(); ++it2) {
632                         if (it2.getInset()->lyxCode() == code) {
633                                 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
634                                 if (inset->getContents() == from) {
635                                         inset->setContents(to);
636                                         changed_inset = true;
637                                 }
638                         }
639                 }
640                 if (changed_inset) {
641                         need_update = true;
642
643                         // FIXME
644
645                         // The test it.size()==1 was needed to prevent crashes.
646                         // How to set the cursor corretly when it.size()>1 ??
647                         if (it.size() == 1) {
648                                 text->setCursorIntern(*it, 0);
649                                 text->redoParagraphs(text->cursor,
650                                                      boost::next(text->cursor.par()));
651                                 text->fullRebreak();
652                         }
653                 }
654         }
655         text->setCursorIntern(cursor.par(), cursor.pos());
656         return need_update;
657 }
658
659
660 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
661 {
662         // Check if the label 'from' appears more than once
663         vector<string> labels = buffer()->getLabelList();
664
665         if (lyx::count(labels.begin(), labels.end(), from) > 1)
666                 return false;
667
668         return ChangeInsets(Inset::REF_CODE, from, to);
669 }
670
671
672 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
673 {
674         typedef pair<string, string> StringPair;
675
676         vector<StringPair> keys;
677         buffer()->fillWithBibKeys(keys);
678         if (count_if(keys.begin(), keys.end(),
679                      lyx::equal_1st_in_pair<StringPair>(from))
680             > 1)
681                 return false;
682
683         return ChangeInsets(Inset::CITE_CODE, from, to);
684 }
685
686
687 UpdatableInset * BufferView::theLockingInset() const
688 {
689         // If NULL is not allowed we should put an Assert here. (Lgb)
690         if (text)
691                 return text->the_locking_inset;
692         return 0;
693 }
694
695
696 void BufferView::theLockingInset(UpdatableInset * inset)
697 {
698         text->the_locking_inset = inset;
699 }
700
701
702 LyXText * BufferView::getLyXText() const
703 {
704         if (theLockingInset()) {
705                 LyXText * txt = theLockingInset()->getLyXText(this, true);
706                 if (txt)
707                         return txt;
708         }
709         return text;
710 }
711
712
713 LyXText * BufferView::getParentText(Inset * inset) const
714 {
715         if (inset->owner()) {
716                 LyXText * txt = inset->getLyXText(this);
717                 inset = inset->owner();
718                 while (inset && inset->getLyXText(this) == txt)
719                         inset = inset->owner();
720                 if (inset)
721                         return inset->getLyXText(this);
722         }
723         return text;
724 }
725
726
727 Language const * BufferView::getParentLanguage(Inset * inset) const
728 {
729         LyXText * text = getParentText(inset);
730         return text->cursor.par()->getFontSettings(buffer()->params,
731                                                    text->cursor.pos()).language();
732 }
733
734
735 Encoding const * BufferView::getEncoding() const
736 {
737         LyXText * t = getLyXText();
738         if (!t)
739                 return 0;
740
741         LyXCursor const & c = t->cursor;
742         LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
743                                               outerFont(c.par(), t->ownerParagraphs()));
744         return font.language()->encoding();
745 }
746
747
748 void BufferView::haveSelection(bool sel)
749 {
750         pimpl_->workarea().haveSelection(sel);
751 }
752
753
754 int BufferView::workHeight() const
755 {
756         return pimpl_->workarea().workHeight();
757 }