unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@jurta.org>
Cc: emacs-devel@gnu.org, bojohan+news@dd.chalmers.se
Subject: Re: Insert character pairs
Date: Thu, 06 May 2004 16:14:11 +0300	[thread overview]
Message-ID: <871xlxofz0.fsf@mail.jurta.org> (raw)
In-Reply-To: <E1BLSsY-0004wJ-72@fencepost.gnu.org> (Richard Stallman's message of "Wed, 05 May 2004 16:20:30 -0400")

Richard Stallman <rms@gnu.org> 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/

  reply	other threads:[~2004-05-06 13:14 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-29  8:48 Insert character pairs Juri Linkov
2004-04-29 10:12 ` Andreas Schwab
2004-04-29 11:00   ` Juri Linkov
2004-04-29 11:21     ` Andreas Schwab
2004-04-29 11:59       ` Juri Linkov
2004-04-29 14:24 ` Stefan Monnier
2004-04-29 15:02   ` Juri Linkov
2004-04-29 16:57     ` Stefan Monnier
2004-04-30  0:48       ` Juri Linkov
2004-04-30 13:43         ` Stefan Monnier
2004-04-30 23:22           ` Juri Linkov
2004-05-01  8:19 ` Juri Linkov
2004-05-02 16:19   ` Juri Linkov
2004-05-03 14:03     ` Richard Stallman
2004-05-04 15:57     ` Johan Bockgård
2004-05-04 16:48       ` Juri Linkov
2004-05-05 20:20       ` Richard Stallman
2004-05-06 13:14         ` Juri Linkov [this message]
2004-05-06 17:19           ` Stefan Monnier
2004-05-08 21:43             ` Juri Linkov
2004-05-03 19:48   ` Kevin Rodgers
2004-05-04  5:51     ` Juri Linkov
2004-05-04 18:30       ` Kevin Rodgers
2004-05-04 19:32         ` Juri Linkov
2004-05-05 20:20           ` Richard Stallman
2004-05-06 12:02             ` Juri Linkov
2004-05-01 20:26 ` Stefan Monnier
2004-05-02 16:03   ` Juri Linkov
2004-05-02 16:41     ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871xlxofz0.fsf@mail.jurta.org \
    --to=juri@jurta.org \
    --cc=bojohan+news@dd.chalmers.se \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).