Skip to content

@tonyday567 /dotemacs.org
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

(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

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

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
(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
Something went wrong with that request. Please try again.