[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Generalize and standarize dired-plural-s

From: Tino Calancha
Subject: Generalize and standarize dired-plural-s
Date: Sun, 18 Sep 2016 23:00:21 +0900 (JST)
User-agent: Alpine 2.20 (DEB 67 2015-01-07)


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))
        (file-name-nondirectory fn))

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.

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 length >1.
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)
          ((consp arg)
           (if (null (cdr arg)) single plural))
          ((arrayp arg)
           (if (= (length arg) 1) single plural))
           (signal 'wrong-type-argument
                   (list arg 'natnump 'listp 'arrayp))))))

What do you think about this idea?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]