[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 23:38:30 -0500 |
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)