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