Commit 99566e2d13
Copy SHA
99566e2d13b805e89149c6338d59d0b6729549ec
parent: e28aeb4bea
Verified · cmc
cmc <hello@cleberg.net> · 2026-10-05 18:19 UTC
Keys in the Org menu and palette
Each Org menu item shows the keys that run it as a subtitle, and the
palette shows them beside each command, for the current preset: in
Doom, normal-state keys before insert-state ones. Keymap.keyLabels
ranks bindings that hold everywhere before contextual ones.
Changed files (5)
Sources/OrgCore/Keymap/Keymap.swift
+19 · view
@@ -95,6 +95,25 @@ public struct Keymap: Sendable, Equatable {
95 95 return seen . map { ( key : $0 . key , command : $0 . value ) } . sorted { $0 . key . description < $1 . key . description }
96 96 }
97 97
98 // / L i v e k e y s e q u e n c e s t h a t r u n ` c o m m a n d ` , f o r m a t t e d , a t m o s t ` l i m i t ` : w h a t m e n u s a n d t h e
99 // / p a l e t t e s h o w . E a r l i e r ` m o d e s ` f i r s t , t h e n b i n d i n g s t h a t h o l d e v e r y w h e r e b e f o r e t h o s e
100 // / w i t h a ` w h e n ` , t h e n s h o r t e r o n e s .
101 public func keyLabels ( for command : String , modes : [ String ? ] = [ nil ] , limit : Int = 2 ) -> [ String ] {
102 var found : [ ( keys : [ KeyChord ] , rank : [ Int ] ) ] = [ ]
103 for ( order , mode ) in modes . enumerated ( ) {
104 for binding in bindings . reversed ( ) where binding . mode = = mode && binding . command = = command
105 && candidates ( for : binding . keys , mode : mode ) . contains ( binding ) {
106 found . append ( ( binding . keys , [ order , binding . when = = nil ? 0 : 1 , binding . keys . count , found . count ] ) )
107 }
108 }
109 var seen : Set < String > = [ ]
110 return found . sorted { $0 . rank . lexicographicallyPrecedes ( $1 . rank ) }
111 . map { KeySequence . format ( $0 . keys ) }
112 . filter { seen . insert ( $0 ) . inserted }
113 . prefix ( limit )
114 . map { $0 }
115 }
116
98 117 // / T h e f i r s t l i v e b i n d i n g o f ` c o m m a n d ` , f o r s h o w i n g i t s k e y s .
99 118 public func keys ( for command : String , mode : String ? = nil ) -> [ KeyChord ] ? {
100 119 bindings . reversed ( ) . first { $0 . mode = = mode && $0 . command = = command && candidates ( for : $0 . keys , mode : mode ) . contains ( $0 ) } ? . keys
Sources/Orgstar/ContentView.swift
+3 −1 · view
@@ -124,7 +124,7 @@ struct ContentView: View {
124 124 . onChange ( of : options , initial : true ) { _ , options in session . options = options }
125 125 . onChange ( of : preset , initial : true ) { loadKeymap ( ) }
126 126 . sheet ( isPresented : $ commands . showingPalette ) {
127 PaletteView ( keymap : keymap ) { id in
127 PaletteView ( commands : commands ) { id in
128 128 commands . showingPalette = false
129 129 // A f t e r t h e s h e e t c l o s e s , s o a p p c o m m a n d s c a n p r e s e n t t h e i r o w n .
130 130 DispatchQueue . main . async { perform ( id ) }
@@ -325,6 +325,8 @@ struct ContentView: View {
325 325 private func loadKeymap ( ) {
326 326 let loaded = KeymapLoader . load ( preset )
327 327 keymap = loaded . keymap
328 commands . keymap = loaded . keymap
329 commands . modal = preset . isModal
328 330 if let first = loaded . problems . first {
329 331 let more = loaded . problems . count > 1 ? " (and \( loaded . problems . count - 1 ) more) " : " "
330 332 commands . show ( " keymap.toml: \( first ) \( more ) " )
Sources/Orgstar/Keys.swift
+3 −3 · view
@@ -132,7 +132,7 @@ extension EchoArea {
132 132
133 133 // / M - x a n d ⇧ ⌘ P : e v e r y c o m m a n d b y t i t l e , w i t h i t s k e y s .
134 134 struct PaletteView : View {
135 let keymap : Keymap
135 let commands : AppCommands
136 136 let run : ( String ) -> Void
137 137 @ State private var query = " "
138 138 @ Environment ( \ . dismiss ) private var dismiss
@@ -161,8 +161,8 @@ struct PaletteView: View {
161 161 HStack {
162 162 Text ( command . title )
163 163 Spacer ( )
164 if let keys = keymap . keys ( for : command . id ) {
165 Text ( KeySequence . format ( keys ) ) . font ( . system ( . caption , design : . monospaced ) ) . foregroundStyle ( . secondary )
164 if let keys = commands . keyLabel ( for : command . id ) {
165 Text ( keys ) . font ( . system ( . caption , design : . monospaced ) ) . foregroundStyle ( . secondary )
166 166 }
167 167 }
168 168 }
Sources/Orgstar/OrgstarApp.swift
+28 −4 · view
@@ -23,6 +23,16 @@ final class AppCommands {
23 23 var pendingKeys : String ?
24 24 // / T h e e d i t o r ' s e v i l s t a t e i n t h e D o o m p r e s e t .
25 25 var editorState : String ?
26 // / T h e k e y m a p i n u s e , f o r s h o w i n g k e y s i n m e n u s a n d t h e p a l e t t e .
27 var keymap = KeymapPreset . emacs . keymap
28 var modal = false
29
30 // / T h e k e y s t h a t r u n ` c o m m a n d ` , a s m e n u s a n d t h e p a l e t t e s h o w t h e m .
31 func keyLabel ( for command : String ) -> String ? {
32 let labels = keymap . keyLabels ( for : command , modes : modal ? [ " normal " , " insert " ] : [ nil ] )
33 return labels . isEmpty ? nil : labels . joined ( separator : " , " )
34 }
35
26 36 // / T h e e d i t o r ' s s e l e c t i o n , f o r t h e m o d e l i n e .
27 37 var selection = NSRange ( location : 0 , length : 0 )
28 38 var keyHints : [ KeyHint ] = [ ]
@@ -223,10 +233,7 @@ struct OrgstarApp: App {
223 233 }
224 234 . commands {
225 235 CommandMenu ( " Org " ) {
226 ForEach ( EditorCommands . all . filter { $0 . id . hasPrefix ( " org. " ) } , id : \ . id ) { command in
227 Button ( command . title ) { commands . editor ? . run ( command . id ) }
228 . disabled ( commands . editor = = nil )
229 }
236 OrgMenuItems ( commands : commands )
230 237 }
231 238 }
232 239 Settings {
@@ -235,6 +242,23 @@ struct OrgstarApp: App {
235 242 }
236 243 }
237 244
245 // / T h e O r g m e n u : e v e r y o r g c o m m a n d , w i t h t h e k e y s t h a t r u n i t i n t h e c u r r e n t k e y m a p .
246 struct OrgMenuItems : View {
247 let commands : AppCommands
248
249 var body : some View {
250 ForEach ( EditorCommands . all . filter { $0 . id . hasPrefix ( " org. " ) } , id : \ . id ) { command in
251 Button {
252 commands . editor ? . run ( command . id )
253 } label : {
254 Text ( command . title )
255 if let keys = commands . keyLabel ( for : command . id ) { Text ( keys ) }
256 }
257 . disabled ( commands . editor = = nil )
258 }
259 }
260 }
261
238 262 // / W i n d o w ▸ A g e n d a .
239 263 struct AgendaMenuItem : View {
240 264 @ Environment ( \ . openWindow ) private var openWindow
Tests/OrgCoreTests/KeymapTests.swift
+10 · view
@@ -87,6 +87,16 @@ struct KeymapTests {
87 87 # expect ( keymap . keys ( for : " org.todo.cycle " ) . map ( KeySequence . format ) = = " S-<right> " )
88 88 }
89 89
90 @ Test func keyLabelsForMenus ( ) {
91 let emacs = KeymapPreset . emacs . keymap
92 # expect ( emacs . keyLabels ( for : " org.todo.cycle " ) = = [ " C-c C-t " , " S-<right> " ] )
93 # expect ( emacs . keyLabels ( for : " org.schedule " ) = = [ " C-c C-s " ] )
94 let doom = KeymapPreset . doom . keymap
95 let labels = doom . keyLabels ( for : " org.todo.cycle " , modes : [ " normal " , " insert " ] )
96 # expect ( labels = = [ " C-c C-t " , " SPC m t " ] )
97 # expect ( doom . keyLabels ( for : " app.quit " , modes : [ " normal " , " insert " ] ) = = [ " SPC q q " ] )
98 }
99
90 100 @ Test func dispatchesPrefixes ( ) {
91 101 var dispatcher = KeyDispatcher ( keymap : KeymapPreset . emacs . keymap )
92 102 let c = KeyChord ( " c " , . control ) , t = KeyChord ( " t " , . control ) , z = KeyChord ( " z " , . control )