Emacs: Fix M-. without a hassle

M-. (i.e. the Alt dot key combination in Emacs) does xref-find-definitions which only works if you have set up something called a tags table which is incomprehensible and a hassle and therefore unacceptable. It is much easier to use describe-function or describe-symbol, which open up *Help* pages in Emacs.

Even better is find-function which goes into Lisp source code, straight into the bowels of Emacs where the magic happens. However, so-called primitives have been written in C and, for those primitive functions, find-function offers locating the C source code, which is a dead end because C source code is not there by default. You’d have to download the C source code and set it up for find-function to do anything useful, and then update the C source code alongside with (and separately from) Emacs installations, which is a major hassle.

To make M-. work while evading those hassles, here’s an idea: If it’s in Lisp, take me to the source code, but if it’s a primitive, open up *Help* instead.

(defun find-function-divert-C (function)
  "Do `find-function', but for C primitives divert to `describe-function'."
  (interactive (find-function-read))
  (if (subrp (symbol-function function))
      (describe-function function)
    (find-function function)))
(defalias 'xref-find-definitions 'find-function-divert-C)

Leave a Reply

Your email address will not be published. Required fields are marked *