mb-depth.el 2.83 KB
Newer Older
Kim F. Storm's avatar
Kim F. Storm committed
1 2
;;; mb-depth.el --- Indicate minibuffer-depth in prompt
;;
3
;; Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012  Free Software Foundation, Inc.
Kim F. Storm's avatar
Kim F. Storm committed
4 5 6 7 8 9
;;
;; Author: Miles Bader <miles@gnu.org>
;; Keywords: convenience

;; This file is part of GNU Emacs.

10
;; GNU Emacs is free software: you can redistribute it and/or modify
Kim F. Storm's avatar
Kim F. Storm committed
11
;; it under the terms of the GNU General Public License as published by
12 13
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Kim F. Storm's avatar
Kim F. Storm committed
14 15 16 17 18 19 20

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
21
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Kim F. Storm's avatar
Kim F. Storm committed
22 23 24

;;; Commentary:
;;
25
;; Defines the minor mode `minibuffer-depth-indicate-mode'.
Kim F. Storm's avatar
Kim F. Storm committed
26 27 28 29 30 31 32
;;
;; When active, any recursive use of the minibuffer will show
;; the recursion depth in the minibuffer prompt.  This is only
;; useful if `enable-recursive-minibuffers' is non-nil.

;;; Code:

33
(defvar minibuffer-depth-indicator-function nil
34 35 36 37
  "If non-nil, function to set up the minibuffer depth indicator.
It is called with one argument, the minibuffer depth,
and must return a string.")

Kim F. Storm's avatar
Kim F. Storm committed
38 39 40
;; An overlay covering the prompt.  This is a buffer-local variable in
;; each affected minibuffer.
;;
41 42
(defvar minibuffer-depth-overlay)
(make-variable-buffer-local 'minibuffer-depth-overlay)
Kim F. Storm's avatar
Kim F. Storm committed
43 44

;; This function goes on minibuffer-setup-hook
45 46
(defun minibuffer-depth-setup ()
  "Set up a minibuffer for `minibuffer-depth-indicate-mode'.
Kim F. Storm's avatar
Kim F. Storm committed
47 48
The prompt should already have been inserted."
  (when (> (minibuffer-depth) 1)
49 50 51 52
    (setq minibuffer-depth-overlay (make-overlay (point-min) (1+ (point-min))))
    (overlay-put minibuffer-depth-overlay 'before-string
		 (if minibuffer-depth-indicator-function
		     (funcall minibuffer-depth-indicator-function (minibuffer-depth))
53
		   (propertize (format "[%d]" (minibuffer-depth)) 'face 'highlight)))
54
    (overlay-put minibuffer-depth-overlay 'evaporate t)))
Kim F. Storm's avatar
Kim F. Storm committed
55 56

;;;###autoload
57 58
(define-minor-mode minibuffer-depth-indicate-mode
  "Toggle Minibuffer Depth Indication mode.
Kim F. Storm's avatar
Kim F. Storm committed
59 60 61 62 63 64 65 66
When active, any recursive use of the minibuffer will show
the recursion depth in the minibuffer prompt.  This is only
useful if `enable-recursive-minibuffers' is non-nil.

With prefix argument ARG, turn on if positive, otherwise off.
Returns non-nil if the new state is enabled."
  :global t
  :group 'minibuffer
67
  (if minibuffer-depth-indicate-mode
Kim F. Storm's avatar
Kim F. Storm committed
68
      ;; Enable the mode
69
      (add-hook 'minibuffer-setup-hook 'minibuffer-depth-setup)
Kim F. Storm's avatar
Kim F. Storm committed
70
    ;; Disable the mode
71
    (remove-hook 'minibuffer-setup-hook 'minibuffer-depth-setup)))
Kim F. Storm's avatar
Kim F. Storm committed
72 73 74

(provide 'mb-depth)

Miles Bader's avatar
Miles Bader committed
75
;; arch-tag: 50224089-5bf5-46f8-803d-18f018c5eacf
Kim F. Storm's avatar
Kim F. Storm committed
76
;;; mb-depth.el ends here