patch 9.0.0861: solution for "!!sort" in closed fold is not optimal

Problem:    Solution for "!!sort" in closed fold is not optimal.
Solution:   Use a different range instead of the subtle difference in handling
            a range with an offset. (issue #11487)
diff --git a/src/ops.c b/src/ops.c
index 4515453..ed8a5d8 100644
--- a/src/ops.c
+++ b/src/ops.c
@@ -3333,14 +3333,32 @@
 	    stuffcharReadbuff('.');
 	else
 	    stuffnumReadbuff((long)oap->start.lnum);
-	if (oap->end.lnum != oap->start.lnum)
+
+#ifdef FEAT_FOLDING
+	// When using !! on a closed fold the range ".!" works best to operate
+	// on, it will be made the whole closed fold later.
+	linenr_T endOfStartFold = oap->start.lnum;
+	(void)hasFolding(oap->start.lnum, NULL, &endOfStartFold);
+#endif
+	if (oap->end.lnum != oap->start.lnum
+#ifdef FEAT_FOLDING
+		&& oap->end.lnum != endOfStartFold
+#endif
+	   )
 	{
+	    // Make it a range with the end line.
 	    stuffcharReadbuff(',');
 	    if (oap->end.lnum == curwin->w_cursor.lnum)
 		stuffcharReadbuff('.');
 	    else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
 		stuffcharReadbuff('$');
-	    else if (oap->start.lnum == curwin->w_cursor.lnum)
+	    else if (oap->start.lnum == curwin->w_cursor.lnum
+#ifdef FEAT_FOLDING
+		    // do not use ".+number" for a closed fold, it would count
+		    // folded lines twice
+		    && !hasFolding(oap->end.lnum, NULL, NULL)
+#endif
+		    )
 	    {
 		stuffReadbuff((char_u *)".+");
 		stuffnumReadbuff((long)oap->line_count - 1);