Re: [O] make orgtbl-ascii-plot easier to install

2014-08-29 Thread Nicolas Goaziou
Hello,

Thierry Banel tbanelweb...@free.fr writes:

 This new patch takes into account all your feedback.
 Thanks again for the time spent !

Patch applied (with tiny changes to comments formatting, and a few
trailing whitespaces).

Thank you for this work.


Regards,

-- 
Nicolas Goaziou



Re: [O] make orgtbl-ascii-plot easier to install

2014-08-29 Thread Thierry Banel
Le 29/08/2014 11:54, Nicolas Goaziou a écrit :

 Patch applied (with tiny changes to comments formatting, and a few
 trailing whitespaces).


Great!
I will now write a few words of documentation.
This page [[info:org#Org-Plot]] seems to be the right place, unless
someone has a better idea.




Re: [O] make orgtbl-ascii-plot easier to install

2014-08-28 Thread Thierry Banel
Hi Nicolas.

This new patch takes into account all your feedback.
Thanks again for the time spent !

Thierry


Le 28/08/2014 01:28, Nicolas Goaziou a écrit :

 You forgot a space at the end of the sentence. Actually, this is the
 case in all your docstrings.
Right. Fixed.


 Use \\[universal-argument] instead of C-u.
I didn't knew this macro. Applied.

 Just to be sure to catch the following (odd) table

  ||
  ||
  | header |
  ||
  | values |

 I suggest the following

   (while (eq (car table) 'hline) (setq table (cdr table)))
   (dolist (x (or (cdr (memq 'hline table)) table))
This is a strengthened algorithm, and it is shorter than the previous
version. Applied!

 The second sentence should start a new line.
Right. Fixed.

From fac4561630c229fdb2356a93a0b575376c80bb7e Mon Sep 17 00:00:00 2001
From: Thierry Banel tbanelweb...@free.fr
Date: Thu, 28 Aug 2014 22:28:27 +0200
Subject: [PATCH] Add ascii plotting in tables

* org-table.el (orgtbl-ascii-plot): top-level function.
(orgtbl-ascii-draw), (orgtbl-uc-draw-grid), (orgtbl-uc-draw-cont):
functions which go in table formulas for drawing bars.
* org.el: key binding and menu binding

Thanks to Michael Brand and Nicolas Goaziou for feedback and
enhancements.
---
 lisp/org-table.el | 105 +-
 lisp/org.el   |   4 ++-
 2 files changed, 107 insertions(+), 2 deletions(-)

diff --git a/lisp/org-table.el b/lisp/org-table.el
index 06a1008..4be95bc 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -4326,7 +4326,8 @@ to execute outside of tables.
 	 [Move Column Left org-metaleft :active (org-at-table-p) :keys M-left]
 	 [Move Column Right org-metaright :active (org-at-table-p) :keys M-right]
 	 [Delete Column org-shiftmetaleft :active (org-at-table-p) :keys M-S-left]
-	 [Insert Column org-shiftmetaright :active (org-at-table-p) :keys M-S-right])
+	 [Insert Column org-shiftmetaright :active (org-at-table-p) :keys M-S-right]
+	 [Ascii plot orgtbl-ascii-plot :active (org-at-table-p) :keys C-c p])
 	(Row
 	 [Move Row Up org-metaup :active (org-at-table-p) :keys M-up]
 	 [Move Row Down org-metadown :active (org-at-table-p) :keys M-down]
@@ -5008,6 +5009,108 @@ it here: http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.;
   (user-error Please download ascii-art-to-unicode.el (use C-c C-l to insert the link to it)))
 (buffer-string)))
 
+;; Put the cursor in a column containing numerical values
+;; of an Org-Mode table,
+;; type C-c p
+;; A new column is added with a bar plot.
+;; When the table is refreshed (C-u C-c *),
+;; the plot is updated to reflect the new values.
+
+(defun orgtbl-ascii-draw (value min max optional width characters)
+  Draws an ascii bar in a table.
+VALUE is a the value to plot, the width of the bar to draw.  A
+value equal to MIN will be displayed as empty (zero width bar).
+A value equal to MAX will draw a bar filling all the WIDTH.
+WIDTH is the expected width in characters of the column.
+CHARACTERS is a string that will compose the bar, with shades of
+grey from pure white to pure black.  It defaults to a 10
+characters string of regular ascii characters.
+  (let* ((characters (or characters  .:;c!lhVHW))
+	 (width (or width 12))
+	 (value (if (numberp value) value (string-to-number value)))
+	 (value (* (/ (- (+ value 0.0) min) (- max min)) width)))
+(cond
+ (( value 0) too small)
+ (( value width) too large)
+ (t
+  (let ((len (1- (length characters
+	(concat
+	 (make-string (floor value) (elt characters len))
+	 (string (elt characters
+		  (floor (* (- value (floor value)) len))
+  
+;;;###autoload
+(defun orgtbl-ascii-plot (optional ask)
+  Draws an ascii bar plot in a column.
+With cursor in a column containing numerical values, this
+function will draw a plot in a new column.
+ASK, if given, is a numeric prefix to override the default 12
+characters width of the plot.  ASK may also be the
+\\[universal-argument] prefix, which will prompt for the width.
+  (interactive P)
+  (let ((col (org-table-current-column))
+	(min  1e999) ;; 1e999 will be converted to infinity
+	(max -1e999) ;; which is the desired result
+	(table (org-table-to-lisp))
+	(length
+	 (cond ((consp ask)
+		(read-number Length of column  12))
+	   ((numberp ask) ask)
+	   (t 12
+(while (eq (car table) 'hline) ;; skip any hline a the top of table
+  (setq table (cdr table)))
+(dolist (x
+	 (or (cdr (memq 'hline table)) table)) ;; skip table header if any
+  (when (consp x)
+	(setq x (nth (1- col) x))
+	(when (string-match
+	   ^[-+]?\\([0-9]*[.]\\)?[0-9]*\\([eE][+-]?[0-9]+\\)?$
+	   x)
+	  (setq x (string-to-number x))
+	  (when ( min x) (setq min x))
+	  (when ( max x) (setq max x)
+(org-table-insert-column)
+(org-table-move-column-right)
+(org-table-store-formulas
+ (cons
+  (cons
+   (number-to-string (1+ col))
+   (format '(%s $%s %s %s %s)
+	   

Re: [O] make orgtbl-ascii-plot easier to install

2014-08-27 Thread Thierry Banel
Hereafter is an enhanced version of orgtbl-ascii-plot.

Thanks, Nicolas, for your feedback. Enhancements you suggested include:
- let-binding (instead of setq)
- better support for table headers (correct parsing of 'hline)
- no dependency on cl-lib (hopefully achieving Emacs 23 support)
- use dolist (instead of mapc)
- clean up doc-strings

Have fun
Thierry


From 002e3b5baec9ba513c70914b9bfe17966aa9ca24 Mon Sep 17 00:00:00 2001
From: Thierry Banel tbanelweb...@free.fr
Date: Wed, 27 Aug 2014 23:11:11 +0200
Subject: [PATCH] Add ascii plotting in tables

* org-table.el (orgtbl-ascii-plot): top-level function.
(orgtbl-ascii-draw), (orgtbl-uc-draw-grid), (orgtbl-uc-draw-cont):
functions which go in table formulas for drawing bars.
* org.el: key binding and menu binding

Thanks to Michael Brand and Nicolas Goaziou for feedback and
enhancements.
---
 lisp/org-table.el | 105 +-
 lisp/org.el   |   4 ++-
 2 files changed, 107 insertions(+), 2 deletions(-)

diff --git a/lisp/org-table.el b/lisp/org-table.el
index 06a1008..88ae094 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -4326,7 +4326,8 @@ to execute outside of tables.
 	 [Move Column Left org-metaleft :active (org-at-table-p) :keys M-left]
 	 [Move Column Right org-metaright :active (org-at-table-p) :keys M-right]
 	 [Delete Column org-shiftmetaleft :active (org-at-table-p) :keys M-S-left]
-	 [Insert Column org-shiftmetaright :active (org-at-table-p) :keys M-S-right])
+	 [Insert Column org-shiftmetaright :active (org-at-table-p) :keys M-S-right]
+	 [Ascii plot orgtbl-ascii-plot :active (org-at-table-p) :keys C-c p])
 	(Row
 	 [Move Row Up org-metaup :active (org-at-table-p) :keys M-up]
 	 [Move Row Down org-metadown :active (org-at-table-p) :keys M-down]
@@ -5008,6 +5009,108 @@ it here: http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.;
   (user-error Please download ascii-art-to-unicode.el (use C-c C-l to insert the link to it)))
 (buffer-string)))
 
+;; Put the cursor in a column containing numerical values
+;; of an Org-Mode table,
+;; type C-c p
+;; A new column is added with a bar plot.
+;; When the table is refreshed (C-u C-c *),
+;; the plot is updated to reflect the new values.
+
+(defun orgtbl-ascii-draw (value min max optional width characters)
+  Draws an ascii bar in a table.
+VALUE is a the value to plot, the width of the bar to draw.  A
+value equal to MIN will be displayed as empty (zero width bar).
+A value equal to MAX will draw a bar filling all the WIDTH.
+WIDTH is the expected width in characters of the column.
+CHARACTERS is a string that will compose the bar, with shades of
+grey from pure white to pure black. It defaults to a 10
+characters string of regular ascii characters.
+  (let* ((characters (or characters  .:;c!lhVHW))
+	 (width (or width 12))
+	 (value (if (numberp value) value (string-to-number value)))
+	 (value (* (/ (- (+ value 0.0) min) (- max min)) width)))
+(cond
+ (( value 0) too small)
+ (( value width) too large)
+ (t
+  (let ((len (1- (length characters
+	(concat
+	 (make-string (floor value) (elt characters len))
+	 (string (elt characters
+		  (floor (* (- value (floor value)) len))
+  
+;;;###autoload
+(defun orgtbl-ascii-plot (optional ask)
+  Draws an ascii bars plot in a column.
+With cursor in a column containing numerical values, this
+function will draw a plot in a new column. ASK, if given, is a
+numeric prefix to override the default 12 characters width of the
+plot. ASK may also be the C-u prefix, which will prompt for the
+width.
+  (interactive P)
+  (let ((col (org-table-current-column))
+	(min  1e999) ;; 1e999 will be converted to infinity
+	(max -1e999) ;; which is the desired result
+	(table (org-table-to-lisp))
+	(length
+	 (cond ((consp ask)
+		(read-number Length of column  12))
+	   ((numberp ask) ask)
+	   (t 12
+(dolist (x 
+	 (let ((t1 ;; skip table header
+		(if (eq (car table) 'hline)
+			(cdr table)
+		  table)))
+	   (or (cdr (memq 'hline t1)) t1)))
+  (when (consp x)
+	(setq x (nth (1- col) x))
+	(when (string-match
+	   ^[-+]?\\([0-9]*[.]\\)?[0-9]*\\([eE][+-]?[0-9]+\\)?$
+	   x)
+	  (setq x (string-to-number x))
+	  (when ( min x) (setq min x))
+	  (when ( max x) (setq max x)
+(org-table-insert-column)
+(org-table-move-column-right)
+(org-table-store-formulas
+ (cons
+  (cons
+   (number-to-string (1+ col))
+   (format '(%s $%s %s %s %s)
+	   orgtbl-ascii-draw col min max length))
+  (org-table-get-stored-formulas)))
+(org-table-recalculate t)))
+
+;; Example of extension: unicode characters
+;; Here are two examples of different styles.
+
+;; Unicode block characters are used to give a smooth effect.
+;; See http://en.wikipedia.org/wiki/Block_Elements
+;; Use one of those drawing functions
+;; - orgtbl-ascii-draw   (the default ascii)
+;; - orgtbl-uc-draw-grid (unicode with a 

Re: [O] make orgtbl-ascii-plot easier to install

2014-08-27 Thread Nicolas Goaziou
Thierry Banel tbanelweb...@free.fr writes:

 Hereafter is an enhanced version of orgtbl-ascii-plot.

 Thanks, Nicolas, for your feedback. Enhancements you suggested include:
 - let-binding (instead of setq)
 - better support for table headers (correct parsing of 'hline)
 - no dependency on cl-lib (hopefully achieving Emacs 23 support)
 - use dolist (instead of mapc)
 - clean up doc-strings

Thank you.

 +(defun orgtbl-ascii-plot (optional ask)
 +  Draws an ascii bars plot in a column.
 +With cursor in a column containing numerical values, this
 +function will draw a plot in a new column. ASK, if given, is a

You forgot a space at the end of the sentence. Actually, this is the
case in all your docstrings.

 +numeric prefix to override the default 12 characters width of the
 +plot. ASK may also be the C-u prefix, which will prompt for the
 +width.

Use \\[universal-argument] instead of C-u.

 +(dolist (x 
 +  (let ((t1 ;; skip table header
 + (if (eq (car table) 'hline)
 + (cdr table)
 +   table)))

Just to be sure to catch the following (odd) table

 ||
 ||
 | header |
 ||
 | values |

I suggest the following

  (while (eq (car table) 'hline) (setq table (cdr table)))
  (dolist (x (or (cdr (memq 'hline table)) table))
(when (consp x)
 (setq x (nth (1- col) x))
 (when (string-match ^[-+]?\\([0-9]*[.]\\)?[0-9]*\\([eE][+-]?[0-9]+\\)?$ 
x)
   (setq x (string-to-number x))
   (when ( min x) (setq min x))
   (when ( max x) (setq max x)

 +(defun orgtbl-uc-draw-grid (value min max optional width)
 +  Draws an ascii bar in a table. It is a variant of

The second sentence should start a new line.

 +(defun orgtbl-uc-draw-cont (value min max optional width)
 +  Draws an ascii bar in a table. It is a variant of

Ditto.


Regards,

-- 
Nicolas Goaziou



Re: [O] make orgtbl-ascii-plot easier to install

2014-08-26 Thread Nicolas Goaziou
Hello,

Thierry Banel tbanelweb...@free.fr writes:

 Sorry for the late answer, I was on the road.
 The patch is attached hereafter.

Thank you for the patch. Some comments follow.

 From 5fddaba2208c2cb4ce3b6bc24d0d10571124fb39 Mon Sep 17 00:00:00 2001
 From: Thierry Banel tbanelweb...@free.fr
 Date: Mon, 11 Aug 2014 00:00:21 +0200
 Subject: [PATCH]  * org-table.el: add ascii plotting in tables, 
  (orgtbl-ascii-plot): top-level function 
  (orgtbl-ascii-draw), (orgtbl-uc-draw-grid), 
  (orgtbl-uc-draw-cont): helper functions which go in table

If they are helper functions, I suggest to use -- in their name, e.g.,
`orgbtl--ascii-draw'.

 +(defun orgtbl-ascii-draw (value min max optional width characters)
 +  Draws an ascii bar in a table.
 +VALUE is a the value to plot, the width of the bar to draw.
 +A value equal to MIN will be displayed as empty (zero width bar).
 +A value equal to MAX will draw a bar filling all the WIDTH.
 +WIDTH is the expected width in characters of the column.
 +CHARACTERS is a string of characters that will compose the bar,

This is a minor issue, but a string of characters sounds strange:
aren't all strings constituted of characters? Maybe you really want
a list of characters.

 +with shades of grey from pure white to pure black.
 +It defaults to a 10 characters string of regular ascii characters.
 +

Spurious newline at the end of the docstring.

 +  (unless characters (setq characters  .:;c!lhVHW))
 +  (unless width (setq width 12))

I suggest let-binding variables instead:

  (let ((characters (or characters  .:;c!lhvHW))
(width (or width 12

 +  (if (stringp value)
 +  (setq value (string-to-number value)))

Prefer `and' or `when' over one-armed `if'.  Also, this may be dangerous
since `string-to-number' can return funny values.  Why wouldn't you
simply forbid strings and limit VALUE to integers.

 +  (setq value (* (/ (- (+ value 0.0) min) (- max min)) width))

(let ((value ...)))

 +  (cond
 +   (( value 0) too small)
 +   (( value width) too large)
 +   (t
 +(let ((len (1- (length characters
 +  (concat
 +   (make-string (floor value) (elt characters len))
 +   (string (elt characters
 + (floor (* (- value (floor value)) len)
 +
 +(defun orgtbl-uc-draw-grid (value min max optional width)
 +  Draws an ascii bar in a table.
 +It is a variant of orgtbl-ascii-draw with Unicode block characters,
 +for a smooth display.
 +Bars appear as grids (to the extend the font allows).
 +

Spurious newline. I wouldn't put the last sentence on its own line, too.
Also, isn't it extent?

 +(defun orgtbl-uc-draw-cont (value min max optional width)
 +  Draws an ascii bar in a table.
 +It is a variant of orgtbl-ascii-draw with Unicode block characters,
 +for a smooth display.
 +Bars are solid (to the extend the font allows).
 +

Ditto.

 +  (orgtbl-ascii-draw value min max width  
 \u258F\u258E\u258D\u258C\u258B\u258A\u2589\u2588))
 +
 +;;;###autoload
 +(defun orgtbl-ascii-plot (optional ask)
 +  Draws an ascii bars plot in a column, out of values found in another 
 column.
 +A numeric prefix may be given to override the default 12 characters wide 
 plot.
 +

You must refer explicitly to ASK in your docstring. In particular, you
may want to detail the distinction between '(4) and 4.

Spurious newline too.

 +  (interactive P)
 +  (let ((col (org-table-current-column))
 + (min  1e999)

`most-positive-fixnum'

 + (max -1e999)

`most-negative-fixnum'

 + (length 12)
 + (table (org-table-to-lisp)))
 +(cond ((consp ask)
 +(setq length
 +  (or
 +   (read-string Length of column [12]  nil nil 12)
 +   12)))
 +   ((numberp ask)
 +(setq length ask)))

(let ((length (cond ((consp ask)
 (read-number Length of column [12]  nil nil 12))
((numberp ask) ask)
(t 12)

 +(mapc

Small nitpick: I suggest to use `dolist' instead of `mapc' (no funcall
overhead).

 + (lambda (x)
 +   (when (consp x)
 +  (setq x (nth (1- col) x))
 +  (when (string-match
 + ^[-+]?\\([0-9]*[.]\\)?[0-9]*\\([eE][+-]?[0-9]+\\)?$
 + x)

Would `org-table-number-regexp' make sense here instead of the
hard-coded regexp?

 +(setq x (string-to-number x))
 +(if ( min x) (setq min x))
 +(if ( max x) (setq max x)

(when ( min x) ...)
(when ( max x))

 + (or (memq 'hline table) table)) ;; skip table header if any

This check is not sufficient in the following cases:

  |---|
  | no header |
  |---|

and

  |--|
  |  header  |
  |--|
  | contents |

IOW, you need to eliminate the leading 'hline, if any, and skip until
the next 'hline if there is one and if there is something after it.


Regards,

-- 
Nicolas Goaziou



Re: [O] make orgtbl-ascii-plot easier to install

2014-08-26 Thread Thierry Banel

  
  
Thanks Nicolas
for your valuable insight. I will apply your recommendations.

Le 26/08/2014 10:29, Nicolas Goaziou a écrit :
  

  
 (orgtbl-ascii-plot): top-level function 
 (orgtbl-ascii-draw), 	(orgtbl-uc-draw-grid), 
 (orgtbl-uc-draw-cont): helper functions which go in table

  
  
If they are helper functions, I suggest to use "--" in their name, e.g.,
`orgbtl--ascii-draw'.



  Well, all those functions have user-visibility. They appear in the
  #+TBLFM: line. I should avoid the words "helper function" here.


  
+CHARACTERS is a string of characters that will compose the bar,

  
  
This is a minor issue, but a "string of characters" sounds strange:
aren't all strings constituted of characters? Maybe you really want
a list of characters.




  Yes :) Just string.


  
+  (unless characters (setq characters " .:;c!lhVHW"))
+  (unless width (setq width 12))

  
  
I suggest let-binding variables instead:

  (let ((characters (or characters " .:;c!lhvHW"))
(width (or width 12



  I'll change to let-binding. What is the rational for that ? Better
  byte-code ?


  

  
+  (if (stringp value)
+  (setq value (string-to-number value)))

  
  
Prefer `and' or `when' over one-armed `if'.  Also, this may be dangerous
since `string-to-number' can return funny values.  Why wouldn't you
simply forbid strings and limit VALUE to integers.



  Can I limit VALUE to numbers ? VALUE comes from a cell in the
  table. The formula line of the table looks like this:
  
  #+TBLFM: $2='(orgtbl-ascii-draw $1 1 3 12)
  
  VALUE is $1 in this formula.
  If (string-to-number VALUE) fails, it will return zero, which is
  not very harmful. But yes, you are right, it would be better to
  check funny values, and explicitly do something sensible in this
  case.
  


  
+  (orgtbl-ascii-draw value min max width " \u258F\u258E\u258D\u258C\u258B\u258A\u2589\u2588"))
+
+;;;###autoload
+(defun orgtbl-ascii-plot (optional ask)
+  "Draws an ascii bars plot in a column, out of values found in another column.
+A numeric prefix may be given to override the default 12 characters wide plot.
+"

  
  
You must refer explicitly to ASK in your docstring. In particular, you
may want to detail the distinction between '(4) and 4.



  Absolutely.


  
+  (interactive "P")
+  (let ((col (org-table-current-column))
+	(min  1e999)

  
  
`most-positive-fixnum'


  Actually this should be `cl-most-positive-float', because
  everything here works in floating point values. Thanks for the
  clue.


  
+	(length 12)
+	(table (org-table-to-lisp)))
+(cond ((consp ask)
+	   (setq length
+		 (or
+		  (read-string "Length of column [12] " nil nil 12)
+		  12)))
+	  ((numberp ask)
+	   (setq length ask)))

  
  
(let ((length (cond ((consp ask)
 (read-number "Length of column [12] " nil nil 12))
((numberp ask) ask)
(t 12)



  Ok, shorter.


  

  
+(mapc

  
  
Small nitpick: I suggest to use `dolist' instead of `mapc' (no funcall
overhead).


  Sure, easier to read.
  
  


  
+ (lambda (x)
+   (when (consp x)
+	 (setq x (nth (1- col) x))
+	 (when (string-match
+		"^[-+]?\\([0-9]*[.]\\)?[0-9]*\\([eE][+-]?[0-9]+\\)?$"
+		x)

  
  
Would `org-table-number-regexp' make sense here instead of the
hard-coded regexp?


  Before hard-coding this regexp, I took a look at what was already
  there. org-table-number-regexp matches too much, for instance it
  matches:
    "345"
    "345()"
    "345%45" 
    "0x45"   // hexadecimal
    "1101#2"  // base two
  Anyway, the string feeds (string-to-number x) which does not
  accept all those variations. So I created a regexp exactly fitted
  for (string-to-number).  
  


  
+ (or (memq 'hline table) table)) ;; skip table header if any

  
  
This check is not sufficient in the following cases:

  |---|
  | no header |
  |---|

and

  |--|
  |  header  |
  |--|
  | contents |

IOW, you need to eliminate the leading 'hline, if any,and skip until
the next 'hline if there is one and if there is something after it.



  Ok. Not completely fool-proof, but better. On the other hand, this
  loop searches for the min and the max of a column, ignoring
  entries which are not numbers. So it could just iterate over the
  full column, including any kind of headers.
  
    | header | -- ignored
    || -- ignored
    |  1 | -- used 

Re: [O] make orgtbl-ascii-plot easier to install

2014-08-26 Thread Nicolas Goaziou
Thierry Banel tbanelweb...@free.fr writes:

 I'll change to let-binding. What is the rational for that ? Better
 byte-code ?

This is faster, indeed. Also, the scope of the binding is explicit. It
is sometimes hard to tell where the value of a setq'ed variable comes
from.

 Can I limit VALUE to numbers ? VALUE comes from a cell in the table.
 The formula line of the table looks like this:

 #+TBLFM: $2='(orgtbl-ascii-draw $1 1 3 12)

 VALUE is $1 in this formula.
 If (string-to-number VALUE) fails, it will return zero, which is not
 very harmful.

Nevermind then.

 Actually this should be `cl-most-positive-float', because everything
 here works in floating point values. Thanks for the clue.

Oh, right. Then, I wouldn't bother in this case. Org supports Emacs 23
and cl-lib is not easily available.

 Small nitpick: I suggest to use `dolist' instead of `mapc' (no funcall
 overhead).

 Sure, easier to read.

This is also kinda like let vs setq.

 Before hard-coding this regexp, I took a look at what was already
 there. org-table-number-regexp matches too much, for instance it
 matches:
 345
 345()
 345%45 
 0x45 // hexadecimal
 1101#2 // base two
 Anyway, the string feeds (string-to-number x) which does not accept
 all those variations. So I created a regexp exactly fitted for
 (string-to-number). 

OK.

 IOW, you need to eliminate the leading 'hline, if any,and skip until
 the next 'hline if there is one and if there is something after it.

 Ok. Not completely fool-proof, but better.

Do you think of another case that wouldn't be covered by this?

 On the other hand, this loop searches for the min and the max of
 a column, ignoring entries which are not numbers. So it could just
 iterate over the full column, including any kind of headers.

 | header | -- ignored
 || -- ignored
 | 1 | -- used (will set min=1)
 | 2 | -- used
 | xx | -- ignored
 | 3 | -- used (will set max=3)
 || -- ignored

But header and the rest of the column may be both numbers but expressed
in different units, e.g. header could be a year and column inhabitants.
FWIW, I think skipping header (when there is one) is a good idea.


Regards,

-- 
Nicolas Goaziou



Re: [O] make orgtbl-ascii-plot easier to install

2014-08-26 Thread Thierry Banel

  
  
Le 26/08/2014 22:27, Nicolas Goaziou a
  écrit :


  Thierry Banel tbanelweb...@free.fr writes:


  
I'll change to let-binding. What is the rational for that ? Better
byte-code ?

  
  
This is faster, indeed. Also, the scope of the binding is explicit. It
is sometimes hard to tell where the value of a setq'ed variable comes
from.


Good !


  
Actually this should be `cl-most-positive-float', because everything
here works in floating point values. Thanks for the clue.

  
  
Oh, right. Then, I wouldn't bother in this case. Org supports Emacs 23
and cl-lib is not easily available.


  
Small nitpick: I suggest to use `dolist' instead of `mapc' (no funcall
overhead).

Sure, easier to read.

  
  
This is also kinda like let vs setq.


Do you recommend to stay away from CL  CL-LIB for ORG stuff
  ? `DOLIST' comes from CL as well...



  
  
IOW, you need to eliminate the leading 'hline, if any,and skip until
the next 'hline if there is one and if there is something after it.

Ok. Not completely fool-proof, but better.

  
  
Do you think of another case that wouldn't be covered by this?


  1st case: a header and several hlines.
  In this case, values 1,2,3 will be ignored.
  
  | header |
  || -- leading hline: skipped
  |  1 |
  |  2 |
  |  3 |
  || -- next hline: ignore whatever is before
  |  4 |
  |  5 |
  |  6 |
  
  2nd case: no header at all, values in boxes.
  Values 1,2,3 will be ignored as well.
  
  || -- leading hline: skipped
  |  1 |
  |  2 |
  |  3 |
  || -- next hline: ignore whatever is before
  |  4 |
  |  5 |
  |  6 |
  || -- last hline
  


  


  
On the other hand, this loop searches for the min and the max of
a column, ignoring entries which are not numbers. So it could just
iterate over the full column, including any kind of headers.

| header | -- ignored
|| -- ignored
| 1 | -- used (will set min=1)
| 2 | -- used
| xx | -- ignored
| 3 | -- used (will set max=3)
|| -- ignored

  
  
But header and the rest of the column may be both numbers but expressed
in different units, e.g. header could be a year and column inhabitants.
FWIW, I think skipping header (when there is one) is a good idea.



Good use-case !
Now I am not sure what to do anymore.
There is already a heuristic algorithm in org-table to apply
  column-formulas. It does a good job at avoiding computing the
  formula for headers. Maybe I should mimic it.

  Header surrounded by hlines:
  
|+|
| Header |    | -- formula not applied
|+|
|  1 | 10 | -- $1*10
|  2 | 20 | -- $1*10
|+|
|  3 | 30 | -- $1*10
|  4 | 40 | -- $1*10
#+TBLFM: $2=$1*10


  Header over several lines, no top-hline:
  
| Head1 |    | -- formula not applied
| Head2 |    | -- formula not applied
|---+|
| 3 | 30 | -- $1*10
| 4 | 40 | -- $1*10
#+TBLFM: $2=$1*10

  
  No header, just values
  
| 3 | 30 | -- $1*10
| 4 | 40 | -- $1*10
#+TBLFM: $2=$1*10

  
The only ill-interpreted case is this one,
with no header.
The first values are considered as headers:

|---+|
| 3 |    | -- formula not applied
| 4 |    | -- formula not applied
|---+|
| 5 | 50 |
| 6 | 60 |
|---+|
#+TBLFM: $2=$1*10


Thanks for all this feedback.
Thierry

  




Re: [O] make orgtbl-ascii-plot easier to install

2014-08-26 Thread Nicolas Goaziou
Thierry Banel tbanelweb...@free.fr writes:

 Do you recommend to stay away from CL  CL-LIB for ORG stuff ?
 `DOLIST' comes from CL as well...

Actually, `dolist' is a macro from subr.el, which is then shadowed by
`cl-dolist', so there's no problem here.

BTW, you can use macros from cl (cl-lib doesn't exist in Emacs 23), not
so functions.

 
 IOW, you need to eliminate the leading 'hline, if any,and skip until
 the next 'hline if there is one and if there is something after it.

 Ok. Not completely fool-proof, but better.

 
 Do you think of another case that wouldn't be covered by this?

 1st case: a header and several hlines.
 In this case, values 1,2,3 will be ignored.

 | header |
 || -- leading hline: skipped
 | 1 |
 | 2 |
 | 3 |
 || -- next hline: ignore whatever is before
 | 4 |
 | 5 |
 | 6 |

I wasn't clear. A leading hline is when the very first row is a hline
(or worse consecutive hlines start the table). In the example above,
there is no such leading hline, so you don't eliminate it. Therefore,
only header will be skipped.

 2nd case: no header at all, values in boxes.
 Values 1,2,3 will be ignored as well.

 || -- leading hline: skipped
 | 1 |
 | 2 |
 | 3 |
 || -- next hline: ignore whatever is before
 | 4 |
 | 5 |
 | 6 |
 || -- last hline

There is a header. By definition, anything in the first column group is
a header. There is no limitations on the number of rows involved.

It may be a bit surprising, but this is consistent with the rest of Org.

 The only ill-interpreted case is this one, with no header.
 The first values are considered as headers:

 |---+|
 | 3 | | -- formula not applied
 | 4 | | -- formula not applied
 |---+|
 | 5 | 50 |
 | 6 | 60 |
 |---+|
 #+TBLFM: $2=$1*10

See above.


Regards,

-- 
Nicolas Goaziou



Re: [O] make orgtbl-ascii-plot easier to install

2014-08-26 Thread Thierry Banel
Everything is clear now.
Thanks Nicolas for those guidelines.
Regards,
Thierry

Le 27/08/2014 00:09, Nicolas Goaziou a écrit :
 Actually, `dolist' is a macro from subr.el, which is then shadowed by
 `cl-dolist', so there's no problem here.

 BTW, you can use macros from cl (cl-lib doesn't exist in Emacs 23), not
 so functions.
Ok, good.

 

 By definition, anything in the first column group is
 a header. There is no limitations on the number of rows involved.

 It may be a bit surprising, but this is consistent with the rest of Org.

 Regards, 
Good.





Re: [O] make orgtbl-ascii-plot easier to install

2014-08-10 Thread Thierry Banel
Le 28/07/2014 16:40, Bastien a écrit :
 Of course, I am still open to Dominik Carsten suggestion
 to add it into the core (org-table.el).
 http://thread.gmane.org/gmane.emacs.orgmode/79668
 Please go ahead and provide a patch for this, I agree this
 is a nice addition for Org's core.

Sorry for the late answer, I was on the road.
The patch is attached hereafter.

Have fun
Thierry


From 5fddaba2208c2cb4ce3b6bc24d0d10571124fb39 Mon Sep 17 00:00:00 2001
From: Thierry Banel tbanelweb...@free.fr
Date: Mon, 11 Aug 2014 00:00:21 +0200
Subject: [PATCH] 	* org-table.el: add ascii plotting in tables, 
 (orgtbl-ascii-plot): top-level function 
 (orgtbl-ascii-draw), 	(orgtbl-uc-draw-grid), 
 (orgtbl-uc-draw-cont): helper functions which go in table
 formulas 	for drawing bars 	* org.el: C-c p key
 binding on orgtbl-ascii-plot

---
 lisp/org-table.el |   83 +
 lisp/org.el   |1 +
 2 files changed, 84 insertions(+)

diff --git a/lisp/org-table.el b/lisp/org-table.el
index d1609f9..5921902 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -5004,6 +5004,89 @@ it here: http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.;
   (user-error Please download ascii-art-to-unicode.el (use C-c C-l to insert the link to it)))
 (buffer-string)))
 
+(defun orgtbl-ascii-draw (value min max optional width characters)
+  Draws an ascii bar in a table.
+VALUE is a the value to plot, the width of the bar to draw.
+A value equal to MIN will be displayed as empty (zero width bar).
+A value equal to MAX will draw a bar filling all the WIDTH.
+WIDTH is the expected width in characters of the column.
+CHARACTERS is a string of characters that will compose the bar,
+with shades of grey from pure white to pure black.
+It defaults to a 10 characters string of regular ascii characters.
+
+  (unless characters (setq characters  .:;c!lhVHW))
+  (unless width (setq width 12))
+  (if (stringp value)
+  (setq value (string-to-number value)))
+  (setq value (* (/ (- (+ value 0.0) min) (- max min)) width))
+  (cond
+   (( value 0) too small)
+   (( value width) too large)
+   (t
+(let ((len (1- (length characters
+  (concat
+   (make-string (floor value) (elt characters len))
+   (string (elt characters
+		(floor (* (- value (floor value)) len)
+
+(defun orgtbl-uc-draw-grid (value min max optional width)
+  Draws an ascii bar in a table.
+It is a variant of orgtbl-ascii-draw with Unicode block characters,
+for a smooth display.
+Bars appear as grids (to the extend the font allows).
+
+  ;; http://en.wikipedia.org/wiki/Block_Elements
+  ;; best viewed with the DejaVu Sans Mono font
+  (orgtbl-ascii-draw value min max width  \u258F\u258E\u258D\u258C\u258B\u258A\u2589))
+
+(defun orgtbl-uc-draw-cont (value min max optional width)
+  Draws an ascii bar in a table.
+It is a variant of orgtbl-ascii-draw with Unicode block characters,
+for a smooth display.
+Bars are solid (to the extend the font allows).
+
+  (orgtbl-ascii-draw value min max width  \u258F\u258E\u258D\u258C\u258B\u258A\u2589\u2588))
+
+;;;###autoload
+(defun orgtbl-ascii-plot (optional ask)
+  Draws an ascii bars plot in a column, out of values found in another column.
+A numeric prefix may be given to override the default 12 characters wide plot.
+
+  (interactive P)
+  (let ((col (org-table-current-column))
+	(min  1e999)
+	(max -1e999)
+	(length 12)
+	(table (org-table-to-lisp)))
+(cond ((consp ask)
+	   (setq length
+		 (or
+		  (read-string Length of column [12]  nil nil 12)
+		  12)))
+	  ((numberp ask)
+	   (setq length ask)))
+(mapc
+ (lambda (x)
+   (when (consp x)
+	 (setq x (nth (1- col) x))
+	 (when (string-match
+		^[-+]?\\([0-9]*[.]\\)?[0-9]*\\([eE][+-]?[0-9]+\\)?$
+		x)
+	   (setq x (string-to-number x))
+	   (if ( min x) (setq min x))
+	   (if ( max x) (setq max x)
+ (or (memq 'hline table) table)) ;; skip table header if any
+(org-table-insert-column)
+(org-table-move-column-right)
+(org-table-store-formulas
+ (cons
+  (cons
+   (number-to-string (1+ col))
+   (format '(%s $%s %s %s %s)
+	   orgtbl-ascii-draw col min max length))
+  (org-table-get-stored-formulas)))
+(org-table-recalculate t)))
+
 (defun org-table-get-remote-range (name-or-id form)
   Get a field value or a list of values in a range from table at ID.
 
diff --git a/lisp/org.el b/lisp/org.el
index 9b25204..928a5dd 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -19459,6 +19459,7 @@ boundaries.
 (org-defkey org-mode-map \C-c='org-table-eval-formula)
 (org-defkey org-mode-map \C-c''org-edit-special)
 (org-defkey org-mode-map \C-c`'org-table-edit-field)
+(org-defkey org-mode-map \C-cp'orgtbl-ascii-plot)
 (org-defkey org-mode-map \C-c|'org-table-create-or-convert-from-region)
 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
 (org-defkey org-mode-map \C-c~'org-table-create-with-table.el)

Re: [O] make orgtbl-ascii-plot easier to install

2014-07-28 Thread Bastien
Hi Thierry,

Thierry Banel tbanelweb...@free.fr writes:

 I moved orgtbl-ascii-plot to MELPA (it was in Worg).
 (orgtbl-ascii-plot draws plots in pure Emacs by typing C-c p).

Thanks for this.

 Of course, I am still open to Dominik Carsten suggestion
 to add it into the core (org-table.el).
 http://thread.gmane.org/gmane.emacs.orgmode/79668

Please go ahead and provide a patch for this, I agree this
is a nice addition for Org's core.

Best,

-- 
 Bastien