]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/pp.el
Merge from emacs-24; up to 2014-07-27T09:41:59Z!ttn@gnu.org
[gnu-emacs] / lisp / emacs-lisp / pp.el
index ad73134d254c3871a0671d483b32f8b4c15b2876..dd012fab9daaa271e098a6fd90e224cde4144767 100644 (file)
@@ -1,7 +1,6 @@
 ;;; pp.el --- pretty printer for Emacs Lisp
 
-;; Copyright (C) 1989, 1993, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1989, 1993, 2001-2014 Free Software Foundation, Inc.
 
 ;; Author: Randal Schwartz <merlyn@stonehenge.com>
 ;; Keywords: lisp
@@ -33,7 +32,7 @@
   :group 'lisp)
 
 (defcustom pp-escape-newlines t
-  "*Value of `print-escape-newlines' used by pp-* functions."
+  "Value of `print-escape-newlines' used by pp-* functions."
   :type 'boolean
   :group 'pp)
 
   "Return a string containing the pretty-printed representation of OBJECT.
 OBJECT can be any Lisp object.  Quoting characters are used as needed
 to make output that `read' can handle, whenever this is possible."
-  (save-excursion
-    (set-buffer (generate-new-buffer " pp-to-string"))
-    (unwind-protect
-       (progn
-         (lisp-mode-variables nil)
-         (set-syntax-table emacs-lisp-mode-syntax-table)
-         (let ((print-escape-newlines pp-escape-newlines)
-               (print-quoted t))
-           (prin1 object (current-buffer)))
-          (pp-buffer)
-         (buffer-string))
-      (kill-buffer (current-buffer)))))
+  (with-temp-buffer
+    (lisp-mode-variables nil)
+    (set-syntax-table emacs-lisp-mode-syntax-table)
+    (let ((print-escape-newlines pp-escape-newlines)
+          (print-quoted t))
+      (prin1 object (current-buffer)))
+    (pp-buffer)
+    (buffer-string)))
 
 ;;;###autoload
 (defun pp-buffer ()
@@ -62,9 +57,7 @@ to make output that `read' can handle, whenever this is possible."
   (while (not (eobp))
     ;; (message "%06d" (- (point-max) (point)))
     (cond
-     ((condition-case err-var
-          (prog1 t (down-list 1))
-        (error nil))
+     ((ignore-errors (down-list 1) t)
       (save-excursion
         (backward-char 1)
         (skip-chars-backward "'`#^")
@@ -73,10 +66,8 @@ to make output that `read' can handle, whenever this is possible."
            (point)
            (progn (skip-chars-backward " \t\n") (point)))
           (insert "\n"))))
-     ((condition-case err-var
-          (prog1 t (up-list 1))
-        (error nil))
-      (while (looking-at "\\s)")
+     ((ignore-errors (up-list 1) t)
+      (while (looking-at-p "\\s)")
         (forward-char 1))
       (delete-region
        (point)
@@ -105,8 +96,7 @@ after OUT-BUFFER-NAME."
         (temp-buffer-show-function
          (function
           (lambda (buf)
-            (save-excursion
-              (set-buffer buf)
+            (with-current-buffer buf
               (goto-char (point-min))
               (end-of-line 1)
               (if (or (< (1+ (point)) (point-max))
@@ -120,7 +110,8 @@ after OUT-BUFFER-NAME."
                         (progn
                           (select-window window)
                           (run-hooks 'temp-buffer-show-hook))
-                      (select-window old-selected)
+                      (when (window-live-p old-selected)
+                        (select-window old-selected))
                       (message "See buffer %s." out-buffer-name)))
                 (message "%s" (buffer-substring (point-min) (point)))
                 ))))))
@@ -136,18 +127,16 @@ after OUT-BUFFER-NAME."
   "Evaluate EXPRESSION and pretty-print its value.
 Also add the value to the front of the list in the variable `values'."
   (interactive
-   (list (read-from-minibuffer "Eval: " nil read-expression-map t
-                              'read-expression-history)))
+   (list (read--expression "Eval: ")))
   (message "Evaluating...")
-  (setq values (cons (eval expression) values))
+  (setq values (cons (eval expression lexical-binding) values))
   (pp-display-expression (car values) "*Pp Eval Output*"))
 
 ;;;###autoload
 (defun pp-macroexpand-expression (expression)
   "Macroexpand EXPRESSION and pretty-print its value."
   (interactive
-   (list (read-from-minibuffer "Macroexpand: " nil read-expression-map t
-                              'read-expression-history)))
+   (list (read--expression "Macroexpand: ")))
   (pp-display-expression (macroexpand expression) "*Pp Macroexpand Output*"))
 
 (defun pp-last-sexp ()
@@ -157,7 +146,7 @@ Also add the value to the front of the list in the variable `values'."
     (save-excursion
       (forward-sexp -1)
       ;; If first line is commented, ignore all leading comments:
-      (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
+      (if (save-excursion (beginning-of-line) (looking-at-p "[ \t]*;"))
          (progn
            (setq exp (buffer-substring (point) pt))
            (while (string-match "\n[ \t]*;+" exp start)
@@ -176,7 +165,7 @@ With argument, pretty-print output into current buffer.
 Ignores leading comment characters."
   (interactive "P")
   (if arg
-      (insert (pp-to-string (eval (pp-last-sexp))))
+      (insert (pp-to-string (eval (pp-last-sexp) lexical-binding)))
     (pp-eval-expression (pp-last-sexp))))
 
 ;;;###autoload
@@ -204,5 +193,4 @@ Ignores leading comment characters."
 
 (provide 'pp)                          ; so (require 'pp) works
 
-;; arch-tag: b0f7c65b-02c7-42bb-9ee3-508a59b8fbb9
 ;;; pp.el ends here