简体   繁体   English

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

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

I have a hash-table and would like to export the hash-tables into an org-buffer. 我有一个哈希表,想将哈希表导出到组织缓冲区中。 What the hash-table should print out to an org-buffer: take the keys, if the key's value is not a hash then it's ":: " otherwise if the key's value is a hash-table then the key is a heading and so on. 哈希表应打印到组织缓冲区的内容:获取键,如果键的值不是哈希值,则为“ ::”;否则,如果键的值为哈希表,则键为标题,依此类推上。 Q. 1. I couldn't find if there is an 'import' into the org-buffer implemented already. 问:1.我找不到是否已经实现了对组织缓冲区的“导入”。 If there is, can someone point me to it? 如果有,有人可以指出我吗? 2. Has someone written anything similar to this? 2.有人写过类似的东西吗? I can do it (it seems simple enough) but would hate to reinvent the wheel. 我可以做到(这似乎很简单),但不愿重新发明轮子。 If there is a library already out there that can take a structure (hash-table) and import it into an org-buffer, that would be awesome. 如果已经有一个可以采用结构(哈希表)并将其导入到组织缓冲区的库,那就太好了。

Thanks. 谢谢。

I have provided an example of the output of what that hash-table should be represented into the org-buffer and the raw hash-table. 我已经提供了该哈希表应在组织缓冲区和原始哈希表中表示的输出示例。

* 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

Raw-hash (i have a list which has only one hash in it): 原始哈希(我的列表中只有一个哈希):

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

I have stolen you all the fun;-). 我把所有的乐趣都偷走了;-)。 The output is a bit different to what you suggested. 输出与您建议的有所不同。 I do not indent the headers of the leaves so that org recognizes the structure. 我不缩进叶子的标题,以便组织识别结构。

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

The comment section would not allow me extra characters so I couldn't post this code there. 注释部分不允许我添加多余的字符,因此无法在此处发布此代码。

Thank you for your solution, it's neat!! 谢谢您的解决方案,整洁!!

I am new to elisp and do not know all the functions properly. 我是elisp的新手,并不完全了解所有功能。 Really like the 'make-string', my alternative is that I keep a list of stars per heeding and then concat them wherever necessary. 确实像“制作字符串”一样,我的选择是在每次聆听时保留一张星星列表,然后在必要时将它们连接起来。 I came up with solution shortly after i asked the question. 我问了问题后不久,我想出了解决方案。 I like your approach. 我喜欢你的方法。

A few notes about my hash is that all the keys are symbols hence this: '(symbol-name k)' 关于我的哈希的几点说明是,所有键都是符号,因此:'(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