【问题标题】:how to import a hash-table into an org-mode in emacs?如何将哈希表导入emacs中的组织模式?
【发布时间】:2014-11-17 08:21:21
【问题描述】:

我有一个哈希表,想将哈希表导出到一个组织缓冲区。 哈希表应该打印到组织缓冲区的内容: 取键,如果键的值不是散列,那么它是“::”,否则如果键的值是散列表,那么键是标题,依此类推。 问。 1. 我找不到已经实现的 org-buffer 中是否有“导入”。如果有,有人可以指点我吗? 2. 有人写过类似的吗?我可以做到(看起来很简单),但不愿意重新发明轮子。如果已经有一个库可以采用结构(哈希表)并将其导入到 org-buffer 中,那就太棒了。

谢谢。

我提供了一个输出示例,说明该哈希表应在 org-buffer 和原始哈希表中表示的内容。

* key "project-example" :id: "12345" ** affected-versions :id: "12332" :name: "SlimShady" :archived: nil :release-date: "2014-10-01T04:00:00.000Z" :released: nil :sequence: 81 :assigned-to: "m&m" :attach-name: nil ** components :id: "3214" :name: "Dr.Dre" :created: "2014-11-13T15:49:15.000Z" ** customer-fld-vals: :custom-fld-id: "cust-id-112233" :key: nil :values: "Fill me" :description: nil :duedate: nil :environment: nil :fixVersions: nil :key: "project-example" :priority: "high" :project: "EX" :reporter: "YourName" :resolution: "xx" :status: "xx" :summary: "Write something here" :type: "xx" :updated: "2014-11-15T22:52:13.000Z" :votes: 0

原始哈希(我有一个列表,其中只有一个哈希):

((hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data (id "12345" affected-versions #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data (id "12332" name "SlimShady" archived nil release-date "2014-10-01T04:00:00.000Z" released nil sequence 81)) assigned-to "m&m" attach-name nil components #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data (id "3214" name "Dr.Dre")) created "2014-11-13T15:49:15.000Z" customer-fld-vals #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data (customfieldId "cust-id-112233" key nil values ("Fill me"))) description nil duedate nil environment nil fixVersions nil key "project-example" priority "high" project "EX" reporter "YourName" resolution "xx" status "xx" summary "Write something here" type "xx" updated "2014-11-15T22:52:13.000Z" votes 0)))

【问题讨论】:

    标签: emacs hash elisp org-mode emacs24


    【解决方案1】:

    我偷走了你所有的乐趣;-)。输出与您建议的有点不同。我不缩进叶子的标题,以便 org 识别结构。

    (defun org-import-hash (title hash &optional level noindent)
      "Import HASH table into an org buffer.
    Put TITLE into the first heading.
    The hash table starts with LEVEL where LEVEL defaults to 1.
    Indent inserted region by org-mode unless NOINDENT is non-nil."
      (interactive "sTitle:\nXHash-table:")
      (unless level (setq level 1))
      (let ((b (point)))
        (insert (if (or (looking-back "\n") (= (point) 1)) "" "\n") (make-string level ?*) (format " %s\n" title))
        (maphash (lambda (key val)
               (cond
            ((hash-table-p val)
             (org-import-hash key val (1+ level) t))
            ;; other special cases here
            (t
             (insert (format " :%s: %s\n" key val)))
            ))
             hash)
        (unless noindent
          (indent-region b (point)))
        ))
    

    【讨论】:

    • 抱歉,我还不能“投票”你的答案,我需要更多的声誉。
    【解决方案2】:

    评论部分不允许我使用额外的字符,所以我无法在此处发布此代码。

    感谢您的解决方案,很好!

    我是 elisp 的新手,不了解所有功能。真的很喜欢'make-string',我的替代方案是我保留一个明星列表,然后在必要时将它们连接起来。 在我提出问题后不久,我想出了解决方案。我喜欢你的方法。

    关于我的哈希的一些注意事项是所有键都是符号,因此:'(symbol-name k)'

    (defun ->string (whatever)
      (cond
       ((equal 'integer (type-of whatever)) (number-to-string whatever))
       ((equal 'cons (type-of whatever)) (mapconcat 'identity (mapcar '->string whatever) " " ))
       ((equal 'string (type-of whatever)) whatever)))
    
    (defun out->print (dstring)
      (print dstring))
    
    (defun out->buffer (dstring)
      (insert dstring))
    
    (defun print-issues (dhash stars)
      (maphash (lambda (k v)
                 (progn (if (equal 'hash-table (type-of v))
                            (progn
                              (let ((nstars (cons "*" stars)))
                                (out->buffer  (concat (apply 'concat nstars) " " (symbol-name k) "\n"))
                                (print-issues v nstars)))
                          (out->buffer (concat (replace-regexp-in-string "\*" "\s"
                                                                         (apply 'concat stars))
                                               ":"
                                               (symbol-name k)
                                               ":"
                                               " -- "
                                               (->string v) "\n")))))
               dhash))
    

    【讨论】:

    • 您可以用内置的prin1-to-string 替换->string,它也处理符号。另一种可能性是format 与令牌%S%s。此外,您似乎并没有真正使用out->print
    • out->print 我刚刚添加它是为了在屏幕上看到它。我正在尝试尽可能多地分离关注点。
    • 将替换 ->string 函数。谢谢
    猜你喜欢
    • 2014-05-24
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多