diff options
| author | Noam Postavsky | 2017-03-02 22:37:03 -0500 |
|---|---|---|
| committer | Noam Postavsky | 2017-03-02 23:03:20 -0500 |
| commit | f5388ba8a7f3970afd0e2bcc52c834ae56178442 (patch) | |
| tree | 125960f2cedd2c4586e8b2ebcf33d926eae4c54c | |
| parent | 55c0c3e31bc3dff83753cdba6288228bd025ac84 (diff) | |
| download | emacs-f5388ba8a7f3970afd0e2bcc52c834ae56178442.tar.gz emacs-f5388ba8a7f3970afd0e2bcc52c834ae56178442.zip | |
Switch pp.el to lexical binding
Additionally, do some minor code cleanup.
* lisp/emacs-lisp/pp.el: Set lexical-binding.
(pp-buffer): Use skip-syntax-forward.
(pp-eval-expression): Use push.
(pp-last-sexp): Use with-syntax-table.
* test/lisp/emacs-lisp/pp-tests.el: New tests.
| -rw-r--r-- | lisp/emacs-lisp/pp.el | 51 | ||||
| -rw-r--r-- | test/lisp/emacs-lisp/pp-tests.el | 35 |
2 files changed, 53 insertions, 33 deletions
diff --git a/lisp/emacs-lisp/pp.el b/lisp/emacs-lisp/pp.el index 2938c37e8a8..7ef46a48bde 100644 --- a/lisp/emacs-lisp/pp.el +++ b/lisp/emacs-lisp/pp.el | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | ;;; pp.el --- pretty printer for Emacs Lisp | 1 | ;;; pp.el --- pretty printer for Emacs Lisp -*- lexical-binding: t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 1989, 1993, 2001-2017 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 1989, 1993, 2001-2017 Free Software Foundation, Inc. |
| 4 | 4 | ||
| @@ -67,8 +67,7 @@ to make output that `read' can handle, whenever this is possible." | |||
| 67 | (progn (skip-chars-backward " \t\n") (point))) | 67 | (progn (skip-chars-backward " \t\n") (point))) |
| 68 | (insert "\n")))) | 68 | (insert "\n")))) |
| 69 | ((ignore-errors (up-list 1) t) | 69 | ((ignore-errors (up-list 1) t) |
| 70 | (while (looking-at-p "\\s)") | 70 | (skip-syntax-forward ")") |
| 71 | (forward-char 1)) | ||
| 72 | (delete-region | 71 | (delete-region |
| 73 | (point) | 72 | (point) |
| 74 | (progn (skip-chars-forward " \t\n") (point))) | 73 | (progn (skip-chars-forward " \t\n") (point))) |
| @@ -129,7 +128,7 @@ Also add the value to the front of the list in the variable `values'." | |||
| 129 | (interactive | 128 | (interactive |
| 130 | (list (read--expression "Eval: "))) | 129 | (list (read--expression "Eval: "))) |
| 131 | (message "Evaluating...") | 130 | (message "Evaluating...") |
| 132 | (setq values (cons (eval expression lexical-binding) values)) | 131 | (push (eval expression lexical-binding) values) |
| 133 | (pp-display-expression (car values) "*Pp Eval Output*")) | 132 | (pp-display-expression (car values) "*Pp Eval Output*")) |
| 134 | 133 | ||
| 135 | ;;;###autoload | 134 | ;;;###autoload |
| @@ -141,22 +140,21 @@ Also add the value to the front of the list in the variable `values'." | |||
| 141 | 140 | ||
| 142 | (defun pp-last-sexp () | 141 | (defun pp-last-sexp () |
| 143 | "Read sexp before point. Ignores leading comment characters." | 142 | "Read sexp before point. Ignores leading comment characters." |
| 144 | (let ((stab (syntax-table)) (pt (point)) start exp) | 143 | (with-syntax-table emacs-lisp-mode-syntax-table |
| 145 | (set-syntax-table emacs-lisp-mode-syntax-table) | 144 | (let ((pt (point))) |
| 146 | (save-excursion | 145 | (save-excursion |
| 147 | (forward-sexp -1) | 146 | (forward-sexp -1) |
| 148 | ;; If first line is commented, ignore all leading comments: | 147 | (read |
| 149 | (if (save-excursion (beginning-of-line) (looking-at-p "[ \t]*;")) | 148 | ;; If first line is commented, ignore all leading comments: |
| 150 | (progn | 149 | (if (save-excursion (beginning-of-line) (looking-at-p "[ \t]*;")) |
| 151 | (setq exp (buffer-substring (point) pt)) | 150 | (let ((exp (buffer-substring (point) pt)) |
| 152 | (while (string-match "\n[ \t]*;+" exp start) | 151 | (start nil)) |
| 153 | (setq start (1+ (match-beginning 0)) | 152 | (while (string-match "\n[ \t]*;+" exp start) |
| 154 | exp (concat (substring exp 0 start) | 153 | (setq start (1+ (match-beginning 0)) |
| 155 | (substring exp (match-end 0))))) | 154 | exp (concat (substring exp 0 start) |
| 156 | (setq exp (read exp))) | 155 | (substring exp (match-end 0))))) |
| 157 | (setq exp (read (current-buffer))))) | 156 | exp) |
| 158 | (set-syntax-table stab) | 157 | (current-buffer))))))) |
| 159 | exp)) | ||
| 160 | 158 | ||
| 161 | ;;;###autoload | 159 | ;;;###autoload |
| 162 | (defun pp-eval-last-sexp (arg) | 160 | (defun pp-eval-last-sexp (arg) |
| @@ -178,19 +176,6 @@ Ignores leading comment characters." | |||
| 178 | (insert (pp-to-string (macroexpand-1 (pp-last-sexp)))) | 176 | (insert (pp-to-string (macroexpand-1 (pp-last-sexp)))) |
| 179 | (pp-macroexpand-expression (pp-last-sexp)))) | 177 | (pp-macroexpand-expression (pp-last-sexp)))) |
| 180 | 178 | ||
| 181 | ;;; Test cases for quote | ||
| 182 | ;; (pp-eval-expression ''(quote quote)) | ||
| 183 | ;; (pp-eval-expression ''((quote a) (quote b))) | ||
| 184 | ;; (pp-eval-expression ''('a 'b)) ; same as above | ||
| 185 | ;; (pp-eval-expression ''((quote (quote quote)) (quote quote))) | ||
| 186 | ;; These do not satisfy the quote test. | ||
| 187 | ;; (pp-eval-expression ''quote) | ||
| 188 | ;; (pp-eval-expression ''(quote)) | ||
| 189 | ;; (pp-eval-expression ''(quote . quote)) | ||
| 190 | ;; (pp-eval-expression ''(quote a b)) | ||
| 191 | ;; (pp-eval-expression ''(quotefoo)) | ||
| 192 | ;; (pp-eval-expression ''(a b)) | ||
| 193 | |||
| 194 | (provide 'pp) ; so (require 'pp) works | 179 | (provide 'pp) ; so (require 'pp) works |
| 195 | 180 | ||
| 196 | ;;; pp.el ends here | 181 | ;;; pp.el ends here |
diff --git a/test/lisp/emacs-lisp/pp-tests.el b/test/lisp/emacs-lisp/pp-tests.el new file mode 100644 index 00000000000..b9ed79c7749 --- /dev/null +++ b/test/lisp/emacs-lisp/pp-tests.el | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | ;;; pp-tests.el --- Test suite for pretty printer. -*- lexical-binding: t -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2017 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; This file is part of GNU Emacs. | ||
| 6 | |||
| 7 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 8 | ;; it under the terms of the GNU General Public License as published by | ||
| 9 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 10 | ;; (at your option) any later version. | ||
| 11 | |||
| 12 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;; GNU General Public License for more details. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Code: | ||
| 21 | |||
| 22 | (require 'pp) | ||
| 23 | |||
| 24 | (ert-deftest pp-print-quote () | ||
| 25 | (should (string= (pp-to-string 'quote) "quote")) | ||
| 26 | (should (string= (pp-to-string ''quote) "'quote")) | ||
| 27 | (should (string= (pp-to-string '('a 'b)) "('a 'b)\n")) | ||
| 28 | (should (string= (pp-to-string '(''quote 'quote)) "(''quote 'quote)\n")) | ||
| 29 | (should (string= (pp-to-string '(quote)) "(quote)\n")) | ||
| 30 | (should (string= (pp-to-string '(quote . quote)) "(quote . quote)\n")) | ||
| 31 | (should (string= (pp-to-string '(quote a b)) "(quote a b)\n")) | ||
| 32 | (should (string= (pp-to-string '(quotefoo)) "(quotefoo)\n")) | ||
| 33 | (should (string= (pp-to-string '(a b)) "(a b)\n"))) | ||
| 34 | |||
| 35 | ;;; pp-tests.el ends here. | ||