]> git.lyx.org Git - features.git/blob - src/BufferView2.C
Reduced header file includes somewhat
[features.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(this);
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(this);
292                 update(text, BufferView::SELECT|BufferView::FITCUR);
293                 owner()->message(_("Copy"));
294         }
295 }
296
297
298 void BufferView::cut()
299 {
300         if (available()) {
301                 hideCursor();
302                 update(text, BufferView::SELECT|BufferView::FITCUR);
303                 text->cutSelection(this);
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(this);
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(this);
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         hideCursor();
350         beforeChange(text);
351         text->selectSelectedWord(this);
352         toggleSelection(false);
353         update(text, BufferView::SELECT|BufferView::FITCUR);
354 }
355
356
357 void BufferView::endOfSpellCheck()
358 {
359         if (!available()) return;
360    
361         hideCursor();
362         beforeChange(text);
363         text->selectSelectedWord(this);
364         text->clearSelection(this);
365         update(text, BufferView::SELECT|BufferView::FITCUR);
366 }
367
368
369 void BufferView::replaceWord(string const & replacestring)
370 {
371         if (!available()) return;
372
373         LyXText * tt = getLyXText();
374         hideCursor();
375         update(tt, BufferView::SELECT|BufferView::FITCUR);
376    
377         /* clear the selection (if there is any) */ 
378         toggleSelection(false);
379         update(tt, BufferView::SELECT|BufferView::FITCUR);
380    
381         /* clear the selection (if there is any) */ 
382         toggleSelection(false);
383         tt->replaceSelectionWithString(this, replacestring);
384    
385         tt->setSelectionOverString(this, replacestring);
386
387         // Go back so that replacement string is also spellchecked
388         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
389                 tt->cursorLeft(this);
390         }
391         update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
392 }
393 // End of spellchecker stuff
394
395
396 bool BufferView::lockInset(UpdatableInset * inset)
397 {
398         if (!theLockingInset() && inset) {
399                 theLockingInset(inset);
400                 return true;
401         } else if (inset) {
402             return theLockingInset()->lockInsetInInset(this, inset);
403         }
404         return false;
405 }
406
407
408 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
409 {
410         if (theLockingInset() && available()) {
411                 LyXCursor cursor = text->cursor;
412                 if ((cursor.pos() - 1 >= 0) &&
413                     (cursor.par()->getChar(cursor.pos() - 1) ==
414                      Paragraph::META_INSET) &&
415                     (cursor.par()->getInset(cursor.pos() - 1) ==
416                      theLockingInset()->getLockingInset()))
417                         text->setCursor(this, cursor,
418                                         cursor.par(), cursor.pos() - 1);
419                 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
420                 LyXText * txt = getLyXText();
421                 if (theLockingInset()->getLockingInset()->lyxCode() ==
422                     Inset::TEXT_CODE &&
423                     (txt->real_current_font.language() !=
424                      buffer()->params.language
425                      || txt->real_current_font.isVisibleRightToLeft()
426                      != buffer()->params.language->RightToLeft()))
427                         shape = (txt->real_current_font.isVisibleRightToLeft())
428                                 ? LyXScreen::REVERSED_L_SHAPE
429                                 : LyXScreen::L_SHAPE;
430                 y += cursor.y() + theLockingInset()->insetInInsetY();
431                 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
432                                                   shape);
433         }
434 }
435
436
437 void BufferView::hideLockedInsetCursor()
438 {
439         if (theLockingInset() && available()) {
440                 pimpl_->screen_->hideCursor();
441         }
442 }
443
444
445 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
446 {
447         if (theLockingInset() && available()) {
448                 y += text->cursor.y() + theLockingInset()->insetInInsetY();
449                 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
450                         updateScrollbar();
451         }
452 }
453
454
455 int BufferView::unlockInset(UpdatableInset * inset)
456 {
457         if (inset && theLockingInset() == inset) {
458                 inset->insetUnlock(this);
459                 theLockingInset(0);
460                 finishUndo();
461                 return 0;
462         } else if (inset && theLockingInset() &&
463                    theLockingInset()->unlockInsetInInset(this, inset)) {
464                 finishUndo();
465                 return 0;
466         }
467         return bufferlist.unlockInset(inset);
468 }
469
470
471 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
472 {
473         if (!theLockingInset())
474                 return; // shouldn't happen
475         if (kind == Undo::EDIT) // in this case insets would not be stored!
476                 kind = Undo::FINISH;
477         setUndo(this, kind,
478                 text->cursor.par(),
479                 text->cursor.par()->next());
480 }
481
482
483 void BufferView::updateInset(Inset * inset, bool mark_dirty)
484 {
485         pimpl_->updateInset(inset, mark_dirty);
486 }
487
488
489 bool BufferView::ChangeInsets(Inset::Code code,
490                               string const & from, string const & to)
491 {
492         bool flag = false;
493         Paragraph * par = buffer()->paragraph;
494         LyXCursor cursor = text->cursor;
495         LyXCursor tmpcursor = cursor;
496         cursor.par(tmpcursor.par());
497         cursor.pos(tmpcursor.pos());
498
499         while (par) {
500                 bool flag2 = false;
501                 for (Paragraph::inset_iterator it = par->inset_iterator_begin();
502                      it != par->inset_iterator_end(); ++it) {
503                         if ((*it)->lyxCode() == code) {
504                                 InsetCommand * inset = static_cast<InsetCommand *>(*it);
505                                 if (inset->getContents() == from) {
506                                         inset->setContents(to);
507                                         flag2 = true;
508                                 }
509                         }
510                 }
511                 if (flag2) {
512                         flag = true;
513                         // this is possible now, since SetCursor takes
514                         // care about footnotes
515                         text->setCursorIntern(this, par, 0);
516                         text->redoParagraphs(this, text->cursor,
517                                              text->cursor.par()->next());
518                         text->fullRebreak(this);
519                 }
520                 par = par->next();
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 }