From: Maxim Nikulin <manikulin@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: greedy substitution in org-open-file
Date: Tue, 16 Feb 2021 00:04:57 +0700 [thread overview]
Message-ID: <s0e9jr$eve$1@ciao.gmane.io> (raw)
In-Reply-To: <87mtw8fupl.fsf@kyleam.com>
On 13/02/2021 11:38, Kyle Meyer wrote:
>
> All right, here's
> a format-spec-inspired fix. At the very least it needs doc updates and
> a comment or two.
Thank you. I am hardly familiar with elisp so it would be difficult for
me to express the same. My comments are mostly a matter of taste.
Sorry, I have not tried to run the code yet.
> 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]")
Personally I do not see a reason to limit substitutions just to just
"%s". I would consider "[a-zA-Z0-9]".
> + (replace-match
> + (or (cdr (assoc (match-string 0) specification))
> + (error "Invalid format string"))
I think, substitution character in the error message could be useful
during debugging, something like (format "Invalid format character %%%s"
(match-string 0)).
> + '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)))
Should not be numbered substitutions passed through shell-quote-argument
as well? Besides just page numbers the link could be named internal
anchor where more characters are allowed. It is the reason why in
general I prefer bare exec to shell commands.
I am unsure concerning optional matches as
"\\(?:\\.pdf\\)\\(?:::\\([0-9]+\\)\\)?\\'"
Maybe they should not be used at all in favor of separate entries with
and without page number. Maybe nil should coalesce to empty string "".
Certainly I am against "nil" string for a missed group. By the way, in
the original format-spec, cdr is applied after the check whether assoc
value is nil.
> + (number-sequence 1 ngroups))))))))
> (save-window-excursion
> (message "Running %s...done" cmd)
> (start-process-shell-command cmd nil cmd)
next prev parent reply other threads:[~2021-02-15 17:10 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-20 16:08 greedy substitution in org-open-file Maxim Nikulin
2021-02-12 7:16 ` Kyle Meyer
2021-02-12 16:46 ` Maxim Nikulin
2021-02-13 4:38 ` Kyle Meyer
2021-02-15 17:04 ` Maxim Nikulin [this message]
2021-03-03 12:47 ` Maxim Nikulin
2021-03-21 12:36 ` Maxim Nikulin
2022-08-27 17:20 ` [PATCH] org.el: Fix percent substitutions in `org-open-file' Max Nikulin
2022-09-02 12:08 ` Ihor Radchenko
2022-09-02 15:41 ` Max Nikulin
2022-09-03 8:26 ` Ihor Radchenko
2022-09-04 12:16 ` [PATCH v2] " Max Nikulin
2022-09-05 5:46 ` Ihor Radchenko
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='s0e9jr$eve$1@ciao.gmane.io' \
--to=manikulin@gmail.com \
--cc=emacs-orgmode@gnu.org \
/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
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public 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).