* Possible to conditionally bind variable?
@ 2014-09-11 11:11 Tory S. Anderson
2014-09-11 12:24 ` Nicolas Richard
0 siblings, 1 reply; 8+ messages in thread
From: Tory S. Anderson @ 2014-09-11 11:11 UTC (permalink / raw)
To: emacs list
I have several redundancies in the following code:
(defun go-or-make-agenda (&optional new-frame)
(interactive "P")
(let ((buffer "\*Org Agenda\*"))
(if (get-buffer buffer)
(if new-frame
(switch-to-buffer-other-frame buffer)
(switch-to-buffer buffer))
(if new-frame
(switch-to-buffer-other-frame org-agenda-list)
(org-agenda-list)))))
I'd like to do something like the following, to chop out much of the switch-to-[other-]-buffer cruft:
(defun go-or-make-agenda (&optional new-frame)
(interactive "P")
(let ((buffer "\*Org Agenda\*")
(some-other-buffer "*scratch*")
(my-switch-function (if new-frame ;; is there some way to do this?
'(switch-to-other-buffer)
'(switch-to-buffer)))
(if (get-buffer buffer)
(my-switch-function buffer)
(my-switch-function some-other-buffer)))))
I'm not sure this is possible (short of the rather daunting http://stackoverflow.com/questions/25115876/conditional-variable-binding-in-common-lisp), but I have trust that in the awesomeness of Lisp it just might be.
- Tory
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Possible to conditionally bind variable?
2014-09-11 11:11 Possible to conditionally bind variable? Tory S. Anderson
@ 2014-09-11 12:24 ` Nicolas Richard
2014-09-11 12:34 ` Stefan Monnier
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Nicolas Richard @ 2014-09-11 12:24 UTC (permalink / raw)
To: Tory S. Anderson; +Cc: emacs list
torys.anderson@gmail.com (Tory S. Anderson) writes:
> (defun go-or-make-agenda (&optional new-frame)
> (interactive "P")
> (let ((buffer "\*Org Agenda\*")
> (some-other-buffer "*scratch*")
> (my-switch-function (if new-frame ;; is there some way to do this?
> '(switch-to-other-buffer)
> '(switch-to-buffer)))
> (if (get-buffer buffer)
> (my-switch-function buffer)
> (my-switch-function some-other-buffer)))))
untested :
(defun go-or-make-agenda (&optional new-frame)
(interactive "P")
(let ((buffer "\*Org Agenda\*")
(some-other-buffer "*scratch*")
(my-switch-function (if new-frame ;; is there some way to do this?
'switch-to-buffer-other-frame
'switch-to-buffer)))
(if (get-buffer buffer)
(funcall my-switch-function buffer)
(funcall my-switch-function some-other-buffer))))
HTH,
--
Nicolas Richard
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Possible to conditionally bind variable?
2014-09-11 12:24 ` Nicolas Richard
@ 2014-09-11 12:34 ` Stefan Monnier
2014-09-11 12:43 ` Tory S. Anderson
[not found] ` <mailman.8684.1410438907.1147.help-gnu-emacs@gnu.org>
2 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2014-09-11 12:34 UTC (permalink / raw)
To: help-gnu-emacs
> (defun go-or-make-agenda (&optional new-frame)
> (interactive "P")
> (let ((buffer "\*Org Agenda\*")
> (some-other-buffer "*scratch*")
> (my-switch-function (if new-frame ;; is there some way to do this?
> 'switch-to-buffer-other-frame
> 'switch-to-buffer)))
> (if (get-buffer buffer)
> (funcall my-switch-function buffer)
> (funcall my-switch-function some-other-buffer))))
Aka
(defun go-or-make-agenda (&optional new-frame)
(interactive "P")
(let ((buffer "\*Org Agenda\*")
(some-other-buffer "*scratch*")
(my-switch-function (if new-frame ;; is there some way to do this?
'switch-to-buffer-other-frame
'switch-to-buffer)))
(funcall my-switch-function
(if (get-buffer buffer) buffer some-other-buffer))))
Aka
(defun go-or-make-agenda (&optional new-frame)
(interactive "P")
(let ((buffer "\*Org Agenda\*")
(some-other-buffer "*scratch*"))
(funcall (if new-frame 'switch-to-buffer-other-frame 'switch-to-buffer)
(if (get-buffer buffer) buffer some-other-buffer))))
-- Stefan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Possible to conditionally bind variable?
2014-09-11 12:24 ` Nicolas Richard
2014-09-11 12:34 ` Stefan Monnier
@ 2014-09-11 12:43 ` Tory S. Anderson
2014-09-11 13:54 ` Nicolas Richard
[not found] ` <mailman.8690.1410443522.1147.help-gnu-emacs@gnu.org>
[not found] ` <mailman.8684.1410438907.1147.help-gnu-emacs@gnu.org>
2 siblings, 2 replies; 8+ messages in thread
From: Tory S. Anderson @ 2014-09-11 12:43 UTC (permalink / raw)
To: Nicolas Richard; +Cc: emacs list
Wow! It works!
So, I forgot the funcall my first time and it didn't work; it said "Symbol's function definition is void: my-switch-function". How do the funcalls fix it? I thought the first item in parens was automatically called as a function unless quoted?
Lisp FTW
Nicolas Richard <theonewiththeevillook@yahoo.fr> writes:
> torys.anderson@gmail.com (Tory S. Anderson) writes:
>> (defun go-or-make-agenda (&optional new-frame)
>> (interactive "P")
>> (let ((buffer "\*Org Agenda\*")
>> (some-other-buffer "*scratch*")
>> (my-switch-function (if new-frame ;; is there some way to do this?
>> '(switch-to-other-buffer)
>> '(switch-to-buffer)))
>> (if (get-buffer buffer)
>> (my-switch-function buffer)
>> (my-switch-function some-other-buffer)))))
>
> untested :
>
> (defun go-or-make-agenda (&optional new-frame)
> (interactive "P")
> (let ((buffer "\*Org Agenda\*")
> (some-other-buffer "*scratch*")
> (my-switch-function (if new-frame ;; is there some way to do this?
> 'switch-to-buffer-other-frame
> 'switch-to-buffer)))
> (if (get-buffer buffer)
> (funcall my-switch-function buffer)
> (funcall my-switch-function some-other-buffer))))
>
> HTH,
^ permalink raw reply [flat|nested] 8+ messages in thread
[parent not found: <mailman.8684.1410438907.1147.help-gnu-emacs@gnu.org>]
* Re: Possible to conditionally bind variable?
[not found] ` <mailman.8684.1410438907.1147.help-gnu-emacs@gnu.org>
@ 2014-09-12 3:55 ` Rusi
2014-09-12 3:59 ` Rusi
0 siblings, 1 reply; 8+ messages in thread
From: Rusi @ 2014-09-12 3:55 UTC (permalink / raw)
To: help-gnu-emacs
On Thursday, September 11, 2014 6:04:33 PM UTC+5:30, Stefan Monnier wrote:
> > (defun go-or-make-agenda (&optional new-frame)
> > (interactive "P")
> > (let ((buffer "\*Org Agenda\*")
> > (some-other-buffer "*scratch*")
> > (my-switch-function (if new-frame ;; is there some way to do this?
> > 'switch-to-buffer-other-frame
> > 'switch-to-buffer)))
> > (if (get-buffer buffer)
> > (funcall my-switch-function buffer)
> > (funcall my-switch-function some-other-buffer))))
> Aka
> (defun go-or-make-agenda (&optional new-frame)
> (interactive "P")
> (let ((buffer "\*Org Agenda\*")
> (some-other-buffer "*scratch*")
> (my-switch-function (if new-frame ;; is there some way to do this?
> 'switch-to-buffer-other-frame
> 'switch-to-buffer)))
> (funcall my-switch-function
> (if (get-buffer buffer) buffer some-other-buffer))))
> Aka
> (defun go-or-make-agenda (&optional new-frame)
> (interactive "P")
> (let ((buffer "\*Org Agenda\*")
> (some-other-buffer "*scratch*"))
> (funcall (if new-frame 'switch-to-buffer-other-frame 'switch-to-buffer)
> (if (get-buffer buffer) buffer some-other-buffer))))
Seeing this beautiful code, cant help making the remark:
This is called functional programming; guys like Stefan do it
guys like talk about it.
Here is some more talk -- what Stefan has demoed above is one of the
many cases outlined there:
http://blog.languager.org/2012/10/functional-programming-lost-booty.html
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Possible to conditionally bind variable?
2014-09-12 3:55 ` Rusi
@ 2014-09-12 3:59 ` Rusi
0 siblings, 0 replies; 8+ messages in thread
From: Rusi @ 2014-09-12 3:59 UTC (permalink / raw)
To: help-gnu-emacs
On Friday, September 12, 2014 9:25:46 AM UTC+5:30, Rusi wrote:
> On Thursday, September 11, 2014 6:04:33 PM UTC+5:30, Stefan Monnier wrote:
> > (defun go-or-make-agenda (&optional new-frame)
> > (interactive "P")
> > (let ((buffer "\*Org Agenda\*")
> > (some-other-buffer "*scratch*"))
> > (funcall (if new-frame 'switch-to-buffer-other-frame 'switch-to-buffer)
> > (if (get-buffer buffer) buffer some-other-buffer))))
> Seeing this beautiful code, cant help making the remark:
> This is called functional programming; guys like Stefan do it
> guys like talk about it.
that should have been "guys like me talk about it." :-)
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2014-09-12 3:59 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-11 11:11 Possible to conditionally bind variable? Tory S. Anderson
2014-09-11 12:24 ` Nicolas Richard
2014-09-11 12:34 ` Stefan Monnier
2014-09-11 12:43 ` Tory S. Anderson
2014-09-11 13:54 ` Nicolas Richard
[not found] ` <mailman.8690.1410443522.1147.help-gnu-emacs@gnu.org>
2014-09-12 2:56 ` Rusi
[not found] ` <mailman.8684.1410438907.1147.help-gnu-emacs@gnu.org>
2014-09-12 3:55 ` Rusi
2014-09-12 3:59 ` Rusi
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).