branch: externals/denote commit c88d9daaf1ee3c577c5c99e4cc8dc38d771e0aae Author: Jean-Philippe Gagné Guay <jeanphilippe...@gmail.com> Commit: Jean-Philippe Gagné Guay <jeanphilippe...@gmail.com>
Fix issue in denote-retrieve.el and refactor the code. When a note title ended with a ")" (or other special characters), denote-retrieve--value-title did not return the last character. The regexps have been modified to focus only on the key part of the line. The value part is easy to retrieve once the key is matched. We just need to trim (of spaces and quotes) the remainder of the line. denote-retrieve--value has been removed. denote-retrieve--search is doing all the work. Given a KEY-REGEXP, it retrieves the value part of the matching line. With optional KEY argument, it retrieves the key part instead. --- denote-dired.el | 2 +- denote-retrieve.el | 80 ++++++++++++++++++++++++------------------------------ 2 files changed, 37 insertions(+), 45 deletions(-) diff --git a/denote-dired.el b/denote-dired.el index 4e20b5aa47..9da31ce205 100644 --- a/denote-dired.el +++ b/denote-dired.el @@ -284,7 +284,7 @@ TITLE, DATE, KEYWORDS, FILENAME, ID, and FILETYPE are all strings "Return likely file type of FILE. The return value is for `denote--file-meta-header'." (pcase (file-name-extension file) - ("md" (if (string-match-p "title\\s-*=" (denote-retrieve--value-title file 0)) + ("md" (if (string-match-p "title\\s-*=" (denote-retrieve--value-title file t)) 'markdown-toml 'markdown-yaml)) ("txt" 'text) diff --git a/denote-retrieve.el b/denote-retrieve.el index 22c46f9366..3d63b2c8b9 100644 --- a/denote-retrieve.el +++ b/denote-retrieve.el @@ -32,23 +32,17 @@ (require 'denote) (require 'xref) -(defconst denote-retrieve--title-front-matter-regexp - "^\\(?:#\\+\\)?\\(?:title\\)\\s-*[:=]\\s-*[\"']?\\(?1:.*\\b\\)[\"']?" - "Regular expression for title key and value. -The match that needs to be extracted is explicityly marked as -group 1.") - -(defconst denote-retrieve--id-front-matter-regexp - "^.?.?\\b\\(?:identifier\\|[Ii][Dd]\\)\\s-*[:=]\\s-*[\"']?\\(?1:[0-9T]+\\)[\"']?" - "Regular expression for identifier key and value. -The match that needs to be extracted is explicityly marked as -group 1.") - -(defconst denote-retrieve--date-front-matter-regexp - "^\\(?:#\\+\\)?\\(?:date\\)\\s-*[:=]\\s-*[\"']?\\(?1:.*\\b]?\\)[\"']?" - "Regular expression for date key and value. -The match that needs to be extracted is explicityly marked as -group 1.") +(defconst denote-retrieve--title-front-matter-key-regexp + "^\\(?:#\\+\\)?\\(?:title\\)\\s-*[:=]" + "Regular expression for title key.") + +(defconst denote-retrieve--id-front-matter-key-regexp + "^.?.?\\b\\(?:identifier\\|[Ii][Dd]\\)\\s-*[:=]" + "Regular expression for identifier key.") + +(defconst denote-retrieve--date-front-matter-key-regexp + "^\\(?:#\\+\\)?\\(?:date\\)\\s-*[:=]" + "Regular expression for date key.") (defun denote-retrieve--filename-identifier (file) "Extract identifier from FILE name." @@ -58,35 +52,33 @@ group 1.") (match-string 0 file)) (error "Cannot find `%s' as a file" file))) -(defun denote-retrieve--search (regexp &optional group) - "Search for REGEXP in the current buffer. -With optional GROUP match it, else match group 1." - (save-excursion - (save-restriction - (widen) - (goto-char (point-min)) - (re-search-forward regexp nil t 1) - (unless (eq (point) (point-min)) - (match-string-no-properties (or group 1)))))) - -(defun denote-retrieve--value (file regexp &optional group) - "Return REGEXP value from FILE. -FILE is a note in the variable `denote-directory'. - -Optional GROUP is a regexp construct for -`denote-retrieve--search'." +(defun denote-retrieve--search (file key-regexp &optional key) + "Return the value associated with the KEY-REGEXP key in the +current buffer from FILE. +If optional KEY is non-nil, return the key instead." (with-temp-buffer (insert-file-contents file) - (or (denote-retrieve--search regexp group) - nil))) - -(defun denote-retrieve--value-title (file &optional group) - "Return title from FILE, optionally matching regexp GROUP." - (denote-retrieve--value file denote-retrieve--title-front-matter-regexp group)) - -(defun denote-retrieve--value-date (file &optional group) - "Return date from FILE, optionally matching regexp GROUP." - (denote-retrieve--value file denote-retrieve--date-front-matter-regexp group)) + (save-excursion + (save-restriction + (widen) + (goto-char (point-min)) + (when (re-search-forward key-regexp nil t 1) + (if key + (match-string-no-properties 0) + (let ((trims "[ \t\n\r\"']+")) + (string-trim + (buffer-substring-no-properties (point) (point-at-eol)) + trims trims)))))))) + +(defun denote-retrieve--value-title (file &optional key) + "Return title value from FILE. +If optional KEY is non-nil, return the key instead." + (denote-retrieve--search file denote-retrieve--title-front-matter-key-regexp key)) + +(defun denote-retrieve--value-date (file &optional key) + "Return date value from FILE. +If optional KEY is non-nil, return the key instead." + (denote-retrieve--search file denote-retrieve--date-front-matter-key-regexp key)) (defun denote-retrieve--read-file-prompt () "Prompt for regular file in variable `denote-directory'."