Subject: bug#13978: 24.3; New minor mode eldoc-post-insert-mode

From: Leo Liu <[email-masked]>
Date: Sun, 17 Mar 2013 03:23:49 +0800

The attached patch adds a new minor mode eldoc-post-insert-mode to
eldoc.el; the new mode can also be used by eval-expression (screenshot
attached). The feature supersedes eldoc-eval in GNU ELPA.

See discussion: http://article.gmane.org/gmane.emacs.devel/144524

>From e3aff878ebed7e4f5b67f539b9caaf5a0309a9c3 Mon Sep 17 00:00:00 2001
From: Leo Liu <[email-masked]>
Date: Sun, 17 Mar 2013 02:44:58 +0800
Subject: [PATCH 1/3] New variable eval-expression-minibuffer-hook

---
 lisp/simple.el | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lisp/simple.el b/lisp/simple.el
index 526cc64c..0c366db3 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1235,6 +1235,9 @@ (defun eval-expression-print-format (value)
             (format " (#o%o, #x%x, %s)" value value char-string)
           (format " (#o%o, #x%x)" value value)))))
 
+(defvar eval-expression-minibuffer-hook nil
+  "Hook run by `eval-expression' when entering the minibuffer.")
+
 ;; We define this, rather than making `eval' interactive,
 ;; for the sake of completion of names like eval-region, eval-buffer.
 (defun eval-expression (eval-expression-arg
@@ -1253,9 +1256,11 @@ (defun eval-expression (eval-expression-arg
 this command arranges for all errors to enter the debugger."
   (interactive
    (list (let ((minibuffer-completing-symbol t))
-	   (read-from-minibuffer "Eval: "
-				 nil read-expression-map t
-				 'read-expression-history))
+	   (minibuffer-with-setup-hook
+	       (lambda () (run-hooks 'eval-expression-minibuffer-hook))
+	     (read-from-minibuffer "Eval: "
+				   nil read-expression-map t
+				   'read-expression-history)))
 	 current-prefix-arg))
 
   (if (null eval-expression-debug-on-error)
-- 
1.8.2

>From d816ede71aa79999cfbaa3951b1d378b4cde361c Mon Sep 17 00:00:00 2001
From: Leo <[email-masked]>
Date: Wed, 5 Oct 2011 17:33:58 +0800
Subject: [PATCH 2/3] Allow eldoc to post messages to the mode-line

---
 lisp/emacs-lisp/eldoc.el | 43 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/lisp/emacs-lisp/eldoc.el b/lisp/emacs-lisp/eldoc.el
index 0f018573..8bce26c0 100644
--- a/lisp/emacs-lisp/eldoc.el
+++ b/lisp/emacs-lisp/eldoc.el
@@ -146,6 +146,11 @@ (defvar eldoc-current-idle-delay eldoc-idle-delay
   "Idle time delay currently in use by timer.
 This is used to determine if `eldoc-idle-delay' is changed by the user.")
 
+(defvar eldoc-message-function nil
+  "The function used by `eldoc-message' to display messages.
+It should receive the same arguments as `message'. If this is
+nil, `eldoc-minibuffer-message' is used.")
+
 
 ;;;###autoload
 (define-minor-mode eldoc-mode
@@ -188,8 +193,40 @@ (defun eldoc-schedule-timer ()
          (setq eldoc-current-idle-delay eldoc-idle-delay)
          (timer-set-idle-time eldoc-timer eldoc-idle-delay t))))
 
+(defvar eldoc-mode-line-string nil)
+(put 'eldoc-mode-line-string 'risky-local-variable t)
+
+(defun eldoc-minibuffer-message (format-string &rest args)
+  "Show messages in the mode-line when in the minibuffer.
+Otherwise, behave like function `message'."
+  (if (minibufferp)
+      (progn
+	(with-current-buffer
+	    (window-buffer
+	     (or (window-in-direction 'above (minibuffer-window))
+		 (minibuffer-selected-window)
+		 (get-largest-window)))
+	  (unless (and (listp mode-line-format)
+		       (assq 'eldoc-mode-line-string mode-line-format))
+	    (setq mode-line-format
+		  (list "" '(eldoc-mode-line-string
+			     (" " eldoc-mode-line-string " "))
+			mode-line-format))))
+	(add-hook 'minibuffer-exit-hook
+		  (lambda () (setq eldoc-mode-line-string nil))
+		  nil t)
+	(cond
+	 ((null format-string)
+	  (setq eldoc-mode-line-string nil))
+	 ((stringp format-string)
+	  (setq eldoc-mode-line-string
+		(apply 'format format-string args))))
+	(force-mode-line-update))
+    (apply 'message format-string args)))
+
 (defun eldoc-message (&rest args)
-  (let ((omessage eldoc-last-message))
+  (let ((omessage eldoc-last-message)
+        (msgfunc (or eldoc-message-function #'eldoc-minibuffer-message)))
     (setq eldoc-last-message
 	  (cond ((eq (car args) eldoc-last-message) eldoc-last-message)
 		((null (car args)) nil)
@@ -203,8 +240,8 @@ (defun eldoc-message (&rest args)
     ;; they are Legion.
     ;; Emacs way of preventing log messages.
     (let ((message-log-max nil))
-      (cond (eldoc-last-message (message "%s" eldoc-last-message))
-	    (omessage (message nil)))))
+      (cond (eldoc-last-message (funcall msgfunc "%s" eldoc-last-message))
+	    (omessage (funcall msgfunc nil)))))
   eldoc-last-message)
 
 ;; This function goes on pre-command-hook for XEmacs or when using idle
-- 
1.8.2

>From e0bbc2b6ea1745f8383a7ad8313031c5b6e3dc1e Mon Sep 17 00:00:00 2001
From: Leo Liu <[email-masked]>
Date: Sun, 17 Mar 2013 03:03:11 +0800
Subject: [PATCH 3/3] Implement eldoc-post-insert-mode

---
 lisp/emacs-lisp/eldoc.el | 50 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 32 insertions(+), 18 deletions(-)

diff --git a/lisp/emacs-lisp/eldoc.el b/lisp/emacs-lisp/eldoc.el
index 8bce26c0..2f3e1644 100644
--- a/lisp/emacs-lisp/eldoc.el
+++ b/lisp/emacs-lisp/eldoc.el
@@ -174,6 +174,18 @@ (define-minor-mode eldoc-mode
    (remove-hook 'post-command-hook 'eldoc-schedule-timer)
    (remove-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area)))
 
+(define-minor-mode eldoc-post-insert-mode nil
+  :group 'eldoc :lighter ""
+  (setq eldoc-last-message nil)
+  (let ((prn-info (lambda ()
+		    (unless eldoc-mode
+		      (eldoc-print-current-symbol-info-1)))))
+    (if eldoc-post-insert-mode
+	(add-hook 'post-self-insert-hook prn-info nil t)
+      (remove-hook 'post-self-insert-hook prn-info t))))
+
+(add-hook 'eval-expression-minibuffer-hook 'eldoc-post-insert-mode)
+
 ;;;###autoload
 (defun turn-on-eldoc-mode ()
   "Unequivocally turn on ElDoc mode (see command `eldoc-mode')."
@@ -297,29 +309,31 @@ (defvar eldoc-documentation-function nil
 This variable is expected to be made buffer-local by modes (other than
 Emacs Lisp mode) that support ElDoc.")
 
-(defun eldoc-print-current-symbol-info ()
+(defun eldoc-print-current-symbol-info-1 ()
   (condition-case err
-      (and (eldoc-display-message-p)
-	   (if eldoc-documentation-function
-	       (eldoc-message (funcall eldoc-documentation-function))
-	     (let* ((current-symbol (eldoc-current-symbol))
-		    (current-fnsym  (eldoc-fnsym-in-current-sexp))
-		    (doc (cond
-			  ((null current-fnsym)
-			   nil)
-			  ((eq current-symbol (car current-fnsym))
-			   (or (apply 'eldoc-get-fnsym-args-string
-				      current-fnsym)
-			       (eldoc-get-var-docstring current-symbol)))
-			  (t
-			   (or (eldoc-get-var-docstring current-symbol)
-			       (apply 'eldoc-get-fnsym-args-string
-				      current-fnsym))))))
-	       (eldoc-message doc))))
+      (if eldoc-documentation-function
+	  (eldoc-message (funcall eldoc-documentation-function))
+	(let* ((current-symbol (eldoc-current-symbol))
+	       (current-fnsym  (eldoc-fnsym-in-current-sexp))
+	       (doc (cond
+		     ((null current-fnsym)
+		      nil)
+		     ((eq current-symbol (car current-fnsym))
+		      (or (apply 'eldoc-get-fnsym-args-string
+				 current-fnsym)
+			  (eldoc-get-var-docstring current-symbol)))
+		     (t
+		      (or (eldoc-get-var-docstring current-symbol)
+			  (apply 'eldoc-get-fnsym-args-string
+				 current-fnsym))))))
+	  (eldoc-message doc)))
     ;; This is run from post-command-hook or some idle timer thing,
     ;; so we need to be careful that errors aren't ignored.
     (error (message "eldoc error: %s" err))))
 
+(defun eldoc-print-current-symbol-info ()
+  (and (eldoc-display-message-p) (eldoc-print-current-symbol-info-1)))
+
 (defun eldoc-get-fnsym-args-string (sym &optional index)
   "Return a string containing the parameter list of the function SYM.
 If SYM is a subr and no arglist is obtainable from the docstring
-- 
1.8.2