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