]> git.lyx.org Git - lyx.git/blob - src/BufferView2.C
Fix crash when running lyx -dbg insets -e ...
[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
333
334 void BufferView::insertCorrectQuote()
335 {
336         char c;
337
338         if (text->cursor.pos())
339                 c = text->cursor.par()->getChar(text->cursor.pos() - 1);
340         else 
341                 c = ' ';
342
343         insertInset(new InsetQuotes(c, buffer()->params));
344 }
345
346
347 /* these functions are for the spellchecker */ 
348 string const BufferView::nextWord(float & value)
349 {
350         if (!available()) {
351                 value = 1;
352                 return string();
353         }
354
355         return text->selectNextWord(this, value);
356 }
357
358   
359 void BufferView::selectLastWord()
360 {
361         if (!available()) return;
362    
363         hideCursor();
364         beforeChange(text);
365         text->selectSelectedWord(this);
366         toggleSelection(false);
367         update(text, BufferView::SELECT|BufferView::FITCUR);
368 }
369
370
371 void BufferView::endOfSpellCheck()
372 {
373         if (!available()) return;
374    
375         hideCursor();
376         beforeChange(text);
377         text->selectSelectedWord(this);
378         text->clearSelection(this);
379         update(text, BufferView::SELECT|BufferView::FITCUR);
380 }
381
382
383 void BufferView::replaceWord(string const & replacestring)
384 {
385         if (!available()) return;
386
387         LyXText * tt = getLyXText();
388         hideCursor();
389         update(tt, BufferView::SELECT|BufferView::FITCUR);
390    
391         /* clear the selection (if there is any) */ 
392         toggleSelection(false);
393         update(tt, BufferView::SELECT|BufferView::FITCUR);
394    
395         /* clear the selection (if there is any) */ 
396         toggleSelection(false);
397         tt->replaceSelectionWithString(this, replacestring);
398    
399         tt->setSelectionOverString(this, replacestring);
400
401         // Go back so that replacement string is also spellchecked
402         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
403                 tt->cursorLeft(this);
404         }
405         update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
406 }
407 // End of spellchecker stuff
408
409
410 bool BufferView::lockInset(UpdatableInset * inset)
411 {
412         if (!theLockingInset() && inset) {
413                 theLockingInset(inset);
414                 return true;
415         } else if (inset) {
416             return theLockingInset()->lockInsetInInset(this, inset);
417         }
418         return false;
419 }
420
421
422 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
423 {
424         if (theLockingInset() && available()) {
425                 LyXCursor cursor = text->cursor;
426                 if ((cursor.pos() - 1 >= 0) &&
427                     (cursor.par()->getChar(cursor.pos() - 1) ==
428                      Paragraph::META_INSET) &&
429                     (cursor.par()->getInset(cursor.pos() - 1) ==
430                      theLockingInset()->getLockingInset()))
431                         text->setCursor(this, cursor,
432                                         cursor.par(), cursor.pos() - 1);
433                 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
434                 LyXText * txt = getLyXText();
435                 if (theLockingInset()->getLockingInset()->lyxCode() ==
436                     Inset::TEXT_CODE &&
437                     (txt->real_current_font.language() !=
438                      buffer()->params.language
439                      || txt->real_current_font.isVisibleRightToLeft()
440                      != buffer()->params.language->RightToLeft()))
441                         shape = (txt->real_current_font.isVisibleRightToLeft())
442                                 ? LyXScreen::REVERSED_L_SHAPE
443                                 : LyXScreen::L_SHAPE;
444                 y += cursor.y() + theLockingInset()->insetInInsetY();
445                 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
446                                                   shape);
447         }
448 }
449
450
451 void BufferView::hideLockedInsetCursor()
452 {
453         if (theLockingInset() && available()) {
454                 pimpl_->screen_->hideCursor();
455         }
456 }
457
458
459 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
460 {
461         if (theLockingInset() && available()) {
462                 y += text->cursor.y() + theLockingInset()->insetInInsetY();
463                 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
464                         updateScrollbar();
465         }
466 }
467
468
469 int BufferView::unlockInset(UpdatableInset * inset)
470 {
471         if (inset && theLockingInset() == inset) {
472                 inset->insetUnlock(this);
473                 theLockingInset(0);
474                 finishUndo();
475                 return 0;
476         } else if (inset && theLockingInset() &&
477                    theLockingInset()->unlockInsetInInset(this, inset)) {
478                 finishUndo();
479                 return 0;
480         }
481         return bufferlist.unlockInset(inset);
482 }
483
484
485 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
486 {
487         if (!theLockingInset())
488                 return; // shouldn't happen
489         if (kind == Undo::EDIT) // in this case insets would not be stored!
490                 kind = Undo::FINISH;
491         setUndo(this, kind,
492                 text->cursor.par(),
493                 text->cursor.par()->next());
494 }
495
496
497 void BufferView::updateInset(Inset * inset, bool mark_dirty)
498 {
499         pimpl_->updateInset(inset, mark_dirty);
500 }
501
502
503 bool BufferView::ChangeInsets(Inset::Code code,
504                               string const & from, string const & to)
505 {
506         bool flag = false;
507         Paragraph * par = buffer()->paragraph;
508         LyXCursor cursor = text->cursor;
509         LyXCursor tmpcursor = cursor;
510         cursor.par(tmpcursor.par());
511         cursor.pos(tmpcursor.pos());
512
513         while (par) {
514                 bool flag2 = false;
515                 for (Paragraph::inset_iterator it = par->inset_iterator_begin();
516                      it != par->inset_iterator_end(); ++it) {
517                         if ((*it)->lyxCode() == code) {
518                                 InsetCommand * inset = static_cast<InsetCommand *>(*it);
519                                 if (inset->getContents() == from) {
520                                         inset->setContents(to);
521                                         flag2 = true;
522                                 }
523                         }
524                 }
525                 if (flag2) {
526                         flag = true;
527                         // this is possible now, since SetCursor takes
528                         // care about footnotes
529                         text->setCursorIntern(this, par, 0);
530                         text->redoParagraphs(this, text->cursor,
531                                              text->cursor.par()->next());
532                         text->fullRebreak(this);
533                 }
534                 par = par->next();
535         }
536         text->setCursorIntern(this, cursor.par(), cursor.pos());
537         return flag;
538 }
539
540
541 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
542 {
543         // Check if the label 'from' appears more than once
544         vector<string> labels = buffer()->getLabelList();
545         if (count(labels.begin(), labels.end(), from) > 1)
546                 return false;
547
548         return ChangeInsets(Inset::REF_CODE, from, to);
549 }
550
551
552 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
553 {
554
555         vector<pair<string,string> > keys = buffer()->getBibkeyList();  
556         if (count_if(keys.begin(), keys.end(), 
557                      lyx::equal_1st_in_pair<string,string>(from)) 
558             > 1)
559                 return false;
560
561         return ChangeInsets(Inset::CITE_CODE, from, to);
562 }
563
564
565 UpdatableInset * BufferView::theLockingInset() const
566 {
567         // If NULL is not allowed we should put an Assert here. (Lgb)
568         if (text)
569                 return text->the_locking_inset;
570         return 0;
571 }
572
573
574 void BufferView::theLockingInset(UpdatableInset * inset)
575 {
576         text->the_locking_inset = inset;
577 }
578
579
580 LyXText * BufferView::getLyXText() const
581 {
582         if (theLockingInset()) {
583                 LyXText * txt = theLockingInset()->getLyXText(this, true);
584                 if (txt)
585                         return txt;
586         }
587         return text;
588 }
589
590
591 LyXText * BufferView::getParentText(Inset * inset) const
592 {
593         if (inset->owner()) {
594                 LyXText * txt = inset->getLyXText(this);
595                 inset = inset->owner();
596                 while (inset && inset->getLyXText(this) == txt)
597                         inset = inset->owner();
598                 if (inset)
599                         return inset->getLyXText(this);
600         }
601         return text;
602 }
603
604
605 Language const * BufferView::getParentLanguage(Inset * inset) const
606 {
607         LyXText * text = getParentText(inset);
608         return text->cursor.par()->getFontSettings(buffer()->params,
609                                                    text->cursor.pos()).language();
610 }