emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/org-remark 356c87468a 31/67: feature(nov) find-source a


From: ELPA Syncer
Subject: [elpa] externals/org-remark 356c87468a 31/67: feature(nov) find-source and link correctly (WIP)
Date: Sat, 22 Jul 2023 06:59:02 -0400 (EDT)

branch: externals/org-remark
commit 356c87468a0283b3c011162acf5cfc7019280927
Author: Noboru Ota <me@nobiot.com>
Commit: Noboru Ota <me@nobiot.com>

    feature(nov) find-source and link correctly (WIP)
---
 org-remark-global-tracking.el | 20 +++++++-----
 org-remark-nov.el             | 72 ++++++++++++++++++++++++++++++++++++++-----
 org-remark.el                 | 31 ++++++++++++-------
 3 files changed, 98 insertions(+), 25 deletions(-)

diff --git a/org-remark-global-tracking.el b/org-remark-global-tracking.el
index ba73b21aec..28b740f9a2 100644
--- a/org-remark-global-tracking.el
+++ b/org-remark-global-tracking.el
@@ -5,7 +5,7 @@
 ;; Author: Noboru Ota <me@nobiot.com>
 ;; URL: https://github.com/nobiot/org-remark
 ;; Created: 15 August 2021
-;; Last modified: 21 May 2023
+;; Last modified: 24 June 2023
 ;; Package-Requires: ((emacs "27.1") (org "9.4"))
 ;; Keywords: org-mode, annotation, note-taking, marginal-notes, wp
 
@@ -112,9 +112,9 @@ user option to use your own custom function."
           (concat (file-name-sans-extension
                    (file-name-nondirectory source-filename))
                   "-notes.org")))
-    ;; If buffer is not visiting a file, a default file name.  If this
-    ;; file name is not suitable, either override the function or set
-    ;; the user option to a custom function.
+    ;; If buffer is not visiting a file, we use the default file name.
+    ;; If this file name is not suitable, either override the function
+    ;; or set the user option to a custom function.
     (expand-file-name "marginalia.org" user-emacs-directory)))
 
 (defalias
@@ -138,11 +138,17 @@ This function is meant to be added to `find-file-hook' by
 
 (defun org-remark-notes-get-file-name ()
   "Return the name of marginal notes file for current buffer."
+  (org-remark-notes-get-file-name-for-mode))
+
+(cl-defgeneric org-remark-notes-get-file-name-for-mode ()
+  "Return the name of marginal notes file for current buffer.
+This method is major modes derived from text-mode.")
+
+(cl-defmethod org-remark-notes-get-file-name-for-mode ()
+  "Return the name of marginal notes file for current buffer.
+This method is major modes derived from text-mode."
   (if (functionp org-remark-notes-file-name)
       (funcall org-remark-notes-file-name)
-    ;; If not function, assume string and return it as the file name.
-    ;; TODO when buffer is not visitng a file, assume file resides in
-    ;; `user-emacs-directory'
     (if buffer-file-name org-remark-notes-file-name
       (expand-file-name org-remark-notes-file-name user-emacs-directory))))
 
diff --git a/org-remark-nov.el b/org-remark-nov.el
index f29d7b530e..07cc8e69e5 100644
--- a/org-remark-nov.el
+++ b/org-remark-nov.el
@@ -2,15 +2,17 @@
 
 ;; URL: https://github.com/nobiot/org-remark
 ;; Created: 9 January 2023
-;; Last modified: 21 May 2023
+;; Last modified: 24 June 2023
 
 ;;; Commentary:
 
 ;;; Code:
 
 (require 'nov)
+;;(require 'org-remark-global-tracking)
 (declare-function org-remark-highlights-load "org-remark")
-(defvar org-remark-notes-headline-functions)
+;;(defvar org-remark-notes-headline-functions)
+(require 'org-remark) ;; require to recognize 
`org-remark-notes-headline-functions'
 
 ;;;###autoload
 (define-minor-mode org-remark-nov-mode
@@ -21,8 +23,10 @@
       ;; Enable
       (progn
         (add-hook 'org-remark-source-find-file-name-functions
-                  #'org-remark-get-epub-filename)
+                  #'org-remark-get-epub-source)
         (add-hook 'org-remark-highlight-link-to-source-functions 
#'org-remark-nov-link)
+        (add-hook 'org-remark-highlight-link-to-source-functions
+                  #'org-remark-nov-link)
         ;; When users turn the page (document in nov-mode's terminology)
         ;; `nov-mode' will erase the current buffer and render the new document
         ;; content in the same buffer.  This means the highlights currently
@@ -31,18 +35,44 @@
         (add-hook 'nov-post-html-render-hook #'org-remark-highlights-load)
         (add-to-list 'org-remark-notes-headline-functions
                      '(nov-mode . ((1 . 
org-remark-nov-highlight-add-book-headline-maybe)
-                                   (2 . 
org-remark-highlight-add-source-headline-maybe)))))
+                                   (2 . 
org-remark-nov-add-source-headline-maybe)))))
     ;; Disable
     (remove-hook 'org-remark-source-find-file-name-functions
                  #'org-remark-get-epub-filename)
     (remove-hook 'org-remark-highlight-link-to-source-functions
                  #'org-remark-nov-link)
+    (remove-hook 'org-remark-highlight-link-to-source-functions
+              #'org-remark-nov-link)
     (remove-hook 'nov-post-html-render-hook #'org-remark-highlights-load)
-    (remove-hook 'org-remark-highlights-after-load-hook
-                 #'org-remark-highlights-adjust-positions)
     (setq org-remark-notes-headline-functions
           (assq-delete-all 'nov-mode org-remark-notes-headline-functions))))
 
+(cl-defmethod org-remark-notes-get-file-name-for-mode (&context (major-mode 
nov-mode))
+  "For nov.el mode, do something"
+  (let ((filename
+         (cond (;; if `org-remark-notes-file-name' is a user's custom 
function, use it as is.
+                (and (functionp org-remark-notes-file-name)
+                     (not (eq org-remark-notes-file-name 
#'org-remark-notes-file-name-function)))
+                (funcall org-remark-notes-file-name))
+               ;; if it is a default function, then do something different for 
nov.el.
+               ((functionp org-remark-notes-file-name)
+                (concat (file-name-sans-extension nov-file-name)
+                  "-notes.org"))
+               ;; the only other case is a string. Assume the default
+               ;; marginalia.org but it should be the same for custom
+               ;; string.
+               (t
+                org-remark-notes-file-name))))
+    ;; Even if `org-remark-notes-file-name' is a user-defined custom
+    ;; function, check if it is relative. If so, make it absolute by
+    ;; adding the directory path to the epub file. The purpose is to
+    ;; avoid the directory to be the temp file that nov.el creates for
+    ;; the html file to be rendered.
+    (unless (file-name-absolute-p filename)
+      ;; major mode is nov and in the document buffer.
+      (setq filename (expand-file-name filename (file-name-directory 
nov-file-name))))
+    filename))
+
 (defun org-remark-get-epub-source ()
   "Return the path of the epub source from which the present session is 
initiated."
   (when (eq major-mode 'nov-mode)
@@ -51,12 +81,21 @@
      "/"
      (file-name-base (cdr (aref nov-documents nov-documents-index))))))
 
+(defun org-remark-nov-get-epub-document-title ()
+  "Return the path of the epub source from which the present session is 
initiated."
+  (when (eq major-mode 'nov-mode)
+    (let ((document-info-pair (aref nov-documents nov-documents-index)))
+      (concat
+       (symbol-name (car document-info-pair)) ;; id
+       "/"
+       (file-name-base (cdr document-info-pair)))))) ;; temp file
+
 (defun org-remark-get-epub-filename ()
   "Return the path of the epub source from which the present session is 
initiated."
   (when (eq major-mode 'nov-mode)
     nov-file-name))
 
-(defun org-remark-nov-link (_filname)
+(defun org-remark-nov-link (_filname _point)
   "Return \"nov:\" link with current point in `nov-mode' buffer.
 
 This function only works when the mode is `nov-mode'.
@@ -97,6 +136,25 @@ Assume the current buffer is NOTES-BUF."
             (org-set-property "org-remark-nov-file" filename)
             (org-back-to-heading) (point))))))
 
+(defun org-remark-nov-add-source-headline-maybe (level source-buf notes-buf)
+  "Add a new source headline if not yet present in NOTES-BUF.
+Return the point of beginning of source headline regardless of it
+being newly added or already present.
+
+SOURCE-BUF is the source buffer that contains highlights.
+
+Assume the current buffer is NOTES-BUF."
+  (let (source-name title)
+    (with-current-buffer source-buf
+      (setq source-name (org-remark-get-epub-source))
+      (setq title (org-remark-nov-get-epub-document-title)))
+    (with-current-buffer notes-buf
+      ;; Return the beginning point of the headline. Create if not present
+      (or (org-find-property
+           org-remark-prop-source-file source-name)
+          (org-remark-new-headline
+           level title (list org-remark-prop-source-file source-name))))))
+
 ;; navigate from notes to document
 (defun test/find-nov-file-buffer ()
   (interactive)
diff --git a/org-remark.el b/org-remark.el
index 4ba88cbe17..70f662af49 100644
--- a/org-remark.el
+++ b/org-remark.el
@@ -6,7 +6,7 @@
 ;; URL: https://github.com/nobiot/org-remark
 ;; Version: 1.1.0
 ;; Created: 22 December 2020
-;; Last modified: 29 May 2023
+;; Last modified: 24 June 2023
 ;; Package-Requires: ((emacs "27.1") (org "9.4"))
 ;; Keywords: org-mode, annotation, note-taking, marginal-notes, wp,
 
@@ -271,7 +271,9 @@ recommended to turn it on as part of Emacs initialization.
       (org-remark-highlights-load)
       (add-hook 'after-save-hook #'org-remark-save nil t)
       (add-hook 'org-remark-highlights-after-load-hook
-          #'org-remark-highlights-adjust-positions))
+                #'org-remark-highlights-adjust-positions)
+      (add-hook 'org-remark-highlight-link-to-source-functions
+                #'org-remark-highlight-link-to-source-default 80))
      (t
       ;; Deactivate
       (when org-remark-highlights
@@ -280,7 +282,9 @@ recommended to turn it on as part of Emacs initialization.
       (setq org-remark-highlights nil)
       (remove-hook 'after-save-hook #'org-remark-save t)
       (remove-hook 'org-remark-highlights-after-load-hook
-                   #'org-remark-highlights-adjust-positions))))
+                   #'org-remark-highlights-adjust-positions)
+      (remove-hook 'org-remark-highlight-link-to-source-functions
+                   #'org-remark-highlight-link-to-source-default))))
 
 
 ;;;; Org-remark Menu
@@ -928,7 +932,7 @@ 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 line-num link orgid)
+  (let (title beg end props id text filename link orgid)
     (with-current-buffer source-buf
       (setq title (org-remark-highlight-get-title)
             beg (overlay-start highlight)
@@ -941,12 +945,8 @@ beginning of source-headline, which should be one level 
up."
                    (buffer-substring-no-properties beg end)))
             filename (org-remark-source-get-file-name
                       (org-remark-source-find-file-name))
-            line-num (org-current-line beg)
-            link (if buffer-file-name
-                     (concat "[[file:" filename
-                             (when line-num (format "::%d" line-num)) "]]")
-                   (run-hook-with-args-until-success
-                    'org-remark-highlight-link-to-source-functions filename))
+            link (run-hook-with-args-until-success
+                  'org-remark-highlight-link-to-source-functions filename beg)
             orgid (org-remark-highlight-get-org-id beg))
       ;; TODO ugly to add the beg end after setq above
       (plist-put props org-remark-prop-source-beg (number-to-string beg))
@@ -1055,6 +1055,13 @@ Adjustment is done by TEXT, which should be the original 
text of the highlight."
        (when (re-search-forward text paragraph-end :noerror)
          (move-overlay highlight (match-beginning 0) (match-end 0)))))))
 
+(defun org-remark-highlight-link-to-source-default (filename point)
+  "Return Org link string for the source when adding a highlight.
+Default function for `org-remark-highlight-link-to-source-functions'."
+  (if buffer-file-name
+      (let ((line-num (org-current-line point)))
+        (concat "[[file:" filename
+                (when line-num (format "::%d" line-num)) "]]"))))
 
 ;;;;; org-remark-notes
 ;;    Private functions that work on marginal notes buffer (notes
@@ -1310,7 +1317,9 @@ load the highlights"
   ;; Loop highlights and add them to the current buffer
   (let (overlays) ;; highlight overlays
     (when-let* ((notes-filename (org-remark-notes-get-file-name))
-                (notes-buf (find-file-noselect notes-filename))
+                (default-dir default-directory)
+                (notes-buf (or (find-buffer-visiting notes-filename)
+                               (find-file-noselect notes-filename)))
                 (source-buf (current-buffer)))
       (dolist (highlight (org-remark-highlights-get notes-buf) overlays)
         (let* ((location (plist-get highlight :location))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]