PascalVKooten
PascalVKooten

Reputation: 21453

Visiting required file from .emacs

Is it possible to visit a mentioned file in require like:

    (require 'key-chord)

For which when clicking on "key-chord" (or some other input than clicking), makes you visit that file?

EDIT: Given that we want to visit the file "key-chord.el"

Upvotes: 1

Views: 192

Answers (3)

phils
phils

Reputation: 73274

M-x find-library RET prompts you for the name of a library, and then visits that file.

Calling it with point on a valid library name will make that the default prompt value.

Calling it with point anywhere within a require statement will also do the right thing.

I use find-library frequently, and bind it to C-hC-l for convenience.

Upvotes: 1

tripleee
tripleee

Reputation: 189487

locate-library tells you the file name, and find-file opens the file in a buffer for editing. (You might want find-file-read-only if you merely want to inspect it.)

(find-file (locate-library "key-chord.el" t))

You can turn it into a function, something like this:

(defun find-locate-library (lib)
  "Visit the source for library LIB in a buffer."
  (let ((location (locate-library (concat lib ".el") t)))
    (if location
      (find-file location)
     (message "Could not find library %s" lib)) ))

How to hook this into a viewing or editing buffer is a separate topic. I suppose you could amend Elisp mode to make library names clickable, but I cannot tell off-hand how to do that.

Thanks to @Deokhwan Kim for the suggestion to limit search to ".el" files only.

Upvotes: 1

jpkotta
jpkotta

Reputation: 9417

Put the cursor on whatever is being required, e.g. the "k" in "key-chord", and do M-x ffap RET RET. ffap is an alias for find-file-at-point, which understands require, at least in lisp-mode. I have ffap bound to C-x f, because I rarely have use for that key's default binding.

Some Emacs installations don't have the .el files (only the .elc files). In Debian/Ubuntu, you need to install a separate package to get them (emacs-version-el, IIRC).

Upvotes: 4

Related Questions