From mboxrd@z Thu Jan 1 00:00:00 1970 From: Bernt Hansen Subject: Re: Can this function be written better? Date: Mon, 06 Dec 2010 16:53:36 -0500 Message-ID: <8739qapnz3.fsf@norang.ca> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: from [140.186.70.92] (port=56009 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PPj09-0000m2-0s for emacs-orgmode@gnu.org; Mon, 06 Dec 2010 16:53:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PPj07-0002PX-Op for emacs-orgmode@gnu.org; Mon, 06 Dec 2010 16:53:40 -0500 Received: from mho-01-ewr.mailhop.org ([204.13.248.71]:46493) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PPj07-0002PQ-Gp for emacs-orgmode@gnu.org; Mon, 06 Dec 2010 16:53:39 -0500 In-Reply-To: (Nathan Neff's message of "Mon, 6 Dec 2010 12:29:37 -0600") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Nathan Neff Cc: emacs-orgmode Nathan Neff 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