]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetCitation.cpp
* Inset: Prepare for an eventual merge of updateLabels() and addToToc()
[lyx.git] / src / insets / InsetCitation.cpp
index 1cd5c8e4b0620831b55c690eb9fbb69bbdbe3185..a9597ca872b8d8939d52a2e2355aaa257d9730be 100644 (file)
 
 #include "Buffer.h"
 #include "BufferParams.h"
-#include "debug.h"
+#include "support/debug.h"
 #include "DispatchResult.h"
+#include "support/gettext.h"
+#include "EmbeddedFiles.h"
 #include "FuncRequest.h"
 #include "LaTeXFeatures.h"
 
-#include "frontends/controllers/frontend_helpers.h"
-
-#include "support/fs_extras.h"
 #include "support/lstrings.h"
+#include "support/docstream.h"
 
 #include <algorithm>
 
-#include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/exception.hpp>
-
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
-using support::FileName;
-using support::getStringFromVector;
-using support::getVectorFromString;
-using support::ltrim;
-using support::prefixIs;
-using support::rtrim;
-using support::split;
-using support::tokenPos;
-
-using std::endl;
-using std::string;
-using std::vector;
-
-namespace fs = boost::filesystem;
-
-
 namespace {
 
 vector<string> const init_possible_cite_commands()
 {
-       char const * const pos[] = {
-               "cite", "citet", "citep", "citealt", "citealp",
+       char const * const possible[] = {
+               "cite", "nocite", "citet", "citep", "citealt", "citealp",
                "citeauthor", "citeyear", "citeyearpar",
                "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
                "Citet",  "Citep",  "Citealt",  "Citealp",  "Citeauthor",
@@ -64,27 +47,20 @@ vector<string> const init_possible_cite_commands()
                "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
                "citefield", "citetitle", "cite*"
        };
-       size_t const size_pos = sizeof(pos) / sizeof(pos[0]);
+       size_t const size_possible = sizeof(possible) / sizeof(possible[0]);
 
-       return vector<string>(pos, pos + size_pos);
+       return vector<string>(possible, possible + size_possible);
 }
 
 
 vector<string> const & possible_cite_commands()
 {
-       static vector<string> const pos = init_possible_cite_commands();
-       return pos;
-}
-
-
-bool is_possible_cite_command(string const & input)
-{
-       vector<string> const & possibles = possible_cite_commands();
-       vector<string>::const_iterator const end = possibles.end();
-       return std::find(possibles.begin(), end, input) != end;
+       static vector<string> const possible = init_possible_cite_commands();
+       return possible;
 }
 
 
+//FIXME See the header for the issue.
 string const default_cite_command(biblio::CiteEngine engine)
 {
        string str;
@@ -110,13 +86,13 @@ string const
                asValidLatexCommand(string const & input, biblio::CiteEngine const engine)
 {
        string const default_str = default_cite_command(engine);
-       if (!is_possible_cite_command(input))
+       if (!InsetCitation::isCompatibleCommand(input))
                return default_str;
 
        string output;
        switch (engine) {
                case biblio::ENGINE_BASIC:
-                       output = default_str;
+                       output = input;
                        break;
 
                case biblio::ENGINE_NATBIB_AUTHORYEAR:
@@ -130,63 +106,54 @@ string const
                                output = input;
                        break;
 
-                       case biblio::ENGINE_JURABIB: {
-               // Jurabib does not support the 'uppercase' natbib style.
-                               if (input[0] == 'C')
-                                       output = string(1, 'c') + input.substr(1);
-                               else
-                                       output = input;
+               case biblio::ENGINE_JURABIB: {
+                       // Jurabib does not support the 'uppercase' natbib style.
+                       if (input[0] == 'C')
+                               output = string(1, 'c') + input.substr(1);
+                       else
+                               output = input;
 
-               // Jurabib does not support the 'full' natbib style.
-                               string::size_type const n = output.size() - 1;
-                               if (output != "cite*" && output[n] == '*')
-                                       output = output.substr(0, n);
+                       // Jurabib does not support the 'full' natbib style.
+                       string::size_type const n = output.size() - 1;
+                       if (output != "cite*" && output[n] == '*')
+                               output = output.substr(0, n);
 
-                               break;
-                       }
+                       break;
+               }
        }
 
        return output;
 }
 
 
-docstring const getNatbibLabel(Buffer const & buffer,
+docstring const getComplexLabel(Buffer const & buffer,
                            string const & citeType, docstring const & keyList,
                            docstring const & before, docstring const & after,
                            biblio::CiteEngine engine)
 {
        // Only start the process off after the buffer is loaded from file.
-       if (!buffer.fully_loaded())
+       if (!buffer.isFullyLoaded())
                return docstring();
 
        // Cache the labels
-       typedef std::map<Buffer const *, BiblioInfo> CachedMap;
+       typedef map<Buffer const *, BiblioInfo> CachedMap;
        static CachedMap cached_keys;
 
        // and cache the timestamp of the bibliography files.
-       static std::map<FileName, time_t> bibfileStatus;
+       static map<FileName, time_t> bibfileStatus;
 
        BiblioInfo biblist;
 
-       vector<FileName> const & bibfilesCache = buffer.getBibfilesCache();
+       EmbeddedFileList const & bibfilesCache = buffer.getBibfilesCache();
        // compare the cached timestamps with the actual ones.
        bool changed = false;
-       for (vector<FileName>::const_iterator it = bibfilesCache.begin();
+       for (EmbeddedFileList::const_iterator it = bibfilesCache.begin();
                        it != bibfilesCache.end(); ++ it) {
                FileName const f = *it;
-               try {
-                       std::time_t lastw = fs::last_write_time(f.toFilesystemEncoding());
-                       if (lastw != bibfileStatus[f]) {
-                               changed = true;
-                               bibfileStatus[f] = lastw;
-                       }
-               }
-               catch (fs::filesystem_error & fserr) {
+               time_t lastw = f.lastModified();
+               if (lastw != bibfileStatus[f]) {
                        changed = true;
-                       lyxerr << "Couldn't find or read bibtex file "
-                              << f << endl;
-                       LYXERR(Debug::DEBUG) << "Fs error: "
-                                            << fserr.what() << endl;
+                       bibfileStatus[f] = lastw;
                }
        }
 
@@ -194,9 +161,10 @@ docstring const getNatbibLabel(Buffer const & buffer,
        if (cached_keys[&buffer].empty() || bibfileStatus.empty() || changed) {
                biblist.fillWithBibKeys(&buffer);
                cached_keys[&buffer] = biblist;
-       } else
+       } else {
                // use the cached keys
                biblist = cached_keys[&buffer];
+       }
 
        if (biblist.empty())
                return docstring();
@@ -242,20 +210,29 @@ docstring const getNatbibLabel(Buffer const & buffer,
        }
 
        docstring after_str;
-       if (!after.empty()) {
-               // The "after" key is appended only to the end of the whole.
+       // The "after" key is appended only to the end of the whole.
+       if (cite_type == "nocite")
+               after_str =  " (" + _("not cited") + ')';
+       else if (!after.empty()) {
                after_str = ", " + after;
        }
 
        // One day, these might be tunable (as they are in BibTeX).
-       char const op  = '('; // opening parenthesis.
-       char const cp  = ')'; // closing parenthesis.
-       // puctuation mark separating citation entries.
-       char const * const sep = ";";
+       char op, cp;    // opening and closing parenthesis.
+       char * sep;     // punctuation mark separating citation entries.
+       if (engine == biblio::ENGINE_BASIC) {
+               op  = '[';
+               cp  = ']';
+               sep = ",";
+       } else {
+               op  = '(';
+               cp  = ')';
+               sep = ";";
+       }
 
-       docstring const op_str(' ' + docstring(1, op));
-       docstring const cp_str(docstring(1, cp) + ' ');
-       docstring const sep_str(from_ascii(sep) + ' ');
+       docstring const op_str = ' ' + docstring(1, op);
+       docstring const cp_str = docstring(1, cp) + ' ';
+       docstring const sep_str = from_ascii(sep) + ' ';
 
        docstring label;
        vector<docstring> keys = getVectorFromString(keyList);
@@ -272,11 +249,19 @@ docstring const getNatbibLabel(Buffer const & buffer,
 
                // authors1/<before>;  ... ;
                //  authors_last, <after>
-               if (cite_type == "cite" && engine == biblio::ENGINE_JURABIB) {
-                       if (it == keys.begin())
-                               label += author + before_str + sep_str;
-                       else
-                               label += author + sep_str;
+               if (cite_type == "cite") {
+                       if (engine == biblio::ENGINE_BASIC) {
+                               label += *it + sep_str;
+                       } else if (engine == biblio::ENGINE_JURABIB) {
+                               if (it == keys.begin())
+                                       label += author + before_str + sep_str;
+                               else
+                                       label += author + sep_str;
+                       }
+
+               // nocite
+               } else if (cite_type == "nocite") {
+                       label += *it + sep_str;
 
                // (authors1 (<before> year);  ... ;
                //  authors_last (<before> year, <after>)
@@ -357,7 +342,8 @@ docstring const getNatbibLabel(Buffer const & buffer,
                label = before_str + label;
        }
 
-       if (cite_type == "citep" || cite_type == "citeyearpar")
+       if (cite_type == "citep" || cite_type == "citeyearpar" || 
+           (cite_type == "cite" && engine == biblio::ENGINE_BASIC) )
                label = op + label + cp;
 
        return label;
@@ -366,9 +352,7 @@ docstring const getNatbibLabel(Buffer const & buffer,
 
 docstring const getBasicLabel(docstring const & keyList, docstring const & after)
 {
-       using support::contains;
-
-       docstring keys(keyList);
+       docstring keys = keyList;
        docstring label;
 
        if (contains(keys, ',')) {
@@ -396,6 +380,28 @@ InsetCitation::InsetCitation(InsetCommandParams const & p)
 {}
 
 
+CommandInfo const * InsetCitation::findInfo(string const & /* cmdName */)
+{
+       // standard cite does only take one argument if jurabib is
+       // not used, but jurabib extends this to two arguments, so
+       // we have to allow both here. InsetCitation takes care that
+       // LaTeX output is nevertheless correct.
+       static const char * const paramnames[] =
+               {"after", "before", "key", ""};
+       static const bool isoptional[] = {true, true, false};
+       static const CommandInfo info = {3, paramnames, isoptional};
+       return &info;
+}
+
+
+bool InsetCitation::isCompatibleCommand(string const & cmd)
+{
+       vector<string> const & possibles = possible_cite_commands();
+       vector<string>::const_iterator const end = possibles.end();
+       return find(possibles.begin(), end, cmd) != end;
+}
+
+
 docstring const InsetCitation::generateLabel(Buffer const & buffer) const
 {
        docstring const before = getParam("before");
@@ -403,15 +409,12 @@ docstring const InsetCitation::generateLabel(Buffer const & buffer) const
 
        docstring label;
        biblio::CiteEngine const engine = buffer.params().getEngine();
-       if (engine != biblio::ENGINE_BASIC) {
-               label = getNatbibLabel(buffer, getCmdName(), getParam("key"),
-                                      before, after, engine);
-       }
+       label = getComplexLabel(buffer, getCmdName(), getParam("key"),
+                              before, after, engine);
 
        // Fallback to fail-safe
-       if (label.empty()) {
+       if (label.empty())
                label = getBasicLabel(getParam("key"), after);
-       }
 
        return label;
 }
@@ -459,9 +462,7 @@ int InsetCitation::plaintext(Buffer const & buffer, odocstream & os,
 }
 
 
-namespace {
-
-docstring const cleanupWhitespace(docstring const & citelist)
+static docstring const cleanupWhitespace(docstring const & citelist)
 {
        docstring::const_iterator it  = citelist.begin();
        docstring::const_iterator end = citelist.end();
@@ -478,23 +479,20 @@ docstring const cleanupWhitespace(docstring const & citelist)
        return result;
 }
 
-// end anon namyspace
-}
 
 int InsetCitation::docbook(Buffer const &, odocstream & os,
                           OutputParams const &) const
 {
-       os << "<citation>"
+       os << from_ascii("<citation>")
           << cleanupWhitespace(getParam("key"))
-          << "</citation>";
+          << from_ascii("</citation>");
        return 0;
 }
 
 
-int InsetCitation::textString(Buffer const & buf, odocstream & os,
-                      OutputParams const & op) const
+void InsetCitation::textString(Buffer const & buf, odocstream & os) const
 {
-       return plaintext(buf, os, op);
+       plaintext(buf, os, OutputParams(0));
 }
 
 
@@ -541,14 +539,4 @@ void InsetCitation::validate(LaTeXFeatures & features) const
 }
 
 
-void InsetCitation::replaceContents(string const & from, string const & to)
-{
-       if (tokenPos(getContents(), ',', from) != -1) {
-               vector<string> items = getVectorFromString(getContents());
-               std::replace(items.begin(), items.end(), from, to);
-               setContents(getStringFromVector(items));
-       }
-}
-
-
 } // namespace lyx