4

Is there a pre-defined emacs function or emacs add-on package that will fill a rectangle with numbers? For example: 012 9 010 or 10 008 11 The closest I've found is rectangle-number-lines, which seems to have limitations. If I'm not mistaken, it doesn't allow decreasing numbers and forces step-value=1 (e.g. can't have 1, 3, 5).

  • Can you please clarify what you want to do? – Dan Apr 29 '15 at 21:04
  • 1
    sorry for the oversight. i want to select a rectangular region and then fill it with numbers, a different number for each line. i want the option of having the numbers decrease instead of increase, and i want the option of having the difference between numbers on successive lines (the step) to be any (moderately sized) positive number that I want. – zugzwang Apr 29 '15 at 21:12
  • 1
    You should M-x report-emacs-bug to suggest that rectangle-number-lines be enhanced to also take an initial number and a step value. I think these would be useful improvements. – phils Apr 29 '15 at 23:30
  • believe it or not, i don't have internet at home, where my emacs is... i'm in the apt mgr's ofc now. what is the raw equivalent to M-x report-emacs-bug? – zugzwang Apr 30 '15 at 20:24
  • zugzwang: The manual approach is to email the lists.gnu.org/mailman/listinfo/bug-gnu-emacs list address. Be sure to look at gnu.org/software/emacs/manual/html_node/emacs/Bugs.html as well. – phils May 1 '15 at 0:31
3

Here's a way to do this using Calc:

  1. C-x * c launches the calculator.
  2. v x ask to create a vector of a given length.
  3. 10 RET let it be ten elements long (for example).
  4. P * multiply all numbers by Pi (again, just an example).
  5. v u unpacks the vector into the stack.
  6. C-u 0 y copy the entire stack back into the last active buffer.

This might look like a lot of operations to do, but really, if you are used to Calc, then it takes less than a few seconds to do it.


Below is the result:

3.14159265359
6.28318530718
9.42477796077
12.5663706144
15.707963268
18.8495559215
21.9911485751
25.1327412287
28.2743338823
31.4159265359
  • very creative; as newbie I would have never thought of using calc. I wasn't looking forward to having to code my own function. i am almost always working with integers. does the calc approach permit formatting, (e.g. %2d), as rectangle-number-lines does? If not, can formatting (somehow) be coupled with the calc approach? – zugzwang Apr 29 '15 at 23:11
  • This seems to include line numbers sometimes as well, is there a way to disable that? – user2699 Apr 30 '15 at 0:32
  • @user2699 it will include the line number (and a line break) if you select and copy from the Calc buffer. The alternative is to use y (possibly with the prefix). Positive prefix gives the number of lines to yank, negative prefix gives the number of the line to yank and zero (as is in the example above) tells it to copy the whole stack. – wvxvw Apr 30 '15 at 6:17
  • As for the formatting, I think you might be interested in d f option: gnu.org/software/emacs/manual/html_node/calc/Float-Formats.html – wvxvw Apr 30 '15 at 6:19
  • 1
    user2699: In calc use d l to switch off line numbering. That way they won't be included in the text when it is copied into the other buffer. – phils Apr 30 '15 at 7:15
2

The tiny package might suit your needs, which is also available through Melpa.

How to use tiny-expand?

  • Write a little snippet following the below general format
  • With point at the end of that snippet, M-x tiny-expand

General Format

;; General Format
;; mBSEO|F
;;  ||||||
;;  |||||+--> (optional) Format - %x | 0x%x | %c | %s | %(+ x x) | %014.2f | %03d | %(date "Jan 16" (* x 7))
;;  ||||+---> (optional) Pipe character to separate Format for reading clarity
;;  |||+----> (optional) Operator - *xx | (* x x) | (+ x ?A) | *2+3x | (* 2 (+ 3 x))
;;  ||+-----> End value
;;  |+------> (optional) Separator - Space | , | \n (default=Space)
;;  +-------> (optional) Begin value (default=0)
;; - No space allowed between 'm' and 'B'
;; - No space allowed between 'E' and 'O'

Solution

Following the above general format, here is how you can get a column of numbers with different increments, starting number, end number, etc

  1. Type any of the below snippets in the buffer

    • Numbers starting with 0, incrementing by 1: m0\n10
    • Numbers starting with 0, incrementing by 2: m0\n10*2x
    • Numbers starting with 1, incrementing by 2: m0\n10+1*2x
  2. Do M-x tiny-expand

More of my notes on using this package

1

You can use multiple-cursors and the function mc/insert-numbers to do most of what you want (I don't think you can set the step).

Pipes are cursors.

|
|
|
|

After running mc/insert-numbers:

0|
1|
2|
3|

After C-4 C-2 mc/insert-numbers:

42|
43|
44|
45|
1

You could wrap rectangle-number-lines and add a step argument. Call it like C-u M-x my-r-n-l to prompt for the arguments.

(defun my-r-n-l (start end start-at step &optional format)
  "Insert numbers in front of the region-rectangle.

START-AT, if non-nil, should be a number from which to begin
counting.  STEP, if non-nil should be a number to increment by.
FORMAT, if non-nil, should be a format string to pass to `format'
along with the line count.  When called interactively with a
prefix argument, prompt for START-AT, STEP, and FORMAT."
  (interactive
   (if current-prefix-arg
       (let* ((start (region-beginning))
              (end   (region-end))
              (start-at (read-number "Number to count from: " 1))
              (step (read-number "Step: " 1)))
         (list start end start-at step
               (read-string "Format string: "
                            (rectange--default-line-number-format
                             start end start-at))))
     (list (region-beginning) (region-end) 1 1 nil)))
  (cl-letf (((symbol-function 'rectangle-number-line-callback)
             `(lambda (start _end format-string)
                (move-to-column start t)
                (insert (format format-string rectangle-number-line-counter))
                (setq rectangle-number-line-counter
                      (+ rectangle-number-line-counter ,step)))))
    (rectangle-number-lines start end start-at format)))
  • wow, thanks. interest partly because i need this and partly (as newbie) to dive in to the elisp pool simultaneous with the elisp tutorial. you killed two birds. – zugzwang Apr 30 '15 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.