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 {
4747 let blank = blankBeforeHeading
4848 let level = currentLevel
4949 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) }
5153 if respectContent || invisible {
5254 if level == nil {
5355 outlineNextHeading()
Sources/OrgstarMobile/EditorScreen.swift +4 −4
@@ -649,10 +649,10 @@ struct KeyBar: View {
649649 private static let keys: [(label: String, symbol: String?, keys: String)] = [
650650 ("Fold", "arrow.right.to.line", "TAB"),
651651 ("Overview", "arrow.left.to.line", "S-TAB"),
652 ("Promote", "arrow.left", "M-<left>"),
653 ("Demote", "arrow.right", "M-<right>"),
654 ("Move up", "arrow.up", "M-<up>"),
655 ("Move down", "arrow.down", "M-<down>"),
652 ("Promote", "decrease.indent", "M-<left>"),
653 ("Demote", "increase.indent", "M-<right>"),
654 ("Move up", "arrow.up.square", "M-<up>"),
655 ("Move down", "arrow.down.square", "M-<down>"),
656656 ("New heading or item", "plus", "M-RET"),
657657 ("TODO", nil, "C-c C-t"),
658658 ("Act at point", "checkmark.square", "C-c C-c"),
Sources/OrgstarMobile/MobileEditor+Display.swift +14 −2
@@ -221,13 +221,25 @@ extension MobileEditor {
221221 textView.scrollRangeToVisible(NSRange(location: target, length: 0))
222222 }
223223
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.
225227 func selectionChanged() {
226228 let interval = Perf.begin("editor.selection")
227229 defer { interval.end() }
228230 redrawSelectedBlocks()
229231 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 }
231243 revealCaretLine()
232244 }
233245}
Sources/OrgstarMobile/MobileEditor.swift +4
@@ -108,6 +108,9 @@ final class MobileEditor: NSObject, DocumentBuffer {
108108 internal(set) var fold = FoldState()
109109 var globalVisibility: GlobalVisibility?
110110 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
111114 /// Blocks drawn on a band.
112115 let shaded = LockedRanges()
113116 let fragmentStyle = FragmentStyle()
@@ -444,6 +447,7 @@ final class MobileEditor: NSObject, DocumentBuffer {
444447 /// The Emacs keymap's commands on an iPad keyboard. Keys it doesn't bind, Command shortcuts
445448 /// and editing keys (C-a, C-e, C-k...) are the text system's.
446449 func handleKey(_ key: UIKey) -> Bool {
450 movedByArrow = [.keyboardUpArrow, .keyboardDownArrow, .keyboardLeftArrow, .keyboardRightArrow].contains(key.keyCode)
447451 guard let chord = Self.chord(key), !chord.modifiers.contains(.command) else { return false }
448452 if isearch != nil, isearchKey(chord) { return true }
449453 if modal { return handleModal(chord) }
Tests/OrgCoreTests/StructureCommandTests.swift +15
@@ -35,6 +35,21 @@ struct StructureCommandTests {
3535 }
3636 #expect(result.selection == [9..<9])
3737 }
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 }
3853}
3954
4055struct StructureOracleTests {
Tests/OrgEditorAppKitTests/EditorTests.swift +9
@@ -136,6 +136,15 @@ struct FoldingTests {
136136 #expect(h.caret == 3)
137137 }
138138
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
139148 @Test func copyingAcrossAFoldIncludesHiddenText() {
140149 let h = Harness(outline)
141150 h.caret(at: 1)