config.el 17 KB

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