2014-11-17 136 views
0

我有一個散列表,並希望將散列表導出到組織緩衝區。 散列表應該輸出到組織緩衝區: 獲取關鍵字,如果關鍵字的值不是散列,那麼它是「::」,否則如果關鍵字的值是散列表,則關鍵字是標題等等。 問: 1.我找不到已經實施的組織緩衝區中是否存在「導入」。如果有的話,有人可以指點我嗎? 2.有人寫過類似的東西嗎?我可以做到這一點(這似乎很簡單),但不願意重新發明輪子。如果已經有一個庫可以使用一個結構(散列表)並將其導入到組織緩衝區中,那就太棒了。如何將散列表導入emacs中的組織模式?

謝謝。

我提供了一個散列表應該被表示成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)))

回答

0

我偷了你所有的樂趣;-)。輸出與您所建議的有所不同。我不縮進葉子的標題,以便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))) 
    )) 
+0

對不起,我還不能「給予好評」你的答案,我需要更多的聲譽。 – user2762156

0

評論部分不允許我多餘的字符,所以我不能在這裏發佈此代碼。

謝謝你的解決方案,它很整潔!

我是新來的elisp,並不知道所有功能正常。真的喜歡'make-string',我的另一個選擇是我保留一張星星的名單,然後在必要時連接它們。 我問這個問題後不久就想出瞭解決方案。我喜歡你的方法。

我的散列的幾個注意事項是,所有的鍵符號,因此這一點:「(符號名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)) 
+0

你可以用內置的'prin1-to-string'替換' - > string',它也處理符號。另一種可能性是'格式',標記爲'%S'或'%s'。此外,看起來你並沒有真正使用'out-> print'。 – Tobias

+0

'''out-> print''' 我剛添加它在屏幕上看它。 我試圖儘可能地分離出問題。 – user2762156

+0

將取代''' - > string'''函數。 謝謝 – user2762156