emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: Maxim Nikulin <manikulin@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: greedy substitution in org-open-file
Date: Fri, 12 Feb 2021 23:38:30 -0500	[thread overview]
Message-ID: <87mtw8fupl.fsf@kyleam.com> (raw)
In-Reply-To: <s06bcc$uqt$1@ciao.gmane.io>

Maxim Nikulin writes:

> On 12/02/2021 14:16, Kyle Meyer wrote:

>> Not relevant for the underlying issue, but doesn't xpdf require a colon
>> before the page number (i.e. ":%1")?
>
> At least for the application in debian & ubuntu xpdf package, page 
> number should be specified without a colon. It is Xt interface to 
> poppler PDF library, recently its maintainer decided to switch to 
> xpopple project as upstream. UI is derived from old version of xpdf. 
> Latest original xpdf version is based on Qt and might have different 
> convention in respect to page numbers.

Okay.  Fwiw the xpdf version I have that requires ":" before the page is
4.03.

>> What about flipping the processing, handling the %N placeholders first
>> and then formatting the file name?  Seems to work on my end, though I
>> haven't tested it thoroughly.
>
> I could anticipate similar problems if named destinations are involved. 
> I have not checked but I expect that internal links might have "%s" in 
> their names at least for some file types.

Indeed, flipping the order unsurprisingly just flips which placeholders
can be problematic.  A very contrived example:

  (setq org-file-apps
        '(("\\.pdf::\\([A-z%]+\\)\\'" . "doesntmatter %s %1")))

  ;; file:/tmp/test.pdf::a%sb =>
  ;; Running doesntmatter /tmp/test.pdf a/tmp/test.pdfb...done

> That is why I would strongly prefer substitutions performed in a
> single pass. I do not like it, but it seems that simplified variant of
> format-spec is better. It should allows substitutions with digit. I
> hope, single digit should be enough.

True, realistically I don't think anyone has a command in org-file-apps
that relies on more than a couple of capture groups.  All right, here's
a format-spec-inspired fix.  At the very least it needs doc updates and
a comment or two.

diff --git a/lisp/org.el b/lisp/org.el
index 5b1443c4e..e8f60fd83 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -8644,6 +8644,23 @@ (defun org--file-apps-regexp-alist (list &optional add-auto-mode)
    (when add-auto-mode
      (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
 
+(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)))
+            (t
+             (error "Invalid format string"))))
+    (buffer-string)))
+
 ;;;###autoload
 (defun org-open-file (path &optional in-emacs line search)
   "Open the file at PATH.
@@ -8745,24 +8762,20 @@ (defun org-open-file (path &optional in-emacs line search)
       ;; Remove quotes around the file name - we'll use shell-quote-argument.
       (while (string-match "['\"]%s['\"]" cmd)
 	(setq cmd (replace-match "%s" t t cmd)))
-      (setq cmd (replace-regexp-in-string
-		 "%s"
-		 (shell-quote-argument (convert-standard-filename file))
-		 cmd
-		 nil t))
-
-      ;; Replace "%1", "%2" etc. in command with group matches from regex
-      (save-match-data
-	(let ((match-index 1)
-	      (number-of-groups (- (/ (length link-match-data) 2) 1)))
-	  (set-match-data link-match-data)
-	  (while (<= match-index number-of-groups)
-	    (let ((regex (concat "%" (number-to-string match-index)))
-		  (replace-with (match-string match-index dlink)))
-	      (while (string-match regex cmd)
-		(setq cmd (replace-match replace-with t t cmd))))
-	    (setq match-index (+ match-index 1)))))
-
+      (setq cmd
+            (org--open-file-format-spec
+             cmd
+             (cons
+              (cons "s" (shell-quote-argument
+                         (convert-standard-filename file)))
+              (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))))))))
       (save-window-excursion
 	(message "Running %s...done" cmd)
 	(start-process-shell-command cmd nil cmd)


  reply	other threads:[~2021-02-13  4:39 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 [this message]
2021-02-15 17:04       ` Maxim Nikulin
2021-03-03 12:47       ` Maxim Nikulin
2021-03-21 12:36       ` Maxim Nikulin

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=87mtw8fupl.fsf@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=manikulin@gmail.com \
    --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).