Function: denote-rewrite-front-matter
denote-rewrite-front-matter is a byte-compiled function defined in
denote.el.
Signature
(denote-rewrite-front-matter FILE TITLE KEYWORDS SIGNATURE DATE IDENTIFIER FILE-TYPE)
Documentation
Rewrite front matter of note after denote-rename-file.
The FILE, TITLE, KEYWORDS, SIGNATURE, DATE, IDENTIFIER, and FILE-TYPE are given by the renaming command and are used to construct new front matter values if appropriate.
If denote-rename-confirmations contains rewrite-front-matter,
prompt to confirm the rewriting of the front matter.
Source Code
;; Defined in ~/.emacs.d/elpa/denote-4.2.3/denote.el
(defun denote-rewrite-front-matter (file title keywords signature date identifier file-type)
"Rewrite front matter of note after `denote-rename-file'.
The FILE, TITLE, KEYWORDS, SIGNATURE, DATE, IDENTIFIER, and FILE-TYPE
are given by the renaming command and are used to construct new front
matter values if appropriate.
If `denote-rename-confirmations' contains `rewrite-front-matter',
prompt to confirm the rewriting of the front matter."
(let* ((front-matter (denote--front-matter file-type))
(file-content (with-current-buffer (find-file-noselect file) (buffer-string)))
(components-in-template (denote--get-front-matter-components-order front-matter file-type))
(components-in-file (denote--get-front-matter-components-order file-content file-type))
(components-to-add '())
(components-to-remove '())
(components-to-modify '())
(new-front-matter (denote--format-front-matter title date keywords identifier signature file-type))
(old-and-new-front-matter-lines (denote--get-old-and-new-front-matter-lines file new-front-matter file-type)))
;; Build the lists of components to add, remove, modify.
(dolist (component '(title keywords signature identifier date))
;; Ignore the component if it is not in the template. It is not added, removed or modified.
(when (memq component components-in-template)
(let ((value (pcase component ('title title) ('keywords keywords) ('signature signature) ('date date) ('identifier identifier))))
(cond ((and (not (memq component components-in-file))
(denote--component-has-value-p component value))
(push component components-to-add))
((and (memq component components-in-file)
;; The component can still be marked for modification.
(not (memq component denote-front-matter-components-present-even-if-empty-value))
(not (denote--component-has-value-p component value)))
(push component components-to-remove))
((and (memq component components-in-file)
(not (string= (alist-get 'old (alist-get component old-and-new-front-matter-lines))
(alist-get 'new (alist-get component old-and-new-front-matter-lines)))))
(push component components-to-modify))))))
;; There should be at least one component in the file and the template.
(when (and (seq-intersection components-in-file components-in-template)
(or components-to-add components-to-remove components-to-modify))
(when-let* ((final-components (denote--get-final-components-for-rewrite
components-in-file components-in-template components-to-add)))
(with-current-buffer (find-file-noselect file)
(when (or (not (memq 'rewrite-front-matter denote-rename-confirmations))
(y-or-n-p (denote--get-front-matter-rewrite-prompt
final-components
components-to-add components-to-remove components-to-modify
old-and-new-front-matter-lines)))
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
;; Position point at the beginning of the first front matter line
(let ((first-component (car (seq-difference final-components components-to-add))))
(re-search-forward
(funcall (denote--get-component-key-regexp-function first-component) file-type) nil t 1)
(goto-char (line-beginning-position)))
;; Do the modifications
(dolist (component final-components)
(let ((component-key-regexp-function (denote--get-component-key-regexp-function component))
(new-line (alist-get 'new (alist-get component old-and-new-front-matter-lines))))
(cond ((memq component components-to-remove)
(re-search-forward (funcall component-key-regexp-function file-type) nil t 1)
(delete-region (line-beginning-position) (line-beginning-position 2)))
((memq component components-to-add)
(insert (concat new-line "\n")))
((memq component components-to-modify)
(denote--rewrite-front-matter-line component new-line file-type)
(goto-char (line-beginning-position 2)))
(t
(goto-char (line-beginning-position 2))))))))))))))