Index: main/org-autofocus.el
===================================================================
--- main/org-autofocus.el	(revision main,18)
+++ main/org-autofocus.el	(revision main,19)
@@ -119,4 +119,33 @@
 
 
+(defun org-af--end-of-task (pos)
+  "Return the position of the end of the task at POS (excluding end of line)."
+  (next-single-property-change pos 'org-hd-marker))
+
+
+(defun org-af--next-task (from-pos)
+  "Return the position of the next visible task, from FROM-POS."
+  (setf from-pos (next-single-property-change from-pos 'org-hd-marker))
+  (while (eq (get-text-property from-pos 'org-todo-blocked) 'invisible)
+    ;; The EOL doesn't have an ORG-HD-MARKER property, so search twice to skip
+    ;; over it.
+    (setf from-pos (next-single-property-change from-pos 'org-hd-marker)
+          from-pos (next-single-property-change from-pos 'org-hd-marker)))
+  from-pos)
+
+
+(defun org-af--first-task ()
+  "Return the position of the first visible task."
+  (org-af--next-task 1))
+
+
+(defun org-af--prev-mark (pos)
+  "Return the position of the first mark previous to POS, or NIL if none are previous."
+  (let* ((prev (previous-single-char-property-change pos 'org-af-marked)))
+    (if (= prev 1)
+        nil
+      (1- prev))))
+
+
 (defun org-af-mark ()
   "Mark or unmark the current task, dim the previous marked task if any, and hide any tasks between."
@@ -126,5 +155,5 @@
   (let ((inhibit-read-only t)
         (task-point (point))
-        bol marked)
+        marked)
 
     (unless (org-af--dimmed-p task-point)
@@ -135,21 +164,23 @@
 
       (save-excursion
-        ;; Go to previous mark, if any, or first task otherwise.
-        (while (and (= (forward-line -1) 0)
-                    (get-text-property (point) 'type)
-                    (setq bol (point))
-                    (not (org-af--marked-p bol))))
-        (goto-char bol)
-
-        (when (org-af--marked-p bol)
+        (let ((prev-marked (org-af--prev-mark task-point))
+              hide-from)
+
+          (cond
+           (prev-marked
+            (let ((eot (org-af--end-of-task prev-marked)))
+              (if marked
+                  (org-af--dim prev-marked eot)
+                (org-af--undim prev-marked eot))
+              (setf hide-from (org-af--next-task eot))))
+
+           (t
+            (setf hide-from (org-af--first-task))))
+
           (if marked
-              (org-af--dim bol (line-end-position))
-            (org-af--undim bol (line-end-position))))
-
-        (forward-line)
-        (setq bol (point))
-        (if marked
-            (add-text-properties bol task-point '(category org-af-hidden))
-          (remove-text-properties bol task-point '(category org-af-hidden)))))))
+              (add-text-properties hide-from task-point
+                                   '(category org-af-hidden))
+            (remove-text-properties hide-from task-point
+                                    '(category org-af-hidden))))))))
 
 
