emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] New "project" option for org-link-file-path-type
@ 2020-10-29  0:46 Jack Kamm
  2020-11-02  5:41 ` Kyle Meyer
  0 siblings, 1 reply; 6+ messages in thread
From: Jack Kamm @ 2020-10-29  0:46 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 412 bytes --]

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
it.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ol.el-New-option-project-for-org-link-file-path-type.patch --]
[-- Type: text/x-patch, Size: 2735 bytes --]

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 |
 #+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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-04-25  3:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-29  0:46 [PATCH] New "project" option for org-link-file-path-type Jack Kamm
2020-11-02  5:41 ` Kyle Meyer
2020-11-04 18:11   ` Jack Kamm
2020-11-06  3:33     ` Kyle Meyer
2020-11-12  1:05       ` Jack Kamm
2021-04-25  3:29         ` Timothy

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).