Commit 0a98939218

0a98939218fed456ffea150c12767bb72cc4ca5f

parent: 72f809fcd7

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-13 01:07 UTC

Folded headings: a new heading goes after the subtree; arrow keys skip folds on iOS

M-RET with the caret at the end of a folded heading stands for Emacs's point
behind the ellipsis and inserts after the subtree, instead of between the
heading and its hidden body. On iOS an arrow key moving into a fold jumps past
it, as on the Mac, rather than opening it. The key bar's structure buttons get
icons that don't read as caret movement.

Layout: unified · split

Sources/OrgCore/Commands/StructureCommands.swift +3 −1
@@ -47,7 +47,9 @@ extension EmacsBuffer {
47 let blank = blankBeforeHeading 47 let blank = blankBeforeHeading
48 let level = currentLevel 48 let level = currentLevel
49 let stars = String(repeating: "*", count: level ?? 1) 49 let stars = String(repeating: "*", count: level ?? 1)
50 let invisible = hidden.contains { $0.contains(max(point - 1, 0)) } 50 // The caret at the end of a folded heading's line, just before its hidden body, is where
51 // Emacs's point sits behind the ellipsis.
52 let invisible = hidden.contains { $0.contains(max(point - 1, 0)) || (!$0.isEmpty && $0.lowerBound == point + 1 && atHeading && eolp) }
51 if respectContent || invisible { 53 if respectContent || invisible {
52 if level == nil { 54 if level == nil {
53 outlineNextHeading() 55 outlineNextHeading()
Sources/OrgstarMobile/EditorScreen.swift +4 −4
@@ -649,10 +649,10 @@ struct KeyBar: View {
649 private static let keys: [(label: String, symbol: String?, keys: String)] = [ 649 private static let keys: [(label: String, symbol: String?, keys: String)] = [
650 ("Fold", "arrow.right.to.line", "TAB"), 650 ("Fold", "arrow.right.to.line", "TAB"),
651 ("Overview", "arrow.left.to.line", "S-TAB"), 651 ("Overview", "arrow.left.to.line", "S-TAB"),
652 ("Promote", "arrow.left", "M-<left>"), 652 ("Promote", "decrease.indent", "M-<left>"),
653 ("Demote", "arrow.right", "M-<right>"), 653 ("Demote", "increase.indent", "M-<right>"),
654 ("Move up", "arrow.up", "M-<up>"), 654 ("Move up", "arrow.up.square", "M-<up>"),
655 ("Move down", "arrow.down", "M-<down>"), 655 ("Move down", "arrow.down.square", "M-<down>"),
656 ("New heading or item", "plus", "M-RET"), 656 ("New heading or item", "plus", "M-RET"),
657 ("TODO", nil, "C-c C-t"), 657 ("TODO", nil, "C-c C-t"),
658 ("Act at point", "checkmark.square", "C-c C-c"), 658 ("Act at point", "checkmark.square", "C-c C-c"),
Sources/OrgstarMobile/MobileEditor+Display.swift +14 −2
@@ -221,13 +221,25 @@ extension MobileEditor {
221 textView.scrollRangeToVisible(NSRange(location: target, length: 0)) 221 textView.scrollRangeToVisible(NSRange(location: target, length: 0))
222 } 222 }
223 223
224 /// The caret left hidden text (a tap past a fold): what hides it opens. 224 /// The caret in hidden text: an arrow key moving forward into a fold jumps past it and
225 /// moving back jumps to the end of the heading line, as on the Mac; a tap past a fold
226 /// opens it.
225 func selectionChanged() { 227 func selectionChanged() {
226 let interval = Perf.begin("editor.selection") 228 let interval = Perf.begin("editor.selection")
227 defer { interval.end() } 229 defer { interval.end() }
228 redrawSelectedBlocks() 230 redrawSelectedBlocks()
229 let caret = textView.selectedRange.location 231 let caret = textView.selectedRange.location
230 if hidden.containing(caret) != nil { unfold(around: caret) } 232 defer {
233 previousCaret = textView.selectedRange.location
234 movedByArrow = false
235 }
236 if let range = hidden.containing(caret) {
237 if movedByArrow, textView.selectedRange.length == 0 {
238 textView.selectedRange = NSRange(location: caret >= previousCaret ? range.upperBound : range.lowerBound - 1, length: 0)
239 } else {
240 unfold(around: caret)
241 }
242 }
231 revealCaretLine() 243 revealCaretLine()
232 } 244 }
233} 245}
Sources/OrgstarMobile/MobileEditor.swift +4
@@ -108,6 +108,9 @@ final class MobileEditor: NSObject, DocumentBuffer {
108 internal(set) var fold = FoldState() 108 internal(set) var fold = FoldState()
109 var globalVisibility: GlobalVisibility? 109 var globalVisibility: GlobalVisibility?
110 let hidden = LockedRanges() 110 let hidden = LockedRanges()
111 /// An arrow key moved the caret, and where it was before; the caret skips folds then.
112 var movedByArrow = false
113 var previousCaret = 0
111 /// Blocks drawn on a band. 114 /// Blocks drawn on a band.
112 let shaded = LockedRanges() 115 let shaded = LockedRanges()
113 let fragmentStyle = FragmentStyle() 116 let fragmentStyle = FragmentStyle()
@@ -444,6 +447,7 @@ final class MobileEditor: NSObject, DocumentBuffer {
444 /// The Emacs keymap's commands on an iPad keyboard. Keys it doesn't bind, Command shortcuts 447 /// The Emacs keymap's commands on an iPad keyboard. Keys it doesn't bind, Command shortcuts
445 /// and editing keys (C-a, C-e, C-k...) are the text system's. 448 /// and editing keys (C-a, C-e, C-k...) are the text system's.
446 func handleKey(_ key: UIKey) -> Bool { 449 func handleKey(_ key: UIKey) -> Bool {
450 movedByArrow = [.keyboardUpArrow, .keyboardDownArrow, .keyboardLeftArrow, .keyboardRightArrow].contains(key.keyCode)
447 guard let chord = Self.chord(key), !chord.modifiers.contains(.command) else { return false } 451 guard let chord = Self.chord(key), !chord.modifiers.contains(.command) else { return false }
448 if isearch != nil, isearchKey(chord) { return true } 452 if isearch != nil, isearchKey(chord) { return true }
449 if modal { return handleModal(chord) } 453 if modal { return handleModal(chord) }
Tests/OrgCoreTests/StructureCommandTests.swift +15
@@ -35,6 +35,21 @@ struct StructureCommandTests {
35 } 35 }
36 #expect(result.selection == [9..<9]) 36 #expect(result.selection == [9..<9])
37 } 37 }
38
39 /// The caret at the end of a folded heading stands for Emacs's point behind the ellipsis:
40 /// the new heading goes after the subtree, not between the heading and its folded body.
41 @Test func newHeadingAtTheEndOfAFoldedHeadingGoesAfterTheSubtree() {
42 let text = "* One\nbody one\n** One child\nchild body\n* Two\nbody two\n"
43 // As the editors hide a folded heading: the lines after it, to its section's end.
44 let fold = 6..<(text as NSString).range(of: "* Two").location
45 let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [5..<5], hidden: [fold])
46 guard case .commit(let result) = InsertHeading().run(in: context) else {
47 Issue.record("expected a commit")
48 return
49 }
50 let new = result.edits.sorted { $0.range.lowerBound > $1.range.lowerBound }.reduce(text) { $1.apply(to: $0) }
51 #expect(new == "* One\nbody one\n** One child\nchild body\n* \n* Two\nbody two\n")
52 }
38} 53}
39 54
40struct StructureOracleTests { 55struct StructureOracleTests {
Tests/OrgEditorAppKitTests/EditorTests.swift +9
@@ -136,6 +136,15 @@ struct FoldingTests {
136 #expect(h.caret == 3) 136 #expect(h.caret == 3)
137 } 137 }
138 138
139 @Test func newHeadingAtTheEndOfAFoldedHeadingGoesAfterItsSubtree() {
140 let h = Harness(outline)
141 h.caret(at: 1)
142 h.editor.cycleAtCaret()
143 h.caret(at: 3)
144 h.editor.run("org.heading.insert")
145 #expect(h.string == "* a\nbody\n** b\nmore\n* \n* c\ntext\n")
146 }
147
139 @Test func copyingAcrossAFoldIncludesHiddenText() { 148 @Test func copyingAcrossAFoldIncludesHiddenText() {
140 let h = Harness(outline) 149 let h = Harness(outline)
141 h.caret(at: 1) 150 h.caret(at: 1)