LCOV - code coverage report
Current view: top level - lisp/emacs-lisp - pp.el (source / functions) Hit Total Coverage
Test: tramp-tests-after.info Lines: 0 81 0.0 %
Date: 2017-08-30 10:12:24 Functions: 0 10 0.0 %

          Line data    Source code
       1             : ;;; pp.el --- pretty printer for Emacs Lisp  -*- lexical-binding: t -*-
       2             : 
       3             : ;; Copyright (C) 1989, 1993, 2001-2017 Free Software Foundation, Inc.
       4             : 
       5             : ;; Author: Randal Schwartz <merlyn@stonehenge.com>
       6             : ;; Keywords: lisp
       7             : 
       8             : ;; This file is part of GNU Emacs.
       9             : 
      10             : ;; GNU Emacs is free software: you can redistribute it and/or modify
      11             : ;; it under the terms of the GNU General Public License as published by
      12             : ;; the Free Software Foundation, either version 3 of the License, or
      13             : ;; (at your option) any later version.
      14             : 
      15             : ;; GNU Emacs is distributed in the hope that it will be useful,
      16             : ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             : ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18             : ;; GNU General Public License for more details.
      19             : 
      20             : ;; 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/>.
      22             : 
      23             : ;;; Commentary:
      24             : 
      25             : ;;; Code:
      26             : 
      27             : (defvar font-lock-verbose)
      28             : 
      29             : (defgroup pp nil
      30             :   "Pretty printer for Emacs Lisp."
      31             :   :prefix "pp-"
      32             :   :group 'lisp)
      33             : 
      34             : (defcustom pp-escape-newlines t
      35             :   "Value of `print-escape-newlines' used by pp-* functions."
      36             :   :type 'boolean
      37             :   :group 'pp)
      38             : 
      39             : ;;;###autoload
      40             : (defun pp-to-string (object)
      41             :   "Return a string containing the pretty-printed representation of OBJECT.
      42             : OBJECT can be any Lisp object.  Quoting characters are used as needed
      43             : to make output that `read' can handle, whenever this is possible."
      44           0 :   (with-temp-buffer
      45           0 :     (lisp-mode-variables nil)
      46           0 :     (set-syntax-table emacs-lisp-mode-syntax-table)
      47           0 :     (let ((print-escape-newlines pp-escape-newlines)
      48             :           (print-quoted t))
      49           0 :       (prin1 object (current-buffer)))
      50           0 :     (pp-buffer)
      51           0 :     (buffer-string)))
      52             : 
      53             : ;;;###autoload
      54             : (defun pp-buffer ()
      55             :   "Prettify the current buffer with printed representation of a Lisp object."
      56           0 :   (goto-char (point-min))
      57           0 :   (while (not (eobp))
      58             :     ;; (message "%06d" (- (point-max) (point)))
      59           0 :     (cond
      60           0 :      ((ignore-errors (down-list 1) t)
      61           0 :       (save-excursion
      62           0 :         (backward-char 1)
      63           0 :         (skip-chars-backward "'`#^")
      64           0 :         (when (and (not (bobp)) (memq (char-before) '(?\s ?\t ?\n)))
      65           0 :           (delete-region
      66           0 :            (point)
      67           0 :            (progn (skip-chars-backward " \t\n") (point)))
      68           0 :           (insert "\n"))))
      69           0 :      ((ignore-errors (up-list 1) t)
      70           0 :       (skip-syntax-forward ")")
      71           0 :       (delete-region
      72           0 :        (point)
      73           0 :        (progn (skip-chars-forward " \t\n") (point)))
      74           0 :       (insert ?\n))
      75           0 :      (t (goto-char (point-max)))))
      76           0 :   (goto-char (point-min))
      77           0 :   (indent-sexp))
      78             : 
      79             : ;;;###autoload
      80             : (defun pp (object &optional stream)
      81             :   "Output the pretty-printed representation of OBJECT, any Lisp object.
      82             : Quoting characters are printed as needed to make output that `read'
      83             : can handle, whenever this is possible.
      84             : Output stream is STREAM, or value of `standard-output' (which see)."
      85           0 :   (princ (pp-to-string object) (or stream standard-output)))
      86             : 
      87             : (defun pp-display-expression (expression out-buffer-name)
      88             :   "Prettify and display EXPRESSION in an appropriate way, depending on length.
      89             : If a temporary buffer is needed for representation, it will be named
      90             : after OUT-BUFFER-NAME."
      91           0 :   (let* ((old-show-function temp-buffer-show-function)
      92             :          ;; Use this function to display the buffer.
      93             :          ;; This function either decides not to display it at all
      94             :          ;; or displays it in the usual way.
      95             :          (temp-buffer-show-function
      96           0 :           (function
      97             :            (lambda (buf)
      98           0 :              (with-current-buffer buf
      99           0 :                (goto-char (point-min))
     100           0 :                (end-of-line 1)
     101           0 :                (if (or (< (1+ (point)) (point-max))
     102           0 :                        (>= (- (point) (point-min)) (frame-width)))
     103           0 :                    (let ((temp-buffer-show-function old-show-function)
     104           0 :                          (old-selected (selected-window))
     105           0 :                          (window (display-buffer buf)))
     106           0 :                      (goto-char (point-min)) ; expected by some hooks ...
     107           0 :                      (make-frame-visible (window-frame window))
     108           0 :                      (unwind-protect
     109           0 :                          (progn
     110           0 :                            (select-window window)
     111           0 :                            (run-hooks 'temp-buffer-show-hook))
     112           0 :                        (when (window-live-p old-selected)
     113           0 :                          (select-window old-selected))
     114           0 :                        (message "See buffer %s." out-buffer-name)))
     115           0 :                  (message "%s" (buffer-substring (point-min) (point)))
     116           0 :                  ))))))
     117           0 :     (with-output-to-temp-buffer out-buffer-name
     118           0 :       (pp expression)
     119           0 :       (with-current-buffer standard-output
     120           0 :         (emacs-lisp-mode)
     121           0 :         (setq buffer-read-only nil)
     122           0 :         (set (make-local-variable 'font-lock-verbose) nil)))))
     123             : 
     124             : ;;;###autoload
     125             : (defun pp-eval-expression (expression)
     126             :   "Evaluate EXPRESSION and pretty-print its value.
     127             : Also add the value to the front of the list in the variable `values'."
     128             :   (interactive
     129           0 :    (list (read--expression "Eval: ")))
     130           0 :   (message "Evaluating...")
     131           0 :   (push (eval expression lexical-binding) values)
     132           0 :   (pp-display-expression (car values) "*Pp Eval Output*"))
     133             : 
     134             : ;;;###autoload
     135             : (defun pp-macroexpand-expression (expression)
     136             :   "Macroexpand EXPRESSION and pretty-print its value."
     137             :   (interactive
     138           0 :    (list (read--expression "Macroexpand: ")))
     139           0 :   (pp-display-expression (macroexpand-1 expression) "*Pp Macroexpand Output*"))
     140             : 
     141             : (defun pp-last-sexp ()
     142             :   "Read sexp before point.  Ignores leading comment characters."
     143           0 :   (with-syntax-table emacs-lisp-mode-syntax-table
     144           0 :     (let ((pt (point)))
     145           0 :       (save-excursion
     146           0 :         (forward-sexp -1)
     147           0 :         (read
     148             :          ;; If first line is commented, ignore all leading comments:
     149           0 :          (if (save-excursion (beginning-of-line) (looking-at-p "[ \t]*;"))
     150           0 :              (let ((exp (buffer-substring (point) pt))
     151             :                    (start nil))
     152           0 :                (while (string-match "\n[ \t]*;+" exp start)
     153           0 :                  (setq start (1+ (match-beginning 0))
     154           0 :                        exp (concat (substring exp 0 start)
     155           0 :                                    (substring exp (match-end 0)))))
     156           0 :                exp)
     157           0 :            (current-buffer)))))))
     158             : 
     159             : ;;;###autoload
     160             : (defun pp-eval-last-sexp (arg)
     161             :   "Run `pp-eval-expression' on sexp before point.
     162             : With argument, pretty-print output into current buffer.
     163             : Ignores leading comment characters."
     164             :   (interactive "P")
     165           0 :   (if arg
     166           0 :       (insert (pp-to-string (eval (pp-last-sexp) lexical-binding)))
     167           0 :     (pp-eval-expression (pp-last-sexp))))
     168             : 
     169             : ;;;###autoload
     170             : (defun pp-macroexpand-last-sexp (arg)
     171             :   "Run `pp-macroexpand-expression' on sexp before point.
     172             : With argument, pretty-print output into current buffer.
     173             : Ignores leading comment characters."
     174             :   (interactive "P")
     175           0 :   (if arg
     176           0 :       (insert (pp-to-string (macroexpand-1 (pp-last-sexp))))
     177           0 :     (pp-macroexpand-expression (pp-last-sexp))))
     178             : 
     179             : (provide 'pp)                           ; so (require 'pp) works
     180             : 
     181             : ;;; pp.el ends here

Generated by: LCOV version 1.12