From mboxrd@z Thu Jan 1 00:00:00 1970 From: Subject: SOLVED: elisp formulas in column view (without converting to tables) Date: Fri, 13 Mar 2009 23:43:31 +0000 Message-ID: <87sklh0z18.fsf@it.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LiH5f-0001R9-NJ for emacs-orgmode@gnu.org; Fri, 13 Mar 2009 19:46:59 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LiH5Z-0001QO-V3 for emacs-orgmode@gnu.org; Fri, 13 Mar 2009 19:46:58 -0400 Received: from [199.232.76.173] (port=35068 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LiH5Z-0001QL-Pt for emacs-orgmode@gnu.org; Fri, 13 Mar 2009 19:46:53 -0400 Received: from main.gmane.org ([80.91.229.2]:56148 helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1LiH5Z-00014z-78 for emacs-orgmode@gnu.org; Fri, 13 Mar 2009 19:46:53 -0400 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1LiH5W-00058A-OX for emacs-orgmode@gnu.org; Fri, 13 Mar 2009 23:46:50 +0000 Received: from 87-194-46-7.bethere.co.uk ([87.194.46.7]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 13 Mar 2009 23:46:50 +0000 Received: from news by 87-194-46-7.bethere.co.uk with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 13 Mar 2009 23:46:50 +0000 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Cc: RLAdams@adamsInfoServ.com Hi, I have rewritten the org-columns-compute function to allow elisp formulas in column view. It allows you to specify how to accumulate values from child headers, and how to specify the value for the current header, based on other columns. In the column specification you place elisp code between the braces {} after the column name. This elisp should return a list of 2 values. The first value should be the value for that header, and the second value should be the running total of values so far at that level, which will eventually be used to set the value for the parent header. The following values may be referenced by your elisp code: | $name | the value of the 'name' column for this header (swap 'name' for whatever columns you have) | | curval | the current value for this header | | curtotal | the running total from headers at the same level as this one | | prevtotal | the total from headers below this level | Here is the column specification for the example that Eric gave: :COLUMNS: %25ITEM %2d{`(,(or curval 0) 0)} %f{`(,(or curval 0) 0)} %2fd{`(,(+ $f $d) 0)} This will ensure that the 'fd' column contains the sum of the 'f' and 'd' columns. If there are any missing values in the 'f' and 'd' columns they will be replaced with 0's. No running total is needed for that example so the second element of the return list just contains 0. I use the ` and , chars to evaluate the elements between the brackets before returning the list, but you could also use the (list val1 val2) form. Here is a more complicated example that calculates the geometric mean of child headers: :COLUMNS: %25ITEM %5count{`(,(or prevtotal 1) ,(if curtotal (1+ curtotal) 1))} %21val{`(,(if prevtotal (exp (/ prevtotal $count)) (or curval 1)) ,(if curtotal (+ curtotal (log curval)) (or (log curval) 1)))} The 'val' column will contain either raw data, or a geometric mean depending on whether the header has children or not. The extra column 'count' is used to count the number of children (is there an org function for this?). You could use the calc-eval function in your elisp code to do some calculations for you, but this would get a bit messy as it takes a string argument and returns a string, so you would need to make lots of use of the number-to-string and string-to-number functions. I guess it is not a very simple solution but it does the job for now. It anyone can improve on it that would be great... or maybe Dominik already has something? Here it is: (defun org-columns-compute (property) "Compute the values of property PROPERTY hierarchically, for the entire buffer." (interactive) (let* ((re (concat "^" outline-regexp)) (lmax 30) ; Does anyone use deeper levels??? (level 0) (ass (assoc property org-columns-current-fmt-compiled)) ;; parse elisp form if there is one (form (nth 3 ass)) (uselisp (and (> (length form) 1) (or (equal "(" (substring form 0 1)) (equal "(" (substring form 1 2))))) (form (if uselisp (replace-regexp-in-string "\$\\([^()\" ]+\\)" "(string-to-number (org-entry-get nil \"\\1\"))" (nth 3 ass) t))) ;; vector to hold running totals for each level (lsum (make-vector lmax (if uselisp nil 0))) (format (nth 4 ass)) (printf (nth 5 ass)) (beg org-columns-top-level-marker) last-level val valflag end sumpos sum-alist str str1 useval prevtotal curtotal newvals) (save-excursion ;; Find the region to compute (goto-char beg) (setq end (condition-case nil (org-end-of-subtree t) (error (point-max)))) (goto-char end) ;; Walk the tree from the back and do the computations (while (re-search-backward re beg t) (setq sumpos (match-beginning 0) last-level level level (org-outline-level) ;; total from children, or nil if there were none prevtotal (if (< level last-level) (aref lsum last-level) nil) ;; total at this level curtotal (aref lsum level) ;; current property value as string val (org-entry-get nil property) ;; is it non-empty? valflag (and val (string-match "\\S-" val)) ;; current property value as number (or nil if empty) curval (if valflag (org-column-string-to-number val format) nil) ;; get values to replace current value and running total newvals (if uselisp (eval-expression (read form)) (list (or prevtotal curval) (+ curtotal (or prevtotal curval 0))))) (cond ((< level last-level) ; we have moved up to a parent (setq ;; new value, as string str (if (nth 0 newvals) (org-columns-number-to-string (nth 0 newvals) format printf) nil) ;; add text properties to it useval (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold) ;; get current text properties sum-alist (get-text-property sumpos 'org-summaries)) ;; put new value here as a text property (if (assoc property sum-alist) (setcdr (assoc property sum-alist) useval) (push (cons property useval) sum-alist) (org-unmodified (add-text-properties sumpos (1+ sumpos) (list 'org-summaries sum-alist)))) ;; put new org property value (if (nth 0 newvals) (org-entry-put nil property str)) ;; set value for current level total (when (or prevtotal valflag) (aset lsum level (nth 1 newvals))) ;; clear totals for deeper levels (loop for l from (1+ level) to (1- lmax) do (aset lsum l (if uselisp nil 0)))) ((>= level last-level) ; we have not moved up to a parent ;; set new org property value and add to total for this level (org-entry-put nil property (org-columns-number-to-string (nth 0 newvals) format printf)) (aset lsum level (nth 1 newvals))) (t (error "This should not happen"))))))) -- aleblanc