Cleaned up the description fo C-c C-t for changing TODO states.
[org-mode.git] / CONTRIB / lisp / org-irc.el
blob464ae4433ce558c1626184e3df459a0667990172
1 ;;; org-irc.el --- Store links to IRC sessions.
2 ;; Copyright (C) 2008 Phil Jackson
3 ;;
4 ;; Author: Philip Jackson <emacs@shellarchive.co.uk>
5 ;; Keywords: erc, irc, link, org
6 ;; Version: 0.03
7 ;;
8 ;; This file is not yet part of GNU Emacs.
9 ;;
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; Link to an IRC session. Only ERC has been implemented at the
29 ;; moment.
31 ;; Place org-irc.el in your load path and add this to your
32 ;; .emacs:
34 ;; (require 'org-irc)
36 ;; Please note that at the moment only ERC is supported. Other clients
37 ;; shouldn't be diffficult to add though.
39 ;; Then set `org-irc-link-to-logs' to non-nil if you would like a
40 ;; file:/ type link to be created to the current line in the logs or
41 ;; to t if you would like to create an irc:/ style link.
43 ;; Links within an org buffer might look like this:
45 ;; [[irc:/irc.freenode.net/#emacs/bob][chat with bob in #emacs on freenode]]
46 ;; [[irc:/irc.freenode.net/#emacs][#emacs on freenode]]
47 ;; [[irc:/irc.freenode.net/]]
49 ;; If, when the resulting link is visited, there is no connection to a
50 ;; requested server then one will be created.
52 ;;; Code:
54 (require 'org)
56 (defvar org-irc-client 'erc
57 "The IRC client to act on")
58 (defvar org-irc-link-to-logs t
59 "non-nil will store a link to the logs, nil will store an irc: style link")
61 ;; Generic functions/config (extend for other clients)
63 (add-to-list 'org-store-link-functions
64 'org-irc-store-link)
66 (org-add-link-type "irc" 'org-irc-visit nil)
68 (defun org-irc-visit (link)
69 "Dispatch to the correct visit function based on the client"
70 (let ((link (org-irc-parse-link link)))
71 (cond
72 ((eq org-irc-client 'erc)
73 (org-irc-visit-erc link))
75 (error "erc only known client")))))
77 (defun org-irc-parse-link (link)
78 "Get a of irc link attributes where `link' looks like
79 server:port/chan/user (port, chan and user being optional)."
80 (let* ((parts (split-string link "/" t))
81 (len (length parts)))
82 (when (or (< len 1) (> len 3))
83 (error "Failed to parse link needed 1-3 parts, got %d." len))
84 (setcar parts (split-string (car parts) ":" t))
85 parts))
87 ;;;###autoload
88 (defun org-irc-store-link ()
89 "Dispatch to the appropreate function to store a link to
90 something IRC related"
91 (cond
92 ((eq major-mode 'erc-mode)
93 (org-irc-erc-store-link))))
95 ;; ERC specific functions
97 (defun org-irc-erc-get-line-from-log (erc-line)
98 "Find the most suitable line to link to from the erc logs. If
99 the user is on the erc-prompt then search backward for the first
100 non-blank line, otherwise return the current line."
101 (erc-save-buffer-in-logs)
102 (with-current-buffer (find-file-noselect (erc-current-logfile))
103 (goto-char (point-max))
104 (concat
105 "file:" (abbreviate-file-name
106 buffer-file-name)
107 ;; can we get a '::' part?
108 (if (equal erc-line (erc-prompt))
109 (progn
110 (goto-char (point-at-bol))
111 (when (search-backward-regexp "^[^ ]" nil t)
112 (concat "::" (buffer-substring-no-properties
113 (point-at-bol)
114 (point-at-eol)))))
115 (when (search-backward erc-line nil t)
116 (concat "::" (buffer-substring-no-properties
117 (point-at-bol)
118 (point-at-eol))))))))
120 (defun org-irc-erc-store-link ()
121 "Return an org compatible file:/ link which links to a log file
122 of the current ERC session"
123 (if org-irc-link-to-logs
124 (let ((erc-line (buffer-substring-no-properties
125 (point-at-bol) (point-at-eol))))
126 (if (erc-logging-enabled nil)
127 (progn
128 (setq cpltxt (org-irc-erc-get-line-from-log erc-line))
130 (error "This ERC session is not being logged")))
131 (let ((link (org-irc-get-erc-link)))
132 (if link
133 (progn
134 (setq cpltxt (concat "irc:/" link))
135 (org-make-link cpltxt)
136 (setq link (org-irc-parse-link link))
137 (org-store-link-props :type "irc"
138 :server (car (car link))
139 :port (or (cadr (pop link))
140 erc-default-port)
141 :nick (pop link))
143 (error "Failed to create (non-log) ERC link")))))
145 (defun org-irc-get-erc-link ()
146 "Return an org compatible irc:/ link from an ERC buffer"
147 (let ((link (concat erc-server-announced-name ":"
148 erc-session-port)))
149 (concat link "/"
150 (if (and (erc-default-target)
151 (erc-channel-p (erc-default-target))
152 (get-text-property (point) 'erc-parsed)
153 (elt (get-text-property (point) 'erc-parsed) 1))
154 ;; we can get a nick
155 (let ((nick
156 (car
157 (erc-parse-user
158 (elt (get-text-property (point)
159 'erc-parsed) 1)))))
160 (concat (erc-default-target) "/"
161 (substring nick 1)))
162 (erc-default-target)))))
164 (defun org-irc-visit-erc (link)
165 "Visit an ERC buffer based on criteria from the followed link"
166 (let* ((server (car (car link)))
167 (port (or (cadr (pop link)) erc-default-port))
168 (server-buffer)
169 (buffer-list
170 (erc-buffer-filter
171 (lambda nil
172 (let ((tmp-server-buf (erc-server-buffer)))
173 (and tmp-server-buf
174 (with-current-buffer tmp-server-buf
175 (and
176 (string= erc-session-port port)
177 (string= erc-server-announced-name server)
178 (setq server-buffer tmp-server-buf)))))))))
179 (if buffer-list
180 (let ((chan-name (pop link)))
181 ;; if we got a channel name then switch to it or join it
182 (if chan-name
183 (let ((chan-buf (find-if
184 (lambda (x)
185 (string= (buffer-name x) chan-name))
186 buffer-list)))
187 (if chan-buf
188 (progn
189 (switch-to-buffer chan-buf)
190 ;; if we got a nick, and they're in the chan,
191 ;; then start a chat with them
192 (let ((nick (pop link)))
193 (when nick
194 (if (find nick (erc-get-server-nickname-list)
195 :test 'string=)
196 (progn
197 (goto-char (point-max))
198 (insert (concat nick ": ")))
199 (error "%s not found in %s" nick chan)))))
200 (progn
201 (switch-to-buffer server-buffer)
202 (erc-cmd-JOIN chan-name))))
203 (switch-to-buffer server-buffer)))
204 ;; no server match, make new connection
205 (erc-select :server server :port port))))
207 (provide 'org-irc)
209 ;;; org-irc.el ends here