al-imenus.el 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ;;; al-imenus.el --- Additional functionality for imenus
  2. ;; Copyright © 2014–2017 Alex Kost
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;;
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (require 'cl-lib)
  17. (require 'imenus)
  18. (require 'al-minibuffer)
  19. (declare-function ivy-read "ivy" t)
  20. (defun al/imenus-completing-read (prompt collection &optional predicate
  21. require-match initial-input
  22. history def inherit-input-method)
  23. "Function for `imenus-completing-read-function'.
  24. This is like `completing-read' but with supporting sorting
  25. candidates for `ivy'."
  26. (cond
  27. ((and (eq 'ivy al/completing-read-engine)
  28. (require 'ivy nil t))
  29. ;; Disable flx match to make `ivy--sort' sort candidates using
  30. ;; `ivy-sort-matches-functions-alist'.
  31. (let (ivy--flx-featurep)
  32. (ivy-read prompt collection
  33. :preselect (thing-at-point 'symbol)
  34. :initial-input initial-input
  35. :history history
  36. :caller 'imenus)))
  37. (t
  38. (funcall completing-read-function
  39. prompt collection predicate require-match
  40. initial-input history def inherit-input-method))))
  41. ;;; Searching/"imenu"-ing elisp files
  42. (defvar al/imenus-elisp-directories (list user-emacs-directory)
  43. "List of directories used by `al/imenus-search-elisp-directories'.")
  44. (defvar al/imenus-elisp-re "^[^.].*\\.el\\'"
  45. "Regexp for files to search in `al/imenus-elisp-directories'.")
  46. (defvar al/imenus-elisp-prompt "Search elisp files: "
  47. "Prompt used by `al/imenus-search-elisp-directories'.")
  48. ;;;###autoload
  49. (defun al/imenus-search-elisp-directories ()
  50. "Perform `imenus' on elisp files from `al/imenus-elisp-directories'."
  51. (interactive)
  52. (let ((files (cl-mapcan (lambda (dir)
  53. (directory-files dir t al/imenus-elisp-re))
  54. al/imenus-elisp-directories)))
  55. (imenus-files files nil al/imenus-elisp-prompt)))
  56. (provide 'al-imenus)
  57. ;;; al-imenus.el ends here