]> git.eshelyaron.com Git - emacs.git/commitdiff
Merge pull request from justbur/find-form
authorJohn Wiegley <johnw@newartisans.com>
Mon, 13 Feb 2017 21:53:56 +0000 (16:53 -0500)
committerGitHub <noreply@github.com>
Mon, 13 Feb 2017 21:53:56 +0000 (16:53 -0500)
Add function use-package-jump-to-package-form
GitHub-reference: https://github.com/jwiegley/use-package/issues/359

1  2 
lisp/use-package/use-package.el

index d993ce645301a02afd459492e796f7935311c108,a5195146df87319a6cc293c060b717b01af963dc..f933ce2d936c2427c19bbecb47e38f45fcdd5a57
@@@ -182,46 -176,65 +182,87 @@@ Must be set before loading use-package.
    :type 'boolean
    :group 'use-package)
  
 +(defcustom use-package-ensure-function 'use-package-ensure-elpa
 +  "Function that ensures a package is installed.
 +This function is called with one argument, the package name as a
 +symbol, by the `:ensure' keyword.
 +
 +The default value uses package.el to install the package."
 +  :type '(choice (const :tag "package.el" use-package-ensure-elpa)
 +                 (function :tag "Custom"))
 +  :group 'use-package)
 +
 +(defcustom use-package-defaults
 +  '((:config '(t) t)
 +    (:ensure use-package-always-ensure use-package-always-ensure)
 +    (:pin use-package-always-pin use-package-always-pin))
 +  "Alist of default values for `use-package' keywords.
 +Each entry in the alist is a list of three elements. The first
 +element is the `use-package' keyword and the second is a form
 +that can be evaluated to get the default value. The third element
 +is a form that can be evaluated to determine whether or not to
 +assign a default value; if it evaluates to nil, then the default
 +value is not assigned even if the keyword is not present in the
 +`use-package' form."
 +  :type '(repeat (list symbol sexp sexp)))
 +
  (when use-package-enable-imenu-support
 -  ;; Not defined in Emacs 24
 -  (defvar lisp-mode-symbol-regexp
 -    "\\(?:\\sw\\|\\s_\\|\\\\.\\)+")
 -  (add-to-list
 -   'lisp-imenu-generic-expression
 -   (list "Package"
 -         (purecopy (concat "^\\s-*("
 -                           (eval-when-compile
 -                             (regexp-opt
 -                              '("use-package" "require")
 -                              t))
 -                           "\\s-+\\(" lisp-mode-symbol-regexp "\\)"))
 -         2)))
 +  (eval-after-load 'lisp-mode
 +    `(let ((sym-regexp (or (bound-and-true-p lisp-mode-symbol-regexp)
 +                           "\\(?:\\sw\\|\\s_\\|\\\\.\\)+")))
 +       (add-to-list
 +        'lisp-imenu-generic-expression
 +        (list "Packages"
 +              (concat "^\\s-*("
 +                      ,(eval-when-compile
 +                         (regexp-opt '("use-package" "require") t))
 +                      "\\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
 +;;; Utility functions
  ;;
  
  (defun use-package-as-symbol (string-or-symbol)