From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thierry Banel Subject: Re: make orgtbl-ascii-plot easier to install Date: Thu, 28 Aug 2014 22:46:19 +0200 Message-ID: <53FF951B.4030300@free.fr> References: <53C2ADB0.2010404@free.fr> <87oaw9wg3s.fsf@bzg.ath.cx> <53E7F088.8010401@free.fr> <87tx4zhcvs.fsf@nicolasgoaziou.fr> <53FCE574.3010307@free.fr> <87vbpfnghj.fsf@nicolasgoaziou.fr> <53FCFF47.4060805@free.fr> <87r402oqce.fsf@nicolasgoaziou.fr> <53FE4F09.5080408@free.fr> <871ts1h5qv.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------060306000108060400070800" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:53168) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XN6aN-00082g-DY for emacs-orgmode@gnu.org; Thu, 28 Aug 2014 16:46:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XN6aL-0006ki-Sq for emacs-orgmode@gnu.org; Thu, 28 Aug 2014 16:46:23 -0400 Received: from smtp4-g21.free.fr ([2a01:e0c:1:1599::13]:13045) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XN6aL-0006ka-Hu for emacs-orgmode@gnu.org; Thu, 28 Aug 2014 16:46:21 -0400 Received: from [IPv6:2a01:e35:2e21:def0:dd82:c839:5eba:c7e6] (unknown [IPv6:2a01:e35:2e21:def0:dd82:c839:5eba:c7e6]) by smtp4-g21.free.fr (Postfix) with ESMTP id 3C5094C8092 for ; Thu, 28 Aug 2014 22:46:19 +0200 (CEST) In-Reply-To: <871ts1h5qv.fsf@nicolasgoaziou.fr> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org This is a multi-part message in MIME format. --------------060306000108060400070800 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable 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 =E9crit : > > 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. --------------060306000108060400070800 Content-Type: text/x-diff; name="0001-Add-ascii-plotting-in-tables.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-Add-ascii-plotting-in-tables.patch" >From fac4561630c229fdb2356a93a0b575376c80bb7e Mon Sep 17 00:00:00 2001 From: Thierry Banel 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-"] ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-"] ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-"] - ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-"]) + ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-"] + ["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-"] ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-"] @@ -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)" + "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 grid effect) +;; - orgtbl-uc-draw-cont (smooth unicode) + +;; This is best viewed with the "DejaVu Sans Mono" font +;; (use M-x set-default-font). + +(defun orgtbl-uc-draw-grid (value min max &optional width) + "Draws a bar in a table using block unicode characters. +It is a variant of orgtbl-ascii-draw with Unicode block +characters, for a smooth display. Bars appear as grids (to the +extent 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 a bar in a table using block unicode characters. +It is a variant of orgtbl-ascii-draw with Unicode block +characters, for a smooth display. Bars are solid (to the extent +the font allows)." + (orgtbl-ascii-draw value min max width + " \u258F\u258E\u258D\u258C\u258B\u258A\u2589\u2588")) + (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 9fada32..e05a416 100755 --- a/lisp/org.el +++ b/lisp/org.el @@ -19470,6 +19470,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) @@ -21142,7 +21143,8 @@ on context. See the individual commands for more information." ["Move Column Left" org-metaleft (org-at-table-p)] ["Move Column Right" org-metaright (org-at-table-p)] ["Delete Column" org-shiftmetaleft (org-at-table-p)] - ["Insert Column" org-shiftmetaright (org-at-table-p)]) + ["Insert Column" org-shiftmetaright (org-at-table-p)] + ["Ascii plot" orgtbl-ascii-plot (org-at-table-p)]) ("Row" ["Move Row Up" org-metaup (org-at-table-p)] ["Move Row Down" org-metadown (org-at-table-p)] -- 1.9.1 --------------060306000108060400070800--