[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] building tagcloud datastructure in elisp

From: Jonathan Leech-Pepin
Subject: Re: [O] building tagcloud datastructure in elisp
Date: Wed, 12 Sep 2012 19:34:09 -0400

Hello Marcello,

On 12 September 2012 14:41, Marcelo de Moraes Serpa <address@hidden> wrote:
> Hi list,
> How hard would it be to parse a bunch of org files and build an elisp data
> structure (Hash?) that represents a tagcloud? All tags in all headlines and
> subtrees should be taken into account (for all org files that are parsed).
> Could I use org-element to help me parse this or is there a better way?
> I'm just learning the org API, and I've only done a bunch of elisp hacks, so
> any insight would be greatly appreciated!

I'm learning as well, mostly by providing a feature I could use, or by
seeing a problem I find interesting and deciding I want to find a
solution to it.

> Thanks,
> - Marcelo.

Org-element doesn't seem to include tag-inheritance when providing
tags for a given headline, so counting inherited tags becomes slightly
more complex.

The following should provide what you want:

#+begin_src emacs-lisp
  (defun zin/org-tag-cloud-freq (&optional inherit file)
    "Return an alist containing tag and frequency.

  When INHERIT is given, the frequency of a tag includes the number
  of subheadings (to indicate tag inheritance).  FILE allows for an
  arbitrary file to be retrieved and used for tag counting."
    (interactive "P")
    (when file
      (find-file file))
    (let* ((source (org-element-parse-buffer 'headline))
           (tags (org-element-map
                  source 'headline
                  (lambda (headline)
                    (let ((tags (org-export-get-tags headline source))
                          (count (if inherit
                                     (length (org-element-map headline
'headline 'identity))
                      (list tags count)))))
      (setq taglist
            (mapcar (lambda (s)
                      (when (car s)
                        (loop for item in (car s) collect
                              (list item (cadr s))))) tags))
      (setq taglist
            (loop for item in taglist append item))
      (dolist (tag taglist result)
        (let* ((tagitem (car tag))
               (tagcount (cadr tag))
               (sofar (assoc tagitem result)))
          (if sofar
              (setcdr sofar (+ tagcount (cdr sofar)))
            (push (cons tagitem tagcount) result))))
      (format "%s" result)))

  (defun zin/org-tag-freq-list (files &optional inherit)
    "List of files to be processed by `zin/org-tag-cloud-freq'.

  Returns a single alist of tag counts."
    (let (result)
      (dolist (file files result)
        (let ((entries (zin/org-tag-cloud-freq inherit file)))
          (loop for tag in entries do
                (let ((tagitem (car tag))
                      (tagcount (cdr tag))
                      (sofar (assoc tagitem result)))
                  (if sofar
                      (setcdr sofar (+ tagcount (cdr sofar)))
                    (push (cons tagitem tagcount) result))))))
      (format "%s" result)))

The dolist loop for counting the tags themselves comes from
There may be a cleaner way to obtain the list of tags and associated
counts but this provides the values.

The first function will work on any Org buffer to return the list of
tags while the second will do so for a list of org files (for example

I hope this helps



reply via email to

[Prev in Thread] Current Thread [Next in Thread]