emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: Mario Frasca <mario@anche.no>
Cc: emacs-orgmode@gnu.org
Subject: Re: `with` as a list.
Date: Sat, 30 May 2020 20:25:34 +0000	[thread overview]
Message-ID: <87ftbhuq4h.fsf@kyleam.com> (raw)
In-Reply-To: <dcba6e4d-2d65-1e6a-f7c6-2675de17f45e@anche.no>

Mario Frasca writes:

> diff --git a/lisp/org-plot.el b/lisp/org-plot.el
> index a23195d2a..c44cca991 100644
> --- a/lisp/org-plot.el
> +++ b/lisp/org-plot.el
> @@ -156,7 +156,8 @@ and dependent variables."
>  			  table)))
>      ;; write table to gnuplot grid datafile format
>      (with-temp-file data-file
> -      (let ((num-rows (length table)) (num-cols (length (nth 0 table)))
> +      (let ((num-rows (length table))
> +	    (num-cols (length (nth 0 table)))

I don't disagree with the style preference here, but it's easier on
reviewers if the patch doesn't contain unrelated changes.

>  	    (gnuplot-row (lambda (col row value)
>  			   (setf col (+ 1 col)) (setf row (+ 1 row))
>  			   (format "%f  %f  %f\n%f  %f  %f\n"
> @@ -179,6 +180,22 @@ and dependent variables."
>  	  (setf back-edge "") (setf front-edge ""))))
>      row-vals))
>  
> +(defun org-plot/zip-deps-with (num-cols ind deps with)
> +  "Describe each column to be plotted as (col . with).
> +Loops over DEPS and WITH in order to cons their elements.
> +If the DEPS list of columns is not given, use all columns from 1
> +to NUM-COLS, excluding IND.
> +If WITH is given as a string, use the given value for all
> +columns.
> +If WITH is given as a list, and it's shorter than DEPS, expand it
> +with the global default value."

Thanks for updating the docstring.

> +  (unless deps
> +    (setq deps (remove ind (number-sequence 1 num-cols))))
> +  (unless (listp with)
> +    (setq with (make-list (length deps) with)))
> +  (setq with (append with (make-list (- (length deps) (length with)) "lines")))

The caller can crash this with something like

    make-list(-1 "lines")
    (append with (make-list (- (length deps) (length with)) "lines"))
    (setq with (append with (make-list (- (length deps) (length with)) "lines")))
    org-plot/zip-deps-with(3 1 (2 3) (lines points lines))
    [...]

if they accidentally give more `with` values than `deps`.

Also, if the `(unless (listp with) ...` condition is entered, I think
the second make-list call is unnecessary.

So, combining those two points, perhaps something like this:

    (setq with (if (listp with)
                   (append with
                           (make-list (max 0 (- (length deps) (length with)))
                                      "lines"))
                 (make-list (length deps) with)))

> +  (cl-mapcar 'cons deps with))

Nit-pick: s/'/#'/

The latter is short for `function' and, unlike the former, lets the
byte-compiler know that cons is a function (enabling a warning if, say,
you accidentally typed `conss`).

>  (defun org-plot/gnuplot-script (data-file num-cols params &optional preface)
>    "Write a gnuplot script to DATA-FILE respecting the options set in PARAMS.
>  NUM-COLS controls the number of columns plotted in a 2-d plot.
> @@ -240,22 +257,24 @@ manner suitable for prepending to a user-specified script."
>  			       "%Y-%m-%d-%H:%M:%S") "\"")))
>      (unless preface
>        (pcase type			; plot command
> -	(`2d (dotimes (col num-cols)
> -	       (unless (and (eq type '2d)
> -			    (or (and ind (equal (1+ col) ind))
> -				(and deps (not (member (1+ col) deps)))))
> -		 (setf plot-lines
> -		       (cons
> -			(format plot-str data-file
> -				(or (and ind (> ind 0)
> -					 (not text-ind)
> -					 (format "%d:" ind)) "")
> -				(1+ col)
> -				(if text-ind (format ":xticlabel(%d)" ind) "")
> -				with
> -				(or (nth col col-labels)
> -				    (format "%d" (1+ col))))
> -			plot-lines)))))
> +	(`2d (dolist
> +		 (col-with
> +		  (org-plot/zip-deps-with num-cols ind deps with))
> +	       (setf plot-lines
> +		     (cons
> +		      (format plot-str data-file
> +			      (or (and ind (> ind 0)
> +				       (not text-ind)
> +				       (format "%d:" ind)) "")
> +			      (car col-with)
> +			      (if text-ind (format ":xticlabel(%d)" ind) "")
> +			      (cdr col-with)
> +			      (apply (lambda (x)
> +				       (if (= 0 (length x))
> +					   (format "%d" (car col-with))
> +					 x))
> +				     (list (nth (1- (car col-with)) col-labels))))

If I'm reading it correctly, (apply ...) could be simplified to

    (or (nth (1- (car col-with)) col-labels)
        (format "%d" (car col-with)))

> diff --git a/testing/lisp/test-org-plot.el b/testing/lisp/test-org-plot.el
> new file mode 100644
> index 000000000..2bbd09b5f
> --- /dev/null
> +++ b/testing/lisp/test-org-plot.el
> @@ -0,0 +1,50 @@
> +;;; test-org-plot.el --- Tests for org-plot.el
> +
> +;; Copyright (C) 2020  Mario Frasca
> +
> +;; Author: Mario Frasca <mario at anche dot no>
> +
> +;; Released under the GNU General Public License version 3
> +;; see: http://www.gnu.org/licenses/gpl-3.0.html

Could you update this header to match the style used by other tests
(see, e.g., test-org-num.el)?

Tests themselves look good to me.  Thanks for adding them.  I think it'd
also be good to add a test for the with-longer-than-deps case I
mentioned above.

The manual should also be updated to mention this new feature, and it'd
be good to have an ORG-NEWS entry.

Thanks for working on this.


  reply	other threads:[~2020-05-30 20:26 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-22 16:07 `with` as a list Mario Frasca
2020-05-28 13:34 ` [PATCH] [FEATURE] " Mario Frasca
2020-05-30  6:22 ` Kyle Meyer
2020-05-30 14:23   ` Mario Frasca
2020-05-30 14:38     ` Mario Frasca
2020-05-30 20:23     ` Kyle Meyer
2020-05-30 21:29       ` Mario Frasca
2020-05-31 20:18         ` Mario Frasca
2020-06-01  0:19           ` Kyle Meyer
2020-06-01  1:47             ` Mario Frasca
2020-06-01  2:31               ` Kyle Meyer
2020-06-03 15:09                 ` Mario Frasca
2020-06-03 15:13                   ` Bastien
2020-06-03 15:18                     ` Mario Frasca
2020-06-03 15:29                       ` Bastien
2020-06-03 17:08                         ` Mario Frasca
2020-06-03 22:15                         ` Mario Frasca
2020-06-03 15:25                     ` Mario Frasca
2020-06-03 15:30                       ` Bastien
2020-05-30 16:01   ` Mario Frasca
2020-05-30 20:25     ` Kyle Meyer [this message]
2020-05-30 21:36       ` Mario Frasca
2020-05-31  0:36         ` Kyle Meyer

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

  List information: https://www.orgmode.org/

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

  git send-email \
    --in-reply-to=87ftbhuq4h.fsf@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mario@anche.no \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).