Fix org-indent-mode message when mode is refused
[org-mode.git] / contrib / lisp / org-collector.el
blob2646b3f50f0efaa0acf11274c096b404d2ad308c
1 ;;; org-collector --- collect properties into tables
3 ;; Copyright (C) 2008 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-read-prop (prop)
113 "Convert the string property PROP to a number if appropriate.
114 Otherwise if prop looks like a list (meaning it starts with a
115 '(') then read it as lisp, otherwise return it unmodified as a
116 string."
117 (if (and (stringp prop) (not (equal prop "")))
118 (let ((out (string-to-number prop)))
119 (if (equal out 0)
120 (if (or (equal "(" (substring prop 0 1)) (equal "'" (substring prop 0 1)))
121 (read prop)
122 (if (string-match "^\\(+0\\|-0\\|0\\)$" prop)
124 (progn (set-text-properties 0 (length prop) nil prop)
125 prop)))
126 out))
127 prop))
129 (defun org-dblock-write:propview (params)
130 "collect the column specification from the #+cols line
131 preceeding the dblock, then update the contents of the dblock."
132 (interactive)
133 (condition-case er
134 (let ((cols (plist-get params :cols))
135 (conds (plist-get params :conds))
136 (match (plist-get params :match))
137 (scope (plist-get params :scope))
138 (content-lines (org-split-string (plist-get params :content) "\n"))
139 id table line pos)
140 (save-excursion
141 (when (setq id (plist-get params :id))
142 (cond ((not id) nil)
143 ((eq id 'global) (goto-char (point-min)))
144 ((eq id 'local) nil)
145 ((setq idpos (org-find-entry-with-id id))
146 (goto-char idpos))
147 (t (error "Cannot find entry with :ID: %s" id))))
148 (org-narrow-to-subtree)
149 (setq table (org-propview-to-table (org-propview-collect cols conds match scope)))
150 (widen))
151 (setq pos (point))
152 (when content-lines
153 (while (string-match "^#" (car content-lines))
154 (insert (pop content-lines) "\n")))
155 (insert table) (insert "\n|--") (org-cycle) (move-end-of-line 1)
156 (message (format "point-%d" pos))
157 (while (setq line (pop content-lines))
158 (when (string-match "^#" line)
159 (insert "\n" line)))
160 (goto-char pos)
161 (org-table-recalculate 'all))
162 (org-collector-error (widen) (error "%s" er))
163 (error (widen) (error "%s" er))))
165 (defun org-propview-eval-w-props (props body)
166 "evaluate the BODY-FORMS binding the variables using the
167 variables and values specified in props"
168 (condition-case nil ;; catch any errors
169 (eval `(let ,(mapcar
170 (lambda (pair) (list (intern (car pair)) (cdr pair)))
171 props)
172 ,body))
173 (error nil)))
175 (defun org-propview-collect (cols &optional conds match scope)
176 (interactive)
177 ;; collect the properties from every header
178 (let* ((header-props
179 (let ((org-trust-scanner-tags t))
180 (org-map-entries (quote (cons (cons "ITEM" (org-get-heading t))
181 (org-entry-properties)))
182 match scope)))
183 ;; read property values
184 (header-props (mapcar (lambda (props)
185 (mapcar (lambda (pair) (cons (car pair) (org-read-prop (cdr pair))))
186 props))
187 header-props))
188 ;; collect all property names
189 (prop-names (mapcar 'intern (delete-dups
190 (apply 'append (mapcar (lambda (header)
191 (mapcar 'car header))
192 header-props))))))
193 (append
194 (list
195 (mapcar (lambda (el) (format "%S" el)) cols) ;; output headers
196 'hline) ;; ------------------------------------------------
197 (mapcar ;; calculate the value of the column for each header
198 (lambda (props) (mapcar (lambda (col) (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 (lambda (col) (org-propview-eval-w-props props col)) conds))
207 props))
208 header-props))
209 header-props)))))
211 (defun org-propview-to-table (results)
212 ;; (message (format "cols:%S" cols))
213 (orgtbl-to-orgtbl
214 (mapcar
215 (lambda (row)
216 (if (equal row 'hline)
217 'hline
218 (mapcar (lambda (el) (format "%S" el)) row)))
219 (delq nil results)) '()))
221 (provide 'org-collector)
222 ;;; org-collector ends here