all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: "Kaushal Modi" <kaushal.modi@gmail.com>,
	"Help Gnu Emacs mailing list" <help-gnu-emacs@gnu.org>,
	"Andreas Röhler" <andreas.roehler@online.de>
Subject: RE: Rationale behind conversion of a nil prefix arg to numeric 1
Date: Mon, 5 Sep 2016 09:32:17 -0700 (PDT)	[thread overview]
Message-ID: <6012db9f-079c-48bd-b51d-64f162e869f9@default> (raw)
In-Reply-To: <CAFyQvY04XLvMowGgUw+h0gfm7G12KQFWw52ecMPSPfGOTy6bKQ@mail.gmail.com>

> > 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	other threads:[~2016-09-05 16:32 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-04 12:23 Rationale behind conversion of a nil prefix arg to numeric 1 Florian v. Savigny
2016-09-04 14:23 ` Eli Zaretskii
2016-09-04 17:11   ` Andreas Röhler
2016-09-04 18:33     ` Eli Zaretskii
2016-09-05  7:16       ` Andreas Röhler
2016-09-05 14:56         ` Eli Zaretskii
2016-09-05 15:22           ` Kaushal Modi
2016-09-05 16:32             ` Drew Adams [this message]
2016-09-05 18:41             ` Andreas Röhler
2016-09-05 19:02               ` Eli Zaretskii
2016-09-06  7:10                 ` tomas
2016-09-06  9:45                   ` Florian v. Savigny
2016-09-06 10:27                     ` tomas
2016-09-06 11:27                     ` Andreas Röhler
2016-09-06 13:27                       ` Florian v. Savigny
2016-09-06 15:22                         ` Drew Adams
2016-09-06 14:20                       ` Kaushal Modi
2016-09-06 16:44                         ` Andreas Röhler
2016-09-06 16:41                           ` Kaushal Modi
2016-09-07 23:11                         ` Charles Millar
2016-09-08 13:10                           ` Kaushal Modi
2016-09-08 14:42                             ` Charles Millar
2016-09-08 15:40                           ` Drew Adams
2016-09-06  7:22                 ` Andreas Röhler
2016-09-05 17:04         ` Drew Adams
2016-09-05 21:39           ` Florian v. Savigny
     [not found]           ` <<877faqvxfp.fsf@bertrandrussell.Speedport_W_723V_1_40_000>
2016-09-05 23:32             ` Drew Adams
2016-09-06  3:45               ` B.V. Raghav
2016-09-04 22:53   ` Florian v. Savigny
     [not found]   ` <<87d1kjjmys.fsf@bertrandrussell.Speedport_W_723V_1_40_000>
2016-09-05  0:52     ` Drew Adams
2016-09-15 12:47 ` Thien-Thi Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6012db9f-079c-48bd-b51d-64f162e869f9@default \
    --to=drew.adams@oracle.com \
    --cc=andreas.roehler@online.de \
    --cc=help-gnu-emacs@gnu.org \
    --cc=kaushal.modi@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.