branch: externals/org-remark commit 6e3254d231f63d9cca8c0a86d5f47233f98e27eb Author: Noboru Ota <m...@nobiot.com> Commit: Noboru Ota <m...@nobiot.com>
refactor: highlight headlne text --- org-remark-line-highlighter.el | 117 +++++++++++++---------------------------- org-remark.el | 19 ++++--- 2 files changed, 48 insertions(+), 88 deletions(-) diff --git a/org-remark-line-highlighter.el b/org-remark-line-highlighter.el index 1b2fe949ea..25520d04ae 100644 --- a/org-remark-line-highlighter.el +++ b/org-remark-line-highlighter.el @@ -30,6 +30,17 @@ ;;(require 'org-remark) +(defface org-remark-line-highlighter + '((((class color) (min-colors 88) (background light)) + :foreground "#e2d980") + (((class color) (min-colors 88) (background dark)) + :foreground "#e2d980") + (t + :inherit highlight)) + "Face for the default line highlighter pen.") + +(defvar org-remark-line-icon "*") + (defun org-remark-line-pos-bol (pos) "Return the beginning of the line position for POS." (save-excursion @@ -57,28 +68,6 @@ by `overlays-in'." #'(lambda (&rest args) (set-window-margins nil 2))) -(defun test/overlay-put-line-highlight (ov) - (let* ((left-margin (or (car (window-margins)) - ;; when nil = no margin, set to 1 - (progn (set-window-margins nil 2) - 2))) - (spaces (- left-margin 2)) - (string (with-temp-buffer (insert-char ?\s spaces) - (insert "•") - (buffer-string)))) - (overlay-put ov 'before-string (propertize "! " 'display - `((margin left-margin) - ,(propertize string 'face 'modus-themes-diff-refine-removed)))) - ;;(overlay-put ov 'category "org-remark-line") ;; need to fix property add logic - ov)) - -(defun org-remark-line-highlight-modified (ov after-p beg end &optional length) - "This is good! Move the overlay to follow the point when ENTER in the line." - (when after-p - (save-excursion (goto-char beg) - (when (looking-at "\n") - (move-overlay ov (1+ beg) (1+ beg)))))) - ;;;###autoload (defun org-remark-mark-line (beg end &optional id mode) (interactive (org-remark-beg-end 'line)) @@ -90,72 +79,38 @@ by `overlays-in'." ;; to call this correct function (list 'org-remark-type 'line))) -;; (add-hook 'org-remark-beg-end-dwim-functions #'org-remark-line-beg-end) -;; This is not a good solution. The normal highlight gets zero length with this. -;; It needs to be "type" differentiated by a defmethod, etc. - -;; (remove-hook 'org-remark-beg-end-dwim-functions #'org-remark-line-beg-end) - -;; (defun org-remark-line-beg-end () -;; "Return a list of beg and end both being the bol." -;; (let ((bol (org-remark-line-pos-bol (point)))) -;; (list bol bol))) - (cl-defmethod org-remark-beg-end ((org-remark-type (eql 'line))) (let ((bol (org-remark-line-pos-bol (point)))) (list bol bol))) (cl-defmethod org-remark-highlight-mark-overlay (ov face (org-remark-type (eql 'line))) - (test/overlay-put-line-highlight ov) ;; LINE + (org-remark-line-highlight-overlay-put ov face) ;; LINE (overlay-put ov 'insert-in-front-hooks (list 'org-remark-line-highlight-modified))) -;; (defun org-remark-line-highlight-mark -;; (beg end &optional id mode label face properties) -;; "Apply the FACE to the whole line that contains BEG." -;; ;; Ensure to turn on the local minor mode -;; (unless org-remark-mode (org-remark-mode +1)) -;; ;; When highlights are toggled hidden, only the new one gets highlighted in -;; ;; the wrong toggle state. -;; (when org-remark-highlights-hidden (org-remark-highlights-show)) -;; (let ((ov (make-overlay beg end nil :front-advance)) -;; ;; UUID is too long; does not have to be the full length -;; (id (if id id (substring (org-id-uuid) 0 8))) -;; (filename (org-remark-source-find-file-name))) -;; (if (not filename) -;; (message (format "org-remark: Highlights not saved.\ -;; This buffer (%s) is not supported" (symbol-name major-mode))) -;; (org-with-wide-buffer -;; (org-remark-highlight-mark-overlay ov face 'line) -;; (while properties -;; (let ((prop (pop properties)) -;; (val (pop properties))) -;; (overlay-put ov prop val))) -;; (when label (overlay-put ov 'org-remark-label label)) -;; (overlay-put ov 'org-remark-id id) -;; ;; Keep track of the overlay in a local variable. It's a list that is -;; ;; guaranteed to contain only org-remark overlays as opposed to the one -;; ;; returned by `overlay-lists' that lists all overlays. -;; (push ov org-remark-highlights) -;; ;; for mode, nil and :change result in saving the highlight. :load -;; ;; bypasses save. -;; (unless (eq mode :load) -;; (let* ((notes-buf (find-file-noselect -;; (org-remark-notes-get-file-name))) -;; (source-buf (current-buffer)) -;; ;; Get props for create and change modes -;; (notes-props -;; (org-remark-highlight-add ov source-buf notes-buf))) -;; (when notes-props -;; (org-remark-highlight-put-props ov notes-props)) -;; ;; Save the notes buffer when not loading -;; (unless (eq notes-buf (current-buffer)) -;; (with-current-buffer notes-buf (save-buffer)))))) -;; (deactivate-mark) -;; (org-remark-highlights-housekeep) -;; (org-remark-highlights-sort) -;; (setq org-remark-source-setup-done t) -;; ;; Return overlay -;; ov))) +(defun org-remark-line-highlight-overlay-put (ov face) + (let* ((face (or face 'org-remark-line-highlighter)) + (left-margin (or (car (window-margins)) + ;; when nil = no margin, set to 1 + (progn (set-window-margins nil 2) + 2))) + (spaces (- left-margin 2)) + (string (with-temp-buffer (insert-char ?\s spaces) + (insert org-remark-line-icon) + (buffer-string)))) + (overlay-put ov 'before-string (propertize "! " 'display + `((margin left-margin) + ,(propertize string 'face face)))) + ov)) + +(defun org-remark-line-highlight-modified (ov after-p beg end &optional length) + "This is good! Move the overlay to follow the point when ENTER in the line." + (when after-p + (save-excursion (goto-char beg) + (when (looking-at "\n") + (move-overlay ov (1+ beg) (1+ beg)))))) + +(cl-defmethod org-remark-highlight-headline-text (ov (org-remark-type (eql 'line))) + "Line highlight") (provide 'org-remark-line) ;;; org-remark-line.el ends here diff --git a/org-remark.el b/org-remark.el index f144104b22..e3dc0051e5 100644 --- a/org-remark.el +++ b/org-remark.el @@ -619,7 +619,7 @@ If you have done so by error, you could still `undo' it in the marginal notes buffer, but not in the current buffer as adding and removing overlays are not part of the undo tree." (interactive "d\nP") - (when-let ((ov (org-remark-find-overlay-at-point point)) + (when-let ((ov (org-remark-find-dwim point)) (id (overlay-get ov 'org-remark-id))) ;; Remove the highlight overlay and id Where there is more than one, ;; remove only one. It should be last-in-first-out in general but @@ -987,6 +987,14 @@ buffer for automatic sync." ;;; Return notes-props notes-props)) +(cl-defgeneric org-remark-highlight-headline-text ((org-remark-type))) + +(cl-defmethod org-remark-highlight-headline-text (ov (org-remark-type (eql nil))) + "Assume it is called within `org-with-wide-buffer' of the source." + (replace-regexp-in-string + "\n" " " + (buffer-substring-no-properties (overlay-start ov) (overlay-end ov)))) + (defun org-remark-highlight-add-or-update-highlight-headline (highlight source-buf notes-buf) "Add a new HIGHLIGHT headlne to the NOTES-BUF or update it. Return notes-props as a property list. @@ -996,19 +1004,16 @@ HIGHLIGHT is an overlay from the SOURCE-BUF. Assume the current buffer is NOTES-BUF and point is placed on the beginning of source-headline, which should be one level up." ;; Add org-remark-link with updated line-num as a property - (let (title beg end props id text filename link orgid) + (let (title beg end props id text filename link orgid org-remark-type) (with-current-buffer source-buf (setq title (org-remark-highlight-get-title) beg (overlay-start highlight) end (overlay-end highlight) props (overlay-properties highlight) id (plist-get props 'org-remark-id) + org-remark-type (overlay-get highlight 'org-remark-type) text (org-with-wide-buffer - (if (string= "line" (overlay-get highlight 'org-remark-type)) - "Line highlight" ;; for now - (replace-regexp-in-string - "\n" " " - (buffer-substring-no-properties beg end)))) + (org-remark-highlight-headline-text highlight org-remark-type)) filename (org-remark-source-get-file-name (org-remark-source-find-file-name)) link (run-hook-with-args-until-success