Subject: [PATCH] 30.0.50; calc vector parsing: spaces should not be separators except with [...]

From: Paul Nelson <[email-masked]>
Date: Fri, 26 Jan 2024 22:38:28 +0100

There are two ways to input vectors (or matrices) in calc.

(A) Using brackets (e.g., [x,y,z]), in any language mode.

(B) Using language-specific constructions (e.g., \begin{pmatrix} x & y
& z \end{pmatrix} in latex mode).

I would like to report a bug with (B) and to propose a fix.

Steps to reproduce (all with emacs -Q): use "C-x * * d L" to start
calc and set the language mode to latex, then evaluate the lisp forms
below, e.g., in the *scratch* buffer.

Example 1: parsing gives the wrong answer.

(math-read-expr "\\begin{pmatrix} x y & z \\end{pmatrix}")
Result: (vec (var x var-x) (var y var-y) (var z var-z))
Expected result: (vec (* (var x var-x) (var y var-y)) (var z var-z))

Example 2: parsing gives an error on valid latex.

(math-read-expr "\\begin{pmatrix} x \\\\ y + 1 \\end{pmatrix}")
Result: (error 24 "Expected a number")
Expected result: (vec (vec (var x var-x)) (vec (+ (var y var-y) 1)))

These examples are not contrived.  They make it infeasible to use calc
as a latex parser for matrices.  Fortunately, they are easy to fix.

Both issues arise because calc allows spaces as separators with both
methods (A) and (B).  I believe that spaces should be allowed as
separators with method (A) (e.g., [x y z] as an alternative to
[x,y,z]), but never with method (B).

With the attached patch, calc allows spaces as separators in a vector
only when the vector was specified using "[".

In GNU Emacs 30.0.50 (build 1, aarch64-apple-darwin22.5.0, NS
 appkit-2299.60 Version 13.4.1 (Build 22F82)) of 2023-08-17 built on
 d51735
Repository revision: 5856ea5e4e897f4cb5cd1c3c28d14b335fe5cf54
Repository branch: master
Windowing system distributor 'Apple', version 10.3.2487
System Description:  macOS 14.2.1

Configured using:
 'configure --with-ns --with-native-compilation --with-tree-sitter
 --with-gif --with-png --with-jpeg --with-rsvg --with-tiff
 --with-imagemagick --with-x-toolkit=gtk3 --with-xwidgets'

Configured features:
ACL DBUS GLIB GNUTLS IMAGEMAGICK JSON LCMS2 LIBXML2 MODULES NATIVE_COMP
NOTIFY KQUEUE NS PDUMPER PNG RSVG SQLITE3 THREADS TOOLKIT_SCROLL_BARS
TREE_SITTER WEBP XIM XWIDGETS ZLIB

Important settings:
  value of $LC_CTYPE: UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util time-date mm-decode mm-bodies mm-encode mail-parse
rfc2231 mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045
ietf-drums mm-util mail-prsvr mail-utils calc-vec calc-aent calc-math
calc-lang compile text-property-search comint ansi-osc ansi-color ring
comp comp-cstr warnings icons subr-x rx cl-seq cl-macs gv cl-extra
help-mode bytecomp byte-compile calc-ext calc-misc cl-loaddefs cl-lib
calc-menu calc calc-loaddefs rect calc-macs rmc iso-transl tooltip cconv
eldoc paren electric uniquify ediff-hook vc-hooks lisp-float-type
elisp-mode mwheel term/ns-win ns-win ucs-normalize mule-util
term/common-win touch-screen tool-bar dnd fontset image regexp-opt
fringe tabulated-list replace newcomment text-mode lisp-mode prog-mode
register page tab-bar menu-bar rfn-eshadow isearch easymenu timer select
scroll-bar mouse jit-lock font-lock syntax font-core term/tty-colors
frame minibuffer nadvice seq simple cl-generic indonesian philippine
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite emoji-zwj charscript
charprop case-table epa-hook jka-cmpr-hook help abbrev obarray oclosure
cl-preloaded button loaddefs theme-loaddefs faces cus-face macroexp
files window text-properties overlay sha1 md5 base64 format env
code-pages mule custom widget keymap hashtable-print-readable backquote
threads xwidget-internal dbusbind kqueue cocoa ns lcms2 multi-tty
make-network-process native-compile emacs)

Memory information:
((conses 16 129014 14615) (symbols 48 11069 0) (strings 32 29343 1471)
 (string-bytes 1 823136) (vectors 16 20069)
 (vector-slots 8 395724 11724) (floats 8 43 191) (intervals 56 259 0)
 (buffers 992 13))