help-gnu-emacs
[Top][All Lists]
Advanced

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

RE: Rationale behind conversion of a nil prefix arg to numeric 1


From: Drew Adams
Subject: RE: Rationale behind conversion of a nil prefix arg to numeric 1
Date: Mon, 5 Sep 2016 09:32:17 -0700 (PDT)

> > Commands that need to distinguish between those should not use "p".
>
> Exactly.
> Just treat the "p" (lowercase p) interactive form as the special case
> where you only need to deal with numeric arguments and the default numeric
> argument is 1.
> 
> If you need to deal with numeric, nil and other non-numeric arguments like
> (4), (16), etc, use the "P" (uppercase p) interactive form. The "P"
> interactive form passes the args to the function in their raw, untouched
> form.

What Eli and Kaushal said.

Again, this is a _feature_.  You just need to learn what it is about
(and what it is not about).  The doc is quite clear - give it a try.

In particular, if your code wants to know WHETHER a prefix argument
was EXPLICITLY provided by the USER, then you need to test the RAW
prefix argument.  If you then want to know what the NUMERIC value
is, use `prefix-numeric-value':

(defun foo (arg)
  (interactive "P") ; <=== uppercase P: RAW prefix arg
  (if (not arg)
      (no-prefix-arg-provided---usual/default-case)
    (let ((nval  (prefix-numeric-value arg)))
      (cond ((= 42 nval) (answer-to-everything-it-seems))
            ((> nval 0)  (at-least-its-positive))
            ((< nval 0)  (so-negative!))
            (t           (nothing-at-all))))))

If you want to check for PARTICULAR non-nil raw prefix values:

(if (not arg)
    (no-prefix-arg-provided---usual/default-case)
  (cond ((= arg '-)   (handle-M--))
        ((= arg '-42) (handle-M---4-2-or-C-u---4-2)) ; etc.
        ((= arg '4)   (handle-M-4--or-C-u-4))
        ((and (consp arg)  (= 4 (car arg)))  (handle-plain-C-u))
        ((and (consp arg)  (= 16 (car arg))) (handle-plain-C-u-C-u))
        ...))

Note that `C-u' gives `(4)' as the raw prefix arg, and `C-u 4' gives
`4' as the raw prefix arg.  And `prefix-numeric-value' gives `4' in
both cases.

Experiment:

(defun foo (arg)
  "Show the raw prefix arg and its numeric value."
  (interactive "P")
  (message "ARG: %S, Numeric value: %S"
           arg (prefix-numeric-value arg)))

The case that this thread is about is just this one: `M-x foo'.
(Much ado about nothing.)



reply via email to

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