diff options
Diffstat (limited to 'src/jd/features')
| -rw-r--r-- | src/jd/features/emacs-xyz.scm | 127 | 
1 files changed, 127 insertions, 0 deletions
diff --git a/src/jd/features/emacs-xyz.scm b/src/jd/features/emacs-xyz.scm new file mode 100644 index 0000000..d79b920 --- /dev/null +++ b/src/jd/features/emacs-xyz.scm  | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | (define-module (jd features emacs-xyz) | ||
| 2 | #:use-module (rde features) | ||
| 3 | #:use-module (rde features emacs) | ||
| 4 | |||
| 5 | #:export (feature-emacs-org-uni)) | ||
| 6 | |||
| 7 | (define* (feature-emacs-org-uni | ||
| 8 | #:key | ||
| 9 | (capture-key "u") | ||
| 10 | (org-uni-task-olp (list "Tasks")) | ||
| 11 | (org-uni-class-prop-name "UNI-CLASS") | ||
| 12 | (org-uni-course-prop-name "COURSE")) | ||
| 13 | (ensure-pred string? capture-key) | ||
| 14 | (ensure-pred list? org-uni-task-olp) | ||
| 15 | (ensure-pred string? org-uni-course-prop-name) | ||
| 16 | |||
| 17 | (ensure-pred string? org-uni-class-prop-name) | ||
| 18 | |||
| 19 | (define emacs-f-name 'org-uni) | ||
| 20 | (define f-name (symbol-append 'emacs- emacs-f-name)) | ||
| 21 | |||
| 22 | (define (get-home-services config) | ||
| 23 | (list | ||
| 24 | (rde-elisp-configuration-service | ||
| 25 | emacs-f-name | ||
| 26 | config | ||
| 27 | `((defvar org-uni-class-prop-name ,org-uni-class-prop-name) | ||
| 28 | (defvar org-uni-task-olp (list ,@org-uni-task-olp) | ||
| 29 | "An outline path to task heading, that start from uni class heading.") | ||
| 30 | (defun org-uni-normalize (s) | ||
| 31 | (let ((replacements | ||
| 32 | '(("ą" . "a") ("ć" . "c") ("ę" . "e") ("ł" . "l") | ||
| 33 | ("ń" . "n") ("ó" . "o") ("ś" . "s") ("ź" . "z") ("ż" . "z") | ||
| 34 | ("Ą" . "A") ("Ć" . "C") ("Ę" . "E") ("Ł" . "L") | ||
| 35 | ("Ń" . "N") ("Ó" . "O") ("Ś" . "S") ("Ź" . "Z") ("Ż" . "Z")))) | ||
| 36 | (dolist (pair replacements s) | ||
| 37 | (setq s (replace-regexp-in-string (car pair) (cdr pair) s))))) | ||
| 38 | (defun org-uni-select-class () | ||
| 39 | (require 'dash) | ||
| 40 | (let* ((choices | ||
| 41 | (-non-nil | ||
| 42 | (org-map-entries | ||
| 43 | (lambda () | ||
| 44 | (let* ((heading (nth 4 (org-heading-components))) | ||
| 45 | (file (buffer-file-name)) | ||
| 46 | (heading-path (org-get-outline-path t)) | ||
| 47 | (heading-norm (replace-regexp-in-string | ||
| 48 | "[^a-z0-9]+" "_" | ||
| 49 | (org-uni-normalize (downcase heading))))) | ||
| 50 | (when (org-entry-get (point) org-uni-class-prop-name) | ||
| 51 | (cons (format "[%s] %s" | ||
| 52 | (file-name-nondirectory file) | ||
| 53 | heading) | ||
| 54 | (list :file file | ||
| 55 | :heading-path heading-path | ||
| 56 | :heading heading | ||
| 57 | :heading-norm heading-norm))))) | ||
| 58 | nil | ||
| 59 | 'agenda))) | ||
| 60 | (selection (completing-read "Select class: " | ||
| 61 | (mapcar 'car choices) nil t))) | ||
| 62 | (cdr (assoc selection choices)))) | ||
| 63 | (defun org-uni-goto-class () | ||
| 64 | (interactive) | ||
| 65 | (let* ((sel (org-uni-select-class)) | ||
| 66 | (file (plist-get sel :file)) | ||
| 67 | (olp (plist-get sel :heading-path))) | ||
| 68 | (find-file file) | ||
| 69 | (let ((m (org-find-olp olp t))) | ||
| 70 | (when m | ||
| 71 | (widen) | ||
| 72 | (goto-char m) | ||
| 73 | (org-narrow-to-subtree) | ||
| 74 | (org-show-subtree) | ||
| 75 | (set-marker m nil))))) | ||
| 76 | (defun org-uni-open-course-link () | ||
| 77 | (interactive) | ||
| 78 | (let* ((sel (org-uni-select-class)) | ||
| 79 | (file (plist-get sel :file)) | ||
| 80 | (olp (plist-get sel :heading-path))) | ||
| 81 | (with-temp-buffer | ||
| 82 | (insert-file-contents file) | ||
| 83 | (org-mode) | ||
| 84 | (let ((m (org-find-olp olp t))) | ||
| 85 | (when m | ||
| 86 | (goto-char m) | ||
| 87 | (let ((course-link (org-entry-get (point) ,org-uni-course-prop-name))) | ||
| 88 | (when course-link | ||
| 89 | (org-link-open-from-string course-link)))))))) | ||
| 90 | (defun org-uni-capture-task-function () | ||
| 91 | (let* ((sel (org-uni-select-class)) | ||
| 92 | (file (plist-get sel :file)) | ||
| 93 | (heading (plist-get sel :heading)) | ||
| 94 | (heading-path (plist-get sel :heading-path)) | ||
| 95 | (heading-norm (plist-get sel :heading-norm)) | ||
| 96 | (olp (append heading-path org-uni-task-olp)) | ||
| 97 | (path (org-capture-expand-file file)) | ||
| 98 | (m (org-find-olp (cons path olp)))) | ||
| 99 | (org-capture-put :uni-class-tag heading-norm) | ||
| 100 | (set-buffer (marker-buffer m)) | ||
| 101 | (org-capture-put-target-region-and-position) | ||
| 102 | (widen) | ||
| 103 | (goto-char m) | ||
| 104 | (set-marker m nil))) | ||
| 105 | (defvar org-uni-map (make-sparse-keymap) | ||
| 106 | "Keymap for org-uni commands.") | ||
| 107 | (define-key org-uni-map (kbd "u") 'org-uni-goto-class) | ||
| 108 | (define-key org-uni-map (kbd "c") 'org-uni-open-course-link) | ||
| 109 | (define-key rde-app-map (kbd "u") org-uni-map) | ||
| 110 | (with-eval-after-load | ||
| 111 | 'org-capture | ||
| 112 | (add-to-list | ||
| 113 | 'org-capture-templates | ||
| 114 | '(,capture-key | ||
| 115 | "University task" entry | ||
| 116 | (function org-uni-capture-task-function) | ||
| 117 | "* TODO %? :%(org-capture-get :uni-class-tag):\n:PROPERTIES:\n:CREATED: %U\n:END:\n")))) | ||
| 118 | #:summary "This Emacs Lisp script enhances =org-mode= with a feature for managing university tasks." | ||
| 119 | #:commentary "This Emacs Lisp script enhances =org-mode= with a feature for managing university tasks." | ||
| 120 | #:authors (list "Jakub Dlugosz <jakub@jdlugosz.com>") | ||
| 121 | #:url "https://jdlugosz.com/" | ||
| 122 | #:keywords '(convenience)))) | ||
| 123 | |||
| 124 | (feature | ||
| 125 | (name f-name) | ||
| 126 | (values '()) | ||
| 127 | (home-services-getter get-home-services))) | ||
