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