Browse Source

Fix `org-hide-block-toggle' return value

* lisp/org.el (org-hide-block-toggle-maybe): Do not return a non-nil
  value when toggling failed.
(org-hide-block-toggle): Update docstring.

* testing/lisp/test-org.el (test-org/hide-block-toggle-maybe): New
Nicolas Goaziou 4 years ago
2 changed files with 17 additions and 7 deletions
  1. 9 7
  2. 8 0

+ 9 - 7

@@ -7323,15 +7323,16 @@ Optional arguments START and END can be used to limit the range."
 (defun org-hide-block-toggle-maybe ()
   "Toggle visibility of block at point.
-Do not throw an error.  Return t when toggling is successful."
+Unlike to `org-hide-block-toggle', this function does not throw
+an error.  Return a non-nil value when toggling is successful."
-  (ignore-errors (org-hide-block-toggle) t))
+  (and (ignore-errors (org-hide-block-toggle)) t))
 (defun org-hide-block-toggle (&optional force)
   "Toggle the visibility of the current block.
 When optional argument FORCE is `off', make block visible.  If it
 is non-nil, hide it unconditionally.  Throw an error when not at
-a block."
+a block.  Return a non-nil value when toggling is successful."
   (let ((element (org-element-at-point)))
     (unless (memq (org-element-type element)
@@ -7350,8 +7351,7 @@ a block."
        ;; Do nothing when not before or at the block opening line or
        ;; at the block closing line.
-       ((let ((eol (line-end-position)))
-	  (and (> eol start) (/= eol end))))
+       ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
        ((and (not (eq force 'off))
 	     (not (memq t (mapcar
 			   (lambda (o)
@@ -7372,9 +7372,11 @@ a block."
 	  ;; a visible part of the buffer.
 	  (when (> (line-beginning-position) start)
 	    (goto-char start)
-	    (beginning-of-line))))
+	    (beginning-of-line))
+	  ;; Signal successful toggling.
+	  t))
        ((or (not force) (eq force 'off))
-	(dolist (ov overlays)
+	(dolist (ov overlays t)
 	  (when (memq ov org-hide-block-overlays)
 	    (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
 	  (when (eq (overlay-get ov 'invisible) 'org-hide-block)

+ 8 - 0

@@ -1951,6 +1951,14 @@ Text.
      (get-char-property (point) 'invisible))))
+(ert-deftest test-org/hide-block-toggle-maybe ()
+  "Test `org-hide-block-toggle-maybe' specifications."
+  (should
+   (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
+     (org-hide-block-toggle-maybe)))
+  (should-not
+   (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
 (provide 'test-org)