From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id fdNuGw4+V2BOMwAA0tVLHw (envelope-from ) for ; Sun, 21 Mar 2021 12:37:34 +0000 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id cLTEFg4+V2BLEQAAB5/wlQ (envelope-from ) for ; Sun, 21 Mar 2021 12:37:34 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 7585D26677 for ; Sun, 21 Mar 2021 13:37:33 +0100 (CET) Received: from localhost ([::1]:52644 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lNxKk-0004G3-UJ for larch@yhetil.org; Sun, 21 Mar 2021 08:37:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42364) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lNxKP-0004Eg-Ap for emacs-orgmode@gnu.org; Sun, 21 Mar 2021 08:37:09 -0400 Received: from ciao.gmane.io ([116.202.254.214]:46562) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lNxKN-0008RM-KI for emacs-orgmode@gnu.org; Sun, 21 Mar 2021 08:37:09 -0400 Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1lNxKL-00062Y-H1 for emacs-orgmode@gnu.org; Sun, 21 Mar 2021 13:37:05 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: emacs-orgmode@gnu.org From: Maxim Nikulin Subject: Re: greedy substitution in org-open-file Date: Sun, 21 Mar 2021 19:36:58 +0700 Message-ID: References: <874kih92nb.fsf@kyleam.com> <87mtw8fupl.fsf@kyleam.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------288480AB3FFEF21195174A18" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 In-Reply-To: <87mtw8fupl.fsf@kyleam.com> Content-Language: en-US Received-SPF: pass client-ip=116.202.254.214; envelope-from=geo-emacs-orgmode@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: 28 X-Spam_score: 2.8 X-Spam_bar: ++ X-Spam_report: (2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_ADSP_CUSTOM_MED=0.001, FORGED_GMAIL_RCVD=1, FORGED_MUA_MOZILLA=2.309, FREEMAIL_FORGED_FROMDOMAIN=0.25, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, NICE_REPLY_A=-0.001, NML_ADSP_CUSTOM_MED=0.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1616330253; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=uDesQn+W9IDr9vKAQNSLkKHEHNvu4CZIyCI3FJJELfo=; b=KisOlRlrThtl75PY/ckk4a4zUxIbBTkk9Z94F/a9rXdojax2chgDYnRKCGLUZZM3BPe48n x/hNEnO6ChD5pTX3tPtrmSrTC6Pm5Gqsgw7kKQz/IOSnPe6ZdKH5+7XPA0eh9/wnV0VYXc +MEd306Os6UH+lE52skMsVwjGvIab4VOCxEaQ77iSP3ex6WiKYA3+sxdgK97+9ZxJQiaph 8KEYm7PAO9ktqhaCyLd57JlMX6Xya11Q3Km43qygCMHI+n6h2uTPXVxiHOn2SdghwxPELp tjwayfQyz33B7V2r6lWXdtp103XIFBdgd3BGhYvp5iDV50LnnpgpoYiZojLBRA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1616330253; a=rsa-sha256; cv=none; b=WZdKwr/J8nOwiOlS1hxQS72eBOfZAXZWV0MHNR1bwqre/tOAiSvEtsphO3oBKzPExlSd4T Xf21Ycxmp6LB95Q0gvUvSq50G+YaxCJPtoxSB+fqZGS9yu6VSnOBq12jFF67i/CF4V7c3y aaurKmUTg5RWmgvuODQBJpG9Q54y9wzdL/bDzNwcWPltsp+DDJ5AP3Jx3dvHdVIfJYaCZL Wqq+YLB0hZXZcQLlvw0Y/V/QgQ4BdybqjphaMI/hXd/VvIdTKxr53XMWgNHFfk7iPuqt0K gTE0qYCl+eHB/R8Akz97QGMYFHJckvqIJZkAqucTwOVS6vdL0NbinYnczs++gQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Spam-Score: -1.82 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: 7585D26677 X-Spam-Score: -1.82 X-Migadu-Scanner: scn0.migadu.com X-TUID: z8HXWF7r4EXg This is a multi-part message in MIME format. --------------288480AB3FFEF21195174A18 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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%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. --------------288480AB3FFEF21195174A18 Content-Type: text/x-patch; charset=UTF-8; name="test-org--open-file-format-spec.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="test-org--open-file-format-spec.patch" 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 %’" 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 %1 %s’" text))))) + (provide 'test-org) ;;; test-org.el ends here --------------288480AB3FFEF21195174A18--