繁体   English   中英

如何在emacs中将哈希表导入组织模式?

[英]how to import a hash-table into an org-mode in emacs?

我有一个哈希表,想将哈希表导出到组织缓冲区中。 哈希表应打印到组织缓冲区的内容:获取键,如果键的值不是哈希值,则为“ ::”;否则,如果键的值为哈希表,则键为标题,依此类推上。 问:1.我找不到是否已经实现了对组织缓冲区的“导入”。 如果有,有人可以指出我吗? 2.有人写过类似的东西吗? 我可以做到(这似乎很简单),但不愿重新发明轮子。 如果已经有一个可以采用结构(哈希表)并将其导入到组织缓冲区的库,那就太好了。

谢谢。

我已经提供了该哈希表应在组织缓冲区和原始哈希表中表示的输出示例。

* 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)))

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

(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)))
    ))

注释部分不允许我添加多余的字符,因此无法在此处发布此代码。

谢谢您的解决方案,整洁!!

我是elisp的新手,并不完全了解所有功能。 确实像“制作字符串”一样,我的选择是在每次聆听时保留一张星星列表,然后在必要时将它们连接起来。 我问了问题后不久,我想出了解决方案。 我喜欢你的方法。

关于我的哈希的几点说明是,所有键都是符号,因此:'(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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM