Jean-Marc Lasgouttes wrote:
>>> Here is what I had in mind. Works quite well.
>> Who would have guessed what the innocent commit of such esoteric lfun 
>> would trigger :)
>
> It is still small activity :) Does it work for you?

Will try tomorrow.

>> But since I caught your attention, would you mind writing down dociterator
>> loop through all graphic insets, which are currently marked in the block? I
>> need similar but more useful lfun in which all graphic insets get the same
>> group ID (by eg the first one).  I see hints in DocIterator.h, but the
>> description of methods is not clear enough to just use it instead of
>> studying the whole iterator class to be sure what I am doing...
>
> Something like what inset-forall does?
>
> The idea is to use an InsetIterator (just a DocIterator that does
> forwardInset() with operator++ and returns the inset with operator*. Then one
> has to check the inset code by hand (which is not nice IMO).

But I need range from  [cur.selectionBegin();cur.selectionEnd()] so neither
of proposed ideas works in straightforward way.
This is what I come up with, if (!from.nextInset()) line is taken from
inset-forall but I have actually no idea what situation it handles.
Comments?

Pavel
diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index b2e3186b17..110abadb3c 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -1151,6 +1151,10 @@ bool BufferView::getStatus(FuncRequest const & cmd, 
FuncStatus & flag)
                flag.setEnabled(true);
                break;
 
+       case LFUN_GRAPHICS_UNIFY:
+               flag.setEnabled(cur.selection());
+               break;
+
        case LFUN_WORD_FINDADV: {
                FindAndReplaceOptions opt;
                istringstream iss(to_utf8(cmd.argument()));
@@ -1697,6 +1701,48 @@ void BufferView::dispatch(FuncRequest const & cmd, 
DispatchResult & dr)
                break;
        }
 
+       case LFUN_GRAPHICS_UNIFY: {
+
+               cur.recordUndoFullBuffer();
+
+               DocIterator from, to;
+               from = cur.selectionBegin();
+               to = cur.selectionEnd();
+
+               string newId = cmd.getArg(0);
+               bool fetchId=newId.empty(); //if we wait for groupId from first 
graphics inset
+
+               string grp = graphics::getGroupParams(buffer_, newId);
+               InsetGraphicsParams grp_par;
+               InsetGraphics::string2params(grp, buffer_, grp_par);
+
+               if (!from.nextInset())  //how this happens?
+                       from.forwardInset();
+
+               while (!from.empty() && from < to) {
+                       Inset * inset = from.nextInset();
+                       if (!inset)
+                               break;
+                       docstring insname = inset->layoutName();
+                       if (insname == "Graphics") {
+                               InsetGraphics * ig = inset->asInsetGraphics();
+                               if (!ig)
+                                       break;
+                               InsetGraphicsParams inspar = ig->getParams();
+                               if (fetchId) {
+                                       grp_par = inspar;
+                                       fetchId = false;
+
+                               } else {
+                                       grp_par.filename = inspar.filename;
+                                       ig->setParams(grp_par);
+                               }
+                       }
+                       from.forwardInset();
+               }
+               break;
+       }
+
        case LFUN_STATISTICS: {
                DocIterator from, to;
                if (cur.selection()) {
diff --git a/src/FuncCode.h b/src/FuncCode.h
index 4a5983147d..cce61f0df5 100644
--- a/src/FuncCode.h
+++ b/src/FuncCode.h
@@ -476,7 +476,8 @@ enum FuncCode
        LFUN_DEVEL_MODE_TOGGLE,         // lasgouttes 20170723
        //370
        LFUN_EXPORT_CANCEL,             // rgh, 20171227
-       LFUN_BUFFER_ANONYMIZE,             // sanda, 20180201
+       LFUN_BUFFER_ANONYMIZE,          // sanda, 20180201
+       LFUN_GRAPHICS_UNIFY,            // sanda, 20180207
        LFUN_LASTACTION                 // end of the table
 };
 
diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp
index 193c822cd2..a4bc7d1fad 100644
--- a/src/LyXAction.cpp
+++ b/src/LyXAction.cpp
@@ -3555,6 +3555,17 @@ void LyXAction::init()
  */
                { LFUN_SET_GRAPHICS_GROUP, "set-graphics-group", Noop, Edit },
 
+/*!
+ * \var lyx::FuncCode lyx::LFUN_GRAPHICS_UNIFY
+ * \li Action: Set the same group for all graphics insets in the marked block.
+ * \li Syntax: graphics-unify [<GROUP>]
+ * \li Params: <GROUP>: Id for an existing group. In case the Id is an empty 
string,
+                        the group Id from the first graphics inset will be 
used.
+ * \li Origin: sanda, 7 Feb 2018
+ * \endvar
+ */
+               { LFUN_GRAPHICS_UNIFY, "graphics-unify", Noop, Edit },
+
 
 /*!
  * \var lyx::FuncCode lyx::LFUN_SPACE_INSERT

Reply via email to