emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Extract item body with drawers/properties
@ 2012-05-06 22:28 Christopher J. White
  2012-05-08  9:53 ` Bastien
  0 siblings, 1 reply; 2+ messages in thread
From: Christopher J. White @ 2012-05-06 22:28 UTC (permalink / raw)
  To: emacs-orgmode

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)
           )
         )
       )
     )
   )

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Extract item body with drawers/properties
  2012-05-06 22:28 Extract item body with drawers/properties Christopher J. White
@ 2012-05-08  9:53 ` Bastien
  0 siblings, 0 replies; 2+ messages in thread
From: Bastien @ 2012-05-08  9:53 UTC (permalink / raw)
  To: Christopher J. White; +Cc: emacs-orgmode

Hi Christopher,

"Christopher J. White" <chris@grierwhite.com> writes:

> Is there a function to extract the body of an item minus all the auxiliary
> information?

Yes -- see org-element.el in contrib/lisp/ and ̀org-element-parse-buffer'
as a starting point.  Nicolas might give further directions on how to get
the body text only.

> * Item
>   This is the text I want.
>   And here is the second line.
>   SCHEDULED: <2012-05-12>
>   DEADLINE: <2012-05-13>

Please put SCHEDULED: <2012-05-12> and DEADLINE: <2012-05-13> on the
line right after the headline "Item".  It will produce unexpected
results on some commands right now.  

>   :PROPERTIES:
>   :foo: bar
>   :END:

I would also recommend putting this right below the SCHEDULED/DEADLINE
line.  

>   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..."

There is `org-agenda-get-some-entry-text' but you don't want to look at
it... because it's tuned for use in agenda only.

> 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?  

Not yet -- but building one from org-element.el is possible.

> If not, does it make sense to make my version below
> workable for org-mode developers in general?

Please have a look at what Aurélien is working on right now:
http://orgmode.org/w/org-sync.git

The purpose is exactly this: build a gateway between Org and
external services like toodledo.  There is no support for toodledo
service in Aurélien's code for now, but I think there will be when
he will be done.

You might also be interested in org-x:

  http://thread.gmane.org/gmane.emacs.orgmode/45570

AFAIU, org-x ignores the content of a subtree, so this will not
help you that much -- but the idea of connecting Org with external 
services is there.

> (Related, what is the right term for this block of text?  Note?  Content?
> Text?)

I'd call this the "contents" of a section.  Note the plural form, as
each section can contain paragraphs, code snippets, etc.

HTH,

-- 
 Bastien

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-05-08  9:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-06 22:28 Extract item body with drawers/properties Christopher J. White
2012-05-08  9:53 ` Bastien

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).