簡體   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