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 2P1aJApYJ2AaMQAA0tVLHw (envelope-from ) for ; Sat, 13 Feb 2021 04:39:38 +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 aMEQIApYJ2AnTgAAB5/wlQ (envelope-from ) for ; Sat, 13 Feb 2021 04:39:38 +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 CDC9B1BFCA for ; Sat, 13 Feb 2021 05:39:37 +0100 (CET) Received: from localhost ([::1]:41658 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lAmiW-0001ji-Jw for larch@yhetil.org; Fri, 12 Feb 2021 23:39:36 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35886) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lAmhf-0001jT-KW for emacs-orgmode@gnu.org; Fri, 12 Feb 2021 23:38:43 -0500 Received: from out2.migadu.com ([188.165.223.204]:14514) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lAmhZ-0001ci-Kb for emacs-orgmode@gnu.org; Fri, 12 Feb 2021 23:38:43 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kyleam.com; s=key1; t=1613191113; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=sHPDxOuIzR9MrrvsJ14Vg0owGu9Yfz1/yfxcgA9cUk8=; b=Nh3cG+sx329mWuFLBTIw+ROXNZtK9TFyKde1iJIniLfRmF4mGAF3C09KVCyZyaubIUqR6p T6vGOz4eHSSFy1bNrEiguxrqtW7RAAC0fkxV09hv4KPXW8fAIwqO2L+XJQuKmqJm8Ig4hc X+FbA9OtryhMEuLbXmuu6N66S8Z2GfmvWG0NzRR5WiQkuNlrBg7gHjyASG+e08q25jH+sy JR/4G5agLs7Z4jKsUy3WICJLsktBCWaIrxuogcN7O0wO4YDaY0e1bpJqlv5yZkdSuLiht/ JiDVWoui6EqMq6luwhpoJkup0goAGJd00Xodm5WEg+qmosNt/IMd3jp+G7u9fg== From: Kyle Meyer To: Maxim Nikulin Subject: Re: greedy substitution in org-open-file In-Reply-To: References: <874kih92nb.fsf@kyleam.com> Date: Fri, 12 Feb 2021 23:38:30 -0500 Message-ID: <87mtw8fupl.fsf@kyleam.com> MIME-Version: 1.0 Content-Type: text/plain X-Migadu-Auth-User: kyle@kyleam.com Received-SPF: pass client-ip=188.165.223.204; envelope-from=kyle@kyleam.com; helo=out2.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: , Cc: emacs-orgmode@gnu.org Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -2.56 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=kyleam.com header.s=key1 header.b=Nh3cG+sx; dmarc=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: CDC9B1BFCA X-Spam-Score: -2.56 X-Migadu-Scanner: scn0.migadu.com X-TUID: KiF2eog8Sp3E 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)