[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] New "project" option for org-link-file-path-type

From: Jack Kamm
Subject: [PATCH] New "project" option for org-link-file-path-type
Date: Wed, 28 Oct 2020 17:46:03 -0700

The attached patch adds a "project" option for org-link-file-path-type.

When this is set, links to files under the current project root will be
relative, while links elsewhere are absolute.

It relies on project.el, which appears to have been added in emacs 25. I
used fboundp to check whether the functionality is available, and to
silence compiler warnings. I'm not sure if this is the correct way to do

>From c5f9d4043a6cf6a325d122be24214356f36446f1 Mon Sep 17 00:00:00 2001
From: Jack Kamm <jackkamm@gmail.com>
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 |
+*** 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)))))
              (if (string-match (concat "^" (regexp-quote

reply via email to

[Prev in Thread] Current Thread [Next in Thread]