From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:c151::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id paj0DScsQmBkaAAA0tVLHw (envelope-from ) for ; Fri, 05 Mar 2021 13:03:35 +0000 Received: from aspmx2.migadu.com ([2001:41d0:2:c151::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id aJ0jCScsQmCcNQAAbx9fmQ (envelope-from ) for ; Fri, 05 Mar 2021 13:03:35 +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 aspmx2.migadu.com (Postfix) with ESMTPS id 9CBC41A950 for ; Fri, 5 Mar 2021 14:03:34 +0100 (CET) Received: from localhost ([::1]:59356 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lIA7B-0005Ab-RY for larch@yhetil.org; Fri, 05 Mar 2021 08:03:33 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:50446) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lIA6X-000592-Bi for emacs-orgmode@gnu.org; Fri, 05 Mar 2021 08:02:53 -0500 Received: from ciao.gmane.io ([116.202.254.214]:46238) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lIA6V-0007zs-SX for emacs-orgmode@gnu.org; Fri, 05 Mar 2021 08:02:53 -0500 Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1lIA6T-000724-6h for emacs-orgmode@gnu.org; Fri, 05 Mar 2021 14:02:49 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: emacs-orgmode@gnu.org From: Maxim Nikulin 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 Message-ID: References: <87lfb5pbej.fsf@gmail.com> <87lfb5c6j0.fsf@posteo.net> <87sg5cb4ki.fsf@posteo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 In-Reply-To: <87sg5cb4ki.fsf@posteo.net> Content-Language: en-US Received-SPF: pass client-ip=116.202.254.214; envelope-from=geo-emacs-orgmode@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: 28 X-Spam_score: 2.8 X-Spam_bar: ++ X-Spam_report: (2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_ADSP_CUSTOM_MED=0.001, FORGED_GMAIL_RCVD=1, FORGED_MUA_MOZILLA=2.309, FREEMAIL_FORGED_FROMDOMAIN=0.25, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, NICE_REPLY_A=-0.001, NML_ADSP_CUSTOM_MED=0.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -1.76 Authentication-Results: aspmx2.migadu.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=gmail.com (policy=none); spf=pass (aspmx2.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: 9CBC41A950 X-Spam-Score: -1.76 X-Migadu-Scanner: scn0.migadu.com X-TUID: HBQDE+MtK53s 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 "::\\(?:\\([0-9]+\\)?\\(?:::\\(.+\\)\\)?\\|\\(.*\\)\\)$" target))) (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'" invalid)) (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))) #+end_src #+begin_src elisp :results value list (mapcar #'wa-pdf-destination-zathura-args '( "~/Download/grub.pdf::95::do" "file.pdf::95" "file.pdf::::do" "file.pdf" ;; "::" ;; "::95" ;; "file.pdf::a" ;; "file.pdf::95:do" )) #+end_src