emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Maxim Nikulin <manikulin@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: Org mode links: Open a PDF file at a given page and highlight a given string
Date: Fri, 5 Mar 2021 20:02:37 +0700	[thread overview]
Message-ID: <s1ta5j$phs$1@ciao.gmane.io> (raw)
In-Reply-To: <87sg5cb4ki.fsf@posteo.net>

On 03/03/2021 23:11, Juan Manuel Macías wrote:
> Maxim Nikulin writes:
>> Please, do not forget to pass stings coming from user input through
>> shell-quote-argument.
> So, maybe it would look better like this (`start-process' instead of
> `start-process-shell-command')?:

My intention was just to warn you against of opening a door
to shell injections.

Personally, as a workaround I would add an org-file-apps entry with
a single argument combining page and search string and would write
a tiny script that splits such argument and invokes the viewer.
I consider it as a better option in the sense of forward compatibility
since it allows to avoid custom link types. I expect that the bug
will be fixed soon.

I still suppose that it is serious limitation that such link format
does not support named link targets inside PDF files. Maybe the part
before second "::" could be considered as a named target
if it does not look like a number. I am unsure concerning search
strings containing "::", they may require more accurate regexp
or using e.g. percent encoding as in URLs.

> #+begin_src emacs-lisp
> (org-link-set-parameters
>   "pdf-pag"
>   :follow (lambda (path)
> 	   (let ((pag (if (string-match "::\\([1-9]+\\):*:*\\(.*\\)" path)
> 			  (format "--page=%s" (match-string 1 path))
> 			(error "no pages")))
> 		 (clean-path (expand-file-name (replace-regexp-in-string "::.+" "" path)))
> 		 (str (when (string-match "::\\([1-9]+\\)::\\(.+\\)" path)
> 			(format "--find=%s" (match-string 2 path)))))
> 	     (if str
> 		 (start-process "zathura" nil "/usr/bin/zathura"
> 				clean-path
> 				pag
> 				str)
> 	       (start-process "zathura" nil "/usr/bin/zathura"
> 			      clean-path
> 			      pag)))))
> #+end_src

If your are asking my opinion on your function, I think that the
variant with start-process is a better one. There is a low level
alternative make-process but it requires more code, so it is less
convenient. As to the style of lisp code, I am not a proper
person to make suggestions.

I suspect that your function has a problem with page numbers like 10.

I do not like repetition of the regexp and tend to think that minor
variations are unintended. On the other hand a variant I could offer
is not shorter despite just one regexp and just one call of a
match function.

#+begin_src elisp
   (defun wa-pdf-destination-zathura-args (target)
     (let ((suffix (string-match 
       (if (not suffix)
	  (list (expand-file-name target))
	(let* ((invalid (match-string 3 target))
	       (file (cond
		      ((zerop suffix) (error "No file path in '%s'" target))
		      (invalid (error "Invalid destination within file: '%s'"
		      (t (substring target 0 suffix))))
	       (page (match-string 1 target))
	       (search (match-string 2 target)))
	  (seq-remove #'null
		      (list (and page "--page") page
			    (and search "--find") search
			    (expand-file-name file)))))))

   (defun wa-launch-pdf-viewer (target)
     (let ((viewer "zathura")
	  (command (wa-pdf-destination-zathura-args target))
	  ;; Do not allocate a pty. Really required only if the application
	  ;; spawns background children and exits (xdg-open, gio open,
	  ;; kde-open5), see Emacs Bug #44824.
	  (process-connection-type nil))
       (apply #'start-process viewer "*Messages*" viewer command)))

#+begin_src elisp :results value list
   (mapcar #'wa-pdf-destination-zathura-args
	    ;; "::"
	    ;; "::95"
	    ;; "file.pdf::a"
	    ;; "file.pdf::95:do"

  reply	other threads:[~2021-03-05 13:03 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-02 20:07 Org mode links: Open a PDF file at a given page and highlight a given string Rodrigo Morales
2021-03-02 22:36 ` Kyle Meyer
2021-03-03 12:37   ` Maxim Nikulin
2021-09-27 16:39   ` Max Nikulin
2021-03-03  2:31 ` Juan Manuel Macías
2021-03-03 14:51   ` Maxim Nikulin
2021-03-03 16:11     ` Juan Manuel Macías
2021-03-05 13:02       ` Maxim Nikulin [this message]
2022-09-03 13:00 ` Max Nikulin
2022-09-20 11:54   ` Ihor Radchenko
2022-09-20 17:03     ` Max Nikulin
2022-09-21  8:17       ` Ihor Radchenko
2022-09-21  8:18       ` Ihor Radchenko
2023-01-25 11:46         ` AW
2023-01-25 11:55           ` Ihor Radchenko
2023-01-25 12:35           ` Max Nikulin
2023-01-25 14:33           ` Jean Louis
2022-10-04  6:45 ` 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:

  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='s1ta5j$phs$1@ciao.gmane.io' \
    --to=manikulin@gmail.com \
    --cc=emacs-orgmode@gnu.org \


* 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


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).