13

I'd like to do

(destructuring-bind (start end) (bounds-of-thing-at-point 'symbol))

But bounds-of-thing-at-point returns a cons cell and not a list, so destructuring-bind doesn't work. What could work for this case?

ocodo
  • 29,401
  • 18
  • 105
  • 117
abo-abo
  • 20,038
  • 3
  • 50
  • 71
  • 2
    `(destructuring-bind (start . end) (cons 'x 'y) (message "%s %s"))` works for me. What version of Emacs is that? –  Jul 18 '13 at 10:20
  • Works perfectly, @wvxvw. Didn't think of using the dot. But the docstring wasn't very informative. Can you post the answer so that I can accept? – abo-abo Jul 18 '13 at 11:07

4 Answers4

25

Since destructuring-bind is a macro from cl package, it may be worthwhile to look into Common Lisp documentation for more examples.

This page shows the syntax of the macro. Note the (wholevar reqvars optvars . var). Though I'm not sure cl version of destructuring-bind actually supports all of the less common cases (many keywords only make sense when used with Common Lisp macros / functions, but don't have that meaning in Emacs Lisp).

Thus:

(destructuring-bind (start . end) (bounds-of-thing-at-point 'symbol) ;...)

should work.

9

I'd use

(pcase-let ((`(,start . ,end) (bounds-of-thing-at-point 'symbol)))
  ...)
Stefan
  • 27,908
  • 4
  • 53
  • 82
3

I cannot think on anything as elegant as destructuring-bind, but this would work:

(let* ((b (bounds-of-thing-at-point 'symbol))
       (start (car b))
       (end   (cdr b)))
  ...)
juanleon
  • 9,220
  • 30
  • 41
  • You've stored the bounds to `b`, then tried to access them from `x`. – Tyler Jul 18 '13 at 13:19
  • 1
    Fixed! Thanks for pointing it out (anyway, wvxvw answer is better, as it specifically addresses what abo-abo was trying to do) – juanleon Jul 18 '13 at 13:25
1

Nowadays we can use -let in dash.el. It is like pcase-let, just a bit cleaner:

(-let (((start . end) (bounds-of-thing-at-point 'symbol)))
  ...)
JJPandari
  • 3,454
  • 1
  • 17
  • 24