all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Optional Arguments
@ 2020-12-07  3:17 pietru
  2020-12-07  8:16 ` Alexis Roda
  0 siblings, 1 reply; 23+ messages in thread
From: pietru @ 2020-12-07  3:17 UTC (permalink / raw)
  To: Help Gnu Emacs

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) )





^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07  3:17 Optional Arguments pietru
@ 2020-12-07  8:16 ` Alexis Roda
  2020-12-07 12:37   ` pietru
  0 siblings, 1 reply; 23+ messages in thread
From: Alexis Roda @ 2020-12-07  8:16 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

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) )
>
>
>
>


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07  8:16 ` Alexis Roda
@ 2020-12-07 12:37   ` pietru
  2020-12-07 13:06     ` tomas
  0 siblings, 1 reply; 23+ messages in thread
From: pietru @ 2020-12-07 12:37 UTC (permalink / raw)
  To: Alexis Roda; +Cc: Help Gnu Emacs



> 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) )
> >
> >
> >
> >
>



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 12:37   ` pietru
@ 2020-12-07 13:06     ` tomas
  2020-12-07 13:35       ` Anders Dalskov
                         ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: tomas @ 2020-12-07 13:06 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

[-- Attachment #1: Type: text/plain, Size: 1775 bytes --]

On Mon, Dec 07, 2020 at 01:37:34PM +0100, pietru@caramail.com wrote:
> 
> 
> > 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)

[...]

As Alexis already said, the parameters in your function aren't probably
doing what you think they do. Consider:

  (setq ma 15)
  (setq mb 26)
  
  (defun foo (ma mb)
    (setq ma 5)
    (setq mb 6)
    (message "ma: %d mb: %d" ma mb))
  
  (foo ma mb)

=> "ma: 5 mb: 6"

  (message "ma: %d mb: %d" ma mb)

=> "ma: 15 mb: 26"

I suspect you are passing the parameters to get their values
"outside" the function. I can only guess that, because you don't
show any context (take this into account to help others help
you :)

But this won't work, because the (setq ...) whithin the function
doesn't "see" the variables outside, but creates variables inside,
initialized to whatever /values/ you pass to the function at call
time.

In other words: what would you expect your function `typh-word-markers'
do do if you call it like so:

  (typh-word-markers 15 16)

...would you expect it to change the number 15 to whatever the word's
beginning position is? You would mess up maths with that :)

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 13:06     ` tomas
@ 2020-12-07 13:35       ` Anders Dalskov
  2020-12-07 14:24       ` pietru
  2020-12-07 15:06       ` pietru
  2 siblings, 0 replies; 23+ messages in thread
From: Anders Dalskov @ 2020-12-07 13:35 UTC (permalink / raw)
  To: tomas, pietru; +Cc: Help Gnu Emacs

Doesn't `(bounds-of-thing-at-point 'word)` accomplish what you want?

On Mon, Dec 7, 2020, at 14:06, tomas@tuxteam.de wrote:
> On Mon, Dec 07, 2020 at 01:37:34PM +0100, pietru@caramail.com wrote:
> > 
> > 
> > > 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)
> 
> [...]
> 
> As Alexis already said, the parameters in your function aren't probably
> doing what you think they do. Consider:
> 
>   (setq ma 15)
>   (setq mb 26)
>   
>   (defun foo (ma mb)
>     (setq ma 5)
>     (setq mb 6)
>     (message "ma: %d mb: %d" ma mb))
>   
>   (foo ma mb)
> 
> => "ma: 5 mb: 6"
> 
>   (message "ma: %d mb: %d" ma mb)
> 
> => "ma: 15 mb: 26"
> 
> I suspect you are passing the parameters to get their values
> "outside" the function. I can only guess that, because you don't
> show any context (take this into account to help others help
> you :)
> 
> But this won't work, because the (setq ...) whithin the function
> doesn't "see" the variables outside, but creates variables inside,
> initialized to whatever /values/ you pass to the function at call
> time.
> 
> In other words: what would you expect your function `typh-word-markers'
> do do if you call it like so:
> 
>   (typh-word-markers 15 16)
> 
> ...would you expect it to change the number 15 to whatever the word's
> beginning position is? You would mess up maths with that :)
> 
> Cheers
>  - t
> 
> Attachments:
> * signature.asc

-- 
Anders



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  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 15:06       ` pietru
  2 siblings, 1 reply; 23+ messages in thread
From: pietru @ 2020-12-07 14:24 UTC (permalink / raw)
  To: tomas; +Cc: Help Gnu Emacs



> Sent: Monday, December 07, 2020 at 2:06 PM
> From: tomas@tuxteam.de
> To: pietru@caramail.com
> Cc: "Alexis Roda" <alexis.roda.villalonga@gmail.com>, "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Optional Arguments
>
> On Mon, Dec 07, 2020 at 01:37:34PM +0100, pietru@caramail.com wrote:
> >
> >
> > > 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)
>
> [...]
>
> As Alexis already said, the parameters in your function aren't probably
> doing what you think they do. Consider:
>
>   (setq ma 15)
>   (setq mb 26)
>
>   (defun foo (ma mb)
>     (setq ma 5)
>     (setq mb 6)
>     (message "ma: %d mb: %d" ma mb))
>
>   (foo ma mb)
>
> => "ma: 5 mb: 6"
>
>   (message "ma: %d mb: %d" ma mb)
>
> => "ma: 15 mb: 26"

Hmmm. Variables local to function.

> I suspect you are passing the parameters to get their values
> "outside" the function. I can only guess that, because you don't
> show any context (take this into account to help others help
> you :)

Correct

> But this won't work, because the (setq ...) whithin the function
> doesn't "see" the variables outside, but creates variables inside,
> initialized to whatever /values/ you pass to the function at call
> time.
>
> In other words: what would you expect your function `typh-word-markers'
> do do if you call it like so:
>
>   (typh-word-markers 15 16)
>
> ...would you expect it to change the number 15 to whatever the word's
> beginning position is? You would mess up maths with that :)

Yes, it would mess up.

> Cheers
>  - t
>



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 13:06     ` tomas
  2020-12-07 13:35       ` Anders Dalskov
  2020-12-07 14:24       ` pietru
@ 2020-12-07 15:06       ` pietru
  2020-12-07 15:51         ` tomas
  2 siblings, 1 reply; 23+ messages in thread
From: pietru @ 2020-12-07 15:06 UTC (permalink / raw)
  To: tomas; +Cc: Help Gnu Emacs



> Sent: Monday, December 07, 2020 at 2:06 PM
> From: tomas@tuxteam.de
> To: pietru@caramail.com
> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Optional Arguments
>
> On Mon, Dec 07, 2020 at 01:37:34PM +0100, pietru@caramail.com wrote:
> >
> >
> > > 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)
>
> [...]
>
> As Alexis already said, the parameters in your function aren't probably
> doing what you think they do. Consider:
>
>   (setq ma 15)
>   (setq mb 26)
>
>   (defun foo (ma mb)
>     (setq ma 5)
>     (setq mb 6)
>     (message "ma: %d mb: %d" ma mb))
>
>   (foo ma mb)
>
> => "ma: 5 mb: 6"
>
>   (message "ma: %d mb: %d" ma mb)
>
> => "ma: 15 mb: 26"
>
> I suspect you are passing the parameters to get their values
> "outside" the function. I can only guess that, because you don't
> show any context (take this into account to help others help
> you :)
>
> But this won't work, because the (setq ...) whithin the function
> doesn't "see" the variables outside, but creates variables inside,
> initialized to whatever /values/ you pass to the function at call
> time.
>
> In other words: what would you expect your function `typh-word-markers'
> do do if you call it like so:
>
>   (typh-word-markers 15 16)
>
> ...would you expect it to change the number 15 to whatever the word's
> beginning position is? You would mess up maths with that :)

How can one get computed values from a function then?

> Cheers
>  - t
>



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 14:24       ` pietru
@ 2020-12-07 15:38         ` tomas
  2020-12-07 18:13           ` pietru
  0 siblings, 1 reply; 23+ messages in thread
From: tomas @ 2020-12-07 15:38 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

[-- Attachment #1: Type: text/plain, Size: 823 bytes --]

On Mon, Dec 07, 2020 at 03:24:36PM +0100, pietru@caramail.com wrote:

[...]

> > => "ma: 15 mb: 26"
> 
> Hmmm. Variables local to function.

Yes. "Local" meaning here either dynamical extent (called functions
"see" the variables up the call chain, think Unix shells) or
lexical extent (code contexts "see" the variables of enclosing
code contexts (think C or Java or...), depending on whether you
chose lexical binding [1].

[...]

> > ...would you expect it to change the number 15 to whatever the word's
> > beginning position is? You would mess up maths with that :)
> 
> Yes, it would mess up.

Gödel's nothing against that :-D

Cheers

[1] Cf. Chapter "Lexical Binding" in the Elisp manual, or here
    https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html

 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 15:06       ` pietru
@ 2020-12-07 15:51         ` tomas
  2020-12-07 17:51           ` pietru
  0 siblings, 1 reply; 23+ messages in thread
From: tomas @ 2020-12-07 15:51 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

[-- Attachment #1: Type: text/plain, Size: 973 bytes --]

On Mon, Dec 07, 2020 at 04:06:13PM +0100, pietru@caramail.com wrote:

[...]

> How can one get computed values from a function then?

Either you return it (in your case, e.g. by returning a pair)

  (cons ma mb)

or (less preferable) by modifying variables "outside" of the
function. You have to take carefully into account what kind
of binding the program runs under, or to use "global" (in
the sense of program scope: buffer-local variables count as
global here) variables, and you have a very good Petri dish
for all sort of nasty bugs ;-)

Under lexical binding, this, for example, would do:

  (let ((ma 0)
        (mb 0))
  
    (defun foo ()
      (setq ma 5)
      (setq mb 7))
  
    (foo)
    (message "ma: %d mb: %d" ma mb))

(the function `foo' "sees" the variables `ma', `mb' set up in
the enclosing scope). Only recommended for small, tightly knit
snippets: the (human) reader should be able to "see" that too.

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 15:51         ` tomas
@ 2020-12-07 17:51           ` pietru
  2020-12-07 18:33             ` Arthur Miller
  2020-12-07 20:13             ` Stefan Monnier
  0 siblings, 2 replies; 23+ messages in thread
From: pietru @ 2020-12-07 17:51 UTC (permalink / raw)
  To: tomas; +Cc: Help Gnu Emacs



> Sent: Monday, December 07, 2020 at 4:51 PM
> From: tomas@tuxteam.de
> To: pietru@caramail.com
> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Optional Arguments
>
> On Mon, Dec 07, 2020 at 04:06:13PM +0100, pietru@caramail.com wrote:
>
> [...]
>
> > How can one get computed values from a function then?
>
> Either you return it (in your case, e.g. by returning a pair)
>
>   (cons ma mb)

Because by default, a function returns the value of the last expression
evaluated as the return value.  Correct?

> or (less preferable) by modifying variables "outside" of the
> function. You have to take carefully into account what kind
> of binding the program runs under, or to use "global" (in
> the sense of program scope: buffer-local variables count as
> global here) variables, and you have a very good Petri dish
> for all sort of nasty bugs ;-)
>
> Under lexical binding, this, for example, would do:
>
>   (let ((ma 0)
>         (mb 0))
>
>     (defun foo ()
>       (setq ma 5)
>       (setq mb 7))
>
>     (foo)
>     (message "ma: %d mb: %d" ma mb))
>
> (the function `foo' "sees" the variables `ma', `mb' set up in
> the enclosing scope). Only recommended for small, tightly knit
> snippets: the (human) reader should be able to "see" that too.
>
> Cheers
>  - t
>



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 15:38         ` tomas
@ 2020-12-07 18:13           ` pietru
  2020-12-07 19:01             ` Arthur Miller
  2020-12-07 19:51             ` Alexis Roda
  0 siblings, 2 replies; 23+ messages in thread
From: pietru @ 2020-12-07 18:13 UTC (permalink / raw)
  To: tomas; +Cc: Help Gnu Emacs

I'm having a go at returning the two values from a function

(defun word-markers ()
  (let ((ma mb))
    (skip-chars-backward "[:alpha:]")
    (setq ma (point))
    (skip-chars-forward "[:alpha:]")
    (setq mb (point))
    (cons ma mb) ))

(defun test ()
   (interactive)

   (let ((deactivate-mark nil) bounds $ma $mb)
      (if (use-region-p)
         (setq $ma (region-beginning) $mb (region-end))
         (save-excursion
            (setq bounds (word-markers))
            (setq $mu (car bounds))
            (setq $mv (car bounds)) ))

      (message "Bounds: %s" $bounds)
      (message "Region: [%s, %s]" $ma $mb)  ))

But something's not right.


> Sent: Monday, December 07, 2020 at 4:38 PM
> From: tomas@tuxteam.de
> To: pietru@caramail.com
> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Optional Arguments
>
> On Mon, Dec 07, 2020 at 03:24:36PM +0100, pietru@caramail.com wrote:
> 
> [...]
> 
> > > => "ma: 15 mb: 26"
> > 
> > Hmmm. Variables local to function.
> 
> Yes. "Local" meaning here either dynamical extent (called functions
> "see" the variables up the call chain, think Unix shells) or
> lexical extent (code contexts "see" the variables of enclosing
> code contexts (think C or Java or...), depending on whether you
> chose lexical binding [1].
> 
> [...]
> 
> > > ...would you expect it to change the number 15 to whatever the word's
> > > beginning position is? You would mess up maths with that :)
> > 
> > Yes, it would mess up.
> 
> Gödel's nothing against that :-D
> 
> Cheers
> 
> [1] Cf. Chapter "Lexical Binding" in the Elisp manual, or here
>     https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html
> 
>  - t
>



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  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
  1 sibling, 1 reply; 23+ messages in thread
From: Arthur Miller @ 2020-12-07 18:33 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

pietru@caramail.com writes:

>> Sent: Monday, December 07, 2020 at 4:51 PM
>> From: tomas@tuxteam.de
>> To: pietru@caramail.com
>> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
>> Subject: Re: Optional Arguments
>>
>> On Mon, Dec 07, 2020 at 04:06:13PM +0100, pietru@caramail.com wrote:
>>
>> [...]
>>
>> > How can one get computed values from a function then?
>>
>> Either you return it (in your case, e.g. by returning a pair)
>>
>>   (cons ma mb)
>
> Because by default, a function returns the value of the last expression
> evaluated as the return value.  Correct?
Yes.

You probably wish to prefer to return values instead of using globals as
Thomas already pointed out.



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 18:33             ` Arthur Miller
@ 2020-12-07 18:49               ` pietru
  0 siblings, 0 replies; 23+ messages in thread
From: pietru @ 2020-12-07 18:49 UTC (permalink / raw)
  To: Arthur Miller; +Cc: Help Gnu Emacs



> Sent: Monday, December 07, 2020 at 7:33 PM
> From: "Arthur Miller" <arthur.miller@live.com>
> To: pietru@caramail.com
> Cc: tomas@tuxteam.de, "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Optional Arguments
>
> pietru@caramail.com writes:
>
> >> Sent: Monday, December 07, 2020 at 4:51 PM
> >> From: tomas@tuxteam.de
> >> To: pietru@caramail.com
> >> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> >> Subject: Re: Optional Arguments
> >>
> >> On Mon, Dec 07, 2020 at 04:06:13PM +0100, pietru@caramail.com wrote:
> >>
> >> [...]
> >>
> >> > How can one get computed values from a function then?
> >>
> >> Either you return it (in your case, e.g. by returning a pair)
> >>
> >>   (cons ma mb)
> >
> > Because by default, a function returns the value of the last expression
> > evaluated as the return value.  Correct?
> Yes.
>
> You probably wish to prefer to return values instead of using globals as
> Thomas already pointed out.

Quite right.  Still, this is all new for me from the code side of things.



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 18:13           ` pietru
@ 2020-12-07 19:01             ` Arthur Miller
  2020-12-07 19:42               ` pietru
  2020-12-07 19:51             ` Alexis Roda
  1 sibling, 1 reply; 23+ messages in thread
From: Arthur Miller @ 2020-12-07 19:01 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

pietru@caramail.com writes:

> I'm having a go at returning the two values from a function
>
> (defun word-markers ()
>   (let ((ma mb))
>     (skip-chars-backward "[:alpha:]")
>     (setq ma (point))
>     (skip-chars-forward "[:alpha:]")
>     (setq mb (point))
>     (cons ma mb) ))
>
> (defun test ()
>    (interactive)
>
>    (let ((deactivate-mark nil) bounds $ma $mb)
>       (if (use-region-p)
>          (setq $ma (region-beginning) $mb (region-end))
>          (save-excursion
>             (setq bounds (word-markers))
>             (setq $mu (car bounds))
>             (setq $mv (car bounds)) ))
>
>       (message "Bounds: %s" $bounds)
>       (message "Region: [%s, %s]" $ma $mb)  ))
>
> But something's not right.

You have probably made a typo when you are setq-ing 'mu' and 'mv'

> (setq $mu (car bounds)) <-- introducing new variable in global scope: $mu
> (setq $mv (car bounds)) <-- same here: $mv

> (message "Region: [%s, %s]" $ma $mb) <-- potentially nil vars $ma and $mb

You probably want somthing like this:

(defun test ()
   (interactive)
   (let ((deactivate-mark nil)
         bounds ma mb)
     (if (use-region-p)
         (setq ma (region-beginning) mb (region-end))
       (save-excursion
         (setq bounds (word-markers))
         (setq ma (car bounds))
         (setq mb (cdr bounds))))
   (message "Bounds: %s" (pp bounds))
   (message "Region: [%s %s]" ma mb)
   (message "Region string: [%s]" (buffer-substring ma mb))))



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 19:01             ` Arthur Miller
@ 2020-12-07 19:42               ` pietru
  2020-12-07 19:54                 ` Michael Heerdegen
  0 siblings, 1 reply; 23+ messages in thread
From: pietru @ 2020-12-07 19:42 UTC (permalink / raw)
  To: Arthur Miller; +Cc: Help Gnu Emacs

Am getting an error with the following.

(defun word-markers ()
   (let ((ma mb))
      ;;(message "s: %s" (cdr (bounds-of-thing-at-point 'word)))
      (skip-chars-backward "[:alpha:]")
      (setq ma (point))
      (skip-chars-forward "[:alpha:]")
      (setq mb (point))
      (cons ma mb) ))

Debugger entered--Lisp error: (void-variable mb)
  (let ((ma mb)) (skip-chars-backward "[:alpha:]") (setq ma (point)) (skip-chars-forward "[:alpha:]") (setq mb (point)) (cons ma mb))
  word-markers()

> Sent: Monday, December 07, 2020 at 8:01 PM
> From: "Arthur Miller" <arthur.miller@live.com>
> To: pietru@caramail.com
> Cc: tomas@tuxteam.de, "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Optional Arguments
>
> pietru@caramail.com writes:
>
> > I'm having a go at returning the two values from a function
> >
> > (defun word-markers ()
> >   (let ((ma mb))
> >     (skip-chars-backward "[:alpha:]")
> >     (setq ma (point))
> >     (skip-chars-forward "[:alpha:]")
> >     (setq mb (point))
> >     (cons ma mb) ))
> >
> > (defun test ()
> >    (interactive)
> >
> >    (let ((deactivate-mark nil) bounds $ma $mb)
> >       (if (use-region-p)
> >          (setq $ma (region-beginning) $mb (region-end))
> >          (save-excursion
> >             (setq bounds (word-markers))
> >             (setq $mu (car bounds))
> >             (setq $mv (car bounds)) ))
> >
> >       (message "Bounds: %s" $bounds)
> >       (message "Region: [%s, %s]" $ma $mb)  ))
> >
> > But something's not right.
>
> You have probably made a typo when you are setq-ing 'mu' and 'mv'
>
> > (setq $mu (car bounds)) <-- introducing new variable in global scope: $mu
> > (setq $mv (car bounds)) <-- same here: $mv
>
> > (message "Region: [%s, %s]" $ma $mb) <-- potentially nil vars $ma and $mb
>
> You probably want somthing like this:
>
> (defun test ()
>    (interactive)
>    (let ((deactivate-mark nil)
>          bounds ma mb)
>      (if (use-region-p)
>          (setq ma (region-beginning) mb (region-end))
>        (save-excursion
>          (setq bounds (word-markers))
>          (setq ma (car bounds))
>          (setq mb (cdr bounds))))
>    (message "Bounds: %s" (pp bounds))
>    (message "Region: [%s %s]" ma mb)
>    (message "Region string: [%s]" (buffer-substring ma mb))))
>



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 18:13           ` pietru
  2020-12-07 19:01             ` Arthur Miller
@ 2020-12-07 19:51             ` Alexis Roda
  1 sibling, 0 replies; 23+ messages in thread
From: Alexis Roda @ 2020-12-07 19:51 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

https://www.gnu.org/software/emacs/manual/html_node/eintr/Parts-of-let-Expression.html

According to that, your let expression defines a variable called 'ma'
initialized with the value given by 'mb', which is not defined at that
point.

Try with:

(defun word-markers ()
  (let ((ma nil)
        (mb nil))
    (skip-chars-backward "[:alpha:]")
    (setq ma (point))
    (skip-chars-forward "[:alpha:]")
    (setq mb (point))
    (cons ma mb)))

or

(defun word-markers ()
  (let (ma
        mb)
    (skip-chars-backward "[:alpha:]")
    (setq ma (point))
    (skip-chars-forward "[:alpha:]")
    (setq mb (point))
    (cons ma mb)))

In the second example 'ma' and 'mb' are initialized implicitly with nil.

Also variables '$mu' and '$mv' aren't defined.

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

> I'm having a go at returning the two values from a function
>
> (defun word-markers ()
>   (let ((ma mb))
>     (skip-chars-backward "[:alpha:]")
>     (setq ma (point))
>     (skip-chars-forward "[:alpha:]")
>     (setq mb (point))
>     (cons ma mb) ))
>
> (defun test ()
>    (interactive)
>
>    (let ((deactivate-mark nil) bounds $ma $mb)
>       (if (use-region-p)
>          (setq $ma (region-beginning) $mb (region-end))
>          (save-excursion
>             (setq bounds (word-markers))
>             (setq $mu (car bounds))
>             (setq $mv (car bounds)) ))
>
>       (message "Bounds: %s" $bounds)
>       (message "Region: [%s, %s]" $ma $mb)  ))
>
> But something's not right.
>
>
> > Sent: Monday, December 07, 2020 at 4:38 PM
> > From: tomas@tuxteam.de
> > To: pietru@caramail.com
> > Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> > Subject: Re: Optional Arguments
> >
> > On Mon, Dec 07, 2020 at 03:24:36PM +0100, pietru@caramail.com wrote:
> >
> > [...]
> >
> > > > => "ma: 15 mb: 26"
> > >
> > > Hmmm. Variables local to function.
> >
> > Yes. "Local" meaning here either dynamical extent (called functions
> > "see" the variables up the call chain, think Unix shells) or
> > lexical extent (code contexts "see" the variables of enclosing
> > code contexts (think C or Java or...), depending on whether you
> > chose lexical binding [1].
> >
> > [...]
> >
> > > > ...would you expect it to change the number 15 to whatever the word's
> > > > beginning position is? You would mess up maths with that :)
> > >
> > > Yes, it would mess up.
> >
> > Gödel's nothing against that :-D
> >
> > Cheers
> >
> > [1] Cf. Chapter "Lexical Binding" in the Elisp manual, or here
> >
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html
> >
> >  - t
> >
>
>


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  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
  0 siblings, 2 replies; 23+ messages in thread
From: Michael Heerdegen @ 2020-12-07 19:54 UTC (permalink / raw)
  To: help-gnu-emacs

pietru@caramail.com writes:

> Am getting an error with the following.
>
> (defun word-markers ()
>    (let ((ma mb)) <--

You bind `ma' to the value of `mb'.  That's not what you want.

Michael.




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 17:51           ` pietru
  2020-12-07 18:33             ` Arthur Miller
@ 2020-12-07 20:13             ` Stefan Monnier
  2020-12-07 20:25               ` pietru
  1 sibling, 1 reply; 23+ messages in thread
From: Stefan Monnier @ 2020-12-07 20:13 UTC (permalink / raw)
  To: help-gnu-emacs

>> Either you return it (in your case, e.g. by returning a pair)
>>
>>   (cons ma mb)
>
> Because by default, a function returns the value of the last expression
> evaluated as the return value.  Correct?

Almost: to be correct you'd have to remove "by default, ".


        Stefan




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 19:54                 ` Michael Heerdegen
@ 2020-12-07 20:21                   ` pietru
  2020-12-07 20:52                   ` Arthur Miller
  1 sibling, 0 replies; 23+ messages in thread
From: pietru @ 2020-12-07 20:21 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs


> Sent: Monday, December 07, 2020 at 8:54 PM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Optional Arguments
>
> pietru@caramail.com writes:
>
> > Am getting an error with the following.
> >
> > (defun word-markers ()
> >    (let ((ma mb)) <--
>
> You bind `ma' to the value of `mb'.  That's not what you want.

I got on the manual, only when variables stand alone and not surrounded by parentheses,
are being bound to nil.

> Michael.
>
>
>



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 20:13             ` Stefan Monnier
@ 2020-12-07 20:25               ` pietru
  2020-12-07 20:39                 ` Christopher Dimech
  0 siblings, 1 reply; 23+ messages in thread
From: pietru @ 2020-12-07 20:25 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


> Sent: Monday, December 07, 2020 at 9:13 PM
> From: "Stefan Monnier" <monnier@iro.umontreal.ca>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Optional Arguments
>
> >> Either you return it (in your case, e.g. by returning a pair)
> >>
> >>   (cons ma mb)
> >
> > Because by default, a function returns the value of the last expression
> > evaluated as the return value.  Correct?
>
> Almost: to be correct you'd have to remove "by default, ".

Not by default, always.

>
>         Stefan
>
>
>



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 20:25               ` pietru
@ 2020-12-07 20:39                 ` Christopher Dimech
  0 siblings, 0 replies; 23+ messages in thread
From: Christopher Dimech @ 2020-12-07 20:39 UTC (permalink / raw)
  To: pietru; +Cc: help-gnu-emacs, Stefan Monnier

> Sent: Monday, December 07, 2020 at 9:25 PM
> From: pietru@caramail.com
> To: "Stefan Monnier" <monnier@iro.umontreal.ca>
> Cc: help-gnu-emacs@gnu.org
> Subject: Re: Optional Arguments
>
>
> > Sent: Monday, December 07, 2020 at 9:13 PM
> > From: "Stefan Monnier" <monnier@iro.umontreal.ca>
> > To: help-gnu-emacs@gnu.org
> > Subject: Re: Optional Arguments
> >
> > >> Either you return it (in your case, e.g. by returning a pair)
> > >>
> > >>   (cons ma mb)
> > >
> > > Because by default, a function returns the value of the last expression
> > > evaluated as the return value.  Correct?
> >
> > Almost: to be correct you'd have to remove "by default, ".
>
> Not by default, always.

Wah-wah, wee-wah.  Correct.

> >
> >         Stefan
> >
> >
> >
>
>



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  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
  1 sibling, 1 reply; 23+ messages in thread
From: Arthur Miller @ 2020-12-07 20:52 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

Michael Heerdegen <michael_heerdegen@web.de> writes:

> pietru@caramail.com writes:
>
>> Am getting an error with the following.
>>
>> (defun word-markers ()
>>    (let ((ma mb)) <--
>
> You bind `ma' to the value of `mb'.  That's not what you want.
Exactly; this is what I had in my scratch when I tested: I just didn't
copy everything in previous answer:

(defun word-markers ()
  (let (ma mb)
    (skip-chars-backward "[:alpha:]")
    (setq ma (point))
    (skip-chars-forward "[:alpha:]")
    (setq mb (point))
    (cons ma mb)))

(defun test ()
   (interactive)
   (let ((deactivate-mark nil)
         bounds ma mb)
     (if (use-region-p)
         (setq ma (region-beginning) mb (region-end))
       (save-excursion
         (setq bounds (word-markers))
         (setq ma (car bounds))
         (setq mb (cdr bounds))))
   (message "Bounds: %s" (pp bounds))
   (message "Region: [%s %s]" ma mb)
   (message "Region-string: [%s]" (buffer-substring-no-properties ma mb))))

(test)

Tou should drop one parenthesis around ma and mb in your let
expression. That will declare two variables and initialize them to nil both.



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Optional Arguments
  2020-12-07 20:52                   ` Arthur Miller
@ 2020-12-07 21:21                     ` pietru
  0 siblings, 0 replies; 23+ messages in thread
From: pietru @ 2020-12-07 21:21 UTC (permalink / raw)
  To: Arthur Miller; +Cc: Michael Heerdegen, help-gnu-emacs

> Sent: Monday, December 07, 2020 at 9:52 PM
> From: "Arthur Miller" <arthur.miller@live.com>
> To: "Michael Heerdegen" <michael_heerdegen@web.de>
> Cc: help-gnu-emacs@gnu.org
> Subject: Re: Optional Arguments
>
> Michael Heerdegen <michael_heerdegen@web.de> writes:
>
> > pietru@caramail.com writes:
> >
> >> Am getting an error with the following.
> >>
> >> (defun word-markers ()
> >>    (let ((ma mb)) <--

I see it now, the parenthesis was assigning to ma other than nil.
Thank you so very much.

> > You bind `ma' to the value of `mb'.  That's not what you want.
> Exactly; this is what I had in my scratch when I tested: I just didn't
> copy everything in previous answer:
>
> (defun word-markers ()
>   (let (ma mb)
>     (skip-chars-backward "[:alpha:]")
>     (setq ma (point))
>     (skip-chars-forward "[:alpha:]")
>     (setq mb (point))
>     (cons ma mb)))
>
> (defun test ()
>    (interactive)
>    (let ((deactivate-mark nil)
>          bounds ma mb)
>      (if (use-region-p)
>          (setq ma (region-beginning) mb (region-end))
>        (save-excursion
>          (setq bounds (word-markers))
>          (setq ma (car bounds))
>          (setq mb (cdr bounds))))
>    (message "Bounds: %s" (pp bounds))
>    (message "Region: [%s %s]" ma mb)
>    (message "Region-string: [%s]" (buffer-substring-no-properties ma mb))))
>
> (test)
>
> Tou should drop one parenthesis around ma and mb in your let
> expression. That will declare two variables and initialize them to nil both.
>
>



^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2020-12-07 21:21 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-07  3:17 Optional Arguments pietru
2020-12-07  8:16 ` Alexis Roda
2020-12-07 12:37   ` pietru
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

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.