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.
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index 78cd29576..b6e42dc99 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -8236,6 +8236,72 @@ two
(call-interactively #'org-paste-subtree)
(buffer-string)))))
+(ert-deftest org-test/org--open-file-format-spec ()
+ "Test `org-open-file' helper `org--open-file-format-spec'."
+ (let ((def-spec '(("s" . "file.pdf") ("1" . "10") ("2" . "pattern"))))
+ (should (equal "zathura --page 10 --find pattern file.pdf"
+ (org--open-file-format-spec
+ "zathura --page %1 --find %2 %s" def-spec)))
+ (should (equal "no subst"
+ (org--open-file-format-spec
+ "no subst" def-spec)))
+ (should (equal "simple file.pdf"
+ (org--open-file-format-spec
+ "simple %s" def-spec)))
+ (should (equal "with --page 10 file.pdf"
+ (org--open-file-format-spec
+ "with --page %1 %s" def-spec)))
+ (should (equal "file.pdf at start"
+ (org--open-file-format-spec
+ "%s at start" def-spec)))
+ (should (equal "in the file.pdf middle"
+ (org--open-file-format-spec
+ "in the %s middle" def-spec)))
+ (should (equal "literal %"
+ (org--open-file-format-spec
+ "literal %%" def-spec)))
+ (should (equal "literal %s in the middle"
+ (org--open-file-format-spec
+ "literal %%s in the middle" def-spec)))
+ (should (equal "% literal at start"
+ (org--open-file-format-spec
+ "%% literal at start" def-spec)))
+ (should (equal "" (org--open-file-format-spec "" def-spec)))
+ (should (equal "adjucent 10file.pdf substitutions"
+ (org--open-file-format-spec
+ "adjucent %1%s substitutions" def-spec))))
+
+ (should (equal "many -f first -s second -t third -e eigth file.pdf"
+ (org--open-file-format-spec
+ "many -f %1 -s %2 -t %3 -e %8 %s"
+ '(("1" . "first") ("2" . "second") ("3" . "third")
+ ("4" . "fourth") ("5" . "firth") ("6" . "sixth")
+ ("7" . "seventh") ("8" . "eigth") ("s" . "file.pdf")))))
+
+ ;; I am afraid to add recursive substitutions like ("%s" . "recursive %s").
+ (should (equal "no-recursion-with file-%1.pdf --page 10"
+ (org--open-file-format-spec
+ "no-recursion-with %s --page %1"
+ '(("s" . "file-%1.pdf") ("1" . "10")))))
+ (should (equal "no-recursion-with --search printf-%s file.pdf"
+ (org--open-file-format-spec
+ "no-recursion-with --search %1 %s"
+ '(("s" . "file.pdf") ("1" . "printf-%s")))))
+
+ (let* ((err
+ (should-error (org--open-file-format-spec "invalid-end %" ())
+ :type 'error))
+ (text (cadr err)))
+ (should (and (stringp text)
+ (string-match-p "’invalid-end <ERROR ->%’" text))))
+ (let* ((err
+ (should-error (org--open-file-format-spec "missed --subst %1 %s"
+ '(("s" . "file.pdf")))
+ :type 'error))
+ (text (cadr err)))
+ (should (and (stringp text)
+ (string-match-p "’missed --subst <ERROR ->%1 %s’" text)))))
+
(provide 'test-org)
;;; test-org.el ends here