dired.el introduces the function `dired-plural-s' to pluralize
correctly output messages as:
(format "Kill Dired buffer%s of %s, too? "
(dired-plural-s (length buf-list))
In this example, it returns "s" if (length buf-list) > 1. Otherwise,
it returns "".
Of course, this task is not exclusive of Dired: many other packages
in Emacs source code do the same thing.
I agree. I have also had something in my emacs config that made me wish of the existence of a function like this.
As you know, for some words the plural is different that just
appending "s" to the end. For instance, in message.el:
"Really use %s possibly unknown group%s: %s? "
(if (= (length errors) 1) "this" "these")
(if (= (length errors) 1) "" "s")
(mapconcat 'identity errors ", "))
"this" -> "these" is not handled by `dired-plural-s'.
I am wondering if it has sense to add a standard function,
`string-plural-s', covering both cases: I), II).
Then, we might do
`dired-plural-s' and alias for the new function.
We might update all the 'plularizations' in Emacs source code to use
the new function as well.
Following function could do the job:
(defun string-plural-s (arg &optional string plural)
Return plural of STRING if ARG is nil, or an integer >1, or a seq of
If ARG is =1 or a sequence of length =1, return STRING.
Optional arg PLURAL is the plural of STRING.
If STRING is nil, return \"s\" or \"\"."
(let ((single (or string ""))
(plural (if (and string plural) plural
(apply #'string (append string "s" nil)))))
(cond ((natnump arg)
(if (= arg 1) single plural))
((null arg) plural)
(if (null (cdr arg)) single plural))
(if (= (length arg) 1) single plural))
(list arg 'natnump 'listp 'arrayp))))))
What do you think about this idea?
I like this idea.
The only comment I have is that the "append 's' to pluralize" part should not be hard-coded into the string-plural-s function.
How about have a "string-plural--conditions-function" variable (or something named like that), and then you funcall that variable in string-plural-s?
The "string-plural--conditions-function" variable would be a lambda function that would accept an arg STRING and output a plural string for that STRING.
Inside the lambda, would be an elaborate cond form that would decide how to pluralize the string
this -> these
grape -> grapes
knife -> knives
ruby -> rubies
sheep -> sheep
This "string-plural--conditions-function" variable will be quite an elaborate cond form as it would try to cover all the corner cases of English pluralization. I am suggesting this to be a separate function so that the logic of string-plural-s stays clear and succinct and the English grammar rules do not come in the way while understanding that function.