1 ;;; org-collector --- collect properties into tables
3 ;; Copyright (C) 2008-2012 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
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)
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/>.
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 ;; |---------------------+----------|
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 ;; |-----------------+----------|
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 |
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 |
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)
103 (if (> (length list
) 1)
104 (and (car list
) (and-rest (cdr list
)))
108 (put 'org-collector-error
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."
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"))
127 (when (setq id
(plist-get params
:id
))
129 ((eq id
'global
) (goto-char (point-min)))
131 ((setq idpos
(org-find-entry-with-id id
))
133 (t (error "Cannot find entry with :ID: %s" id
))))
134 (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
))
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
)
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
159 (lambda (pair) (list (intern (car pair
)) (cdr pair
)))
164 (defun org-propview-get-with-inherited (&optional inherit
)
166 (org-entry-properties)
169 (let* ((n (symbol-name i
))
170 (p (org-entry-get (point) n
'do-inherit
)))
171 (when p
(cons n p
))))
174 (defun org-propview-collect (cols stringformat
&optional conds match scope inherit colnames
)
176 ;; collect the properties from every header
178 (let ((org-trust-scanner-tags t
) alst
)
180 (quote (cons (cons "ITEM" (org-get-heading t
))
181 (org-propview-get-with-inherited inherit
)))
183 ;; read property values
185 (mapcar (lambda (props)
186 (mapcar (lambda (pair)
187 (cons (car pair
) (org-babel-read (cdr pair
))))
190 ;; collect all property names
192 (mapcar 'intern
(delete-dups
193 (apply 'append
(mapcar (lambda (header)
194 (mapcar 'car header
))
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
)))
206 ;; eliminate the headers which don't satisfy the property
210 (if (and-rest (mapcar
212 (org-propview-eval-w-props props col
))
218 (defun org-propview-to-table (results stringformat
)
219 ;; (message (format "cols:%S" cols))
223 (if (equal row
'hline
)
225 (mapcar (lambda (el) (format stringformat el
)) row
)))
226 (delq nil results
)) '()))
228 (provide 'org-collector
)
229 ;;; org-collector ends here