branch: externals/denote commit cab563803c7a73c5d4935e6cb2efd36d342e7815 Merge: 621a721468 5d9f1992e9 Author: Protesilaos Stavrou <i...@protesilaos.com> Commit: GitHub <nore...@github.com>
Merge pull request #100 from jeanphilippegg/filetype-heuristics Fix denote--filetype-heuristics for md files when no key regexps match --- denote.el | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/denote.el b/denote.el index 780081a4dd..f310a81e17 100644 --- a/denote.el +++ b/denote.el @@ -665,22 +665,22 @@ identifier: %s :keywords-key-regexp "^#\\+filetags\\s-*:" :keywords-value-function denote--format-keywords-for-org-front-matter :keywords-value-reverse-function denote--extract-keywords-from-front-matter) - (markdown-toml + (markdown-yaml :extension ".md" - :front-matter ,denote-toml-front-matter - :title-key-regexp "^title\\s-*=" + :front-matter ,denote-yaml-front-matter + :title-key-regexp "^title\\s-*:" :title-value-function denote--surround-with-quotes :title-value-reverse-function denote--trim-whitespace-then-quotes - :keywords-key-regexp "^tags\\s-*=" + :keywords-key-regexp "^tags\\s-*:" :keywords-value-function denote--format-keywords-for-md-front-matter :keywords-value-reverse-function denote--extract-keywords-from-front-matter) - (markdown-yaml + (markdown-toml :extension ".md" - :front-matter ,denote-yaml-front-matter - :title-key-regexp "^title\\s-*:" + :front-matter ,denote-toml-front-matter + :title-key-regexp "^title\\s-*=" :title-value-function denote--surround-with-quotes :title-value-reverse-function denote--trim-whitespace-then-quotes - :keywords-key-regexp "^tags\\s-*:" + :keywords-key-regexp "^tags\\s-*=" :keywords-value-function denote--format-keywords-for-md-front-matter :keywords-value-reverse-function denote--extract-keywords-from-front-matter) (text @@ -1263,23 +1263,29 @@ See the format of `denote-file-types'." (defun denote--filetype-heuristics (file) "Return likely file type of FILE. Use the file extension to detect the file type of the file. -If more than one file type correspond to this file extension, -use the first file type for which the key-title-kegexp matches -in the file. -Else, if nothing works, the file type is assumed to be the first -in `denote-file-types'." + +If more than one file type correspond to this file extension, use +the first file type for which the key-title-kegexp matches in the +file or, if none matches, use the first type with this file +extension in `denote-file-type'. + +If no file types in `denote-file-types' has the file extension, +the file type is assumed to be the first of `denote-file-types'." (let* ((file-type) (extension (file-name-extension file t)) (types (denote--file-types-with-extension extension))) - (if (= (length types) 1) - (setq file-type (caar types)) - (when-let ((found-type (seq-find - (lambda (type) - (denote--regexp-in-file-p (plist-get (cdr type) :title-key-regexp) file)) - types))) - (setq file-type (car found-type)))) - (unless file-type - (setq file-type (caar denote-file-types))) + (cond ((not types) + (setq file-type (caar denote-file-types))) + ((= (length types) 1) + (setq file-type (caar types))) + (t + (if-let ((found-type + (seq-find + (lambda (type) + (denote--regexp-in-file-p (plist-get (cdr type) :title-key-regexp) file)) + types))) + (setq file-type (car found-type)) + (setq file-type (caar types))))) file-type)) (defun denote--file-attributes-time (file)