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

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

[elpa] externals/denote 21d9cf02b4 4/4: Merge pull request #69 from jean


From: ELPA Syncer
Subject: [elpa] externals/denote 21d9cf02b4 4/4: Merge pull request #69 from jeanphilippegg/rename-functions
Date: Mon, 1 Aug 2022 13:57:34 -0400 (EDT)

branch: externals/denote
commit 21d9cf02b4a97783419be7f30ed9d62ffed86ac4
Merge: 7d6c51eb86 a72817326a
Author: Protesilaos Stavrou <info@protesilaos.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #69 from jeanphilippegg/rename-functions
    
    Remove denote-dired-rename-file-and-add-front-matter
---
 denote.el | 146 ++++++++++++++++++++++++--------------------------------------
 1 file changed, 57 insertions(+), 89 deletions(-)

diff --git a/denote.el b/denote.el
index f68f6d9a7e..48b7398f08 100644
--- a/denote.el
+++ b/denote.el
@@ -1131,18 +1131,29 @@ appropriate."
       (goto-char (point-min))
       (insert new-front-matter))))
 
+(defun denote--file-match-p (regexp file)
+  "Return t if REGEXP matches in the FILE."
+  (with-current-buffer (find-file-noselect file)
+    (save-excursion
+      (save-restriction
+        (widen)
+        (goto-char (point-min))
+        (re-search-forward regexp nil t 1)))))
+
 (defun denote--edit-front-matter-p (file)
   "Test if FILE should be subject to front matter rewrite.
-This is relevant for `denote--rewrite-front-matter': if FILE
-has no front matter, then we abort early instead of trying to
-replace what isn't there."
+This is relevant for `denote--rewrite-front-matter'. We can edit
+the front matter if it contains a \"title\" line and a \"tags\"
+line (the exact syntax depending on the file type)."
   (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)))))
+         (string-prefix-p (denote-directory) (expand-file-name 
default-directory))
+         (denote--file-match-p denote--retrieve-title-front-matter-key-regexp 
file)
+         (denote--file-match-p 
denote--retrieve-keywords-front-matter-key-regexp file))))
 
 (defun denote--rewrite-keywords (file keywords)
   "Rewrite KEYWORDS in FILE outright.
@@ -1153,8 +1164,7 @@ 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))
+  (when-let ((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)
@@ -1209,8 +1219,7 @@ Throw error is FILE is not regular, else return FILE."
 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--edit-front-matter-p file)
-             (old-title (denote--retrieve-value-title file))
+  (when-let ((old-title (denote--retrieve-value-title file))
              (old-keywords (denote--retrieve-value-keywords file))
              (new-title title)
              (new-keywords (denote--file-meta-keywords
@@ -1235,7 +1244,7 @@ appropriate."
               (replace-match (concat "\\1" new-keywords) t)))))))
 
 ;;;###autoload
-(defun denote-dired-rename-file (file title keywords)
+(defun denote-rename-file (file title keywords)
   "Rename file and update existing front matter if appropriate.
 
 If in Dired, consider FILE to be the one at point, else prompt
@@ -1272,8 +1281,17 @@ to double-check the effect).  The rewrite of the FILE and
 KEYWORDS in the front matter should not affect the rest of the
 block.
 
-If the file doesn't have front matter, skip this step (see the
-command `denote-dired-rename-file-and-add-front-matter').
+If the file doesn't have front matter, add one at the top of the
+file without asking.
+
+Front matter is added only when the file is one of the supported
+file types (per `denote-file-type').  For per-file-type front
+matter, refer to the variables:
+
+- `denote-org-front-matter'
+- `denote-text-front-matter'
+- `denote-toml-front-matter'
+- `denote-yaml-front-matter'
 
 This command is intended to (i) rename existing Denote notes
 while updating their title and keywords in the front matter, (ii)
@@ -1298,44 +1316,19 @@ will not---manage such files)."
     (when (denote--rename-file-prompt file new-name)
       (denote--rename-file file new-name)
       (denote-update-dired-buffers)
-      (denote--rewrite-front-matter new-name title keywords))))
-
-;;;###autoload
-(defun denote-dired-rename-file-and-add-front-matter (file title keywords)
-  "Rename FILE and unconditionally add front matter.
+      (if (denote--edit-front-matter-p new-name)
+          (denote--rewrite-front-matter new-name title keywords)
+        (denote--add-front-matter new-name title keywords id)))))
 
-This command has the same modalities of interaction as
-`denote-dired-rename-file' in terms of the FILE, TITLE, and
-KEYWORDS prompts, except it always inserts front matter at the
-start of the file.  It does not check if any front matter is
-already present.
-
-Front matter is added only when the file is one of the supported
-file types (per `denote-file-type').  For per-file-type front
-matter, refer to the variables:
+(define-obsolete-function-alias
+  'denote-dired-rename-file-and-add-front-matter
+  'denote-rename-file
+  "0.5.0")
 
-- `denote-org-front-matter'
-- `denote-text-front-matter'
-- `denote-toml-front-matter'
-- `denote-yaml-front-matter'"
-  (interactive
-   (let ((file (denote--rename-dired-file-or-prompt)))
-     (list
-      file
-      (denote--title-prompt
-       (or (denote--retrieve-value-title file)
-           (file-name-sans-extension (file-name-nondirectory file))))
-      (denote--keywords-prompt))))
-  (let* ((dir (file-name-directory 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--rename-file-prompt file new-name)
-      (denote--rename-file file new-name)
-      (denote-update-dired-buffers)
-      (denote--add-front-matter new-name title keywords id))))
+(define-obsolete-function-alias
+  'denote-dired-rename-file
+  'denote-rename-file
+  "0.5.0")
 
 (define-obsolete-function-alias
   'denote-dired-convert-file-to-denote
@@ -1360,52 +1353,20 @@ The operation does the following:
 - a prompt is asked once for the KEYWORDS field and the input is
   applied to all file names;
 
-- if the file is recognized as a Denote note, rewrite its front
-  matter to include the new keywords.  A confirmation to carry
-  out this step is performed once at the outset.  Note that the
-  affected buffers are not saved.  The user can thus check them
-  to confirm that the new front matter does not cause any
+- if the file is recognized as a Denote note, add a front matter
+  or rewrite it to include the new keywords. A confirmation to
+  carry out this step is performed once at the outset. Note that
+  the affected buffers are not saved. The user can thus check
+  them to confirm that the new front matter does not cause any
   problems (e.g. with the command `diff-buffer-with-file').
   Multiple buffers can be saved with `save-some-buffers' (read
-  its doc string)."
-  (interactive nil dired-mode)
-  (if-let ((marks (dired-get-marked-files))
-           (keywords (denote--keywords-prompt)))
-      (let ((rewrite (yes-or-no-p "Rewrite front matter of keywords, if 
relevant (buffers are not saved)?")))
-        (progn
-          (dolist (file marks)
-            (let* ((dir (file-name-directory file))
-                   (id (denote--file-name-id file))
-                   (title (or (denote--retrieve-value-title file)
-                              (file-name-sans-extension
-                               (file-name-nondirectory file))))
-                   (extension (file-name-extension file t))
-                   (new-name (denote--format-file
-                              dir id keywords (denote--sluggify title) 
extension)))
-              (denote--rename-file file new-name)
-              (when rewrite
-                (denote--rewrite-keywords new-name keywords))))
-          (revert-buffer)))
-    (user-error "No marked files; aborting")))
-
-;;;###autoload
-(defun denote-dired-rename-marked-files-and-add-front-matters ()
-  "Like `denote-dired-rename-marked-files' but add front matter.
-
-The additon of front matter takes place only if the given file
-has the appropriate file type extension (per the user option
-`denote-file-type').
-
-Buffers are not saved.  The user can thus check them to confirm
-that the new front matter does not cause any problems (e.g. by
-invoking the command `diff-buffer-with-file').
-
-Multiple buffers can be saved with `save-some-buffers' (read its
-doc string)."
+  its doc string). The addition of front matter takes place only
+  if the given file has the appropriate file type extension (per
+  the user option `denote-file-type')."
   (interactive nil dired-mode)
   (if-let ((marks (dired-get-marked-files))
            (keywords (denote--keywords-prompt))
-           ((yes-or-no-p "Add front matter to all relevant files (buffers are 
not saved)?")))
+           ((yes-or-no-p "Add front matter or rewrite front matter of keywords 
(buffers are not saved)?")))
       (progn
         (dolist (file marks)
           (let* ((dir (file-name-directory file))
@@ -1417,10 +1378,17 @@ doc string)."
                  (new-name (denote--format-file
                             dir id keywords (denote--sluggify title) 
extension)))
             (denote--rename-file file new-name)
-            (denote--add-front-matter new-name title keywords id)))
+            (if (denote--edit-front-matter-p new-name)
+                (denote--rewrite-keywords new-name keywords)
+              (denote--add-front-matter new-name title keywords id))))
         (revert-buffer))
     (user-error "No marked files; aborting")))
 
+(define-obsolete-function-alias
+  'denote-dired-rename-marked-files-and-add-front-matter
+  'denote-dired-rename-marked-files
+  "0.5.0")
+
 ;;;; The Denote faces
 
 (defgroup denote-faces ()



reply via email to

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