[சனி பிப்ரவரி 22, 2025] Ihor Radchenko wrote: >>> Visuwesh <visuwe...@gmail.com> writes: >>>>> 1. do a variable for selecting absolute path: to be able to have a file >>>>> link that works with absolute path; and the reason is that being >>>>> relative, this, does not combine good when you move your latex export >>>>> somewhere else (in my case, I usually move it to /tmp/, and from there I >>>>> decide when I want to save something persistently), hence it fails, and >>>>> I got tired on fixing the links, in fact, the absolute path sometimes is >>>>> smaller than the relative one >>>>> =file:/tmp/clipboard-20240916T013308.png]]= vs >>>>> =[[file:../../../tmp/clipboard-20240916T011913.png]]= >>>> >>>> This can be done in the org side. I guess adding another defcustom >>>> would be the way to go? I don't see how it is possible to make >>>> org-yank-image-save-method accept another option. >>> We can re-use `org-link-file-path-type'. >>> Probably, simply by factoring out the cond from `org-insert-link' and >>> then reusing it in yank handler. >> >> I think this will indeed be the best. We can reuse this everywhere we >> insert the link in yank-media+DND code (which IIRC uses the same >> function at the very end). > > Visuwesh, do you want to proceed with this part?
Do we only want this for images, or for everything? Everything entails images, all dropped files, dropped image files only, and XDND. This would warrant a NEWS entry too, right? If we go for every file: link, the patch would be like below: diff --git a/lisp/ol.el b/lisp/ol.el index b456f79e6..dec4f2ac7 100644 --- a/lisp/ol.el +++ b/lisp/ol.el @@ -2565,6 +2565,31 @@ (defun org-store-link (arg &optional interactive?) (org-link--add-to-stored-links link desc))) (car org-stored-links))))) +(defun org-link--normalise-filename (filename &optional absolute-always) + "Return FILENAME as required by the value of `org-link-file-path-type'. +When ABSOLUTE-ALWAYS is non-nil, always return an absolute and +abbreviated filename." + (cond + ((or (eq org-link-file-path-type 'absolute) absolute-always) + (abbreviate-file-name (expand-file-name filename))) + ((eq org-link-file-path-type 'noabbrev) + (expand-file-name filename)) + ((eq org-link-file-path-type 'relative) + (file-relative-name filename)) + ((functionp org-link-file-path-type) + (funcall org-link-file-path-type filename)) + (t + (save-match-data + (if (string-match (concat "^" (regexp-quote + (expand-file-name + (file-name-as-directory + default-directory)))) + (expand-file-name filename)) + ;; We are linking a file with relative path name. + (substring (expand-file-name filename) + (match-end 0)) + (abbreviate-file-name (expand-file-name filename))))))) + ;;;###autoload (defun org-insert-link (&optional complete-file link-location description) "Insert a link. At the prompt, enter the link. @@ -2752,27 +2777,7 @@ (defun org-insert-link (&optional complete-file link-location description) link path-start (match-beginning 0)) (substring-no-properties link (match-end 0)))) (origpath path)) - (cond - ((or (eq org-link-file-path-type 'absolute) - (equal complete-file '(16))) - (setq path (abbreviate-file-name (expand-file-name path)))) - ((eq org-link-file-path-type 'noabbrev) - (setq path (expand-file-name path))) - ((eq org-link-file-path-type 'relative) - (setq path (file-relative-name path))) - ((functionp org-link-file-path-type) - (setq path (funcall org-link-file-path-type path))) - (t - (save-match-data - (if (string-match (concat "^" (regexp-quote - (expand-file-name - (file-name-as-directory - default-directory)))) - (expand-file-name path)) - ;; We are linking a file with relative path name. - (setq path (substring (expand-file-name path) - (match-end 0))) - (setq path (abbreviate-file-name (expand-file-name path))))))) + (setq path (org-link--normalise-filename path (equal complete '(16)))) (setq link (concat type path (and search (concat "::" search)))) (when (equal desc origpath) (setq desc path))))) diff --git a/lisp/org.el b/lisp/org.el index d012819a9..6e56f7a00 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -20450,8 +20450,7 @@ (defun org--image-yank-media-handler (mimetype data) filename (if (eq org-yank-image-save-method 'attach) temporary-file-directory - org-yank-image-save-method))) - link) + org-yank-image-save-method)))) (when (and (not (eq org-yank-image-save-method 'attach)) (not (file-directory-p org-yank-image-save-method))) (make-directory org-yank-image-save-method t)) @@ -20461,11 +20460,12 @@ (defun org--image-yank-media-handler (mimetype data) (with-temp-file absname (insert data))) (if (null (eq org-yank-image-save-method 'attach)) - (setq link (org-link-make-string (concat "file:" (file-relative-name absname)))) + (insert (org-link-make-string + (concat "file:" + (org-link--normalise-filename absname)))) (require 'org-attach) (org-attach-attach absname nil 'mv) - (setq link (org-link-make-string (concat "attachment:" filename)))) - (insert link))) + (insert (org-link-make-string (concat "attachment:" filename)))))) ;; I cannot find a spec for this but ;; https://indigo.re/posts/2021-12-21-clipboard-data.html and pcmanfm @@ -20595,7 +20595,9 @@ (defun org--dnd-local-file-handler (url action &optional separator) (`open (dnd-open-local-file url action)) (`file-link (let ((filename (dnd-get-local-file-name url))) - (insert (org-link-make-string (concat "file:" filename)) separator)))))) + (insert (org-link-make-string + (concat "file:" (org-link--normalise-filename filename))) + separator)))))) (defun org--dnd-attach-file (url action separator) "Attach filename given by URL using method pertaining to ACTION. @@ -20643,8 +20645,9 @@ (defun org--dnd-attach-file (url action separator) "file:" "attachment:") (if separatep - (expand-file-name (file-name-nondirectory filename) - org-yank-image-save-method) + (org-link--normalise-filename + (expand-file-name (file-name-nondirectory filename) + org-yank-image-save-method)) (file-name-nondirectory filename)))) separator) 'private)) @@ -20672,7 +20675,9 @@ (defun org--dnd-xds-function (need-name filename) (pcase org--dnd-xds-method (`attach (insert (org-link-make-string (concat "attachment:" (file-name-nondirectory filename))))) - (`file-link (insert (org-link-make-string (concat "file:" filename)))) + (`file-link (insert (org-link-make-string + (concat "file:" + (org-link--normalise-filename filename))))) (`open (find-file filename))) (setq-local org--dnd-xds-method nil)))