config.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. ;;; $DOOMDIR/config.el -*- lexical-binding: t; -*-
  2. (setq user-full-name "Colin Powell"
  3. user-mail-address "colin@unbl.ink")
  4. (nyan-mode)
  5. ;; load pinentry
  6. (when (require 'pinentry nil t)
  7. (pinentry-start))
  8. (require 'server)
  9. (unless (server-running-p)
  10. (server-start))
  11. (setq doom-theme 'doom-xcode
  12. doom-font (font-spec :family "Iosevka" :size 14 :weight 'regular)
  13. doom-big-font (font-spec :family "Iosevka" :size 18 :weight 'regular)
  14. doom-variable-pitch-font (font-spec :family "Overpass" :size 12))
  15. (setq display-line-numbers-type t)
  16. ;; change `org-directory'. It must be set before org loads!
  17. (setq org-directory "~/var/org/")
  18. (load! "+agenda-fix")
  19. (defun vulpea-agenda-files-update (&rest _)
  20. (setq org-agenda-files vulpea-project-files))
  21. (setq org-roam-directory "~/var/org/"
  22. org-roam-dailies-directory "dailies")
  23. (advice-add 'org-agenda :before #'vulpea-agenda-files-update)
  24. (advice-add 'org-todo-list :before #'vulpea-agenda-files-update)
  25. (load! "+django-tests")
  26. (map! :after python
  27. :map python-mode-map
  28. :localleader
  29. (:prefix ("t" . "tests")
  30. :desc "django test at point" "d" #'django-run-test-at-point
  31. :desc "django tests for file" "f" #'django-run-tests-for-current-file
  32. :desc "django all tests" "a" (cmd! (django-run-tests "" nil))))
  33. (setq +format-on-save-disabled-modes (add-to-list '+format-on-save-disabled-modes 'typescript-mode))
  34. (map! ;; Easier window movement
  35. :n "C-h" 'evil-window-left
  36. :n "C-j" 'evil-window-down
  37. :n "C-k" 'evil-window-up
  38. :n "C-l" 'evil-window-right
  39. (:map evil-treemacs-state-map
  40. "C-h" 'evil-window-left
  41. "C-l" 'evil-window-right)
  42. :leader
  43. (:prefix "f"
  44. :desc "Find file in dotfiles" "t" #'+hlissner/find-in-dotfiles
  45. :desc "Browse dotfiles" "T" #'+hlissner/browse-dotfiles)
  46. (:prefix "b"
  47. :desc "Black format buffer" "f" #'blacken-buffer
  48. :desc "isort buffer" "I" #'py-isort-buffer
  49. :desc "Links in buffer" "l" #'ace-link-org))
  50. (defun unfill-paragraph ()
  51. "Takes a multi-line paragraph and makes it into a single line of text."
  52. (interactive)
  53. (let ((fill-column (point-max)))
  54. (fill-paragraph nil)))
  55. (define-key global-map "\M-z" 'unfill-paragraph)
  56. (defun file-notify-rm-all-watches ()
  57. "Remove all existing file notification watches from Emacs."
  58. (interactive)
  59. (maphash
  60. (lambda (key _value)
  61. (file-notify-rm-watch key))
  62. file-notify-descriptors))
  63. (setq frame-title-format
  64. '(""
  65. (:eval
  66. (if (s-contains-p org-roam-directory (or buffer-file-name ""))
  67. (replace-regexp-in-string
  68. ".*/[0-9]*-?" "☰ "
  69. (subst-char-in-string ?_ ? buffer-file-name))
  70. "%b"))
  71. (:eval
  72. (let ((project-name (projectile-project-name)))
  73. (unless (string= "-" project-name)
  74. (format (if (buffer-modified-p) " ◉ %s" "  ●  %s") project-name))))))
  75. (setq mm-text-html-renderer 'w3m)
  76. (setq w3m-fill-column 88)
  77. (setq lsp-lens-enable 1
  78. lsp-ui-sideline-enable 1
  79. lsp-enable-links 1
  80. lsp-headerline-breadcrumb-enable 1
  81. lsp-modeline-code-actions-enable 1
  82. lsp-modeline-diagnostics-enable 1
  83. lsp-completion-show-detail 1
  84. lsp-file-watch-threshold nil)
  85. ;; check for hosts folder and find any init-HOSTNAME.el files in there and load them
  86. (defvar host (substring (shell-command-to-string "hostname") 0 -1))
  87. (defvar host-dir "~/.config/doom/hosts/")
  88. (add-load-path! host-dir)
  89. ;; Setup nov.el mode for epubs and change font
  90. (add-to-list 'auto-mode-alist '("\\.epub\\'" . nov-mode))
  91. (defun my-nov-font-setup ()
  92. (face-remap-add-relative 'variable-pitch :family "Overpass"
  93. :height 1.0))
  94. (add-hook 'nov-mode-hook 'my-nov-font-setup)
  95. ;;(let ((init-host-feature (intern (concat "init-" host ".el"))))
  96. ;; (load-file init-host-feature))
  97. (defvar host-init (concat "~/.config/doom/hosts/init-" host ".el"))
  98. (if (file-exists-p host-init)
  99. (load-file host-init))
  100. (load-file "~/.config/doom/+agenda-fix.el")
  101. ;; Enable org-modern mode per buffer
  102. ;(add-hook 'org-mode-hook #'org-modern-mode)
  103. ;(add-hook 'org-agenda-finalize-hook #'org-modern-agenda)
  104. ;; Or globally
  105. (with-eval-after-load 'org (global-org-modern-mode))
  106. (require 'cl-lib)
  107. (defun eshell-load-bash-aliases ()
  108. "Read Bash aliases and add them to the list of eshell aliases."
  109. ;; Bash needs to be run - temporarily - interactively
  110. ;; in order to get the list of aliases.
  111. (with-temp-buffer
  112. (call-process "bash" nil '(t nil) nil "-ci" "alias")
  113. (goto-char (point-min))
  114. (cl-letf (((symbol-function 'eshell-write-aliases-list) #'ignore))
  115. (while (re-search-forward "alias \\(.+\\)='\\(.+\\)'$" nil t)
  116. (eshell/alias (match-string 1) (match-string 2))))
  117. (eshell-write-aliases-list)))
  118. ;; We only want Bash aliases to be loaded when Eshell loads its own aliases,
  119. ;; rather than every time `eshell-mode' is enabled.
  120. (add-hook 'eshell-alias-load-hook 'eshell-load-bash-aliases)
  121. (defun eshell-run-direnv-allow()
  122. (direnv-allow))
  123. (add-hook 'eshell-directory-change-hook 'eshell-run-direnv-allow)
  124. (defun org-raw-timestamp-to-iso (raw-ts)
  125. "Convert Org RAW-TS like `<2025-06-12 Thu 14:00>` to `YYYY-MM-DDThh:mm:ss`."
  126. (when raw-ts
  127. (let* ((ts (org-parse-time-string raw-ts))
  128. (year (nth 5 ts)) (mon (nth 4 ts)) (day (nth 3 ts))
  129. (hour (nth 2 ts) 0) (min (nth 1 ts) 0))
  130. (format "%04d-%02d-%02dT%02d:%02d:00" year mon day hour min))))
  131. (defun org-extract-labeled-timestamps ()
  132. "Return an alist of labeled ISO-formatted timestamps in the current Org subtree."
  133. (save-restriction
  134. (org-narrow-to-subtree)
  135. (let ((parsed (org-element-parse-buffer))
  136. (labeled-ts '()))
  137. (org-element-map parsed '(timestamp)
  138. (lambda (ts)
  139. (let* ((type (org-element-property :type ts))
  140. (raw (org-element-property :raw-value ts))
  141. (time (org-parse-time-string raw t))
  142. (date (format "%04d-%02d-%02d"
  143. (nth 5 time) (nth 4 time) (nth 3 time)))
  144. (hour (nth 2 time))
  145. (min (nth 1 time))
  146. (with-time (and hour min (format "%sT%02d:%02d" date hour min)))
  147. (label (cond
  148. ((eq type 'active) "timestamp")
  149. ((eq type 'inactive) "inactive-timestamp")
  150. (t "timestamp"))))
  151. (push (cons label (or with-time date)) labeled-ts))))
  152. ;; Add planning info from heading (DEADLINE, SCHEDULED, CLOSED)
  153. (dolist (key '("DEADLINE" "SCHEDULED" "CLOSED"))
  154. (let ((raw (org-entry-get nil key t)))
  155. (when raw
  156. (let* ((ts (org-parse-time-string raw t))
  157. (date (format "%04d-%02d-%02d" (nth 5 ts) (nth 4 ts) (nth 3 ts)))
  158. (hour (nth 2 ts))
  159. (min (nth 1 ts))
  160. (with-time (and hour min (format "%sT%02d:%02d" date hour min))))
  161. (push (cons (downcase key) (or with-time date)) labeled-ts)))))
  162. (delete-dups labeled-ts))))
  163. (defun org-get-body ()
  164. "Return the body text under the current Org heading as a string."
  165. (save-excursion
  166. (org-back-to-heading t)
  167. (let ((start (progn (forward-line) (point)))
  168. (end (progn (org-end-of-subtree t t) (point))))
  169. (buffer-substring-no-properties start end))))
  170. (defun org-strip-timestamps-from-text (text)
  171. "Remove Org timestamps and planning lines from TEXT."
  172. (let* ((timestamp-re (rx (or (seq "<" (+ (not (any ">"))) ">")
  173. (seq "[" (+ (not (any "]"))) "]"))))
  174. (planning-line-re (rx line-start (zero-or-more space)
  175. (or "DEADLINE:" "SCHEDULED:" "CLOSED:") " "))
  176. ;; Step 1: remove full planning lines
  177. (without-planning-lines
  178. (replace-regexp-in-string
  179. (concat planning-line-re ".*\n?") "" text))
  180. ;; Step 2: remove inline timestamps
  181. (without-inline
  182. (replace-regexp-in-string timestamp-re "" without-planning-lines)))
  183. (string-trim without-inline)))
  184. (defun org-extract-drawers ()
  185. "Extract all drawers (like LOGBOOK, PROPERTIES, etc.) from current org entry.
  186. Returns an alist of (DRAWER-NAME . CONTENT) pairs.
  187. - PROPERTIES content is parsed into (KEY . VALUE)
  188. - Other drawers are returned as lists of lines (strings)"
  189. (save-excursion
  190. (org-back-to-heading t)
  191. (let ((end (save-excursion (org-end-of-subtree t t)))
  192. (drawers '()))
  193. (while (re-search-forward "^\\s-*:\\([A-Z]+\\):\\s-*$" end t)
  194. (let* ((name (match-string 1))
  195. (start (match-end 0))
  196. (drawer-end (when (re-search-forward "^\\s-*:END:\\s-*$" end t)
  197. (match-beginning 0))))
  198. (when drawer-end
  199. (let ((content (buffer-substring-no-properties start drawer-end)))
  200. (setq drawers
  201. (cons
  202. (cons name
  203. (if (string= name "PROPERTIES")
  204. ;; parse :KEY: VALUE
  205. (org-parse-properties content)
  206. ;; just return line list
  207. (split-string content "\n" t "[ \t]+")))
  208. drawers))))))
  209. (reverse drawers))))
  210. (defun org-parse-properties (content)
  211. "Parse PROPERTIES drawer content into an alist."
  212. (let ((lines (split-string content "\n" t))
  213. (props '()))
  214. (dolist (line lines)
  215. (when (string-match "^\\s-*:\\([^:]+\\):\\s-*\\(.*\\)$" line)
  216. (push (cons (match-string 1 line) (match-string 2 line)) props)))
  217. (reverse props)))
  218. (defun org-clean-body-text (text)
  219. "Remove planning lines, timestamps, and drawers from TEXT."
  220. (let* ((timestamp-re
  221. (rx (or (seq "<" (+ (not (any ">"))) ">")
  222. (seq "[" (+ (not (any "]"))) "]"))))
  223. (planning-re
  224. (rx line-start (zero-or-more space)
  225. (or "DEADLINE:" "SCHEDULED:" "CLOSED:") " " (* nonl) "\n"))
  226. (text (replace-regexp-in-string planning-re "" text))
  227. (text (replace-regexp-in-string timestamp-re "" text))
  228. (text (org-strip-all-drawers text)))
  229. (string-trim text)))
  230. (defun org-strip-timestamps-drawers-notes-from-text (text)
  231. "Strip timestamps, planning lines, drawers, and note blocks from Org TEXT."
  232. (let* ((timestamp-re
  233. (rx (or (seq "<" (+ (not (any ">"))) ">")
  234. (seq "[" (+ (not (any "]"))) "]"))))
  235. (planning-re
  236. (rx line-start (zero-or-more space)
  237. (or "DEADLINE:" "SCHEDULED:" "CLOSED:") " " (* nonl) "\n"))
  238. (drawer-re
  239. "^\\s-*:[A-Z]+:\\(?:.\\|\n\\)*?:END:\n?")
  240. (note-block-re
  241. (rx-to-string
  242. `(and bol (* space) "- Note taken on "
  243. (or "[" "<") (+ (not (any "]>"))) (or "]" ">")
  244. (*? anything)
  245. (or "\n\n" eos))
  246. t)))
  247. ;; Strip drawers first
  248. (setq text (replace-regexp-in-string drawer-re "" text))
  249. ;; Strip entire note blocks (greedy match up to next blank line or end)
  250. (setq text (replace-regexp-in-string note-block-re "" text))
  251. ;; Strip planning lines and timestamps
  252. (setq text (replace-regexp-in-string planning-re "" text))
  253. (setq text (replace-regexp-in-string timestamp-re "" text))
  254. ;; Trim and return
  255. (string-trim text)))
  256. (defun org-get-body-stripped ()
  257. "Get cleaned Org entry body without timestamps, planning lines, drawers, or notes."
  258. (org-strip-timestamps-drawers-notes-from-text (org-get-body)))
  259. (defun org-extract-notes ()
  260. "Extract notes from Org entry, each as an alist with `timestamp` and `content`."
  261. (save-excursion
  262. (org-back-to-heading t)
  263. (let ((start (progn (forward-line) (point)))
  264. (end (progn (org-end-of-subtree t t) (point)))
  265. result) ;; ✅ initialize result list
  266. (save-restriction
  267. (narrow-to-region start end)
  268. (goto-char (point-min))
  269. (while (re-search-forward "^\\s-*[-+] Note taken on \\[\\([^]]+\\)\\]\\s-*\\(?:\\\\\\\\\\)?\\s-*$" nil t)
  270. (let* ((raw-ts (match-string 1))
  271. (timestamp (let* ((ts (org-parse-time-string raw-ts t)))
  272. (format "%04d-%02d-%02dT%02d:%02d"
  273. (nth 5 ts) (nth 4 ts) (nth 3 ts)
  274. (nth 2 ts) (nth 1 ts))))
  275. (note-start (progn
  276. (forward-line)
  277. ;; allow one optional blank line
  278. (when (looking-at-p "^\\s-*$") (forward-line))
  279. (point)))
  280. (note-end (or (save-excursion
  281. (re-search-forward "^\\s-*[-+] Note taken on \\[" nil t))
  282. (point-max)))
  283. (content (string-trim
  284. (buffer-substring-no-properties note-start (1- note-end)))))
  285. (push `(("timestamp" . ,timestamp)
  286. ("content" . ,content))
  287. result))))
  288. (nreverse result))))
  289. (require 'subr-x) ;; for string-trim and string functions, usually loaded by default
  290. (defun my-org-generate-uuid ()
  291. "Generate a random UUID string."
  292. (let ((uuid (md5 (format "%s%s%s%s%s"
  293. (user-uid)
  294. (emacs-pid)
  295. (float-time)
  296. (random)
  297. (emacs-pid)))))
  298. (concat (substring uuid 0 8) "-"
  299. (substring uuid 8 12) "-"
  300. (substring uuid 12 16) "-"
  301. (substring uuid 16 20) "-"
  302. (substring uuid 20 32))))
  303. (defun my-org-get-or-create-id ()
  304. "Get the ID property of the current Org heading, or create and set one if missing.
  305. Returns the ID string."
  306. (let ((id (org-entry-get nil "ID")))
  307. (unless id
  308. (setq id (my-org-generate-uuid))
  309. (org-entry-put nil "ID" id)
  310. (save-buffer)) ;; optional: save file after inserting ID
  311. id))
  312. (defun org-clock-on-state-change ()
  313. "Clock in when state is STRT, clock out otherwise."
  314. (when (and (derived-mode-p 'org-mode)
  315. (not (org-before-first-heading-p)))
  316. (pcase org-state
  317. ("STRT"
  318. (unless org-clock-marker
  319. (org-clock-in)))
  320. ((or "DONE" "CANC" "WAIT" "HOLD" "TODO")
  321. (when org-clock-marker
  322. (org-clock-out))))))
  323. (defun send-org-todo-to-endpoint-on-state-change ()
  324. "Send the current Org-mode TODO item to an HTTP endpoint."
  325. (interactive)
  326. (when (org-at-heading-p)
  327. (let ((state (org-get-todo-state)))
  328. (when (member state '("STRT" "DONE"))
  329. (let* ((heading (org-get-heading t t t t))
  330. (current-time (format-time-string "%Y-%m-%dT%H:%M:%SZ" (current-time) t)) ;; UTC ISO8601
  331. (tags (org-get-tags))
  332. (timestamps (org-extract-labeled-timestamps))
  333. (drawers (org-extract-drawers))
  334. (properties (cdr (assoc "PROPERTIES" drawers)))
  335. (todo-id (my-org-get-or-create-id))
  336. (body (org-get-body-stripped))
  337. (notes (org-extract-notes))
  338. (properties (org-entry-properties))
  339. (endpoint "https://life.lab.unbl.ink/webhook/emacs/")
  340. (data `(("description" . ,heading)
  341. ("labels" . ,tags)
  342. ("state" . ,state)
  343. ("timestamps" . ,timestamps)
  344. ("notes" . ,notes)
  345. ("drawers" . ,drawers)
  346. ("emacs_id" . ,todo-id)
  347. ("updated_at" . ,current-time)
  348. ("source" . "orgmode")
  349. ("properties" . ,properties)
  350. ("body" . ,body))))
  351. (request
  352. endpoint
  353. :type "POST"
  354. :headers '(("Content-Type" . "application/json"))
  355. :data (json-encode data)
  356. :headers '(("Authorization" . "Token 58e898c0e88bd6333b1a9e8de82e81f36c4b64e")
  357. ("Content-Type" . "application/json"))
  358. :parser 'json-read
  359. :success (cl-function
  360. (lambda (&key data &allow-other-keys)
  361. (message "Sent TODO: %s" data)))
  362. :error (cl-function
  363. (lambda (&rest args &key error-thrown &allow-other-keys)
  364. (message "Error sending TODO: %S" error-thrown))))))
  365. (org-clock-on-state-change)
  366. )))
  367. (add-hook 'org-after-todo-state-change-hook #'send-org-todo-to-endpoint-on-state-change)
  368. (defun life-scrobble-url ()
  369. "Open https://life.lab.unbl.ink/ with scrobble_url set to a URL.
  370. - If in an `eww` buffer, use its current URL.
  371. - Otherwise, use the clipboard/kill ring.
  372. Always open the result in `eww`."
  373. (interactive)
  374. (let* ((url (cond
  375. ;; In eww, grab current page URL
  376. ((derived-mode-p 'eww-mode)
  377. (plist-get eww-data :url))
  378. ;; Else clipboard
  379. (t (current-kill 0 t))))
  380. (encoded (url-hexify-string url))
  381. (scrobble-url (concat "https://life.lab.unbl.ink/?scrobble_url=" encoded)))
  382. (eww scrobble-url)))
  383. ;; Bind globally to C-c l
  384. (global-set-key (kbd "C-c l") #'life-scrobble-url)
  385. (after! magit
  386. (defvar my/ssh-key-injector-script
  387. (expand-file-name "~/.bin/load_keys"))
  388. (defun my/ssh-agent-has-keys-p (&rest _ignore)
  389. "Non-nil if ssh-agent currently has at least one identity loaded."
  390. (eq 0 (call-process "ssh-add" nil nil nil "-l")))
  391. (defun my/ensure-ssh-keys-loaded (&rest _ignore)
  392. "Ensure ssh-agent has keys loaded; if not, run injector script."
  393. (unless (my/ssh-agent-has-keys-p)
  394. (unless (file-executable-p my/ssh-key-injector-script)
  395. (user-error "SSH injector script not executable: %s" my/ssh-key-injector-script))
  396. (let ((buf (get-buffer-create "*ssh-key-injector*")))
  397. (with-current-buffer buf (erase-buffer))
  398. (let ((exit (call-process-shell-command my/ssh-key-injector-script nil buf t)))
  399. (unless (eq exit 0)
  400. (display-buffer buf)
  401. (user-error "SSH key injection failed (see *ssh-key-injector*)"))))))
  402. ;; IMPORTANT: remove then re-add, so we don't keep an old advised function object around
  403. (advice-remove 'magit-status #'my/ensure-ssh-keys-loaded)
  404. (advice-add 'magit-status :before #'my/ensure-ssh-keys-loaded)
  405. ;; optional:
  406. (advice-remove 'magit-fetch #'my/ensure-ssh-keys-loaded)
  407. (advice-add 'magit-fetch :before #'my/ensure-ssh-keys-loaded)
  408. (advice-remove 'magit-push #'my/ensure-ssh-keys-loaded)
  409. (advice-add 'magit-push :before #'my/ensure-ssh-keys-loaded)
  410. (advice-remove 'magit-pull #'my/ensure-ssh-keys-loaded)
  411. (advice-add 'magit-pull :before #'my/ensure-ssh-keys-loaded))