emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Insert character pairs


From: Juri Linkov
Subject: Re: Insert character pairs
Date: Tue, 04 May 2004 19:48:21 +0300
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux)

address@hidden (Johan Bockgård) writes:
> Juri Linkov <address@hidden> 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 @@
 
 ;;;###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))

-- 
Juri Linkov
http://www.jurta.org/emacs/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]