3

I've got a list of cells I want to select, but I'd like to do this without the selection cursor moving over each. Is there a way I can do this?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
b3m2a1
  • 46,870
  • 3
  • 92
  • 239

1 Answers1

2

We can do this in a similar way to this. But the trick is simply to start with a FrontEnd`NotebookSuspendScreenUpdates on the notebook in question.

Here's a way to do this:

Options[selectCells] =
  Options[NotebookFind];
selectCells[cells : {__CellObject}, ops : OptionsPattern[]] :=

  With[{c = GroupBy[cells, ParentNotebook], tag = CreateUUID[]},
   KeyValueMap[
    CheckAbort[
      FrontEndExecute@
       FrontEnd`NotebookSuspendScreenUpdates[#];
      With[{nb = #},
       Map[
        Function[
         SelectionMove[#, All, Cell, AutoScroll -> False];
         FrontEndExecute@
          FrontEnd`SelectionAddCellTags[nb, {tag}];
         ],
        #2
        ]
       ];
      NotebookFind[#, tag, All, CellTags, ops];
      FrontEndExecute@
       FrontEnd`SelectionRemoveCellTags[NotebookSelection[#], tag];
      FrontEndExecute@
       FrontEnd`NotebookResumeScreenUpdates[#];,
      FrontEndExecute@
        FrontEnd`NotebookResumeScreenUpdates[#];
      ] &,
    c
    ];
   c
   ];
selectCells[cell_CellObject, ops : OptionsPattern[]] :=

 selectCells[{cell}, ops]

This we can use this on a list of cells and they will be selected, but without each being visibly selected in turn.

Note that we wrap it in a CheckAbort with a FrontEnd`NotebookResumeScreenUpdates so that if things go pear-shaped we don't lose our screen updating.

This can be made yet more useful, in fact, by including a function to map after the cell has been selected. In doing so one gets a function mapped over cell selections, but without it being visible what's happening.

b3m2a1
  • 46,870
  • 3
  • 92
  • 239
  • I'm not sure what is the goal, selectCells@Cells[][[;; 2]] leaves the selection on those two cells. By the way selectCells@First@Cells[] throws recursion error. – Kuba Aug 23 '17 at 20:39
  • And if the selection is moved at the end, that isn't this question the same? selecting non adjacent cells – Kuba Aug 23 '17 at 20:43
  • @Kuba oops, forgot to put the list in that prevents the recursion. It's similar but semantically it's different to me, in that it's a) about selecting a list of cells, not just to the end (because you can do the latter with "SelectNextLine") and b) it's about hiding the selection process, which is very useful -- panning through each selection looks really clumsy. I wouldn't mind if it's closed as a duplicate, but I think at the very least having it here is useful for those who don't think to search for "select to the end". – b3m2a1 Aug 23 '17 at 20:53
  • I linked different topic than you did. :) – Kuba Aug 23 '17 at 21:00
  • @Kuba hah, I totally missed that. Sorry. Yeah, it's essentially the same as that, except hiding what you're doing. – b3m2a1 Aug 23 '17 at 21:02