emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Jonathan Leech-Pepin <jonathan.leechpepin@gmail.com>
To: Marcelo de Moraes Serpa <celoserpa@gmail.com>
Cc: Org Mode <emacs-orgmode@gnu.org>
Subject: Re: building tagcloud datastructure in elisp
Date: Wed, 12 Sep 2012 19:34:09 -0400	[thread overview]
Message-ID: <CAEWDx5f_KSqTJ4jDB27KRzk0CpdQgGLdtxs=Q8EjXfAFLpTQ3A@mail.gmail.com> (raw)
In-Reply-To: <CACHMzOHitX5sDPu5AN+voFCN15NMAZLsPRXo4Un5t43HOe1gjA@mail.gmail.com>

Hello Marcello,

On 12 September 2012 14:41, Marcelo de Moraes Serpa <celoserpa@gmail.com> 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



      parent reply	other threads:[~2012-09-12 23:34 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-12 18:41 building tagcloud datastructure in elisp Marcelo de Moraes Serpa
2012-09-12 18:58 ` Eric Schulte
2012-09-12 23:34 ` Jonathan Leech-Pepin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAEWDx5f_KSqTJ4jDB27KRzk0CpdQgGLdtxs=Q8EjXfAFLpTQ3A@mail.gmail.com' \
    --to=jonathan.leechpepin@gmail.com \
    --cc=celoserpa@gmail.com \
    --cc=emacs-orgmode@gnu.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).