枚举所有org-mode标签

8
如何生成一个有序列表,列出org-mode文件中所有标签(例如:tag:)?假设我有以下形式的列表:
* Head1        :foo:bar:
** Subhead1    :foo:
* Head2
** Subhead2    :foo:bar:

我想要生成一个包含此文件中所有标签以及每个标签使用次数的列表。例如:
:foo:    3
:bar:    2  
3个回答

5
这是一个较短的版本。
(defun get-tag-counts ()
  (let ((all-tags '()))
    (org-map-entries
     (lambda ()
       (let ((tag-string (car (last (org-heading-components)))))
     (when tag-string   
       (setq all-tags
         (append all-tags (split-string tag-string ":" t)))))))


    ;; now get counts
    (loop for tag in (-uniq all-tags) 
      collect (cons tag (cl-count tag all-tags :test 'string=)))))

1
我无法使用John Kitchin发布的代码,因为它需要交互式函数。来自IRC freenode/#emacs的bpalmer非常乐意帮助我。请找到一个可行的示例,它会在相应树下方输出所有标签。
; use this in order to be able to use loop on its own
(require 'cl)
 
;; count tags (see John's answer)
(defun get-tag-counts ()
  (let ((all-tags '()))
    (org-map-entries
     (lambda ()
       (let ((tag-string (car (last (org-heading-components)))))
         (when tag-string  
           (setq all-tags
                 (append all-tags (split-string tag-string ":" t)))))))
 
    ;; now get counts
    (loop for tag in (seq-uniq all-tags)
          collect (cons tag (cl-count tag all-tags :test 'string=)))))

;; wrap get-tag-counts in an interactive function
(defun create-tag-counts-buffer ()
  (interactive)
  (let ((tags (get-tag-counts)) (b (get-buffer-create "*Org Tag Count*")))
    (dolist (tag tags) (insert (car tag)) (insert "\n")) (display-buffer b)))

0

这里有一种方法。

(setq my-hash (make-hash-table :test 'equal))

(org-map-entries
 (lambda ()
   (let ((tag-string (car (last (org-heading-components))))
     (current-count))
     (when tag-string   
       (dolist (tag (split-string tag-string ":" t))

      (setq current-count (gethash tag my-hash))
     (if current-count;
         (puthash tag (+ 1 current-count)  my-hash)
       (puthash tag 1 my-hash))
     )
       )
     )
   )
 )

;; https://github.com/Wilfred/ht.el
(require 'ht)
(ht-map
 (lambda (key value)
   (list key value))
  my-hash)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接