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

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

Re: Optional Arguments


From: pietru
Subject: Re: Optional Arguments
Date: Mon, 7 Dec 2020 20:42:59 +0100

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



reply via email to

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