summaryrefslogtreecommitdiff
path: root/lisp/use-package/use-package.el
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2017-02-13 16:53:56 -0500
committerGitHub <noreply@github.com>2017-02-13 16:53:56 -0500
commit0905a7b1c728612788718014458a8f265e265389 (patch)
treee33836ed8fa039608e55d9bcd710b104fe0603e9 /lisp/use-package/use-package.el
parentc287aa308581a9936fbca1c1641241c6aba91a27 (diff)
parentc13ca927c201c53f2107d54ba2dad8beb15d84ba (diff)
downloademacs-0905a7b1c728612788718014458a8f265e265389.tar.gz
emacs-0905a7b1c728612788718014458a8f265e265389.tar.bz2
emacs-0905a7b1c728612788718014458a8f265e265389.zip
Merge pull request from justbur/find-form
Add function use-package-jump-to-package-form GitHub-reference: https://github.com/jwiegley/use-package/issues/359
Diffstat (limited to 'lisp/use-package/use-package.el')
-rw-r--r--lisp/use-package/use-package.el41
1 files changed, 41 insertions, 0 deletions
diff --git a/lisp/use-package/use-package.el b/lisp/use-package/use-package.el
index d993ce64530..f933ce2d936 100644
--- a/lisp/use-package/use-package.el
+++ b/lisp/use-package/use-package.el
@@ -219,6 +219,47 @@ value is not assigned even if the keyword is not present in the
"\\s-+\\(" sym-regexp "\\)")
2)))))
+(defvar use-package-form-regexp "^\\s-*(\\s-*use-package\\s-+\\_<%s\\_>"
+ "Regexp used in `use-package-jump-to-package-form' to find use
+package forms in user files.")
+
+(defun use-package--find-require (package)
+ "Find file that required PACKAGE by searching
+`load-history'. Returns an absolute file path or nil if none is
+found."
+ (catch 'suspect
+ (dolist (filespec load-history)
+ (dolist (entry (cdr filespec))
+ (when (equal entry (cons 'require package))
+ (throw 'suspect (car filespec)))))))
+
+(defun use-package-jump-to-package-form (package)
+ "Attempt to find and jump to the `use-package' form that loaded
+PACKAGE. This will only find the form if that form actually
+required PACKAGE. If PACKAGE was previously required then this
+function will jump to the file that orginally required PACKAGE
+instead."
+ (interactive (list (completing-read "Package: " features)))
+ (let* ((package (if (stringp package) (intern package) package))
+ (requiring-file (use-package--find-require package))
+ file location)
+ (if (null requiring-file)
+ (user-error "Can't find file that requires this feature.")
+ (setq file (if (string= (file-name-extension requiring-file) "elc")
+ (concat (file-name-sans-extension requiring-file) ".el")
+ requiring-file))
+ (when (file-exists-p file)
+ (find-file-other-window file)
+ (save-excursion
+ (goto-char (point-min))
+ (setq location
+ (re-search-forward
+ (format use-package-form-regexp package) nil t)))
+ (if (null location)
+ (message "No use-package form found.")
+ (goto-char location)
+ (beginning-of-line))))))
+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Utility functions