config.el 17 KB

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