unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: pietru@caramail.com
To: Alexis Roda <alexis.roda.villalonga@gmail.com>
Cc: Help Gnu Emacs <help-gnu-emacs@gnu.org>
Subject: Re: Optional Arguments
Date: Mon, 7 Dec 2020 13:37:34 +0100	[thread overview]
Message-ID: <trinity-c2aa36dd-59c5-4cbf-a06e-9642899c6c12-1607344654425@3c-app-mailcom-bs11> (raw)
In-Reply-To: <CAMDYoXbgcDOamURBRCAF4LAtyd=Ey9m-+tXdX0z-=BC4vco1Lg@mail.gmail.com>



> Sent: Monday, December 07, 2020 at 9:16 AM
> From: "Alexis Roda" <alexis.roda.villalonga@gmail.com>
> To: pietru@caramail.com
> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Optional Arguments
>
> Hi,
> 
> Not sure what your question is.

It is bit confusing.

I want to use typh-word-markers to compute ma and mb so I can use the
two values in another function.  Had put (interactive) in my effort to
test it, but created problems as you say.  The function would be called
when cursor is on a word, so that I get the word "beg" and "end".

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


 
> 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	other threads:[~2020-12-07 12:37 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-07  3:17 Optional Arguments pietru
2020-12-07  8:16 ` Alexis Roda
2020-12-07 12:37   ` pietru [this message]
2020-12-07 13:06     ` tomas
2020-12-07 13:35       ` Anders Dalskov
2020-12-07 14:24       ` pietru
2020-12-07 15:38         ` tomas
2020-12-07 18:13           ` pietru
2020-12-07 19:01             ` Arthur Miller
2020-12-07 19:42               ` pietru
2020-12-07 19:54                 ` Michael Heerdegen
2020-12-07 20:21                   ` pietru
2020-12-07 20:52                   ` Arthur Miller
2020-12-07 21:21                     ` pietru
2020-12-07 19:51             ` Alexis Roda
2020-12-07 15:06       ` pietru
2020-12-07 15:51         ` tomas
2020-12-07 17:51           ` pietru
2020-12-07 18:33             ` Arthur Miller
2020-12-07 18:49               ` pietru
2020-12-07 20:13             ` Stefan Monnier
2020-12-07 20:25               ` pietru
2020-12-07 20:39                 ` Christopher Dimech

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=trinity-c2aa36dd-59c5-4cbf-a06e-9642899c6c12-1607344654425@3c-app-mailcom-bs11 \
    --to=pietru@caramail.com \
    --cc=alexis.roda.villalonga@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).