Function: denote--rename-file

denote--rename-file is a byte-compiled function defined in denote.el.

Signature

(denote--rename-file FILE TITLE KEYWORDS SIGNATURE DATE IDENTIFIER)

Documentation

Rename FILE according to the other parameters.

Parameters TITLE, KEYWORDS, SIGNATURE, DATE, and IDENTIFIER are as described in denote-rename-file and are assumed to be valid (TITLE and SIGNATURE are strings, KEYWORDS is a list, etc.).

This function only does the work necessary to rename a file according to its parameters. In particular, it does not prompt for anything. It is meant to be combined with denote--rename-get-file-info-from-prompts-or-existing to create a renaming command.

Respect denote-rename-confirmations, denote-save-buffers and denote-kill-buffers.

Source Code

;; Defined in ~/.emacs.d/elpa/denote-4.2.3/denote.el
(defun denote--rename-file (file title keywords signature date identifier)
  "Rename FILE according to the other parameters.
Parameters TITLE, KEYWORDS, SIGNATURE, DATE, and IDENTIFIER are as
described in `denote-rename-file' and are assumed to be valid (TITLE and
SIGNATURE are strings, KEYWORDS is a list, etc.).

This function only does the work necessary to rename a file
according to its parameters.  In particular, it does not prompt
for anything.  It is meant to be combined with
`denote--rename-get-file-info-from-prompts-or-existing' to create
a renaming command.

Respect `denote-rename-confirmations', `denote-save-buffers' and
`denote-kill-buffers'."
  (let* ((initial-state (if (find-buffer-visiting file) 'visited 'not-visited))
         (file-type (denote-filetype-heuristics file))
         (keywords (denote-keywords-sort keywords))
         (directory (file-name-directory file))
         (extension (denote-get-file-extension file))
         (date (cond (date date)
                     (denote-accept-nil-date date)
                     (t (or (file-attribute-modification-time (file-attributes file))
                            (current-time)))))
         (old-identifier (or (denote-retrieve-filename-identifier file) ""))
         ;; Handle empty id
         (identifier (if (and (string-empty-p identifier)
                              (or (eq denote-generate-identifier-automatically t)
                                  (eq denote-generate-identifier-automatically 'on-rename)))
                         (funcall denote-get-identifier-function
                                  nil
                                  (or date
                                      (file-attribute-modification-time (file-attributes file))
                                      (current-time)))
                       identifier))
         (identifier (cond ((string-empty-p identifier) identifier)
                           ((string= old-identifier identifier) identifier)
                           ((and (not (string-empty-p old-identifier)) (denote--file-has-backlinks-p file))
                            (user-error "The identifier cannot be modified: that will break existing links"))
                           (t (funcall denote-get-identifier-function identifier nil))))
         (new-name (denote-format-file-name directory identifier keywords title extension signature))
         (max-mini-window-height denote-rename-max-mini-window-height))
    (when (and (file-regular-p new-name)
               (not (string= (expand-file-name file) (expand-file-name new-name))))
      (user-error "The destination file `%s' already exists" new-name))
    ;; Modify file name, buffer name, or both
    (when (denote-rename-file-prompt file new-name)
      (denote-rename-file-and-buffer file new-name))
    ;; Handle front matter if new-name is of a supported type (rewrite or add front matter)
    (when (and denote-rename-rewrite-front-matter
               (denote-file-has-supported-extension-p file)
               (denote-file-is-writable-and-supported-p new-name))
      (if (denote--file-has-front-matter-p new-name file-type)
          (denote-rewrite-front-matter new-name title keywords signature date identifier file-type)
        (when (denote-add-front-matter-prompt new-name)
          (denote-prepend-front-matter new-name title keywords signature date identifier file-type))))
    (when (and denote-used-identifiers (not (string-empty-p identifier)))
      (puthash identifier t denote-used-identifiers))
    (denote--handle-save-and-kill-buffer 'rename new-name initial-state)
    (setq denote-current-data
          (list
           (cons 'title title)
           (cons 'keywords keywords)
           (cons 'signature signature)
           (cons 'directory directory)
           (cons 'date date)
           (cons 'id identifier)
           (cons 'file-type file-type)
           (cons 'template "")))
    (run-hooks 'denote-after-rename-file-hook)
    new-name))