Browse Source

ol.el: New option to set org-link-file-path-type to a function

* lisp/ol.el (org-link-file-path-type): Add new option.
(org-insert-link): Handle function option for org-link-file-path-type.
Jack Kamm 5 years ago
parent
commit
5371b30fe5
2 changed files with 28 additions and 2 deletions
  1. 18 0
      etc/ORG-NEWS
  2. 10 2
      lisp/ol.el

+ 18 - 0
etc/ORG-NEWS

@@ -35,6 +35,24 @@ omit a file description was to omit the header argument entirely,
 which made it difficult/impossible to provide a default value for
 which made it difficult/impossible to provide a default value for
 =file-desc=.
 =file-desc=.
 
 
+*** New option to set ~org-link-file-path-type~ to a function
+
+~org-link-file-path-type~ can now be set to a function that takes the
+full filename as an argument and returns the path to link to.
+
+For example, if you use ~project.el~, you can set this function to use
+relative links within a project as follows:
+
+#+begin_src emacs-lisp
+(setq (org-link-file-path-type
+       (lambda (path)
+         (let* ((proj (project-current))
+                (root (if proj (project-root proj) default-directory)))
+           (if (string-prefix-p (expand-file-name root) path)
+               (file-relative-name path)
+             (abbreviate-file-name path))))))
+#+end_src
+
 ** New features
 ** New features
 *** =ob-python= improvements to =:return= header argument 
 *** =ob-python= improvements to =:return= header argument 
 
 

+ 10 - 2
lisp/ol.el

@@ -212,13 +212,18 @@ relative  Relative to the current directory, i.e. the directory of the file
 absolute  Absolute path, if possible with ~ for home directory.
 absolute  Absolute path, if possible with ~ for home directory.
 noabbrev  Absolute path, no abbreviation of home directory.
 noabbrev  Absolute path, no abbreviation of home directory.
 adaptive  Use relative path for files in the current directory and sub-
 adaptive  Use relative path for files in the current directory and sub-
-          directories of it.  For other files, use an absolute path."
+          directories of it.  For other files, use an absolute path.
+
+Alternatively, users may supply a custom function that takes the
+full filename as an argument and returns the path."
   :group 'org-link
   :group 'org-link
   :type '(choice
   :type '(choice
 	  (const relative)
 	  (const relative)
 	  (const absolute)
 	  (const absolute)
 	  (const noabbrev)
 	  (const noabbrev)
-	  (const adaptive))
+	  (const adaptive)
+	  (function))
+  :package-version '(Org . "9.5")
   :safe #'symbolp)
   :safe #'symbolp)
 
 
 (defcustom org-link-abbrev-alist nil
 (defcustom org-link-abbrev-alist nil
@@ -1876,6 +1881,9 @@ Use TAB to complete link prefixes, then RET for type-specific completion support
 	    (setq path (expand-file-name path)))
 	    (setq path (expand-file-name path)))
 	   ((eq org-link-file-path-type 'relative)
 	   ((eq org-link-file-path-type 'relative)
 	    (setq path (file-relative-name path)))
 	    (setq path (file-relative-name path)))
+	   ((functionp org-link-file-path-type)
+	    (setq path (funcall org-link-file-path-type
+				(expand-file-name path))))
 	   (t
 	   (t
 	    (save-match-data
 	    (save-match-data
 	      (if (string-match (concat "^" (regexp-quote
 	      (if (string-match (concat "^" (regexp-quote