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