0

I am new to Emacs and I am reading Elisp guide inside Emacs. I am at the Args as Variable or list chapter. There is listed below code-

 (concat "The " (number-to-string (+ 2 value)) "red foxes.") 

value here is an integer set by some function.

Now I want to print fox instead of foxes if the list argument to the concat function returns 1. How can I do that?

4
  • You are using + 2, so unless fill-column is -1 I don't see how you can get 1 out of that (+ 2 fill-column). C-h v fill-column RET on emacs -Q gives me 70, for instance. Commented Aug 10, 2021 at 9:47
  • @manuel-uberti I edited my question Commented Aug 10, 2021 at 9:56
  • Thank you, I updated my answer as well. Commented Aug 10, 2021 at 9:58
  • emacs.stackexchange.com/tags/elisp/info Commented Aug 10, 2021 at 14:50

1 Answer 1

4

I would use format instead of concat. For example:

(let* ((n (+ 2 value)) (f (if (= 1 n) "fox" "foxes"))) (format "The %d red %s" n f)) 

You can check the documentation of format (C-h f format RET) to understand the meaning of %d and %s and know more about this function. Check also C-h f let* RET to know more about let bindings.

2
  • 1
    FWIW, since Emacs 27 there's also the function ngettext for picking between singular/plural, although it's currently an English-specific no-op :) Commented Aug 10, 2021 at 10:40
  • This worked, but I think I have to study more to understand this code(let*). Thanks for follow up for documentation as well. Commented Aug 10, 2021 at 13:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.