Commit 273e4d176e
Copy SHA
273e4d176eec0f10ccac2b529a998dc78a39cc3f
parent: b4ec29e3b2
Verified · cmc
cmc <hello@cleberg.net> · 2026-10-07 22:28 UTC
Fix quoted header values, shell values and setup-file staleness
Babel keeps track of quoted header values, so tangling evaluates
only unquoted (...) as Lisp, as org-babel-read does. Shell blocks run
as a script return the exit status for :results value, as ob-shell
appends echo $? there too.
#+SETUPFILE: the naming file counts as read, so a file naming itself
adds nothing. Setup files are read again when a file reloads from
disk, on useDiskVersion and on a conflict merge; the agenda doesn't
reuse its cache for files naming setup files; and an index row's
settings version includes its setup keywords, so a changed setup file
indexes the files naming it again.
Changed files (13)
Sources/OrgApp/AgendaModel.swift
+2 −1 · view
@@ -102,7 +102,8 @@ public final class AgendaModel {
102 102 let attributes = try ? FileManager . default . attributesOfItem ( atPath : path )
103 103 let mtime = attributes ? [ . modificationDate ] as ? Date ? ? . distantPast
104 104 let size = ( attributes ? [ . size ] as ? NSNumber ) ? . intValue ? ? - 1
105 let cached = cache [ path ] . flatMap { $0 . mtime = = mtime && $0 . size = = size ? $0 . source : nil }
105 // A f i l e r e a d i n g s e t u p f i l e s i s r e a d a g a i n : t h e y m a y h a v e c h a n g e d .
106 let cached = cache [ path ] . flatMap { $0 . mtime = = mtime && $0 . size = = size && ! $0 . source . readsSetupFiles ? $0 . source : nil }
106 107 reads . append ( ( path , cached , mtime , size , nil ) )
107 108 }
108 109 let loaded = await Task . detached {
Sources/OrgApp/DocumentSession.swift
+11 −4 · view
@@ -90,12 +90,17 @@ public final class DocumentSession {
90 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 91 public func refreshSetup ( ) {
92 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 ) }
93 resolveSetup ( entry )
95 94 refresh ( )
96 95 editVersion += 1
97 96 }
98 97
98 // / T h e e n t r y ' s s e t u p f i l e s r e a d a g a i n , a s i t s t e x t n a m e s t h e m n o w .
99 private func resolveSetup ( _ entry : Entry ) {
100 let settings = defaults . resolvingSetupFiles ( in : entry . buffer . document . text , path : entry . path , read : readText )
101 if settings != entry . buffer . document . defaults { entry . buffer . update { $0 . setDefaults ( settings ) } }
102 }
103
99 104 // / O n e o p e n f i l e .
100 105 @ MainActor
101 106 final class Entry {
@@ -358,7 +363,8 @@ public final class DocumentSession {
358 363 do {
359 364 guard let bytes = try fileSystem . read ( url ) else { return }
360 365 if isDirty { try recovery . keep ( try buffer . document . encodedText ( ) , for : url , label : " local " ) }
361 buffer . update { $0 = DocumentState ( bytes : bytes , defaults : $0 . defaults ) }
366 let path = url . path
367 buffer . update { $0 = loaded ( bytes , path : path ) }
362 368 current ? . conflict = nil
363 369 } catch {
364 370 lastError = " Can't read \( name ) : \( error . localizedDescription ) "
@@ -383,7 +389,7 @@ public final class DocumentSession {
383 389 try recovery . keep ( try buffer . document . encodedText ( ) , for : url , label : " local " )
384 390 let merged = mergeWithMarkers ( base : versions . base , ours : versions . mine , theirs : versions . disk , oursLabel : " yours " , theirsLabel : " disk " )
385 391 try buffer . update { state in
386 state = DocumentState ( bytes : disk , defaults : state . defaults )
392 state = loaded ( disk , path : url . path )
387 393 try state . apply ( lineEdits ( from : versions . disk , to : merged ) , baseRevision : state . revision )
388 394 }
389 395 current ? . conflict = nil
@@ -835,6 +841,7 @@ public final class DocumentSession {
835 841 entry . conflict = " \( name ) changed on disk, and the changes conflict with yours. "
836 842 case . merged , . reloaded :
837 843 entry . conflict = nil
844 resolveSetup ( entry )
838 845 // E d i t s m e r g e d w i t h t h e d i s k ' s s t i l l n e e d s a v i n g .
839 846 scheduleAutosave ( entry )
840 847 case . unchanged :
Sources/OrgCore/Agenda/AgendaSource.swift
+3 · view
@@ -4,6 +4,8 @@ import Foundation
4 4 // / a n d t h e a c t i v e t i m e s t a m p s a n d d a t e r a n g e s o r g ' s a g e n d a w o u l d c o n s i d e r . P o s i t i o n s a r e U T F - 1 6 .
5 5 public struct AgendaSource : Sendable {
6 6 public let path : String
7 // / T h e f i l e n a m e s ` # + S E T U P F I L E ` s .
8 public let readsSetupFiles : Bool
7 9
8 10 struct Heading : Sendable {
9 11 // / S t a r t o f t h e h e a d i n g l i n e .
@@ -96,6 +98,7 @@ public struct AgendaSource: Sendable {
96 98
97 99 public init ( path : String , text : String , defaults : OrgSettings = . default ) {
98 100 self . path = path
101 readsSetupFiles = text . range ( of : " #+setupfile: " , options : . caseInsensitive ) != nil
99 102 let tree = OrgParser . parse ( text , defaults : defaults )
100 103 let model = DocumentModel ( tree : tree )
101 104 let ns = text as NSString
Sources/OrgCore/Compute/Babel.swift
+11 −7 · view
@@ -337,7 +337,7 @@ public enum Babel {
337 337 // c o m m a n d l i n e a f t e r i t a n d ` : s t d i n ` a s i t s i n p u t ; t h e v a l u e i s i t s l a s t l i n e .
338 338 var script = ( shebang ? ? " #!/usr/bin/env \( executable ) " ) + " \n "
339 339 if params . single [ " :padline " ] != " no " { script += " \n " }
340 script += ( lines + [ body ] ) . joined ( separator : " \n " )
340 script += ( lines + [ body ] ) . joined ( separator : " \n " ) + ( valueIsExitStatus ? " \n echo $? " : " " )
341 341 let command = Babel . scriptFileToken + ( stdin != nil || cmdline != nil ? " " + ( cmdline ? ? " " ) : " " )
342 342 var job = BabelJob (
343 343 language : language , runner : . process ( executable : executable , arguments : [ " -c " , command ] ) ,
@@ -562,6 +562,8 @@ public enum Babel {
562 562 var single : [ String : String ] = [ : ]
563 563 var vars : [ ( name : String , value : String ) ] = [ ]
564 564 var wrap : String ?
565 // / K e y s w h o s e v a l u e i s L i s p t o e v a l u a t e ( ` o r g - b a b e l - r e a d ` ) : u n q u o t e d , s t a r t i n g w i t h ` ( ` .
566 var lisp : Set < String > = [ ]
565 567
566 568 static let resultGroups : [ Set < String > ] = [
567 569 [ " file " , " list " , " vector " , " table " , " scalar " , " verbatim " ] ,
@@ -610,7 +612,12 @@ public enum Babel {
610 612 layers += extra
611 613
612 614 for layer in layers {
613 for ( key , value ) in layer {
615 for ( key , raw ) in layer {
616 let quoted = key != " :var " && raw . count >= 2 && raw . hasPrefix ( " \" " ) && raw . hasSuffix ( " \" " )
617 let value = quoted ? Params . unescape ( String ( raw . dropFirst ( ) . dropLast ( ) ) ) : raw
618 if key != " :var " {
619 if ! quoted , value . hasPrefix ( " ( " ) { lisp . insert ( key ) } else { lisp . remove ( key ) }
620 }
614 621 switch key {
615 622 case " :results " :
616 623 for word in value . split ( whereSeparator : \ . isWhitespace ) . map ( String . init ) {
@@ -715,11 +722,8 @@ public enum Babel {
715 722 let trimmed = part . trimmingCharacters ( in : . whitespaces )
716 723 guard trimmed . hasPrefix ( " : " ) else { continue }
717 724 let key = String ( trimmed . prefix { ! $0 . isWhitespace } ) . lowercased ( )
718 var value = trimmed . dropFirst ( key . count ) . trimmingCharacters ( in : . whitespaces )
719 if key != " :var " , value . count >= 2 , value . hasPrefix ( " \" " ) , value . hasSuffix ( " \" " ) {
720 value = Params . unescape ( String ( value . dropFirst ( ) . dropLast ( ) ) )
721 }
722 pairs . append ( ( key , value ) )
725 // Q u o t e s s t a y u n t i l t h e v a l u e i s u s e d , s o a q u o t e d s t r i n g i s n ' t t a k e n f o r L i s p .
726 pairs . append ( ( key , trimmed . dropFirst ( key . count ) . trimmingCharacters ( in : . whitespaces ) ) )
723 727 }
724 728 return pairs
725 729 }
Sources/OrgCore/Compute/Tangle.swift
+2 −1 · view
@@ -128,7 +128,8 @@ public enum Tangle {
128 128
129 129 static func params ( _ block : SrcBlockInfo , model : DocumentModel , text : NSString , path : String ) throws -> Babel . Params {
130 130 var params = try Babel . Params ( block : block , model : model , text : text )
131 for ( key , value ) in params . single where value . hasPrefix ( " ( " ) && key != " :tangle-mode " {
131 for key in params . lisp where key != " :tangle-mode " {
132 guard let value = params . single [ key ] else { continue }
132 133 params . single [ key ] = try evaluate ( value , key : key , path : path )
133 134 }
134 135 return params
Sources/OrgCore/Parser/SetupFile.swift
+5 −4 · view
@@ -5,9 +5,10 @@ import Foundation
5 5 // / n a m i n g f i l e ' s f o l d e r ; U R L s a r e n ' t f e t c h e d .
6 6 public enum SetupFile {
7 7 // / T h e k e y w o r d l i n e s t h e s e t u p f i l e s ` t e x t ` n a m e s b r i n g , i n o r d e r , a s ` # + K E Y : v a l u e ` l i n e s .
8 // / ` r e a d ` g i v e s a f i l e ' s t e x t , o r n i l w h e n i t c a n ' t b e r e a d .
9 public static func keywordLines ( in text : String , directory : String , read : ( String ) -> String ? ) -> String {
10 lines ( in : text , directory : directory , read : read , own : false , seen : [ ] )
8 // / ` r e a d ` g i v e s a f i l e ' s t e x t , o r n i l w h e n i t c a n ' t b e r e a d . ` p a t h ` , t h e n a m i n g f i l e ' s ,
9 // / c o u n t s a s r e a d a l r e a d y , a s ` b u f f e r - f i l e - n a m e ` d o e s i n O r g .
10 public static func keywordLines ( in text : String , directory : String , path : String ? = nil , read : ( String ) -> String ? ) -> String {
11 lines ( in : text , directory : directory , read : read , own : false , seen : path . map { [ ( $0 as NSString ) . standardizingPath ] } ? ? [ ] )
11 12 }
12 13
13 14 private static func lines ( in text : String , directory : String , read : ( String ) -> String ? , own : Bool , seen : Set < String > ) -> String {
@@ -45,7 +46,7 @@ extension OrgSettings {
45 46 // / T h e s e s e t t i n g s 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 ` t e x t ` n a m e s , f o r a f i l e a t ` p a t h ` .
46 47 public func resolvingSetupFiles ( in text : String , path : String , read : ( String ) -> String ? ) -> OrgSettings {
47 48 var copy = self
48 copy . setupKeywords = SetupFile . keywordLines ( in : text , directory : ( path as NSString ) . deletingLastPathComponent , read : read )
49 copy . setupKeywords = SetupFile . keywordLines ( in : text , directory : ( path as NSString ) . deletingLastPathComponent , path : path , read : read )
49 50 return copy
50 51 }
51 52
Sources/OrgIndex/FileRecord.swift
+21 −1 · view
@@ -120,13 +120,33 @@ public struct FileRecord: Sendable, Equatable {
120 120 self . size = bytes . count
121 121 self . mtime = mtime
122 122 self . hash = Self . hash ( bytes )
123 self . settingsVersion = settings . version
124 123 let source = SourceText ( bytes : bytes )
125 124 var resolved = settings
126 125 resolved . org = settings . org . resolvingSetupFiles ( in : source . text , path : path , read : OrgSettings . readFile )
126 self . settingsVersion = Self . settingsVersion ( settings , text : source . text , setupKeywords : resolved . org . setupKeywords )
127 127 headings = kind = = . conflict ? [ ] : Self . headings ( source . text , settings : resolved )
128 128 }
129 129
130 // / T h e s e t t i n g s v e r s i o n a f i l e ' s r o w s a r e b u i l t u n d e r : t h e i n d e x ' s , w i t h t h e k e y w o r d s o f
131 // / t h e s e t u p f i l e s i t n a m e s m i x e d i n , s o a c h a n g e d s e t u p f i l e i n d e x e s i t a g a i n .
132 public static func settingsVersion ( _ settings : IndexSettings , text : String , setupKeywords : String ) -> Int {
133 guard text . range ( of : " #+setupfile: " , options : . caseInsensitive ) != nil else { return settings . version }
134 var hash : UInt64 = 0xcbf29ce484222325
135 for byte in setupKeywords . utf8 {
136 hash ^= UInt64 ( byte )
137 hash = hash &* 0x100000001b3
138 }
139 return settings . version + 1_000_000 * ( 1 + Int ( hash % 1_000_000 ) )
140 }
141
142 // / ` s e t t i n g s V e r s i o n ` f o r a f i l e ' s b y t e s , r e a d i n g t h e s e t u p f i l e s i t n a m e s .
143 public static func settingsVersion ( _ settings : IndexSettings , bytes : [ UInt8 ] , path : String ) -> Int {
144 let text = SourceText ( bytes : bytes ) . text
145 guard text . range ( of : " #+setupfile: " , options : . caseInsensitive ) != nil else { return settings . version }
146 let keywords = settings . org . resolvingSetupFiles ( in : text , path : path , read : OrgSettings . readFile ) . setupKeywords
147 return settingsVersion ( settings , text : text , setupKeywords : keywords )
148 }
149
130 150 // / A f i l e t h e i n d e x l i s t s w i t h o u t r e a d i n g : ` h a s h ` s t a n d s f o r i t s s i z e a n d m t i m e .
131 151 public init ( path : String , root : String , unreadSize size : Int , mtime : Double , settings : IndexSettings ) {
132 152 self . path = path
Sources/OrgWorkspace/Reconciler.swift
+4 −2 · view
@@ -90,7 +90,8 @@ public struct Reconciler: Sendable {
90 90 guard let data = try ? Data ( contentsOf : file . url ) else { continue }
91 91 let bytes = [ UInt8 ] ( data )
92 92 let hash = FileRecord . hash ( bytes )
93 if let state = known [ path ] , state . hash = = hash , state . settingsVersion = = settings . version , state . kind = = file . kind {
93 if let state = known [ path ] , state . hash = = hash , state . settingsVersion = = FileRecord . settingsVersion ( settings , bytes : bytes , path : path ) ,
94 state . kind = = file . kind {
94 95 change . touches . append ( ( path , file . mtime ) )
95 96 report . touched . append ( path )
96 97 } else {
@@ -105,7 +106,8 @@ public struct Reconciler: Sendable {
105 106 let path = candidate . file . url . path
106 107 if known [ path ] = = nil ,
107 108 let ( oldPath , _ ) = missing . first ( where : {
108 $0 . value . hash = = candidate . hash && $0 . value . settingsVersion = = settings . version && $0 . value . kind = = candidate . file . kind
109 $0 . value . hash = = candidate . hash && $0 . value . kind = = candidate . file . kind
110 && $0 . value . settingsVersion = = ( candidate . file . kind = = . other ? settings . version : FileRecord . settingsVersion ( settings , bytes : candidate . bytes , path : path ) )
109 111 } ) {
110 112 missing [ oldPath ] = nil
111 113 change . moves . append ( ( oldPath , path , candidate . file . mtime ) )
Tests/OrgAppTests/AppTests.swift
+12 · view
@@ -216,6 +216,18 @@ struct DocumentSessionTests {
216 216 # expect ( session . document ? . isDirty = = false )
217 217 }
218 218
219 @ Test func reloadingFromDiskReadsSetupFilesAgain ( ) throws {
220 let notes = try Folder ( )
221 try notes . write ( " setup.org " , " #+TODO: NEXT | DONE \n " )
222 let file = try notes . write ( " a.org " , " * WAIT One \n " )
223 let session = DocumentSession ( recovery : FileRecoveryStore ( directory : notes . url . appendingPathComponent ( " .r " ) ) )
224 session . open ( file . path )
225 try notes . write ( " a.org " , " #+SETUPFILE: setup.org \n * WAIT One \n " )
226 try notes . write ( " setup.org " , " #+TODO: WAIT | DONE \n " )
227 session . fileChanged ( file . path )
228 # expect ( session . document ? . tree . settings . todoKeywordNames . contains ( " WAIT " ) = = true )
229 }
230
219 231 @ Test func missingFileReportsAnError ( ) {
220 232 let session = DocumentSession ( )
221 233 session . open ( " /nonexistent/a.org " )
Tests/OrgCoreTests/BabelTests.swift
+3 · view
@@ -145,6 +145,9 @@ struct BabelTests {
145 145 " #+NAME: items \n - x \n - y \n \n #+begin_src sh :stdin items \n wc -l | tr -d ' ' \n #+end_src \n " ,
146 146 " #+begin_src bash :shebang #!/bin/bash :results output \n echo ${BASH_VERSION:+bash} \n #+end_src \n " ,
147 147 " #+begin_src sh :cmdline a b :padline no :var x=5 :results output \n echo $x $# \n #+end_src \n " ,
148 " #+NAME: in \n | hello | \n \n #+begin_src sh :results value :stdin in \n cat \n false \n #+end_src \n " ,
149 " #+begin_src sh :results value :cmdline x \n echo one \n #+end_src \n " ,
150 " #+begin_src bash :shebang #!/bin/bash :cmdline \" a b \" c :results output \n echo $# \n #+end_src \n " ,
148 151 " #+begin_src sh :stdin src :results output \n tr a-z A-Z \n #+end_src \n \n #+NAME: src \n #+begin_src sh \n echo hello \n #+end_src \n " ,
149 152 // ` : v a r ` f r o m o t h e r b l o c k s ' r e s u l t s .
150 153 " #+begin_src sh :var x=five \n echo $((x+1)) \n #+end_src \n \n #+NAME: five \n #+begin_src sh \n echo 5 \n #+end_src \n " ,
Tests/OrgCoreTests/SetupFileTests.swift
+5 · view
@@ -36,6 +36,11 @@ struct SetupFileTests {
36 36 # expect ( lines . contains ( " #+TYP_TODO: BUG | FIXED \n " ) )
37 37 # expect ( lines . components ( separatedBy : " #+TODO: " ) . count = = 2 )
38 38 # expect ( SetupFile . keywordLines ( in : " * x \n " , directory : " /w " ) { files [ $0 ] } = = " " )
39 // A f i l e n a m i n g i t s e l f a d d s n o t h i n g ; a s e t u p f i l e n a m i n g i t b a c k a d d s o n l y i t s o w n .
40 let own = " #+SETUPFILE: a.org \n #+TODO: A B | C \n "
41 # expect ( SetupFile . keywordLines ( in : own , directory : " /w " , path : " /w/a.org " ) { $0 = = " /w/a.org " ? own : nil } = = " " )
42 let back = [ " /w/a.org " : " #+SETUPFILE: b.org \n #+TODO: A | B \n " , " /w/b.org " : " #+SETUPFILE: a.org \n #+TODO: X | Y \n " ]
43 # expect ( SetupFile . keywordLines ( in : back [ " /w/a.org " ] ! , directory : " /w " , path : " /w/a.org " ) { back [ $0 ] } = = " #+TODO: X | Y \n " )
39 44 # expect ( SetupFile . keywordLines ( in : " #+SETUPFILE: https://example.com/a.org \n " , directory : " /w " ) { _ in " #+TODO: X \n " } = = " " )
40 45 }
41 46
Tests/OrgCoreTests/TangleTests.swift
+3 · view
@@ -223,6 +223,9 @@ struct TangleTests {
223 223 # + begin_src sh : tangle ( file - name - sans - extension ( file - name - nondirectory buffer - file - name ) )
224 224 echo named after the file
225 225 # + end_src
226 # + begin_src sh : tangle quoted . sh : prologue " (cd /tmp && ls) "
227 echo quoted
228 # + end_src
226 229 # + begin_src python - r - l " #(ref:%s) " : tangle refs . py
227 230 x = 1 # ( ref : one )
228 231 y = 2 ( ref : kept )
Tests/OrgWorkspaceTests/ReconcileTests.swift
+13 · view
@@ -42,6 +42,19 @@ struct ReconcileTests {
42 42 # expect ( try reconciler . reconcile ( root : folder . url ) . touched = = [ a . path ] )
43 43 }
44 44
45 @ Test func changedSetupFileReindexesTheFilesNamingIt ( ) throws {
46 let folder = try Folder ( )
47 let index = try IndexStore ( )
48 let reconciler = Reconciler ( index : index , settings : IndexSettings ( ) )
49 try folder . write ( " setup.txt " , " #+TODO: TODO | DONE \n " )
50 let a = try folder . write ( " a.org " , " #+SETUPFILE: setup.txt \n * NEXT Alpha \n " )
51 _ = try reconciler . reconcile ( root : folder . url )
52 # expect ( try reconciler . reconcile ( root : folder . url ) . indexed . isEmpty )
53 try folder . write ( " setup.txt " , " #+TODO: NEXT | DONE \n " )
54 # expect ( try reconciler . reconcile ( root : folder . url ) . indexed . contains ( a . path ) )
55 # expect ( try reconciler . reconcile ( root : folder . url ) . indexed . isEmpty )
56 }
57
45 58 @ Test func newSettingsVersionReindexes ( ) throws {
46 59 let folder = try Folder ( )
47 60 let index = try IndexStore ( )