#include "context.h"
#include "FloatList.h"
#include "lengthcommon.h"
-#include "support/FileInfo.h"
#include "support/lstrings.h"
-#include "support/tostr.h"
+#include "support/convert.h"
#include "support/filetools.h"
+#include <boost/filesystem/operations.hpp>
+#include <boost/tuple/tuple.hpp>
+
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
-using lyx::support::FileInfo;
using lyx::support::MakeAbsPath;
using lyx::support::rtrim;
using lyx::support::suffixIs;
using std::string;
using std::vector;
+namespace fs = boost::filesystem;
+
/// thin wrapper around parse_text using a string
string parse_text(Parser & p, unsigned flags, const bool outer,
char const * const known_latex_commands[] = { "ref", "cite", "label", "index",
"printindex", "pageref", "url", "vref", "vpageref", "prettyref", "eqref", 0 };
+/*!
+ * natbib commands.
+ * We can't put these into known_latex_commands because the argument order
+ * is reversed in lyx if there are 2 arguments.
+ * The starred forms are also known.
+ */
+char const * const known_natbib_commands[] = { "cite", "citet", "citep",
+"citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
+"citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
+
+/*!
+ * jurabib commands.
+ * We can't put these into known_latex_commands because the argument order
+ * is reversed in lyx if there are 2 arguments.
+ * No starred form other than "cite*" known.
+ */
+char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
+"citealt", "citealp", "citeauthor", "citeyear", "citeyearpar", "fullcite",
+// jurabib commands not (yet) supported by LyX:
+// "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
+// "footciteauthor", "footciteyear", "footciteyearpar",
+"citefield", "citetitle", "cite*", 0 };
+
/// LaTeX names for quotes
char const * const known_quotes[] = { "glqq", "grqq", "quotedblbase",
"textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
* The latter can be a real unit like "pt", or a latex length variable
* like "\textwidth". The unit may contain additional stuff like glue
* lengths, but we don't care, because such lengths are ERT anyway.
- * \return true if \param value and \param unit are valid.
+ * \returns true if \p value and \p unit are valid.
*/
bool splitLatexLength(string const & len, string & value, string & unit)
{
/*!
- * Translates a LaTeX length into \param value, \param unit and
- * \param special parts suitable for a box inset.
+ * Translates a LaTeX length into \p value, \p unit and
+ * \p special parts suitable for a box inset.
* The difference from translate_len() is that a box inset knows about
- * some special "units" that are stored in \param special.
+ * some special "units" that are stored in \p special.
*/
void translate_box_len(string const & length, string & value, string & unit, string & special)
{
* in \p extensions.
*/
string find_file(string const & name, string const & path,
- char const * const * extensions)
+ char const * const * extensions)
{
for (char const * const * what = extensions; *what; ++what) {
// We don't use ChangeExtension() because it does the wrong
// thing if name contains a dot.
string const trial = name + '.' + (*what);
- if (FileInfo(MakeAbsPath(trial, path)).exist())
+ if (fs::exists(MakeAbsPath(trial, path)))
return trial;
}
return string();
void end_inset(ostream & os)
{
- os << "\n\\end_inset \n\n";
+ os << "\n\\end_inset\n\n";
}
newcontext.check_layout(os);
for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
if (*it == '\\')
- os << "\n\\backslash \n";
- else if (*it == '\n')
- os << "\n\\newline \n";
- else
+ os << "\n\\backslash\n";
+ else if (*it == '\n') {
+ newcontext.new_paragraph(os);
+ newcontext.check_layout(os);
+ } else
os << *it;
}
newcontext.check_end_layout(os);
newcontext.check_layout(os);
for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
if (*it == '\\')
- os << "\n\\backslash \n";
+ os << "\n\\backslash\n";
else
os << *it;
}
// make sure that our comment is the last thing on the line
- os << "\n\\newline";
+ newcontext.new_paragraph(os);
+ newcontext.check_layout(os);
newcontext.check_end_layout(os);
end_inset(os);
}
eat_whitespace(p, os, context, false);
}
}
- parse_text_snippet(p, os, FLAG_ITEM, outer, context);
+ parse_text(p, os, FLAG_ITEM, outer, context);
context.check_end_layout(os);
if (parent_context.deeper_paragraph) {
// We must suppress the "end deeper" because we
* \return true if the command was parsed, false otherwise.
*/
bool parse_command(string const & command, Parser & p, ostream & os,
- bool outer, Context & context)
+ bool outer, Context & context)
{
if (known_commands.find(command) != known_commands.end()) {
vector<ArgumentType> const & template_arguments = known_commands[command];
/// Parses a minipage or parbox
void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
- Context & parent_context, bool use_parbox)
+ Context & parent_context, bool use_parbox)
{
string position;
string inner_pos;
end_inset(os);
}
- else if (name == "tabular") {
+ else if (name == "tabular" || name == "longtable") {
parent_context.check_layout(os);
begin_inset(os, "Tabular ");
- handle_tabular(p, os, parent_context);
+ handle_tabular(p, os, name == "longtable", parent_context);
end_inset(os);
}
if (p.next_token().asInput() == "[") {
os << "placement " << p.getArg('[', ']') << '\n';
}
- os << "wide " << tostr(is_starred)
+ os << "wide " << convert<string>(is_starred)
<< "\nsideways false"
<< "\nstatus open\n\n";
parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
parent_context.new_paragraph(os);
}
if (name == "flushleft" || name == "raggedright")
- parent_context.add_extra_stuff("\\align left ");
+ parent_context.add_extra_stuff("\\align left\n");
else if (name == "flushright" || name == "raggedleft")
- parent_context.add_extra_stuff("\\align right ");
+ parent_context.add_extra_stuff("\\align right\n");
else
- parent_context.add_extra_stuff("\\align center ");
+ parent_context.add_extra_stuff("\\align center\n");
parse_text(p, os, FLAG_END, outer, parent_context);
// Just in case the environment is empty ..
parent_context.extra_stuff.erase();
* Spaces are skipped, but comments are written to \p os.
*/
void eat_whitespace(Parser & p, ostream & os, Context & context,
- bool eatParagraph)
+ bool eatParagraph)
{
while (p.good()) {
Token const & t = p.get_token();
* \param newvalue New value of the attribute
*/
void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
- Context & context, string const & attribute,
- string & currentvalue, string const & newvalue)
+ Context & context, string const & attribute,
+ string & currentvalue, string const & newvalue)
{
context.check_layout(os);
string oldvalue = currentvalue;
currentvalue = newvalue;
- os << '\n' << attribute << ' ' << newvalue << " \n";
+ os << '\n' << attribute << ' ' << newvalue << "\n";
parse_text_snippet(p, os, flags, outer, context);
currentvalue = oldvalue;
- os << '\n' << attribute << ' ' << oldvalue << " \n";
+ os << '\n' << attribute << ' ' << oldvalue << "\n";
+}
+
+
+/// get the arguments of a natbib or jurabib citation command
+std::pair<string, string> getCiteArguments(Parser & p, bool natbibOrder)
+{
+ // We need to distinguish "" and "[]", so we can't use p.getOpt().
+
+ // text before the citation
+ string before;
+ // text after the citation
+ string after = p.getFullOpt();
+
+ if (!after.empty()) {
+ before = p.getFullOpt();
+ if (natbibOrder && !before.empty())
+ std::swap(before, after);
+ }
+ return std::make_pair(before, after);
}
} // anonymous namespace
LyXLayout_ptr newlayout;
// Store the latest bibliographystyle (needed for bibtex inset)
string bibliographystyle;
+ bool const use_natbib = used_packages.find("natbib") != used_packages.end();
+ bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
while (p.good()) {
Token const & t = p.get_token();
; // ignore it in {}`` or -{}-
else if (s == "[" || s == "]" || s == "*")
os << s;
- else if (is_known(next.cs(), known_sizes))
+ else if (is_known(next.cs(), known_sizes)) {
// s will change the size, so we must reset
// it here
- os << s << "\n\\size " << context.font.size
- << " \n";
- else if (is_known(next.cs(), known_font_families))
+ os << s;
+ if (!context.atParagraphStart())
+ os << "\n\\size "
+ << context.font.size << "\n";
+ } else if (is_known(next.cs(), known_font_families)) {
// s will change the font family, so we must
// reset it here
- os << s << "\n\\family "
- << context.font.family << " \n";
- else if (is_known(next.cs(), known_font_series))
+ os << s;
+ if (!context.atParagraphStart())
+ os << "\n\\family "
+ << context.font.family << "\n";
+ } else if (is_known(next.cs(), known_font_series)) {
// s will change the font series, so we must
// reset it here
- os << s << "\n\\series "
- << context.font.series << " \n";
- else if (is_known(next.cs(), known_font_shapes))
+ os << s;
+ if (!context.atParagraphStart())
+ os << "\n\\series "
+ << context.font.series << "\n";
+ } else if (is_known(next.cs(), known_font_shapes)) {
// s will change the font shape, so we must
// reset it here
- os << s << "\n\\shape "
- << context.font.shape << " \n";
- else if (is_known(next.cs(), known_old_font_families) ||
- is_known(next.cs(), known_old_font_series) ||
- is_known(next.cs(), known_old_font_shapes))
+ os << s;
+ if (!context.atParagraphStart())
+ os << "\n\\shape "
+ << context.font.shape << "\n";
+ } else if (is_known(next.cs(), known_old_font_families) ||
+ is_known(next.cs(), known_old_font_series) ||
+ is_known(next.cs(), known_old_font_shapes)) {
// s will change the font family, series
// and shape, so we must reset it here
- os << s
- << "\n\\family " << context.font.family
- << " \n\\series " << context.font.series
- << " \n\\shape " << context.font.shape
- << " \n";
- else {
+ os << s;
+ if (!context.atParagraphStart())
+ os << "\n\\family "
+ << context.font.family
+ << "\n\\series "
+ << context.font.series
+ << "\n\\shape "
+ << context.font.shape << "\n";
+ } else {
handle_ert(os, "{", context, false);
// s will end the current layout and begin a
// new one if necessary
else if (t.cs() == "noindent") {
p.skip_spaces();
- context.add_extra_stuff("\\noindent ");
+ context.add_extra_stuff("\\noindent\n");
}
else if (t.cs() == "appendix") {
p.skip_spaces();
- context.add_extra_stuff("\\start_of_appendix ");
+ context.add_extra_stuff("\\start_of_appendix\n");
}
// Must attempt to parse "Section*" before "Section".
}
else if (t.cs() == "includegraphics") {
+ bool const clip = p.next_token().asInput() == "*";
+ if (clip)
+ p.get_token();
map<string, string> opts = split_map(p.getArg('[', ']'));
+ if (clip)
+ opts["clip"] = string();
string name = subst(p.verbatim_item(), "\\lyxdot ", ".");
string const path = getMasterFilePath();
// We want to preserve relative / absolute filenames,
// therefore path is only used for testing
- if (!FileInfo(MakeAbsPath(name, path)).exist()) {
+ if (!fs::exists(MakeAbsPath(name, path))) {
// The file extension is probably missing.
// Now try to find it out.
string const dvips_name =
} else if (!pdftex_name.empty())
name = pdftex_name;
- if (!FileInfo(MakeAbsPath(name, path)).exist())
+ if (!fs::exists(MakeAbsPath(name, path)))
cerr << "Warning: Could not find graphics file '"
<< name << "'." << endl;
}
end_inset(os);
}
- else if (t.cs() == "footnote") {
+ else if (t.cs() == "footnote" ||
+ (t.cs() == "thanks" && context.layout->intitle)) {
p.skip_spaces();
context.check_layout(os);
begin_inset(os, "Foot\n");
else if (t.cs() == "hfill") {
context.check_layout(os);
- os << "\n\\hfill \n";
+ os << "\n\\hfill\n";
skip_braces(p);
p.skip_spaces();
}
else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
+ // FIXME: Somehow prevent title layouts if
+ // "maketitle" was not found
p.skip_spaces();
skip_braces(p); // swallow this
}
Font oldFont = context.font;
context.font.init();
context.font.size = oldFont.size;
- os << "\n\\family " << context.font.family << " \n";
- os << "\n\\series " << context.font.series << " \n";
- os << "\n\\shape " << context.font.shape << " \n";
+ os << "\n\\family " << context.font.family << "\n";
+ os << "\n\\series " << context.font.series << "\n";
+ os << "\n\\shape " << context.font.shape << "\n";
if (t.cs() == "textnormal") {
parse_text_snippet(p, os, FLAG_ITEM, outer, context);
context.font = oldFont;
- os << "\n\\shape " << oldFont.shape << " \n";
- os << "\n\\series " << oldFont.series << " \n";
- os << "\n\\family " << oldFont.family << " \n";
+ os << "\n\\shape " << oldFont.shape << "\n";
+ os << "\n\\series " << oldFont.series << "\n";
+ os << "\n\\family " << oldFont.family << "\n";
} else
eat_whitespace(p, os, context, false);
}
else if (t.cs() == "underbar") {
+ // Do NOT handle \underline.
+ // \underbar cuts through y, g, q, p etc.,
+ // \underline does not.
context.check_layout(os);
- os << "\n\\bar under \n";
+ os << "\n\\bar under\n";
parse_text_snippet(p, os, FLAG_ITEM, outer, context);
- os << "\n\\bar default \n";
+ os << "\n\\bar default\n";
}
else if (t.cs() == "emph" || t.cs() == "noun") {
context.check_layout(os);
- os << "\n\\" << t.cs() << " on \n";
+ os << "\n\\" << t.cs() << " on\n";
parse_text_snippet(p, os, FLAG_ITEM, outer, context);
- os << "\n\\" << t.cs() << " default \n";
+ os << "\n\\" << t.cs() << " default\n";
+ }
+
+ else if (use_natbib &&
+ is_known(t.cs(), known_natbib_commands) &&
+ ((t.cs() != "citefullauthor" &&
+ t.cs() != "citeyear" &&
+ t.cs() != "citeyearpar") ||
+ p.next_token().asInput() != "*")) {
+ context.check_layout(os);
+ // tex lyx
+ // \citet[before][after]{a} \citet[after][before]{a}
+ // \citet[before][]{a} \citet[][before]{a}
+ // \citet[after]{a} \citet[after]{a}
+ // \citet{a} \citet{a}
+ string command = '\\' + t.cs();
+ if (p.next_token().asInput() == "*") {
+ command += '*';
+ p.get_token();
+ }
+ if (command == "\\citefullauthor")
+ // alternative name for "\\citeauthor*"
+ command = "\\citeauthor*";
+
+ // text before the citation
+ string before;
+ // text after the citation
+ string after;
+
+ boost::tie(before, after) = getCiteArguments(p, true);
+ if (command == "\\cite") {
+ // \cite without optional argument means
+ // \citet, \cite with at least one optional
+ // argument means \citep.
+ if (before.empty() && after.empty())
+ command = "\\citet";
+ else
+ command = "\\citep";
+ }
+ if (before.empty() && after == "[]")
+ // avoid \citet[]{a}
+ after.erase();
+ else if (before == "[]" && after == "[]") {
+ // avoid \citet[][]{a}
+ before.erase();
+ after.erase();
+ }
+ begin_inset(os, "LatexCommand ");
+ os << command << after << before
+ << '{' << p.verbatim_item() << "}\n";
+ end_inset(os);
+ }
+
+ else if (use_jurabib &&
+ is_known(t.cs(), known_jurabib_commands)) {
+ context.check_layout(os);
+ string const command = '\\' + t.cs();
+ char argumentOrder = '\0';
+ vector<string> const & options = used_packages["jurabib"];
+ if (std::find(options.begin(), options.end(),
+ "natbiborder") != options.end())
+ argumentOrder = 'n';
+ else if (std::find(options.begin(), options.end(),
+ "jurabiborder") != options.end())
+ argumentOrder = 'j';
+
+ // text before the citation
+ string before;
+ // text after the citation
+ string after;
+
+ boost::tie(before, after) =
+ getCiteArguments(p, argumentOrder != 'j');
+ string const citation = p.verbatim_item();
+ if (!before.empty() && argumentOrder == '\0') {
+ cerr << "Warning: Assuming argument order "
+ "of jurabib version 0.6 for\n'"
+ << command << before << after << '{'
+ << citation << "}'.\n"
+ "Add 'jurabiborder' to the jurabib "
+ "package options if you used an\n"
+ "earlier jurabib version." << endl;
+ }
+ begin_inset(os, "LatexCommand ");
+ os << command << after << before
+ << '{' << citation << "}\n";
+ end_inset(os);
}
else if (is_known(t.cs(), known_latex_commands)) {
+ // This needs to be after the check for natbib and
+ // jurabib commands, because "cite" has different
+ // arguments with natbib and jurabib.
context.check_layout(os);
begin_inset(os, "LatexCommand ");
os << '\\' << t.cs();
- os << p.getOpt();
- os << p.getOpt();
- os << '{' << p.verbatim_item() << "}\n";
+ // lyx cannot handle newlines in a latex command
+ // FIXME: Move the substitution into parser::getOpt()?
+ os << subst(p.getOpt(), "\n", " ");
+ os << subst(p.getOpt(), "\n", " ");
+ os << '{' << subst(p.verbatim_item(), "\n", " ") << "}\n";
end_inset(os);
}
context.font.size = oldsize;
context.font.family =
known_coded_font_families[where - known_old_font_families];
- os << "\n\\family " << context.font.family << " \n"
- << "\\series " << context.font.series << " \n"
- << "\\shape " << context.font.shape << " \n";
+ os << "\n\\family " << context.font.family << "\n"
+ << "\\series " << context.font.series << "\n"
+ << "\\shape " << context.font.shape << "\n";
eat_whitespace(p, os, context, false);
}
context.font.size = oldsize;
context.font.series =
known_coded_font_series[where - known_old_font_series];
- os << "\n\\family " << context.font.family << " \n"
- << "\\series " << context.font.series << " \n"
- << "\\shape " << context.font.shape << " \n";
+ os << "\n\\family " << context.font.family << "\n"
+ << "\\series " << context.font.series << "\n"
+ << "\\shape " << context.font.shape << "\n";
eat_whitespace(p, os, context, false);
}
context.font.size = oldsize;
context.font.shape =
known_coded_font_shapes[where - known_old_font_shapes];
- os << "\n\\family " << context.font.family << " \n"
- << "\\series " << context.font.series << " \n"
- << "\\shape " << context.font.shape << " \n";
+ os << "\n\\family " << context.font.family << "\n"
+ << "\\series " << context.font.series << "\n"
+ << "\\shape " << context.font.shape << "\n";
eat_whitespace(p, os, context, false);
}
else if (t.cs() == "textbackslash") {
context.check_layout(os);
- os << "\n\\backslash \n";
+ os << "\n\\backslash\n";
skip_braces(p);
}
handle_ert(os, "\\\\*" + p.getOpt(), context);
}
else {
- os << "\n\\newline \n";
+ os << "\n\\newline\n";
}
}
skip_braces(p);
}
+ else if (t.cs() == "newcommand" ||
+ t.cs() == "providecommand" ||
+ t.cs() == "renewcommand") {
+ // these could be handled by parse_command(), but
+ // we need to call add_known_command() here.
+ string name = t.asInput();
+ if (p.next_token().asInput() == "*") {
+ // Starred form. Eat '*'
+ p.get_token();
+ name += '*';
+ }
+ string const command = p.verbatim_item();
+ string const opt1 = p.getOpt();
+ string const opt2 = p.getFullOpt();
+ add_known_command(command, opt1, !opt2.empty());
+ string const ert = name + '{' + command + '}' +
+ opt1 + opt2 +
+ '{' + p.verbatim_item() + '}';
+ handle_ert(os, ert, context);
+ }
+
else if (t.cs() == "vspace") {
bool starred = false;
if (p.next_token().asInput() == "*") {