unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: "Emacs-Devel" <emacs-devel@gnu.org>
Subject: pp.el patch
Date: Fri, 18 Apr 2008 14:12:59 -0700	[thread overview]
Message-ID: <003d01c8a198$fb540e70$0200a8c0@us.oracle.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 782 bytes --]

The attached pp.el patch does the following:

* Adds two user options used by `pp-eval-expression':
  `pp-eval-expression-print-length'
  `pp-eval-expression-print-level'

* Changes `pp-eval-expression' in these ways:

 . Respects the new options and `eval-expression-debug-on-error'. 

 . Reads the expression using keymap `pp-read-expression-map',
   which provides some Emacs-Lisp mode bindings.

 . Adds an optional arg INSERT-VALUE (prefix-arg), similar to
   what `eval-expression' has. See doc string.

I find it useful. YMMV.  I bind it to `M-:'. 

In minibuffer maps, I bind `M-:' to a command that calls this after binding
`enable-recursive-minibuffers' to t. With a negative prefix arg, I yank string
values (e.g. of variables) to the minibuffer (without double-quotes).

[-- Attachment #2: pp-2008-04-18.patch --]
[-- Type: application/octet-stream, Size: 4908 bytes --]

diff -u -w "pp-CVS-2008-04-18.el" "pp-patched-2008-04-18.el"
--- pp-CVS-2008-04-18.el	2008-04-18 13:48:24.000000000 -0700
+++ pp-patched-2008-04-18.el	2008-04-18 13:54:40.000000000 -0700
@@ -39,6 +39,30 @@
   :type 'boolean
   :group 'pp)
 
+(defcustom pp-eval-expression-print-length nil
+  "Value for `print-length' while printing value in `pp-eval-expression'.
+A value of nil means no limit."
+  :group 'pp :type '(choice (const :tag "No Limit" nil) integer))
+
+(defcustom pp-eval-expression-print-level nil
+  "Value for `print-level' while printing value in `pp-eval-expression'.
+A value of nil means no limit."
+  :group 'pp :type '(choice (const :tag "No Limit" nil) integer))
+
+(defvar pp-read-expression-map nil
+  "`read-expression-map' with some Emacs-Lisp key bindings.")
+(unless pp-read-expression-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "\M-\t" 'lisp-complete-symbol)
+    (define-key map "\t" 'lisp-indent-line)
+    (define-key map "\e\C-q" 'indent-sexp)
+    (define-key map "\e\t" 'lisp-complete-symbol)
+    (define-key map "\e\C-x" 'eval-defun)
+    (define-key map "\e\C-q" 'indent-pp-sexp)
+    ;;(define-key map "\177" 'backward-delete-char-untabify)
+    (set-keymap-parent map minibuffer-local-map)
+    (setq pp-read-expression-map map)))
+
 ;;;###autoload
 (defun pp-to-string (object)
   "Return a string containing the pretty-printed representation of OBJECT.
@@ -97,14 +121,47 @@
   (princ (pp-to-string object) (or stream standard-output)))
 
 ;;;###autoload
-(defun pp-eval-expression (expression)
-  "Evaluate EXPRESSION and pretty-print its value.
-Also add the value to the front of the list in the variable `values'."
+(defun pp-eval-expression (expression &optional insert-value)
+  "Evaluate an Emacs-Lisp expression and pretty-print its value.
+Add the value to the front of the variable `values'.
+With a prefix arg, insert the value into the current buffer at point.
+With no prefix arg:
+ If the value fits on one line (frame width) show it in the echo area.
+ Otherwise, show the value in buffer *Pp Eval Output*.
+
+With a negative prefix arg, if the value is a string, then insert it
+into the buffer without double-quotes (`\"').
+
+This command respects options `pp-eval-expression-print-length',
+`pp-eval-expression-print-level', and
+`eval-expression-debug-on-error'.
+
+Emacs-Lisp mode completion and indentation bindings are in effect."
   (interactive
-   (list (read-from-minibuffer "Eval: " nil read-expression-map t
-			       'read-expression-history)))
+   (list (read-from-minibuffer "Eval: " nil pp-read-expression-map t
+                               'read-expression-history)
+         current-prefix-arg))
   (message "Evaluating...")
+  (if (null eval-expression-debug-on-error)
+      (setq values (cons (eval expression) values))
+    (let ((old-value (make-symbol "t")) new-value)
+      ;; Bind `debug-on-error' to something unique so that we can
+      ;; detect when evaled code changes it.
+      (let ((debug-on-error old-value))
   (setq values (cons (eval expression) values))
+	(setq new-value debug-on-error))
+      ;; If evaled code has changed the value of `debug-on-error',
+      ;; propagate that change to the global binding.
+      (unless (eq old-value new-value) (setq debug-on-error new-value))))
+  (let ((print-length pp-eval-expression-print-length)
+	(print-level pp-eval-expression-print-level))
+    (cond (insert-value
+           (message "Evaluating...done. Value inserted.")
+           (setq insert-value (prefix-numeric-value insert-value))
+           (if (or (not (stringp (car values))) (wholenump insert-value))
+               (pp (car values) (current-buffer))
+             (princ (car values) (current-buffer))))
+          (t
   (let* ((old-show-function temp-buffer-show-function)
 	 ;; Use this function to display the buffer.
 	 ;; This function either decides not to display it at all
@@ -130,14 +187,16 @@
 		       (select-window old-selected)
 		       (message "Evaluating...done.  \
 See buffer *Pp Eval Output*.")))
-		 (message "%s" (buffer-substring (point-min) (point)))
-		 ))))))
+                         (message "%s" (buffer-substring (point-min) (point)))))))))
     (with-output-to-temp-buffer "*Pp Eval Output*"
       (pp (car values))
       (with-current-buffer standard-output
-	(emacs-lisp-mode)
 	(setq buffer-read-only nil)
-	(set (make-local-variable 'font-lock-verbose) nil)))))
+                 (let ((emacs-lisp-mode-hook nil)
+                       (change-major-mode-hook nil))
+                   (emacs-lisp-mode))
+                 (set (make-local-variable 'font-lock-verbose) nil)
+                 (font-lock-fontify-buffer))))))))
 
 ;;;###autoload
 (defun pp-eval-last-sexp (arg)

Diff finished at Fri Apr 18 13:56:06

             reply	other threads:[~2008-04-18 21:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-18 21:12 Drew Adams [this message]
2008-04-22 15:42 ` pp.el patch Michael Olson
2008-04-22 16:37   ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='003d01c8a198$fb540e70$0200a8c0@us.oracle.com' \
    --to=drew.adams@oracle.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).