all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Eric Schulte" <schulte.eric@gmail.com>
To: mail@christianmoe.com
Cc: Bastien <bzg@altern.org>,
	emacs-orgmode@gnu.org, Martin Halder <martin.halder@gmail.com>
Subject: Re: org table calc and lisp for hh:mm timetable
Date: Mon, 21 Mar 2011 22:40:14 -0600	[thread overview]
Message-ID: <87mxknhioh.fsf@gmail.com> (raw)
In-Reply-To: <87fwqhl5mu.fsf@gmail.com> (Eric Schulte's message of "Sun, 20 Mar 2011 17:43:53 -0600")

[-- Attachment #1: Type: text/plain, Size: 1500 bytes --]

>
> 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.
>

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...

| 2:30 | 2 | 75 |
#+TBLFM: $3=$1/$2

It may make sense to also include functionality for converting the
result back into a time string, e.g.

#+begin_src emacs-lisp
  (defun org-time-seconds-to-string (secs)
    "Convert a number of seconds to a time string."
    (cond ((>= secs 3600) (format-seconds "%h:%.2m:%.2s" secs))
          ((>= secs 60) (format-seconds "%m:%.2s" secs))
          (t (format-seconds "%s" secs))))
#+end_src

| 2:30 | 2 | 1:15 |
#+TBLFM: $3='(org-time-seconds-to-string (/ (string-to-number $1) (string-to-number $2)))

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.

Best -- Eric


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-table-convert-times-to-integers-on-table-formula.patch --]
[-- Type: text/x-diff, Size: 2242 bytes --]

From 76b416013ee4c9a492c8ddced57727215165c298 Mon Sep 17 00:00:00 2001
From: Eric Schulte <schulte.eric@gmail.com>
Date: Mon, 21 Mar 2011 19:43:19 -0600
Subject: [PATCH] org-table: convert times to integers on table formula evaluation

* 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(-)

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)
 
+(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)))
-- 
1.7.1


  reply	other threads:[~2011-03-22  4:40 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-15 18:32 org table calc and lisp for hh:mm timetable Martin Halder
2011-03-15 19:22 ` Eric S Fraga
2011-03-15 19:49   ` Martin Halder
2011-03-15 20:37     ` Eric S Fraga
2011-03-15 21:47     ` Christian Moe
2011-03-16  9:22       ` Martin Halder
2011-03-17  7:49         ` Bastien
2011-03-20 17:50           ` Eric Schulte
2011-03-20 19:57             ` Eric S Fraga
2011-03-20 17:50           ` Eric Schulte
2011-03-20 21:00             ` Christian Moe
2011-03-20 23:43               ` Eric Schulte
2011-03-22  4:40                 ` Eric Schulte [this message]
2011-03-22  9:36                   ` Christian Moe
2011-03-24  1:18                     ` Eric Schulte
2011-03-24 18:35                       ` Martin Halder
2011-03-22 10:52                   ` Carsten Dominik
2011-07-02 11:38                     ` Bastien
2011-03-16  9:28       ` Eric S Fraga

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87mxknhioh.fsf@gmail.com \
    --to=schulte.eric@gmail.com \
    --cc=bzg@altern.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@christianmoe.com \
    --cc=martin.halder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.