mydenotefuncs.el 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ;"Generate an aligned, fontified buffer of all Denote files using fd."
  2. (defvar denote-browser-map
  3. (let ((map (make-sparse-keymap)))
  4. (define-key map (kbd "RET") 'denote-browser-open-file)
  5. map)
  6. "Keymap for the custom Denote browser.")
  7. (defface denote-browser-dir '((t :inherit font-lock-comment-face)) "Face for subdirs.")
  8. (defface denote-browser-id '((t :inherit font-lock-constant-face)) "Face for IDs.")
  9. (defface denote-browser-title '((t :inherit font-lock-keyword-face :weight bold)) "Face for titles.")
  10. (defface denote-browser-tags '((t :inherit font-lock-type-face)) "Face for tags.")
  11. (defun denote-browser-open-file ()
  12. "Open the file on the current line."
  13. (interactive)
  14. (if (get-text-property (point) 'denote-path)
  15. (find-file (get-text-property (point) 'denote-path))
  16. (message "No file on this line.")))
  17. (defun list-denote-files ()
  18. "Generate an aligned, fontified buffer of all Denote files using fd."
  19. (interactive)
  20. (let* ((default-directory (expand-file-name "~/lyrinv/SOR/Notes/"))
  21. (raw-output (shell-command-to-string "fd -t f"))
  22. (lines (split-string raw-output "\n" t))
  23. (buf (get-buffer-create "*Denote Browser*")))
  24. (with-current-buffer buf
  25. (let ((inhibit-read-only t))
  26. (erase-buffer)
  27. (use-local-map denote-browser-map)
  28. (setq-local buffer-read-only t)
  29. (dolist (line lines)
  30. ;; Regex to break down: Subdir/ + ID + Title + Keywords
  31. (if (string-match "^\\(.*\\/\\)?\\([0-9A-Za-z]+\\)--\\([^\n__]+\\)\\(__.*\\)?\\.\\([a-z]+\\)$" line)
  32. (let* ((dir (or (match-string 1 line) ""))
  33. (id (or (match-string 2 line) ""))
  34. (title (or (match-string 3 line) ""))
  35. (tags (or (match-string 4 line) ""))
  36. (full-path (expand-file-name line default-directory))
  37. (start (point)))
  38. ;; Clean up trailing slash and tag underscores for legibility
  39. (setq dir (if (string-empty-p dir) "." (directory-file-name dir)))
  40. (setq tags (if (string-empty-p tags) "" (replace-regexp-in-string "__" " " tags)))
  41. ;; Insert padded, aligned string components
  42. (insert (propertize (format "%-20s " dir) 'face 'denote-browser-dir))
  43. (insert (propertize (format "%-16s " id) 'face 'denote-browser-id))
  44. (insert (propertize (format "%-45s " title) 'face 'denote-browser-title))
  45. (insert (propertize tags 'face 'denote-browser-tags))
  46. (insert "\n")
  47. ;; Attach the raw file path to the entire line for RET to use
  48. (add-text-properties start (point) (list 'denote-path full-path)))
  49. ;; Fallback layout for non-standard files (e.g., gereedschap.ods)
  50. (let* ((dir (or (file-name-directory line) "."))
  51. (file (file-name-nondirectory line))
  52. (full-path (expand-file-name line default-directory))
  53. (start (point)))
  54. (insert (propertize (format "%-20s " (directory-file-name dir)) 'face 'denote-browser-dir))
  55. (insert (propertize (format "%-16s " "No-ID") 'face 'denote-browser-id))
  56. (insert (propertize file 'face 'denote-browser-title))
  57. (insert "\n")
  58. (add-text-properties start (point) (list 'denote-path full-path)))))))
  59. (pop-to-buffer buf)))