From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: [RFC] Link-type for attachments, more attach options Date: Tue, 20 Nov 2018 00:52:25 +0100 Message-ID: <877eh8vd52.fsf_-_@nicolasgoaziou.fr> References: <87muqo8o68.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:45659) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gOtLI-0006Ch-BY for emacs-orgmode@gnu.org; Mon, 19 Nov 2018 18:52:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gOtLD-0003VW-JE for emacs-orgmode@gnu.org; Mon, 19 Nov 2018 18:52:36 -0500 Received: from relay10.mail.gandi.net ([217.70.178.230]:35719) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gOtLD-0003Uk-A7 for emacs-orgmode@gnu.org; Mon, 19 Nov 2018 18:52:31 -0500 In-Reply-To: ("Gustav =?utf-8?Q?Wikstr=C3=B6m=22's?= message of "Sat, 17 Nov 2018 20:26:40 +0000") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Gustav =?utf-8?Q?Wikstr=C3=B6m?= Cc: Org Mode List Hello, Gustav Wikstr=C3=B6m writes: > Yeah - I liked "attached" because I prefer clear keywords. But sure, > we can keep it shorter. I'd suggest "@" instead in that case. Patch > updated with that. "@" syntax is a reserved syntax for citations in the "wip-cite" branch. I'd rather not use it here. Also, years ago, "att" link type was used to link to attachments, hence my proposal. >> > When working with ATTACH_DIR there are now a couple of new options ava= ilable: >> > - org-attach-dir-inherit-by-default >>=20 >> What is the difference between this and >> `org-attach-allow-inheritance'? You didn't answer this question, did you? Something is fishy here anyways. Why is "ATTACH_DIR_INHERIT" needed at all? If `org-attach-allow-inheritance', "ATTACH_DIR" should be inherited. Why depend on another property? I'd rather not make these things more complex. >> > - org-attach-dir-create-if-not-exist >>=20 >> What is the use-case for this one? It doesn't seem terribly useful at=20 >> first glance. > > If you try to open attachments on a node where there is no ID or >> ATTACH_DIR, the default behavior is to add an ID and create a folder >> based on that ID. I would like Org-mode to not do that. This >> customization give the user the option to change that. Instead of >> providing this customization one could just change the default >> behavior of org-attach, since it's a bit offensive to create folders >> when I didn't ask for it. But instead of changing the default, >> I thought this way was more graceful. I wouldn't mind skipping this >> customization though, if the behavior was changed to what >> I implemented with org-attach-dir-create-if-not-exist set to nil. Considering attaching is about moving, or copying files somewhere, creating a folder doesn't sound terribly offensive. You don't even have to know the name of the folder. Do you suggest to raise an error if there is no folder available for the attached documents? If so, wouldn't it be better to ask the user first? >> > - org-attach-dir-relative >>=20 >> I'm not sure to understand this one. > > When adding folders to nodes using option "s" in org-attach, the path > is absolute. This makes attachment-directory paths relative to > location of the file instead. OK. >> > +This list shows the full set of built-in external link types: >> > +| http | web | >> > +| https | secure web | >> > +| doi | DOI for electronic resources | >> > +| file | file-links | >> > +| file+sys | file-links forced to open via OS | >> > +| file+emacs | file-links forced to open via emacs | >> > +| attached | links to attachments for nodes | >> > +| docview | doc-view mode | >> > +| id | Link to heading by id | >> > +| news | Usenet link | >> > +| mailto | mail link | >> > +| mhe | MH-E folder link | >> > +| rmail | Rmail link | >> > +| gnus | Gnus link | >> > +| bbdb | BBDB link | >> > +| irc | IRC link | >> > +| info | Info link | >> > +| shell | shell command | >> > +| elisp | interactive elisp command link | >> > + >> > +The following list shows examples for each link type. >> > + >> > +| =3Dhttp://www.astro.uva.nl/=3Ddominik=3D | on the web = | >> > +| =3Ddoi:10.1000/182=3D | DOI for an electron= ic resource | >> > +| =3Dfile:/home/dominik/images/jupiter.jpg=3D | file, absolute path= | >> > +| =3D/home/dominik/images/jupiter.jpg=3D | same as above = | >> > +| =3Dfile:papers/last.pdf=3D | file, relative path= | >> > +| =3D./papers/last.pdf=3D | same as above = | >> > +| =3Dfile:/ssh:me@some.where:papers/last.pdf=3D | file, path on remot= e machine | >> > +| =3D/ssh:me@some.where:papers/last.pdf=3D | same as above = | >> > +| =3Dfile:sometextfile::NNN=3D | file, jump to line = number | >> > +| =3Dfile:projects.org=3D | another Org file = | >> > +| =3Dfile:projects.org::some words=3D | text search in Org = file[fn:28] | >> > +| =3Dfile:projects.org::*task title=3D | heading search in O= rg file | >> > +| =3Dfile+sys:/path/to/file=3D | open via OS, like d= ouble-click | >> > +| =3Dfile+emacs:/path/to/file=3D | force opening by Em= acs | >> > +| =3Dattached:projects.org=3D | file in folder atta= ched to headline | >> > +| =3Dattached:projects.org::some words=3D | text search in atta= ched file | >> > +| =3Ddocview:papers/last.pdf::NNN=3D | open in doc-view mo= de at page | >> > +| =3Did:B7423F4D-2E8A-471B-8810-C40F074717E9=3D | link to heading by = ID | >> > +| =3Dnews:comp.emacs=3D | Usenet link = | >> > +| =3Dmailto:adent@galaxy.net=3D | mail link = | >> > +| =3Dmhe:folder=3D | MH-E folder link = | >> > +| =3Dmhe:folder#id=3D | MH-E message link = | >> > +| =3Drmail:folder=3D | Rmail folder link = | >> > +| =3Drmail:folder#id=3D | Rmail message link = | >> > +| =3Dgnus:group=3D | Gnus group link = | >> > +| =3Dgnus:group#id=3D | Gnus article link = | >> > +| =3Dbbdb:R.*Stallman=3D | BBDB link (with reg= exp) | >> > +| =3Dirc:/irc.com/#emacs/bob=3D | IRC link = | >> > +| =3Dinfo:org#External links=3D | Info node link = | >> > +| =3Dshell:ls *.org=3D | shell command = | >> > +| =3Delisp:org-agenda=3D | interactive Elisp c= ommand | >> > +| =3Delisp:(find-file "Elisp.org")=3D | Elisp form to evalu= ate | >>=20 >> I'm not sure to like the change above. It introduces a lot of=20 >> redundancy. >>=20 > > Currently the list in the documentation is just a bunch of examples. >> I've looked at it a couple of times asking myself "What is the >> complete list of built in link types?". This commit "fixes" that. >> I wouldn't say its redundant since all the rows in the second list >> are just examples. It is still redundant. For example, the first table has | info | Info link | whereas the second one tells us | info:org#External links | Info node link | In this case, | Info link | info:org#External links | would be sufficient enough. I have the feeling documentation can be improved here. Also, file+sys and file+emacs are deprecated. They can be removed. > I can't say I understand the use of :safe here. But added it anyways. > If you care, please explain why it's needed. Package-version is added. > I set it to 9.2. Correct? If :safe is not set, Emacs will warn every time the variable is set as a local file variable. It should be 9.3, not 9.2. >> > +(defun org-attach-open-link (link &optional in-emacs) >> > + "LINK is expanded with the attached directory and opened the same=20 >> > +way as file-links are." >>=20 >> You need to expound the arguments in the docstring. > > Sorry, I don't understand what I'm supposed to do here... I changed > the comment to (maybe?) make it read better. Other than that, I'm at > a loss. Every argument needs to be documented in the docstring. What is LINK type? What is IN-EMACS? >> > + (file-types-re (format "[][]\\[\\(?:file\\|attached\\|[./~]%s\\)" >> > (if (not link-abbrevs) "" >> > (format "\\|\\(?:%s:\\)" >> > (regexp-opt link-abbrevs)))))) >>=20 >> Why is it needed? "attached" link type is already registered, so you=20 >> don't need to also hard-code it here. > > This is when parsing the buffer for images. I couldn't get org-mode to > display images without this. This is still a hack, and clearly not the way to go, IMO. If not already possible, we could add a new parameter in `org-link-parameters' or some such. This is another issue, tho. > +(defcustom org-attach-dir-create-if-not-exists t > + "Choose whether ATTACH_DIR-directories should be created if they do > not exist since before. Too long. Maybe: When non-nil, ATTACH_DIR is created automatically if it doesn't exist. Otherwise, ... > +Default is to create them." > + :group 'org-attach > + :type 'boolean > + :package-version '(Org . "9.2") > + :safe #'booleanp) > + > +(defcustom org-attach-dir-relative nil > + "Choose whether ATTACH_DIR-directories should be added as relative lin= ks or not. Too long. Maybe something like: Non-nil means ATTACH_DIR is relative to the attachment node directory.=20 > +Defaults to not relative." Defaults to absolute location. > + (let ((old (org-attach-dir nil)) > + (new > + (progn > + (if arg (org-entry-delete nil "ATTACH_DIR") > + (let* ((attach-dir (read-directory-name > + "Attachment directory: " > + (org-entry-get nil > + "ATTACH_DIR"))) > + (current-dir (file-name-directory (or default-directory > + buffer-file-name))) > + (attach-dir-relative (file-relative-name attach-dir current-dir))) > + (if org-attach-dir-relative > + (org-entry-put nil "ATTACH_DIR" attach-dir-relative) > + (org-entry-put nil "ATTACH_DIR" attach-dir)))) (org-entry-put nil "ATTACH_DIR" (if org-attach-dir-relative=20 attach-dir-relative attach-dir)) > +(defun org-attach-open-link (link &optional in-emacs) > + "Attach link type LINK is expanded with the attached directory and ope= ned. > +This is done in the same way as file-links are opened." > + (interactive "P") > + (let (line search) > + (if (string-match "::\\([0-9]+\\)\\'" link) > + (setq line (string-to-number (match-string 1 link)) > + link (substring link 0 (match-beginning 0))) > + (if (string-match "::\\(.+\\)\\'" link) > + (setq search (match-string 1 link) > + link (substring link 0 (match-beginning 0))))) Use `cond' instead. > + (if (string-match "[*?{]" (file-name-nondirectory link)) > + (dired (org-attach-expand link)) > + (org-open-file (org-attach-expand link) in-emacs line search)))) > + > +(defun org-attach-complete-link () > + "Advise the user with the available files in the attachment directory." > + (let (file link attached-dir) > + (setq attached-dir (expand-file-name (org-attach-dir))) > + (setq file (read-file-name "File: " attached-dir)) > + (let ((pwd (file-name-as-directory attached-dir)) > + (pwd1 (file-name-as-directory (abbreviate-file-name > + attached-dir)))) > + (cond > + ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file) > + (setq link (concat "@:" (match-string 1 file)))) > + ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)") > + (expand-file-name file)) > + (setq link (concat > + "@:" (match-string 1 (expand-file-name file))))) > + (t (setq link (concat "@:" file))))) > + link)) I suggest: (let* ((attached-dir (expand-file-name (org-attach-dir))) (file (read-file-name "File: " attached-dir)) (pwd (file-name-as-directory attached-dir)) (pwd-relative (file-name-as-directory (abbreviate-file-name attached-dir)))) (cond ((string-match ...) (concat ...)) ... (t (concat ...)))) > +(defun org-attach-export-link (link description format) > + "Translate \"attached\" (@) LINK from Org mode format to exported FORM= AT. > +Also includes the DESCRIPTION of the link in the export." > + (save-excursion > + (let (path desc) > + (if (string-match "::\\([0-9]+\\)\\'" link) > + (setq link (substring link 0 (match-beginning 0))) > + (if (string-match "::\\(.+\\)\\'" link) > + (setq link (substring link 0 (match-beginning 0))))) Use `cond' instead. > + (search-forward (concat "@:" (org-link-escape link))) What is the use for the line above? > diff --git a/lisp/org.el b/lisp/org.el > index eb1affbc7..9ed663bb9 100644 > --- a/lisp/org.el > +++ b/lisp/org.el > @@ -4428,6 +4428,7 @@ This is needed for font-lock setup.") > (beg end)) > (declare-function org-agenda-set-restriction-lock "org-agenda" (&optiona= l type)) > (declare-function org-agenda-skip "org-agenda" ()) > +(declare-function org-attach-expand "org-attach" (&optional if-exists)) > (declare-function org-attach-reveal "org-attach" (&optional if-exists)) > (declare-function org-gnus-follow-link "org-gnus" (&optional group artic= le)) > (declare-function org-indent-mode "org-indent" (&optional arg)) > @@ -18721,7 +18722,7 @@ boundaries." > ;; Check absolute, relative file names and explicit > ;; "file:" links. Also check link abbreviations since > ;; some might expand to "file" links. > - (file-types-re (format "[][]\\[\\(?:file\\|[./~]%s\\)" > + (file-types-re (format "[][]\\[\\(?:file\\|@\\|[./~]%s\\)" > (if (not link-abbrevs) "" > (format "\\|\\(?:%s:\\)" > (regexp-opt link-abbrevs)))))) > @@ -18730,14 +18731,20 @@ boundaries." > ;; Check if we're at an inline image, i.e., an image file > ;; link without a description (unless INCLUDE-LINKED is > ;; non-nil). > - (when (and (equal "file" (org-element-property :type link)) > + (when (and (or (equal "file" (org-element-property :type link)) > + (equal "@" (org-element-property :type link))) > (or include-linked > (null (org-element-contents link))) > (string-match-p file-extension-re > (org-element-property :path link))) > - (let ((file (expand-file-name > - (org-link-unescape > - (org-element-property :path link))))) > + (let ((file (if (equal "@" (org-element-property :type link)) > + (require 'org-attach) > + (org-attach-expand > + (org-link-unescape > + (org-element-property :path link))) > + (expand-file-name > + (org-link-unescape > + (org-element-property :path link)))))) > (when (file-exists-p file) > (let ((width > ;; Apply `org-image-actual-width' specifications. This part can be omitted for now. Something better has to be found. Thank you. Regards, --=20 Nicolas Goaziou