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