Browse Source

Move `org-heading-delete-metadata' into "org-list.el"

* lisp/org.el (org-heading-delete-metadata): Move to...
* lisp/org-list.el (org-list--delete-metadata): ... here.  Refactor
  code.
(org-toggle-item): Use new function.

* testing/lisp/test-org-list.el (test-org-list/toggle-item): Add test.

At the moment, this function is only used internally in "org-list.el",
so it belongs there.

Also refrain from removing log or clock drawers, because they may not
contain all clocks or notes anyway. Besides, the information they
contain may still make sense in a plain list.
Nicolas Goaziou 7 years ago
parent
commit
5aaf3dfc7e
3 changed files with 32 additions and 25 deletions
  1. 13 2
      lisp/org-list.el
  2. 0 22
      lisp/org.el
  3. 19 1
      testing/lisp/test-org-list.el

+ 13 - 2
lisp/org-list.el

@@ -825,6 +825,17 @@ This function modifies STRUCT."
 		       (t (cons pos (cdar ind-to-ori))))))
 		  (cdr struct)))))
 
+(defun org-list--delete-metadata ()
+  "Delete metadata from the heading at point.
+Metadata are tags, planning information and properties drawers."
+  (save-match-data
+    (org-with-wide-buffer
+     (org-set-tags-to nil)
+     (delete-region (line-beginning-position 2)
+		    (save-excursion
+		      (org-end-of-meta-data)
+		      (org-skip-whitespace)
+		      (if (eobp) (point) (line-beginning-position)))))))
 
 
 ;;; Accessors
@@ -2994,7 +3005,7 @@ With a prefix argument ARG, change the region in a single item."
 	((org-at-heading-p)
 	 ;; Remove metadata
 	 (let (org-loop-over-headlines-in-active-region)
-	   (org-heading-delete-metadata))
+	   (org-list--delete-metadata))
 	 (let* ((bul (org-list-bullet-string "-"))
 		(bul-len (length bul))
 		;; Indentation of the first heading.  It should be
@@ -3017,7 +3028,7 @@ With a prefix argument ARG, change the region in a single item."
 	       (when (< level ref-level) (setq ref-level level))
 	       ;; Remove metadata
 	       (let (org-loop-over-headlines-in-active-region)
-		 (org-heading-delete-metadata))
+		 (org-list--delete-metadata))
 	       ;; Remove stars and TODO keyword.
 	       (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
 	       (delete-region (point) (or (match-beginning 3)

+ 0 - 22
lisp/org.el

@@ -7674,28 +7674,6 @@ Set it to HEADING when provided."
 	   (org-set-tags nil t)
 	   (when (looking-at "[ \t]*$") (replace-match ""))))))))
 
-(defun org-heading-delete-metadata ()
-  "Delete metadata from the heading at point.
-Metadata are tags, planning information and property/log/clock drawers."
-  (org-back-to-heading t)
-  (org-with-wide-buffer
-   (save-match-data
-     (let ((limit (save-excursion (outline-next-heading))))
-       (org-set-tags-to nil)
-       (save-excursion
-	 (when (re-search-forward
-		(concat org-planning-line-re ".*$") limit t)
-	   (replace-match "")))
-       (save-excursion
-	 (when (re-search-forward org-property-drawer-re limit t)
-	   (replace-match "")))
-       (save-excursion
-	 (when (re-search-forward org-log-drawer-re limit t)
-	   (replace-match "")))
-       (save-excursion
-	 (when (re-search-forward org-clock-drawer-re limit t)
-	   (replace-match "")))))))
-
 (defun org-insert-heading-after-current ()
   "Insert a new heading with same level as current, after current subtree."
   (interactive)

+ 19 - 1
testing/lisp/test-org-list.el

@@ -1,6 +1,6 @@
 ;;; test-org-list.el --- Tests for org-list.el
 
-;; Copyright (C) 2012, 2013, 2014  Nicolas Goaziou
+;; Copyright (C) 2012, 2013, 2014, 2018  Nicolas Goaziou
 
 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
 
@@ -937,6 +937,24 @@
 	  (org-test-with-temp-text "* TODO line"
 	    (org-toggle-item nil)
 	    (buffer-string))))
+  ;; When turning headlines into items, make sure planning info line
+  ;; and properties drawers are removed.  This also includes empty
+  ;; lines following them.
+  (should
+   (equal "- H\n"
+	  (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29 Thu>"
+	    (org-toggle-item nil)
+	    (buffer-string))))
+  (should
+   (equal "- H\n"
+	  (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
+	    (org-toggle-item nil)
+	    (buffer-string))))
+  (should
+   (equal "- H\nText"
+	  (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n\nText"
+	    (org-toggle-item nil)
+	    (buffer-string))))
   ;; When a region is marked and first line is a headline, all
   ;; headlines are turned into items.
   (should