Index: main/org-autofocus.el
===================================================================
--- main/org-autofocus.el	(revision main,10)
+++ main/org-autofocus.el	(revision main,11)
@@ -101,10 +101,94 @@
 
 
+(defun org-af--timestamp ()
+  "Return the current local time, with nanoseconds, formatted as an ISO string."
+  (format-time-string "%Y-%m-%d %H:%M:%S.%N" (current-time)))
+
+
+(defun org-af-defer (&optional no-redisplay)
+  "Defer the current task and update the display, unless NO-REDISPLAY."
+  (interactive)
+  (org-af--beg-of-task)
+  (if (org-af--dimmed-p (point))
+      (message "dimmed; skipping")
+    (when (org-af--marked-p (point))
+      (org-af-mark))
+
+    ;; This comes from the guts of the org-agenda-set-property command.
+    (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
+		         (org-agenda-error)))
+	   (buffer (marker-buffer hdmarker))
+	   (pos (marker-position hdmarker))
+	   (inhibit-read-only t)
+           (time (org-af--timestamp)))
+      (org-with-remote-undo buffer
+        (with-current-buffer buffer
+	  (widen)
+	  (goto-char pos)
+	  (org-show-context 'agenda)
+	  (org-set-property "TOUCHED" time))))
+
+    (save-excursion
+      (let* ((inhibit-read-only t)
+             (text (delete-and-extract-region (point)
+                                              (1+ (line-end-position)))))
+        (goto-char (point-max))
+        (save-excursion
+          (insert text))))))
+
+
+(defun org-af-complete ()
+  (interactive)
+  (org-af--beg-of-task)
+  (when (org-af--marked-p (point))
+    (org-af-mark))
+  (org-agenda-todo)
+  (org-af-defer t))
+
+
 (defvar org-autofocus-mode-map (make-sparse-keymap))
 (define-key org-autofocus-mode-map "." 'org-af-mark)
+(define-key org-autofocus-mode-map "d" 'org-af-defer)
+(define-key org-autofocus-mode-map "c" 'org-af-complete)
+(define-key org-autofocus-mode-map "t" 'org-af-complete)
+
+
+(defun org-af--touched (entry)
+  (let* ((marker (get-text-property 0 'org-hd-marker entry))
+         (touched (org-entry-get marker "TOUCHED")))
+    (unless touched
+      (setq touched (org-af--timestamp))
+      (org-entry-put marker "TOUCHED" touched))
+    touched))
+
+
+(defun org-af--cmp (a b)
+  "Compare A to B.
+If A > B, return +1; if A < B, return -1; else, return NIL."
+  (let* ((a-touched (org-af--touched a))
+         (b-touched (org-af--touched b))
+         (c (compare-strings a-touched nil nil b-touched nil nil)))
+    (cond
+     ((eq c t)  nil)
+     ((< c 0)   -1)
+     ((> c 0)   +1)
+     (t (error "%S and %S compared as %S" a-touched b-touched c)))))
 
 
 (define-minor-mode org-autofocus-mode nil nil " AF" org-autofocus-mode-map
-  nil)
+  ;;(setq org-agenda-redo-command '(org-autofocus))
+  )
+
+
+(defun org-autofocus ()
+  (interactive)
+  (let ((org-agenda-cmp-user-defined 'org-af--cmp)
+        (org-agenda-sorting-strategy '(user-defined-up)))
+    (org-agenda nil "n")
+    (org-autofocus-mode)))
+
+
+(add-hook 'org-agenda-mode-hook 'org-autofocus-mode)
+(remove-hook 'org-agenda-mode-hook 'org-autofocus-mode)
 
 
