Function: denote--get-final-components-for-rewrite

denote--get-final-components-for-rewrite is a byte-compiled function defined in denote.el.

Signature

(denote--get-final-components-for-rewrite COMPONENTS-IN-FILE COMPONENTS-IN-TEMPLATE COMPONENTS-TO-ADD)

Documentation

Return the final components to handle by a front matter rewrite operation.

COMPONENTS-TO-ADD is the list of components that have to be added to COMPONENTS-IN-FILE to build the list of components that will need to be handled during a front matter rewrite operation.

COMPONENTS-IN-TEMPLATE is the list of components in a front matter template. They are used to determine how the COMPONENTS-TO-ADD are added to COMPONENTS-IN-FILE.

Example:
          file = (title signature)
      template = (title keywords date id signature)

The date line is missing from the file. From the template, we find out that it needs to be added *after* a keywords line. Since we don't have one in the file, we keep looking for a line to add it *after* and find a title line. Had we not found the title line in the file, we would have searched for a line to insert it *before*. We would have inserted the date line before the signature line, for example.

This is repeated until all missing components are added.

Source Code

;; Defined in ~/.emacs.d/elpa/denote-4.2.3/denote.el
(defun denote--get-final-components-for-rewrite (components-in-file components-in-template components-to-add)
  "Return the final components to handle by a front matter rewrite operation.

COMPONENTS-TO-ADD is the list of components that have to be added to
COMPONENTS-IN-FILE to build the list of components that will need to be
handled during a front matter rewrite operation.

COMPONENTS-IN-TEMPLATE is the list of components in a front matter
template.  They are used to determine how the COMPONENTS-TO-ADD are
added to COMPONENTS-IN-FILE.

Example:
          file = (title signature)
      template = (title keywords date id signature)

The date line is missing from the file.  From the template, we find out
that it needs to be added *after* a keywords line.  Since we don't have
one in the file, we keep looking for a line to add it *after* and find a
title line.  Had we not found the title line in the file, we would have
searched for a line to insert it *before*.  We would have inserted the
date line before the signature line, for example.

This is repeated until all missing components are added."
  (let ((final-components (copy-sequence components-in-file)))
    (dolist (component components-to-add)
      (if-let* ((previous-components-in-template
                 (seq-take-while (lambda (x) (not (eq x component))) components-in-template))
                (first-previous-component-in-file
                 (seq-find (lambda (x) (memq x final-components)) (reverse previous-components-in-template))))
          ;; Insert after the existing element.
          (let ((sublist final-components))
            (while sublist
              (if (not (eq (car sublist) first-previous-component-in-file))
                  (setq sublist (cdr sublist))
                (push component (cdr sublist))
                (setq sublist nil))))
        (let* ((next-components-in-template
                (cdr (seq-drop-while (lambda (x) (not (eq x component))) components-in-template)))
               (first-next-component-in-file
                (seq-find (lambda (x) (memq x final-components)) next-components-in-template)))
          ;; Insert before the existing element.  The intention is to
          ;; modify final-components, but it does not work when push
          ;; is called on sublist on the first iteration of the loop.
          (if (eq (car final-components) first-next-component-in-file)
              (push component final-components)
            (let ((sublist final-components))
              (while sublist
                (if (not (eq (car sublist) first-next-component-in-file))
                    (setq sublist (cdr sublist))
                  (push component sublist)
                  (setq sublist nil))))))))
    final-components))