emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: emacs-orgmode@gnu.org
Subject: [CODE] org-open-link-from-string in a program
Date: Thu, 08 Aug 2013 13:58:35 +0800	[thread overview]
Message-ID: <87haf0soz8.fsf@ericabrahamsen.net> (raw)
In-Reply-To: 87ehab5fgr.fsf@ericabrahamsen.net

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> I'm trying to write a small function that programmatically follows a
> link to a gnus message, then calls
> `gnus-summary-wide-reply-with-original' to start a reply to that
> message.

Okay, this seems like a fair amount of code for something that doesn't
actually do all that much, but here it is. I have one capture template
that incorporates a link to a gnus message in the headline (for REPLY
todos), and a different one that prompts for one or more mailto: or
bbdb: links, and puts them in the headline (with an EMAIL todo).
Sometimes I have mailto links and message links in the same header, so I
can reply to a message and copy other people on the reply.

The two main functions here (org-mail-handle-mail and
org-mail-handle-mail-agenda) look at the headline under point and
hopefully DTRT with the links they find there.

Like I said, the whole point of this is single-key-in, single-key-out
capturing and handling of email todos.

Things that are weird:

1. I've used an "org-mail" prefix, which doesn't otherwise exist.
2. It assumes you're using gnus
3. It assumes you want to reply to messages using `gnus-wide-reply-with-original'
4. It still seems a wee bit fragile. Gnus doesn't take kindly to be
operated non-interactively; I've used call-interactively where I can,
just in case, but sometimes odd things happen.

Anyway, there it is. I'd be happy to stick it on worg, put it in
contrib, or just leave it here. If anyone wants it tweaked or expanded
or generalized (it doesn't do org-contact contacts, for example), just
let me know.


(defvar org-mail-window-conf nil
  "Save org-buffer window configuration here, for later
  restoration.")

(defun org-mail-restore-after-send ()
  (gnus-summary-exit nil t)
  (when (window-configuration-p org-mail-window-conf)
    (set-window-configuration org-mail-window-conf))
  (call-interactively 'org-agenda-todo)) 

(defun org-mail-handle-mail (&optional interactive-p)
  "Handle mail-related links for current headline."
tC  (interactive "p")
  (unless (org-back-to-heading t)
    (error "Not in an org item"))
  (when interactive-p
    (setq org-mail-window-conf (current-window-configuration)))
  (let ((todo-kwd
	 (org-element-property :todo-keyword (org-element-at-point))) 
	message mailto)
    (while (re-search-forward org-any-link-re (line-end-position) t) 
      (let ((addr (or (match-string-no-properties 2)
		      (match-string-no-properties 0))))
	(cond
	 ((string-match "^<?gnus:" addr)
	  (push (substring addr (match-end 0)) message))
	 ((string-match "^<?mailto:" addr)
	  (push (substring addr (match-end 0)) mailto))
	 ((and (featurep 'bbdb)
	       (string-match-p "^<?bbdb:" addr))
	  (with-current-buffer bbdb-buffer-name
	    (let ((recs bbdb-records))
	      (org-open-link-from-string addr)
	      (let ((mail (bbdb-mail-address (bbdb-current-record))))
		(bbdb-display-records recs)
		(push mail mailto))))))))
    (cond
     (message
      (org-gnus-open (org-link-unescape (car message)))
      (call-interactively
       'gnus-summary-wide-reply-with-original)
      (when mailto
	(message-goto-to)
	(insert ", ")
	(insert (mapconcat 'identity mailto ", "))
	(message-goto-body))
      (add-to-list 'message-exit-actions
		   'org-mail-restore-after-send t))
     (mailto
      (compose-mail (mapconcat 'identity mailto ", ")
		    nil nil nil nil nil nil 
		    'org-mail-restore-after-send))
     (t
      (error "No mail-related links in headline")))))

(defun org-mail-handle-mail-agenda ()
  "Examine item at point for mail-related links, and handle them."
  (interactive)
  (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  (org-agenda-check-no-diary)
  (let* ((marker (or (org-get-at-bol 'org-hd-marker)
		     (org-agenda-error)))
	 (buffer (marker-buffer marker))
	 (pos (marker-position marker)))
    (setq org-mail-window-conf (current-window-configuration))
    (with-current-buffer buffer
      (widen)
      (goto-char pos)
      (org-mail-handle-mail))))

      parent reply	other threads:[~2013-08-08  5:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-03  8:46 org-open-link-from-string in a program Eric Abrahamsen
2013-08-03  9:18 ` Thorsten Jolitz
2013-08-03 10:26   ` Eric Abrahamsen
2013-08-03 10:56     ` Thorsten Jolitz
2013-08-03 11:04       ` Eric Abrahamsen
2013-08-03 11:18         ` Thorsten Jolitz
2013-08-04  4:21 ` Eric Abrahamsen
2013-08-04  8:22 ` Eric Abrahamsen
2013-08-08  5:58 ` Eric Abrahamsen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87haf0soz8.fsf@ericabrahamsen.net \
    --to=eric@ericabrahamsen.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).