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

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

[elpa] externals/denote d3b35f6f2f 2/4: Merge pull request #66 from jean


From: ELPA Syncer
Subject: [elpa] externals/denote d3b35f6f2f 2/4: Merge pull request #66 from jeanphilippegg/reorganization
Date: Sat, 30 Jul 2022 00:57:37 -0400 (EDT)

branch: externals/denote
commit d3b35f6f2f3ab1ad9e3cd5ea0341b1243adf00b4
Merge: e82a2fbb11 b284da5293
Author: Protesilaos Stavrou <info@protesilaos.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #66 from jeanphilippegg/reorganization
    
    Move functions from denote-dired.el and create some utility functions
---
 denote-dired.el    | 118 ++++++--------------------------------------------
 denote-retrieve.el |   4 --
 denote.el          | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 139 insertions(+), 108 deletions(-)

diff --git a/denote-dired.el b/denote-dired.el
index 8e0c08ce90..c77e184b08 100644
--- a/denote-dired.el
+++ b/denote-dired.el
@@ -200,26 +200,6 @@ old name followed by the new one.  This applies to the 
command
 
 ;;;; File helper functions
 
-(defun denote-dired--file-attributes-time (file)
-  "Return `file-attribute-modification-time' of FILE as identifier."
-  (format-time-string
-   denote--id-format
-   (file-attribute-modification-time (file-attributes file))))
-
-(defun denote-dired--file-name-id (file)
-  "Return FILE identifier, else generate one."
-  (cond
-   ((string-match denote--id-regexp file)
-    (substring file (match-beginning 0) (match-end 0)))
-   ((denote-dired--file-attributes-time file))
-   (t (format-time-string denote--id-format))))
-
-(defun denote-dired--rename-buffer (old-name new-name)
-  "Rename OLD-NAME buffer to NEW-NAME, when appropriate."
-  (when-let ((buffer (find-buffer-visiting old-name)))
-    (with-current-buffer buffer
-      (set-visited-file-name new-name nil t))))
-
 (defun denote-dired--rename-dired-file-or-prompt ()
   "Return Dired file at point, else prompt for one.
 
@@ -247,44 +227,9 @@ Return t if the file is renamed, nil otherwise."
                     (propertize (file-name-nondirectory new-name) 'face 
'success)))))
       (when response
         (rename-file old-name new-name nil)
-        (denote-dired--rename-buffer old-name new-name))
+        (denote--rename-buffer old-name new-name))
       response)))
 
-(defun denote-dired-update-dired-buffers ()
-  "Update Dired buffers of variable `denote-directory'."
-  (mapc
-   (lambda (buf)
-     (with-current-buffer buf
-       (when (and (eq major-mode 'dired-mode)
-                  (string-prefix-p (denote-directory)
-                                   (expand-file-name default-directory)))
-         (revert-buffer))))
-   (buffer-list)))
-
-(defun denote-dired--filetype-heuristics (file)
-  "Return likely file type of FILE.
-The return value is for `denote--file-meta-header'."
-  (pcase (file-name-extension file)
-    ("md" (if-let ((title-key (denote-retrieve--value-title file t))
-                   ((string-match-p "title\\s-*=" title-key)))
-              'markdown-toml
-            'markdown-yaml))
-    ("txt" 'text)
-    (_ 'org)))
-
-(defun denote-dired--edit-front-matter-p (file)
-  "Test if FILE should be subject to front matter rewrite.
-This is relevant for `denote-dired--rewrite-front-matter': if FILE
-has no front matter, then we abort early instead of trying to
-replace what isn't there."
-  (when-let ((ext (file-name-extension file)))
-    (and (file-regular-p file)
-         (file-writable-p file)
-         (not (denote--file-empty-p file))
-         (string-match-p "\\(md\\|org\\|txt\\)\\'" ext)
-         ;; Heuristic to check if this is one of our notes
-         (string= (expand-file-name default-directory) (denote-directory)))))
-
 ;; FIXME 2022-07-25: We should make the underlying regular expressions
 ;; that `denote-retrieve--value-title' targets more refined, so that we
 ;; capture eveyrhing at once.
@@ -293,12 +238,12 @@ replace what isn't there."
 The FILE, TITLE, and KEYWORDS are passed from the renaming
 command and are used to construct new front matter values if
 appropriate."
-  (when-let ((denote-dired--edit-front-matter-p file)
+  (when-let ((denote--edit-front-matter-p file)
              (old-title (denote-retrieve--value-title file))
              (old-keywords (denote-retrieve--value-keywords file))
              (new-title title)
              (new-keywords (denote--file-meta-keywords
-                            keywords (denote-dired--filetype-heuristics 
file))))
+                            keywords (denote--filetype-heuristics file))))
       (with-current-buffer (find-file-noselect file)
         (when (y-or-n-p (format
                          "Replace front matter?\n-%s\n+%s\n\n-%s\n+%s?"
@@ -318,41 +263,6 @@ appropriate."
               (search-forward old-keywords nil t 1)
               (replace-match (concat "\\1" new-keywords) t)))))))
 
-(defun denote-dired--rewrite-keywords-no-question (file keywords)
-  "Rewrite KEYWORDS in FILE outright.
-
-Do the same as `denote-dired--rewrite-front-matter' for keywords,
-but do not for confirmation.
-
-This is for use in `denote-dired-rename-marked-files' or related.
-Those commands ask for confirmation once before performing an
-operation on multiple files."
-  (when-let ((denote-dired--edit-front-matter-p file)
-             (old-keywords (denote-retrieve--value-keywords file))
-             (new-keywords (denote--file-meta-keywords
-                            keywords (denote-dired--filetype-heuristics 
file))))
-    (with-current-buffer (find-file-noselect file)
-      (save-excursion
-        (save-restriction
-          (widen)
-          (goto-char (point-min))
-          (re-search-forward denote-retrieve--keywords-front-matter-key-regexp 
nil t 1)
-          (search-forward old-keywords nil t 1)
-          (replace-match (concat "\\1" new-keywords) t))))))
-
-(defun denote-dired--add-front-matter (file title keywords id)
-  "Prepend front matter to FILE if `denote--only-note-p'.
-The TITLE, KEYWORDS and ID are passed from the renaming
-command and are used to construct a new front matter block if
-appropriate."
-  (when-let* (((denote--only-note-p file))
-              (filetype (denote-dired--filetype-heuristics file))
-              (date (denote--date (date-to-time id)))
-              (new-front-matter (denote--file-meta-header title date keywords 
id filetype)))
-    (with-current-buffer (find-file-noselect file)
-      (goto-char (point-min))
-      (insert new-front-matter))))
-
 ;;;; Renaming commands
 
 ;;;###autoload
@@ -411,13 +321,13 @@ will not---manage such files)."
            (file-name-sans-extension (file-name-nondirectory file))))
       (denote--keywords-prompt))))
   (let* ((dir (file-name-directory file))
-         (id (denote-dired--file-name-id file))
+         (id (denote--file-name-id file))
          (extension (file-name-extension file t))
          (new-name (denote--format-file
                     dir id keywords (denote--sluggify title) extension))
          (max-mini-window-height 0.33)) ; allow minibuffer to be resized
     (when (denote-dired--rename-file file new-name)
-      (denote-dired-update-dired-buffers)
+      (denote-update-dired-buffers)
       (denote-dired--rewrite-front-matter new-name title keywords))))
 
 ;;;###autoload
@@ -447,14 +357,14 @@ matter, refer to the variables:
            (file-name-sans-extension (file-name-nondirectory file))))
       (denote--keywords-prompt))))
   (let* ((dir (file-name-directory file))
-         (id (denote-dired--file-name-id file))
+         (id (denote--file-name-id file))
          (extension (file-name-extension file t))
          (new-name (denote--format-file
                     dir id keywords (denote--sluggify title) extension))
          (max-mini-window-height 0.33)) ; allow minibuffer to be resized
     (when (denote-dired--rename-file file new-name)
-      (denote-dired-update-dired-buffers)
-      (denote-dired--add-front-matter new-name title keywords id))))
+      (denote-update-dired-buffers)
+      (denote--add-front-matter new-name title keywords id))))
 
 (define-obsolete-function-alias
   'denote-dired-convert-file-to-denote
@@ -494,7 +404,7 @@ The operation does the following:
         (progn
           (dolist (file marks)
             (let* ((dir (file-name-directory file))
-                   (id (denote-dired--file-name-id file))
+                   (id (denote--file-name-id file))
                    (title (or (denote-retrieve--value-title file)
                               (file-name-sans-extension
                                (file-name-nondirectory file))))
@@ -502,9 +412,9 @@ The operation does the following:
                    (new-name (denote--format-file
                               dir id keywords (denote--sluggify title) 
extension)))
               (rename-file file new-name)
-              (denote-dired--rename-buffer file new-name)
+              (denote--rename-buffer file new-name)
               (when rewrite
-                (denote-dired--rewrite-keywords-no-question new-name 
keywords))))
+                (denote--rewrite-keywords new-name keywords))))
           (revert-buffer)))
     (user-error "No marked files; aborting")))
 
@@ -529,7 +439,7 @@ doc string)."
       (progn
         (dolist (file marks)
           (let* ((dir (file-name-directory file))
-                 (id (denote-dired--file-name-id file))
+                 (id (denote--file-name-id file))
                  (title (or (denote-retrieve--value-title file)
                             (file-name-sans-extension
                              (file-name-nondirectory file))))
@@ -537,8 +447,8 @@ doc string)."
                  (new-name (denote--format-file
                             dir id keywords (denote--sluggify title) 
extension)))
             (rename-file file new-name)
-            (denote-dired--rename-buffer file new-name)
-            (denote-dired--add-front-matter new-name title keywords id)))
+            (denote--rename-buffer file new-name)
+            (denote--add-front-matter new-name title keywords id)))
         (revert-buffer))
     (user-error "No marked files; aborting")))
 
diff --git a/denote-retrieve.el b/denote-retrieve.el
index 45cef4fd02..0ce9bc2ba5 100644
--- a/denote-retrieve.el
+++ b/denote-retrieve.el
@@ -45,10 +45,6 @@
   "^\\(?:#\\+\\)?\\(?:date\\)\\s-*[:=]"
   "Regular expression for date key.")
 
-(defconst denote-retrieve--keywords-front-matter-key-regexp
-  "^\\(?:#\\+\\)?\\(?:tags\\|filetags\\)\\s-*[:=]"
-  "Regular expression for keywords key.")
-
 (defun denote-retrieve--filename-identifier (file)
   "Extract identifier from FILE name."
   (if (file-exists-p file)
diff --git a/denote.el b/denote.el
index 2fceb1ff8b..238b308a95 100644
--- a/denote.el
+++ b/denote.el
@@ -522,6 +522,12 @@ output is sorted with `string-lessp'."
           (add-to-history 'denote--keyword-history kw))
         (delete-dups keywords)))
 
+;;;; Front matter retrieval functions
+
+(defconst denote-retrieve--keywords-front-matter-key-regexp
+  "^\\(?:#\\+\\)?\\(?:tags\\|filetags\\)\\s-*[:=]"
+  "Regular expression for keywords key.")
+
 ;;;; New note
 
 ;;;;; Common helpers for new notes
@@ -574,6 +580,20 @@ treatment)."
         (denote--format-markdown-keywords kw)
       (mapconcat #'downcase kw "  "))))
 
+;; TODO 2022-07-29: For now, for Org files, this functions only supports
+;; keywords separated with 2 spaces (as created by
+;; `denote--file-meta-keywords'), but we should try to handle all valid
+;; Org syntaxes.
+(defun denote--extract-keywords-from-front-matter (file &optional type)
+  "Extract keywords from front matter of FILE with TYPE.
+This is the reverse operation of `denote--file-meta-keywords'."
+  (let ((fm-keywords (denote-retrieve--value-keywords file)))
+    (if (or (eq type 'markdown-toml) (eq type 'markdown-yaml) (eq type 'md))
+        (split-string
+         (string-trim-right (string-trim-left fm-keywords "\\[") "\\]")
+         ", " t "\s*\"\s*")
+      (split-string fm-keywords "  " t " "))))
+
 (defvar denote-toml-front-matter
   "+++
 title      = %S
@@ -945,5 +965,110 @@ This is equivalent to calling `denote' when 
`denote-prompts' is set to
 
 (defalias 'denote-create-note-in-subdirectory (symbol-function 
'denote-subdirectory))
 
+;;;; Note modification
+
+;;;;; Common helpers for note modifications
+
+(declare-function denote-retrieve--value-title "denote-retrieve" (file 
&optional key))
+(declare-function denote-retrieve--value-keywords "denote-retrieve" (file 
&optional key))
+
+(defun denote--filetype-heuristics (file)
+  "Return likely file type of FILE.
+The return value is for `denote--file-meta-header'."
+  (pcase (file-name-extension file)
+    ("md" (if-let ((title-key (denote-retrieve--value-title file t))
+                   ((string-match-p "title\\s-*=" title-key)))
+              'markdown-toml
+            'markdown-yaml))
+    ("txt" 'text)
+    (_ 'org)))
+
+(defun denote--file-attributes-time (file)
+  "Return `file-attribute-modification-time' of FILE as identifier."
+  (format-time-string
+   denote--id-format
+   (file-attribute-modification-time (file-attributes file))))
+
+(defun denote--file-name-id (file)
+  "Return FILE identifier, else generate one."
+  (cond
+   ((string-match denote--id-regexp file)
+    (substring file (match-beginning 0) (match-end 0)))
+   ((denote--file-attributes-time file))
+   (t (format-time-string denote--id-format))))
+
+(defun denote-update-dired-buffers ()
+  "Update Dired buffers of variable `denote-directory'."
+  (mapc
+   (lambda (buf)
+     (with-current-buffer buf
+       (when (and (eq major-mode 'dired-mode)
+                  (string-prefix-p (denote-directory)
+                                   (expand-file-name default-directory)))
+         (revert-buffer))))
+   (buffer-list)))
+
+(defun denote--rename-buffer (old-name new-name)
+  "Rename OLD-NAME buffer to NEW-NAME, when appropriate."
+  (when-let ((buffer (find-buffer-visiting old-name)))
+    (with-current-buffer buffer
+      (set-visited-file-name new-name nil t))))
+
+(defun denote--rename-file (old-name new-name)
+  "Rename file named OLD-NAME to NEW-NAME.
+Update Dired buffers if the file is renamed.
+Return non-nil if the file is renamed, nil otherwise."
+  (unless (string= (expand-file-name old-name) (expand-file-name new-name))
+    (rename-file old-name new-name nil)
+    (denote--rename-buffer old-name new-name)))
+
+(defun denote--add-front-matter (file title keywords id)
+  "Prepend front matter to FILE if `denote--only-note-p'.
+The TITLE, KEYWORDS and ID are passed from the renaming
+command and are used to construct a new front matter block if
+appropriate."
+  (when-let* (((denote--only-note-p file))
+              (filetype (denote--filetype-heuristics file))
+              (date (denote--date (date-to-time id)))
+              (new-front-matter (denote--file-meta-header title date keywords 
id filetype)))
+    (with-current-buffer (find-file-noselect file)
+      (goto-char (point-min))
+      (insert new-front-matter))))
+
+(defun denote--edit-front-matter-p (file)
+  "Test if FILE should be subject to front matter rewrite.
+This is relevant for `denote-dired--rewrite-front-matter': if FILE
+has no front matter, then we abort early instead of trying to
+replace what isn't there."
+  (when-let ((ext (file-name-extension file)))
+    (and (file-regular-p file)
+         (file-writable-p file)
+         (not (denote--file-empty-p file))
+         (string-match-p "\\(md\\|org\\|txt\\)\\'" ext)
+         ;; Heuristic to check if this is one of our notes
+         (string= (expand-file-name default-directory) (denote-directory)))))
+
+(defun denote--rewrite-keywords (file keywords)
+  "Rewrite KEYWORDS in FILE outright.
+
+Do the same as `denote-dired--rewrite-front-matter' for keywords,
+but do not ask for confirmation.
+
+This is for use in `denote-dired-rename-marked-files' or related.
+Those commands ask for confirmation once before performing an
+operation on multiple files."
+  (when-let ((denote--edit-front-matter-p file)
+             (old-keywords (denote-retrieve--value-keywords file))
+             (new-keywords (denote--file-meta-keywords
+                            keywords (denote--filetype-heuristics file))))
+    (with-current-buffer (find-file-noselect file)
+      (save-excursion
+        (save-restriction
+          (widen)
+          (goto-char (point-min))
+          (re-search-forward denote-retrieve--keywords-front-matter-key-regexp 
nil t 1)
+          (search-forward old-keywords nil t 1)
+          (replace-match (concat "\\1" new-keywords) t))))))
+
 (provide 'denote)
 ;;; denote.el ends here



reply via email to

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