Update copyright years.
[org-mode.git] / contrib / lisp / org-collector.el
blob7f3b3441d2ff456250b77fe158046fb283119a9f
1 ;;; org-collector --- collect properties into tables
3 ;; Copyright (C) 2008-2014 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 ;; This program 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 ;; This program 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:
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 (noquote (plist-get params :noquote))
123 (colnames (plist-get params :colnames))
124 (content-lines (org-split-string (plist-get params :content) "\n"))
125 id table line pos)
126 (save-excursion
127 (when (setq id (plist-get params :id))
128 (cond ((not id) nil)
129 ((eq id 'global) (goto-char (point-min)))
130 ((eq id 'local) nil)
131 ((setq idpos (org-find-entry-with-id id))
132 (goto-char idpos))
133 (t (error "Cannot find entry with :ID: %s" id))))
134 (unless (eq id 'global) (org-narrow-to-subtree))
135 (setq stringformat (if noquote "%s" "%S"))
136 (setq table (org-propview-to-table
137 (org-propview-collect cols stringformat conds match scope inherit
138 (if colnames colnames cols)) stringformat))
139 (widen))
140 (setq pos (point))
141 (when content-lines
142 (while (string-match "^#" (car content-lines))
143 (insert (pop content-lines) "\n")))
144 (insert table) (insert "\n|--") (org-cycle) (move-end-of-line 1)
145 (message (format "point-%d" pos))
146 (while (setq line (pop content-lines))
147 (when (string-match "^#" line)
148 (insert "\n" line)))
149 (goto-char pos)
150 (org-table-recalculate 'all))
151 (org-collector-error (widen) (error "%s" er))
152 (error (widen) (error "%s" er))))
154 (defun org-propview-eval-w-props (props body)
155 "evaluate the BODY-FORMS binding the variables using the
156 variables and values specified in props"
157 (condition-case nil ;; catch any errors
158 (eval `(let ,(mapcar
159 (lambda (pair) (list (intern (car pair)) (cdr pair)))
160 props)
161 ,body))
162 (error nil)))
164 (defun org-propview-get-with-inherited (&optional inherit)
165 (append
166 (org-entry-properties)
167 (delq nil
168 (mapcar (lambda (i)
169 (let* ((n (symbol-name i))
170 (p (org-entry-get (point) n 'do-inherit)))
171 (when p (cons n p))))
172 inherit))))
174 (defun org-propview-collect (cols stringformat &optional conds match scope inherit colnames)
175 (interactive)
176 ;; collect the properties from every header
177 (let* ((header-props
178 (let ((org-trust-scanner-tags t) alst)
179 (org-map-entries
180 (quote (cons (cons "ITEM" (org-get-heading t))
181 (org-propview-get-with-inherited inherit)))
182 match scope)))
183 ;; read property values
184 (header-props
185 (mapcar (lambda (props)
186 (mapcar (lambda (pair)
187 (cons (car pair) (org-babel-read (cdr pair))))
188 props))
189 header-props))
190 ;; collect all property names
191 (prop-names
192 (mapcar 'intern (delete-dups
193 (apply 'append (mapcar (lambda (header)
194 (mapcar 'car header))
195 header-props))))))
196 (append
197 (list
198 (if colnames colnames (mapcar (lambda (el) (format stringformat el)) cols))
199 'hline) ;; ------------------------------------------------
200 (mapcar ;; calculate the value of the column for each header
201 (lambda (props) (mapcar (lambda (col)
202 (let ((result (org-propview-eval-w-props props col)))
203 (if result result org-propview-default-value)))
204 cols))
205 (if conds
206 ;; eliminate the headers which don't satisfy the property
207 (delq nil
208 (mapcar
209 (lambda (props)
210 (if (and-rest (mapcar
211 (lambda (col)
212 (org-propview-eval-w-props props col))
213 conds))
214 props))
215 header-props))
216 header-props)))))
218 (defun org-propview-to-table (results stringformat)
219 ;; (message (format "cols:%S" cols))
220 (orgtbl-to-orgtbl
221 (mapcar
222 (lambda (row)
223 (if (equal row 'hline)
224 'hline
225 (mapcar (lambda (el) (format stringformat el)) row)))
226 (delq nil results)) '()))
228 (provide 'org-collector)
229 ;;; org-collector ends here