0

With this function, I get the error

test: Wrong type argument: stringp, 2

Is it good to pass prefix this way? Thought that the argument prefix would be filled up with the prefix value.

(defun test (prefix)
  "TODO"
  (interactive "P")
  (message prefix)
  (message "Test"))

1 Answer 1

0
(defun foo (raw num)
  (interactive "P\np")
  (message "RAW: %S, Numeric: %S" raw num))

Now try M-x foo with different prefix args. The message will tell you how interactive handles P and p.


Your error is from function message, which C-h f will tell you expects a string as its first arg. Your first call to message calls it with whatever you used as the raw prefix argument.

M-x set-variable RET debug-on-error RET t enters the debugger when an error is raised, so you can see directly what your problem is.


Read about prefix arguments. In the Elisp manual (C-h i m elisp), i prefix argument takes you to node Prefix Command Arguments, which tells you all about it.

And if you're going to write Elisp code you owe it to yourself to read the manual An Introduction to Programming in Emacs Lisp, also available with C-h i.

10
  • Read it. I did not know one can pass raw and num for tho same prefix argument. For interpreting prefix argument, I find that the raw prefix argument is better to work with.
    – Dilna
    Commented Jul 6, 2022 at 1:35
  • Could I then use a condition such as (cond ((equal raw '(4)) (statements for no C-u condition)?
    – Dilna
    Commented Jul 6, 2022 at 1:56
  • If it is equal to '(4), then there was a C-u prefix. But why not elaborate your function in the light of your findings and try out the cond? Just use (message "No C-u") for the consequent e.g. It would be quicker to try out dozens of variations in the time that you are waiting for an answer here (which might never come).
    – NickD
    Commented Jul 6, 2022 at 4:25
  • You are right of course but was more interested in criticism. Although I made things to work, experienced ones might suggest better ways than my crude test implementation.
    – Dilna
    Commented Jul 6, 2022 at 12:56
  • Why is it that you use raw and num without specifying them as optional function arguments?. I think the correct way to pass prefix arguments is to declare them optional. But I do not know exactly why that is so.
    – Dilna
    Commented Jul 6, 2022 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.