From mboxrd@z Thu Jan 1 00:00:00 1970 From: Carsten Dominik Subject: Re: org table calc and lisp for hh:mm timetable Date: Tue, 22 Mar 2011 11:52:56 +0100 Message-ID: <20D6527E-03EC-49E1-9751-4A042E65B3BD@gmail.com> References: <9999237C-1FBD-481E-AF8D-D68DB85080CE@gmail.com> <87lj0gqjd3.fsf@ucl.ac.uk> <0DDD90AF-9B9A-4FE3-8080-74EF01E6E292@gmail.com> <4D7FDE8C.1030103@christianmoe.com> <00BD91C6-C610-4CDD-B3E0-E9FECDAA372C@gmail.com> <87k4fynog3.fsf@altern.org> <87lj09llzd.fsf@gmail.com> <4D866AD7.4020701@christianmoe.com> <87fwqhl5mu.fsf@gmail.com> <87mxknhioh.fsf@gmail.com> Mime-Version: 1.0 (Apple Message framework v1082) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Return-path: Received: from [140.186.70.92] (port=42600 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q1zCz-0003fe-Uf for emacs-orgmode@gnu.org; Tue, 22 Mar 2011 06:53:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q1zCy-0005rw-GO for emacs-orgmode@gnu.org; Tue, 22 Mar 2011 06:53:05 -0400 Received: from mail-ey0-f169.google.com ([209.85.215.169]:50022) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q1zCy-0005rp-59 for emacs-orgmode@gnu.org; Tue, 22 Mar 2011 06:53:04 -0400 Received: by eyh6 with SMTP id 6so1831806eyh.0 for ; Tue, 22 Mar 2011 03:52:59 -0700 (PDT) In-Reply-To: <87mxknhioh.fsf@gmail.com> 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: Eric Schulte Cc: Bastien , emacs-orgmode@gnu.org, mail@christianmoe.com, Martin Halder On 22.3.2011, at 05:40, Eric Schulte wrote: >>=20 >> While this topic is raised, would it make sense for Org-mode table >> formula to automatically parse any time-like string into time units >> (i.e., base sixty). That would be the easiest for most users, and (I >> imagine) would rarely result in surprising and unexpected behavior. >>=20 >=20 > So, I took a shot at folding this into org-table.el, the resulting = patch > is attached. I'm not sure if this sort of automatic interpretation of > time-like strings into integers is a good idea, or if this is the best > implementation (I'm not incredibly familiar with Org's table handling) > but after a couple of simple tests the patch does seem to work. For > example the following... >=20 > | 2:30 | 2 | 75 | > #+TBLFM: $3=3D$1/$2 I think it might be a bit bold to turn this on by default. How about introducing another flag for the formula to turn on time string processing like this?=20 - Carsten >=20 > It may make sense to also include functionality for converting the > result back into a time string, e.g. >=20 > #+begin_src emacs-lisp > (defun org-time-seconds-to-string (secs) > "Convert a number of seconds to a time string." > (cond ((>=3D secs 3600) (format-seconds "%h:%.2m:%.2s" secs)) > ((>=3D secs 60) (format-seconds "%m:%.2s" secs)) > (t (format-seconds "%s" secs)))) > #+end_src >=20 > | 2:30 | 2 | 1:15 | > #+TBLFM: $3=3D'(org-time-seconds-to-string (/ (string-to-number $1) = (string-to-number $2))) >=20 > While the above is cumbersome, there may be a simpler syntax or > convention -- e.g., whenever one of the inputs is a time string then = the > results are displayed as a time string. Not sure what the best option > is here, but thought this patch may spur some good suggestions. >=20 > Best -- Eric >=20 > =46rom 76b416013ee4c9a492c8ddced57727215165c298 Mon Sep 17 00:00:00 = 2001 > From: Eric Schulte > Date: Mon, 21 Mar 2011 19:43:19 -0600 > Subject: [PATCH] org-table: convert times to integers on table formula = evaluation >=20 > * lisp/org-table.el (org-table-to-time): If cell contents look like a > time string, then converts to an integer. > (org-table-eval-formula): Convert times to integers on table formula > evaluation. > --- > lisp/org-table.el | 26 ++++++++++++++++++++++---- > 1 files changed, 22 insertions(+), 4 deletions(-) >=20 > diff --git a/lisp/org-table.el b/lisp/org-table.el > index 3573032..3674b53 100644 > --- a/lisp/org-table.el > +++ b/lisp/org-table.el > @@ -2273,6 +2273,21 @@ of the new mark." > (cons var (cons value modes))) > modes) >=20 > +(defun org-table-to-time (s) > + "Convert cell to numerical time if contents look like a time = string." > + (cond > + ((and (stringp s) > + (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)" s)) > + (let ((hour (string-to-number (match-string 1 s))) > + (min (string-to-number (match-string 2 s))) > + (sec (string-to-number (match-string 3 s)))) > + (+ (* hour 3600) (* min 60) sec))) > + ((and (stringp s) > + (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)) > + (let ((min (string-to-number (match-string 1 s))) > + (sec (string-to-number (match-string 2 s)))) > + (+ (* min 60) sec))))) > + > (defun org-table-eval-formula (&optional arg equation > suppress-align suppress-const > suppress-store = suppress-analysis) > @@ -2369,10 +2384,13 @@ not overwrite the stored one." > (setq formula (org-table-formula-substitute-names formula))) > (setq orig (or (get-text-property 1 :orig-formula formula) "?")) > (while (> ndown 0) > - (setq fields (org-split-string > - (org-no-properties > - (buffer-substring (point-at-bol) (point-at-eol))) > - " *| *")) > + (setq fields (mapcar (lambda (cell) > + (let ((time (org-table-to-time cell))) > + (if time (number-to-string time) = cell))) > + (org-split-string > + (org-no-properties > + (buffer-substring (point-at-bol) = (point-at-eol))) > + " *| *"))) > (if (eq numbers t) > (setq fields (mapcar > (lambda (x) (number-to-string = (string-to-number x))) > --=20 > 1.7.1 >=20