[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: greedy substitution in org-open-file
From: |
Maxim Nikulin |
Subject: |
Re: greedy substitution in org-open-file |
Date: |
Sun, 21 Mar 2021 19:36:58 +0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 |
On 13/02/2021 11:38, Kyle Meyer wrote:
+(defun org--open-file-format-spec (format specification)
+ (with-temp-buffer
+ (insert format)
+ (goto-char (point-min))
+ (while (search-forward "%" nil t)
+ (cond ((eq (char-after) ?%)
+ (delete-char 1))
+ ((looking-at "[s0-9]")
+ (replace-match
+ (or (cdr (assoc (match-string 0) specification))
+ (error "Invalid format string"))
+ 'fixed-case 'literal)
+ (delete-region (1- (match-beginning 0)) (match-beginning 0)))
Finally I managed to convince myself that delete-region does not change
position in the buffer, so "%s" or "%1" in specification are not a
problem. I am aware that this implementation is a simplified version of
format-spec, but I am still unsure if jumping over a buffer is the best
choice.
I am in doubts if strings or characters should be used in the spec:
'(("s" . "file.pdf")) vs. '((?s . "file.pdf")), but really it does not
matter.
I have created some tests for this function, see the attachment.
Actually I do not like such style of tests since first failure stops
whole test and it is hard to get general impression to which degree the
function under the test is broken, but I am not aware of a better way.
Recently I asked Ihor a similar question:
https://orgmode.org/list/s324b0$74g$1@ciao.gmane.io
I know that functions called from one point are not in favor in org
sources, but I do not mind to have additional helper function to add
tests that all substitutions are properly escaped.
+ (let ((ngroups (- (/ (length link-match-data) 2) 1)))
+ (and (> ngroups 0)
+ (progn
+ (set-match-data link-match-data)
+ (mapcar (lambda (n)
+ (cons (number-to-string n)
+ (match-string-no-properties n dlink)))
+ (number-sequence 1 ngroups))))))))
Matter of taste: it seems that with (number-sequence 1 ngroups 1)) it is
possible to avoid (> ngroups 0).
I have spent some time evaluating how to make errors more helpful to
users. I am unsure if multiline message is acceptable to dump content of
specification. For a while, a place in the format where error has
happened (combined with a different approach to parse format string)
(defun org--open-file-format-spec (format specification)
(apply #'concat
(nreverse
(let ((result nil)
(token-end 0))
(while (string-match "%\\(.?\\)" format token-end)
(let ((token-start (match-beginning 0))
(subst (match-string-no-properties 1 format)))
(push (substring format token-end token-start) result)
(push (if (equal subst "%")
"%"
(or (cdr (assoc subst specification))
(error "Unknown substitution: '%s<ERROR ->%s'"
(substring format 0 token-start)
(substring format token-start nil))))
result))
(setq token-end (match-end 0)))
(push (substring format token-end nil) result)))))
To my surprise neither ^ nor \\` in string-match regexp works if
start-pos is not zero.
test-org--open-file-format-spec.patch
Description: Text Data