I believe there is a bug in SelectionMove
when it is used to select a CellGroup
. The bug is two-fold: not only SelectionMove
fails to perform the operation, but also it doesn't return $Failed
is it should according to the Documentation:
SelectionMove
returns $Failed
if it cannot move the selection in the way you request.
Suppose we have a Notebook containing a CellGroup
. Then we can paste the following cell after this CellGroup
and evaluate it in order to select the previous CellGroup
:
SelectionMove[PreviousCell[], All, CellGroup]
This works in the latest version of Mathematica if PreviousCell[]
is indeed a member of a CellGroup
. But what if it is not? In this case this piece of code just does nothing AND nothing is returned as output, the latter directly contradicts the cited Documentation statement.
The described behavior is also inconsistent with other cases, for example
SelectionMove[EvaluationCell[], Previous, CellGroup]
selects the previous cell (which isn't a member of a CellGroup
). Why then the same approach fail to work with PreviousCell[]
?
The fact that SelectionMove
doesn't return $Failed
when it is unable to perform the requested operation is very unfortunate because it makes programming much more difficult: we must check whether selection is moved after EVERY call to SelectionMove
, but there is no universal way to do this. In the best case it complicates code a lot and is very inefficient. In the worst case it makes it impossible to perform some simple tasks, as a bright example consider this StackExchange question:
The problem raised in this question is so simple that it is hard to believe it can't be solved reliably: we need just to move the selection after the input cell OR after the CellGroup
containing output generated by this cell, and print new cell without scrolling using NotebookWrite
(CellPrint
always scrolls, hence it cannot be used in this case). But careful investigation showed that it is currently impossible to do this in an efficient way mainly due to the described bug.
My main question is:
Is it possible to check in an efficient and reliable way, whether SelectionMove
has failed or not?
Reported to the support as [CASE:3968507].