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: Tue, 04 May 2004 19:48:21 +0300 Organization: JURTA Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <8765bcdtpm.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=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: sea.gmane.org 1083689931 731 80.91.224.253 (4 May 2004 16:58:51 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 4 May 2004 16:58:51 +0000 (UTC) Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Tue May 04 18:58:42 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 1BL3Fi-0005i6-00 for ; Tue, 04 May 2004 18:58:42 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1BL3Fi-0005Ag-00 for ; Tue, 04 May 2004 18:58:42 +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 1BL3DS-0000k5-3R for emacs-devel@quimby.gnus.org; Tue, 04 May 2004 12:56:22 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.30) id 1BL3D2-0000gj-0I for emacs-devel@gnu.org; Tue, 04 May 2004 12:55:56 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.30) id 1BL3CV-0000Qw-PT for emacs-devel@gnu.org; Tue, 04 May 2004 12:55:54 -0400 Original-Received: from [66.33.219.6] (helo=knife.dreamhost.com) by monty-python.gnu.org with esmtp (Exim 4.30) id 1BL3B0-0008Ix-36 for emacs-devel@gnu.org; Tue, 04 May 2004 12:53:50 -0400 Original-Received: from mail.jurta.org (80-235-34-218-dsl.mus.estpak.ee [80.235.34.218]) by knife.dreamhost.com (Postfix) with ESMTP id 4138BE40D9 for ; Tue, 4 May 2004 09:53:46 -0700 (PDT) Original-To: emacs-devel@gnu.org In-Reply-To: (Johan =?iso-8859-1?q?Bockg=E5rd's?= message of "Tue, 04 May 2004 17:57:21 +0200") 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:22721 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:22721 bojohan+news@dd.chalmers.se (Johan Bockg=E5rd) writes: > Juri Linkov writes: >> +(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 (str (pp-to-string (save-excursion >> + (prog1 (read (current-buffer)) > ^ > But this would also lead to all comments being removed , right? Right. I already thought about this problem recently and tried to solve it somehow. Below is the version that calls `pp-to-string' with a string with all comments preserved, and removes newlines except at comments. However, it still don't work right and I'm not sure I should continue to modify `pp-to-string'. That is why I asked on this list today about such existing function that can format Emacs Lisp code prettier than `pp'. (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 (if (and transient-mark-mode mark-active) (buffer-substring (region-beginning) (setq p (region-end))) (buffer-substring (point) (save-excursion (forward-sexp 1) (setq p (point))))) t))) (delete-region (point) p) (save-excursion (insert s))) (indent-sexp))) diff -u pp.el~ pp.el --- pp.el~ 2004-03-23 07:11:29 +++ pp.el 2004-05-04 11:29:17 @@ -37,7 +37,7 @@ =20 ;;;###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." @@ -49,7 +49,19 @@ (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)) + ;; Remove newlines except ones at comments + (comment-normalize-vars) + (goto-char (point-min)) + (goto-char (line-end-position)) + (while (not (eobp)) + (if (not (comment-beginning)) + (delete-char 1) + (forward-line 1)) + (goto-char (line-end-position))))) (goto-char (point-min)) (while (not (eobp)) --=20 Juri Linkov http://www.jurta.org/emacs/