瀏覽代碼

`org-get-heading' returns nil instead of erroring out

* lisp/org.el (org-get-heading): Return nil instead of throwing an
  error before first headline.
Nicolas Goaziou 7 年之前
父節點
當前提交
3f26c9bd2e
共有 1 個文件被更改,包括 21 次插入19 次删除
  1. 21 19
      lisp/org.el

+ 21 - 19
lisp/org.el

@@ -7700,25 +7700,27 @@ So this will delete or add empty lines."
 When NO-TAGS is non-nil, don't include tags.
 When NO-TODO is non-nil, don't include TODO keywords.
 When NO-PRIORITY is non-nil, don't include priority cookie.
-When NO-COMMENT is non-nil, don't include COMMENT string."
-  (save-excursion
-    (org-back-to-heading t)
-    (let ((case-fold-search nil))
-      (looking-at org-complex-heading-regexp)
-      (let ((todo (and (not no-todo) (match-string 2)))
-	    (priority (and (not no-priority) (match-string 3)))
-	    (headline (pcase (match-string 4)
-			(`nil "")
-			((and (guard no-comment) h)
-			 (replace-regexp-in-string
-			  (eval-when-compile
-			    (format "\\`%s[ \t]+" org-comment-string))
-			  "" h))
-			(h h)))
-	    (tags (and (not no-tags) (match-string 5))))
-	(mapconcat #'identity
-		   (delq nil (list todo priority headline tags))
-		   " ")))))
+When NO-COMMENT is non-nil, don't include COMMENT string.
+Return nil before first heading."
+  (unless (org-before-first-heading-p)
+    (save-excursion
+      (org-back-to-heading t)
+      (let ((case-fold-search nil))
+	(looking-at org-complex-heading-regexp)
+	(let ((todo (and (not no-todo) (match-string 2)))
+	      (priority (and (not no-priority) (match-string 3)))
+	      (headline (pcase (match-string 4)
+			  (`nil "")
+			  ((and (guard no-comment) h)
+			   (replace-regexp-in-string
+			    (eval-when-compile
+			      (format "\\`%s[ \t]+" org-comment-string))
+			    "" h))
+			  (h h)))
+	      (tags (and (not no-tags) (match-string 5))))
+	  (mapconcat #'identity
+		     (delq nil (list todo priority headline tags))
+		     " "))))))
 
 (defun org-heading-components ()
   "Return the components of the current heading.