branch: externals/org-remark commit 13f36d72419cc0b645c7177dc2e61ffd70397f32 Author: Noboru Ota <m...@nobiot.com> Commit: Noboru Ota <m...@nobiot.com>
internal: Fix the macro expansion All the reader-macro syntax to construct lists has been replaced with normal syntax. As far as I can see, `seq-find` seems to be the only major macro that relies on functions within this package. The backtick syntax to construct a list has been replaced with `list` and `cons` functions. So no need for the "reader macro" like `(,id ,beg ,end)`. --- org-marginalia.el | 219 +++++++++++++++++++++++++++--------------------------- 1 file changed, 109 insertions(+), 110 deletions(-) diff --git a/org-marginalia.el b/org-marginalia.el index 40ef66a4d9..9fa0b3a0bf 100644 --- a/org-marginalia.el +++ b/org-marginalia.el @@ -5,7 +5,7 @@ ;; Author: Noboru Ota <m...@nobiot.com> ;; URL: https://github.com/nobiot/org-marginalia ;; Version: 0.0.2 -;; Last modified: 2020-12-23T093128 +;; Last modified: 2020-12-23T105737 ;; Package-Requires: ((emacs "27.1") (org "9.4")) ;; Keywords: org-mode, annotation, writing, note-taking, margin-notes @@ -165,8 +165,8 @@ directory. Ensure that it is an Org file." "Keep track of all the highlights. It is a local variable. On save-buffer, kill-buffer, or kill-emacs, the marker will be persisted in the filesystem. -Each highlight is also a list of this type: - (id (beg-marker . end-marker)) +Each highlight is also an alist of this type: + (id beg-marker . end-marker) The text inside the markers can change, too.") (defvar om/last-notes-buffer nil @@ -179,103 +179,6 @@ It is meant to exist only one of these in each Emacs session.") (defconst om/prop-source-beg "marginalia-source-beg") (defconst om/prop-source-end "marginalia-source-end") -;;;; Functions - -;;;;; Private -;; `om/make-highlight-marker' and other private utility functions; however, -;; macro expansion (?) in `om/mark' and others do not seem to understand that -;; they are declared in this file. `declare-function' didn't seem to work. -;; Until I figure out how to work with it, I will put this function here. - -(defun om/save-single-highlight (highlight title source-path) - "Save a single HIGHLIGHT in the marginalia file with properties. -The marginalia file is specified by SOURCE-PATH. If headline with -the same ID already exists, update it based on the new highlight -position and highlighted text as TITLE. If it is a new highlight, -creat a new headline at the end of the buffer." - (let* ((pos (cdr highlight)) - (beg (marker-position (car pos))) - (end (marker-position (cdr pos))) - (text (buffer-substring-no-properties beg end))) - ;; TODO Want to add a check if save is applicable here. - (with-current-buffer (find-file-noselect om/notes-file-path) - (org-with-wide-buffer - (let ((file-headline (org-find-property om/prop-source-file source-path)) - (id-headline (org-find-property om/prop-id (car highlight)))) - (unless file-headline - ;; If file-headline does not exist, create one at the bottom - (goto-char (point-max)) - ;; Ensure to be in the beginning of line to add a new headline - (when (eolp) (open-line 1) (forward-line 1) (beginning-of-line)) - (insert (concat "* " title "\n")) - (org-set-property om/prop-source-file source-path)) - (cond (id-headline - (goto-char id-headline) - ;; Update the existing headline and position properties - (org-edit-headline text) - (org-set-property om/prop-source-beg (number-to-string beg)) - (org-set-property om/prop-source-end (number-to-string end))) - (t ;; No headline with the ID property. Create one - (when-let ((p (org-find-property om/prop-source-file source-path))) - (goto-char p)) - (org-narrow-to-subtree) - (goto-char (point-max)) - ;; Ensure to be in the beginning of line to add a new headline - (when (eolp) (open-line 1) (forward-line 1) (beginning-of-line)) - ;; Create a headline - ;; Add a properties - (insert (concat "** " text "\n")) - (org-set-property om/prop-id (car highlight)) - (org-set-property om/prop-source-beg (number-to-string beg)) - (org-set-property om/prop-source-end (number-to-string end)) - (insert (concat "[[file:" source-path "]" "[" title "]]")))))) - (when (buffer-modified-p) (save-buffer))))) - -(defun om/make-highlight-marker (point) - "Return marker of the insertion-type t for POINT. -The insertion-type is important in order for the highlight -position (beg and end points) in sync with the highlighted text -properties." - (let ((marker (set-marker (make-marker) point))) - (set-marker-insertion-type marker t) - marker)) - -(defun om/list-highlights-positions (&optional reverse) - "Return list of beg points of highlights in this buffer. -By default, the list is in ascending order. -If none, return nil. -If REVERSE is non-nil, return list in the descending order." - (when om/highlights - (let ((list (mapcar (lambda (h) - (marker-position (car (cdr h)))) - om/highlights))) - (if reverse (reverse list) list)))) - -(defun om/sort-highlights-list () - "Utility function to sort `om/sort-highlights'." - (when om/highlights - (setq om/highlights (seq-sort-by (lambda (s) (car (cdr s))) #'< om/highlights)))) - -(defun om/find-next-highlight () - "Return the beg point of the next highlight. -Look through `om/highlights' list." - - (when-let ((points (om/list-highlights-positions))) - ;; Find the first occurance of p > (point). If none, this means all the - ;; points occur before the current point. Take the first one. Assume - ;; `om/highlights' is sorted in the ascending order (it is). - (seq-find (lambda (p) (> p (point))) points (nth 0 points)))) - -(defun om/find-prev-highlight () - "Return the beg point of the previous highlight. -Look through `om/highlights' list (in descending order)." - - (when-let ((points (om/list-highlights-positions 'reverse))) - ;; Find the first occurance of p < (point). If none, this means all the - ;; points occur before the current point. Take the first one. Assume - ;; `om/highlights' is sorted in the descending order . - (seq-find (lambda (p) (< p (point))) points (nth 0 points)))) - ;;;; Commands ;;;###autoload @@ -331,14 +234,14 @@ and `om/prev'." (when (not id) (setq id (substring (org-id-uuid) 0 8))) ;; Add highlight to the text (add-text-properties beg end '(font-lock-face om/highlighter)) - (add-text-properties beg end `(om/id ,id)) + (add-text-properties beg end (list 'om/id id)) ;; Keep track in a local variable It's alist; don't forget the dot ;; (beg . end) ;; The dot "." is imporant to make the car/cdr "getter" interface clean. ;; Also, `set-marker-insertion-type' to set the type t is necessary to move ;; the cursor in sync with the font-lock-face property of the text property. - (push `(,id - ,(om/make-highlight-marker beg) . ,(om/make-highlight-marker end)) + (push (cons id + (cons (om/make-highlight-marker beg) (om/make-highlight-marker end))) om/highlights) (om/sort-highlights-list)) @@ -421,7 +324,7 @@ Load is automatically done when you activate the minor mode." (when-let ((id (car (org--property-local-values "marginalia-id" nil))) (beg (string-to-number (car (org--property-local-values "marginalia-source-beg" nil)))) (end (string-to-number (car (org--property-local-values "marginalia-source-end" nil))))) - (push `(,id ,beg . ,end) highlights))))))) + (push (cons id (cons beg end)) highlights))))))) ;; Back to the current buffer ;; Look highilights and add highlights to the current buffer (dolist (highlight highlights) @@ -452,12 +355,6 @@ marginalia, but will keep the headline and notes." (org-delete-property om/prop-source-beg) (org-delete-property om/prop-source-end)))))) -;; WIP -(defun om/toggle-display () - "WIP: Toggle showing/hiding of highlights in current bufer.") - -(seq-find (lambda (p) (> p (point))) (om/list-highlights-positions) (nth 0 (om/list-highlights-positions) )) - (defun om/next () "Look at the current point, and move to the next highlight, if any. If there is none below the point, but there is a highlight in the @@ -473,6 +370,108 @@ buffer, go back to the last one." (interactive) (if (not om/highlights) (message "No highlights present in this buffer.") (goto-char (om/find-prev-highlight)))) + +;; WIP +(defun om/toggle-display () + "WIP: Toggle showing/hiding of highlights in current bufer.") + +;;;; Functions + +;;;;; Private +;; `om/make-highlight-marker' and other private utility functions; however, +;; macro expansion (?) in `om/mark' and others do not seem to understand that +;; they are declared in this file. `declare-function' didn't seem to work. +;; Until I figure out how to work with it, I will put this function here. + +(defun om/save-single-highlight (highlight title source-path) + "Save a single HIGHLIGHT in the marginalia file with properties. +The marginalia file is specified by SOURCE-PATH. If headline with +the same ID already exists, update it based on the new highlight +position and highlighted text as TITLE. If it is a new highlight, +creat a new headline at the end of the buffer." + (let* ((pos (cdr highlight)) + (beg (marker-position (car pos))) + (end (marker-position (cdr pos))) + (text (buffer-substring-no-properties beg end))) + ;; TODO Want to add a check if save is applicable here. + (with-current-buffer (find-file-noselect om/notes-file-path) + (org-with-wide-buffer + (let ((file-headline (org-find-property om/prop-source-file source-path)) + (id-headline (org-find-property om/prop-id (car highlight)))) + (unless file-headline + ;; If file-headline does not exist, create one at the bottom + (goto-char (point-max)) + ;; Ensure to be in the beginning of line to add a new headline + (when (eolp) (open-line 1) (forward-line 1) (beginning-of-line)) + (insert (concat "* " title "\n")) + (org-set-property om/prop-source-file source-path)) + (cond (id-headline + (goto-char id-headline) + ;; Update the existing headline and position properties + (org-edit-headline text) + (org-set-property om/prop-source-beg (number-to-string beg)) + (org-set-property om/prop-source-end (number-to-string end))) + (t ;; No headline with the ID property. Create one + (when-let ((p (org-find-property om/prop-source-file source-path))) + (goto-char p)) + (org-narrow-to-subtree) + (goto-char (point-max)) + ;; Ensure to be in the beginning of line to add a new headline + (when (eolp) (open-line 1) (forward-line 1) (beginning-of-line)) + ;; Create a headline + ;; Add a properties + (insert (concat "** " text "\n")) + (org-set-property om/prop-id (car highlight)) + (org-set-property om/prop-source-beg (number-to-string beg)) + (org-set-property om/prop-source-end (number-to-string end)) + (insert (concat "[[file:" source-path "]" "[" title "]]")))))) + (when (buffer-modified-p) (save-buffer))))) + +(defun om/make-highlight-marker (point) + "Return marker of the insertion-type t for POINT. +The insertion-type is important in order for the highlight +position (beg and end points) in sync with the highlighted text +properties." + (let ((marker (set-marker (make-marker) point))) + (set-marker-insertion-type marker t) + marker)) + +(defun om/list-highlights-positions (&optional reverse) + "Return list of beg points of highlights in this buffer. +By default, the list is in ascending order. +If none, return nil. +If REVERSE is non-nil, return list in the descending order." + (when om/highlights + (let ((list (mapcar (lambda (h) + (marker-position (car (cdr h)))) + om/highlights))) + (if reverse (reverse list) list)))) + +(defun om/sort-highlights-list () + "Utility function to sort `om/sort-highlights'." + (when om/highlights + (setq om/highlights (seq-sort-by (lambda (s) (car (cdr s))) #'< om/highlights)))) + +(defun om/find-next-highlight () + "Return the beg point of the next highlight. +Look through `om/highlights' list." + + (when-let ((points (om/list-highlights-positions))) + ;; Find the first occurance of p > (point). If none, this means all the + ;; points occur before the current point. Take the first one. Assume + ;; `om/highlights' is sorted in the ascending order (it is). + (seq-find (lambda (p) (> p (point))) points (nth 0 points)))) + +(defun om/find-prev-highlight () + "Return the beg point of the previous highlight. +Look through `om/highlights' list (in descending order)." + + (when-let ((points (om/list-highlights-positions 'reverse))) + ;; Find the first occurance of p < (point). If none, this means all the + ;; points occur before the current point. Take the first one. Assume + ;; `om/highlights' is sorted in the descending order . + (seq-find (lambda (p) (< p (point))) points (nth 0 points)))) + ;;;; Footer (provide 'org-marginalia)