Merged from mwolson@gnu.org--2006 (patch 53)
[planner-el.git] / planner-erc.el
blob843a3521e27d6615224d2eff85c3964c36fce2c7
1 ;;; planner-erc.el --- ERC support for Planner, an organizer for Emacs
3 ;; Copyright (C) 2004, 2006 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: planner-erc.el
7 ;; Keywords: hypermedia erc chat
8 ;; Author: Sacha Chua <sacha@free.net.ph>
9 ;; Description: Create tasks and notes based on IRC
10 ;; URL: http://www.plannerlove.com/
11 ;; Compatibility: Emacs20, Emacs21, XEmacs21
13 ;; This file is part of Planner. It is not part of GNU Emacs.
15 ;; Planner is free software; you can redistribute it and/or modify it
16 ;; under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
20 ;; Planner is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with Planner; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
30 ;;; Commentary:
32 ;; Place planner-erc.el in your load path and add this to your .emacs:
34 ;; (require 'planner-erc)
36 ;; IRC URLs may be of the following forms.
38 ;; irc://server/nick,isnick or
39 ;; irc://server/#channel or
40 ;; irc://server
42 ;; Annotations will be in the following forms.
44 ;; [[irc://server/nick,isnick][Chat with nick on server#channel]]
45 ;; [[irc://server/nick,isnick][Chat with nick on server]]
46 ;; [[irc://server/#channel][Chat on server#channel]]
47 ;; [[irc://server][Chat on server]]
49 ;;; Contributors:
51 ;;; Code:
53 (require 'planner)
54 (require 'erc)
56 (unless (boundp 'erc-server-announced-name)
57 (message "ERC 5.1 or higher is required; please upgrade ERC"))
59 (defvar planner-irc-regexp
60 "\\`[ei]rc://\\([^:/\n]+\\)\\([^/\n]+\\)?\\(?:/\\([^,/\n]+\\)\\(.*\\)\\)?"
61 "Regexp used to match IRC URLs.")
63 ;;;###autoload
64 (defun planner-erc-annotation-from-erc ()
65 "Return an annotation for the current line.
66 This function can be added to `planner-annotation-functions'."
67 (when (eq major-mode 'erc-mode)
68 (if (erc-default-target)
69 (if (erc-channel-p (erc-default-target))
70 (if (and (get-text-property (point) 'erc-parsed)
71 (elt (get-text-property (point) 'erc-parsed) 1))
72 (let ((nick
73 (car
74 (erc-parse-user
75 (elt (get-text-property (point) 'erc-parsed) 1)))))
76 (planner-make-link
77 (concat "irc://"
78 erc-server-announced-name "/"
79 (substring nick 1) ",isnick")
80 (concat "Chat with " nick " on "
81 erc-server-announced-name (erc-default-target))
82 t))
83 (planner-make-link
84 (concat "irc://"
85 erc-server-announced-name "/"
86 (erc-default-target))
87 (concat "Chat on " erc-server-announced-name
88 (erc-default-target))
89 t))
90 (planner-make-link
91 (concat "irc://" erc-server-announced-name "/"
92 (erc-default-target))
93 (concat "Chat with " (erc-default-target) " on "
94 erc-server-announced-name)
95 t))
96 (planner-make-link
97 (concat "irc://" erc-server-announced-name)
98 (concat "Chat on " erc-server-announced-name)
99 t))))
101 ;;;###autoload
102 (defun planner-erc-browse-url (url)
103 "If this is an IRC URL, jump to it."
104 (when (string-match planner-irc-regexp url)
105 (let ((server (match-string 1 url))
106 (port (match-string 2 url))
107 (target (match-string 3 url))
108 (flags (match-string 4 url))
109 buffer-list)
110 ;; find existing buffer
111 (setq buffer-list
112 (erc-buffer-filter
113 (lambda nil
114 (let ((server-buffer (erc-server-buffer)))
115 (and server-buffer
116 (string= server
117 (with-current-buffer server-buffer
118 erc-server-announced-name)))))))
119 ;; switch to buffer or create new connection
120 (if buffer-list
121 (if (not (stringp target))
122 ;; assume that the car is always the server buffer
123 (switch-to-buffer (car buffer-list))
124 (switch-to-buffer
125 (or (catch 'found
126 (dolist (buffer buffer-list)
127 (when (string= target (buffer-name buffer))
128 (throw 'found buffer))))
129 (car buffer-list))))
130 (erc-select :server server :port port)))
134 (planner-add-protocol "[ei]rc://" 'planner-erc-browse-url 'identity)
136 (add-hook 'planner-annotation-functions 'planner-erc-annotation-from-erc)
137 (custom-add-option 'planner-annotation-functions
138 'planner-erc-annotation-from-erc)
140 (provide 'planner-erc)
142 ;;; planner-erc.el ends here