1Index: src/normal.c
2===================================================================
3--- src/normal.c	(revision 57807)
4+++ src/normal.c	(working copy)
5@@ -7393,6 +7393,8 @@
6 	}
7 	else
8 	{
9+	    if (Unix2003_compat && cap->cmdchar == 'S')
10+		beginline(0);
11 	    if (cap->count0)
12 		stuffnumReadbuff(cap->count0);
13 	    stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
14@@ -8318,6 +8320,8 @@
15 
16     if (!checkclearopq(cap->oap))
17     {
18+	int undo_offset = 0;
19+	int save;
20 #ifdef FEAT_FOLDING
21 	if (cap->cmdchar == 'O')
22 	    /* Open above the first line of a folded sequence of lines */
23@@ -8328,11 +8332,21 @@
24 	    (void)hasFolding(curwin->w_cursor.lnum,
25 						NULL, &curwin->w_cursor.lnum);
26 #endif
27-	if (u_save((linenr_T)(curwin->w_cursor.lnum -
28+	if (Unix2003_compat) {
29+	    beginline(0);
30+	    /* Undo of 'O' leaves cursor above the deleted line */
31+	    if (cap->cmdchar == 'O' && curwin->w_cursor.lnum > 1) {
32+		    undo_offset = 1;
33+		    curwin->w_cursor.lnum--;
34+	    }
35+	}
36+	save = u_save((linenr_T)(curwin->w_cursor.lnum + undo_offset -
37 					       (cap->cmdchar == 'O' ? 1 : 0)),
38-		   (linenr_T)(curwin->w_cursor.lnum +
39+		   (linenr_T)(curwin->w_cursor.lnum + undo_offset +
40 					       (cap->cmdchar == 'o' ? 1 : 0))
41-		       ) == OK
42+		) == OK;
43+	curwin->w_cursor.lnum += undo_offset;
44+	if (save
45 		&& open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
46 #ifdef FEAT_COMMENTS
47 		    has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
48