diff options
| author | Jim Porter | 2024-10-19 11:52:42 -0700 |
|---|---|---|
| committer | Jim Porter | 2024-10-19 12:01:10 -0700 |
| commit | 43d5b7a04c4b1a8a7d57f25824df2e8720d2c567 (patch) | |
| tree | 189201134e9e92c6bbf156ebb772f573f65cf71f /doc/misc | |
| parent | c2a9f519f7e9a5cbb5dbe50ba7b4e4b2757bb3b6 (diff) | |
| download | emacs-43d5b7a04c4b1a8a7d57f25824df2e8720d2c567.tar.gz emacs-43d5b7a04c4b1a8a7d57f25824df2e8720d2c567.zip | |
Lazily convert numeric strings to Lisp numbers in Eshell
This should reduce the number of issues with Eshell converting strings
to numbers too aggressively and losing information (e.g. "001" -> 1)
while still allowing almost all of the beneficial uses, like summing a
list of numeric strings with '+'.
* lisp/eshell/esh-util.el (eshell--do-mark-numeric-string): New
function.
(eshell-convert-to-number): Make obsolete in favor of...
(eshell-mark-numeric-string): ... this. Update callers.
* lisp/eshell/esh-arg.el (eshell--numberlike-p): New function...
(eshell-concat-1): ... use it.
* test/lisp/eshell/esh-util-tests.el: Reimplement type conversion tests
to use 'eshell-convertible-to-number-p' instead.
* test/lisp/eshell/esh-var-tests.el
(esh-var-test/interp-var-splice-concat, esh-var-test/interp-concat-cmd)
(esh-var-test/interp-convert-var-split-indices)
(esh-var-test/interp-convert-quoted-var-split-indices)
(esh-var-test/interp-convert-cmd-multiline)
(esh-var-test/interp-convert-cmd-split-indices): Adjust tests to check
the new behavior.
* doc/misc/eshell.texi (Type Conversion): New section.
(Expansion): Clarify concatenation behavior.
Diffstat (limited to 'doc/misc')
| -rw-r--r-- | doc/misc/eshell.texi | 61 |
1 files changed, 46 insertions, 15 deletions
diff --git a/doc/misc/eshell.texi b/doc/misc/eshell.texi index 9a2714b14fb..ee4d0ca09c8 100644 --- a/doc/misc/eshell.texi +++ b/doc/misc/eshell.texi | |||
| @@ -432,16 +432,6 @@ This command writes a list of all files matching the glob pattern | |||
| 432 | 432 | ||
| 433 | @node Arguments | 433 | @node Arguments |
| 434 | @section Arguments | 434 | @section Arguments |
| 435 | Ordinarily, Eshell parses arguments in command form as either strings | ||
| 436 | or numbers, depending on what the parser thinks they look like. To | ||
| 437 | specify an argument of some other data type, you can use a Lisp form | ||
| 438 | (@pxref{Invocation}): | ||
| 439 | |||
| 440 | @example | ||
| 441 | ~ $ echo (list 1 2 3) | ||
| 442 | (1 2 3) | ||
| 443 | @end example | ||
| 444 | |||
| 445 | When calling external commands (and many built-in Eshell commands, | 435 | When calling external commands (and many built-in Eshell commands, |
| 446 | too) Eshell will flatten the arguments the command receives, so | 436 | too) Eshell will flatten the arguments the command receives, so |
| 447 | passing a list as an argument will ``spread'' the elements into | 437 | passing a list as an argument will ``spread'' the elements into |
| @@ -454,13 +444,15 @@ multiple arguments: | |||
| 454 | 3 | 444 | 3 |
| 455 | @end example | 445 | @end example |
| 456 | 446 | ||
| 457 | @subsection Quoting and escaping | 447 | @subsection Quoting and Escaping |
| 458 | As with other shells, you can escape special characters and spaces by | 448 | As with other shells, you can escape special characters and spaces by |
| 459 | prefixing the character with a backslash (@samp{\}), or by surrounding | 449 | prefixing the character with a backslash (@samp{\}), or by surrounding |
| 460 | the string with apostrophes (@samp{''}) or double quotes (@samp{""}). | 450 | the string with apostrophes (@samp{''}) or double quotes (@samp{""}). |
| 461 | This is needed especially for file names with special characters like | 451 | This is needed especially for file names with special characters like |
| 462 | pipe (@samp{|}) or square brackets (@samp{[} or @samp{]}), which could | 452 | pipe (@samp{|}) or square brackets (@samp{[} or @samp{]}), which could |
| 463 | be part of remote file names. | 453 | be part of remote file names. In addition, quoting or escaping an |
| 454 | argument will prevent it from being converted to a number when passed to | ||
| 455 | a Lisp function. | ||
| 464 | 456 | ||
| 465 | When you escape a character with @samp{\} outside of any quotes, the | 457 | When you escape a character with @samp{\} outside of any quotes, the |
| 466 | result is the literal character immediately following it. For | 458 | result is the literal character immediately following it. For |
| @@ -495,7 +487,46 @@ When using expansions (@pxref{Expansion}) in an Eshell command, the | |||
| 495 | result may potentially be of any data type. To ensure that the result | 487 | result may potentially be of any data type. To ensure that the result |
| 496 | is always a string, the expansion can be surrounded by double quotes. | 488 | is always a string, the expansion can be surrounded by double quotes. |
| 497 | 489 | ||
| 498 | @subsection Special argument types | 490 | @subsection Type Conversion |
| 491 | When invoking a Lisp function via command form, Eshell automatically | ||
| 492 | converts string arguments that look like numbers to actual Lisp | ||
| 493 | numbers in order to make it easier to work with numeric values. You can | ||
| 494 | prevent this conversion on a case-by-case basis by quoting or escaping | ||
| 495 | the argument: | ||
| 496 | |||
| 497 | @example | ||
| 498 | ~ $ type-of 1 | ||
| 499 | integer | ||
| 500 | ~ $ type-of "1" | ||
| 501 | string | ||
| 502 | @end example | ||
| 503 | |||
| 504 | When invoking a subcommand in command form, Eshell will split the output | ||
| 505 | line-by-line into a list. Additionally, if every line looks like a | ||
| 506 | number, then Eshell will mark them as numeric so that passing them to a | ||
| 507 | Lisp function will convert them to Lisp numbers: | ||
| 508 | |||
| 509 | @example | ||
| 510 | ~ $ cat numbers.txt | ||
| 511 | 01 | ||
| 512 | 02 | ||
| 513 | 03 | ||
| 514 | ~ $ + $@@@{cat numbers.txt@} | ||
| 515 | 6 | ||
| 516 | @end example | ||
| 517 | |||
| 518 | If you find this behavior inconvenient for certain functions, you can | ||
| 519 | tell Eshell not to perform this conversion for that function: | ||
| 520 | |||
| 521 | @example | ||
| 522 | (put \\='find-file \\='eshell-no-numeric-conversions t) | ||
| 523 | @end example | ||
| 524 | |||
| 525 | @vindex eshell-convert-numeric-arguments | ||
| 526 | You can also disable this conversion behavior entirely by setting | ||
| 527 | @code{eshell-convert-numeric-arguments} to @code{nil}. | ||
| 528 | |||
| 529 | @subsection Special Argument Types | ||
| 499 | In addition to strings and numbers, Eshell supports a number of | 530 | In addition to strings and numbers, Eshell supports a number of |
| 500 | special argument types. These let you refer to various other Emacs | 531 | special argument types. These let you refer to various other Emacs |
| 501 | Lisp data types, such as lists or buffers. | 532 | Lisp data types, such as lists or buffers. |
| @@ -1764,8 +1795,8 @@ behavior depends on the types of each value being concatenated: | |||
| 1764 | Concatenate both values together. | 1795 | Concatenate both values together. |
| 1765 | 1796 | ||
| 1766 | @item one or both numbers | 1797 | @item one or both numbers |
| 1767 | Concatenate the string representation of each value, converting back to | 1798 | Concatenate the string representation of each value. If either value is |
| 1768 | a number if possible. | 1799 | numeric, mark the concatenated value as numeric if possible. |
| 1769 | 1800 | ||
| 1770 | @item one or both (non-@code{nil}) lists | 1801 | @item one or both (non-@code{nil}) lists |
| 1771 | Concatenate ``adjacent'' elements of each value (possibly converting | 1802 | Concatenate ``adjacent'' elements of each value (possibly converting |