Browse Source

Enhance ̀org-insert-drawer': completion and region support.

* org.el (org-insert-drawer): Support completion over known
drawer names and inserting a drawer around the current region.
Bastien Guerry 7 years ago
parent
commit
01c23e4707
1 changed files with 52 additions and 38 deletions
  1. 52 38
      lisp/org.el

+ 52 - 38
lisp/org.el

@@ -14402,45 +14402,59 @@ formats in the current buffer."
   (interactive)
   (org-insert-drawer "PROPERTIES"))
 
-(defun org-insert-drawer (drawer)
+(defun org-insert-drawer (&optional drawer)
   "Insert a drawer into the current entry."
-  (interactive "sDrawer: ")
-  (org-back-to-heading t)
-  (looking-at org-outline-regexp)
-  (let ((indent (if org-adapt-indentation
-		    (- (match-end 0)(match-beginning 0))
-		  0))
-	(beg (point))
-	(re (concat "^[ \t]*" org-keyword-time-regexp))
-	end hiddenp)
-    (outline-next-heading)
-    (setq end (point))
-    (goto-char beg)
-    (while (re-search-forward re end t))
-    (setq hiddenp (outline-invisible-p))
-    (end-of-line 1)
-    (and (equal (char-after) ?\n) (forward-char 1))
-    (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
-      (if (member (match-string 1) '("CLOCK:" ":END:"))
-	  ;; just skip this line
-	  (beginning-of-line 2)
-	;; Drawer start, find the end
-	(re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
-	(beginning-of-line 1)))
-    (org-skip-over-state-notes)
-    (skip-chars-backward " \t\n\r")
-    (if (eq (char-before) ?*) (forward-char 1))
-    (let ((inhibit-read-only t)) (insert "\n:" drawer ":\n:END:"))
-    (beginning-of-line 0)
-    (org-indent-to-column indent)
-    (beginning-of-line 2)
-    (org-indent-to-column indent)
-    (beginning-of-line 0)
-    (if hiddenp
-	(save-excursion
-	  (org-back-to-heading t)
-	  (hide-entry))
-      (org-flag-drawer t))))
+  (interactive)
+  (if (org-region-active-p)
+    (let ((rbeg (region-beginning))
+	  (rend (region-end))
+	  (drawer (or drawer (completing-read "Drawer: " org-drawers))))
+      (goto-char rbeg)
+      (insert ":" drawer ":\n")
+      (move-beginning-of-line 1)
+      (indent-for-tab-command)
+      (goto-char rend)
+      (move-end-of-line 1)
+      (insert "\n:END:")
+      (move-beginning-of-line 1)
+      (indent-for-tab-command))
+    (org-back-to-heading t)
+    (looking-at org-outline-regexp)
+    (let ((indent (if org-adapt-indentation
+		      (- (match-end 0) (match-beginning 0))
+		    0))
+	  (beg (point))
+	  (re (concat "^[ \t]*" org-keyword-time-regexp))
+	  (drawer (or drawer (completing-read "Drawer: " org-drawers)))
+	  end hiddenp)
+      (outline-next-heading)
+      (setq end (point))
+      (goto-char beg)
+      (while (re-search-forward re end t))
+      (setq hiddenp (outline-invisible-p))
+      (end-of-line 1)
+      (and (equal (char-after) ?\n) (forward-char 1))
+      (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
+	(if (member (match-string 1) '("CLOCK:" ":END:"))
+	    ;; just skip this line
+	    (beginning-of-line 2)
+	  ;; Drawer start, find the end
+	  (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
+	  (beginning-of-line 1)))
+      (org-skip-over-state-notes)
+      (skip-chars-backward " \t\n\r")
+      (if (eq (char-before) ?*) (forward-char 1))
+      (let ((inhibit-read-only t)) (insert "\n:" drawer ":\n:END:"))
+      (beginning-of-line 0)
+      (org-indent-to-column indent)
+      (beginning-of-line 2)
+      (org-indent-to-column indent)
+      (beginning-of-line 0)
+      (if hiddenp
+	  (save-excursion
+	    (org-back-to-heading t)
+	    (hide-entry))
+	(org-flag-drawer t)))))
 
 (defvar org-property-set-functions-alist nil
   "Property set function alist.