emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Maxim Nikulin <manikulin@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: greedy substitution in org-open-file
Date: Sun, 21 Mar 2021 19:36:58 +0700	[thread overview]
Message-ID: <s37elb$m2v$1@ciao.gmane.io> (raw)
In-Reply-To: <87mtw8fupl.fsf@kyleam.com>

[-- Attachment #1: Type: text/plain, Size: 3465 bytes --]

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.

[-- Attachment #2: test-org--open-file-format-spec.patch --]
[-- Type: text/x-patch, Size: 3006 bytes --]

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

      parent reply	other threads:[~2021-03-21 12:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-20 16:08 Maxim Nikulin
2021-02-12  7:16 ` Kyle Meyer
2021-02-12 16:46   ` Maxim Nikulin
2021-02-13  4:38     ` Kyle Meyer
2021-02-15 17:04       ` Maxim Nikulin
2021-03-03 12:47       ` Maxim Nikulin
2021-03-21 12:36       ` Maxim Nikulin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='s37elb$m2v$1@ciao.gmane.io' \
    --to=manikulin@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --subject='Re: greedy substitution in org-open-file' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).