Commit 1fcb653ab6
Copy SHA
1fcb653ab643d9b1dfcd6c208e2b4ac2572c2e10
parent: 1554237a4e
Verified · cmc
cmc <hello@cleberg.net> · 2026-10-07 08:03 UTC
Reread setup files on C-c C-c on a keyword line
As org-mode-restart does: C-c C-c on a #+ keyword line reads the
file's #+SETUPFILEs again and reparses with the new settings, keeping
the text and undo history, and the editors restyle.
Changed files (9)
Sources/OrgApp/DocumentSession.swift
+15 −4 · view
@@ -79,10 +79,21 @@ public final class DocumentSession {
79 79
80 80 // / A b u f f e r f o r ` b y t e s ` r e a d f r o m ` p a t h ` , w i t h t h e k e y w o r d s o f t h e s e t u p f i l e s i t n a m e s .
81 81 func loaded ( _ bytes : [ UInt8 ] , path : String ) -> DocumentState {
82 let settings = defaults . resolvingSetupFiles ( in : SourceText ( bytes : bytes ) . text , path : path ) { file in
83 ( try ? self . fileSystem . read ( URL ( fileURLWithPath : file ) ) ) . flatMap { $0 } . flatMap { String ( bytes : $0 , encoding : . utf8 ) }
84 }
85 return DocumentState ( bytes : bytes , defaults : settings )
82 DocumentState ( bytes : bytes , defaults : defaults . resolvingSetupFiles ( in : SourceText ( bytes : bytes ) . text , path : path , read : readText ) )
83 }
84
85 // / A f i l e ' s t e x t , f o r s e t u p f i l e s .
86 func readText ( _ file : String ) -> String ? {
87 ( try ? fileSystem . read ( URL ( fileURLWithPath : file ) ) ) . flatMap { $0 } . flatMap { String ( bytes : $0 , encoding : . utf8 ) }
88 }
89
90 // / C - c C - c o n a k e y w o r d l i n e : t h e o p e n f i l e ' s s e t u p f i l e s r e a d a g a i n ( ` o r g - m o d e - r e s t a r t ` ) .
91 public func refreshSetup ( ) {
92 guard let entry = current else { return }
93 let settings = defaults . resolvingSetupFiles ( in : entry . buffer . document . text , path : entry . path , read : readText )
94 entry . buffer . update { $0 . setDefaults ( settings ) }
95 refresh ( )
96 editVersion += 1
86 97 }
87 98
88 99 // / O n e o p e n f i l e .
Sources/OrgCore/Commands/Command.swift
+2 · view
@@ -194,6 +194,8 @@ public enum Effect: Sendable, Equatable {
194 194 case sparseTree ( show : [ Int ] , highlights : [ Range < Int > ] )
195 195 // / F o l d t h e s u b t r e e o f t h e h e a d i n g s t a r t i n g h e r e ( i n t h e n e w t e x t ) .
196 196 case fold ( Int )
197 // / R e a d t h e f i l e ' s ` # + S E T U P F I L E ` s a g a i n ( ` o r g - m o d e - r e s t a r t ` ) .
198 case refreshSetup
197 199 }
198 200
199 201 public struct EditResult : Sendable , Equatable {
Sources/OrgCore/Commands/CtrlCCtrlC.swift
+1 −1 · view
@@ -62,7 +62,7 @@ public struct CtrlCCtrlC: OrgCommand {
62 62 }
63 63 }
64 64 if buffer . lookingAtLine ( " ^[ \\ t]*# \\ +[A-Za-z_]+: " ) {
65 return . commit ( EditResult ( baseRevision : context . revision , edits : [ ] , effects : [ . message ( " Local setup has been refreshed " ) ] ) )
65 return . commit ( EditResult ( baseRevision : context . revision , edits : [ ] , effects : [ . refreshSetup , . message ( " Local setup has been refreshed " ) ] ) )
66 66 }
67 67 if buffer . propertyAtPoint ( ) != nil || buffer . lookingAtLine ( " ^[ \\ t]*:(?:PROPERTIES|END):[ \\ t]*$ " ) && buffer . existingPropertyBlock ( ) != nil {
68 68 return PropertyAction ( ) . run ( in : context )
Sources/OrgDocument/DocumentState.swift
+8 −1 · view
@@ -27,7 +27,7 @@ public struct DocumentState: Sendable {
27 27 public private ( set ) var tree : OrgTree
28 28 // / I n c r e a s e s o n e v e r y c h a n g e t o ` t e x t ` .
29 29 public private ( set ) var revision = 0
30 public let defaults : OrgSettings
30 public private ( set ) var defaults : OrgSettings
31 31 private var undoStack : [ [ TextEdit ] ] = [ ]
32 32 private var redoStack : [ [ TextEdit ] ] = [ ]
33 33
@@ -38,6 +38,13 @@ public struct DocumentState: Sendable {
38 38 self . defaults = defaults
39 39 }
40 40
41 // / N e w s e t t i n g s f o r t h e s a m e t e x t , a s w h e n s e t u p f i l e s a r e r e a d a g a i n ; u n d o h i s t o r y s t a y s .
42 public mutating func setDefaults ( _ defaults : OrgSettings ) {
43 self . defaults = defaults
44 tree = OrgParser . parse ( text , defaults : defaults )
45 revision += 1
46 }
47
41 48 public var mergeBase : [ UInt8 ] { source . originalBytes }
42 49 public var isDirty : Bool { text != source . text }
43 50 public var isEditable : Bool { source . isEditable }
Sources/OrgEditorAppKit/OrgEditor.swift
+4 · view
@@ -1350,6 +1350,7 @@ extension OrgEditor: DocumentBuffer {
1350 1350 // / m a p p e d t h r o u g h t h e c h a n g e . T e x t - v i e w u n d o r e f e r s t o t h e o l d t e x t , s o i t i s c l e a r e d .
1351 1351 public func update < T > ( _ change : ( inout DocumentState ) throws -> T ) rethrows -> T {
1352 1352 let before = document . text
1353 let defaultsBefore = document . defaults
1353 1354 let selected = textView . selectedRange ( )
1354 1355 let result = try change ( & document )
1355 1356 if document . text != before {
@@ -1364,6 +1365,9 @@ extension OrgEditor: DocumentBuffer {
1364 1365 if ! selection . isEmpty { textView . setSelectedRange ( NSRange ( selection ) ) }
1365 1366 textView . undoManager ? . removeAllActions ( withTarget : textView . textStorage ! )
1366 1367 textView . undoManager ? . removeAllActions ( withTarget : textView )
1368 } else if document . defaults != defaultsBefore {
1369 // N e w s e t t i n g s f o r t h e s a m e t e x t : T O D O k e y w o r d s m a y c o l o r d i f f e r e n t l y .
1370 restyleEverything ( )
1367 1371 }
1368 1372 onChange ? ( )
1369 1373 return result
Sources/Orgstar/EditorView.swift
+1 · view
@@ -114,6 +114,7 @@ struct EditorView: NSViewRepresentable {
114 114 case . copy ( let text ) :
115 115 NSPasteboard . general . clearContents ( )
116 116 NSPasteboard . general . setString ( text , forType : . string )
117 case . refreshSetup : session . refreshSetup ( )
117 118 case . message , . sparseTree , . fold : break
118 119 }
119 120 }
Sources/OrgstarMobile/EditorScreen.swift
+1 · view
@@ -162,6 +162,7 @@ struct EditorScreen: View {
162 162 editor . onEditBlock = { edit in sheet = . block ( edit ) }
163 163 editor . onTangle = { scope in message = session . tangle ( scope ) }
164 164 editor . onBabel = { job in run ( job ) }
165 editor . onRefreshSetup = { session . refreshSetup ( ) }
165 166 editor . onAgenda = { start , count in model . agendaRequest = . init ( start : start , count : count ) }
166 167 editor . onAppCommand = { id in
167 168 switch id {
Sources/OrgstarMobile/MobileEditor.swift
+6 · view
@@ -66,6 +66,8 @@ final class MobileEditor: NSObject, DocumentBuffer {
66 66 var onTangle : ( ( Tangle . Scope ) -> Void ) ?
67 67 // / A s o u r c e b l o c k t o r u n ( C - c C - c ) .
68 68 var onBabel : ( ( BabelJob ) -> Void ) ?
69 // / T h e f i l e ' s s e t u p f i l e s t o r e a d a g a i n ( C - c C - c o n a k e y w o r d l i n e ) .
70 var onRefreshSetup : ( ( ) -> Void ) ?
69 71 // / A b l o c k , a t a b l e ' s f o r m u l a s o r a f i e l d t o e d i t a p a r t ( C - c ' , C - c ` ) .
70 72 var onEditBlock : ( ( BlockEdit ) -> Void ) ?
71 73 // / A f o l l o w e d t i m e s t a m p ' s a g e n d a d a y s : t h e f i r s t ( a ` D a y s ` n u m b e r ) a n d h o w m a n y .
@@ -119,6 +121,7 @@ final class MobileEditor: NSObject, DocumentBuffer {
119 121
120 122 func update < T > ( _ change : ( inout DocumentState ) throws -> T ) rethrows -> T {
121 123 let before = document . text
124 let defaultsBefore = document . defaults
122 125 let selected = textView . selectedRange
123 126 let result = try change ( & document )
124 127 if document . text != before {
@@ -128,6 +131,8 @@ final class MobileEditor: NSObject, DocumentBuffer {
128 131 textView . selectedRange = NSRange ( location : min ( selected . location , length ) , length : 0 )
129 132 textView . undoManager ? . removeAllActions ( )
130 133 setFolds ( fold )
134 } else if document . defaults != defaultsBefore {
135 restyle ( 0. . < ( document . text as NSString ) . length )
131 136 }
132 137 return result
133 138 }
@@ -157,6 +162,7 @@ final class MobileEditor: NSObject, DocumentBuffer {
157 162 var state = fold
158 163 state . folds . insert ( start )
159 164 setFolds ( state )
165 case . refreshSetup : onRefreshSetup ? ( )
160 166 default : break
161 167 }
162 168 }
Tests/OrgAppTests/AppTests.swift
+17 · view
@@ -199,6 +199,23 @@ struct DocumentSessionTests {
199 199 # expect ( session . document ? . tree . settings . todoKeywordNames . contains ( " NEXT " ) = = true )
200 200 }
201 201
202 @ Test func keywordCtrlCCtrlCRereadsSetupFiles ( ) throws {
203 let notes = try Folder ( )
204 try notes . write ( " setup.org " , " #+TODO: NEXT | DONE \n " )
205 let file = try notes . write ( " a.org " , " #+SETUPFILE: setup.org \n * WAIT One \n " )
206 let session = DocumentSession ( recovery : FileRecoveryStore ( directory : notes . url . appendingPathComponent ( " .r " ) ) )
207 session . open ( file . path )
208 # expect ( session . document ? . tree . settings . todoKeywordNames . contains ( " WAIT " ) = = false )
209 try notes . write ( " setup.org " , " #+TODO: NEXT WAIT | DONE \n " )
210 let text = session . document ! . text
211 let context = EditContext ( revision : 0 , text : text , tree : OrgParser . parse ( text ) , selection : [ 2. . < 2 ] )
212 guard case . commit ( let result ) = CtrlCCtrlC ( ) . run ( in : context ) else { Issue . record ( " no commit " ) ; return }
213 # expect ( result . effects . contains ( . refreshSetup ) )
214 session . refreshSetup ( )
215 # expect ( session . document ? . tree . settings . todoKeywordNames . contains ( " WAIT " ) = = true )
216 # expect ( session . document ? . isDirty = = false )
217 }
218
202 219 @ Test func missingFileReportsAnError ( ) {
203 220 let session = DocumentSession ( )
204 221 session . open ( " /nonexistent/a.org " )