From c5f9d4043a6cf6a325d122be24214356f36446f1 Mon Sep 17 00:00:00 2001 From: Jack Kamm Date: Wed, 28 Oct 2020 17:29:04 -0700 Subject: [PATCH] ol.el: New option "project" for org-link-file-path-type * lisp/ol.el (org-link-file-path-type): Add new option. (org-insert-link): Handle project option for org-link-file-path-type. --- etc/ORG-NEWS | 8 ++++++++ lisp/ol.el | 17 +++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 7f935bf52..b9adc9089 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -88,6 +88,14 @@ package, to convert pandas Dataframes into orgmode tables: | 2 | 3 | 6 | #+end_src +*** New option to use relative paths for links in same project + +If =org-link-file-path-type= is =project=, inserted links under the +current project root will use relative paths. + +If not in a project, or if =project.el= is not available (as in older +versions of Emacs), links behave as default (=adaptive=). + * Version 9.4 ** Incompatible changes *** Possibly broken internal file links: please check and fix diff --git a/lisp/ol.el b/lisp/ol.el index 951bb74e7..9c48bd9b5 100644 --- a/lisp/ol.el +++ b/lisp/ol.el @@ -212,13 +212,17 @@ (defcustom org-link-file-path-type 'adaptive absolute Absolute path, if possible with ~ for home directory. noabbrev Absolute path, no abbreviation of home directory. 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. +project Use relative path for files in the current project and sub- + directories of it. For other files, usue an absolute path. + If project.el is not available, behave as adaptive." :group 'org-link :type '(choice (const relative) (const absolute) (const noabbrev) - (const adaptive)) + (const adaptive) + (const project)) :safe #'symbolp) (defcustom org-link-abbrev-alist nil @@ -1876,6 +1880,15 @@ (defun org-insert-link (&optional complete-file link-location description) (setq path (expand-file-name path))) ((eq org-link-file-path-type 'relative) (setq path (file-relative-name path))) + ((and (fboundp 'project-current) + (fboundp 'project-root) + (project-current) + (eq org-link-file-path-type 'project)) + (if (string-prefix-p (expand-file-name (project-root + (project-current))) + (expand-file-name path)) + (setq path (file-relative-name path)) + (setq path (abbreviate-file-name (expand-file-name path))))) (t (save-match-data (if (string-match (concat "^" (regexp-quote -- 2.29.1