2003-08-23 00:17:00 +00:00
|
|
|
/**
|
|
|
|
* \file toc.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-07-21 15:51:07 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
* \author Angus Leeming
|
2006-04-19 14:48:22 +00:00
|
|
|
* \author Abdelrazak Younes
|
2002-07-21 15:51:07 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-07-21 15:51:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "toc.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
#include "buffer.h"
|
2003-09-09 11:24:33 +00:00
|
|
|
#include "bufferparams.h"
|
2005-07-04 13:05:03 +00:00
|
|
|
#include "FloatList.h"
|
2003-09-04 03:54:04 +00:00
|
|
|
#include "funcrequest.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
#include "LyXAction.h"
|
2003-09-06 17:23:08 +00:00
|
|
|
#include "paragraph.h"
|
2006-04-19 14:48:22 +00:00
|
|
|
#include "cursor.h"
|
|
|
|
#include "debug.h"
|
2002-07-21 15:51:07 +00:00
|
|
|
|
2003-09-06 18:38:02 +00:00
|
|
|
#include "frontends/LyXView.h"
|
|
|
|
|
2003-05-13 14:36:24 +00:00
|
|
|
#include "insets/insetfloat.h"
|
2005-05-12 10:16:04 +00:00
|
|
|
#include "insets/insetoptarg.h"
|
2003-05-13 14:36:24 +00:00
|
|
|
#include "insets/insetwrap.h"
|
|
|
|
|
2005-01-06 16:39:35 +00:00
|
|
|
#include "support/convert.h"
|
2003-05-13 14:36:24 +00:00
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
using std::vector;
|
2002-07-21 16:59:01 +00:00
|
|
|
using std::max;
|
|
|
|
using std::ostream;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2002-07-21 15:51:07 +00:00
|
|
|
|
2003-07-27 13:18:55 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace toc {
|
2002-07-21 15:51:07 +00:00
|
|
|
|
|
|
|
string const TocItem::asString() const
|
|
|
|
{
|
|
|
|
return string(4 * depth, ' ') + str;
|
|
|
|
}
|
|
|
|
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
void TocItem::goTo(LyXView & lv_) const
|
|
|
|
{
|
2005-01-06 15:40:49 +00:00
|
|
|
string const tmp = convert<string>(id_);
|
2002-08-13 17:43:40 +00:00
|
|
|
lv_.dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
|
2002-07-21 15:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-21 18:57:15 +00:00
|
|
|
FuncRequest TocItem::action() const
|
2002-07-21 15:51:07 +00:00
|
|
|
{
|
2005-01-06 15:40:49 +00:00
|
|
|
return FuncRequest(LFUN_GOTO_PARAGRAPH, convert<string>(id_));
|
2002-07-21 15:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const getType(string const & cmdName)
|
|
|
|
{
|
|
|
|
// special case
|
|
|
|
if (cmdName == "tableofcontents")
|
2005-07-27 15:22:08 +00:00
|
|
|
return "TOC";
|
2002-07-21 15:51:07 +00:00
|
|
|
else
|
|
|
|
return cmdName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-27 15:22:08 +00:00
|
|
|
string const getGuiName(string const & type, Buffer const & buffer)
|
2005-07-04 13:05:03 +00:00
|
|
|
{
|
|
|
|
FloatList const & floats =
|
|
|
|
buffer.params().getLyXTextClass().floats();
|
2005-07-27 15:22:08 +00:00
|
|
|
if (floats.typeExist(type))
|
|
|
|
return floats.getType(type).name();
|
2005-07-04 13:05:03 +00:00
|
|
|
else
|
2005-07-27 15:22:08 +00:00
|
|
|
return type;
|
2005-07-04 13:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-28 07:41:31 +00:00
|
|
|
TocList const getTocList(Buffer const & buf)
|
2002-07-21 15:51:07 +00:00
|
|
|
{
|
|
|
|
TocList toclist;
|
|
|
|
|
2003-09-09 09:47:59 +00:00
|
|
|
BufferParams const & bufparams = buf.params();
|
2005-05-12 10:16:04 +00:00
|
|
|
const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
|
2002-07-21 15:51:07 +00:00
|
|
|
|
2003-08-28 07:41:31 +00:00
|
|
|
ParConstIterator pit = buf.par_iterator_begin();
|
|
|
|
ParConstIterator end = buf.par_iterator_end();
|
2002-11-07 00:37:09 +00:00
|
|
|
for (; pit != end; ++pit) {
|
2003-09-12 17:13:22 +00:00
|
|
|
|
2005-05-12 10:16:04 +00:00
|
|
|
// the string that goes to the toc (could be the optarg)
|
|
|
|
string tocstring;
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
// For each paragraph, traverse its insets and look for
|
2002-09-24 18:36:20 +00:00
|
|
|
// FLOAT_CODE or WRAP_CODE
|
2003-06-12 11:09:55 +00:00
|
|
|
InsetList::const_iterator it = pit->insetlist.begin();
|
|
|
|
InsetList::const_iterator end = pit->insetlist.end();
|
2002-07-21 21:21:06 +00:00
|
|
|
for (; it != end; ++it) {
|
2005-05-12 10:16:04 +00:00
|
|
|
switch (it->inset->lyxCode()) {
|
|
|
|
case InsetBase::FLOAT_CODE:
|
2004-03-25 09:16:36 +00:00
|
|
|
static_cast<InsetFloat*>(it->inset)
|
|
|
|
->addToToc(toclist, buf);
|
2005-05-12 10:16:04 +00:00
|
|
|
break;
|
|
|
|
case InsetBase::WRAP_CODE:
|
2004-03-25 09:16:36 +00:00
|
|
|
static_cast<InsetWrap*>(it->inset)
|
|
|
|
->addToToc(toclist, buf);
|
2005-05-12 10:16:04 +00:00
|
|
|
break;
|
|
|
|
case InsetBase::OPTARG_CODE: {
|
|
|
|
if (!tocstring.empty())
|
|
|
|
break;
|
|
|
|
Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
|
|
|
|
if (!pit->getLabelstring().empty())
|
2005-06-09 09:58:08 +00:00
|
|
|
tocstring = pit->getLabelstring()
|
2005-05-12 10:16:04 +00:00
|
|
|
+ ' ';
|
|
|
|
tocstring += par.asString(buf, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2002-07-21 15:51:07 +00:00
|
|
|
}
|
2002-07-28 22:50:13 +00:00
|
|
|
}
|
2005-05-12 10:16:04 +00:00
|
|
|
|
|
|
|
/// now the toc entry for the paragraph
|
|
|
|
int const toclevel = pit->layout()->toclevel;
|
2005-10-25 13:43:50 +00:00
|
|
|
if (toclevel != LyXLayout::NOT_IN_TOC
|
|
|
|
&& toclevel >= min_toclevel
|
2005-05-12 10:16:04 +00:00
|
|
|
&& toclevel <= bufparams.tocdepth) {
|
|
|
|
// insert this into the table of contents
|
|
|
|
if (tocstring.empty())
|
|
|
|
tocstring = pit->asString(buf, true);
|
2005-06-09 09:58:08 +00:00
|
|
|
TocItem const item(pit->id(), toclevel - min_toclevel,
|
2005-05-12 10:16:04 +00:00
|
|
|
tocstring);
|
2005-07-27 15:22:08 +00:00
|
|
|
toclist["TOC"].push_back(item);
|
2005-05-12 10:16:04 +00:00
|
|
|
}
|
2002-07-21 15:51:07 +00:00
|
|
|
}
|
|
|
|
return toclist;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-19 14:48:22 +00:00
|
|
|
TocItem const getCurrentTocItem(Buffer const & buf, LCursor const & cur,
|
|
|
|
std::string const & type)
|
|
|
|
{
|
|
|
|
// This should be cached:
|
|
|
|
TocList tmp = getTocList(buf);
|
|
|
|
|
|
|
|
// Is the type supported?
|
|
|
|
/// \todo TocItem() should create an invalid TocItem()
|
|
|
|
/// \todo create TocItem::isValid()
|
|
|
|
TocList::iterator toclist_it = tmp.find(type);
|
|
|
|
if (toclist_it == tmp.end())
|
|
|
|
return TocItem(-1, -1, string());
|
|
|
|
|
|
|
|
Toc const toc_vector = toclist_it->second;
|
|
|
|
ParConstIterator const current(cur);
|
|
|
|
int start = toc_vector.size() - 1;
|
|
|
|
|
|
|
|
/// \todo cache the ParConstIterator values inside TocItem
|
|
|
|
for (int i = start; i >= 0; --i) {
|
|
|
|
|
|
|
|
ParConstIterator const it
|
|
|
|
= buf.getParFromID(toc_vector[i].id_);
|
|
|
|
|
|
|
|
// A good solution for TocItems inside insets would be to do:
|
|
|
|
//
|
|
|
|
//if (std::distance(it, current) <= 0)
|
|
|
|
// return toc_vector[i];
|
|
|
|
//
|
|
|
|
// But for an unknown reason, std::distance(current, it) always
|
|
|
|
// returns a positive value and std::distance(it, current) takes forever...
|
|
|
|
// So for now, we do:
|
|
|
|
if (it.pit() <= current.pit())
|
|
|
|
return toc_vector[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are before the first TocItem:
|
|
|
|
return toc_vector[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-28 07:41:31 +00:00
|
|
|
vector<string> const getTypes(Buffer const & buffer)
|
2002-07-21 15:51:07 +00:00
|
|
|
{
|
|
|
|
vector<string> types;
|
|
|
|
|
|
|
|
TocList const tmp = getTocList(buffer);
|
|
|
|
|
|
|
|
TocList::const_iterator cit = tmp.begin();
|
|
|
|
TocList::const_iterator end = tmp.end();
|
|
|
|
|
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
types.push_back(cit->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-28 07:41:31 +00:00
|
|
|
void asciiTocList(string const & type, Buffer const & buffer, ostream & os)
|
2002-07-21 15:51:07 +00:00
|
|
|
{
|
|
|
|
TocList const toc_list = getTocList(buffer);
|
|
|
|
TocList::const_iterator cit = toc_list.find(type);
|
|
|
|
if (cit != toc_list.end()) {
|
|
|
|
Toc::const_iterator ccit = cit->second.begin();
|
|
|
|
Toc::const_iterator end = cit->second.end();
|
|
|
|
for (; ccit != end; ++ccit)
|
|
|
|
os << ccit->asString() << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-09 00:26:19 +00:00
|
|
|
void outline(OutlineOp mode, Buffer * buf, pit_type & pit)
|
2006-03-29 05:08:42 +00:00
|
|
|
{
|
|
|
|
ParagraphList & pars = buf->text().paragraphs();
|
|
|
|
ParagraphList::iterator bgn = pars.begin();
|
|
|
|
ParagraphList::iterator s = boost::next(bgn, pit);
|
|
|
|
ParagraphList::iterator p = s;
|
|
|
|
ParagraphList::iterator end = pars.end();
|
|
|
|
|
|
|
|
LyXTextClass::const_iterator lit =
|
|
|
|
buf->params().getLyXTextClass().begin();
|
|
|
|
LyXTextClass::const_iterator const lend =
|
|
|
|
buf->params().getLyXTextClass().end();
|
|
|
|
|
|
|
|
int const thistoclevel = s->layout()->toclevel;
|
|
|
|
int toclevel;
|
|
|
|
switch (mode) {
|
|
|
|
case UP: {
|
|
|
|
if (p != end)
|
|
|
|
++p;
|
|
|
|
for (; p != end; ++p) {
|
|
|
|
toclevel = p->layout()->toclevel;
|
2006-04-05 23:56:29 +00:00
|
|
|
if (toclevel != LyXLayout::NOT_IN_TOC
|
2006-03-29 05:08:42 +00:00
|
|
|
&& toclevel <= thistoclevel) {
|
2006-04-05 23:56:29 +00:00
|
|
|
break;
|
2006-03-29 05:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ParagraphList::iterator q = s;
|
|
|
|
if (q != bgn)
|
|
|
|
--q;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
for (; q != bgn; --q) {
|
|
|
|
toclevel = q->layout()->toclevel;
|
2006-04-05 23:56:29 +00:00
|
|
|
if (toclevel != LyXLayout::NOT_IN_TOC
|
2006-03-29 05:08:42 +00:00
|
|
|
&& toclevel <= thistoclevel) {
|
2006-04-05 23:56:29 +00:00
|
|
|
break;
|
2006-03-29 05:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pit_type const newpit = std::distance(pars.begin(), q);
|
|
|
|
pit_type const len = std::distance(s, p);
|
|
|
|
pit += len;
|
|
|
|
pars.insert(q, s, p);
|
|
|
|
s = boost::next(pars.begin(), pit);
|
|
|
|
ParagraphList::iterator t = boost::next(s, len);
|
|
|
|
pit = newpit;
|
|
|
|
pars.erase(s, t);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DOWN: {
|
2006-04-05 23:56:29 +00:00
|
|
|
if (p != end)
|
2006-03-29 05:08:42 +00:00
|
|
|
++p;
|
|
|
|
for (; p != end; ++p) {
|
|
|
|
toclevel = p->layout()->toclevel;
|
2006-04-05 23:56:29 +00:00
|
|
|
if (toclevel != LyXLayout::NOT_IN_TOC
|
2006-03-29 05:08:42 +00:00
|
|
|
&& toclevel <= thistoclevel) {
|
2006-04-05 23:56:29 +00:00
|
|
|
break;
|
2006-03-29 05:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ParagraphList::iterator q = p;
|
|
|
|
if (q != end)
|
|
|
|
++q;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
for (; q != end; ++q) {
|
|
|
|
toclevel = q->layout()->toclevel;
|
2006-04-05 23:56:29 +00:00
|
|
|
if (toclevel != LyXLayout::NOT_IN_TOC
|
2006-03-29 05:08:42 +00:00
|
|
|
&& toclevel <= thistoclevel) {
|
2006-04-05 23:56:29 +00:00
|
|
|
break;
|
2006-03-29 05:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pit_type const newpit = std::distance(pars.begin(), q);
|
|
|
|
pit_type const len = std::distance(s, p);
|
|
|
|
pars.insert(q, s, p);
|
|
|
|
s = boost::next(pars.begin(), pit);
|
|
|
|
ParagraphList::iterator t = boost::next(s, len);
|
|
|
|
pit = newpit - len;
|
|
|
|
pars.erase(s, t);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IN:
|
|
|
|
for (; lit != lend; ++lit) {
|
|
|
|
if ((*lit)->toclevel == thistoclevel + 1) {
|
|
|
|
s->layout((*lit));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OUT:
|
|
|
|
for (; lit != lend; ++lit) {
|
|
|
|
if ((*lit)->toclevel == thistoclevel - 1) {
|
|
|
|
s->layout((*lit));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
} // namespace toc
|
2003-07-27 13:18:55 +00:00
|
|
|
} // namespace lyx
|