Sources/OrgCore/Keymap/Presets.swift

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

543 lines · 14228 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        [[bind]]
154        keys = "C-c a"
155        command = "app.agenda"
156        [[bind]]
157        keys = "C-c C-e h h"
158        command = "app.export.html"
159        [[bind]]
160        keys = "C-c C-e h o"
161        command = "app.export.html-open"
162        [[bind]]
163        keys = "C-c C-e m m"
164        command = "app.export.markdown"
165        [[bind]]
166        keys = "C-c C-e l p"
167        command = "app.export.pdf"
168        [[bind]]
169        keys = "C-c C-e l l"
170        command = "app.export.latex"
171        [[bind]]
172        keys = "C-c C-e o o"
173        command = "app.export.odt"
174        [[bind]]
175        keys = "C-c C-e t u"
176        command = "app.export.text"
177
178        # Org
179        [[bind]]
180        keys = "C-c C-t"
181        command = "org.todo.cycle"
182        [[bind]]
183        keys = "S-<right>"
184        command = "org.todo.cycle"
185        when = "heading"
186        [[bind]]
187        keys = "S-<up>"
188        command = "org.priority.up"
189        when = "heading"
190        [[bind]]
191        keys = "S-<down>"
192        command = "org.priority.down"
193        when = "heading"
194        [[bind]]
195        keys = "M-<left>"
196        command = "org.heading.promote"
197        when = "heading"
198        [[bind]]
199        keys = "M-<right>"
200        command = "org.heading.demote"
201        when = "heading"
202        [[bind]]
203        keys = "M-S-<left>"
204        command = "org.subtree.promote"
205        when = "heading"
206        [[bind]]
207        keys = "M-S-<right>"
208        command = "org.subtree.demote"
209        when = "heading"
210        [[bind]]
211        keys = "M-<up>"
212        command = "org.subtree.up"
213        when = "heading"
214        [[bind]]
215        keys = "M-<down>"
216        command = "org.subtree.down"
217        when = "heading"
218        [[bind]]
219        keys = "M-RET"
220        command = "org.heading.insert"
221        [[bind]]
222        keys = "C-RET"
223        command = "org.heading.insert-after-subtree"
224        [[bind]]
225        keys = "M-S-RET"
226        command = "org.heading.insert-todo"
227        [[bind]]
228        keys = "C-c C-n"
229        command = "org.heading.next"
230        [[bind]]
231        keys = "C-c C-p"
232        command = "org.heading.previous"
233        [[bind]]
234        keys = "C-c C-f"
235        command = "org.heading.forward-same-level"
236        [[bind]]
237        keys = "C-c C-b"
238        command = "org.heading.backward-same-level"
239        [[bind]]
240        keys = "C-c C-u"
241        command = "org.heading.up"
242        [[bind]]
243        keys = "C-c C-q"
244        command = "org.tags.set"
245        [[bind]]
246        keys = "M-RET"
247        command = "org.item.insert"
248        when = "item"
249        [[bind]]
250        keys = "M-S-RET"
251        command = "org.item.insert-checkbox"
252        when = "item"
253        [[bind]]
254        keys = "M-<left>"
255        command = "org.item.outdent"
256        when = "item"
257        [[bind]]
258        keys = "M-<right>"
259        command = "org.item.indent"
260        when = "item"
261        [[bind]]
262        keys = "M-S-<left>"
263        command = "org.item.outdent-tree"
264        when = "item"
265        [[bind]]
266        keys = "M-S-<right>"
267        command = "org.item.indent-tree"
268        when = "item"
269        [[bind]]
270        keys = "M-<up>"
271        command = "org.item.up"
272        when = "item"
273        [[bind]]
274        keys = "M-<down>"
275        command = "org.item.down"
276        when = "item"
277        [[bind]]
278        keys = "C-c C-c"
279        command = "org.checkbox.toggle"
280        when = "item"
281        [[bind]]
282        keys = "C-c C-x C-b"
283        command = "org.checkbox.toggle"
284        [[bind]]
285        keys = "C-c C-w"
286        command = "app.refile"
287        [[bind]]
288        keys = "C-c C-x C-s"
289        command = "app.archive"
290        [[bind]]
291        keys = "C-c $"
292        command = "app.archive"
293        [[bind]]
294        keys = "C-c ."
295        command = "org.timestamp.active"
296        [[bind]]
297        keys = "C-c !"
298        command = "org.timestamp.inactive"
299        [[bind]]
300        keys = "C-c C-s"
301        command = "org.schedule"
302        [[bind]]
303        keys = "C-c C-d"
304        command = "org.deadline"
305        [[bind]]
306        keys = "M-q"
307        command = "org.fill-paragraph"
308        [[bind]]
309        keys = "C-c C-c"
310        command = "org.table.align"
311        when = "table"
312        [[bind]]
313        keys = "C-c C-c"
314        command = "org.table.recalc-all"
315        when = "tblfm"
316        [[bind]]
317        keys = "C-c *"
318        command = "org.table.recalc"
319        when = "table"
320        [[bind]]
321        keys = "C-c C-c"
322        command = "org.babel.execute"
323        when = "src"
324        [[bind]]
325        keys = "M-<up>"
326        command = "org.table.row-up"
327        when = "table"
328        [[bind]]
329        keys = "M-<down>"
330        command = "org.table.row-down"
331        when = "table"
332        [[bind]]
333        keys = "M-<left>"
334        command = "org.table.column-left"
335        when = "table"
336        [[bind]]
337        keys = "M-<right>"
338        command = "org.table.column-right"
339        when = "table"
340        [[bind]]
341        keys = "M-S-<right>"
342        command = "org.table.insert-column"
343        when = "table"
344        [[bind]]
345        keys = "M-S-<left>"
346        command = "org.table.delete-column"
347        when = "table"
348        [[bind]]
349        keys = "M-S-<up>"
350        command = "org.table.kill-row"
351        when = "table"
352        [[bind]]
353        keys = "M-S-<down>"
354        command = "org.table.insert-row"
355        when = "table"
356        [[bind]]
357        keys = "C-c -"
358        command = "org.table.insert-hline"
359        when = "table"
360        [[bind]]
361        keys = "C-c |"
362        command = "org.table.create"
363        [[bind]]
364        keys = "S-<up>"
365        command = "org.timestamp.up"
366        when = "timestamp"
367        [[bind]]
368        keys = "S-<down>"
369        command = "org.timestamp.down"
370        when = "timestamp"
371        [[bind]]
372        keys = "S-<right>"
373        command = "org.timestamp.day-later"
374        when = "timestamp"
375        [[bind]]
376        keys = "S-<left>"
377        command = "org.timestamp.day-earlier"
378        when = "timestamp"
379
380        """
381
382    /// Standard Mac text keys stay as they are; org commands use Control-Command chords, which
383    /// macOS text editing leaves free.
384    static let macBindings = """
385        [[bind]]
386        keys = "C-s-t"
387        command = "org.todo.cycle"
388        [[bind]]
389        keys = "C-s-<up>"
390        command = "org.priority.up"
391        [[bind]]
392        keys = "C-s-<down>"
393        command = "org.priority.down"
394        [[bind]]
395        keys = "C-s-<left>"
396        command = "org.heading.promote"
397        [[bind]]
398        keys = "C-s-<right>"
399        command = "org.heading.demote"
400        [[bind]]
401        keys = "C-M-s-<left>"
402        command = "org.subtree.promote"
403        [[bind]]
404        keys = "C-M-s-<right>"
405        command = "org.subtree.demote"
406        [[bind]]
407        keys = "C-M-s-<up>"
408        command = "org.subtree.up"
409        [[bind]]
410        keys = "C-M-s-<down>"
411        command = "org.subtree.down"
412        [[bind]]
413        keys = "s-RET"
414        command = "org.heading.insert"
415        [[bind]]
416        keys = "C-s-RET"
417        command = "org.heading.insert-after-subtree"
418        [[bind]]
419        keys = "s-S-RET"
420        command = "org.heading.insert-todo"
421        [[bind]]
422        keys = "M-s-<down>"
423        command = "org.heading.next"
424        [[bind]]
425        keys = "M-s-<up>"
426        command = "org.heading.previous"
427        [[bind]]
428        keys = "C-s-q"
429        command = "org.tags.set"
430        [[bind]]
431        keys = "s-RET"
432        command = "org.item.insert"
433        when = "item"
434        [[bind]]
435        keys = "s-S-RET"
436        command = "org.item.insert-checkbox"
437        when = "item"
438        [[bind]]
439        keys = "C-s-<left>"
440        command = "org.item.outdent"
441        when = "item"
442        [[bind]]
443        keys = "C-s-<right>"
444        command = "org.item.indent"
445        when = "item"
446        [[bind]]
447        keys = "C-M-s-<left>"
448        command = "org.item.outdent-tree"
449        when = "item"
450        [[bind]]
451        keys = "C-M-s-<right>"
452        command = "org.item.indent-tree"
453        when = "item"
454        [[bind]]
455        keys = "C-M-s-<up>"
456        command = "org.item.up"
457        when = "item"
458        [[bind]]
459        keys = "C-M-s-<down>"
460        command = "org.item.down"
461        when = "item"
462        [[bind]]
463        keys = "C-s-c"
464        command = "org.checkbox.toggle"
465        [[bind]]
466        keys = "C-s-w"
467        command = "app.refile"
468        [[bind]]
469        keys = "C-s-a"
470        command = "app.archive"
471        [[bind]]
472        keys = "C-s-."
473        command = "org.timestamp.active"
474        [[bind]]
475        keys = "C-M-s-."
476        command = "org.timestamp.inactive"
477        [[bind]]
478        keys = "C-s-s"
479        command = "org.schedule"
480        [[bind]]
481        keys = "C-s-d"
482        command = "org.deadline"
483        [[bind]]
484        keys = "C-s-p"
485        command = "org.fill-paragraph"
486        [[bind]]
487        keys = "C-s-<up>"
488        command = "org.table.row-up"
489        when = "table"
490        [[bind]]
491        keys = "C-s-<down>"
492        command = "org.table.row-down"
493        when = "table"
494        [[bind]]
495        keys = "C-s-<left>"
496        command = "org.table.column-left"
497        when = "table"
498        [[bind]]
499        keys = "C-s-<right>"
500        command = "org.table.column-right"
501        when = "table"
502        [[bind]]
503        keys = "C-M-s-<right>"
504        command = "org.table.insert-column"
505        when = "table"
506        [[bind]]
507        keys = "C-M-s-<left>"
508        command = "org.table.delete-column"
509        when = "table"
510        [[bind]]
511        keys = "C-M-s-<up>"
512        command = "org.table.kill-row"
513        when = "table"
514        [[bind]]
515        keys = "C-M-s-<down>"
516        command = "org.table.insert-row"
517        when = "table"
518        [[bind]]
519        keys = "C-s--"
520        command = "org.table.insert-hline"
521        when = "table"
522        [[bind]]
523        keys = "C-s-\\\\"
524        command = "org.table.create"
525        [[bind]]
526        keys = "C-s-<up>"
527        command = "org.timestamp.up"
528        when = "timestamp"
529        [[bind]]
530        keys = "C-s-<down>"
531        command = "org.timestamp.down"
532        when = "timestamp"
533        [[bind]]
534        keys = "C-s-<right>"
535        command = "org.timestamp.day-later"
536        when = "timestamp"
537        [[bind]]
538        keys = "C-s-<left>"
539        command = "org.timestamp.day-earlier"
540        when = "timestamp"
541
542        """
543}