al-minibuffer.el 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ;;; al-minibuffer.el --- Additional functionality for minibuffer
  2. ;; Copyright © 2013–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. ;; Idea to use a custom completing-read function with the possibility to
  17. ;; fallback to `completing-read-default' came from
  18. ;; <http://www.emacswiki.org/emacs/InteractivelyDoThings#toc15>.
  19. (defvar al/completing-read-engine
  20. (if (boundp 'ivy-mode)
  21. 'ivy
  22. 'ido)
  23. "Engine used by `al/completing-read'.
  24. Can be either `ivy', `ido' or nil (to fallback to
  25. `completing-read-default').")
  26. (declare-function ivy-completing-read "ivy" t)
  27. (defun al/completing-read (prompt collection &optional predicate
  28. require-match initial-input
  29. hist def inherit-input-method)
  30. "Function for `completing-read-function' variable.
  31. Use completion engine depending on `al/completing-read-engine'."
  32. ;; Match is never required in the following calls, otherwise it's not
  33. ;; possible to select "#XXXXXX" with `read-color'.
  34. (cl-case al/completing-read-engine
  35. (ivy
  36. (ivy-completing-read prompt collection predicate
  37. nil initial-input
  38. hist def inherit-input-method))
  39. (ido
  40. (ido-completing-read prompt (all-completions "" collection predicate)
  41. nil nil initial-input hist def))
  42. (t
  43. ;; `minibuffer-complete' (bound to TAB in minibuffer prompt) calls
  44. ;; `completion-in-region', so return
  45. ;; `completion-in-region-function' to default value (in particular,
  46. ;; ivy changes it).
  47. (let ((completion-in-region-function 'completion--in-region))
  48. (completing-read-default prompt collection predicate
  49. nil initial-input
  50. hist def inherit-input-method)))))
  51. (defun al/complete-default (fun &rest args)
  52. "Use `completing-read-default' for FUN.
  53. This function is intended to be used as an 'around' advice for
  54. FUN, for example:
  55. (advice-add 'org-set-tags :around #'al/complete-default)"
  56. (let (al/completing-read-engine)
  57. (apply fun args)))
  58. (provide 'al-minibuffer)
  59. ;;; al-minibuffer.el ends here