]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCompleter.cpp
This is the last of the commits that hopes to enforce the distinction between "layout...
[lyx.git] / src / frontends / qt4 / GuiCompleter.cpp
1 /**
2  * \file GuiCompleter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Stefan Schimanski
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiWorkArea.h"
14
15 #include "Buffer.h"
16 #include "BufferView.h"
17 #include "Cursor.h"
18 #include "Dimension.h"
19 #include "FuncRequest.h"
20 #include "GuiView.h"
21 #include "LyXFunc.h"
22 #include "LyXRC.h"
23 #include "Paragraph.h"
24 #include "version.h"
25
26 #include "support/debug.h"
27
28 #include <QApplication>
29 #include <QAbstractListModel>
30 #include <QHeaderView>
31 #include <QPainter>
32 #include <QPixmapCache>
33 #include <QScrollBar>
34 #include <QItemDelegate>
35 #include <QTreeView>
36 #include <QTimer>
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42 namespace frontend {
43
44 class RtlItemDelegate : public QItemDelegate {
45 public:
46         explicit RtlItemDelegate(QObject * parent = 0)
47                 : QItemDelegate(parent) {}
48
49 protected:
50         virtual void drawDisplay(QPainter * painter,
51                 QStyleOptionViewItem const & option,
52                 QRect const & rect, QString const & text) const
53         {
54                 // FIXME: do this more elegantly
55                 docstring stltext = qstring_to_ucs4(text);
56                 reverse(stltext.begin(), stltext.end());
57                 QItemDelegate::drawDisplay(painter, option, rect, toqstr(stltext));
58         }
59 };
60
61
62 class PixmapItemDelegate : public QItemDelegate {
63 public:
64         explicit PixmapItemDelegate(QObject *parent = 0)
65         : QItemDelegate(parent) {}
66
67 protected:
68         void paint(QPainter *painter, const QStyleOptionViewItem &option,
69                    const QModelIndex &index) const
70         {
71                 QStyleOptionViewItem opt = setOptions(index, option);
72                 QVariant value = index.data(Qt::DisplayRole);
73                 QPixmap pixmap = qvariant_cast<QPixmap>(value);
74                 
75                 // draw
76                 painter->save();
77                 drawBackground(painter, opt, index);
78                 if (!pixmap.isNull()) {
79                         const QSize size = pixmap.size();
80                         painter->drawPixmap(option.rect.left() + (16 - size.width()) / 2,
81                                 option.rect.top() + (option.rect.height() - size.height()) / 2,
82                                 pixmap);
83                 }
84                 drawFocus(painter, opt, option.rect);
85                 painter->restore();
86         }
87 };
88
89
90 class GuiCompletionModel : public QAbstractListModel {
91 public:
92         ///
93         GuiCompletionModel(QObject * parent,
94                 Inset::CompletionList const * l)
95                 : QAbstractListModel(parent), list_(l) {}
96         ///
97         ~GuiCompletionModel()
98                 { delete list_; }
99         ///
100         bool sorted() const
101         {
102                 if (list_)
103                         return list_->sorted();
104                 else
105                         return false;
106         }
107         ///
108         int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const
109         {
110                 return 2;
111         }
112         ///
113         int rowCount(const QModelIndex & /*parent*/ = QModelIndex()) const
114         {
115                 if (list_ == 0)
116                         return 0;
117                 else
118                         return list_->size();
119         }
120
121         ///
122         QVariant data(const QModelIndex & index, int role) const
123         {
124                 if (list_ == 0)
125                         return QVariant();
126
127                 if (index.row() < 0 || index.row() >= rowCount())
128                         return QVariant();
129
130                 if (role != Qt::DisplayRole && role != Qt::EditRole)
131                     return QVariant();
132                     
133                 if (index.column() == 0)
134                         return toqstr(list_->data(index.row()));
135                 else if (index.column() == 1) {
136                         // get icon from cache
137                         QPixmap scaled;
138                         QString const name = ":" + toqstr(list_->icon(index.row()));
139                         if (!QPixmapCache::find("completion" + name, scaled)) {
140                                 // load icon from disk
141                                 QPixmap p = QPixmap(name);
142                                 if (!p.isNull()) {
143                                         // scale it to 16x16 or smaller
144                                         scaled = p.scaled(min(16, p.width()), min(16, p.height()), 
145                                                 Qt::KeepAspectRatio, Qt::SmoothTransformation);
146                                 }
147
148                                 QPixmapCache::insert("completion" + name, scaled);
149                         }
150                         return scaled;
151                 }
152                 return QVariant();
153         }
154
155 private:
156         ///
157         Inset::CompletionList const * list_;
158 };
159
160
161 GuiCompleter::GuiCompleter(GuiWorkArea * gui, QObject * parent)
162         : QCompleter(parent), gui_(gui), updateLock_(0),
163           inlineVisible_(false)
164 {
165         // Setup the completion popup
166         setModel(new GuiCompletionModel(this, 0));
167         setCompletionMode(QCompleter::PopupCompletion);
168         setWidget(gui_);
169         
170         // create the popup
171         QTreeView *listView = new QTreeView;
172         listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
173         listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
174         listView->setSelectionBehavior(QAbstractItemView::SelectRows);
175         listView->setSelectionMode(QAbstractItemView::SingleSelection);
176         listView->header()->hide();
177         listView->setIndentation(0);
178         listView->setUniformRowHeights(true);
179         setPopup(listView);
180         popup()->setItemDelegateForColumn(1, new PixmapItemDelegate(this));
181         rtlItemDelegate_ = new RtlItemDelegate(this);
182         
183         // create timeout timers
184         popup_timer_.setSingleShot(true);
185         inline_timer_.setSingleShot(true);
186         connect(this, SIGNAL(highlighted(const QString &)),
187                 this, SLOT(popupHighlighted(const QString &)));
188         connect(this, SIGNAL(activated(const QString &)),
189                 this, SLOT(popupActivated(const QString &)));
190         connect(&popup_timer_, SIGNAL(timeout()),
191                 this, SLOT(showPopup()));
192         connect(&inline_timer_, SIGNAL(timeout()),
193                 this, SLOT(showInline()));
194 }
195
196
197 GuiCompleter::~GuiCompleter()
198 {
199         popup()->hide();
200 }
201
202
203 bool GuiCompleter::eventFilter(QObject * watched, QEvent * e)
204 {
205         // hijack back the tab key from the popup
206         // (which stole it from the workspace before)
207         if (e->type() == QEvent::KeyPress && popupVisible()) {
208                 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
209                 switch (ke->key()) {
210                 case Qt::Key_Tab:
211                         tab();
212                         ke->accept();
213                         return true;
214                 default: break;
215                 }
216         }
217         
218         return QCompleter::eventFilter(watched, e);
219 }
220
221
222 bool GuiCompleter::popupPossible(Cursor const & cur) const
223 {
224         return QApplication::activeWindow()
225                 && gui_->hasFocus()
226                 && cur.inset().completionSupported(cur);
227 }
228
229
230 bool GuiCompleter::inlinePossible(Cursor const & cur) const
231 {
232         return cur.inset().inlineCompletionSupported(cur);
233 }
234
235
236 bool GuiCompleter::completionAvailable() const
237 {
238         size_t n = popup()->model()->rowCount();
239
240         // if there is exactly one, we have to check whether it is a 
241         // real completion, i.e. longer than the current prefix.
242         if (n == 1 && completionPrefix() == currentCompletion())
243             return false;
244
245         return n > 0;
246 }
247
248
249 bool GuiCompleter::popupVisible() const
250 {
251         return popup()->isVisible();
252 }
253
254
255 bool GuiCompleter::inlineVisible() const
256 {
257         // In fact using BufferView::inlineCompletionPos.empty() should be
258         // here. But unfortunately this information is not good enough
259         // because destructive operations like backspace might invalidate
260         // inlineCompletionPos. But then the completion should stay visible
261         // (i.e. reshown on the next update). Hence be keep this information
262         // in the inlineVisible_ variable.
263         return inlineVisible_;
264 }
265
266
267 void GuiCompleter::updateVisibility(Cursor & cur, bool start, bool keep, bool cursorInView)
268 {
269         // parameters which affect the completion
270         bool moved = cur != old_cursor_;
271         if (moved)
272                 old_cursor_ = cur;
273
274         bool possiblePopupState = popupPossible(cur) && cursorInView;
275         bool possibleInlineState = inlinePossible(cur) && cursorInView;
276
277         // we moved or popup state is not ok for popup?
278         if ((moved && !keep) || !possiblePopupState)
279                 hidePopup(cur);
280
281         // we moved or inline state is not ok for inline completion?
282         if ((moved && !keep) || !possibleInlineState)
283                 hideInline(cur);
284
285         // we inserted something and are in a possible popup state?
286         if (!popupVisible() && possiblePopupState && start
287                 && cur.inset().automaticPopupCompletion())
288                 popup_timer_.start(int(lyxrc.completion_popup_delay * 1000));
289
290         // we inserted something and are in a possible inline completion state?
291         if (!inlineVisible() && possibleInlineState && start
292                 && cur.inset().automaticInlineCompletion())
293                 inline_timer_.start(int(lyxrc.completion_inline_delay * 1000));
294
295         // update prefix if any completion is possible
296         bool modelActive = model()->rowCount() > 0;
297         if (possiblePopupState || possibleInlineState) {
298                 if (modelActive)
299                         updatePrefix(cur);
300                 else
301                         updateAvailability();
302         }
303 }
304
305
306 void GuiCompleter::updateVisibility(bool start, bool keep)
307 {
308         Cursor cur = gui_->bufferView().cursor();
309         cur.updateFlags(Update::None);
310         
311         updateVisibility(cur, start, keep);
312         
313         if (cur.disp_.update())
314                 gui_->bufferView().processUpdateFlags(cur.disp_.update());
315 }
316
317
318 void GuiCompleter::updatePrefix(Cursor & cur)
319 {
320         // get new prefix. Do nothing if unchanged
321         QString newPrefix = toqstr(cur.inset().completionPrefix(cur));
322         if (newPrefix == completionPrefix())
323                 return;
324         
325         // value which should be kept selected
326         QString old = currentCompletion();
327         if (old.length() == 0)
328                 old = last_selection_;
329         
330         // update completer to new prefix
331         setCompletionPrefix(newPrefix);
332
333         // update popup because its size might have changed
334         if (popupVisible())
335                 updatePopup(cur);
336
337         // restore old selection
338         setCurrentCompletion(old);
339         
340         // if popup is not empty, the new selection will
341         // be our last valid one
342         QString const & s = currentCompletion();
343         if (popupVisible() || inlineVisible()) {
344                 if (s.length() > 0)
345                         last_selection_ = s;
346                 else
347                         last_selection_ = old;
348         }
349
350         // update inline completion because the default
351         // completion string might have changed
352         if (inlineVisible())
353                 updateInline(cur, s);
354 }
355
356
357 void GuiCompleter::updateInline(Cursor & cur, QString const & completion)
358 {
359         if (!cur.inset().inlineCompletionSupported(cur))
360                 return;
361         
362         // compute postfix
363         docstring prefix = cur.inset().completionPrefix(cur);
364         docstring postfix = from_utf8(fromqstr(completion.mid(prefix.length())));
365         
366         // shorten it if necessary
367         if (lyxrc.completion_inline_dots != -1
368             && postfix.size() > unsigned(lyxrc.completion_inline_dots))
369                 postfix = postfix.substr(0, lyxrc.completion_inline_dots - 1) + "...";
370
371         // set inline completion at cursor position
372         size_t uniqueTo = max(longestUniqueCompletion().size(), prefix.size());
373         gui_->bufferView().setInlineCompletion(cur, cur, postfix, uniqueTo - prefix.size());
374         inlineVisible_ = true;
375 }
376
377
378 void GuiCompleter::updatePopup(Cursor & cur)
379 {
380         if (!cur.inset().completionSupported(cur))
381                 return;
382         
383         if (completionCount() == 0)
384                 return;
385         
386         // get dimensions of completion prefix
387         Dimension dim;
388         int x;
389         int y;
390         cur.inset().completionPosAndDim(cur, x, y, dim);
391         
392         // and calculate the rect of the popup
393         QRect rect;
394         if (popup()->layoutDirection() == Qt::RightToLeft)
395                 rect = QRect(x + dim.width() - 200, y - dim.ascent() - 3, 200, dim.height() + 6);
396         else
397                 rect = QRect(x, y - dim.ascent() - 3, 200, dim.height() + 6);
398         
399         // show/update popup
400         QTreeView * p = static_cast<QTreeView *>(popup());
401         p->setColumnWidth(0, popup()->width() - 22 - p->verticalScrollBar()->width());
402
403         complete(rect);
404 }
405
406
407 void GuiCompleter::updateAvailability()
408 {
409         // this should really only be of interest if no completion is
410         // visible yet, i.e. especially if automatic completion is disabled.
411         if (inlineVisible() || popupVisible())
412                 return;
413         Cursor & cur = gui_->bufferView().cursor();
414         if (!popupPossible(cur) && !inlinePossible(cur))
415                 return;
416         
417         updateModel(cur, false, false);
418 }
419         
420
421 void GuiCompleter::updateModel(Cursor & cur, bool popupUpdate, bool inlineUpdate)
422 {
423         // value which should be kept selected
424         QString old = currentCompletion();
425         if (old.length() == 0)
426                 old = last_selection_;
427
428         // set whether rtl
429         bool rtl = false;
430         if (cur.inTexted()) {
431                 Paragraph const & par = cur.paragraph();
432                 Font const font =
433                 par.getFontSettings(cur.bv().buffer().params(), cur.pos());
434                 rtl = font.isVisibleRightToLeft();
435         }
436         popup()->setLayoutDirection(rtl ? Qt::RightToLeft : Qt::LeftToRight);
437
438         // turn the direction of the strings in the popup.
439         // Qt does not do that itself.
440         popup()->setItemDelegateForColumn(0, rtl ? rtlItemDelegate_ : 0);
441
442         // set new model
443         Inset::CompletionList const * list = cur.inset().createCompletionList(cur);
444         setModel(new GuiCompletionModel(this, list));
445         if (list->sorted())
446                 setModelSorting(QCompleter::CaseSensitivelySortedModel);
447         else
448                 setModelSorting(QCompleter::UnsortedModel);
449
450         // set prefix
451         QString newPrefix = toqstr(cur.inset().completionPrefix(cur));
452         if (newPrefix != completionPrefix())
453                 setCompletionPrefix(newPrefix);
454
455         // show popup
456         if (popupUpdate)
457                 updatePopup(cur);
458
459         // restore old selection
460         setCurrentCompletion(old);
461         
462         // if popup is not empty, the new selection will
463         // be our last valid one
464         if (popupVisible() || inlineVisible()) {
465                 QString const & s = currentCompletion();
466                 if (s.length() > 0)
467                         last_selection_ = s;
468                 else
469                         last_selection_ = old;
470         }
471
472         // show inline completion
473         if (inlineUpdate)
474                 updateInline(cur, currentCompletion());
475 }
476
477
478 void GuiCompleter::showPopup(Cursor & cur)
479 {
480         if (!popupPossible(cur))
481                 return;
482         
483         updateModel(cur, true, inlineVisible());
484 }
485
486
487 void GuiCompleter::hidePopup(Cursor & cur)
488 {
489         popup()->hide();
490         if (popup_timer_.isActive())
491                 popup_timer_.stop();
492         
493         if (!inlineVisible())
494                 setModel(new GuiCompletionModel(this, 0));
495 }
496
497
498 void GuiCompleter::showInline(Cursor & cur)
499 {
500         if (!inlinePossible(cur))
501                 return;
502         
503         updateModel(cur, popupVisible(), true);
504 }
505
506
507 void GuiCompleter::hideInline(Cursor & cur)
508 {
509         gui_->bufferView().setInlineCompletion(cur, DocIterator(), docstring());
510         inlineVisible_ = false;
511         
512         if (!popupVisible())
513                 setModel(new GuiCompletionModel(this, 0));
514 }
515
516
517 void GuiCompleter::showPopup()
518 {
519         Cursor cur = gui_->bufferView().cursor();
520         cur.updateFlags(Update::None);
521         
522         showPopup(cur);
523
524         // redraw if needed
525         if (cur.disp_.update())
526                 gui_->bufferView().processUpdateFlags(cur.disp_.update());
527 }
528
529
530 void GuiCompleter::showInline()
531 {
532         Cursor cur = gui_->bufferView().cursor();
533         cur.updateFlags(Update::None);
534         
535         showInline(cur);
536
537         // redraw if needed
538         if (cur.disp_.update())
539                 gui_->bufferView().processUpdateFlags(cur.disp_.update());
540 }
541
542
543 void GuiCompleter::hidePopup()
544 {
545         Cursor cur = gui_->bufferView().cursor();
546         cur.updateFlags(Update::None);
547         
548         hidePopup(cur);
549         
550         // redraw if needed
551         if (cur.disp_.update())
552                 gui_->bufferView().processUpdateFlags(cur.disp_.update());
553 }
554
555
556 void GuiCompleter::hideInline()
557 {
558         Cursor cur = gui_->bufferView().cursor();
559         cur.updateFlags(Update::None);
560         
561         hideInline(cur);
562         
563         // redraw if needed
564         if (cur.disp_.update())
565                 gui_->bufferView().processUpdateFlags(cur.disp_.update());
566 }
567
568
569 void GuiCompleter::activate()
570 {
571         if (!popupVisible() && !inlineVisible())
572                 return;
573
574         popupActivated(currentCompletion());
575 }
576
577
578 void GuiCompleter::tab()
579 {
580         BufferView * bv = &gui_->bufferView();
581         Cursor cur = bv->cursor();
582         cur.updateFlags(Update::None);
583         
584         // check that inline completion is active
585         if (!inlineVisible()) {
586                 // try to activate the inline completion
587                 if (cur.inset().inlineCompletionSupported(cur)) {
588                         showInline();
589                         
590                         // show popup without delay because the completion was not unique
591                         if (lyxrc.completion_popup_after_complete
592                             && !popupVisible()
593                             && popup()->model()->rowCount() > 1)
594                                 popup_timer_.start(0);
595
596                         return;
597                 }
598                 // or try popup
599                 if (!popupVisible() && cur.inset().completionSupported(cur)) {
600                         showPopup();
601                         return;
602                 }
603                 
604                 return;
605         }
606         
607         // If completion is active, at least complete by one character
608         docstring prefix = cur.inset().completionPrefix(cur);
609         docstring completion = from_utf8(fromqstr(currentCompletion()));
610         if (completion.size() <= prefix.size()) {
611                 // finalize completion
612                 cur.inset().insertCompletion(cur, docstring(), true);
613                 
614                 // hide popup and inline completion
615                 hidePopup(cur);
616                 hideInline(cur);
617                 updateVisibility(false, false);
618                 return;
619         }
620         docstring nextchar = completion.substr(prefix.size(), 1);
621         if (!cur.inset().insertCompletion(cur, nextchar, false))
622                 return;
623         updatePrefix(cur);
624
625         // try to complete as far as it is unique
626         docstring longestCompletion = longestUniqueCompletion();
627         prefix = cur.inset().completionPrefix(cur);
628         docstring postfix = longestCompletion.substr(min(longestCompletion.size(), prefix.size()));
629         cur.inset().insertCompletion(cur, postfix, false);
630         old_cursor_ = bv->cursor();
631         updatePrefix(cur);
632
633         // show popup without delay because the completion was not unique
634         if (lyxrc.completion_popup_after_complete
635             && !popupVisible()
636             && popup()->model()->rowCount() > 1)
637                 popup_timer_.start(0);
638
639         // redraw if needed
640         if (cur.disp_.update())
641                 gui_->bufferView().processUpdateFlags(cur.disp_.update());
642 }
643
644
645 QString GuiCompleter::currentCompletion() const
646 {
647         if (!popup()->selectionModel()->hasSelection())
648                 return QString();
649
650         // Not sure if this is bug in Qt: currentIndex() always 
651         // return the first element in the list.
652         QModelIndex idx = popup()->currentIndex();
653         return popup()->model()->data(idx, Qt::EditRole).toString();
654 }
655
656
657 void GuiCompleter::setCurrentCompletion(QString const & s)
658 {       
659         QAbstractItemModel const & model = *popup()->model();
660         size_t n = model.rowCount();
661         if (n == 0)
662                 return;
663
664         // select the first if s is empty
665         if (s.length() == 0) {
666                 updateLock_++;
667                 popup()->setCurrentIndex(model.index(0, 0));
668                 updateLock_--;
669                 return;
670         }
671
672         // find old selection in model
673         size_t i;
674         if (modelSorting() == QCompleter::UnsortedModel) {
675                 // In unsorted models, iterate through list until the s is found
676                 for (i = 0; i < n; ++i) {
677                         QString const & is
678                         = model.data(model.index(i, 0), Qt::EditRole).toString();
679                         if (is == s)
680                                 break;
681                 }
682         } else {
683                 // In sorted models, do binary search for s.
684                 int l = 0;
685                 int r = n - 1;
686                 while (r >= l && l < int(n)) {
687                         size_t mid = (r + l) / 2;
688                         QString const & mids
689                         = model.data(model.index(mid, 0),
690                                      Qt::EditRole).toString();
691
692                         // left or right?
693                         // FIXME: is this really the same order that the docstring
694                         // from the CompletionList has?
695                         int c = s.compare(mids, Qt::CaseSensitive);
696                         if (c == 0) {
697                                 l = mid;
698                                 break;
699                         } else if (l == r) {
700                                 l = n;
701                                 break;
702                         } else if (c > 0)
703                                 // middle is not far enough
704                                 l = mid + 1;
705                         else
706                                 // middle is too far
707                                 r = mid - 1;
708                 }
709
710                 // loop was left without finding anything
711                 if (r < l)
712                         i = n;
713                 else
714                         i = l;
715                 BOOST_ASSERT(0 <= i && i <= n);
716         }
717
718         // select the first if none was found
719         if (i == n)
720                 i = 0;
721
722         updateLock_++;
723         popup()->setCurrentIndex(model.index(i, 0));
724         updateLock_--;
725 }
726
727
728 size_t commonPrefix(QString const & s1, QString const & s2)
729 {
730         // find common prefix
731         size_t j;
732         size_t n1 = s1.length();
733         size_t n2 = s2.length();
734         for (j = 0; j < n1 && j < n2; ++j) {
735                 if (s1.at(j) != s2.at(j))
736                         break;
737         }
738         return j;
739 }
740
741
742 docstring GuiCompleter::longestUniqueCompletion() const
743 {
744         QAbstractItemModel const & model = *popup()->model();
745         size_t n = model.rowCount();
746         if (n == 0)
747                 return docstring();
748         QString s = model.data(model.index(0, 0), Qt::EditRole).toString();
749         
750         if (modelSorting() == QCompleter::UnsortedModel) {
751                 // For unsorted model we cannot do more than iteration.
752                 // Iterate through the completions and cut off where s differs
753                 for (size_t i = 0; i < n && s.length() > 0; ++i) {
754                         QString const & is
755                         = model.data(model.index(i, 0), Qt::EditRole).toString();
756
757                         s = s.left(commonPrefix(is, s));
758                 }
759         } else {
760                 // For sorted models we can do binary search multiple times,
761                 // each time to find the first string which has s not as prefix.
762                 size_t i = 0;
763                 while (i < n && s.length() > 0) {
764                         // find first string that does not have s as prefix
765                         // via binary search in [i,n-1]
766                         size_t r = n - 1;
767                         do {
768                                 // get common prefix with the middle string
769                                 size_t mid = (r + i) / 2;
770                                 QString const & mids
771                                 = model.data(model.index(mid, 0), 
772                                         Qt::EditRole).toString();
773                                 size_t oldLen = s.length();
774                                 size_t len = commonPrefix(mids, s);
775                                 s = s.left(len);
776
777                                 // left or right?
778                                 if (oldLen == len) {
779                                         // middle is not far enough
780                                         i = mid + 1;
781                                 } else {
782                                         // middle is maybe too far
783                                         r = mid;
784                                 }
785                         } while (r - i > 0 && i < n);
786                 }
787         }
788
789         return from_utf8(fromqstr(s));
790 }
791
792
793 void GuiCompleter::popupActivated(const QString & completion)
794 {
795         Cursor cur = gui_->bufferView().cursor();
796         cur.updateFlags(Update::None);
797         
798         docstring prefix = cur.inset().completionPrefix(cur);
799         docstring postfix = from_utf8(fromqstr(completion.mid(prefix.length())));
800         cur.inset().insertCompletion(cur, postfix, true);
801         hidePopup(cur);
802         hideInline(cur);
803         
804         if (cur.disp_.update())
805                 gui_->bufferView().processUpdateFlags(cur.disp_.update());
806 }
807
808
809 void GuiCompleter::popupHighlighted(const QString & completion)
810 {
811         if (updateLock_ > 0)
812                 return;
813
814         Cursor cur = gui_->bufferView().cursor();
815         cur.updateFlags(Update::None);
816         
817         if (inlineVisible())
818                 updateInline(cur, completion);
819         
820         if (cur.disp_.update())
821                 gui_->bufferView().processUpdateFlags(cur.disp_.update());
822 }
823
824 } // namespace frontend
825 } // namespace lyx
826
827 #include "GuiCompleter_moc.cpp"