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

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

Re: Optional Arguments


From: Alexis Roda
Subject: Re: Optional Arguments
Date: Mon, 7 Dec 2020 09:16:56 +0100

Hi,

Not sure what your question is.

Regarding the error, I'm assuming that you execute the function with 'M-x
typh-word-markers'. That won't work interactively because the function
expects two arguments and 'interactive' don't tell emacs how to get them.
It works if you execute it with 'M-: (typh-word-markers nil nil)', from
IELM or from the scratch buffer.

That said, in your code the arguments are not required, they are used only
as local variables, not to provide information to the function, so the
function can be written with a 'let' [1][2] form defining local variables:

(defun typh-word-markers ()
  (interactive)
  (let ((ma)
        (mb))
    (skip-chars-backward "[:alpha:]")
    (setq ma (point))
    (skip-chars-forward "[:alpha:]")
    (setq mb (point))
    (message "[ma,mb]: %s,%s" ma mb)) )

In the first function '&optional' is not strictly required. Since you
specify in 'interactive' the 'n' option [3] (A number, read with the
minibuffer. If the input is not a number, the user has to try again. ‘n’
never uses the prefix argument. Prompt.) the user will be prompted for a
numeric value and the function will always get an argument. That's the
opposite case to typh-word-markers.

Probably you want the 'p' code [3]: The numeric prefix argument. (Note that
this ‘p’ is lower case.) No I/O.

(defun typh-skip-chars (n)
   (interactive "p")
   (if (= n 1)
      (skip-chars-forward "[:alpha:]")
     (skip-chars-backward "[:alpha:]")) )

That way executing 'M-x typh-skip-chars' will skip chars forward while 'C-u
M-x typh-skip-chars' will skip chars backward. No interactive input will be
requested to the user, the input to the function is provided with the
prefix argument [4]. That's more idiomatic.


Hope this helps

[1]
https://www.gnu.org/software/emacs/manual/html_node/eintr/Parts-of-let-Expression.html
[2]
https://www.gnu.org/software/emacs/manual/html_node/eintr/Sample-let-Expression.html#Sample-let-Expression
[3]
https://www.gnu.org/software/emacs/manual/html_node/elisp/Interactive-Codes.html
[4] https://www.gnu.org/software/emacs/manual/html_node/emacs/Arguments.html


Missatge de l'adreça <pietru@caramail.com> del dia dl., 7 de des. 2020 a
les 4:17:

> Would like to have two functions
>
> 1. skip-chars - skip to beg of word (if no argument) or end of word (if
> argument is 1)
>
> 2. word-markers - get beg and end of a word position of word
>
> Have written the following. For the first one, I have not used optional
> arguments before.
> And have read documentation about functions.
>
> About the second, there errors when I try to execute.
>
> (defun typh-skip-chars (&optional n)
>    (interactive "n Skip direction: ")
>    (if (= n 1)
>       (skip-chars-forward "[:alpha:]")
>       (skip-chars-backward "[:alpha:]")) )
>
> (defun typh-word-markers (ma mb)
>    (interactive)
>    (skip-chars-backward "[:alpha:]")
>    (setq ma (point))
>    (skip-chars-forward "[:alpha:]")
>    (setq mb (point))
>    (message "[ma,mb]: %s,%s" ma mb) )
>
>
>
>


reply via email to

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