Implement new inset-forall lfun. This function iterates over all insets in

current buffer and applies a given function at cursor position before the 
inset. It is actually possible to filter on the type of inset.


Remove all index insets:
inset-forall Index delete-char-forward

Remove all (!!) insets:
inset-forall * delete-char-forward

Close all Notes (also works for a particular branch, for example)
inset-forall Note inset-toggle close

Close only yellow sticky notes
inset-forall Note:Note inset-toggle close

Of course, things may become weird:
Put LyX in an infinite loop if there is at least a Note
inset-forall Note char-backward

In this case, the code will stop after 1000 iterations.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@32834 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Jean-Marc Lasgouttes 2010-01-07 15:47:58 +00:00
parent d555c5c7bc
commit f2e6a5ecf8
3 changed files with 70 additions and 0 deletions

View File

@ -1090,6 +1090,7 @@ bool BufferView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
case LFUN_SCROLL:
case LFUN_SCREEN_UP_SELECT:
case LFUN_SCREEN_DOWN_SELECT:
case LFUN_INSET_FORALL:
flag.setEnabled(true);
break;
@ -1736,6 +1737,53 @@ bool BufferView::dispatch(FuncRequest const & cmd)
break;
}
// This would be in Buffer class if only Cursor did not
// require a bufferview
case LFUN_INSET_FORALL: {
docstring const name = from_utf8(cmd.getArg(0));
string const commandstr = cmd.getLongArg(1);
FuncRequest const fr = lyxaction.lookupFunc(commandstr);
// an arbitrary number to limit number of iterations
const int max_iter = 1000;
int iterations = 0;
Cursor & cur = d->cursor_;
cur.reset();
if (!cur.nextInset())
cur.forwardInset();
cur.beginUndoGroup();
while(cur && iterations < max_iter) {
Inset * ins = cur.nextInset();
if (!ins)
break;
docstring insname = ins->name();
while (!insname.empty()) {
if (insname == name || name == from_utf8("*")) {
cur.recordUndo();
lyx::dispatch(fr);
++iterations;
break;
}
size_t const i = insname.rfind(':');
if (i == string::npos)
break;
insname = insname.substr(0, i);
}
cur.forwardInset();
}
cur.endUndoGroup();
cur.reset();
processUpdateFlags(Update::Force);
if (iterations >= max_iter)
cur.errorMessage(bformat(_("`inset-forall' interrupted because number of actions is larger than %1$s"), max_iter));
else
cur.message(bformat(_("Applied \"%1$s\" to %2$d insets"), from_utf8(commandstr), iterations));
break;
}
case LFUN_ALL_INSETS_TOGGLE: {
string action;
string const name = split(to_utf8(cmd.argument()), action, ' ');

View File

@ -445,6 +445,7 @@ enum FuncCode
LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE, // ARRae 971202
// 345
LFUN_DEBUG_LEVEL_SET,
LFUN_INSET_FORALL, // lasgouttes, 20091127
LFUN_LASTACTION // end of the table
};

View File

@ -3446,6 +3446,27 @@ void LyXAction::init()
{ LFUN_SECTION_SELECT, "section-select", ReadOnly, Edit },
/*!
* \var lyx::FuncCode lyx::LFUN_INSET_FORALL
* \li Action: Apply the given commands on insets of a given name. WARNING: use
at your own risks; this function gives you too many ways of
shooting yourself in the foot. A typical example is
inset-forall Note note-insert
which starts an infinite loop. This is mitigated by the fact
that the number of actions is arbitrarily limited to 1000.
* \li Syntax: inset-forall <NAME> <LFUN-COMMAND>
If <NAME> is *, all insets are matched.
* \li Sample: The name is used like for InsetLayout in layout files: "Note"
matches all note insets, while "Note:Note" only matches LyX
yellow note insets. The following command closes all note insets
inset-forall Note inset-toggle close
* \li Origin: lasgouttes, 27 Nov 2009
* \endvar
*/
{ LFUN_INSET_FORALL, "inset-forall", ReadOnly, Edit },
{ LFUN_NOACTION, "", Noop, Hidden }
#ifndef DOXYGEN_SHOULD_SKIP_THIS
};