]> git.lyx.org Git - lyx.git/blob - src/BufferView2.C
ignore one more file
[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 "LyXView.h"
18 #include "bufferlist.h"
19 #include "lyxscreen.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
28 #include "frontends/Alert.h"
29
30 #include "insets/insetcommand.h" //ChangeRefs
31 #include "insets/inseterror.h"
32
33 #include "support/FileInfo.h"
34 #include "support/filetools.h"
35 #include "support/lyxfunctional.h" //equal_1st_in_pair
36 #include "support/types.h"
37
38 #include <fstream>
39 #include <algorithm>
40
41 extern BufferList bufferlist;
42
43 using lyx::pos_type;
44
45 using std::pair;
46 using std::endl;
47 using std::ifstream;
48 using std::vector;
49 using std::find;
50 using std::count;
51 using std::count_if;
52
53
54 // Inserts a file into current document
55 bool BufferView::insertLyXFile(string const & filen)
56         //
57         // Copyright CHT Software Service GmbH
58         // Uwe C. Schroeder
59         //
60         // Insert a Lyxformat - file into current buffer
61         //
62         // Moved from lyx_cb.C (Lgb)
63 {
64         if (filen.empty()) return false;
65
66         string const fname = MakeAbsPath(filen);
67
68         // check if file exist
69         FileInfo const fi(fname);
70
71         if (!fi.readable()) {
72                 Alert::alert(_("Error!"),
73                            _("Specified file is unreadable: "),
74                            MakeDisplayPath(fname, 50));
75                 return false;
76         }
77         
78         beforeChange(text);
79
80         ifstream ifs(fname.c_str());
81         if (!ifs) {
82                 Alert::alert(_("Error!"),
83                            _("Cannot open specified file: "),
84                            MakeDisplayPath(fname, 50));
85                 return false;
86         }
87         
88         int const c = ifs.peek();
89        
90         LyXLex lex(0, 0);
91         lex.setStream(ifs);
92
93         bool res = true;
94
95         if (c == '#') {
96                 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
97                 res = buffer()->readFile(lex, text->cursor.par());
98         } else {
99                 lyxerr[Debug::INFO] << "Will insert file without header" 
100                                     << endl;
101                 res = buffer()->readLyXformat2(lex, text->cursor.par());
102         }
103
104         resize();
105         return res;
106 }
107
108
109 bool BufferView::removeAutoInsets()
110 {
111         LyXCursor tmpcursor = text->cursor;
112         Paragraph * cur_par = tmpcursor.par();
113         pos_type cur_pos = tmpcursor.pos();
114         
115         bool found = false;
116
117         ParIterator it = buffer()->par_iterator_begin();
118         ParIterator end = buffer()->par_iterator_end();
119         for (; it != end; ++it) {
120                 Paragraph * par = *it;
121                 bool removed = false;
122
123                 text->setCursor(this, par, 0);
124                 
125                 Paragraph::inset_iterator pit = par->inset_iterator_begin();
126                 Paragraph::inset_iterator pend = par->inset_iterator_end();
127                 while (pit != pend) {
128                         if (pit->autoDelete()) {
129                                 removed = true;
130                                 pos_type const pos = pit.getPos();
131                                 
132                                 par->erase(pos);
133                                 if (cur_par == par) {
134                                         if (cur_pos > pos)
135                                                 --cur_pos;
136                                 }
137                         } else {
138                                 ++pit;
139                         }
140                 }
141                 if (removed) {
142                         found = true;
143                         text->redoParagraph(this);
144                 }
145         }
146
147         text->setCursorIntern(this, cur_par, cur_pos);
148
149         return found;
150 }
151
152
153 void BufferView::insertErrors(TeXErrors & terr)
154 {
155         // Save the cursor position
156         LyXCursor cursor = text->cursor;
157
158         TeXErrors::Errors::const_iterator cit = terr.begin();
159         TeXErrors::Errors::const_iterator end = terr.end();
160         for (; cit != end; ++cit) {
161                 string const desctext(cit->error_desc);
162                 string const errortext(cit->error_text);
163                 string const msgtxt = desctext + '\n' + errortext;
164                 int const errorrow = cit->error_in_line;
165
166                 // Insert error string for row number
167                 int tmpid = -1; 
168                 int tmppos = -1;
169
170                 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
171                         buffer()->texrow.increasePos(tmpid, tmppos);
172                 }
173                 
174                 Paragraph * texrowpar = 0;
175
176                 if (tmpid == -1) {
177                         texrowpar = text->firstParagraph();
178                         tmppos = 0;
179                 } else {
180                         texrowpar = buffer()->getParFromID(tmpid);
181                 }
182
183                 if (texrowpar == 0)
184                         continue;
185
186                 InsetError * new_inset = new InsetError(msgtxt);
187                 text->setCursorIntern(this, texrowpar, tmppos);
188                 text->insertInset(this, new_inset);
189                 text->fullRebreak(this);
190         }
191         // Restore the cursor position
192         text->setCursorIntern(this, cursor.par(), cursor.pos());
193 }
194
195
196 void BufferView::setCursorFromRow(int row)
197 {
198         int tmpid = -1; 
199         int tmppos = -1;
200
201         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
202
203         Paragraph * texrowpar;
204
205         if (tmpid == -1) {
206                 texrowpar = text->firstParagraph();
207                 tmppos = 0;
208         } else {
209                 texrowpar = buffer()->getParFromID(tmpid);
210         }
211         text->setCursor(this, texrowpar, tmppos);
212 }
213
214
215 bool BufferView::insertInset(Inset * inset, string const & lout)
216 {
217         return pimpl_->insertInset(inset, lout);
218 }
219
220
221 /* This is also a buffer property (ale) */
222 // Not so sure about that. a goto Label function can not be buffer local, just
223 // think how this will work in a multiwindo/buffer environment, all the
224 // cursors in all the views showing this buffer will move. (Lgb)
225 // OK, then no cursor action should be allowed in buffer. (ale)
226 bool BufferView::gotoLabel(string const & label)
227
228 {
229         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
230              it != buffer()->inset_iterator_end(); ++it) {
231                 vector<string> labels = (*it)->getLabelList();
232                 if (find(labels.begin(),labels.end(),label)
233                      != labels.end()) {
234                         beforeChange(text);
235                         text->setCursor(this, it.getPar(), it.getPos());
236                         text->selection.cursor = text->cursor;
237                         update(text, BufferView::SELECT|BufferView::FITCUR);
238                         return true;
239                 }
240         }
241         return false;
242 }
243
244
245 void BufferView::menuUndo()
246 {
247         if (available()) {
248                 owner()->message(_("Undo"));
249                 hideCursor();
250                 beforeChange(text);
251                 update(text, BufferView::SELECT|BufferView::FITCUR);
252                 if (!textUndo(this))
253                         owner()->message(_("No further undo information"));
254                 else
255                         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
256                 setState();
257         }
258 }
259
260
261 void BufferView::menuRedo()
262 {
263 #if 0 // this should not be here (Jug 20011206)
264         if (theLockingInset()) {
265                 owner()->message(_("Redo not yet supported in math mode"));
266                 return;
267         }
268 #endif
269    
270         if (available()) {
271                 owner()->message(_("Redo"));
272                 hideCursor();
273                 beforeChange(text);
274                 update(text, BufferView::SELECT|BufferView::FITCUR);
275                 if (!textRedo(this))
276                         owner()->message(_("No further redo information"));
277                 else
278                         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
279                 setState();
280         }
281 }
282
283
284 void BufferView::copyEnvironment()
285 {
286         if (available()) {
287                 text->copyEnvironmentType();
288                 owner()->message(_("Paragraph environment type copied"));
289         }
290 }
291
292
293 void BufferView::pasteEnvironment()
294 {
295         if (available()) {
296                 text->pasteEnvironmentType(this);
297                 owner()->message(_("Paragraph environment type set"));
298                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
299         }
300 }
301
302
303 void BufferView::copy()
304 {
305         if (available()) {
306                 text->copySelection(this);
307                 owner()->message(_("Copy"));
308         }
309 }
310
311
312 void BufferView::cut(bool realcut)
313 {
314         if (available()) {
315                 hideCursor();
316                 update(text, BufferView::SELECT|BufferView::FITCUR);
317                 text->cutSelection(this, true, realcut);
318                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
319                 owner()->message(_("Cut"));
320         }
321 }
322
323
324 void BufferView::paste()
325 {
326         if (!available()) return;
327
328         owner()->message(_("Paste"));
329
330         hideCursor();
331         // clear the selection
332         toggleSelection();
333         text->clearSelection();
334         update(text, BufferView::SELECT|BufferView::FITCUR);
335         
336         // paste
337         text->pasteSelection(this);
338         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
339         
340         // clear the selection 
341         toggleSelection();
342         text->clearSelection();
343         update(text, BufferView::SELECT|BufferView::FITCUR);
344 }
345
346
347 /* these functions are for the spellchecker */ 
348 string const BufferView::nextWord(float & value)
349 {
350         if (!available()) {
351                 value = 1;
352                 return string();
353         }
354
355         return text->selectNextWordToSpellcheck(this, value);
356 }
357
358   
359 void BufferView::selectLastWord()
360 {
361         if (!available()) return;
362    
363         LyXCursor cur = text->selection.cursor;
364         hideCursor();
365         beforeChange(text);
366         text->selection.cursor = cur;
367         text->selectSelectedWord(this);
368         toggleSelection(false);
369         update(text, BufferView::SELECT|BufferView::FITCUR);
370 }
371
372
373 void BufferView::endOfSpellCheck()
374 {
375         if (!available()) return;
376    
377         hideCursor();
378         beforeChange(text);
379         text->selectSelectedWord(this);
380         text->clearSelection();
381         update(text, BufferView::SELECT|BufferView::FITCUR);
382 }
383
384
385 void BufferView::replaceWord(string const & replacestring)
386 {
387         if (!available()) return;
388
389         LyXText * tt = getLyXText();
390         hideCursor();
391         update(tt, BufferView::SELECT|BufferView::FITCUR);
392    
393         /* clear the selection (if there is any) */ 
394         toggleSelection(false);
395         update(tt, BufferView::SELECT|BufferView::FITCUR);
396    
397         /* clear the selection (if there is any) */ 
398         toggleSelection(false);
399         tt->replaceSelectionWithString(this, replacestring);
400    
401         tt->setSelectionOverString(this, replacestring);
402
403         // Go back so that replacement string is also spellchecked
404         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
405                 tt->cursorLeft(this);
406         }
407         update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
408 }
409 // End of spellchecker stuff
410
411
412 bool BufferView::lockInset(UpdatableInset * inset)
413 {
414         if (!inset)
415                 return false;
416         // don't relock if we're already locked
417         if (theLockingInset() == inset)
418                 return true;
419         if (!theLockingInset()) {
420                 // first check if it's the inset under the cursor we want lock
421                 // should be most of the time
422                 char const c = text->cursor.par()->getChar(text->cursor.pos());
423                 if (c == Paragraph::META_INSET) {
424                         Inset * in = text->cursor.par()->getInset(text->cursor.pos());
425                         if (inset == in) {
426                                 theLockingInset(inset);
427                                 return true;
428                         }
429                 }
430                 // Then do a deep look of the inset and lock the right one
431                 Paragraph * par = buffer()->paragraph;
432                 int const id = inset->id();
433                 while(par) {
434                         Paragraph::inset_iterator it =
435                                 par->inset_iterator_begin();
436                         Paragraph::inset_iterator const end =
437                                 par->inset_iterator_end();
438                         for (; it != end; ++it) {
439                                 if ((*it) == inset) {
440                                         theLockingInset(inset);
441                                         return true;
442                                 }
443                                 if ((*it)->getInsetFromID(id)) {
444                                         text->setCursorIntern(this, par, it.getPos());
445                                         theLockingInset(static_cast<UpdatableInset *>(*it));
446                                         return theLockingInset()->lockInsetInInset(this, inset);
447                                 }
448                         }
449                         par = par->next();
450                 }
451                 return false;
452         }
453         return theLockingInset()->lockInsetInInset(this, inset);
454 }
455
456
457 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
458 {
459         if (available() && theLockingInset()) {
460                 LyXCursor cursor = text->cursor;
461                 Inset * locking_inset = theLockingInset()->getLockingInset();
462
463                 if ((cursor.pos() - 1 >= 0) &&
464                     cursor.par()->isInset(cursor.pos() - 1) &&
465                     (cursor.par()->getInset(cursor.pos() - 1) ==
466                      locking_inset))
467                         text->setCursor(this, cursor,
468                                         cursor.par(), cursor.pos() - 1);
469                 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
470                 LyXText * txt = getLyXText();
471                 if (locking_inset->isTextInset() &&
472                     locking_inset->lyxCode() != Inset::ERT_CODE &&
473                     (txt->real_current_font.language() !=
474                      buffer()->params.language
475                      || txt->real_current_font.isVisibleRightToLeft()
476                      != buffer()->params.language->RightToLeft()))
477                         shape = (txt->real_current_font.isVisibleRightToLeft())
478                                 ? LyXScreen::REVERSED_L_SHAPE
479                                 : LyXScreen::L_SHAPE;
480                 y += cursor.y() + theLockingInset()->insetInInsetY();
481                 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
482                                                   shape);
483         }
484 }
485
486
487 void BufferView::hideLockedInsetCursor()
488 {
489         if (theLockingInset() && available()) {
490                 pimpl_->screen_->hideCursor();
491         }
492 }
493
494
495 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
496 {
497         if (theLockingInset() && available()) {
498                 y += text->cursor.y() + theLockingInset()->insetInInsetY();
499                 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
500                         updateScrollbar();
501         }
502 }
503
504
505 int BufferView::unlockInset(UpdatableInset * inset)
506 {
507         if (!inset)
508                 return 0;
509         if (inset && theLockingInset() == inset) {
510                 inset->insetUnlock(this);
511                 theLockingInset(0);
512                 // make sure we update the combo !
513                 owner()->setLayout(getLyXText()->cursor.par()->getLayout());
514                 finishUndo();
515                 return 0;
516         } else if (inset && theLockingInset() &&
517                    theLockingInset()->unlockInsetInInset(this, inset)) {
518                 // owner inset has updated the layout combo 
519                 finishUndo();
520                 return 0;
521         }
522         return bufferlist.unlockInset(inset);
523 }
524
525
526 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
527 {
528         if (!theLockingInset())
529                 return; // shouldn't happen
530         if (kind == Undo::EDIT) // in this case insets would not be stored!
531                 kind = Undo::FINISH;
532         setUndo(this, kind,
533                 text->cursor.par(),
534                 text->cursor.par()->next());
535 }
536
537
538 void BufferView::updateInset(Inset * inset, bool mark_dirty)
539 {
540         pimpl_->updateInset(inset, mark_dirty);
541 }
542
543
544 bool BufferView::ChangeInsets(Inset::Code code,
545                               string const & from, string const & to)
546 {
547         bool need_update = false;
548         LyXCursor cursor = text->cursor;
549         LyXCursor tmpcursor = cursor;
550         cursor.par(tmpcursor.par());
551         cursor.pos(tmpcursor.pos());
552
553         ParIterator end = buffer()->par_iterator_end();
554         for (ParIterator it = buffer()->par_iterator_begin();
555              it != end; ++it) {
556                 Paragraph * par = *it;
557                 bool changed_inset = false;
558                 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
559                      it2 != par->inset_iterator_end(); ++it2) {
560                         if ((*it2)->lyxCode() == code) {
561                                 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
562                                 if (inset->getContents() == from) {
563                                         inset->setContents(to);
564                                         changed_inset = true;
565                                 }
566                         }
567                 }
568                 if (changed_inset) {
569                         need_update = true;
570 #ifdef WITH_WARNINGS
571 #warning FIXME
572 #endif
573                         // The test it.size()==1 was needed to prevent crashes.
574                         // How to set the cursor corretly when it.size()>1 ??
575                         if (it.size() == 1) {
576                                 text->setCursorIntern(this, par, 0);
577                                 text->redoParagraphs(this, text->cursor,
578                                                      text->cursor.par()->next());
579                                 text->fullRebreak(this);
580                         }
581                 }
582         }
583         text->setCursorIntern(this, cursor.par(), cursor.pos());
584         return need_update;
585 }
586
587
588 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
589 {
590         // Check if the label 'from' appears more than once
591         vector<string> labels = buffer()->getLabelList();
592         if (count(labels.begin(), labels.end(), from) > 1)
593                 return false;
594
595         return ChangeInsets(Inset::REF_CODE, from, to);
596 }
597
598
599 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
600 {
601
602         vector<pair<string,string> > keys = buffer()->getBibkeyList();  
603         if (count_if(keys.begin(), keys.end(), 
604                      lyx::equal_1st_in_pair<string,string>(from)) 
605             > 1)
606                 return false;
607
608         return ChangeInsets(Inset::CITE_CODE, from, to);
609 }
610
611
612 UpdatableInset * BufferView::theLockingInset() const
613 {
614         // If NULL is not allowed we should put an Assert here. (Lgb)
615         if (text)
616                 return text->the_locking_inset;
617         return 0;
618 }
619
620
621 void BufferView::theLockingInset(UpdatableInset * inset)
622 {
623         text->the_locking_inset = inset;
624 }
625
626
627 LyXText * BufferView::getLyXText() const
628 {
629         if (theLockingInset()) {
630                 LyXText * txt = theLockingInset()->getLyXText(this, true);
631                 if (txt)
632                         return txt;
633         }
634         return text;
635 }
636
637
638 LyXText * BufferView::getParentText(Inset * inset) const
639 {
640         if (inset->owner()) {
641                 LyXText * txt = inset->getLyXText(this);
642                 inset = inset->owner();
643                 while (inset && inset->getLyXText(this) == txt)
644                         inset = inset->owner();
645                 if (inset)
646                         return inset->getLyXText(this);
647         }
648         return text;
649 }
650
651
652 Language const * BufferView::getParentLanguage(Inset * inset) const
653 {
654         LyXText * text = getParentText(inset);
655         return text->cursor.par()->getFontSettings(buffer()->params,
656                                                    text->cursor.pos()).language();
657 }