0

I want to write a custom function to do bellowing work:

(defun my-org-agenda-switch-or-build ()
  "Switch to *Org Agenda* buffer, if not available, then build it."
  (interactive)
  (if (get-buffer "*Org Agenda*")
      (switch-to-buffer "*Org Agenda*")
    (command-execute 'org-agenda-list)
    (bury-buffer)
    (switch-to-buffer "*Org Agenda*")
    ))

;;; start Org Agenda at Emacs startup, and put in end of buffer list:
;; (add-hook 'emacs-startup-hook 'my-eshell-start-or-switch)
(define-key my-org-prefix-map (kbd "o") 'my-org-agenda-switch-or-build)

And here is my demo code, but it does not work. Do you have better idea about this?

(defun my-func/open-and-switch-to-buffer (the-command the-buffer-name &optional whether-switch-to-buffer)
  "Open a `COMMAND', and switch to that `BUFFER' depend on `OPTION'."
  ;; (interactive)
  (if (get-buffer the-buffer-name)
      (progn
        (,the-command)
        (bury-buffer))
    (let (whether-switch-to-buffer (and t whether-switch-to-buffer))
      (and whether-switch-to-buffer (switch-to-buffer the-buffer-name)))))
(define-key my-org-prefix-map (kbd "o") (my-func/open-and-switch-to-buffer 'org-agenda-list "*Org Agenda*" t))
stardiviner
  • 1,090
  • 1
  • 22
  • 33
  • (1) comma only evaluates within a backquoted expression. (2) you need `funcall` to call a function stored as the value of a variable. – phils Jul 22 '14 at 01:34
  • (3) Your `let` expression is all kinds of wrong. Re-read the documentation and syntax requirements carefully. In any case, you can replace all of it with: `(when whether-switch-to-buffer (switch-to-buffer the-buffer-name))` – phils Jul 22 '14 at 01:41
  • @phils I changed my code like this: https://gist.github.com/stardiviner/a15c9c03701fe05cdedf But the keybinding still does not work. Do you know why? – stardiviner Jul 22 '14 at 02:35
  • I hadn't even looked at that line of code. You're calling `(my-func/open-and-switch-to-buffer 'org-agenda-list "*Org Agenda*" t)` and trying to assign the *return value* of that (which will be a buffer object) to a key. I presume you want your key to call that function with those arguments. You can either define an interactive function which does that (assigning the new function to the key), or you can write it as an anonymous function: `(lambda () (interactive) (my-func/open-and-switch-to-buffer 'org-agenda-list "*Org Agenda*" t))` – phils Jul 22 '14 at 02:43

0 Answers0