]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCitation.cpp
Fix string
[lyx.git] / src / frontends / qt4 / GuiCitation.cpp
1
2 /**
3  * \file GuiCitation.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  * \author Kalle Dalheimer
9  * \author Abdelrazak Younes
10  * \author Richard Heck
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "GuiCitation.h"
18
19 #include "GuiApplication.h"
20 #include "GuiSelectionManager.h"
21 #include "LyXToolBox.h"
22 #include "qt_helpers.h"
23
24 #include "Buffer.h"
25 #include "BufferView.h"
26 #include "BiblioInfo.h"
27 #include "BufferParams.h"
28 #include "FuncRequest.h"
29
30 #include "insets/InsetCommand.h"
31
32 #include "support/debug.h"
33 #include "support/docstring.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36
37 #include <QCloseEvent>
38 #include <QMenu>
39 #include <QSettings>
40 #include <QShowEvent>
41 #include <QVariant>
42
43 #include <vector>
44 #include <string>
45
46 #undef KeyPress
47
48 #include "support/regex.h"
49
50 #include <algorithm>
51 #include <string>
52 #include <vector>
53
54 using namespace std;
55 using namespace lyx::support;
56
57 namespace lyx {
58 namespace frontend {
59
60 // FIXME THREAD
61 // I am guessing that it would not hurt to make these private members.
62 static vector<string> citeCmds_;
63 static vector<CitationStyle> citeStyles_;
64
65
66 template<typename String>
67 static QStringList to_qstring_list(vector<String> const & v)
68 {
69         QStringList qlist;
70
71         for (size_t i = 0; i != v.size(); ++i) {
72                 if (v[i].empty())
73                         continue;
74                 qlist.append(lyx::toqstr(v[i]));
75         }
76         return qlist;
77 }
78
79
80 static vector<lyx::docstring> to_docstring_vector(QStringList const & qlist)
81 {
82         vector<lyx::docstring> v;
83         for (int i = 0; i != qlist.size(); ++i) {
84                 if (qlist[i].isEmpty())
85                         continue;
86                 v.push_back(lyx::qstring_to_ucs4(qlist[i]));
87         }
88         return v;
89 }
90
91
92 GuiCitation::GuiCitation(GuiView & lv)
93         : DialogView(lv, "citation", qt_("Citation")),
94           style_(0), params_(insetCode("citation"))
95 {
96         setupUi(this);
97
98         // The filter bar
99         filter_ = new FancyLineEdit(this);
100 #if QT_VERSION >= 0x040600
101         filter_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png"));
102         filter_->setButtonVisible(FancyLineEdit::Right, true);
103         filter_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
104         filter_->setAutoHideButton(FancyLineEdit::Right, true);
105 #endif
106 #if QT_VERSION >= 0x040700
107         filter_->setPlaceholderText(qt_("All avail. citations"));
108 #endif
109
110         filterBarL->addWidget(filter_, 0);
111         findKeysLA->setBuddy(filter_);
112
113         // Add search options as button menu
114         regexp_ = new QAction(qt_("Regular e&xpression"), this);
115         regexp_->setCheckable(true);
116         casesense_ = new QAction(qt_("Case se&nsitive"), this);
117         casesense_->setCheckable(true);
118         instant_ = new QAction(qt_("Search as you &type"), this);
119         instant_->setCheckable(true);
120
121         QMenu * searchOpts = new QMenu(this);
122         searchOpts->addAction(regexp_);
123         searchOpts->addAction(casesense_);
124         searchOpts->addAction(instant_);
125         searchOptionsPB->setMenu(searchOpts);
126
127         connect(citationStyleCO, SIGNAL(activated(int)),
128                 this, SLOT(on_citationStyleCO_currentIndexChanged(int)));
129         connect(fulllistCB, SIGNAL(clicked()),
130                 this, SLOT(changed()));
131         connect(forceuppercaseCB, SIGNAL(clicked()),
132                 this, SLOT(changed()));
133         connect(textBeforeED, SIGNAL(textChanged(QString)),
134                 this, SLOT(updateStyles()));
135         connect(textAfterED, SIGNAL(textChanged(QString)),
136                 this, SLOT(updateStyles()));
137         connect(textBeforeED, SIGNAL(returnPressed()),
138                 this, SLOT(on_okPB_clicked()));
139         connect(textAfterED, SIGNAL(returnPressed()),
140                 this, SLOT(on_okPB_clicked()));
141
142         selectionManager = new GuiSelectionManager(availableLV, selectedLV,
143                         addPB, deletePB, upPB, downPB, &available_model_, &selected_model_);
144         connect(selectionManager, SIGNAL(selectionChanged()),
145                 this, SLOT(setCitedKeys()));
146         connect(selectionManager, SIGNAL(updateHook()),
147                 this, SLOT(updateControls()));
148         connect(selectionManager, SIGNAL(okHook()),
149                 this, SLOT(on_okPB_clicked()));
150
151         connect(filter_, SIGNAL(rightButtonClicked()),
152                 this, SLOT(resetFilter()));
153         connect(filter_, SIGNAL(textEdited(QString)),
154                 this, SLOT(filterChanged(QString)));
155         connect(filter_, SIGNAL(returnPressed()),
156                 this, SLOT(filterPressed()));
157         connect(regexp_, SIGNAL(triggered()),
158                 this, SLOT(regexChanged()));
159         connect(casesense_, SIGNAL(triggered()),
160                 this, SLOT(caseChanged()));
161         connect(instant_, SIGNAL(triggered(bool)),
162                 this, SLOT(instantChanged(bool)));
163
164         setFocusProxy(filter_);
165 }
166
167
168 GuiCitation::~GuiCitation()
169 {
170         delete selectionManager;
171 }
172
173
174 void GuiCitation::closeEvent(QCloseEvent * e)
175 {
176         clearSelection();
177         DialogView::closeEvent(e);
178 }
179
180
181 void GuiCitation::applyView()
182 {
183         int const choice = max(0, citationStyleCO->currentIndex());
184         style_ = choice;
185         bool const full  = fulllistCB->isChecked();
186         bool const force = forceuppercaseCB->isChecked();
187
188         QString const before = textBeforeED->text();
189         QString const after = textAfterED->text();
190
191         applyParams(choice, full, force, before, after);
192 }
193
194
195 void GuiCitation::showEvent(QShowEvent * e)
196 {
197         filter_->clear();
198         availableLV->setFocus();
199         DialogView::showEvent(e);
200 }
201
202
203 void GuiCitation::on_okPB_clicked()
204 {
205         applyView();
206         clearSelection();
207         hide();
208 }
209
210
211 void GuiCitation::on_cancelPB_clicked()
212 {
213         clearSelection();
214         hide();
215 }
216
217
218 void GuiCitation::on_applyPB_clicked()
219 {
220         applyView();
221 }
222
223
224 void GuiCitation::on_restorePB_clicked()
225 {
226         init();
227         updateFilterHint();
228 }
229
230
231 void GuiCitation::updateControls()
232 {
233         BiblioInfo const & bi = bibInfo();
234         updateControls(bi);
235 }
236
237
238 // The main point of separating this out is that the fill*() methods
239 // called in update() do not need to be called for INTERNAL updates,
240 // such as when addPB is pressed, as the list of fields, entries, etc,
241 // will not have changed.
242 void GuiCitation::updateControls(BiblioInfo const & bi)
243 {
244         QModelIndex idx = selectionManager->getSelectedIndex();
245         updateInfo(bi, idx);
246         selectionManager->update();
247 }
248
249
250 void GuiCitation::updateFormatting(CitationStyle currentStyle)
251 {
252         bool const force = currentStyle.forceUpperCase;
253         bool const full = currentStyle.fullAuthorList &&
254                 documentBuffer().params().fullAuthorList();
255         bool const textbefore = currentStyle.textBefore;
256         bool const textafter = currentStyle.textAfter;
257
258         bool const haveSelection =
259                 selectedLV->model()->rowCount() > 0;
260
261         forceuppercaseCB->setEnabled(force && haveSelection);
262         fulllistCB->setEnabled(full && haveSelection);
263         textBeforeED->setEnabled(textbefore && haveSelection);
264         textBeforeLA->setEnabled(textbefore && haveSelection);
265         textAfterED->setEnabled(textafter && haveSelection);
266         textAfterLA->setEnabled(textafter && haveSelection);
267         citationStyleCO->setEnabled(haveSelection);
268         citationStyleLA->setEnabled(haveSelection);
269 }
270
271
272 // Update the styles for the style combo, citationStyleCO, and mark the
273 // settings as changed. Called upon changing the cited keys (including
274 // merely reordering the keys) or editing the text before/after fields.
275 void GuiCitation::updateStyles()
276 {
277         BiblioInfo const & bi = bibInfo();
278         updateStyles(bi);
279         changed();
280 }
281
282
283 // Update the styles for the style combo, citationStyleCO.
284 void GuiCitation::updateStyles(BiblioInfo const & bi)
285 {
286         QStringList selected_keys = selected_model_.stringList();
287         int curr = selectedLV->model()->rowCount() - 1;
288
289         if (curr < 0 || selected_keys.empty()) {
290                 citationStyleCO->clear();
291                 citationStyleCO->setEnabled(false);
292                 citationStyleLA->setEnabled(false);
293                 return;
294         }
295
296         static const size_t max_length = 80;
297         QStringList sty = citationStyles(bi, max_length);
298
299         if (sty.isEmpty()) {
300                 // some error
301                 citationStyleCO->setEnabled(false);
302                 citationStyleLA->setEnabled(false);
303                 citationStyleCO->clear();
304                 return;
305         }
306
307         citationStyleCO->blockSignals(true);
308
309         // save old index
310         int const curindex = citationStyleCO->currentIndex();
311         int const oldIndex = (curindex < 0) ? style_ : curindex;
312         citationStyleCO->clear();
313         citationStyleCO->insertItems(0, sty);
314         citationStyleCO->setEnabled(true);
315         citationStyleLA->setEnabled(true);
316         // restore old index
317         if (oldIndex != -1 && oldIndex < citationStyleCO->count())
318                 citationStyleCO->setCurrentIndex(oldIndex);
319
320         citationStyleCO->blockSignals(false);
321 }
322
323
324 void GuiCitation::fillFields(BiblioInfo const & bi)
325 {
326         fieldsCO->blockSignals(true);
327         int const oldIndex = fieldsCO->currentIndex();
328         fieldsCO->clear();
329         QStringList const fields = to_qstring_list(bi.getFields());
330         fieldsCO->insertItem(0, qt_("All fields"));
331         fieldsCO->insertItem(1, qt_("Keys"));
332         fieldsCO->insertItems(2, fields);
333         if (oldIndex != -1 && oldIndex < fieldsCO->count())
334                 fieldsCO->setCurrentIndex(oldIndex);
335         fieldsCO->blockSignals(false);
336 }
337
338
339 void GuiCitation::fillEntries(BiblioInfo const & bi)
340 {
341         entriesCO->blockSignals(true);
342         int const oldIndex = entriesCO->currentIndex();
343         entriesCO->clear();
344         QStringList const entries = to_qstring_list(bi.getEntries());
345         entriesCO->insertItem(0, qt_("All entry types"));
346         entriesCO->insertItems(1, entries);
347         if (oldIndex != -1 && oldIndex < entriesCO->count())
348                 entriesCO->setCurrentIndex(oldIndex);
349         entriesCO->blockSignals(false);
350 }
351
352
353 bool GuiCitation::isSelected(QModelIndex const & idx)
354 {
355         QString const str = idx.data().toString();
356         return selected_model_.stringList().contains(str);
357 }
358
359
360 void GuiCitation::setButtons()
361 {
362         int const srows = selectedLV->model()->rowCount();
363         applyPB->setEnabled(srows > 0);
364         okPB->setEnabled(srows > 0);
365 }
366
367
368 void GuiCitation::updateInfo(BiblioInfo const & bi, QModelIndex const & idx)
369 {
370         if (!idx.isValid() || bi.empty()) {
371                 infoML->document()->clear();
372                 infoML->setToolTip(qt_("Displays a sketchy preview if a citation is selected above"));
373                 return;
374         }
375
376         infoML->setToolTip(qt_("Sketchy preview of the selected citation"));
377         QString const keytxt = toqstr(
378                 bi.getInfo(qstring_to_ucs4(idx.data().toString()), documentBuffer(), true));
379         infoML->document()->setHtml(keytxt);
380 }
381
382
383 void GuiCitation::findText(QString const & text, bool reset)
384 {
385         //"All Fields" and "Keys" are the first two
386         int index = fieldsCO->currentIndex() - 2;
387         BiblioInfo const & bi = bibInfo();
388         vector<docstring> const & fields = bi.getFields();
389         docstring field;
390
391         if (index <= -1 || index >= int(fields.size()))
392                 //either "All Fields" or "Keys" or an invalid value
393                 field = from_ascii("");
394         else
395                 field = fields[index];
396
397         //Was it "Keys"?
398         bool const onlyKeys = index == -1;
399
400         //"All Entry Types" is first.
401         index = entriesCO->currentIndex() - 1;
402         vector<docstring> const & entries = bi.getEntries();
403         docstring entry_type;
404         if (index < 0 || index >= int(entries.size()))
405                 entry_type = from_ascii("");
406         else
407                 entry_type = entries[index];
408
409         bool const case_sentitive = casesense_->isChecked();
410         bool const reg_exp = regexp_->isChecked();
411
412         findKey(bi, text, onlyKeys, field, entry_type,
413                        case_sentitive, reg_exp, reset);
414         //FIXME
415         //It'd be nice to save and restore the current selection in
416         //availableLV. Currently, we get an automatic reset, since the
417         //model is reset.
418
419         updateControls(bi);
420 }
421
422
423 void GuiCitation::on_fieldsCO_currentIndexChanged(int /*index*/)
424 {
425         findText(filter_->text(), true);
426 }
427
428
429 void GuiCitation::on_entriesCO_currentIndexChanged(int /*index*/)
430 {
431         findText(filter_->text(), true);
432 }
433
434
435 void GuiCitation::on_citationStyleCO_currentIndexChanged(int index)
436 {
437         if (index >= 0 && index < citationStyleCO->count()) {
438                 vector<CitationStyle> const & styles = citeStyles_;
439                 updateFormatting(styles[index]);
440                 changed();
441         }
442 }
443
444
445 void GuiCitation::filterChanged(const QString & text)
446 {
447         if (!text.isEmpty()) {
448                 if (instant_->isChecked())
449                         findText(filter_->text());
450                 return;
451         }
452         findText(filter_->text());
453         filter_->setFocus();
454 }
455
456
457 void GuiCitation::filterPressed()
458 {
459         findText(filter_->text(), true);
460 }
461
462
463 void GuiCitation::resetFilter()
464 {
465         filter_->setText(QString());
466         findText(filter_->text(), true);
467 }
468
469
470 void GuiCitation::caseChanged()
471 {
472         findText(filter_->text());
473 }
474
475
476 void GuiCitation::regexChanged()
477 {
478         findText(filter_->text());
479 }
480
481
482 void GuiCitation::updateFilterHint()
483 {
484         QString const hint = instant_->isChecked() ?
485                 qt_("Enter string to filter the list of available citations") :
486                 qt_("Enter string to filter the list of available citations and press <Enter>");
487         filter_->setToolTip(hint);
488 }
489
490
491 void GuiCitation::instantChanged(bool checked)
492 {
493         if (checked)
494                 findText(filter_->text(), true);
495
496         updateFilterHint();
497 }
498
499
500 void GuiCitation::changed()
501 {
502         setButtons();
503 }
504
505
506 void GuiCitation::applyParams(int const choice, bool full, bool force,
507         QString before, QString after)
508 {
509         if (cited_keys_.isEmpty())
510                 return;
511
512         vector<CitationStyle> const & styles = citeStyles_;
513
514         CitationStyle cs = styles[choice];
515
516         if (!cs.textBefore)
517                 before.clear();
518         if (!cs.textAfter)
519                 after.clear();
520
521         cs.forceUpperCase &= force;
522         cs.fullAuthorList &= full;
523         string const command = citationStyleToString(cs);
524
525         params_.setCmdName(command);
526         params_["key"] = qstring_to_ucs4(cited_keys_.join(","));
527         params_["before"] = qstring_to_ucs4(before);
528         params_["after"] = qstring_to_ucs4(after);
529         dispatchParams();
530 }
531
532
533 void GuiCitation::clearSelection()
534 {
535         cited_keys_.clear();
536         selected_model_.setStringList(cited_keys_);
537 }
538
539
540 void GuiCitation::init()
541 {
542         // Make the list of all available bibliography keys
543         BiblioInfo const & bi = bibInfo();
544         all_keys_ = to_qstring_list(bi.getKeys());
545         available_model_.setStringList(all_keys_);
546
547         // Ditto for the keys cited in this inset
548         QString str = toqstr(params_["key"]);
549         if (str.isEmpty())
550                 cited_keys_.clear();
551         else
552                 cited_keys_ = str.split(",");
553         selected_model_.setStringList(cited_keys_);
554
555         // Initialize the drop downs
556         fillEntries(bi);
557         fillFields(bi);
558
559         // Initialize the citation formatting
560         string const & cmd = params_.getCmdName();
561         CitationStyle const cs = citationStyleFromString(cmd);
562         forceuppercaseCB->setChecked(cs.forceUpperCase);
563         fulllistCB->setChecked(cs.fullAuthorList &&
564                 documentBuffer().params().fullAuthorList());
565         textBeforeED->setText(toqstr(params_["before"]));
566         textAfterED->setText(toqstr(params_["after"]));
567
568         // Update the interface
569         updateControls(bi);
570         updateStyles(bi);
571         if (selected_model_.rowCount()) {
572                 selectedLV->blockSignals(true);
573                 selectedLV->setFocus();
574                 QModelIndex idx = selected_model_.index(0, 0);
575                 selectedLV->selectionModel()->select(idx,
576                                 QItemSelectionModel::ClearAndSelect);
577                 selectedLV->blockSignals(false);
578
579                 // Find the citation style
580                 vector<string> const & cmds = citeCmds_;
581                 vector<string>::const_iterator cit =
582                         std::find(cmds.begin(), cmds.end(), cs.cmd);
583                 int i = 0;
584                 if (cit != cmds.end())
585                         i = int(cit - cmds.begin());
586
587                 // Set the style combo appropriately
588                 citationStyleCO->blockSignals(true);
589                 citationStyleCO->setCurrentIndex(i);
590                 citationStyleCO->blockSignals(false);
591                 updateFormatting(citeStyles_[i]);
592         } else
593                 availableLV->setFocus();
594
595         applyPB->setEnabled(false);
596         okPB->setEnabled(false);
597 }
598
599
600 void GuiCitation::findKey(BiblioInfo const & bi,
601         QString const & str, bool only_keys,
602         docstring field, docstring entry_type,
603         bool case_sensitive, bool reg_exp, bool reset)
604 {
605         // FIXME THREAD
606         // This should be moved to a class member.
607         // Used for optimisation: store last searched string.
608         static QString last_searched_string;
609         // Used to disable the above optimisation.
610         static bool last_case_sensitive;
611         static bool last_reg_exp;
612         // Reset last_searched_string in case of changed option.
613         if (last_case_sensitive != case_sensitive
614                 || last_reg_exp != reg_exp) {
615                         LYXERR(Debug::GUI, "GuiCitation::findKey: optimisation disabled!");
616                 last_searched_string.clear();
617         }
618         // save option for next search.
619         last_case_sensitive = case_sensitive;
620         last_reg_exp = reg_exp;
621
622         Qt::CaseSensitivity qtcase = case_sensitive ?
623                         Qt::CaseSensitive: Qt::CaseInsensitive;
624         QStringList keys;
625         // If new string (str) contains the last searched one...
626         if (!reset &&
627                 !last_searched_string.isEmpty() &&
628                 str.size() > 1 &&
629                 str.contains(last_searched_string, qtcase))
630                 // ... then only search within already found list.
631                 keys = available_model_.stringList();
632         else
633                 // ... else search all keys.
634                 keys = all_keys_;
635         // save searched string for next search.
636         last_searched_string = str;
637
638         QStringList result;
639
640         // First, filter by entry_type, which will be faster than
641         // what follows, so we may get to do that on less.
642         vector<docstring> keyVector = to_docstring_vector(keys);
643         filterByEntryType(bi, keyVector, entry_type);
644
645         if (str.isEmpty())
646                 result = to_qstring_list(keyVector);
647         else
648                 result = to_qstring_list(searchKeys(bi, keyVector, only_keys,
649                         qstring_to_ucs4(str), field, case_sensitive, reg_exp));
650
651         available_model_.setStringList(result);
652 }
653
654
655 QStringList GuiCitation::citationStyles(BiblioInfo const & bi, size_t max_size)
656 {
657         docstring const before = qstring_to_ucs4(textBeforeED->text());
658         docstring const after = qstring_to_ucs4(textAfterED->text());
659         vector<docstring> const keys = to_docstring_vector(cited_keys_);
660         vector<CitationStyle> styles = citeStyles_;
661         // FIXME: pass a dictionary instead of individual before, after, dialog, etc.
662         vector<docstring> ret = bi.getCiteStrings(keys, styles, documentBuffer(),
663                 before, after, from_utf8("dialog"), max_size);
664         return to_qstring_list(ret);
665 }
666
667
668 void GuiCitation::setCitedKeys()
669 {
670         cited_keys_ = selected_model_.stringList();
671         updateStyles();
672 }
673
674
675 bool GuiCitation::initialiseParams(string const & data)
676 {
677         InsetCommand::string2params(data, params_);
678         citeCmds_ = documentBuffer().params().citeCommands();
679         citeStyles_ = documentBuffer().params().citeStyles();
680         init();
681         return true;
682 }
683
684
685 void GuiCitation::clearParams()
686 {
687         params_.clear();
688 }
689
690
691 void GuiCitation::filterByEntryType(BiblioInfo const & bi,
692         vector<docstring> & keyVector, docstring entry_type)
693 {
694         if (entry_type.empty())
695                 return;
696
697         vector<docstring>::iterator it = keyVector.begin();
698         vector<docstring>::iterator end = keyVector.end();
699
700         vector<docstring> result;
701         for (; it != end; ++it) {
702                 docstring const key = *it;
703                 BiblioInfo::const_iterator cit = bi.find(key);
704                 if (cit == bi.end())
705                         continue;
706                 if (cit->second.entryType() == entry_type)
707                         result.push_back(key);
708         }
709         keyVector = result;
710 }
711
712
713 // Escape special chars.
714 // All characters are literals except: '.|*?+(){}[]^$\'
715 // These characters are literals when preceded by a "\", which is done here
716 // @todo: This function should be moved to support, and then the test in tests
717 //        should be moved there as well.
718 static docstring escape_special_chars(docstring const & expr)
719 {
720         // Search for all chars '.|*?+(){}[^$]\'
721         // Note that '[', ']', and '\' must be escaped.
722         static const lyx::regex reg("[.|*?+(){}^$\\[\\]\\\\]");
723
724         // $& is an ECMAScript format expression that expands to all
725         // of the current match
726 #ifdef LYX_USE_STD_REGEX
727         // To prefix a matched expression with a single literal backslash, we
728         // need to escape it for the C++ compiler and use:
729         // FIXME: UNICODE
730         return from_utf8(lyx::regex_replace(to_utf8(expr), reg, string("\\$&")));
731 #else
732         // A backslash in the format string starts an escape sequence in boost.
733         // Thus, to prefix a matched expression with a single literal backslash,
734         // we need to give two backslashes to the regex engine, and escape both
735         // for the C++ compiler and use:
736         // FIXME: UNICODE
737         return from_utf8(lyx::regex_replace(to_utf8(expr), reg, string("\\\\$&")));
738 #endif
739 }
740
741
742 vector<docstring> GuiCitation::searchKeys(BiblioInfo const & bi,
743         vector<docstring> const & keys_to_search, bool only_keys,
744         docstring const & search_expression, docstring field,
745         bool case_sensitive, bool regex)
746 {
747         vector<docstring> foundKeys;
748
749         docstring expr = trim(search_expression);
750         if (expr.empty())
751                 return foundKeys;
752
753         if (!regex)
754                 // We must escape special chars in the search_expr so that
755                 // it is treated as a simple string by lyx::regex.
756                 expr = escape_special_chars(expr);
757
758         lyx::regex reg_exp;
759         try {
760                 reg_exp.assign(to_utf8(expr), case_sensitive ?
761                         lyx::regex_constants::ECMAScript : lyx::regex_constants::icase);
762         } catch (lyx::regex_error const & e) {
763                 // lyx::regex throws an exception if the regular expression is not
764                 // valid.
765                 LYXERR(Debug::GUI, e.what());
766                 return vector<docstring>();
767         }
768
769         vector<docstring>::const_iterator it = keys_to_search.begin();
770         vector<docstring>::const_iterator end = keys_to_search.end();
771         for (; it != end; ++it ) {
772                 BiblioInfo::const_iterator info = bi.find(*it);
773                 if (info == bi.end())
774                         continue;
775
776                 BibTeXInfo const & kvm = info->second;
777                 string data;
778                 if (only_keys)
779                         data = to_utf8(*it);
780                 else if (field.empty())
781                         data = to_utf8(*it) + ' ' + to_utf8(kvm.allData());
782                 else
783                         data = to_utf8(kvm[field]);
784
785                 if (data.empty())
786                         continue;
787
788                 try {
789                         if (lyx::regex_search(data, reg_exp))
790                                 foundKeys.push_back(*it);
791                 }
792                 catch (lyx::regex_error const & e) {
793                         LYXERR(Debug::GUI, e.what());
794                         return vector<docstring>();
795                 }
796         }
797         return foundKeys;
798 }
799
800
801 void GuiCitation::dispatchParams()
802 {
803         std::string const lfun = InsetCommand::params2string(params_);
804         dispatch(FuncRequest(getLfun(), lfun));
805 }
806
807
808 BiblioInfo const & GuiCitation::bibInfo() const
809 {
810         Buffer const & buf = documentBuffer();
811         buf.reloadBibInfoCache();
812         return buf.masterBibInfo();
813 }
814
815
816 void GuiCitation::saveSession() const
817 {
818         Dialog::saveSession();
819         QSettings settings;
820         settings.setValue(
821                 sessionKey() + "/regex", regexp_->isChecked());
822         settings.setValue(
823                 sessionKey() + "/casesensitive", casesense_->isChecked());
824         settings.setValue(
825                 sessionKey() + "/autofind", instant_->isChecked());
826         settings.setValue(
827                 sessionKey() + "/citestyle", style_);
828 }
829
830
831 void GuiCitation::restoreSession()
832 {
833         Dialog::restoreSession();
834         QSettings settings;
835         regexp_->setChecked(settings.value(sessionKey() + "/regex").toBool());
836         casesense_->setChecked(settings.value(sessionKey() + "/casesensitive").toBool());
837         instant_->setChecked(settings.value(sessionKey() + "/autofind").toBool());
838         style_ = settings.value(sessionKey() + "/citestyle").toInt();
839         updateFilterHint();
840 }
841
842
843 Dialog * createGuiCitation(GuiView & lv) { return new GuiCitation(lv); }
844
845
846 } // namespace frontend
847 } // namespace lyx
848
849 #include "moc_GuiCitation.cpp"
850