Skip to content

Instantly share code, notes, and snippets.

@tonyday567
Last active January 28, 2023 13:22
Show Gist options
  • Save tonyday567/4343164 to your computer and use it in GitHub Desktop.
Save tonyday567/4343164 to your computer and use it in GitHub Desktop.

(defun org-random-entry (&optional arg) “Select and goto a random todo item from the global agenda” (interactive “P”) (if org-agenda-overriding-arguments (setq arg org-agenda-overriding-arguments)) (if (and (stringp arg) (not (string-match “\S-” arg))) (setq arg nil)) (let* ((today (org-today)) (date (calendar-gregorian-from-absolute today)) (kwds org-todo-keywords-for-agenda) (lucky-entry nil) (completion-ignore-case t) (org-agenda-buffer (when (buffer-live-p org-agenda-buffer) org-agenda-buffer)) (org-select-this-todo-keyword (if (stringp arg) arg (and arg (integerp arg) (> arg 0) (nth (1- arg) kwds)))) rtn rtnall files file pos marker buffer) (when (equal arg ‘(4)) (setq org-select-this-todo-keyword (org-icompleting-read “Keyword (or KWD1|K2D2|…): ” (mapcar ‘list kwds) nil nil))) (and (equal 0 arg) (setq org-select-this-todo-keyword nil)) (catch ‘exit (org-compile-prefix-format ‘todo) (org-set-sorting-strategy ‘todo) (setq files (org-agenda-files nil ‘ifmode) rtnall nil) (while (setq file (pop files)) (catch ‘nextfile (org-check-agenda-file file) (setq rtn (org-agenda-get-day-entries file date :todo)) (setq rtnall (append rtnall rtn))))

(when rtnall (setq lucky-entry (nth (random (safe-length (setq entries rtnall))) entries))

(setq marker (or (get-text-property 0 ‘org-marker lucky-entry) (org-agenda-error))) (setq buffer (marker-buffer marker)) (setq pos (marker-position marker)) (org-pop-to-buffer-same-window buffer) (widen) (goto-char pos) (when (derived-mode-p ‘org-mode) (org-show-context ‘agenda) (save-excursion (and (outline-next-heading) (org-flag-heading nil))) ; show the next heading (when (outline-invisible-p) (show-entry)) ; display invisible text (run-hooks ‘org-agenda-after-show-hook))))))

@AlexBaranosky
Copy link

Any chance you might update this gist to be easy to cut and paste? As is the formatting and the non-standard quotes make it hard to use. Thanks :)

@jhrr
Copy link

jhrr commented Dec 1, 2014

Hitting the "raw" view button on the top right will give you a correctly formatted version of the code for copy and paste paradise.

@xuchunyang
Copy link

(defun org-random-entry (&optional arg)
  "Select and goto a random todo item from the global agenda"
  (interactive "P")
  (if org-agenda-overriding-arguments
      (setq arg org-agenda-overriding-arguments))
  (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
  (let* ((today (org-today))
         (date (calendar-gregorian-from-absolute today))
         (kwds org-todo-keywords-for-agenda)
         (lucky-entry nil)
         (completion-ignore-case t)
         (org-agenda-buffer (when (buffer-live-p org-agenda-buffer)
                              org-agenda-buffer))
         (org-select-this-todo-keyword
          (if (stringp arg) arg
            (and arg (integerp arg) (> arg 0)
                 (nth (1- arg) kwds))))
         rtn rtnall files file pos marker buffer)
    (when (equal arg '(4))
      (setq org-select-this-todo-keyword
            (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
                                  (mapcar 'list kwds) nil nil)))
    (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
    (catch 'exit
      (org-compile-prefix-format 'todo)
      (org-set-sorting-strategy 'todo)
      (setq files (org-agenda-files nil 'ifmode)
            rtnall nil)
      (while (setq file (pop files))
        (catch 'nextfile
          (org-check-agenda-file file)
          (setq rtn (org-agenda-get-day-entries file date :todo))
          (setq rtnall (append rtnall rtn))))

      (when rtnall
        (setq lucky-entry
              (nth (random
                    (safe-length
                     (setq entries rtnall)))
                   entries))

        (setq marker (or (get-text-property 0 'org-marker lucky-entry)
                         (org-agenda-error)))
        (setq buffer (marker-buffer marker))
        (setq pos (marker-position marker))
        (org-pop-to-buffer-same-window buffer)
        (widen)
        (goto-char pos)
        (when (derived-mode-p 'org-mode)
          (org-show-context 'agenda)
          (save-excursion
            (and (outline-next-heading)
                 (org-flag-heading nil))) ; show the next heading
          (when (outline-invisible-p)
            (show-entry))                 ; display invisible text
          (run-hooks 'org-agenda-after-show-hook))))))

from https://gist.githubusercontent.com/tonyday567/4343164/raw/362ae2370a25d9169c0fa9aaf160061368dff125/dotemacs.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment