1995-10-30 17:35:01 +00:00
|
|
|
;;; pp.el --- pretty printer for Emacs Lisp
|
1996-01-14 07:34:30 +00:00
|
|
|
|
2012-01-05 01:46:05 -08:00
|
|
|
;; Copyright (C) 1989, 1993, 2001-2012 Free Software Foundation, Inc.
|
1994-08-14 09:22:39 +00:00
|
|
|
|
1996-01-19 11:10:56 +00:00
|
|
|
;; Author: Randal Schwartz <merlyn@stonehenge.com>
|
2001-09-04 12:54:14 +00:00
|
|
|
;; Keywords: lisp
|
1994-08-14 09:22:39 +00:00
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2008-05-06 03:21:21 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
1994-08-14 09:22:39 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 03:21:21 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
1994-08-14 09:22:39 +00:00
|
|
|
|
|
|
|
;; 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
|
2008-05-06 03:21:21 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
1994-08-14 09:22:39 +00:00
|
|
|
|
2001-07-16 12:23:00 +00:00
|
|
|
;;; Commentary:
|
|
|
|
|
1994-08-14 09:22:39 +00:00
|
|
|
;;; Code:
|
2001-07-16 12:23:00 +00:00
|
|
|
|
2005-08-29 10:44:49 +00:00
|
|
|
(defvar font-lock-verbose)
|
|
|
|
|
1998-04-05 18:26:32 +00:00
|
|
|
(defgroup pp nil
|
|
|
|
"Pretty printer for Emacs Lisp."
|
|
|
|
:prefix "pp-"
|
|
|
|
:group 'lisp)
|
|
|
|
|
2003-02-04 13:24:35 +00:00
|
|
|
(defcustom pp-escape-newlines t
|
2009-07-22 02:45:34 +00:00
|
|
|
"Value of `print-escape-newlines' used by pp-* functions."
|
1998-04-05 18:26:32 +00:00
|
|
|
:type 'boolean
|
|
|
|
:group 'pp)
|
1994-08-14 09:22:39 +00:00
|
|
|
|
2002-02-14 16:47:11 +00:00
|
|
|
;;;###autoload
|
1994-08-14 09:22:39 +00:00
|
|
|
(defun pp-to-string (object)
|
1998-09-13 03:37:24 +00:00
|
|
|
"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."
|
2012-02-05 03:09:35 +01:00
|
|
|
(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)))
|
1994-08-14 09:22:39 +00:00
|
|
|
|
2004-06-28 23:08:31 +00:00
|
|
|
;;;###autoload
|
2004-05-28 21:05:17 +00:00
|
|
|
(defun pp-buffer ()
|
|
|
|
"Prettify the current buffer with printed representation of a Lisp object."
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (not (eobp))
|
|
|
|
;; (message "%06d" (- (point-max) (point)))
|
|
|
|
(cond
|
2012-02-05 03:09:35 +01:00
|
|
|
((ignore-errors (down-list 1) t)
|
2004-05-28 21:05:17 +00:00
|
|
|
(save-excursion
|
|
|
|
(backward-char 1)
|
|
|
|
(skip-chars-backward "'`#^")
|
2005-06-29 13:52:29 +00:00
|
|
|
(when (and (not (bobp)) (memq (char-before) '(?\s ?\t ?\n)))
|
2004-05-28 21:05:17 +00:00
|
|
|
(delete-region
|
|
|
|
(point)
|
|
|
|
(progn (skip-chars-backward " \t\n") (point)))
|
|
|
|
(insert "\n"))))
|
2012-02-05 03:09:35 +01:00
|
|
|
((ignore-errors (up-list 1) t)
|
|
|
|
(while (looking-at-p "\\s)")
|
2004-05-28 21:05:17 +00:00
|
|
|
(forward-char 1))
|
|
|
|
(delete-region
|
|
|
|
(point)
|
|
|
|
(progn (skip-chars-forward " \t\n") (point)))
|
|
|
|
(insert ?\n))
|
|
|
|
(t (goto-char (point-max)))))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(indent-sexp))
|
|
|
|
|
1995-01-03 23:44:44 +00:00
|
|
|
;;;###autoload
|
1994-08-14 09:22:39 +00:00
|
|
|
(defun pp (object &optional stream)
|
|
|
|
"Output the pretty-printed representation of OBJECT, any Lisp object.
|
1998-09-13 03:37:24 +00:00
|
|
|
Quoting characters are printed as needed to make output that `read'
|
1994-08-14 09:22:39 +00:00
|
|
|
can handle, whenever this is possible.
|
|
|
|
Output stream is STREAM, or value of `standard-output' (which see)."
|
|
|
|
(princ (pp-to-string object) (or stream standard-output)))
|
|
|
|
|
2008-04-26 02:32:33 +00:00
|
|
|
(defun pp-display-expression (expression out-buffer-name)
|
|
|
|
"Prettify and display EXPRESSION in an appropriate way, depending on length.
|
|
|
|
If a temporary buffer is needed for representation, it will be named
|
|
|
|
after OUT-BUFFER-NAME."
|
1995-05-07 00:38:42 +00:00
|
|
|
(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
|
|
|
|
;; or displays it in the usual way.
|
|
|
|
(temp-buffer-show-function
|
1994-08-14 09:22:39 +00:00
|
|
|
(function
|
|
|
|
(lambda (buf)
|
* textmodes/two-column.el (2C-split):
* textmodes/texnfo-upd.el (texinfo-multi-file-included-list):
* textmodes/tex-mode.el (tex-set-buffer-directory):
* textmodes/spell.el (spell-region, spell-string):
* textmodes/reftex.el (reftex-erase-buffer):
(reftex-get-file-buffer-force, reftex-kill-temporary-buffers):
* textmodes/reftex-toc.el (reftex-toc-promote-action):
* textmodes/reftex-sel.el (reftex-get-offset, reftex-insert-docstruct)
(reftex-select-item):
* textmodes/reftex-ref.el (reftex-label-info-update)
(reftex-offer-label-menu):
* textmodes/reftex-index.el (reftex-index-change-entry)
(reftex-index-phrases-info):
* textmodes/reftex-global.el (reftex-create-tags-file)
(reftex-save-all-document-buffers, reftex-ensure-write-access):
* textmodes/reftex-dcr.el (reftex-echo-ref, reftex-echo-cite)
(reftex-view-crossref-from-bibtex):
* textmodes/reftex-cite.el (reftex-bibtex-selection-callback)
(reftex-extract-bib-entries-from-thebibliography)
(reftex-all-used-citation-keys, reftex-create-bibtex-file):
* textmodes/refbib.el (r2b-capitalize-title):
(r2b-convert-buffer, r2b-help):
* textmodes/page-ext.el (pages-directory)
(pages-directory-goto-with-mouse):
* textmodes/bibtex.el (bibtex-validate-globally):
* textmodes/bib-mode.el (bib-capitalize-title):
* textmodes/artist.el (artist-clear-buffer, artist-system):
* progmodes/xscheme.el (global-set-scheme-interaction-buffer):
(local-set-scheme-interaction-buffer, xscheme-process-filter)
(verify-xscheme-buffer, xscheme-enter-interaction-mode)
(xscheme-enter-debugger-mode, xscheme-debugger-mode-p)
(xscheme-send-control-g-interrupt, xscheme-start-process)
(xscheme-process-sentinel, xscheme-cd):
* progmodes/verilog-mode.el (verilog-read-always-signals)
(verilog-set-define, verilog-getopt-file)
(verilog-module-inside-filename-p):
* progmodes/sh-script.el:
* progmodes/python.el (python-pdbtrack-get-source-buffer)
(python-pdbtrack-grub-for-buffer, python-execute-file):
* progmodes/octave-inf.el (inferior-octave):
* progmodes/idlwave.el (idlwave-scan-user-lib-files)
(idlwave-shell-compile-helper-routines, idlwave-set-local)
(idlwave-display-completion-list-xemacs, idlwave-list-abbrevs)
(idlwave-display-completion-list-emacs, idlwave-list-load-path-shadows)
(idlwave-completion-fontify-classes, idlwave-display-calling-sequence):
* progmodes/idlw-shell.el (idlwave-shell-examine-display-clear)
(idlwave-shell-filter, idlwave-shell-examine-highlight)
(idlwave-shell-sentinel, idlwave-shell-filter-directory)
(idlwave-shell-display-line, idlwave-shell-set-bp-in-module)
(idlwave-shell-examine-display, idlwave-shell-run-region)
(idlwave-shell-filter-bp, idlwave-shell-save-and-action)
(idlwave-shell-sources-filter, idlwave-shell-goto-next-error):
* progmodes/idlw-help.el (idlwave-help-get-special-help)
(idlwave-help-get-help-buffer):
* progmodes/gud.el (gud-basic-call, gud-find-class)
(gud-tooltip-activate-mouse-motions-if-enabled):
* progmodes/gdb-mi.el (gdb-mouse-toggle-breakpoint-fringe):
* progmodes/ebrowse.el (ebrowse-member-table, ebrowse-save-tree-as)
(ebrowse-view-exit-fn, ebrowse-tags-list-members-in-file)
(ebrowse-tags-next-file):
* progmodes/ebnf2ps.el (ebnf-generate-eps, ebnf-generate-eps)
(ebnf-eps-production-list, ebnf-begin-file, ebnf-log)
(ebnf-eps-finish-and-write):
* progmodes/cpp.el (cpp-edit-save):
* progmodes/cperl-mode.el (cperl-pod-to-manpage):
* progmodes/cc-defs.el (c-emacs-features):
* progmodes/antlr-mode.el (antlr-invalidate-context-cache)
(antlr-directory-dependencies):
* progmodes/ada-xref.el (ada-gnat-parse-gpr, ada-get-ali-file-name)
(ada-run-application, ada-find-in-src-path, ada-goto-parent)
(ada-find-any-references, ada-make-filename-from-adaname)
(ada-make-body-gnatstub):
* obsolete/rnews.el (news-list-news-groups):
* obsolete/resume.el (resume-suspend-hook,resume-write-buffer-to-file):
* obsolete/iso-acc.el (iso-acc-minibuf-setup):
* net/rcirc.el (rcirc-debug):
* net/newst-treeview.el (newsticker--treeview-list-add-item)
(newsticker--treeview-list-clear, newsticker-treeview-browse-url)
(newsticker--treeview-list-update-faces, newsticker-treeview-save)
(newsticker--treeview-item-show-text, newsticker--treeview-item-show)
(newsticker--treeview-tree-update-tag,newsticker--treeview-buffer-init)
(newsticker-treeview-show-item, newsticker--treeview-unfold-node)
(newsticker--treeview-list-clear-highlight)
(newsticker--treeview-list-update-highlight)
(newsticker--treeview-list-highlight-start)
(newsticker--treeview-tree-update-highlight)
(newsticker--treeview-get-selected-item)
(newsticker-treeview-mark-list-items-old)
(newsticker--treeview-set-current-node):
* net/newst-plainview.el (newsticker--buffer-set-uptodate):
* net/newst-backend.el (newsticker--get-news-by-funcall)
(newsticker--get-news-by-wget, newsticker--image-get)
(newsticker--image-sentinel):
* net/mairix.el (mairix-rmail-fetch-field, mairix-gnus-fetch-field):
* net/eudcb-ph.el (eudc-ph-do-request, eudc-ph-open-session):
(eudc-ph-close-session):
* net/eudc.el (eudc-save-options):
* language/thai-word.el (thai-update-word-table):
* language/japan-util.el (japanese-string-conversion):
* international/titdic-cnv.el (tsang-quick-converter)
(ziranma-converter, ctlau-converter):
* international/mule-cmds.el (describe-language-environment):
* international/ja-dic-cnv.el (skkdic-convert-okuri-ari)
(skkdic-convert-postfix, skkdic-convert-prefix):
(skkdic-convert-okuri-nasi, skkdic-convert):
* emacs-lisp/re-builder.el (reb-update-overlays):
* emacs-lisp/pp.el (pp-to-string, pp-display-expression):
* emacs-lisp/gulp.el (gulp-send-requests):
* emacs-lisp/find-gc.el (trace-call-tree):
* emacs-lisp/eieio-opt.el (eieio-browse, eieio-describe-class)
(eieio-describe-generic):
* emacs-lisp/eieio-base.el (eieio-persistent-read):
* emacs-lisp/edebug.el (edebug-outside-excursion):
* emacs-lisp/debug.el (debugger-make-xrefs):
* emacs-lisp/cust-print.el (custom-prin1-to-string):
* emacs-lisp/chart.el (chart-new-buffer):
* emacs-lisp/authors.el (authors-scan-el, authors-scan-change-log):
Use with-current-buffer.
* textmodes/artist.el (artist-system): Don't call
copy-sequence on a fresh string.
* progmodes/idlw-shell.el (easymenu setup): Use dolist.
2009-10-31 02:38:34 +00:00
|
|
|
(with-current-buffer buf
|
1994-08-14 09:22:39 +00:00
|
|
|
(goto-char (point-min))
|
|
|
|
(end-of-line 1)
|
|
|
|
(if (or (< (1+ (point)) (point-max))
|
1996-01-19 11:24:34 +00:00
|
|
|
(>= (- (point) (point-min)) (frame-width)))
|
1995-05-07 00:38:42 +00:00
|
|
|
(let ((temp-buffer-show-function old-show-function)
|
|
|
|
(old-selected (selected-window))
|
|
|
|
(window (display-buffer buf)))
|
1994-08-14 09:22:39 +00:00
|
|
|
(goto-char (point-min)) ; expected by some hooks ...
|
1995-05-07 00:38:42 +00:00
|
|
|
(make-frame-visible (window-frame window))
|
|
|
|
(unwind-protect
|
|
|
|
(progn
|
|
|
|
(select-window window)
|
|
|
|
(run-hooks 'temp-buffer-show-hook))
|
2012-07-25 11:43:12 +02:00
|
|
|
(when (window-live-p old-selected)
|
|
|
|
(select-window old-selected))
|
2008-04-26 02:32:33 +00:00
|
|
|
(message "See buffer %s." out-buffer-name)))
|
1994-08-14 09:22:39 +00:00
|
|
|
(message "%s" (buffer-substring (point-min) (point)))
|
1995-05-07 00:38:42 +00:00
|
|
|
))))))
|
2008-04-26 02:32:33 +00:00
|
|
|
(with-output-to-temp-buffer out-buffer-name
|
|
|
|
(pp expression)
|
2004-03-22 15:32:24 +00:00
|
|
|
(with-current-buffer standard-output
|
|
|
|
(emacs-lisp-mode)
|
2007-08-03 03:15:33 +00:00
|
|
|
(setq buffer-read-only nil)
|
2004-03-22 15:32:24 +00:00
|
|
|
(set (make-local-variable 'font-lock-verbose) nil)))))
|
1994-08-14 09:22:39 +00:00
|
|
|
|
1995-01-03 23:44:44 +00:00
|
|
|
;;;###autoload
|
2008-04-26 02:32:33 +00:00
|
|
|
(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'."
|
|
|
|
(interactive
|
|
|
|
(list (read-from-minibuffer "Eval: " nil read-expression-map t
|
|
|
|
'read-expression-history)))
|
|
|
|
(message "Evaluating...")
|
|
|
|
(setq values (cons (eval expression) 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)))
|
|
|
|
(pp-display-expression (macroexpand expression) "*Pp Macroexpand Output*"))
|
|
|
|
|
|
|
|
(defun pp-last-sexp ()
|
|
|
|
"Read sexp before point. Ignores leading comment characters."
|
1994-08-14 09:22:39 +00:00
|
|
|
(let ((stab (syntax-table)) (pt (point)) start exp)
|
|
|
|
(set-syntax-table emacs-lisp-mode-syntax-table)
|
|
|
|
(save-excursion
|
|
|
|
(forward-sexp -1)
|
|
|
|
;; If first line is commented, ignore all leading comments:
|
2012-02-05 03:09:35 +01:00
|
|
|
(if (save-excursion (beginning-of-line) (looking-at-p "[ \t]*;"))
|
1994-08-14 09:22:39 +00:00
|
|
|
(progn
|
|
|
|
(setq exp (buffer-substring (point) pt))
|
|
|
|
(while (string-match "\n[ \t]*;+" exp start)
|
|
|
|
(setq start (1+ (match-beginning 0))
|
|
|
|
exp (concat (substring exp 0 start)
|
|
|
|
(substring exp (match-end 0)))))
|
|
|
|
(setq exp (read exp)))
|
|
|
|
(setq exp (read (current-buffer)))))
|
|
|
|
(set-syntax-table stab)
|
2008-04-26 02:32:33 +00:00
|
|
|
exp))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun pp-eval-last-sexp (arg)
|
|
|
|
"Run `pp-eval-expression' on sexp before point.
|
|
|
|
With argument, pretty-print output into current buffer.
|
|
|
|
Ignores leading comment characters."
|
|
|
|
(interactive "P")
|
|
|
|
(if arg
|
|
|
|
(insert (pp-to-string (eval (pp-last-sexp))))
|
|
|
|
(pp-eval-expression (pp-last-sexp))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun pp-macroexpand-last-sexp (arg)
|
|
|
|
"Run `pp-macroexpand-expression' on sexp before point.
|
|
|
|
With argument, pretty-print output into current buffer.
|
|
|
|
Ignores leading comment characters."
|
|
|
|
(interactive "P")
|
|
|
|
(if arg
|
|
|
|
(insert (pp-to-string (macroexpand (pp-last-sexp))))
|
|
|
|
(pp-macroexpand-expression (pp-last-sexp))))
|
1994-08-14 09:22:39 +00:00
|
|
|
|
|
|
|
;;; Test cases for quote
|
|
|
|
;; (pp-eval-expression ''(quote quote))
|
|
|
|
;; (pp-eval-expression ''((quote a) (quote b)))
|
|
|
|
;; (pp-eval-expression ''('a 'b)) ; same as above
|
|
|
|
;; (pp-eval-expression ''((quote (quote quote)) (quote quote)))
|
|
|
|
;; These do not satisfy the quote test.
|
|
|
|
;; (pp-eval-expression ''quote)
|
|
|
|
;; (pp-eval-expression ''(quote))
|
|
|
|
;; (pp-eval-expression ''(quote . quote))
|
|
|
|
;; (pp-eval-expression ''(quote a b))
|
|
|
|
;; (pp-eval-expression ''(quotefoo))
|
|
|
|
;; (pp-eval-expression ''(a b))
|
|
|
|
|
|
|
|
(provide 'pp) ; so (require 'pp) works
|
|
|
|
|
2001-07-16 12:23:00 +00:00
|
|
|
;;; pp.el ends here
|