如何读取defcustom参数值且保留原格式?

这是要获取值的对象:(源: separedit.el)

(defcustom separedit-block-regexp-plists
          '((:header "```\s?\\([^\s\n\r]*\\)$"
                     :body   ""
                     :footer "```$")

            (:header "#\\+BEGIN_SRC\s\\([^\s\n\r]*\\).*$"
                     :body   ""
                     :footer "#\\+END_SRC$")
            ;;; 省略。
            )
          "文档省略。"
          :group 'separedit
          :type 'alist)

获取值:

(format "%S" (get 'separedit-block-regexp-plists 'standard-value))

==> 打印出来的效果:

请教:应该使用什么方法才能使输出与原格式一样:

  '((:header "```\s?\\([^\s\n\r]*\\)$"
             :body   ""
             :footer "```$")

    (:header "#\\+BEGIN_SRC\s\\([^\s\n\r]*\\).*$"
             :body   ""
             :footer "#\\+END_SRC$")
    ;;; 省略。
    )

pp 满足op需求吗?

嗯,pp 比较可读。研究了一下其他几个: print princ prin1 princ1-to-string 格式都偏机器语言。多谢 :grinning:

(insert (pp (get 'separedit-block-regexp-plists 'standard-value)))

补充:意外发现

  1. ‘pp’ 和 ‘pp-macroexpand-last-sexp’ 是在同一个包:pp.el ,新世界的大门打开
  2. pp = pretty printer

从源文件才能得到原格式:

(insert
 (let* ((sym 'separedit-block-regexp-plists)
        (location (find-variable-noselect sym (symbol-file sym))))
   (with-current-buffer (car location)
     (goto-char (cdr location))
     (down-list)
     (forward-sexp 3)
     (let ((bounds (bounds-of-thing-at-point 'sexp)))
       (buffer-substring-no-properties (car bounds) (cdr bounds))))))
2 个赞

完美复制。 :grinning:

学到了新用法,感谢大佬!