Rainer
Rainer

Reputation: 8691

Getting "root" folder of orgmode package installation in emacs via elisp

How can I get the folder in which org-mode is installed in emacs? Depending on the way it was installed, it will be different. Is there a variable which holds this value?

I would need it to access a file which is part of the org-mode installation.

I am not looking for a particular library, but an .R file, which is an R file which I want to load programmatically into R (from elisp code).

So using

(locate-library "ob-R") "/Users/rainerkrug/.emacs.d/org-mode/lisp/ob-R.elc"

I would then have to use the following:

(concat (locate-library "ob-R") "/../etc/") "/Users/rainerkrug/.emacs.d/org-mode/lisp/ob-R.elc/../etc/"

And I still have to get rid of the ob-R.elc

This works, but I am looking for a function which gives me the path

(IS-THERE-SOMETHING-LIKE-THIS "org") "/Users/rainerkrug/.emacs.d/org-mode/"

Thanks

Upvotes: 0

Views: 247

Answers (5)

Rainer
Rainer

Reputation: 8691

I found a solution which is efectively using locate-library and to truncate the not-needed elements (file name and last dir) and assembles them again as a path:

(locate-library "org")
"/Users/rainerkrug/.emacs.d/org-mode/lisp/org.elc"

(split-string (locate-library "org") "/")
("" "Users" "rainerkrug" ".emacs.d" "org-mode" "lisp" "org.elc")


(butlast (split-string (locate-library "org") "/") 2)
("" "Users" "rainerkrug" ".emacs.d" "org-mode")

(append (butlast (split-string (locate-library "org") "/") 2) '("etc" "R"))
("" "Users" "rainerkrug" ".emacs.d" "org-mode" "etc" "R")

(mapconcat 'identity
           (append (butlast (split-string (locate-library "org") "/") 2) '("etc" "R"))
           "/")
"/Users/rainerkrug/.emacs.d/org-mode/etc/R"

As pointed out in the comments, the usage of split-string is suboptimal. Please see accepted answer for the best approach.

Upvotes: 0

user355252
user355252

Reputation:

Emacs provides a rich set of file name manipulation functions which easily solve your problem:

(expand-file-name "../etc/R" (file-name-directory (locate-library "ob-R")))

Upvotes: 3

juanleon
juanleon

Reputation: 9400

You can use this:

(org-find-library-dir "org")

Or, in your case:

(concat (org-find-library-dir "org") "etc/R")

Upvotes: 1

Andrii Tykhonov
Andrii Tykhonov

Reputation: 538

M-x locate-library RET org RET

or, if you would like to open:

M-x find-library RET org RET

Upvotes: 1

Ignacy Moryc
Ignacy Moryc

Reputation: 169

If you don't need to do this programmatically, you could use M-x describe-mode and in the description one of first lines is Org mode defined in org.el The link to org.el is clickable, and leads to org-mode's directory.

Upvotes: 0

Related Questions