From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.devel Subject: Re: Insert character pairs Date: Thu, 06 May 2004 16:14:11 +0300 Organization: JURTA Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <871xlxofz0.fsf@mail.jurta.org> References: <87brlb1840.fsf@mail.jurta.org> <87smeka7aj.fsf@mail.jurta.org> <871xm2ol7j.fsf@mail.jurta.org> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1083856543 22973 80.91.224.253 (6 May 2004 15:15:43 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 6 May 2004 15:15:43 +0000 (UTC) Cc: emacs-devel@gnu.org, bojohan+news@dd.chalmers.se Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Thu May 06 17:15:15 2004 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1BLkah-0007qP-00 for ; Thu, 06 May 2004 17:15:15 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1BLkag-0001m9-00 for ; Thu, 06 May 2004 17:15:14 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1BLkSG-00036C-QU for emacs-devel@quimby.gnus.org; Thu, 06 May 2004 11:06:32 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.30) id 1BLjgR-0000Qf-JU for emacs-devel@gnu.org; Thu, 06 May 2004 10:17:07 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.30) id 1BLinl-0003b0-VF for emacs-devel@gnu.org; Thu, 06 May 2004 09:21:09 -0400 Original-Received: from [66.33.219.19] (helo=spoon.dreamhost.com) by monty-python.gnu.org with esmtp (Exim 4.30) id 1BLinl-0003ap-Gn; Thu, 06 May 2004 09:20:37 -0400 Original-Received: from mail.jurta.org (80-235-39-194-dsl.mus.estpak.ee [80.235.39.194]) by spoon.dreamhost.com (Postfix) with ESMTP id BF0EE13D88C; Thu, 6 May 2004 06:20:34 -0700 (PDT) Original-To: rms@gnu.org In-Reply-To: (Richard Stallman's message of "Wed, 05 May 2004 16:20:30 -0400") User-Agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:22857 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:22857 Richard Stallman writes: > > + (let* (p (str (pp-to-string (save-excursion > > + (prog1 (read (current-buffer)) > ^ > But this would also lead to all comments being removed , right? > > Yes, it would. So people would only want to use this in > special situations. I think it should be harmless in all situations. Luckily, it is possible to achieve this easily with minimal modifications in `pp-to-string': Index: emacs/lisp/emacs-lisp/pp.el =================================================================== RCS file: /cvsroot/emacs/emacs/lisp/emacs-lisp/pp.el,v retrieving revision 1.21 diff -u -r1.21 pp.el --- emacs/lisp/emacs-lisp/pp.el 22 Mar 2004 15:32:24 -0000 1.21 +++ emacs/lisp/emacs-lisp/pp.el 6 May 2004 12:07:31 -0000 @@ -37,10 +37,13 @@ :group 'pp) ;;;###autoload -(defun pp-to-string (object) +(defun pp-to-string (object &optional as-string) "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." +to make output that `read' can handle, whenever this is possible. +Optional argument AS-STRING means that OBJECT is treated as +a string containing the printed representation of Lisp data. +The function modifies this string and returns it pretty-printed." (save-excursion (set-buffer (generate-new-buffer " pp-to-string")) (unwind-protect @@ -49,7 +52,9 @@ (set-syntax-table emacs-lisp-mode-syntax-table) (let ((print-escape-newlines pp-escape-newlines) (print-quoted t)) - (prin1 object (current-buffer))) + (if (not (and as-string (stringp object))) + (prin1 object (current-buffer)) + (princ object (current-buffer)))) (goto-char (point-min)) (while (not (eobp)) ;; (message "%06d" (- (point-max) (point))) @@ -60,8 +65,10 @@ (save-excursion (backward-char 1) (skip-chars-backward "'`#^") - (when (and (not (bobp)) (= ?\ (char-before))) - (delete-char -1) + (when (and (not (bobp)) (memq (char-before) '(?\ ?\t ?\n))) + (delete-region + (point) + (progn (skip-chars-backward " \t\n") (point))) (insert "\n")))) ((condition-case err-var (prog1 t (up-list 1)) @@ -70,7 +77,7 @@ (forward-char 1)) (delete-region (point) - (progn (skip-chars-forward " \t") (point))) + (progn (skip-chars-forward " \t\n") (point))) (insert ?\n)) (t (goto-char (point-max))))) (goto-char (point-min)) Index: emacs/lisp/emacs-lisp/lisp-mode.el =================================================================== RCS file: /cvsroot/emacs/emacs/lisp/emacs-lisp/lisp-mode.el,v retrieving revision 1.155 diff -u -r1.155 lisp-mode.el --- emacs/lisp/emacs-lisp/lisp-mode.el 22 Mar 2004 15:31:46 -0000 1.155 +++ emacs/lisp/emacs-lisp/lisp-mode.el 6 May 2004 13:07:32 -0000 @@ -239,6 +239,7 @@ (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map) (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol) (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun) + (define-key emacs-lisp-mode-map "\e\C-q" 'indent-or-pp-sexp) (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap)) (define-key emacs-lisp-mode-map [menu-bar emacs-lisp] (cons "Emacs-Lisp" map)) @@ -369,6 +370,7 @@ (let ((map (make-sparse-keymap))) (set-keymap-parent map lisp-mode-shared-map) (define-key map "\e\C-x" 'eval-defun) + (define-key map "\e\C-q" 'indent-or-pp-sexp) (define-key map "\e\t" 'lisp-complete-symbol) (define-key map "\n" 'eval-print-last-sexp) map) @@ -1083,6 +1085,27 @@ (lisp-indent-line)) (indent-sexp endmark) (set-marker endmark nil)))) + +(defun indent-or-pp-sexp (&optional arg) + "Indent each line of the list or, with argument, pretty-printify the list." + (interactive "P") + (if arg + (let* (p + (s (pp-to-string + (buffer-substring + (point) + (save-excursion + (forward-sexp 1) + (setq p (point)))) + t))) + (delete-region (point) p) + (save-excursion + (insert s) + (if (eq (char-before) ?\n) + (delete-char -1))) + (indent-sexp)) + (indent-sexp))) ;;;; Lisp paragraph filling commands. -- Juri Linkov http://www.jurta.org/emacs/