config.el 16 KB

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