emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Can this function be written better?
@ 2010-12-06 18:29 Nathan Neff
  2010-12-06 21:53 ` Bernt Hansen
  0 siblings, 1 reply; 3+ messages in thread
From: Nathan Neff @ 2010-12-06 18:29 UTC (permalink / raw)
  To: emacs-orgmode

I'd like to be able to easily toggle the showing/hiding of CLOSED clock
items in the agenda.

I have a function that does exactly that.  My Lisp is terrible (I bet that's
never been said before :-) and I want to try to improve it.

Any suggestions how to improve/refactor the following function?

(defun njn/agenda-toggle-show-closed()
  "Toggle whether closed clock thingies are shown in the agenda"
  (interactive)
  (if (eq njn/org-agenda-show-closed 't)
      (progn (setq org-agenda-log-mode-items (quote (clock)))
	     (setq njn/org-agenda-show-closed nil)
	     (message "NOT Showing closed clock entries in agenda"))
    (progn (setq org-agenda-log-mode-items (quote (closed clock)))
	   (setq njn/org-agenda-show-closed 't)
	   (message "Showing closed clock entries in agenda"))
    ))

Thanks,
--Nate

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

* Re: Can this function be written better?
  2010-12-06 18:29 Can this function be written better? Nathan Neff
@ 2010-12-06 21:53 ` Bernt Hansen
  2010-12-07  3:00   ` Nathan Neff
  0 siblings, 1 reply; 3+ messages in thread
From: Bernt Hansen @ 2010-12-06 21:53 UTC (permalink / raw)
  To: Nathan Neff; +Cc: emacs-orgmode

Nathan Neff <nathan.neff@gmail.com> writes:

> I'd like to be able to easily toggle the showing/hiding of CLOSED clock
> items in the agenda.
>
> I have a function that does exactly that.  My Lisp is terrible (I bet that's
> never been said before :-) and I want to try to improve it.
>
> Any suggestions how to improve/refactor the following function?
>
> (defun njn/agenda-toggle-show-closed()
>   "Toggle whether closed clock thingies are shown in the agenda"
>   (interactive)
>   (if (eq njn/org-agenda-show-closed 't)
>       (progn (setq org-agenda-log-mode-items (quote (clock)))
> 	     (setq njn/org-agenda-show-closed nil)
> 	     (message "NOT Showing closed clock entries in agenda"))
>     (progn (setq org-agenda-log-mode-items (quote (closed clock)))
> 	   (setq njn/org-agenda-show-closed 't)
> 	   (message "Showing closed clock entries in agenda"))
>     ))
>

Hi Nate,

I would probably write it like this: (but I'm no emacs-lisp expert
either)

(defun njn/agenda-toggle-show-closed()
  "Toggle whether closed clock thingies are shown in the agenda"
  (interactive)
  (setq njn/org-agenda-show-closed (not njn/org-agenda-show-closed))
  (setq org-agenda-log-mode-items (if njn/org-agenda-show-closed
				      (quote (closed clock))
				    (quote (clock))))
  (message "%sShowing closed clock entries in agenda" (if njn/org-agenda-show-closed "" "NOT ")))

You have a bug in your version - the test and report is backwards.
When njn/org-agenda-show-closed is t you don't show closed items and
when it's nil you do.

- You don't need to check for equality with 't (and you don't need to
  quote t)
  - everything non-nil is true
  - you can just check that directly in the (if ... )

My version basically does this:
  1. toggle the boolean njn/org-agenda-shot-closed
  2. set org-agenda-log-mode-items based on the boolean
  3. report the value

HTH,
Bernt

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

* Re: Can this function be written better?
  2010-12-06 21:53 ` Bernt Hansen
@ 2010-12-07  3:00   ` Nathan Neff
  0 siblings, 0 replies; 3+ messages in thread
From: Nathan Neff @ 2010-12-07  3:00 UTC (permalink / raw)
  To: Bernt Hansen; +Cc: emacs-orgmode

> Hi Nate,
>
> I would probably write it like this: (but I'm no emacs-lisp expert
> either)
>
> (defun njn/agenda-toggle-show-closed()
>  "Toggle whether closed clock thingies are shown in the agenda"
>  (interactive)
>  (setq njn/org-agenda-show-closed (not njn/org-agenda-show-closed))
>  (setq org-agenda-log-mode-items (if njn/org-agenda-show-closed
>                                      (quote (closed clock))
>                                    (quote (clock))))
>  (message "%sShowing closed clock entries in agenda" (if njn/org-agenda-show-closed "" "NOT ")))
>
> You have a bug in your version - the test and report is backwards.
> When njn/org-agenda-show-closed is t you don't show closed items and
> when it's nil you do.
>
> - You don't need to check for equality with 't (and you don't need to
>  quote t)
>  - everything non-nil is true
>  - you can just check that directly in the (if ... )
>
> My version basically does this:
>  1. toggle the boolean njn/org-agenda-shot-closed
>  2. set org-agenda-log-mode-items based on the boolean
>  3. report the value

Thanks Bernt -- hehe -- I never really noticed the bug.

I'd like to see if there's a way to avoid having a variable at all --
something like this (In functional language / Lisp speak, that is)

If org-agenda-log-mode-items contains (closed) then
    org-agenda-log-mode-items = org-agenda-log-mode-items - (closed)
else
    org-agenda-log-mode-items = org-agenda-log-mode-items + (closed)
end if

I'll try something on my own if I have 15-20 minutes to work on it tonight --

Thanks for the bugfix and this cool line:
(message "%sShowing closed clock entries in agenda" (if
njn/org-agenda-show-closed "" "NOT ")

--Nate

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

end of thread, other threads:[~2010-12-07  3:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-06 18:29 Can this function be written better? Nathan Neff
2010-12-06 21:53 ` Bernt Hansen
2010-12-07  3:00   ` Nathan Neff

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

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