2003-06-20 12:46:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file buffer_funcs.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
2003-06-20 12:46:28 +00:00
|
|
|
|
* \author Alfredo Braunstein
|
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-06-20 12:46:28 +00:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "buffer_funcs.h"
|
|
|
|
|
#include "buffer.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "bufferlist.h"
|
2003-09-09 11:24:33 +00:00
|
|
|
|
#include "bufferparams.h"
|
2004-12-27 16:30:27 +00:00
|
|
|
|
#include "dociterator.h"
|
2005-02-25 11:55:36 +00:00
|
|
|
|
#include "counters.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "errorlist.h"
|
2005-02-25 11:55:36 +00:00
|
|
|
|
#include "Floating.h"
|
|
|
|
|
#include "FloatList.h"
|
2003-06-20 12:46:28 +00:00
|
|
|
|
#include "gettext.h"
|
2005-09-08 16:57:13 +00:00
|
|
|
|
#include "language.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "LaTeX.h"
|
2005-02-25 11:55:36 +00:00
|
|
|
|
#include "lyxtextclass.h"
|
2003-09-06 17:23:08 +00:00
|
|
|
|
#include "paragraph.h"
|
2005-04-19 09:04:25 +00:00
|
|
|
|
#include "paragraph_funcs.h"
|
2006-03-23 20:11:06 +00:00
|
|
|
|
#include "ParagraphList.h"
|
2005-02-25 11:55:36 +00:00
|
|
|
|
#include "ParagraphParameters.h"
|
|
|
|
|
#include "pariterator.h"
|
2003-09-09 11:24:33 +00:00
|
|
|
|
#include "lyxvc.h"
|
|
|
|
|
#include "texrow.h"
|
2003-06-20 12:46:28 +00:00
|
|
|
|
#include "vc-backend.h"
|
2006-04-22 18:48:28 +00:00
|
|
|
|
#include "toc.h"
|
2003-06-20 12:46:28 +00:00
|
|
|
|
|
|
|
|
|
#include "frontends/Alert.h"
|
|
|
|
|
|
2005-02-25 11:55:36 +00:00
|
|
|
|
#include "insets/insetbibitem.h"
|
|
|
|
|
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "support/filetools.h"
|
2005-01-31 10:42:26 +00:00
|
|
|
|
#include "support/fs_extras.h"
|
2003-06-20 12:46:28 +00:00
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
|
|
2006-04-16 14:19:25 +00:00
|
|
|
|
#include <iostream>
|
|
|
|
|
|
2004-11-06 15:23:12 +00:00
|
|
|
|
#include <boost/bind.hpp>
|
2005-01-31 10:42:26 +00:00
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
2004-11-06 15:23:12 +00:00
|
|
|
|
|
2006-04-16 14:19:25 +00:00
|
|
|
|
using namespace std;
|
|
|
|
|
|
2005-02-25 11:55:36 +00:00
|
|
|
|
using lyx::pit_type;
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::support::bformat;
|
2006-04-08 22:31:11 +00:00
|
|
|
|
using lyx::support::libFileSearch;
|
|
|
|
|
using lyx::support::makeDisplayPath;
|
|
|
|
|
using lyx::support::onlyFilename;
|
|
|
|
|
using lyx::support::onlyPath;
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::support::unlink;
|
2003-06-20 12:46:28 +00:00
|
|
|
|
|
2005-02-25 11:55:36 +00:00
|
|
|
|
using std::min;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
namespace fs = boost::filesystem;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
|
extern BufferList bufferlist;
|
2003-06-20 12:46:28 +00:00
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2005-01-05 20:21:27 +00:00
|
|
|
|
bool readFile(Buffer * const b, string const & s)
|
2003-06-20 12:46:28 +00:00
|
|
|
|
{
|
2005-01-05 20:21:27 +00:00
|
|
|
|
BOOST_ASSERT(b);
|
|
|
|
|
|
2003-06-20 12:46:28 +00:00
|
|
|
|
// File information about normal file
|
2005-01-31 10:42:26 +00:00
|
|
|
|
if (!fs::exists(s)) {
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const file = makeDisplayPath(s, 50);
|
2003-06-20 12:46:28 +00:00
|
|
|
|
string text = bformat(_("The specified document\n%1$s"
|
|
|
|
|
"\ncould not be read."), file);
|
|
|
|
|
Alert::error(_("Could not read document"), text);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if emergency save file exists and is newer.
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const e = onlyPath(s) + onlyFilename(s) + ".emergency";
|
2004-03-26 17:49:08 +00:00
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
if (fs::exists(e) && fs::exists(s)
|
|
|
|
|
&& fs::last_write_time(e) > fs::last_write_time(s))
|
2004-03-26 17:49:08 +00:00
|
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const file = makeDisplayPath(s, 20);
|
2005-01-05 20:21:27 +00:00
|
|
|
|
string const text =
|
|
|
|
|
bformat(_("An emergency save of the document "
|
|
|
|
|
"%1$s exists.\n\n"
|
|
|
|
|
"Recover emergency save?"), file);
|
2004-03-26 17:49:08 +00:00
|
|
|
|
switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
|
|
|
|
|
_("&Recover"), _("&Load Original"),
|
|
|
|
|
_("&Cancel")))
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
// the file is not saved if we load the emergency file.
|
|
|
|
|
b->markDirty();
|
|
|
|
|
return b->readFile(e);
|
|
|
|
|
case 1:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
2003-06-20 12:46:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-26 17:49:08 +00:00
|
|
|
|
// Now check if autosave file is newer.
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const a = onlyPath(s) + '#' + onlyFilename(s) + '#';
|
2004-03-26 17:49:08 +00:00
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
if (fs::exists(a) && fs::exists(s)
|
|
|
|
|
&& fs::last_write_time(a) > fs::last_write_time(s))
|
2004-03-26 17:49:08 +00:00
|
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const file = makeDisplayPath(s, 20);
|
2005-01-05 20:21:27 +00:00
|
|
|
|
string const text =
|
|
|
|
|
bformat(_("The backup of the document "
|
|
|
|
|
"%1$s is newer.\n\nLoad the "
|
|
|
|
|
"backup instead?"), file);
|
2004-03-26 17:49:08 +00:00
|
|
|
|
switch (Alert::prompt(_("Load backup?"), text, 0, 2,
|
|
|
|
|
_("&Load backup"), _("Load &original"),
|
|
|
|
|
_("&Cancel") ))
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
// the file is not saved if we load the autosave file.
|
|
|
|
|
b->markDirty();
|
|
|
|
|
return b->readFile(a);
|
|
|
|
|
case 1:
|
|
|
|
|
// Here we delete the autosave
|
|
|
|
|
unlink(a);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
2003-06-20 12:46:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-03-26 17:49:08 +00:00
|
|
|
|
return b->readFile(s);
|
2003-06-20 12:46:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool loadLyXFile(Buffer * b, string const & s)
|
|
|
|
|
{
|
2005-01-05 20:21:27 +00:00
|
|
|
|
BOOST_ASSERT(b);
|
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
if (fs::is_readable(s)) {
|
2003-06-20 12:46:28 +00:00
|
|
|
|
if (readFile(b, s)) {
|
2003-09-09 09:47:59 +00:00
|
|
|
|
b->lyxvc().file_found_hook(s);
|
2005-01-31 10:42:26 +00:00
|
|
|
|
if (!fs::is_writable(s))
|
|
|
|
|
b->setReadonly(true);
|
2003-06-20 12:46:28 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2005-01-31 10:42:26 +00:00
|
|
|
|
} else {
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const file = makeDisplayPath(s, 20);
|
2003-06-20 12:46:28 +00:00
|
|
|
|
// Here we probably should run
|
|
|
|
|
if (LyXVC::file_not_found_hook(s)) {
|
2005-01-05 20:21:27 +00:00
|
|
|
|
string const text =
|
|
|
|
|
bformat(_("Do you want to retrieve the document"
|
|
|
|
|
" %1$s from version control?"), file);
|
2003-06-20 12:46:28 +00:00
|
|
|
|
int const ret = Alert::prompt(_("Retrieve from version control?"),
|
|
|
|
|
text, 0, 1, _("&Retrieve"), _("&Cancel"));
|
|
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
// How can we know _how_ to do the checkout?
|
|
|
|
|
// With the current VC support it has to be,
|
|
|
|
|
// a RCS file since CVS do not have special ,v files.
|
|
|
|
|
RCS::retrieve(s);
|
|
|
|
|
return loadLyXFile(b, s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
Buffer * newFile(string const & filename, string const & templatename,
|
2005-01-05 20:21:27 +00:00
|
|
|
|
bool const isNamed)
|
2003-06-20 12:46:28 +00:00
|
|
|
|
{
|
|
|
|
|
// get a free buffer
|
|
|
|
|
Buffer * b = bufferlist.newBuffer(filename);
|
2005-01-05 20:21:27 +00:00
|
|
|
|
BOOST_ASSERT(b);
|
2003-06-20 12:46:28 +00:00
|
|
|
|
|
|
|
|
|
string tname;
|
|
|
|
|
// use defaults.lyx as a default template if it exists.
|
|
|
|
|
if (templatename.empty())
|
2006-04-08 22:31:11 +00:00
|
|
|
|
tname = libFileSearch("templates", "defaults.lyx");
|
2003-06-20 12:46:28 +00:00
|
|
|
|
else
|
|
|
|
|
tname = templatename;
|
|
|
|
|
|
|
|
|
|
if (!tname.empty()) {
|
2003-07-28 14:40:29 +00:00
|
|
|
|
if (!b->readFile(tname)) {
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const file = makeDisplayPath(tname, 50);
|
2003-07-29 16:40:26 +00:00
|
|
|
|
string const text = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
|
2003-06-20 12:46:28 +00:00
|
|
|
|
Alert::error(_("Could not read template"), text);
|
2006-07-17 15:13:49 +00:00
|
|
|
|
bufferlist.release(b);
|
|
|
|
|
return 0;
|
2003-06-20 12:46:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isNamed) {
|
|
|
|
|
b->setUnnamed();
|
|
|
|
|
b->setFileName(filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b->setReadonly(false);
|
2003-10-22 14:40:24 +00:00
|
|
|
|
b->fully_loaded(true);
|
2003-09-09 09:47:59 +00:00
|
|
|
|
b->updateDocLang(b->params().language);
|
2003-06-20 12:46:28 +00:00
|
|
|
|
|
|
|
|
|
return b;
|
|
|
|
|
}
|
2003-06-24 20:42:15 +00:00
|
|
|
|
|
|
|
|
|
|
2006-08-13 16:16:43 +00:00
|
|
|
|
void bufferErrors(Buffer const & buf, TeXErrors const & terr,
|
|
|
|
|
ErrorList & errorList)
|
2003-06-24 20:42:15 +00:00
|
|
|
|
{
|
|
|
|
|
TeXErrors::Errors::const_iterator cit = terr.begin();
|
|
|
|
|
TeXErrors::Errors::const_iterator end = terr.end();
|
|
|
|
|
|
|
|
|
|
for (; cit != end; ++cit) {
|
2005-06-03 08:48:04 +00:00
|
|
|
|
int id_start = -1;
|
|
|
|
|
int pos_start = -1;
|
|
|
|
|
int errorrow = cit->error_in_line;
|
2005-11-29 13:39:03 +00:00
|
|
|
|
bool found = buf.texrow().getIdFromRow(errorrow, id_start,
|
2006-04-05 23:56:29 +00:00
|
|
|
|
pos_start);
|
2005-06-03 08:48:04 +00:00
|
|
|
|
int id_end = -1;
|
|
|
|
|
int pos_end = -1;
|
|
|
|
|
do {
|
|
|
|
|
++errorrow;
|
2005-11-29 13:39:03 +00:00
|
|
|
|
found = buf.texrow().getIdFromRow(errorrow, id_end,
|
2006-04-05 23:56:29 +00:00
|
|
|
|
pos_end);
|
2005-11-29 13:39:03 +00:00
|
|
|
|
} while (found && id_start == id_end && pos_start == pos_end);
|
2005-06-03 08:48:04 +00:00
|
|
|
|
|
2006-08-13 16:16:43 +00:00
|
|
|
|
errorList.push_back(ErrorItem(cit->error_desc,
|
2006-07-15 22:43:37 +00:00
|
|
|
|
cit->error_text, id_start, pos_start, pos_end));
|
2003-06-24 20:42:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-04-09 00:26:19 +00:00
|
|
|
|
string const bufferFormat(Buffer const & buffer)
|
2003-06-24 20:42:15 +00:00
|
|
|
|
{
|
|
|
|
|
if (buffer.isLinuxDoc())
|
|
|
|
|
return "linuxdoc";
|
|
|
|
|
else if (buffer.isDocBook())
|
|
|
|
|
return "docbook";
|
|
|
|
|
else if (buffer.isLiterate())
|
|
|
|
|
return "literate";
|
|
|
|
|
else
|
|
|
|
|
return "latex";
|
|
|
|
|
}
|
2004-12-27 16:30:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int countWords(DocIterator const & from, DocIterator const & to)
|
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
bool inword = false;
|
|
|
|
|
for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
|
|
|
|
|
// Copied and adapted from isLetter() in ControlSpellChecker
|
|
|
|
|
if (dit.inTexted()
|
|
|
|
|
&& dit.pos() != dit.lastpos()
|
|
|
|
|
&& dit.paragraph().isLetter(dit.pos())
|
|
|
|
|
&& !isDeletedText(dit.paragraph(), dit.pos())) {
|
|
|
|
|
if (!inword) {
|
|
|
|
|
++count;
|
|
|
|
|
inword = true;
|
|
|
|
|
}
|
|
|
|
|
} else if (inword)
|
|
|
|
|
inword = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
2005-02-25 11:55:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2005-08-03 20:21:11 +00:00
|
|
|
|
lyx::depth_type getDepth(DocIterator const & it)
|
2005-02-25 11:55:36 +00:00
|
|
|
|
{
|
2005-08-03 20:21:11 +00:00
|
|
|
|
lyx::depth_type depth = 0;
|
2006-04-05 23:56:29 +00:00
|
|
|
|
for (size_t i = 0 ; i < it.depth() ; ++i)
|
2005-08-03 20:21:11 +00:00
|
|
|
|
if (!it[i].inset().inMathed())
|
|
|
|
|
depth += it[i].paragraph().getDepth() + 1;
|
|
|
|
|
// remove 1 since the outer inset does not count
|
|
|
|
|
return depth - 1;
|
|
|
|
|
}
|
2005-02-25 11:55:36 +00:00
|
|
|
|
|
2005-08-03 20:21:11 +00:00
|
|
|
|
lyx::depth_type getItemDepth(ParIterator const & it)
|
2006-04-05 23:56:29 +00:00
|
|
|
|
{
|
2005-08-03 20:21:11 +00:00
|
|
|
|
Paragraph const & par = *it;
|
|
|
|
|
LYX_LABEL_TYPES const labeltype = par.layout()->labeltype;
|
2005-02-25 11:55:36 +00:00
|
|
|
|
|
2005-08-03 20:21:11 +00:00
|
|
|
|
if (labeltype != LABEL_ENUMERATE && labeltype != LABEL_ITEMIZE)
|
2006-04-05 23:56:29 +00:00
|
|
|
|
return 0;
|
2005-02-25 11:55:36 +00:00
|
|
|
|
|
2005-08-03 20:21:11 +00:00
|
|
|
|
// this will hold the lowest depth encountered up to now.
|
|
|
|
|
lyx::depth_type min_depth = getDepth(it);
|
|
|
|
|
ParIterator prev_it = it;
|
2005-02-25 11:55:36 +00:00
|
|
|
|
while (true) {
|
2005-08-03 20:21:11 +00:00
|
|
|
|
if (prev_it.pit())
|
|
|
|
|
--prev_it.top().pit();
|
|
|
|
|
else {
|
|
|
|
|
// start of nested inset: go to outer par
|
|
|
|
|
prev_it.pop_back();
|
|
|
|
|
if (prev_it.empty()) {
|
|
|
|
|
// start of document: nothing to do
|
|
|
|
|
return 0;
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|
2005-08-03 20:21:11 +00:00
|
|
|
|
}
|
2006-04-05 23:56:29 +00:00
|
|
|
|
|
2005-08-03 20:21:11 +00:00
|
|
|
|
// We search for the first paragraph with same label
|
|
|
|
|
// that is not more deeply nested.
|
|
|
|
|
Paragraph & prev_par = *prev_it;
|
|
|
|
|
lyx::depth_type const prev_depth = getDepth(prev_it);
|
2006-04-05 23:56:29 +00:00
|
|
|
|
if (labeltype == prev_par.layout()->labeltype) {
|
2005-08-03 20:21:11 +00:00
|
|
|
|
if (prev_depth < min_depth) {
|
|
|
|
|
return prev_par.itemdepth + 1;
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|
2005-08-03 20:21:11 +00:00
|
|
|
|
else if (prev_depth == min_depth) {
|
|
|
|
|
return prev_par.itemdepth;
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-08-03 20:21:11 +00:00
|
|
|
|
min_depth = std::min(min_depth, prev_depth);
|
|
|
|
|
// small optimization: if we are at depth 0, we won't
|
|
|
|
|
// find anything else
|
|
|
|
|
if (prev_depth == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-08-03 20:21:11 +00:00
|
|
|
|
bool needEnumCounterReset(ParIterator const & it)
|
2005-02-25 11:55:36 +00:00
|
|
|
|
{
|
2005-08-03 20:21:11 +00:00
|
|
|
|
Paragraph const & par = *it;
|
|
|
|
|
BOOST_ASSERT(par.layout()->labeltype == LABEL_ENUMERATE);
|
|
|
|
|
lyx::depth_type const cur_depth = par.getDepth();
|
|
|
|
|
ParIterator prev_it = it;
|
|
|
|
|
while (prev_it.pit()) {
|
|
|
|
|
--prev_it.top().pit();
|
|
|
|
|
Paragraph const & prev_par = *prev_it;
|
|
|
|
|
if (prev_par.getDepth() <= cur_depth)
|
|
|
|
|
return prev_par.layout()->labeltype != LABEL_ENUMERATE;
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|
2005-08-03 20:21:11 +00:00
|
|
|
|
// start of nested inset: reset
|
|
|
|
|
return true;
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-04-16 14:19:25 +00:00
|
|
|
|
// set the label of a paragraph. This includes the counters.
|
|
|
|
|
void setLabel(Buffer const & buf, ParIterator & it)
|
2005-02-25 11:55:36 +00:00
|
|
|
|
{
|
|
|
|
|
Paragraph & par = *it;
|
|
|
|
|
BufferParams const & bufparams = buf.params();
|
|
|
|
|
LyXTextClass const & textclass = bufparams.getLyXTextClass();
|
|
|
|
|
LyXLayout_ptr const & layout = par.layout();
|
|
|
|
|
Counters & counters = textclass.counters();
|
|
|
|
|
|
|
|
|
|
if (it.pit() == 0) {
|
|
|
|
|
par.params().appendix(par.params().startOfAppendix());
|
|
|
|
|
} else {
|
|
|
|
|
par.params().appendix(it.plist()[it.pit() - 1].params().appendix());
|
|
|
|
|
if (!par.params().appendix() &&
|
|
|
|
|
par.params().startOfAppendix()) {
|
|
|
|
|
par.params().appendix(true);
|
|
|
|
|
textclass.counters().reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-03 20:21:11 +00:00
|
|
|
|
// Compute the item depth of the paragraph
|
|
|
|
|
par.itemdepth = getItemDepth(it);
|
|
|
|
|
|
2005-02-25 11:55:36 +00:00
|
|
|
|
// erase what was there before
|
|
|
|
|
par.params().labelString(string());
|
|
|
|
|
|
|
|
|
|
if (layout->margintype == MARGIN_MANUAL) {
|
|
|
|
|
if (par.params().labelWidthString().empty())
|
|
|
|
|
par.setLabelWidthString(layout->labelstring());
|
|
|
|
|
} else {
|
|
|
|
|
par.setLabelWidthString(string());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// is it a layout that has an automatic label?
|
|
|
|
|
if (layout->labeltype == LABEL_COUNTER) {
|
2005-04-19 09:04:25 +00:00
|
|
|
|
if (layout->toclevel <= buf.params().secnumdepth
|
2005-05-12 10:16:04 +00:00
|
|
|
|
&& (layout->latextype != LATEX_ENVIRONMENT
|
2005-04-19 09:04:25 +00:00
|
|
|
|
|| isFirstInSequence(it.pit(), it.plist()))) {
|
|
|
|
|
counters.step(layout->counter);
|
2005-05-20 09:13:42 +00:00
|
|
|
|
string label = expandLabel(buf, layout,
|
2005-04-19 09:04:25 +00:00
|
|
|
|
par.params().appendix());
|
|
|
|
|
par.params().labelString(label);
|
|
|
|
|
}
|
2005-02-25 11:55:36 +00:00
|
|
|
|
} else if (layout->labeltype == LABEL_ITEMIZE) {
|
|
|
|
|
// At some point of time we should do something more
|
|
|
|
|
// clever here, like:
|
|
|
|
|
// par.params().labelString(
|
|
|
|
|
// bufparams.user_defined_bullet(par.itemdepth).getText());
|
|
|
|
|
// for now, use a simple hardcoded label
|
|
|
|
|
string itemlabel;
|
|
|
|
|
switch (par.itemdepth) {
|
|
|
|
|
case 0:
|
|
|
|
|
itemlabel = "*";
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
itemlabel = "-";
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
itemlabel = "@";
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
itemlabel = "<EFBFBD>";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
par.params().labelString(itemlabel);
|
|
|
|
|
} else if (layout->labeltype == LABEL_ENUMERATE) {
|
|
|
|
|
// FIXME
|
|
|
|
|
// Yes I know this is a really, really! bad solution
|
|
|
|
|
// (Lgb)
|
|
|
|
|
string enumcounter = "enum";
|
|
|
|
|
|
|
|
|
|
switch (par.itemdepth) {
|
|
|
|
|
case 2:
|
|
|
|
|
enumcounter += 'i';
|
|
|
|
|
case 1:
|
|
|
|
|
enumcounter += 'i';
|
|
|
|
|
case 0:
|
|
|
|
|
enumcounter += 'i';
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
enumcounter += "iv";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// not a valid enumdepth...
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-03 20:21:11 +00:00
|
|
|
|
// Maybe we have to reset the enumeration counter.
|
|
|
|
|
if (needEnumCounterReset(it))
|
|
|
|
|
counters.reset(enumcounter);
|
|
|
|
|
|
2005-02-25 11:55:36 +00:00
|
|
|
|
counters.step(enumcounter);
|
|
|
|
|
|
2005-09-09 14:52:55 +00:00
|
|
|
|
string format;
|
|
|
|
|
|
|
|
|
|
switch (par.itemdepth) {
|
|
|
|
|
case 0:
|
|
|
|
|
format = N_("\\arabic{enumi}.");
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
format = N_("(\\alph{enumii})");
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
format = N_("\\roman{enumiii}.");
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
format = N_("\\Alph{enumiv}.");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// not a valid enumdepth...
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
par.params().labelString(counters.counterLabel(buf.B_(format)));
|
2005-02-25 11:55:36 +00:00
|
|
|
|
} else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
|
|
|
|
|
counters.step("bibitem");
|
|
|
|
|
int number = counters.value("bibitem");
|
2005-06-09 09:58:08 +00:00
|
|
|
|
if (par.bibitem())
|
2005-02-25 11:55:36 +00:00
|
|
|
|
par.bibitem()->setCounter(number);
|
2005-05-20 09:13:42 +00:00
|
|
|
|
par.params().labelString(buf.B_(layout->labelstring()));
|
2005-02-25 11:55:36 +00:00
|
|
|
|
// In biblio should't be following counters but...
|
|
|
|
|
} else if (layout->labeltype == LABEL_SENSITIVE) {
|
|
|
|
|
// Search for the first float or wrap inset in the iterator
|
|
|
|
|
string type;
|
|
|
|
|
size_t i = it.depth();
|
|
|
|
|
while (i > 0) {
|
|
|
|
|
--i;
|
|
|
|
|
InsetBase * const in = &it[i].inset();
|
|
|
|
|
if (in->lyxCode() == InsetBase::FLOAT_CODE
|
2006-01-23 10:56:47 +00:00
|
|
|
|
|| in->lyxCode() == InsetBase::WRAP_CODE) {
|
2005-02-25 11:55:36 +00:00
|
|
|
|
type = in->getInsetName();
|
|
|
|
|
break;
|
2006-01-23 10:56:47 +00:00
|
|
|
|
}
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string s;
|
|
|
|
|
if (!type.empty()) {
|
|
|
|
|
Floating const & fl = textclass.floats().getType(type);
|
|
|
|
|
|
|
|
|
|
counters.step(fl.type());
|
|
|
|
|
|
|
|
|
|
// Doesn't work... yet.
|
|
|
|
|
s = bformat(_("%1$s #:"), buf.B_(fl.name()));
|
|
|
|
|
} else {
|
|
|
|
|
// par->SetLayout(0);
|
|
|
|
|
s = buf.B_(layout->labelstring());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
par.params().labelString(s);
|
2006-04-11 08:24:15 +00:00
|
|
|
|
} else if (layout->labeltype == LABEL_NO_LABEL)
|
|
|
|
|
par.params().labelString(string());
|
|
|
|
|
else
|
2005-02-25 11:55:36 +00:00
|
|
|
|
par.params().labelString(buf.B_(layout->labelstring()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // anon namespace
|
|
|
|
|
|
|
|
|
|
|
2006-04-16 14:19:25 +00:00
|
|
|
|
bool updateCurrentLabel(Buffer const & buf,
|
|
|
|
|
ParIterator & it)
|
2006-04-07 22:16:09 +00:00
|
|
|
|
{
|
2006-04-16 14:19:25 +00:00
|
|
|
|
if (it == par_iterator_end(buf.inset()))
|
2006-04-22 18:48:28 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// if (it.lastpit == 0 && LyXText::isMainText())
|
|
|
|
|
// return false;
|
|
|
|
|
|
2006-04-07 22:16:09 +00:00
|
|
|
|
switch (it->layout()->labeltype) {
|
2006-04-16 14:19:25 +00:00
|
|
|
|
|
2006-04-07 22:16:09 +00:00
|
|
|
|
case LABEL_NO_LABEL:
|
|
|
|
|
case LABEL_MANUAL:
|
|
|
|
|
case LABEL_BIBLIO:
|
|
|
|
|
case LABEL_TOP_ENVIRONMENT:
|
|
|
|
|
case LABEL_CENTERED_TOP_ENVIRONMENT:
|
|
|
|
|
case LABEL_STATIC:
|
|
|
|
|
case LABEL_ITEMIZE:
|
2006-04-16 14:19:25 +00:00
|
|
|
|
setLabel(buf, it);
|
|
|
|
|
return true;
|
2006-04-07 22:16:09 +00:00
|
|
|
|
|
|
|
|
|
case LABEL_SENSITIVE:
|
|
|
|
|
case LABEL_COUNTER:
|
|
|
|
|
// do more things with enumerate later
|
|
|
|
|
case LABEL_ENUMERATE:
|
2006-04-16 14:19:25 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is dead code which get rid of a warning:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void updateLabels(Buffer const & buf,
|
|
|
|
|
ParIterator & from, ParIterator & to)
|
|
|
|
|
{
|
|
|
|
|
for (ParIterator it = from; it != to; ++it) {
|
|
|
|
|
if (it.pit() > it.lastpit())
|
|
|
|
|
return;
|
|
|
|
|
if (!updateCurrentLabel (buf, it)) {
|
|
|
|
|
updateLabels(buf);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2006-04-07 22:16:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-04-16 14:19:25 +00:00
|
|
|
|
void updateLabels(Buffer const & buf,
|
|
|
|
|
ParIterator & iter)
|
|
|
|
|
{
|
|
|
|
|
if (updateCurrentLabel(buf, iter))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
updateLabels(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void updateLabels(Buffer const & buf)
|
2005-02-25 11:55:36 +00:00
|
|
|
|
{
|
2006-04-16 14:19:25 +00:00
|
|
|
|
// start over the counters
|
2005-02-25 11:55:36 +00:00
|
|
|
|
buf.params().getLyXTextClass().counters().reset();
|
2006-04-16 14:19:25 +00:00
|
|
|
|
|
|
|
|
|
ParIterator const end = par_iterator_end(buf.inset());
|
2005-02-25 11:55:36 +00:00
|
|
|
|
|
2006-04-16 14:19:25 +00:00
|
|
|
|
for (ParIterator it = par_iterator_begin(buf.inset()); it != end; ++it) {
|
2005-02-25 11:55:36 +00:00
|
|
|
|
// reduce depth if necessary
|
|
|
|
|
if (it.pit()) {
|
|
|
|
|
Paragraph const & prevpar = it.plist()[it.pit() - 1];
|
|
|
|
|
it->params().depth(min(it->params().depth(),
|
|
|
|
|
prevpar.getMaxDepthAfter()));
|
|
|
|
|
} else
|
|
|
|
|
it->params().depth(0);
|
|
|
|
|
|
|
|
|
|
// set the counter for this paragraph
|
2006-04-16 14:19:25 +00:00
|
|
|
|
setLabel(buf, it);
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|
2006-04-22 18:48:28 +00:00
|
|
|
|
|
|
|
|
|
lyx::toc::updateToc(buf);
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-05-20 09:13:42 +00:00
|
|
|
|
string expandLabel(Buffer const & buf,
|
2005-02-25 11:55:36 +00:00
|
|
|
|
LyXLayout_ptr const & layout, bool appendix)
|
|
|
|
|
{
|
2005-05-20 09:13:42 +00:00
|
|
|
|
LyXTextClass const & tclass = buf.params().getLyXTextClass();
|
|
|
|
|
|
2005-06-09 09:58:08 +00:00
|
|
|
|
string fmt = buf.B_(appendix ? layout->labelstring_appendix()
|
2005-05-20 09:13:42 +00:00
|
|
|
|
: layout->labelstring());
|
2005-02-25 11:55:36 +00:00
|
|
|
|
|
|
|
|
|
// handle 'inherited level parts' in 'fmt',
|
|
|
|
|
// i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
|
|
|
|
|
size_t const i = fmt.find('@', 0);
|
|
|
|
|
if (i != string::npos) {
|
|
|
|
|
size_t const j = fmt.find('@', i + 1);
|
|
|
|
|
if (j != string::npos) {
|
|
|
|
|
string parent(fmt, i + 1, j - i - 1);
|
2005-05-20 09:13:42 +00:00
|
|
|
|
string label = expandLabel(buf, tclass[parent], appendix);
|
2005-02-25 11:55:36 +00:00
|
|
|
|
fmt = string(fmt, 0, i) + label + string(fmt, j + 1, string::npos);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-20 09:13:42 +00:00
|
|
|
|
return tclass.counters().counterLabel(fmt);
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|