protesilaos / denote

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

Write `#+filetags: XXX` to file when calling `denote-dired-rename-marked-files-add-keywords` #441

Closed LukasCBossert closed 2 months ago

LukasCBossert commented 2 months ago

I am working heavily on remodeling my data and files. It seemed easiest for me to do this in dired and using denote-dired-rename-marked-files-add-keywords. The keywords are set properly but they are not written to the file itself. I understand that the filename and the filecontent should match because e.g. I am using denote-explore-sync-metadata (from denote-explore) that complains and wants to delete the keyword from the filename because it is obviously missing in the file.

So I was wondering if denote-dired-rename-marked-files-add-keywords etc. could also add the keywords to the file? Or am I missing something?

protesilaos commented 2 months ago

Right now we have the following to determine if we should touch the front matter:

(defun denote--edit-front-matter-p (file file-type)
  "Test if FILE should be subject to front matter rewrite.
Use FILE-TYPE to look for the front matter lines.  This is
relevant for operations that insert or rewrite the front matter
in a Denote note.

For the purposes of this test, FILE is a Denote note when it
contains a title line, a keywords line or both."
  (and (denote--front-matter file-type)
       (or (denote--regexp-in-file-p (denote--title-key-regexp file-type) file)
           (denote--regexp-in-file-p (denote--keywords-key-regexp file-type) file))))

If there is no entry for title/keywords, we do not rewrite it. We do this to be extra safe, though I understand it is a problem in this case.

We have two options here. Either we make our rename commands add/rewrite front matter if the file is supported, or define a new command that runs denote-add-front-matter over the marked files in Dired. The latter seems safer to me but I am open to ideas.

LukasCBossert commented 2 months ago

From my non-technical point of view a new command sounds like a good solution @protesilaos

protesilaos commented 2 months ago

From: "Lukas C. Bossert" @.***> Date: Thu, 19 Sep 2024 06:48:41 -0700

From my non-technical point of view a new command sounds like a good solution @protesilaos

Fine, I will try to write something as soon as possible and then keep you updated.

-- Protesilaos Stavrou https://protesilaos.com

LukasCBossert commented 2 months ago

Great, thanks @protesilaos . Thinking about this matter again, I suppose having #+identifier: inserted into the file would also be a nice feature. As far as I can tell denote is not complaining when #+identifier: is missing in the file as long as the filename has the correct identifier.

LukasCBossert commented 2 months ago

Great, thanks @protesilaos . Thinking about this matter again, I suppose having #+identifier: inserted into the file would also be a nice feature. As far as I can tell denote is not complaining when #+identifier: is missing in the file as long as the filename has the correct identifier.

protesilaos commented 2 months ago

@LukasCBossert Can you try this? The idea is to insert front matter to files that do not have any but are already using the Denote file-naming scheme:

(defun my-denote-dired-add-front-matter ()
  "Like `denote-add-front-matter' but works over Dired marked files."
  (interactive nil dired-mode)
  (if-let ((marks (seq-filter
                   (lambda (m)
                     (and (file-regular-p m)
                          (file-writable-p m)
                          (denote-file-has-identifier-p m)))
                   (dired-get-marked-files))))
      (progn
        (dolist (file marks)
          (denote--add-front-matter
           file
           (denote-retrieve-filename-title file)
           (denote-extract-keywords-from-path file)
           (denote-retrieve-filename-identifier file)
           (or (denote-filetype-heuristics file) 'org)))
        (denote-update-dired-buffers))
    (user-error "No marked Denote files; aborting")))
LukasCBossert commented 2 months ago

@protesilaos actually your code did add the frontmatter to the file, but I noticed a couple of things.

This is the filename: 20231112T153128--edward-tufte__person.org

This is the frontmatter I had before in the file:

:PROPERTIES:
:ID:       dab4ae6c-5245-4cf8-8bb2-c32c9614d023
:END:
#+title:      Edward Tufte
#+modified: [2023-11-12  So 15:46]
#+filetags:   :person:

after executing my-denote-dired-add-front-matter I have this frontmatter:

#+title:      edward-tufte
#+date:       [2023-11-12 Sun 15:31]
#+filetags:   :person:
#+identifier: 20231112T153128

:PROPERTIES:
:ID:       dab4ae6c-5245-4cf8-8bb2-c32c9614d023
:END:
#+title:      Edward Tufte
#+modified: [2023-11-12  So 15:46]
#+filetags:   :person:

I am wondering having the:PROPERTIES:-section before #+title: causes the issue of the "second" frontmatter? Then it is probably no surprise that the title is sluggified. The main issue is solved (having #+filetags and #+identifier added) but there are no the sideeffects.

For explanation: I have :PROPERTIES: in the first line because I have my files created through org-roam but according to the denote-naming-scheme. So I am mixing the two concepts and it actually works great.

protesilaos commented 2 months ago

From: "Lukas C. Bossert" @.***> Date: Sat, 21 Sep 2024 13:17:34 -0700

@protesilaos actually your code did add the frontmatter to the file, but I noticed a couple of things.

This is the filename: 20231112T153128--edward-tufte__person.org

This is the frontmatter I had before in the file:

:PROPERTIES:
:ID:       dab4ae6c-5245-4cf8-8bb2-c32c9614d023
:END:
#+title:      Edward Tufte
#+modified: [2023-11-12  So 15:46]
#+filetags:   :person:

Oh, I thought those files did not have any front matter. In this case, what I sent you is not correct.

Let's take a step back: is the addition of an '#+identifier' line all you need?

[... 17 lines elided]

For explanation: I have :PROPERTIES: in the first line because I have my files created through org-roam but according to the denote-naming-scheme. So I am mixing the two concepts and it actually works great.

This is fine and Denote will work as intended. Note that Denote does not use the '#+identifier' in the front matter. This is there in case some other package/code wants to access that data. Denote only ever reads the identifier from the file name.

-- Protesilaos Stavrou https://protesilaos.com

LukasCBossert commented 2 months ago

when #+identifier is not needed within the file then this can be omitted. Regarding #+filetags it seems that this is already supported as I noticed working on #439

protesilaos commented 2 months ago

From: "Lukas C. Bossert" @.***> Date: Sun, 22 Sep 2024 06:04:05 -0700

when #+identifier is not needed within the file then this can be omitted. Regarding #+filetags it seems that this is already supported as I noticed working on #439

Just to be sure, are we done with this then or is there something still missing?

-- Protesilaos Stavrou https://protesilaos.com

LukasCBossert commented 2 months ago

I guess we can close it then.