]> git.lyx.org Git - lyx.git/blob - src/BufferView2.C
remove unused code
[lyx.git] / src / BufferView2.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "BufferView.h"
14 #include "buffer.h"
15 #include "lyxcursor.h"
16 #include "lyxtext.h"
17 #include "frontends/LyXView.h"
18 #include "bufferlist.h"
19 #include "frontends/screen.h"
20 #include "LaTeX.h"
21 #include "BufferView_pimpl.h"
22 #include "language.h"
23 #include "gettext.h"
24 #include "undo_funcs.h"
25 #include "debug.h"
26 #include "iterators.h"
27 #include "lyxlex.h"
28 #include "WordLangTuple.h"
29
30 #include "frontends/Alert.h"
31 #include "frontends/Dialogs.h"
32
33 #include "insets/insetcommand.h" //ChangeRefs
34 #include "insets/inseterror.h"
35
36 #include "support/FileInfo.h"
37 #include "support/filetools.h"
38 #include "support/lyxfunctional.h" //equal_1st_in_pair
39 #include "support/types.h"
40 #include "support/lyxalgo.h" // lyx_count
41
42 #include <fstream>
43
44 extern BufferList bufferlist;
45
46 using lyx::pos_type;
47
48 using std::pair;
49 using std::endl;
50 using std::ifstream;
51 using std::vector;
52 using std::find;
53 using std::count_if;
54
55
56 // Inserts a file into current document
57 bool BufferView::insertLyXFile(string const & filen)
58         //
59         // Copyright CHT Software Service GmbH
60         // Uwe C. Schroeder
61         //
62         // Insert a LyXformat - file into current buffer
63         //
64         // Moved from lyx_cb.C (Lgb)
65 {
66         if (filen.empty()) return false;
67
68         string const fname = MakeAbsPath(filen);
69
70         // check if file exist
71         FileInfo const fi(fname);
72
73         if (!fi.readable()) {
74                 Alert::alert(_("Error!"),
75                            _("Specified file is unreadable: "),
76                            MakeDisplayPath(fname, 50));
77                 return false;
78         }
79
80         beforeChange(text);
81
82         ifstream ifs(fname.c_str());
83         if (!ifs) {
84                 Alert::alert(_("Error!"),
85                            _("Cannot open specified file: "),
86                            MakeDisplayPath(fname, 50));
87                 return false;
88         }
89
90         int const c = ifs.peek();
91
92         LyXLex lex(0, 0);
93         lex.setStream(ifs);
94
95         bool res = true;
96
97         if (c == '#') {
98                 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
99                 res = buffer()->readFile(lex, text->cursor.par());
100         } else {
101                 lyxerr[Debug::INFO] << "Will insert file without header"
102                                     << endl;
103                 res = buffer()->readLyXformat2(lex, text->cursor.par());
104         }
105
106         resize();
107         return res;
108 }
109
110
111 bool BufferView::removeAutoInsets()
112 {
113         // keep track of which pos and par the cursor was on
114         Paragraph * cursor_par = text->cursor.par();
115         Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
116         Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
117         pos_type cursor_pos = text->cursor.pos();
118
119         bool found = false;
120
121         // Trap the deletion of the paragraph the cursor is in.
122         // Iterate until we find a paragraph that won't be immediately deleted.
123         // In reality this should mean we only execute the body of the while
124         // loop once at most.  However for safety we iterate rather than just
125         // make this an if () conditional.
126         while ((cursor_par_prev || cursor_par_next)
127                && text->setCursor(this,
128                                   cursor_par_prev ? cursor_par_prev : cursor_par_next,
129                                   0)) {
130                 // We just removed cursor_par so have to fix the "cursor"
131                 if (cursor_par_prev) {
132                         // '.' = cursor_par
133                         //  a -> a.
134                         // .
135                         cursor_par = cursor_par_prev;
136                         cursor_pos = cursor_par->size();
137                 } else {
138                         // .  -> .a
139                         //  a
140                         cursor_par = cursor_par_next;
141                         cursor_pos = 0;
142                 }
143                 cursor_par_prev = cursor_par->previous();
144                 cursor_par_next = cursor_par->next();
145         }
146
147         // Iterate through the paragraphs removing autoDelete insets as we go.
148         // If the paragraph ends up empty after all the autoDelete insets are
149         // removed that paragraph will be removed by the next setCursor() call.
150         ParIterator it = buffer()->par_iterator_begin();
151         ParIterator end = buffer()->par_iterator_end();
152         for (; it != end; ++it) {
153                 Paragraph * par = *it;
154                 Paragraph * par_prev = par ? par->previous() : 0;
155                 bool removed = false;
156
157                 if (text->setCursor(this, par, 0)
158                     && cursor_par == par_prev) {
159                         // The previous setCursor line was deleted and that
160                         // was the cursor_par line.  This can only happen if an
161                         // error box was the sole item on cursor_par.
162                         // It is possible for cursor_par_prev to be stray if
163                         // the line it pointed to only had a error box on it
164                         // so we have to set it to a known correct value.
165                         // This is often the same value it already had.
166                         cursor_par_prev = par->previous();
167                         if (cursor_par_prev) {
168                                 // '|' = par, '.' = cursor_par, 'E' = error box
169                                 // First step below may occur before while{}
170                                 //  a    |a      a     a     a.
171                                 //  E -> .E -> |.E -> .  -> |b
172                                 // .      b      b    |b
173                                 //  b
174                                 cursor_par = cursor_par_prev;
175                                 cursor_pos = cursor_par_prev->size();
176                                 cursor_par_prev = cursor_par->previous();
177                                 // cursor_par_next remains the same
178                         } else if (cursor_par_next) {
179                                 // First step below may occur before while{}
180                                 // .
181                                 //  E -> |.E -> |.  -> . -> .|a
182                                 //  a      a      a    |a
183                                 cursor_par = cursor_par_next;
184                                 cursor_pos = 0;
185                                 // cursor_par_prev remains unset
186                                 cursor_par_next = cursor_par->next();
187                         } else {
188                                 // I can't find a way to trigger this
189                                 // so it should be unreachable code
190                                 // unless the buffer is corrupted.
191                                 lyxerr << "BufferView::removeAutoInsets() is bad\n";
192                         }
193                 }
194
195                 InsetList::iterator pit = par->insetlist.begin();
196                 InsetList::iterator pend = par->insetlist.end();
197
198                 while (pit != pend) {
199                         if (pit.getInset()->autoDelete()) {
200                                 removed = true;
201                                 pos_type const pos = pit.getPos();
202
203                                 par->erase(pos);
204                                 // We just invalidated par's inset iterators so
205                                 // we get the next valid iterator position
206                                 pit = par->insetlist.insetIterator(pos);
207                                 // and ensure we have a valid end iterator.
208                                 pend = par->insetlist.end();
209
210                                 if (cursor_par == par) {
211                                         // update the saved cursor position
212                                         if (cursor_pos > pos)
213                                                 --cursor_pos;
214                                 }
215                         } else {
216                                 ++pit;
217                         }
218                 }
219                 if (removed) {
220                         found = true;
221                         text->redoParagraph(this);
222                 }
223         }
224
225         // It is possible that the last line is empty if it was cursor_par
226         // and/or only had an error inset on it.  So we set the cursor to the
227         // start of the doc to force its removal and ensure a valid saved cursor
228         if (text->setCursor(this, text->ownerParagraph(), 0)
229             && 0 == cursor_par_next) {
230                 cursor_par = cursor_par_prev;
231                 cursor_pos = cursor_par->size();
232         } else if (cursor_pos > cursor_par->size()) {
233                 // Some C-Enter lines were removed by the setCursor call which
234                 // then invalidated cursor_pos. It could still be "wrong" because
235                 // the cursor may appear to have jumped but since we collapsed
236                 // some C-Enter lines this should be a reasonable compromise.
237                 cursor_pos = cursor_par->size();
238         }
239
240         // restore the original cursor in its corrected location.
241         text->setCursorIntern(this, cursor_par, cursor_pos);
242
243         return found;
244 }
245
246
247 void BufferView::insertErrors(TeXErrors & terr)
248 {
249         // Save the cursor position
250         LyXCursor cursor = text->cursor;
251
252         TeXErrors::Errors::const_iterator cit = terr.begin();
253         TeXErrors::Errors::const_iterator end = terr.end();
254         for (; cit != end; ++cit) {
255                 string const desctext(cit->error_desc);
256                 string const errortext(cit->error_text);
257                 string const msgtxt = desctext + '\n' + errortext;
258                 int const errorrow = cit->error_in_line;
259
260                 // Insert error string for row number
261                 int tmpid = -1;
262                 int tmppos = -1;
263
264                 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
265                         buffer()->texrow.increasePos(tmpid, tmppos);
266                 }
267
268                 Paragraph * texrowpar = 0;
269
270                 if (tmpid == -1) {
271                         texrowpar = text->ownerParagraph();
272                         tmppos = 0;
273                 } else {
274                         texrowpar = buffer()->getParFromID(tmpid);
275                 }
276
277                 if (texrowpar == 0)
278                         continue;
279
280                 freezeUndo();
281                 InsetError * new_inset = new InsetError(msgtxt);
282                 text->setCursorIntern(this, texrowpar, tmppos);
283                 text->insertInset(this, new_inset);
284                 text->fullRebreak(this);
285                 unFreezeUndo();
286         }
287         // Restore the cursor position
288         text->setCursorIntern(this, cursor.par(), cursor.pos());
289 }
290
291
292 void BufferView::setCursorFromRow(int row)
293 {
294         int tmpid = -1;
295         int tmppos = -1;
296
297         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
298
299         Paragraph * texrowpar;
300
301         if (tmpid == -1) {
302                 texrowpar = text->ownerParagraph();
303                 tmppos = 0;
304         } else {
305                 texrowpar = buffer()->getParFromID(tmpid);
306         }
307         text->setCursor(this, texrowpar, tmppos);
308 }
309
310
311 bool BufferView::insertInset(Inset * inset, string const & lout)
312 {
313         return pimpl_->insertInset(inset, lout);
314 }
315
316
317 /* This is also a buffer property (ale) */
318 // Not so sure about that. a goto Label function can not be buffer local, just
319 // think how this will work in a multiwindo/buffer environment, all the
320 // cursors in all the views showing this buffer will move. (Lgb)
321 // OK, then no cursor action should be allowed in buffer. (ale)
322 bool BufferView::gotoLabel(string const & label)
323
324 {
325         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
326              it != buffer()->inset_iterator_end(); ++it) {
327                 vector<string> labels = (*it)->getLabelList();
328                 if (find(labels.begin(),labels.end(),label)
329                      != labels.end()) {
330                         beforeChange(text);
331                         text->setCursor(this, it.getPar(), it.getPos());
332                         text->selection.cursor = text->cursor;
333                         update(text, BufferView::SELECT|BufferView::FITCUR);
334                         return true;
335                 }
336         }
337         return false;
338 }
339
340
341 void BufferView::menuUndo()
342 {
343         if (!available())
344                 return;
345
346         owner()->message(_("Undo"));
347         hideCursor();
348         beforeChange(text);
349         update(text, BufferView::SELECT|BufferView::FITCUR);
350         if (!textUndo(this))
351                 owner()->message(_("No further undo information"));
352         else
353                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
354         switchKeyMap();
355 }
356
357
358 void BufferView::menuRedo()
359 {
360         if (!available())
361                 return;
362
363         owner()->message(_("Redo"));
364         hideCursor();
365         beforeChange(text);
366         update(text, BufferView::SELECT|BufferView::FITCUR);
367         if (!textRedo(this))
368                 owner()->message(_("No further redo information"));
369         else
370                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
371         switchKeyMap();
372 }
373
374
375 void BufferView::copyEnvironment()
376 {
377         if (available()) {
378                 text->copyEnvironmentType();
379                 owner()->message(_("Paragraph environment type copied"));
380         }
381 }
382
383
384 void BufferView::pasteEnvironment()
385 {
386         if (available()) {
387                 text->pasteEnvironmentType(this);
388                 owner()->message(_("Paragraph environment type set"));
389                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
390         }
391 }
392
393
394 void BufferView::copy()
395 {
396         if (available()) {
397                 getLyXText()->copySelection(this);
398                 owner()->message(_("Copy"));
399         }
400 }
401
402
403 void BufferView::cut(bool realcut)
404 {
405         if (available()) {
406                 hideCursor();
407                 update(text, BufferView::SELECT|BufferView::FITCUR);
408                 text->cutSelection(this, true, realcut);
409                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
410                 owner()->message(_("Cut"));
411         }
412 }
413
414
415 void BufferView::paste()
416 {
417         if (!available())
418                 return;
419
420         owner()->message(_("Paste"));
421
422         hideCursor();
423         // clear the selection
424         toggleSelection();
425         text->clearSelection();
426         update(text, BufferView::SELECT|BufferView::FITCUR);
427
428         // paste
429         text->pasteSelection(this);
430         // bug 393
431         text->clearSelection();
432         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
433 // why fake a selection only I think it should be a real one and not only
434 // a painted one (Jug 20020318).
435 #if 0
436         // clear the selection
437         toggleSelection();
438         text->clearSelection();
439         update(text, BufferView::SELECT|BufferView::FITCUR);
440 #endif
441 }
442
443
444 /* these functions are for the spellchecker */
445 WordLangTuple const BufferView::nextWord(float & value)
446 {
447         if (!available()) {
448                 value = 1;
449                 return WordLangTuple();
450         }
451
452         return text->selectNextWordToSpellcheck(this, value);
453 }
454
455
456 void BufferView::selectLastWord()
457 {
458         if (!available()) return;
459
460         LyXCursor cur = text->selection.cursor;
461         hideCursor();
462         beforeChange(text);
463         text->selection.cursor = cur;
464         text->selectSelectedWord(this);
465         toggleSelection(false);
466         update(text, BufferView::SELECT|BufferView::FITCUR);
467 }
468
469
470 void BufferView::endOfSpellCheck()
471 {
472         if (!available()) return;
473
474         hideCursor();
475         beforeChange(text);
476         text->selectSelectedWord(this);
477         text->clearSelection();
478         update(text, BufferView::SELECT|BufferView::FITCUR);
479 }
480
481
482 void BufferView::replaceWord(string const & replacestring)
483 {
484         if (!available()) return;
485
486         LyXText * tt = getLyXText();
487         hideCursor();
488         update(tt, BufferView::SELECT|BufferView::FITCUR);
489
490         /* clear the selection (if there is any) */
491         toggleSelection(false);
492         update(tt, BufferView::SELECT|BufferView::FITCUR);
493
494         /* clear the selection (if there is any) */
495         toggleSelection(false);
496         tt->replaceSelectionWithString(this, replacestring);
497
498         tt->setSelectionOverString(this, replacestring);
499
500         // Go back so that replacement string is also spellchecked
501         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
502                 tt->cursorLeft(this);
503         }
504         update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
505 }
506 // End of spellchecker stuff
507
508
509 bool BufferView::lockInset(UpdatableInset * inset)
510 {
511         if (!inset)
512                 return false;
513         // don't relock if we're already locked
514         if (theLockingInset() == inset)
515                 return true;
516         if (!theLockingInset()) {
517                 // first check if it's the inset under the cursor we want lock
518                 // should be most of the time
519                 char const c = text->cursor.par()->getChar(text->cursor.pos());
520                 if (c == Paragraph::META_INSET) {
521                         Inset * in = text->cursor.par()->getInset(text->cursor.pos());
522                         if (inset == in) {
523                                 theLockingInset(inset);
524                                 return true;
525                         }
526                 }
527                 // Then do a deep look of the inset and lock the right one
528                 int const id = inset->id();
529                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
530                 ParagraphList::iterator pend = buffer()->paragraphs.end();
531                 for (; pit != pend; ++pit) {
532                         InsetList::iterator it = pit->insetlist.begin();
533                         InsetList::iterator end = pit->insetlist.end();
534                         for (; it != end; ++it) {
535                                 if (it.getInset() == inset) {
536                                         text->setCursorIntern(this, &*pit, it.getPos());
537                                         theLockingInset(inset);
538                                         return true;
539                                 }
540                                 if (it.getInset()->getInsetFromID(id)) {
541                                         text->setCursorIntern(this, &*pit, it.getPos());
542                                         it.getInset()->edit(this);
543                                         return theLockingInset()->lockInsetInInset(this, inset);
544                                 }
545                         }
546                 }
547                 return false;
548         }
549         return theLockingInset()->lockInsetInInset(this, inset);
550 }
551
552
553 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
554 {
555         if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
556                 LyXCursor cursor = text->cursor;
557                 Inset * locking_inset = theLockingInset()->getLockingInset();
558
559                 if ((cursor.pos() - 1 >= 0) &&
560                     cursor.par()->isInset(cursor.pos() - 1) &&
561                     (cursor.par()->getInset(cursor.pos() - 1) ==
562                      locking_inset))
563                         text->setCursor(this, cursor,
564                                         cursor.par(), cursor.pos() - 1);
565                 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
566                 LyXText * txt = getLyXText();
567                 if (locking_inset->isTextInset() &&
568                     locking_inset->lyxCode() != Inset::ERT_CODE &&
569                     (txt->real_current_font.language() !=
570                      buffer()->params.language
571                      || txt->real_current_font.isVisibleRightToLeft()
572                      != buffer()->params.language->RightToLeft()))
573                         shape = (txt->real_current_font.isVisibleRightToLeft())
574                                 ? LyXScreen::REVERSED_L_SHAPE
575                                 : LyXScreen::L_SHAPE;
576                 y += cursor.iy() + theLockingInset()->insetInInsetY();
577                 screen().showManualCursor(text, x, y, asc, desc,
578                                                   shape);
579         }
580 }
581
582
583 void BufferView::hideLockedInsetCursor()
584 {
585         if (theLockingInset() && available()) {
586                 screen().hideCursor();
587         }
588 }
589
590
591 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
592 {
593         if (theLockingInset() && available()) {
594                 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
595                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
596                         updateScrollbar();
597                         return true;
598                 }
599         }
600         return false;
601 }
602
603
604 int BufferView::unlockInset(UpdatableInset * inset)
605 {
606         if (!inset)
607                 return 0;
608         if (inset && theLockingInset() == inset) {
609                 inset->insetUnlock(this);
610                 theLockingInset(0);
611                 // make sure we update the combo !
612                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
613                 // Tell the paragraph dialog that we changed paragraph
614                 owner()->getDialogs().updateParagraph();
615                 finishUndo();
616                 return 0;
617         } else if (inset && theLockingInset() &&
618                    theLockingInset()->unlockInsetInInset(this, inset)) {
619                 // Tell the paragraph dialog that we changed paragraph
620                 owner()->getDialogs().updateParagraph();
621                 // owner inset has updated the layout combo
622                 finishUndo();
623                 return 0;
624         }
625         return bufferlist.unlockInset(inset);
626 }
627
628
629 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
630 {
631         if (!theLockingInset())
632                 return; // shouldn't happen
633         if (kind == Undo::EDIT) // in this case insets would not be stored!
634                 kind = Undo::FINISH;
635         setUndo(this, kind,
636                 text->cursor.par(),
637                 text->cursor.par()->next());
638 }
639
640
641 void BufferView::updateInset(Inset * inset, bool mark_dirty)
642 {
643         pimpl_->updateInset(inset, mark_dirty);
644 }
645
646
647 bool BufferView::ChangeInsets(Inset::Code code,
648                               string const & from, string const & to)
649 {
650         bool need_update = false;
651         LyXCursor cursor = text->cursor;
652         LyXCursor tmpcursor = cursor;
653         cursor.par(tmpcursor.par());
654         cursor.pos(tmpcursor.pos());
655
656         ParIterator end = buffer()->par_iterator_end();
657         for (ParIterator it = buffer()->par_iterator_begin();
658              it != end; ++it) {
659                 Paragraph * par = *it;
660                 bool changed_inset = false;
661                 for (InsetList::iterator it2 = par->insetlist.begin();
662                      it2 != par->insetlist.end(); ++it2) {
663                         if (it2.getInset()->lyxCode() == code) {
664                                 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
665                                 if (inset->getContents() == from) {
666                                         inset->setContents(to);
667                                         changed_inset = true;
668                                 }
669                         }
670                 }
671                 if (changed_inset) {
672                         need_update = true;
673
674                         // FIXME
675
676                         // The test it.size()==1 was needed to prevent crashes.
677                         // How to set the cursor corretly when it.size()>1 ??
678                         if (it.size() == 1) {
679                                 text->setCursorIntern(this, par, 0);
680                                 text->redoParagraphs(this, text->cursor,
681                                                      text->cursor.par()->next());
682                                 text->fullRebreak(this);
683                         }
684                 }
685         }
686         text->setCursorIntern(this, cursor.par(), cursor.pos());
687         return need_update;
688 }
689
690
691 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
692 {
693         // Check if the label 'from' appears more than once
694         vector<string> labels = buffer()->getLabelList();
695
696         if (lyx::count(labels.begin(), labels.end(), from) > 1)
697                 return false;
698
699         return ChangeInsets(Inset::REF_CODE, from, to);
700 }
701
702
703 bool BufferView::ChangeCitationsIfUnique(string const & from,
704                                          string const & to)
705 {
706         typedef pair<string, string> StringPair;
707
708         vector<StringPair> keys = buffer()->getBibkeyList();
709         if (count_if(keys.begin(), keys.end(),
710                      lyx::equal_1st_in_pair<StringPair>(from))
711             > 1)
712                 return false;
713
714         return ChangeInsets(Inset::CITE_CODE, from, to);
715 }
716
717
718 UpdatableInset * BufferView::theLockingInset() const
719 {
720         // If NULL is not allowed we should put an Assert here. (Lgb)
721         if (text)
722                 return text->the_locking_inset;
723         return 0;
724 }
725
726
727 void BufferView::theLockingInset(UpdatableInset * inset)
728 {
729         text->the_locking_inset = inset;
730 }
731
732
733 LyXText * BufferView::getLyXText() const
734 {
735         if (theLockingInset()) {
736                 LyXText * txt = theLockingInset()->getLyXText(this, true);
737                 if (txt)
738                         return txt;
739         }
740         return text;
741 }
742
743
744 LyXText * BufferView::getParentText(Inset * inset) const
745 {
746         if (inset->owner()) {
747                 LyXText * txt = inset->getLyXText(this);
748                 inset = inset->owner();
749                 while (inset && inset->getLyXText(this) == txt)
750                         inset = inset->owner();
751                 if (inset)
752                         return inset->getLyXText(this);
753         }
754         return text;
755 }
756
757
758 Language const * BufferView::getParentLanguage(Inset * inset) const
759 {
760         LyXText * text = getParentText(inset);
761         return text->cursor.par()->getFontSettings(buffer()->params,
762                                                    text->cursor.pos()).language();
763 }