Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

denote-rename-buffer should not be used for files that are not notes #261

Closed
brabalan opened this issue Feb 22, 2024 · 7 comments
Closed

Comments

@brabalan
Copy link
Contributor

I have set-up `denote-rename-buffer' as follows:

(setq denote-rename-buffer-format "[D] %t (%k)")
(denote-rename-buffer-mode 1)

My problem is that it is used for every denote file, including my pdfs stored in denote and viewed in emacs. For them, the buffer name is [D] (), as there is no title nor keyword to retrieve from the front matter. I see there is some code that prevents the renaming when the string is empty, but as I configured denote-rename-buffer-format, this is never the case for me.

Could it be possible to (have an option to) only rename buffers that are notes?

@protesilaos
Copy link
Owner

Thank you @brabalan for reporting this!

You are right. I think we can work around this by tweaking denote-rename-buffer--format. The idea is to check the front matter only if the file is writable, otherwise retrieve data from the file name.

Could it be possible to (have an option to) only rename buffers that are notes?

Yes, we can have this. Though I think a more do-the-right-thing sort of approach is better here to give meaningful, albeit not always perfect, names at all times.

@protesilaos
Copy link
Owner

Perhaps this change is enough?

(defun denote-rename-buffer--format (buffer)
  "Parse the BUFFER through the `denote-rename-buffer-format'."
  (when-let ((file (buffer-file-name buffer))
             (type (denote-filetype-heuristics file)))
    (string-trim
     (format-spec denote-rename-buffer-format
                  (list (cons ?t (cond
                                  ((not (denote-file-is-writable-and-supported-p file))
                                   (denote-retrieve-filename-title file))
                                  ((denote-retrieve-front-matter-title-value file type))
                                  (t  "")))
                        (cons ?i (or (denote-retrieve-filename-identifier file) ""))
                        (cons ?d (or (denote-retrieve-filename-identifier file) ""))
                        (cons ?s (or (denote-retrieve-filename-signature file) ""))
                        (cons ?k (cond
                                  ((not (denote-file-is-writable-and-supported-p file))
                                   (denote-retrieve-filename-keywords file))
                                  ((denote-retrieve-front-matter-keywords-value-as-string file type))
                                  (t  "")))
                        (cons ?% "%"))
                  'delete))))

The diff:

 denote-rename-buffer.el | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/denote-rename-buffer.el b/denote-rename-buffer.el
index 4230b35..91a93cc 100644
--- a/denote-rename-buffer.el
+++ b/denote-rename-buffer.el
@@ -93,11 +93,19 @@ (defun denote-rename-buffer--format (buffer)
              (type (denote-filetype-heuristics file)))
     (string-trim
      (format-spec denote-rename-buffer-format
-                  (list (cons ?t (or (denote-retrieve-front-matter-title-value file type) ""))
+                  (list (cons ?t (cond
+                                  ((not (denote-file-is-writable-and-supported-p file))
+                                   (denote-retrieve-filename-title file))
+                                  ((denote-retrieve-front-matter-title-value file type))
+                                  (t  "")))
                         (cons ?i (or (denote-retrieve-filename-identifier file) ""))
                         (cons ?d (or (denote-retrieve-filename-identifier file) ""))
                         (cons ?s (or (denote-retrieve-filename-signature file) ""))
-                        (cons ?k (or (denote-retrieve-front-matter-keywords-value-as-string file type) ""))
+                        (cons ?k (cond
+                                  ((not (denote-file-is-writable-and-supported-p file))
+                                   (denote-retrieve-filename-keywords file))
+                                  ((denote-retrieve-front-matter-keywords-value-as-string file type))
+                                  (t  "")))
                         (cons ?% "%"))
                   'delete))))
 

@brabalan
Copy link
Contributor Author

Thanks! This would work, but about the following logic: "if denote-retrieve-front-matter-title-value return something, use it, otherwise use denote-retrieve-filename-title"? (and the same for keywords)

@protesilaos
Copy link
Owner

You mean it does not return a value right now?

When I test this on my end, it prompts for a file and then returns its title:

(let* ((file (denote-file-prompt))
       (type (denote-filetype-heuristics file)))
  (cond
   ;; ((not (denote-file-is-writable-and-supported-p file))
   ;;  (denote-retrieve-filename-title file))
   ((denote-retrieve-front-matter-title-value file type))
   (t  "")))

Or did I miss something?

@brabalan
Copy link
Contributor Author

What I mean is that the code for the title could be this:

(let* ((file (denote-file-prompt))
       (type (denote-filetype-heuristics file)))
  (cond
   ;; ((not (denote-file-is-writable-and-supported-p file))
   ;;  (denote-retrieve-filename-title file))
   ((denote-retrieve-front-matter-title-value file type))
   ((denote-retrieve-filename-title file))
   (t  "")))

I tested it and it works both with a note (returning the title from the front matter) and with a binary file (returning the title from the file name).

protesilaos added a commit that referenced this issue Feb 29, 2024
…matter

Thanks to Alan Schmitt for reporting the relevant issue with PDFs and
for commenting on the code I suggested. This was done in issue 261:
<#261>.
@protesilaos
Copy link
Owner

Done, thank you!

@brabalan
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants