Dear LyX Developers,
For the past week or so, I've been trying to put together methods for the
expanded outline view/corkboard that will allow me to insert new headers.
While I have a general roadmap, I've run into problems at one of the milestones.
Specifically, I'd like to create an LFUN that allows me to quickly place the
cursor at the end of the last paragraph of the current section (e.g., the
paragraph before another section at the same toclevel). I initially thought
that I could use some of the outline code to locate the last paragraph in the
section, and then use the LFUN_PARAGRAPH_GOTO function to get me there.
However, this approach doesn't seem to work. Below is the code I've written so
far.
case LFUN_SECTION_GOTO_END: {
int const parId = convert<int>(cmd.getArg(0));
Buffer & buf = *cur.buffer();
DocIterator dit_section = buf.getParFromID(parId);
pit_type &pit = dit_section.pit();
ParagraphList & pars = dit_section.text()->paragraphs();
// Create references to the paragraphs of the section
ParagraphList::iterator const bgn = pars.begin();
ParagraphList::iterator start = boost::next(bgn, pit);
// The first paragraph in the section
ParagraphList::iterator finish = start;
ParagraphList::iterator const end = pars.end();
// Move out (down) from the section header to find last
// paragraph in section
if (finish != end)
++finish;
int const thistoclevel = start->layout().toclevel;
for (; finish != end; ++finish) {
int const toclevel = finish->layout().toclevel;
if (toclevel != Layout::NOT_IN_TOC && toclevel <=
thistoclevel)
break;
}
string const arg = convert<string>(finish->id())
+ ' ' + convert<string>(finish->asString().length());
lyx::dispatch(lyx::FuncRequest(lyx::LFUN_PARAGRAPH_GOTO,arg));
}
I'm having two separate problems:
1.) When executed, this code overshoots the last paragraph by one and places
the cursor at the end of the next heading rather than at the paragraph right
before it).
2.) If the section heading is in a child document, it causes LyX to crash.
I've tried various modifications to the code in order to resolve problem one,
and tried to track down the cause of the crash in problem two and I haven't had
much luck with either.
Does anyone have any suggestions on how I might implement this function? (The
next step after this is to create a function that will allow for the insertion
of a new heading with text. But for that to work, I need the cursor to be in
the correct position.) I'd appreciate any thoughts that people might have.
Cheers,
Rob