pretty print in emacs lisp

By Xah Lee. Date: .

pretty print in emacs lisp

discovered the package lisp/emacs-lisp/pp.el.gz

and it's written by Randal Schwartz, famous perl hacker.

;; Copyright (C) 1989, 1993, 2001-2018 Free Software Foundation, Inc.
;; Author: Randal Schwartz
;; Keywords: lisp

use it like this

(pp
'(defun xah-copy-file-path (&optional Dir-path-only-p) "Copy the current buffer's file path or dired path to `kill-ring'. Result is full path. If `universal-argument' is called first, copy only the dir path. If in dired, copy the file/dir cursor is on, or marked files. If a buffer is not file and not dired, copy value of `default-directory' (which is usually the “current” dir when that buffer was created) URL `http://xahlee.info/emacs/emacs/emacs_copy_file_path.html' Version 2018-06-18" (interactive "P") (let ((fpath (if (string-equal major-mode 'dired-mode) (progn (let ((result (mapconcat 'identity (dired-get-marked-files) "\n"))) (if (equal (length result) 0) (progn default-directory ) (progn result)))) (if (buffer-file-name) (buffer-file-name) (expand-file-name default-directory))))) (kill-new (if Dir-path-only-p (progn (message "Directory copied: %s" (file-name-directory fpath)) (file-name-directory fpath)) (progn (message "File path copied: %s" fpath) fpath )))))
)

the output is like this

"(defun xah-copy-file-path
    (&optional Dir-path-only-p)
  \"Copy the current buffer's file path or dired path to `kill-ring'. Result is full path. If `universal-argument' is called first, copy only the dir path. If in dired, copy the file/dir cursor is on, or marked files. If a buffer is not file and not dired, copy value of `default-directory' (which is usually the “current” dir when that buffer was created) URL `http://xahlee.info/emacs/emacs/emacs_copy_file_path.html' Version 2018-06-18\"
  (interactive \"P\")
  (let
      ((xfpath
        (if
            (string-equal major-mode 'dired-mode)
            (progn
              (let
                  ((xresult
                    (mapconcat 'identity
                               (dired-get-marked-files)
                               \"\\n\")))
                (if
                    (equal
                     (length xresult)
                     0)
                    (progn default-directory)
                  (progn xresult))))
          (if
              (buffer-file-name)
              (buffer-file-name)
            (expand-file-name default-directory)))))
    (kill-new
     (if Dir-path-only-p
         (progn
           (message \"Directory copied: %s\"
                    (file-name-directory xfpath))
           (file-name-directory xfpath))
       (progn
         (message \"File path copied: %s\" xfpath)
         xfpath)))))
"

2018-08-04 thanks to Bo Yao

i wonder if it is practical to use this to implement automatic formatting of lisp code.

see Automatic Formatting Emacs Lisp Code

See also: Emacs: Why I Don't Use paredit