]> git.lyx.org Git - lyx.git/blob - src/BufferView2.C
senseless fix
[lyx.git] / src / BufferView2.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *        
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2001 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #include <fstream>
15 #include <algorithm>
16
17 #include "BufferView.h"
18 #include "buffer.h"
19 #include "lyxcursor.h"
20 #include "lyxtext.h"
21 #include "insets/inseterror.h"
22 #include "insets/insetinfo.h"
23 #include "LyXView.h"
24 #include "bufferlist.h"
25 #include "support/FileInfo.h"
26 #include "lyxscreen.h"
27 #include "support/filetools.h"
28 #include "lyx_gui_misc.h"
29 #include "LaTeX.h"
30 #include "BufferView_pimpl.h"
31 #include "insets/insetcommand.h" //ChangeRefs
32 #include "support/lyxfunctional.h" //equal_1st_in_pair
33 #include "language.h"
34 #include "gettext.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() << "Will insert file with header" << endl;
90                 res = buffer()->readFile(lex, text->cursor.par());
91         } else {
92                 lyxerr.debug() << "Will insert file without header" << 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         LyXParagraph * 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                 LyXParagraph * texrowpar = 0;
154
155                 if (tmpid == -1) {
156                         texrowpar = text->FirstParagraph();
157                         tmppos = 0;
158                 } else {
159                         texrowpar = text->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         LyXParagraph * texrowpar;
183
184         if (tmpid == -1) {
185                 texrowpar = text->FirstParagraph();
186                 tmppos = 0;
187         } else {
188                 texrowpar = text->GetParFromID(tmpid);
189         }
190         text->SetCursor(this, texrowpar, tmppos);
191 }
192
193
194 bool BufferView::insertInset(Inset * inset, string const & lout,
195                              bool /*no_table*/)
196 {
197         // if we are in a locking inset we should try to insert the
198         // inset there otherwise this is a illegal function now
199         if (theLockingInset()) {
200                 if (theLockingInset()->InsertInsetAllowed(inset))
201                     return theLockingInset()->InsertInset(this, inset);
202                 return false;
203         }
204
205         // not quite sure if we want this...
206         text->SetCursorParUndo(buffer());
207         text->FreezeUndo();
208         
209         beforeChange(text);
210         if (!lout.empty()) {
211                 update(text, BufferView::SELECT|BufferView::FITCUR);
212                 text->BreakParagraph(this);
213                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
214
215                 if (text->cursor.par()->size()) {
216                         text->CursorLeft(this);
217                         
218                         text->BreakParagraph(this);
219                         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
220                 }
221
222                 pair<bool, LyXTextClass::size_type> lres =
223                         textclasslist.NumberOfLayout(buffer()->params
224                                                      .textclass, lout);
225                 LyXTextClass::size_type lay;
226                 if (lres.first != false) {
227                         // layout found
228                         lay = lres.second;
229                 } else {
230                         // layout not fount using default "Standard" (0)
231                         lay = 0;
232                 }
233                  
234                 text->SetLayout(this, lay);
235                 
236                 text->SetParagraph(this, 0, 0,
237                                    0, 0,
238                                    VSpace(VSpace::NONE), VSpace(VSpace::NONE),
239                                    LYX_ALIGN_LAYOUT, 
240                                    string(),
241                                    0);
242                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
243                 
244                 text->current_font.setLatex(LyXFont::OFF);
245         }
246         
247         text->InsertInset(this, inset);
248         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
249
250         text->UnFreezeUndo();
251         return true;
252 }
253
254
255 /* This is also a buffer property (ale) */
256 // Not so sure about that. a goto Label function can not be buffer local, just
257 // think how this will work in a multiwindo/buffer environment, all the
258 // cursors in all the views showing this buffer will move. (Lgb)
259 // OK, then no cursor action should be allowed in buffer. (ale)
260 bool BufferView::gotoLabel(string const & label)
261
262 {
263         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
264              it != buffer()->inset_iterator_end(); ++it) {
265                 vector<string> labels = (*it)->getLabelList();
266                 if (find(labels.begin(),labels.end(),label)
267                      != labels.end()) {
268                         beforeChange(text);
269                         text->SetCursor(this, it.getPar(), it.getPos());
270                         text->selection.cursor = text->cursor;
271                         update(text, BufferView::SELECT|BufferView::FITCUR);
272                         return true;
273                 }
274         }
275         return false;
276 }
277
278
279 void BufferView::menuUndo()
280 {
281         if (available()) {
282                 owner()->message(_("Undo"));
283                 hideCursor();
284                 beforeChange(text);
285                 update(text, BufferView::SELECT|BufferView::FITCUR);
286                 if (!text->TextUndo(this))
287                         owner()->message(_("No forther undo information"));
288                 else
289                         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
290                 setState();
291         }
292 }
293
294
295 void BufferView::menuRedo()
296 {
297         if (theLockingInset()) {
298                 owner()->message(_("Redo not yet supported in math mode"));
299                 return;
300         }    
301    
302         if (available()) {
303                 owner()->message(_("Redo"));
304                 hideCursor();
305                 beforeChange(text);
306                 update(text, BufferView::SELECT|BufferView::FITCUR);
307                 if (!text->TextRedo(this))
308                         owner()->message(_("No further redo information"));
309                 else
310                         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
311                 setState();
312         }
313 }
314
315
316 void BufferView::copyEnvironment()
317 {
318         if (available()) {
319                 text->copyEnvironmentType();
320                 // clear the selection, even if mark_set
321                 toggleSelection();
322                 text->ClearSelection(this);
323                 update(text, BufferView::SELECT|BufferView::FITCUR);
324                 owner()->message(_("Paragraph environment type copied"));
325         }
326 }
327
328
329 void BufferView::pasteEnvironment()
330 {
331         if (available()) {
332                 text->pasteEnvironmentType(this);
333                 owner()->message(_("Paragraph environment type set"));
334                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
335         }
336 }
337
338
339 void BufferView::copy()
340 {
341         if (available()) {
342                 text->CopySelection(this);
343                 // clear the selection, even if mark_set
344                 toggleSelection();
345                 text->ClearSelection(this);
346                 update(text, BufferView::SELECT|BufferView::FITCUR);
347                 owner()->message(_("Copy"));
348         }
349 }
350
351
352 void BufferView::cut()
353 {
354         if (available()) {
355                 hideCursor();
356                 update(text, BufferView::SELECT|BufferView::FITCUR);
357                 text->CutSelection(this);
358                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
359                 owner()->message(_("Cut"));
360         }
361 }
362
363
364 void BufferView::paste()
365 {
366         if (!available()) return;
367
368         owner()->message(_("Paste"));
369
370         hideCursor();
371         // clear the selection
372         toggleSelection();
373         text->ClearSelection(this);
374         update(text, BufferView::SELECT|BufferView::FITCUR);
375         
376         // paste
377         text->PasteSelection(this);
378         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
379         
380         // clear the selection 
381         toggleSelection();
382         text->ClearSelection(this);
383         update(text, BufferView::SELECT|BufferView::FITCUR);
384 }
385
386
387 void BufferView::gotoInset(std::vector<Inset::Code> const & codes,
388                            bool same_content)
389 {
390         if (!available()) return;
391    
392         hideCursor();
393         beforeChange(text);
394         update(text, BufferView::SELECT|BufferView::FITCUR);
395
396         string contents;
397         if (same_content &&
398             text->cursor.par()->GetChar(text->cursor.pos()) == LyXParagraph::META_INSET) {
399                 Inset const * inset = text->cursor.par()->GetInset(text->cursor.pos());
400                 if (find(codes.begin(), codes.end(), inset->LyxCode())
401                     != codes.end())
402                         contents =
403                                 static_cast<InsetCommand const *>(inset)->getContents();
404         }
405         
406         if (!text->GotoNextInset(this, codes, contents)) {
407                 if (text->cursor.pos() 
408                     || text->cursor.par() != text->FirstParagraph()) {
409                                 LyXCursor tmp = text->cursor;
410                                 text->cursor.par(text->FirstParagraph());
411                                 text->cursor.pos(0);
412                                 if (!text->GotoNextInset(this, codes, contents)) {
413                                         text->cursor = tmp;
414                                         owner()->message(_("No more insets"));
415                                 }
416                         } else {
417                                 owner()->message(_("No more insets"));
418                         }
419         }
420         update(text, BufferView::SELECT|BufferView::FITCUR);
421         text->selection.cursor = text->cursor;
422 }
423
424
425 void BufferView::gotoInset(Inset::Code code, bool same_content)
426 {
427         gotoInset(vector<Inset::Code>(1, code), same_content);
428 }
429
430
431 void BufferView::insertCorrectQuote()
432 {
433         char c;
434
435         if (text->cursor.pos())
436                 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
437         else 
438                 c = ' ';
439
440         insertInset(new InsetQuotes(c, buffer()->params));
441 }
442
443
444 /* these functions are for the spellchecker */ 
445 string const BufferView::nextWord(float & value)
446 {
447         if (!available()) {
448                 value = 1;
449                 return string();
450         }
451
452         return text->SelectNextWord(this, value);
453 }
454
455   
456 void BufferView::selectLastWord()
457 {
458         if (!available()) return;
459    
460         hideCursor();
461         beforeChange(text);
462         text->SelectSelectedWord(this);
463         toggleSelection(false);
464         update(text, BufferView::SELECT|BufferView::FITCUR);
465 }
466
467
468 void BufferView::endOfSpellCheck()
469 {
470         if (!available()) return;
471    
472         hideCursor();
473         beforeChange(text);
474         text->SelectSelectedWord(this);
475         text->ClearSelection(this);
476         update(text, BufferView::SELECT|BufferView::FITCUR);
477 }
478
479
480 void BufferView::replaceWord(string const & replacestring)
481 {
482         if (!available()) return;
483
484         hideCursor();
485         update(text, BufferView::SELECT|BufferView::FITCUR);
486    
487         /* clear the selection (if there is any) */ 
488         toggleSelection(false);
489         update(text, BufferView::SELECT|BufferView::FITCUR);
490    
491         /* clear the selection (if there is any) */ 
492         toggleSelection(false);
493         text->ReplaceSelectionWithString(this, replacestring);
494    
495         text->SetSelectionOverString(this, replacestring);
496
497         // Go back so that replacement string is also spellchecked
498         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
499                 text->CursorLeft(this);
500         }
501         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
502 }
503 // End of spellchecker stuff
504
505
506 bool BufferView::lockInset(UpdatableInset * inset)
507 {
508         if (!theLockingInset() && inset) {
509                 theLockingInset(inset);
510                 return true;
511         } else if (inset) {
512             return theLockingInset()->LockInsetInInset(this, inset);
513         }
514         return false;
515 }
516
517
518 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
519 {
520         if (theLockingInset() && available()) {
521                 LyXCursor cursor = text->cursor;
522                 if ((cursor.pos() - 1 >= 0) &&
523                     (cursor.par()->GetChar(cursor.pos() - 1) ==
524                      LyXParagraph::META_INSET) &&
525                     (cursor.par()->GetInset(cursor.pos() - 1) ==
526                      theLockingInset()->GetLockingInset()))
527                         text->SetCursor(this, cursor,
528                                         cursor.par(), cursor.pos() - 1);
529                 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
530                 LyXText * txt = getLyXText();
531                 if (theLockingInset()->GetLockingInset()->LyxCode() ==
532                     Inset::TEXT_CODE &&
533                     (txt->real_current_font.language() !=
534                      buffer()->params.language
535                      || txt->real_current_font.isVisibleRightToLeft()
536                      != buffer()->params.language->RightToLeft()))
537                         shape = (txt->real_current_font.isVisibleRightToLeft())
538                                 ? LyXScreen::REVERSED_L_SHAPE
539                                 : LyXScreen::L_SHAPE;
540                 y += cursor.y() + theLockingInset()->InsetInInsetY();
541                 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
542                                                   shape);
543         }
544 }
545
546
547 void BufferView::hideLockedInsetCursor()
548 {
549         if (theLockingInset() && available()) {
550                 pimpl_->screen_->HideCursor();
551         }
552 }
553
554
555 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
556 {
557         if (theLockingInset() && available()) {
558                 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
559                 if (pimpl_->screen_->FitManualCursor(text, this, x, y, asc, desc))
560                         updateScrollbar();
561         }
562 }
563
564
565 int BufferView::unlockInset(UpdatableInset * inset)
566 {
567         if (inset && theLockingInset() == inset) {
568                 inset->InsetUnlock(this);
569                 theLockingInset(0);
570                 text->FinishUndo();
571                 return 0;
572         } else if (inset && theLockingInset() &&
573                    theLockingInset()->UnlockInsetInInset(this, inset)) {
574                 text->FinishUndo();
575                 return 0;
576         }
577         return bufferlist.unlockInset(inset);
578 }
579
580
581 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
582 {
583         if (!theLockingInset())
584                 return; // shouldn't happen
585         if (kind == Undo::EDIT) // in this case insets would not be stored!
586                 kind = Undo::FINISH;
587         text->SetUndo(buffer(), kind,
588                       text->cursor.par()->previous(), 
589                       text->cursor.par()->next());
590 }
591
592
593 void BufferView::updateInset(Inset * inset, bool mark_dirty)
594 {
595         if (!inset)
596                 return;
597
598         // first check for locking insets
599         if (theLockingInset()) {
600                 if (theLockingInset() == inset) {
601                         if (text->UpdateInset(this, inset)) {
602                                 update();
603                                 if (mark_dirty) {
604                                         buffer()->markDirty();
605                                 }
606                                 updateScrollbar();
607                                 return;
608                         }
609                 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
610                         if (text->UpdateInset(this, theLockingInset())) {
611                                 update();
612                                 if (mark_dirty){
613                                         buffer()->markDirty();
614                                 }
615                                 updateScrollbar();
616                                 return;
617                         }
618                 }
619         }
620   
621         // then check the current buffer
622         if (available()) {
623                 hideCursor();
624                 update(text, BufferView::UPDATE);
625                 if (text->UpdateInset(this, inset)) {
626                         if (mark_dirty)
627                                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
628                         else 
629                                 update(text, SELECT);
630                         return;
631                 }
632         }
633 }
634
635
636 bool BufferView::ChangeInsets(Inset::Code code,
637                               string const & from, string const & to)
638 {
639         bool flag = false;
640         LyXParagraph * par = buffer()->paragraph;
641         LyXCursor cursor = text->cursor;
642         LyXCursor tmpcursor = cursor;
643         cursor.par(tmpcursor.par());
644         cursor.pos(tmpcursor.pos());
645
646         while (par) {
647                 bool flag2 = false;
648                 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
649                      it != par->inset_iterator_end(); ++it) {
650                         if ((*it)->LyxCode() == code) {
651                                 InsetCommand * inset = static_cast<InsetCommand *>(*it);
652                                 if (inset->getContents() == from) {
653                                         inset->setContents(to);
654                                         flag2 = true;
655                                 }
656                         }
657                 }
658                 if (flag2) {
659                         flag = true;
660                         // this is possible now, since SetCursor takes
661                         // care about footnotes
662                         text->SetCursorIntern(this, par, 0);
663                         text->RedoParagraphs(this, text->cursor,
664                                              text->cursor.par()->next());
665                         text->FullRebreak(this);
666                 }
667                 par = par->next();
668         }
669         text->SetCursorIntern(this, cursor.par(), cursor.pos());
670         return flag;
671 }
672
673
674 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
675 {
676         // Check if the label 'from' appears more than once
677         vector<string> labels = buffer()->getLabelList();
678         if (count(labels.begin(), labels.end(), from) > 1)
679                 return false;
680
681         return ChangeInsets(Inset::REF_CODE, from, to);
682 }
683
684
685 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
686 {
687
688         vector<pair<string,string> > keys = buffer()->getBibkeyList();  
689         if (count_if(keys.begin(), keys.end(), 
690                      lyx::equal_1st_in_pair<string,string>(from)) 
691             > 1)
692                 return false;
693
694         return ChangeInsets(Inset::CITE_CODE, from, to);
695 }
696
697 UpdatableInset * BufferView::theLockingInset() const
698 {
699         // If NULL is not allowed we should put an Assert here. (Lgb)
700         if (text)
701                 return text->the_locking_inset;
702         return 0;
703 }
704
705
706 void BufferView::theLockingInset(UpdatableInset * inset)
707 {
708         text->the_locking_inset = inset;
709 }
710
711
712 LyXText * BufferView::getLyXText() const
713 {
714         if (theLockingInset()) {
715                 LyXText * txt = theLockingInset()->getLyXText(this, true);
716                 if (txt)
717                         return txt;
718         }
719         return text;
720 }
721
722
723 LyXText * BufferView::getParentText(Inset * inset) const
724 {
725         if (inset->owner()) {
726                 LyXText * txt = inset->getLyXText(this);
727                 inset = inset->owner();
728                 while (inset && inset->getLyXText(this) == txt)
729                         inset = inset->owner();
730                 if (inset)
731                         return inset->getLyXText(this);
732         }
733         return text;
734 }
735
736
737 Language const * BufferView::getParentLanguage(Inset * inset) const
738 {
739         LyXText * text = getParentText(inset);
740         return text->cursor.par()->GetFontSettings(buffer()->params,
741                                                    text->cursor.pos()).language();
742 }