ido-describe-fns.el 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ;;; ido-describe-fns.el --- -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2017 Ryan C. Thompson
  3. ;; Filename: ido-describe-fns.el
  4. ;; Author: Ryan C. Thompson
  5. ;; Created: Tue May 16 18:23:13 2017 (-0400)
  6. ;; Version: 3.17
  7. ;; Package-Requires: ((emacs "26.1") (ido-completing-read+ 3.17))
  8. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  9. ;; Keywords: ido, completion, convenience
  10. ;; This file is NOT part of GNU Emacs.
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12. ;;
  13. ;;; Commentary:
  14. ;; This package implements ido completion for the new `describe-*'
  15. ;; family of functions. These no longer work with ido-ubiquitous
  16. ;; because they use a function-based collection argument to implement
  17. ;; auto-loading of the file corresponding to the prefix you entered in
  18. ;; order to offer completions of symbols from that file.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;; This program is free software: you can redistribute it and/or modify
  22. ;; it under the terms of the GNU General Public License as published by
  23. ;; the Free Software Foundation, either version 3 of the License, or (at
  24. ;; your option) any later version.
  25. ;;
  26. ;; This program is distributed in the hope that it will be useful, but
  27. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  28. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. ;; General Public License for more details.
  30. ;;
  31. ;; You should have received a copy of the GNU General Public License
  32. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  33. ;;
  34. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  35. ;;
  36. ;;; Code:
  37. (require 'ido)
  38. (require 'ido-completing-read+)
  39. ;;;###autoload
  40. (defvar ido-descfns-enable-this-call nil
  41. "If non-nil, then the current call to `ido-completing-read+' is by `ido-descfns-read-from-help-symbol-completion-table'.")
  42. (defvar ido-descfns-orig-predicate nil
  43. "Original predicate from the current completion call.")
  44. ;;;###autoload
  45. (define-minor-mode ido-describe-functions-mode
  46. "Use `ido-completing-read' for `describe-variable' and similar functions.
  47. In particular, it uses ido for any function that uses
  48. `help--symbol-completion-table' as the collection argument to
  49. `completing-read'."
  50. nil
  51. :global t
  52. :group 'ido-ubiquitous)
  53. (defun ido-descfns-maybe-load-prefixes (string)
  54. "Load any files needed to complete the current input.
  55. This function auto-loads new files in the same way as
  56. `help--symbol-completion-table', but without doing any
  57. completion.
  58. Returns non-nil if any new files were loaded."
  59. (let ((old-load-history load-history)
  60. (prefixes (radix-tree-prefixes (help-definition-prefixes) string)))
  61. (help--load-prefixes prefixes)
  62. (not (eq load-history old-load-history))))
  63. (defun ido-descfns-post-self-insert-hook ()
  64. "Maybe load new files and update possible ido completions.
  65. Has no effect unless `ido-descfns-enable-this-call' is non-nil."
  66. (when (and ido-descfns-enable-this-call
  67. (ido-descfns-maybe-load-prefixes ido-text))
  68. (with-no-warnings
  69. (setq ido-cur-list
  70. (all-completions "" obarray ido-descfns-orig-predicate)))))
  71. (defun ido-descfns-setup ()
  72. (add-hook 'post-self-insert-hook #'ido-descfns-post-self-insert-hook)
  73. (add-hook 'minibuffer-exit-hook #'ido-descfns-cleanup))
  74. (defun ido-descfns-cleanup ()
  75. (remove-hook 'post-self-insert-hook #'ido-descfns-post-self-insert-hook)
  76. (remove-hook 'minibuffer-exit-hook #'ido-descfns-cleanup)
  77. (remove-hook 'ido-setup-hook #'ido-descfns-setup))
  78. ;; This advice-based implementation is required for reentrancy
  79. (defadvice ido-completing-read+ (around ido-descfns activate)
  80. (let ((ido-descfns-enable-this-call
  81. (and ido-describe-functions-mode
  82. (eq collection 'help--symbol-completion-table)))
  83. ;; Each call gets its own private copy of these hooks
  84. (ido-setup-hook ido-setup-hook)
  85. (minibuffer-exit-hook minibuffer-exit-hook)
  86. (post-self-insert-hook post-self-insert-hook))
  87. ;; Clean up our copy of the hook in case of recursive completion.
  88. (ido-descfns-cleanup)
  89. (when ido-descfns-enable-this-call
  90. ;; Convert the initial collection into something ido-cr+ will
  91. ;; accept
  92. (setq collection obarray
  93. ido-descfns-orig-predicate predicate)
  94. (add-hook 'ido-setup-hook #'ido-descfns-setup))
  95. ad-do-it
  96. (ido-descfns-cleanup)))
  97. (add-hook 'ido-cr+-before-fallback-hook #'ido-descfns-cleanup)
  98. (provide 'ido-describe-fns)
  99. ;;; ido-describe-fns.el ends here