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