How to find which file provide(d) the feature in emacs elisp -


currently using load-history variable find file feature came from.

suppose find file feature gnus came from.

i execute following code in scratch buffer prints filename , symbols in separate lines consecutively.

(dolist (var load-history)   (princ (format "%s\n" (car var)))   (princ (format "\t%s\n" (cdr var)))) 

and search "(provide . gnus)" , move point start of line(ctrl+a). file name in previous line file feature came from.

is there thing wrong method, or better method exist.

i don't know you're trying this, here notes.

  • your method fine. way hack own solution problem in book.

  • @tom correct shouldn't need this, because problem solved system. i.e. c-h f

but that's not interesting. let's want automatic, more elegant solution. want function -- locate-feature signature:

(defun locate-feature (feature)   "return file-name string `feature' provided"   ...) 

method 1 load-history approach

i'll describe steps took solve this:

  1. you've got important part -- find variable information need.

  2. i notice variable has lot of data. if insert buffer single line, emacs not happy, because it's notoriously bad @ handling long lines. know prett-print package able format data nicely. open *scratch* buffer , run

    m-: (insert (pp-to-string load-history))

  3. i can see data structure i'm dealing with. seems (in pseudo code):

    ((file-name   ((defun|t|provide . symbol)|symbol)*)  ...) 
  4. now write function

    (eval-when-compile (require 'cl)) (defun locate-feature (feature)   "return file name string `feature' provided"   (interactive "sfeature: ")   (dolist (file-info load-history)     (mapc (lambda (element)             (when (and (consp element)                        (eq (car element) 'provide)                        (eq (cdr element) feature))               (when (called-interactively-p 'any)                 (message "%s defined in %s" feature (car file-info)))               (return (car file-info))))           (cdr file-info)))) 

    the code here pretty straight forward. ask emacs functions don't understand.

method 2 approach

method 1 works features. if want know available function defined? not features.

c-h f tells me that, want file-name in string, not of verbose text. want this:

(defun locate-function (func)   "return file-name string `func' defined or autoloaded"   ...) 

here go.

  1. c-h f starting point, want read code defines describe-function. this:

    c-h k c-h f c-x o tab enter

  2. now i'm in help-fns.el @ definition of describe-function. want work function definition. narrowing in order:

    c-x n d

  3. i have hunch interesting command have "find" or "locate" in name, use occur search interesting lines:

    m-s o find\|locate

    no matches. hmmm. not lot of lines in defun. describe-function-1 seems doing real work, try that.

  4. i can visit definition of describe-function-1 via c-h f. have file open. imenu available now:

    c-x n w m-x imenu desc*1 tab enter

  5. narrow , search again:

    c-x n d m-s o up enter

    i see find-lisp-object-file-name looks promising.

  6. after reading c-h f find-lisp-object-file-name come with:

    (defun locate-function (func)   "return file-name string `func' defined or autoloaded"   (interactive "ccommand: ")   (let ((res (find-lisp-object-file-name func (symbol-function func))))     (when (called-interactively-p 'any)       (message "%s defined in %s" func res))     res)) 

now go have fun exploring emacs.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -