mb-depth.el 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;;; mb-depth.el --- Indicate minibuffer-depth in prompt
  2. ;;
  3. ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Miles Bader <miles@gnu.org>
  6. ;; Keywords: convenience
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;
  20. ;; Defines the minor mode `minibuffer-depth-indicate-mode'.
  21. ;;
  22. ;; When active, any recursive use of the minibuffer will show
  23. ;; the recursion depth in the minibuffer prompt. This is only
  24. ;; useful if `enable-recursive-minibuffers' is non-nil.
  25. ;;; Code:
  26. (defvar minibuffer-depth-indicator-function nil
  27. "If non-nil, function to set up the minibuffer depth indicator.
  28. It is called with one argument, the minibuffer depth,
  29. and must return a string.")
  30. ;; An overlay covering the prompt. This is a buffer-local variable in
  31. ;; each affected minibuffer.
  32. ;;
  33. (defvar minibuffer-depth-overlay)
  34. (make-variable-buffer-local 'minibuffer-depth-overlay)
  35. ;; This function goes on minibuffer-setup-hook
  36. (defun minibuffer-depth-setup ()
  37. "Set up a minibuffer for `minibuffer-depth-indicate-mode'.
  38. The prompt should already have been inserted."
  39. (when (> (minibuffer-depth) 1)
  40. (setq minibuffer-depth-overlay (make-overlay (point-min) (1+ (point-min))))
  41. (overlay-put minibuffer-depth-overlay 'before-string
  42. (if minibuffer-depth-indicator-function
  43. (funcall minibuffer-depth-indicator-function (minibuffer-depth))
  44. (propertize (format "[%d]" (minibuffer-depth)) 'face 'highlight)))
  45. (overlay-put minibuffer-depth-overlay 'evaporate t)))
  46. ;;;###autoload
  47. (define-minor-mode minibuffer-depth-indicate-mode
  48. "Toggle Minibuffer Depth Indication mode.
  49. With a prefix argument ARG, enable Minibuffer Depth Indication
  50. mode if ARG is positive, and disable it otherwise. If called
  51. from Lisp, enable the mode if ARG is omitted or nil.
  52. Minibuffer Depth Indication mode is a global minor mode. When
  53. enabled, any recursive use of the minibuffer will show the
  54. recursion depth in the minibuffer prompt. This is only useful if
  55. `enable-recursive-minibuffers' is non-nil."
  56. :global t
  57. :group 'minibuffer
  58. (if minibuffer-depth-indicate-mode
  59. ;; Enable the mode
  60. (add-hook 'minibuffer-setup-hook 'minibuffer-depth-setup)
  61. ;; Disable the mode
  62. (remove-hook 'minibuffer-setup-hook 'minibuffer-depth-setup)))
  63. (provide 'mb-depth)
  64. ;;; mb-depth.el ends here