[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/denote 3a63c763c5 218/355: Improve post-rename front ma
From: |
ELPA Syncer |
Subject: |
[elpa] externals/denote 3a63c763c5 218/355: Improve post-rename front matter rewrite |
Date: |
Sun, 26 Jun 2022 23:58:26 -0400 (EDT) |
branch: externals/denote
commit 3a63c763c5f10086f1b4f973e6ac471d6a44d280
Author: Protesilaos Stavrou <info@protesilaos.com>
Commit: Protesilaos Stavrou <info@protesilaos.com>
Improve post-rename front matter rewrite
Expands on commit 4041e43 with the benefit of 74962d1.
I wanted to make it operate on the front matter only when it makes
sense. For example, if we have a txt file which is empty, we want to
abort silently: just do the rename.
---
denote-dired.el | 72 +++++++++++++++++++++++++++++++--------------------------
1 file changed, 39 insertions(+), 33 deletions(-)
diff --git a/denote-dired.el b/denote-dired.el
index c7942be276..f5de87ba42 100644
--- a/denote-dired.el
+++ b/denote-dired.el
@@ -210,43 +210,49 @@ The return value is for `denote--file-meta-header'."
;; Org does not have a real delimiter. This is the trickiest one.
(_ (re-search-forward "^[\s\t]*$" nil t 1))))
+(defun denote-dired--edit-front-matter-p (file)
+ "Test if FILE should be subject to front matter rewrite."
+ (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= default-directory (abbreviate-file-name
(denote-directory))))))
+
(defun denote-dired-rewrite-front-matter (file title keywords)
"Rewrite front matter of note after `denote-dired-rename-file'.
The FILE, TITLE, and KEYWORDS are passed from the renaming
command and are used to construct a new front matter block."
- (when-let ((ext (file-name-extension file))
- ((and (file-regular-p file)
- (file-writable-p file)
- (string-match-p "\\(md\\|org\\|txt\\)\\'" ext)
- ;; Heuristic to check if this is one of our notes
- (string= default-directory (abbreviate-file-name
(denote-directory))))))
- (let* ((id (denote-retrieve--filename-identifier file))
- (date (denote-retrieve--value-date file))
- (filetype (denote-dired--filetype-heuristics file))
- (new-front-matter (denote--file-meta-header title date keywords id
filetype))
- old-front-matter
- front-matter-delimiter)
- (with-current-buffer (find-file-noselect file)
- (save-excursion
- (goto-char (point-min))
- (setq old-front-matter
- (buffer-substring-no-properties
- (point)
- (progn
- (setq front-matter-delimiter
(denote-dired--front-matter-search-delimiter filetype))
- (point)))))
- (when (and old-front-matter
- (y-or-n-p
- (format "%s\n%s\nReplace front matter?"
- (propertize old-front-matter 'face 'error)
- (propertize new-front-matter 'face 'success))))
- (delete-region (point-min) front-matter-delimiter)
- (goto-char (point-min))
- (insert new-front-matter)
- ;; FIXME 2022-06-16: Instead of `delete-blank-lines', we
- ;; should check if we added any new lines and delete only
- ;; those.
- (delete-blank-lines))))))
+ (when (denote-dired--edit-front-matter-p file)
+ (when-let* ((id (denote-retrieve--filename-identifier file))
+ (date (denote-retrieve--value-date file))
+ (filetype (denote-dired--filetype-heuristics file))
+ (new-front-matter (denote--file-meta-header title date
keywords id filetype)))
+ (let (old-front-matter front-matter-delimiter)
+ (with-current-buffer (find-file-noselect file)
+ (save-excursion
+ (save-restriction
+ (widen)
+ (goto-char (point-min))
+ (setq front-matter-delimiter
(denote-dired--front-matter-search-delimiter filetype))
+ (when front-matter-delimiter
+ (setq old-front-matter
+ (buffer-substring-no-properties
+ (point-min)
+ (progn front-matter-delimiter (point)))))))
+ (when (and old-front-matter
+ (y-or-n-p
+ (format "%s\n%s\nReplace front matter?"
+ (propertize old-front-matter 'face 'error)
+ (propertize new-front-matter 'face 'success))))
+ (delete-region (point-min) front-matter-delimiter)
+ (goto-char (point-min))
+ (insert new-front-matter)
+ ;; FIXME 2022-06-16: Instead of `delete-blank-lines', we
+ ;; should check if we added any new lines and delete only
+ ;; those.
+ (delete-blank-lines)))))))
;;;; Extra fontification
- [elpa] externals/denote e37e429844 198/355: Add possible to-do for using xref, (continued)
- [elpa] externals/denote e37e429844 198/355: Add possible to-do for using xref, ELPA Syncer, 2022/06/27
- [elpa] externals/denote 2327b5beb6 212/355: Add helper FNs to return title and date values, ELPA Syncer, 2022/06/27
- [elpa] externals/denote 98113698a7 202/355: Expand sample configuration, ELPA Syncer, 2022/06/27
- [elpa] externals/denote cbaab43170 240/355: Update Acknowledgement: svnsbck => Sven Seebeck, ELPA Syncer, 2022/06/27
- [elpa] externals/denote 9be3cfab99 215/355: Relocate defcustom where it belongs, ELPA Syncer, 2022/06/27
- [elpa] externals/denote b25704e041 211/355: Make denote-retrieve--value pass optional group, ELPA Syncer, 2022/06/27
- [elpa] externals/denote 24997c6bc5 226/355: Remove "work-in-progress" from backlinks' section, ELPA Syncer, 2022/06/27
- [elpa] externals/denote 25fe9870dc 225/355: Finalise 'denote:' custom Org hyperlink type, ELPA Syncer, 2022/06/27
- [elpa] externals/denote 13bae5f9c4 235/355: Remove instability warnings (v0.1.0 is close), ELPA Syncer, 2022/06/27
- [elpa] externals/denote 1b4dc74e3c 224/355: Update denote-dired.el Commentary section, ELPA Syncer, 2022/06/27
- [elpa] externals/denote 3a63c763c5 218/355: Improve post-rename front matter rewrite,
ELPA Syncer <=
- [elpa] externals/denote 5213c9e7af 241/355: Add section with alternatives to Denote, ELPA Syncer, 2022/06/27
- [elpa] externals/denote e0ff114a93 238/355: Update Denote sources in manual's introduction, ELPA Syncer, 2022/06/27
- [elpa] externals/denote 7ce2f7d3e3 251/355: Use the 'denote-directory' function, not variable, ELPA Syncer, 2022/06/27
- [elpa] externals/denote c8f3e707c2 223/355: Revise one-line descriptions, where needed, ELPA Syncer, 2022/06/27
- [elpa] externals/denote a436cdf07e 221/355: Tweak doc strings, ELPA Syncer, 2022/06/27
- [elpa] externals/denote a5940b315a 265/355: Replace needless 'let*' with 'let', ELPA Syncer, 2022/06/27
- [elpa] externals/denote 5686e86cfc 262/355: Only show valid notes in file prompt, ELPA Syncer, 2022/06/27
- [elpa] externals/denote 3d606cbe95 272/355: Simplify completion via 'org-insert-link', ELPA Syncer, 2022/06/27
- [elpa] externals/denote f21a99775d 256/355: Improve rename prompt; only rename regular files, ELPA Syncer, 2022/06/27
- [elpa] externals/denote 941258d8d4 263/355: Remove faulty MUSTMATCH from file prompt, ELPA Syncer, 2022/06/27