[Top][All Lists]

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

Re: converting octal escape sequences to utf-8 and back

From: Thien-Thi Nguyen
Subject: Re: converting octal escape sequences to utf-8 and back
Date: Sun, 29 May 2011 10:50:01 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

() Roland Winkler <address@hidden>
() Sun, 29 May 2011 01:58:30 -0500

   How can I achieve that I actually get this in
   an output file, too?

In *scratch*, i bind ‘C-j’ to ‘ppq’[0], reproduced here:

  (defun ppq (&optional replace)
    (interactive "P")
    (when replace
      (delete-region (point) (progn (forward-sexp 1) (point))))
      (insert "\n")
      (pp-eval-last-sexp t)
      (when (bolp)
        (delete-char -1))))

The function ‘pp-eval-last-sexp’ winds up calling ‘pp-to-string’.
You can finangle it to achieve the desired result like so:

  (defvar pp-to-string-double-backslash t)
  (defun pp-to-string (object)
    "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."
    (with-current-buffer (generate-new-buffer " pp-to-string")
            (lisp-mode-variables nil)
            (set-syntax-table emacs-lisp-mode-syntax-table)
            (let ((print-escape-newlines pp-escape-newlines)
                  (print-quoted t))
              (prin1 object (current-buffer)))
            ;; Begin finangling.
            (when pp-to-string-double-backslash
              (goto-char (point-min))
              (while (search-forward "\\" nil t)
                (replace-match "\\\\" t t)))
            ;; End finangling.
        (kill-buffer (current-buffer)))))

This was the five-minute top-down way.  The cleaner bottom-up way
is to specify an appropriate PRINTCHARFUN to ‘prin1’, which is at
the heart of all pp funcs.  Perhaps someone else can post that.

[0] http://www.gnuvola.org/software/personal-elisp/dist/lisp/prog-env/ppq.el

reply via email to

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