From f7ff46cecc2f84dd1fd3f51477f7ce88f745486f Mon Sep 17 00:00:00 2001
From: Case Duckworth
Date: Thu, 19 Aug 2021 17:30:07 -0500
Subject: Update `acdw/copy-text-plain'

Totally works great now :)
---
 lisp/acdw.el | 36 +++++++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 7 deletions(-)

(limited to 'lisp')

diff --git a/lisp/acdw.el b/lisp/acdw.el
index 2be0c4e..30cf8af 100644
--- a/lisp/acdw.el
+++ b/lisp/acdw.el
@@ -313,16 +313,38 @@ With a prefix argument, run git pull on the repo first."
 (defun acdw/copy-region-plain (start end)
   "Copy a region to clipboard, removing all Org formatting."
   (interactive "*r")
-  (let ((s (buffer-substring-no-properties start end)))
+  (let ((s (buffer-substring-no-properties start end))
+        (extracted-heading (when (derived-mode-p 'org-mode)
+                             (acdw/org-extract-heading-text))))
     (with-temp-buffer
       (insert s)
       (let ((sentence-end-double-space nil))
-        (unfill-region (point-min) (point-max)))
-      (copy-region-as-kill (point-min) (point-max)))
-    (when (called-interactively-p 'interactive)
-      (indicate-copied-region))
-    (setq deactivate-mark t)
-    nil))
+        ;; Remove org stuff
+        (when extracted-heading         ; Replace org heading with plaintext
+          (goto-char (point-min))
+          (kill-line)
+          (insert extracted-heading))
+        (replace-regexp org-property-drawer-re "") ;Delete properties
+        (replace-regexp org-logbook-drawer-re "") ;Delete logbook
+        (replace-regexp org-list-full-item-re "
+\4")
+        ;; Re-fill text for clipboard
+        (unfill-region (point-min) (point-max))
+        (flush-lines "^$" (point-min) (point-max)))
+      ;; Copy buffer
+      (copy-region-as-kill (point-min) (point-max))))
+  (when (called-interactively-p 'interactive)
+    (indicate-copied-region))
+  (setq deactivate-mark t)
+  nil)
+
+(defun acdw/org-extract-heading-text ()
+  (let ((heading (org-no-properties (org-get-heading t t t t))))
+    (message
+     (replace-regexp-in-string org-link-bracket-re
+                               (lambda (match)
+                                 (match-string-no-properties 2 match))
+                               heading))))
 
 (defun acdw/dir (&optional file make-directory)
   "Place Emacs files in one place.
-- 
cgit 1.4.1-21-gabe81