protesilaos / denote

Simple notes for Emacs with an efficient file-naming scheme
https://protesilaos.com/emacs/denote
GNU General Public License v3.0
512 stars 54 forks source link

Prevent newline between front matter and templates #372

Closed swflint closed 3 months ago

swflint commented 3 months ago

Is there a way to prevent an extra newline from being inserted between front matter and templates? I have some templates that include other Org-related #+... keywords (e.g., for export configuration, etc.), and would prefer all of that to be in one block (and would like to avoid having to do so manually).

protesilaos commented 3 months ago

From: "Samuel W. Flint" @.***> Date: Thu, 6 Jun 2024 08:15:09 -0700

Is there a way to prevent an extra newline from being inserted between front matter and templates? I have some templates that include other Org-related #+... keywords (e.g., for export configuration, etc.), and would prefer all of that to be in one block (and would like to avoid having to do so manually).

Maybe you can modify the 'denote-org-front-matter'? You will notice it has an extra newline at the end. Though I guess you only want this when a template is inserted, right? If so, you can write your small modified version of 'denote-template', like this:

(defun my-denote-template-no-org-front-matter-newline () "Like `denote-template' but without newline after the Org front matter." (declare (interactive-only t)) (interactive) (let ((denote-prompts (denote-add-prompts '(template))) (denote-org-front-matter "#+title: %s

+date: %s

+filetags: %s

+identifier: %s\n"))

(call-interactively #'denote)))

-- Protesilaos Stavrou https://protesilaos.com

swflint commented 3 months ago

That might work, however, my specific use case is for denote-journal and citar-denote. I've played around with an advice-based approach, which seems to work pretty well.

(defcustom denote-templates-front-matter-remove-extra-newlines '(journal)
  "Which templates should front matter have extra newlines removed for?"
  :type '(repeat symbol)
  :group 'denote)

(defun denote-remove-front-matter-newlines-advice (orig &optional title keywords file-type directory date template signature)
  "Remove additional newlines from denote front matter.

Additional newlines will only be removed when TEMPLATE matches
`denote-templates-front-matter-remove-extra-newlines'.

Remaining arguments (TITLE, KEYWORDS, FILE-TYPE, DIRECTORY, DATE,
SIGNATURE) follow for ORIG (i.e., `denote')."
  (let ((denote-org-front-matter (if (cl-find-if (lambda (x)
                                                   (string= template (alist-get x denote-templates "")))
                                                 denote-templates-front-matter-remove-extra-newlines) 
                                     (concat (string-trim denote-org-front-matter) "\n")
                                   denote-org-front-matter)))
    (apply orig title keywords file-type directory date template signature)))
(advice-add 'denote :around #'denote-remove-front-matter-newlines-advice)