org-noter-nov-overlay.el 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ;;; org-noter-nov-overlay.el --- Module to highlight text in nov-mode with notes -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021 Charlie Gordon
  3. ;; Author: Charlie Gordon <char1iegordon@protonmail.com>
  4. ;; Keywords: multimedia
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License,
  8. ;; any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Highlight your precise notes in nov with org-noter-nov-overlay.el
  17. ;;; Code:
  18. (require 'org-noter)
  19. (require 'nov)
  20. (require 'seq)
  21. (defcustom org-noter-nov-overlay-color-property "NOTER_OVERLAY"
  22. "A property that specifies the overlay color for `org-noter-nov-make-ov'.")
  23. (defcustom org-noter-nov-overlay-default-color "SkyBlue"
  24. "Name of the default background color of the overlay `org-noter-nov-make-ov' makes.
  25. Should be one of the element in `defined-colors'.")
  26. (defun org-noter-nov-make-overlays ()
  27. (org-noter--with-selected-notes-window
  28. (let* ((page (buffer-local-value 'nov-documents-index (org-noter--session-doc-buffer session)))
  29. (regexp (org-re-property org-noter-property-note-location t nil
  30. (format (rx "(" (* space) "%d" (+ space)
  31. (+ digit) (+ space) "." (+ space)
  32. (+ digit) (* space) ")")
  33. page))))
  34. (org-with-wide-buffer
  35. (goto-char (point-min))
  36. (while (re-search-forward regexp nil t)
  37. (when-let ((location (org-entry-get nil org-noter-property-note-location nil t)))
  38. (org-noter-nov-make-overlay-no-question)))))))
  39. (defun org-noter-nov-make-overlay ()
  40. "TODO"
  41. (org-noter--with-selected-notes-window
  42. "No notes window exists"
  43. (when (eq (org-noter--session-doc-mode session) 'nov-mode)
  44. (let* ((location-property (org-entry-get nil org-noter-property-note-location nil t))
  45. (location-cons (cdr (read location-property)))
  46. (beg (car location-cons))
  47. (end (cdr location-cons))
  48. (ov-pair (list (make-overlay beg end (org-noter--session-doc-buffer session))))
  49. (hl-color (or (org-entry-get nil org-noter-nov-overlay-color-property nil t)
  50. (if org-noter-insert-note-no-questions
  51. org-noter-nov-overlay-default-color
  52. (read-color "Highlight color: "))))
  53. (hl-color-alt (color-lighten-name hl-color 15))
  54. (action-functions (list
  55. #'org-noter-nov-overlay-sync-current-note
  56. #'org-noter-nov-overlay-sync-current-page-or-chapter)))
  57. (save-excursion
  58. (org-back-to-heading t)
  59. (re-search-forward org-heading-regexp nil t)
  60. (push (make-overlay (match-beginning 1) (match-end 1)) ov-pair))
  61. (dolist (ov ov-pair)
  62. (overlay-put ov 'button ov)
  63. (overlay-put ov 'category 'default-button)
  64. (overlay-put ov 'face (list :background hl-color
  65. :foreground (readable-foreground-color hl-color)))
  66. (org-entry-put nil org-noter-nov-overlay-color-property hl-color)
  67. (overlay-put ov 'mouse-face (list :background hl-color-alt
  68. :foreground (readable-foreground-color hl-color-alt)))
  69. (overlay-put ov 'action (pop action-functions)))))))
  70. (defun org-noter-nov-make-overlay-no-question ()
  71. "Like `org-noter-nov-make-ov', but doesn't ask user to select the overlay color."
  72. (org-noter--with-valid-session
  73. (let ((org-noter-insert-note-no-questions t))
  74. (org-noter-nov-make-overlay))))
  75. (defun org-noter-nov-overlay-sync-current-page-or-chapter (_overlay)
  76. "A wrapper function for `org-noter-sync-current-page-or-chapter'
  77. used exclusively with overlays made with `org-noter-nov-make-overlay'
  78. This wrapper ignores the first argument passed to it and just call
  79. `org-noter-sync-current-page-or-chapter'."
  80. (org-noter-sync-current-page-or-chapter))
  81. (defun org-noter-nov-overlay-sync-current-note (_overlay)
  82. "A wrapper function for `org-noter-nov-overlay-sync-current-note'
  83. used exclusively with overlays made with `org-noter-nov-make-overlay'
  84. This wrapper ignores the first argument passed to it and just call
  85. `org-noter-nov-overlay-sync-current-note'."
  86. (org-noter-sync-current-note))
  87. (add-hook 'nov-post-html-render-hook #'org-noter-nov-make-overlays)
  88. (provide 'org-noter-nov-overlay)
  89. ;;; org-noter-nov-ov.el ends here