Fixed section-header-prefix for trac wiki.
[org-mode/org-kjn.git] / contrib / lisp / org-collector.el
blob1cf063b8b18496863bfe1caa9aa55bb689c9729a
1 ;;; org-collector --- collect properties into tables
3 ;; Copyright (C) 2008-2011 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte <schulte dot eric at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp, experimentation,
7 ;; organization, properties
8 ;; Homepage: http://orgmode.org
9 ;; Version: 0.01
11 ;; This file is not yet part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; Pass in an alist of columns, each column can be either a single
29 ;; property or a function which takes column names as arguments.
31 ;; For example the following propview block would collect the value of
32 ;; the 'amount' property from each header in the current buffer
34 ;; #+BEGIN: propview :cols (ITEM amount)
35 ;; | "ITEM" | "amount" |
36 ;; |---------------------+----------|
37 ;; | "December Spending" | 0 |
38 ;; | "Grocery Store" | 56.77 |
39 ;; | "Athletic club" | 75.0 |
40 ;; | "Restaurant" | 30.67 |
41 ;; | "January Spending" | 0 |
42 ;; | "Athletic club" | 75.0 |
43 ;; | "Restaurant" | 50.00 |
44 ;; |---------------------+----------|
45 ;; | | |
46 ;; #+END:
48 ;; This slightly more selective propview block will limit those
49 ;; headers included to those in the subtree with the id 'december'
50 ;; in which the spendtype property is equal to "food"
52 ;; #+BEGIN: propview :id "december" :conds ((string= spendtype "food")) :cols (ITEM amount)
53 ;; | "ITEM" | "amount" |
54 ;; |-----------------+----------|
55 ;; | "Grocery Store" | 56.77 |
56 ;; | "Restaurant" | 30.67 |
57 ;; |-----------------+----------|
58 ;; | | |
59 ;; #+END:
61 ;; Org Collector allows arbitrary processing of the property values
62 ;; through elisp in the cols: property. This allows for both simple
63 ;; computations as in the following example
65 ;; #+BEGIN: propview :id "results" :cols (ITEM f d list (apply '+ list) (+ f d))
66 ;; | "ITEM" | "f" | "d" | "list" | "(apply (quote +) list)" | "(+ f d)" |
67 ;; |--------+-----+-----+-------------------------+--------------------------+-----------|
68 ;; | "run1" | 2 | 33 | (quote (9 2 3 4 5 6 7)) | 36 | 35 |
69 ;; | "run2" | 2 | 34 | :na | :na | 36 |
70 ;; | "run3" | 2 | 35 | :na | :na | 37 |
71 ;; | "run4" | 2 | 36 | :na | :na | 38 |
72 ;; | | | | | | |
73 ;; #+END:
75 ;; or more complex computations as in the following example taken from
76 ;; an org file where each header in "results" subtree contained a
77 ;; property "sorted_hits" which was passed through the
78 ;; "average-precision" elisp function
80 ;; #+BEGIN: propview :id "results" :cols (ITEM (average-precision sorted_hits))
81 ;; | "ITEM" | "(average-precision sorted_hits)" |
82 ;; |-----------+-----------------------------------|
83 ;; | run (80) | 0.105092 |
84 ;; | run (70) | 0.108142 |
85 ;; | run (10) | 0.111348 |
86 ;; | run (60) | 0.113593 |
87 ;; | run (50) | 0.116446 |
88 ;; | run (100) | 0.118863 |
89 ;; #+END:
90 ;;
92 ;;; Code:
93 (require 'org)
94 (require 'org-table)
96 (defvar org-propview-default-value 0
97 "Default value to insert into the propview table when the no
98 value is calculated either through lack of required variables for
99 a column, or through the generation of an error.")
101 (defun and-rest (list)
102 (if (listp list)
103 (if (> (length list) 1)
104 (and (car list) (and-rest (cdr list)))
105 (car list))
106 list))
108 (put 'org-collector-error
109 'error-conditions
110 '(error column-prop-error org-collector-error))
112 (defun org-dblock-write:propview (params)
113 "collect the column specification from the #+cols line
114 preceeding the dblock, then update the contents of the dblock."
115 (interactive)
116 (condition-case er
117 (let ((cols (plist-get params :cols))
118 (inherit (plist-get params :inherit))
119 (conds (plist-get params :conds))
120 (match (plist-get params :match))
121 (scope (plist-get params :scope))
122 (content-lines (org-split-string (plist-get params :content) "\n"))
123 id table line pos)
124 (save-excursion
125 (when (setq id (plist-get params :id))
126 (cond ((not id) nil)
127 ((eq id 'global) (goto-char (point-min)))
128 ((eq id 'local) nil)
129 ((setq idpos (org-find-entry-with-id id))
130 (goto-char idpos))
131 (t (error "Cannot find entry with :ID: %s" id))))
132 (org-narrow-to-subtree)
133 (setq table (org-propview-to-table
134 (org-propview-collect cols conds match scope inherit)))
135 (widen))
136 (setq pos (point))
137 (when content-lines
138 (while (string-match "^#" (car content-lines))
139 (insert (pop content-lines) "\n")))
140 (insert table) (insert "\n|--") (org-cycle) (move-end-of-line 1)
141 (message (format "point-%d" pos))
142 (while (setq line (pop content-lines))
143 (when (string-match "^#" line)
144 (insert "\n" line)))
145 (goto-char pos)
146 (org-table-recalculate 'all))
147 (org-collector-error (widen) (error "%s" er))
148 (error (widen) (error "%s" er))))
150 (defun org-propview-eval-w-props (props body)
151 "evaluate the BODY-FORMS binding the variables using the
152 variables and values specified in props"
153 (condition-case nil ;; catch any errors
154 (eval `(let ,(mapcar
155 (lambda (pair) (list (intern (car pair)) (cdr pair)))
156 props)
157 ,body))
158 (error nil)))
160 (defun org-propview-get-with-inherited (&optional inherit)
161 (append
162 (org-entry-properties)
163 (delq nil
164 (mapcar (lambda (i)
165 (let* ((n (symbol-name i))
166 (p (org-entry-get (point) n 'do-inherit)))
167 (when p (cons n p))))
168 inherit))))
170 (defun org-propview-collect (cols &optional conds match scope inherit)
171 (interactive)
172 ;; collect the properties from every header
173 (let* ((header-props
174 (let ((org-trust-scanner-tags t) alst)
175 (org-map-entries
176 (quote (cons (cons "ITEM" (org-get-heading t))
177 (org-propview-get-with-inherited inherit)))
178 match scope)))
179 ;; read property values
180 (header-props
181 (mapcar (lambda (props)
182 (mapcar (lambda (pair)
183 (cons (car pair) (org-babel-read (cdr pair))))
184 props))
185 header-props))
186 ;; collect all property names
187 (prop-names
188 (mapcar 'intern (delete-dups
189 (apply 'append (mapcar (lambda (header)
190 (mapcar 'car header))
191 header-props))))))
192 (append
193 (list
194 (mapcar (lambda (el) (format "%S" el)) cols) ;; output headers
195 'hline) ;; ------------------------------------------------
196 (mapcar ;; calculate the value of the column for each header
197 (lambda (props) (mapcar (lambda (col)
198 (let ((result (org-propview-eval-w-props props col)))
199 (if result result org-propview-default-value)))
200 cols))
201 (if conds
202 ;; eliminate the headers which don't satisfy the property
203 (delq nil
204 (mapcar
205 (lambda (props)
206 (if (and-rest (mapcar
207 (lambda (col)
208 (org-propview-eval-w-props props col))
209 conds))
210 props))
211 header-props))
212 header-props)))))
214 (defun org-propview-to-table (results)
215 ;; (message (format "cols:%S" cols))
216 (orgtbl-to-orgtbl
217 (mapcar
218 (lambda (row)
219 (if (equal row 'hline)
220 'hline
221 (mapcar (lambda (el) (format "%S" el)) row)))
222 (delq nil results)) '()))
224 (provide 'org-collector)
226 ;;; org-collector ends here