emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: greedy substitution in org-open-file


From: Kyle Meyer
Subject: Re: greedy substitution in org-open-file
Date: Fri, 12 Feb 2021 02:16:40 -0500

Maxim Nikulin writes:

> Looking into the code related to 'pty problem with 
> start-process-shell-command, I have realized that the following case is 
> not handled correctly:
>
> #+begin_src elisp
>    (setq org-file-apps '(("\\.pdf::\\([0-9]+\\)\\'" . "xpdf %s %1")))
> #+end_src

Not relevant for the underlying issue, but doesn't xpdf require a colon
before the page number (i.e. ":%1")?

> I hope, I adapted an example from [[help:org-file-apps]] correctly. When 
> I try to open the following link using C-c C-o
>
> [[file:test-%1f.pdf::42]]
>
> I get
>
> Running xpdf /home/ubuntu/examples/org/test-\42f.pdf 42...done
>
> I believe, it should be
>
> Running xpdf /home/ubuntu/examples/org/test-%1f.pdf 42...done
>
> Or
>
> Running xpdf /home/ubuntu/examples/org/test-\%1f.pdf 42...done

Yep, it should show up as "Running xpdf /tmp/test-\%1f.pdf :42...done",
with the backslash coming from shell-quote-argument.

> The source of the problem is that %s substitution is expanded at first 
> and regexp groups are replaced later. Ideally, it should be performed in 
> a single pass. I have found format-spec function but I am unsure 
> concerning it, maybe it is avoided intentionally.

I believe format-spec requires the placeholder to be A-z:

  (format-spec "xpdf %s" '((?s . "a")))  => "xpdf a"
  (format-spec "xpdf %s %1" '((?s . "a") (?1 . "b")))  ;; Invalid format string

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.

diff --git a/lisp/org.el b/lisp/org.el
index c61b8fb56..31dbe352a 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -8714,12 +8714,6 @@ (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)
@@ -8731,7 +8725,11 @@ (defun org-open-file (path &optional in-emacs line 
search)
              (while (string-match regex cmd)
                (setq cmd (replace-match replace-with t t cmd))))
            (setq match-index (+ match-index 1)))))
-
+      (setq cmd (replace-regexp-in-string
+                "%s"
+                (shell-quote-argument (convert-standard-filename file))
+                cmd
+                nil t))
       (save-window-excursion
        (message "Running %s...done" cmd)
        (start-process-shell-command cmd nil cmd)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]