fevgenym
fevgenym

Reputation: 598

Emacs interactive read escape sequence

I'm writing simple elisp function, that takes 3 arguments interactively: from, to and separator. Then it inserts a sequence of numbers:

(defun gen-seq (from to sep)
  "Generates sequence of numbers FROM .. TO with separator SEP"
  (interactive "nFrom: \nnTo: \nMSeparator: ")
  (insert (mapconcat 'number-to-string (number-sequence from to) sep)))

But when the sep is \n, it generates something like

1\n2\n3\n4\n5\n6\n7\n8\n9\n10

Is it possible to achieve a column of numbers with this function in my Emacs buffer?

Upvotes: 0

Views: 282

Answers (2)

phils
phils

Reputation: 73274

Nick's answer is the standard approach for entering newlines, but if you really want Emacs to read the string argument as if it were an elisp quoted string, you can do that:

(defun gen-seq (from to sep)
  "Generates sequence of numbers FROM .. TO with separator SEP"
  (interactive "nFrom: \nnTo: \nMSeparator: ")
  (let* ((sepesc (replace-regexp-in-string "\"" "\\\\\"" sep))
         (sepnew (car (read-from-string (concat "\"" sepesc "\"")))))
    (insert (mapconcat 'number-to-string (number-sequence from to) sepnew))))

You can also use (interactive "x") to read an argument, except the user would need to type the double quotes: "\n"

Upvotes: 1

NickD
NickD

Reputation: 6412

You can enter a newline as separator by typing C-q C-j. See Inserting Text in the emacs manual for details.

Upvotes: 1

Related Questions