From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Christopher J. White" Subject: Extract item body with drawers/properties Date: Sun, 06 May 2012 18:28:57 -0400 Message-ID: <4FA6FB29.2060204@grierwhite.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Return-path: Received: from eggs.gnu.org ([208.118.235.92]:37493) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SR9x1-0007zQ-IN for emacs-orgmode@gnu.org; Sun, 06 May 2012 18:29:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SR9wz-0002XK-LF for emacs-orgmode@gnu.org; Sun, 06 May 2012 18:29:11 -0400 Received: from mail7c25.carrierzone.com ([64.29.147.17]:48472) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SR9wz-0002VB-EG for emacs-orgmode@gnu.org; Sun, 06 May 2012 18:29:09 -0400 Received: from [11.1.1.76] (pool-108-49-5-166.bstnma.east.verizon.net [108.49.5.166]) (authenticated bits=0) by mail7c25.carrierzone.com (8.13.6/8.13.1) with ESMTP id q46MSvQc000673 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sun, 6 May 2012 22:28:59 GMT List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Hi Folks, Is there a function to extract the body of an item minus all the auxiliary information? * Item This is the text I want. And here is the second line. SCHEDULED: <2012-05-12> DEADLINE: <2012-05-13> :PROPERTIES: :foo: bar :END: And it's conceivable there is more below drawers... ** Sub-Item 1 ** Sub-Item 2 Basically I want a function that does the following: (org-entry-get-text) "This is the text I want And here is the second line. And it's conceivable there is more below drawers..." Point is at "* Item" when this is called. For one project (org-toodledo), I coded a version (see below) that pulls out the drawers, drops properties SCHEDULED/DEADLINE/CLOSED, and pulls off any indentation, and it works pretty well, although it is probably not complete for all cases. However, I'm now working on extensions for another project (org-taskjuggler) and want to again pull out the note. I tried again to find such a function in the org source files, but I just can't seem to find it. Does it exist? If not, does it make sense to make my version below workable for org-mode developers in general? (Related, what is the right term for this block of text? Note? Content? Text?) Thanks ...cj (defun org-toodledo-entry-note () "Extract the note for this task." (save-excursion (org-back-to-heading t) (when (looking-at org-complex-heading-regexp) (goto-char (match-end 0)) (let ((text (buffer-substring-no-properties (point) (if (re-search-forward org-complex-heading-regexp nil t) (match-beginning 0) (org-end-of-subtree t t))))) (with-temp-buffer (insert text) ;; Pull out DEADLINE / SCHEDULED / CLOSED fields (dolist (str (list (regexp-quote org-deadline-string) (regexp-quote org-scheduled-string) (regexp-quote org-closed-string))) (goto-char (point-min)) (when (re-search-forward (concat "\\<" str " +[<\[][^]>\n]+[]>][ \t]*") nil t) (replace-match "XXXX "))) ;; Drop any empty lines with only XXXX (goto-char (point-min)) (while (re-search-forward "^ *\\(XXXX \\)+\n" nil t) (replace-match "")) ;; Drop any remaining XXXX (goto-char (point-min)) (while (re-search-forward "XXXX " nil t) (replace-match "")) ;; org-export-remove-or-extract-drawers removed an argument sometime around version 7 (if (>= (string-to-number org-version) 7) (org-export-remove-or-extract-drawers org-drawers nil) (org-export-remove-or-extract-drawers org-drawers nil nil)) ;; Trim leading/trailing empty lines, but preserve whitepace at the beginning of the line (goto-char (point-min)) (if (re-search-forward "\\=\\( *\n\\)+" nil t) (replace-match "")) (goto-char (point-min)) (if (re-search-forward "\\( *\n\\)+\\'" nil t) (replace-match "")) (goto-char (point-max)) (insert "\n") ;; Finally, if this was indented and indenting notes, remove indentation (when org-toodledo-indent-task-note (goto-char (point-min)) (when (re-search-forward "^ +" nil t) (let ((str (match-string 0))) (goto-char (point-min)) (while (re-search-forward (format "^%s" str) nil t) (replace-match ""))))) (let ((s (buffer-substring-no-properties (point-min) (point-max)))) (if (string-match "\\(\\`[ \t]*[\n\r]+\\)+" s) (setq s (replace-match "" t t s))) (if (string-match "\\([\n\r]+[ \t]*\\)+\\'" s) (setq s (replace-match "" t t s))) s) ) ) ) ) )