Sources/OrgCore/Keymap/Presets.swift

2068474b3a12dba3acf176f92115566e0932bda9
orgstar/Sources/OrgCore/Keymap/Presets.swift history · blame · raw

507 lines · 13268 bytes

  1/// The keymaps the app ships. The user's `keymap.toml` is layered on top of the chosen one.
  2public enum KeymapPreset: String, Sendable, CaseIterable {
  3    case emacs, mac
  4
  5    public var title: String {
  6        switch self {
  7        case .emacs: "Emacs"
  8        case .mac: "Mac"
  9        }
 10    }
 11
 12    public var keymap: Keymap {
 13        var problems: [String] = []
 14        let map = try! Keymap(toml: toml, problems: &problems)
 15        precondition(problems.isEmpty, "preset \(rawValue): \(problems)")
 16        return map
 17    }
 18
 19    public var toml: String {
 20        switch self {
 21        case .emacs: Self.common + Self.emacsBindings
 22        case .mac: Self.common + Self.macBindings
 23        }
 24    }
 25
 26    static let common = """
 27        [[bind]]
 28        keys = "TAB"
 29        command = "org.cycle"
 30        when = "heading"
 31
 32        [[bind]]
 33        keys = "S-TAB"
 34        command = "org.cycle-global"
 35        [[bind]]
 36        keys = "TAB"
 37        command = "org.table.next-field"
 38        when = "table"
 39        [[bind]]
 40        keys = "S-TAB"
 41        command = "org.table.previous-field"
 42        when = "table"
 43        [[bind]]
 44        keys = "RET"
 45        command = "org.table.next-row"
 46        when = "table"
 47
 48        """
 49
 50    static let emacsBindings = """
 51        # Movement and editing. With a mark set (C-SPC), movement extends the region.
 52        [[bind]]
 53        keys = "C-f"
 54        command = "edit.forward-char"
 55        [[bind]]
 56        keys = "C-b"
 57        command = "edit.backward-char"
 58        [[bind]]
 59        keys = "C-n"
 60        command = "edit.next-line"
 61        [[bind]]
 62        keys = "C-p"
 63        command = "edit.previous-line"
 64        [[bind]]
 65        keys = "C-a"
 66        command = "edit.beginning-of-line"
 67        [[bind]]
 68        keys = "C-e"
 69        command = "edit.end-of-line"
 70        [[bind]]
 71        keys = "M-f"
 72        command = "edit.forward-word"
 73        [[bind]]
 74        keys = "M-b"
 75        command = "edit.backward-word"
 76        [[bind]]
 77        keys = "M-<"
 78        command = "edit.beginning-of-buffer"
 79        [[bind]]
 80        keys = "M->"
 81        command = "edit.end-of-buffer"
 82        [[bind]]
 83        keys = "C-v"
 84        command = "edit.scroll-up"
 85        [[bind]]
 86        keys = "M-v"
 87        command = "edit.scroll-down"
 88        [[bind]]
 89        keys = "C-l"
 90        command = "edit.recenter"
 91        [[bind]]
 92        keys = "C-d"
 93        command = "edit.delete-char"
 94        [[bind]]
 95        keys = "M-d"
 96        command = "edit.kill-word"
 97        [[bind]]
 98        keys = "M-DEL"
 99        command = "edit.backward-kill-word"
100        [[bind]]
101        keys = "C-k"
102        command = "edit.kill-line"
103        [[bind]]
104        keys = "C-y"
105        command = "edit.yank"
106        [[bind]]
107        keys = "C-SPC"
108        command = "edit.set-mark"
109        [[bind]]
110        keys = "C-x h"
111        command = "edit.select-all"
112        [[bind]]
113        keys = "C-w"
114        command = "edit.kill-region"
115        [[bind]]
116        keys = "M-w"
117        command = "edit.copy-region"
118        [[bind]]
119        keys = "C-/"
120        command = "edit.undo"
121        [[bind]]
122        keys = "C-_"
123        command = "edit.undo"
124        [[bind]]
125        keys = "C-x u"
126        command = "edit.undo"
127        [[bind]]
128        keys = "C-g"
129        command = "edit.keyboard-quit"
130        [[bind]]
131        keys = "C-t"
132        command = "edit.transpose-chars"
133        [[bind]]
134        keys = "C-s"
135        command = "edit.find"
136        [[bind]]
137        keys = "C-r"
138        command = "edit.find"
139
140        # Files and commands
141        [[bind]]
142        keys = "C-x C-s"
143        command = "app.save"
144        [[bind]]
145        keys = "C-x C-f"
146        command = "app.quick-open"
147        [[bind]]
148        keys = "C-x b"
149        command = "app.quick-open"
150        [[bind]]
151        keys = "M-x"
152        command = "app.palette"
153
154        # Org
155        [[bind]]
156        keys = "C-c C-t"
157        command = "org.todo.cycle"
158        [[bind]]
159        keys = "S-<right>"
160        command = "org.todo.cycle"
161        when = "heading"
162        [[bind]]
163        keys = "S-<up>"
164        command = "org.priority.up"
165        when = "heading"
166        [[bind]]
167        keys = "S-<down>"
168        command = "org.priority.down"
169        when = "heading"
170        [[bind]]
171        keys = "M-<left>"
172        command = "org.heading.promote"
173        when = "heading"
174        [[bind]]
175        keys = "M-<right>"
176        command = "org.heading.demote"
177        when = "heading"
178        [[bind]]
179        keys = "M-S-<left>"
180        command = "org.subtree.promote"
181        when = "heading"
182        [[bind]]
183        keys = "M-S-<right>"
184        command = "org.subtree.demote"
185        when = "heading"
186        [[bind]]
187        keys = "M-<up>"
188        command = "org.subtree.up"
189        when = "heading"
190        [[bind]]
191        keys = "M-<down>"
192        command = "org.subtree.down"
193        when = "heading"
194        [[bind]]
195        keys = "M-RET"
196        command = "org.heading.insert"
197        [[bind]]
198        keys = "C-RET"
199        command = "org.heading.insert-after-subtree"
200        [[bind]]
201        keys = "M-S-RET"
202        command = "org.heading.insert-todo"
203        [[bind]]
204        keys = "C-c C-n"
205        command = "org.heading.next"
206        [[bind]]
207        keys = "C-c C-p"
208        command = "org.heading.previous"
209        [[bind]]
210        keys = "C-c C-f"
211        command = "org.heading.forward-same-level"
212        [[bind]]
213        keys = "C-c C-b"
214        command = "org.heading.backward-same-level"
215        [[bind]]
216        keys = "C-c C-u"
217        command = "org.heading.up"
218        [[bind]]
219        keys = "C-c C-q"
220        command = "org.tags.set"
221        [[bind]]
222        keys = "M-RET"
223        command = "org.item.insert"
224        when = "item"
225        [[bind]]
226        keys = "M-S-RET"
227        command = "org.item.insert-checkbox"
228        when = "item"
229        [[bind]]
230        keys = "M-<left>"
231        command = "org.item.outdent"
232        when = "item"
233        [[bind]]
234        keys = "M-<right>"
235        command = "org.item.indent"
236        when = "item"
237        [[bind]]
238        keys = "M-S-<left>"
239        command = "org.item.outdent-tree"
240        when = "item"
241        [[bind]]
242        keys = "M-S-<right>"
243        command = "org.item.indent-tree"
244        when = "item"
245        [[bind]]
246        keys = "M-<up>"
247        command = "org.item.up"
248        when = "item"
249        [[bind]]
250        keys = "M-<down>"
251        command = "org.item.down"
252        when = "item"
253        [[bind]]
254        keys = "C-c C-c"
255        command = "org.checkbox.toggle"
256        when = "item"
257        [[bind]]
258        keys = "C-c C-x C-b"
259        command = "org.checkbox.toggle"
260        [[bind]]
261        keys = "C-c C-w"
262        command = "app.refile"
263        [[bind]]
264        keys = "C-c C-x C-s"
265        command = "app.archive"
266        [[bind]]
267        keys = "C-c $"
268        command = "app.archive"
269        [[bind]]
270        keys = "C-c ."
271        command = "org.timestamp.active"
272        [[bind]]
273        keys = "C-c !"
274        command = "org.timestamp.inactive"
275        [[bind]]
276        keys = "C-c C-s"
277        command = "org.schedule"
278        [[bind]]
279        keys = "C-c C-d"
280        command = "org.deadline"
281        [[bind]]
282        keys = "M-q"
283        command = "org.fill-paragraph"
284        [[bind]]
285        keys = "C-c C-c"
286        command = "org.table.align"
287        when = "table"
288        [[bind]]
289        keys = "M-<up>"
290        command = "org.table.row-up"
291        when = "table"
292        [[bind]]
293        keys = "M-<down>"
294        command = "org.table.row-down"
295        when = "table"
296        [[bind]]
297        keys = "M-<left>"
298        command = "org.table.column-left"
299        when = "table"
300        [[bind]]
301        keys = "M-<right>"
302        command = "org.table.column-right"
303        when = "table"
304        [[bind]]
305        keys = "M-S-<right>"
306        command = "org.table.insert-column"
307        when = "table"
308        [[bind]]
309        keys = "M-S-<left>"
310        command = "org.table.delete-column"
311        when = "table"
312        [[bind]]
313        keys = "M-S-<up>"
314        command = "org.table.kill-row"
315        when = "table"
316        [[bind]]
317        keys = "M-S-<down>"
318        command = "org.table.insert-row"
319        when = "table"
320        [[bind]]
321        keys = "C-c -"
322        command = "org.table.insert-hline"
323        when = "table"
324        [[bind]]
325        keys = "C-c |"
326        command = "org.table.create"
327        [[bind]]
328        keys = "S-<up>"
329        command = "org.timestamp.up"
330        when = "timestamp"
331        [[bind]]
332        keys = "S-<down>"
333        command = "org.timestamp.down"
334        when = "timestamp"
335        [[bind]]
336        keys = "S-<right>"
337        command = "org.timestamp.day-later"
338        when = "timestamp"
339        [[bind]]
340        keys = "S-<left>"
341        command = "org.timestamp.day-earlier"
342        when = "timestamp"
343
344        """
345
346    /// Standard Mac text keys stay as they are; org commands use Control-Command chords, which
347    /// macOS text editing leaves free.
348    static let macBindings = """
349        [[bind]]
350        keys = "C-s-t"
351        command = "org.todo.cycle"
352        [[bind]]
353        keys = "C-s-<up>"
354        command = "org.priority.up"
355        [[bind]]
356        keys = "C-s-<down>"
357        command = "org.priority.down"
358        [[bind]]
359        keys = "C-s-<left>"
360        command = "org.heading.promote"
361        [[bind]]
362        keys = "C-s-<right>"
363        command = "org.heading.demote"
364        [[bind]]
365        keys = "C-M-s-<left>"
366        command = "org.subtree.promote"
367        [[bind]]
368        keys = "C-M-s-<right>"
369        command = "org.subtree.demote"
370        [[bind]]
371        keys = "C-M-s-<up>"
372        command = "org.subtree.up"
373        [[bind]]
374        keys = "C-M-s-<down>"
375        command = "org.subtree.down"
376        [[bind]]
377        keys = "s-RET"
378        command = "org.heading.insert"
379        [[bind]]
380        keys = "C-s-RET"
381        command = "org.heading.insert-after-subtree"
382        [[bind]]
383        keys = "s-S-RET"
384        command = "org.heading.insert-todo"
385        [[bind]]
386        keys = "M-s-<down>"
387        command = "org.heading.next"
388        [[bind]]
389        keys = "M-s-<up>"
390        command = "org.heading.previous"
391        [[bind]]
392        keys = "C-s-q"
393        command = "org.tags.set"
394        [[bind]]
395        keys = "s-RET"
396        command = "org.item.insert"
397        when = "item"
398        [[bind]]
399        keys = "s-S-RET"
400        command = "org.item.insert-checkbox"
401        when = "item"
402        [[bind]]
403        keys = "C-s-<left>"
404        command = "org.item.outdent"
405        when = "item"
406        [[bind]]
407        keys = "C-s-<right>"
408        command = "org.item.indent"
409        when = "item"
410        [[bind]]
411        keys = "C-M-s-<left>"
412        command = "org.item.outdent-tree"
413        when = "item"
414        [[bind]]
415        keys = "C-M-s-<right>"
416        command = "org.item.indent-tree"
417        when = "item"
418        [[bind]]
419        keys = "C-M-s-<up>"
420        command = "org.item.up"
421        when = "item"
422        [[bind]]
423        keys = "C-M-s-<down>"
424        command = "org.item.down"
425        when = "item"
426        [[bind]]
427        keys = "C-s-c"
428        command = "org.checkbox.toggle"
429        [[bind]]
430        keys = "C-s-w"
431        command = "app.refile"
432        [[bind]]
433        keys = "C-s-a"
434        command = "app.archive"
435        [[bind]]
436        keys = "C-s-."
437        command = "org.timestamp.active"
438        [[bind]]
439        keys = "C-M-s-."
440        command = "org.timestamp.inactive"
441        [[bind]]
442        keys = "C-s-s"
443        command = "org.schedule"
444        [[bind]]
445        keys = "C-s-d"
446        command = "org.deadline"
447        [[bind]]
448        keys = "C-s-p"
449        command = "org.fill-paragraph"
450        [[bind]]
451        keys = "C-s-<up>"
452        command = "org.table.row-up"
453        when = "table"
454        [[bind]]
455        keys = "C-s-<down>"
456        command = "org.table.row-down"
457        when = "table"
458        [[bind]]
459        keys = "C-s-<left>"
460        command = "org.table.column-left"
461        when = "table"
462        [[bind]]
463        keys = "C-s-<right>"
464        command = "org.table.column-right"
465        when = "table"
466        [[bind]]
467        keys = "C-M-s-<right>"
468        command = "org.table.insert-column"
469        when = "table"
470        [[bind]]
471        keys = "C-M-s-<left>"
472        command = "org.table.delete-column"
473        when = "table"
474        [[bind]]
475        keys = "C-M-s-<up>"
476        command = "org.table.kill-row"
477        when = "table"
478        [[bind]]
479        keys = "C-M-s-<down>"
480        command = "org.table.insert-row"
481        when = "table"
482        [[bind]]
483        keys = "C-s--"
484        command = "org.table.insert-hline"
485        when = "table"
486        [[bind]]
487        keys = "C-s-\\\\"
488        command = "org.table.create"
489        [[bind]]
490        keys = "C-s-<up>"
491        command = "org.timestamp.up"
492        when = "timestamp"
493        [[bind]]
494        keys = "C-s-<down>"
495        command = "org.timestamp.down"
496        when = "timestamp"
497        [[bind]]
498        keys = "C-s-<right>"
499        command = "org.timestamp.day-later"
500        when = "timestamp"
501        [[bind]]
502        keys = "C-s-<left>"
503        command = "org.timestamp.day-earlier"
504        when = "timestamp"
505
506        """
507}