Merged FSF changes into org-mouse.el.
[org-mode.git] / org-irc.el
blob36b03d150884ce7d5f55e8bffb9f225749c319a9
1 ;;; org-irc.el --- Store links to IRC sessions
2 ;;
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Philip Jackson <emacs@shellarchive.co.uk>
6 ;; Keywords: erc, irc, link, org
7 ;; Version: 1.3
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Link to an IRC session. Only ERC has been implemented at the
29 ;; moment.
31 ;; This file is loaded by default whenever org.el is loaded. Please
32 ;; customize the variable `org-default-extensions' to select extensions
33 ;; you would like to use, and to deselect those which you don't want.
35 ;; Please note that at the moment only ERC is supported. Other clients
36 ;; shouldn't be diffficult to add though.
38 ;; Then set `org-irc-link-to-logs' to non-nil if you would like a
39 ;; file:/ type link to be created to the current line in the logs or
40 ;; to t if you would like to create an irc:/ style link.
42 ;; Links within an org buffer might look like this:
44 ;; [[irc:/irc.freenode.net/#emacs/bob][chat with bob in #emacs on freenode]]
45 ;; [[irc:/irc.freenode.net/#emacs][#emacs on freenode]]
46 ;; [[irc:/irc.freenode.net/]]
48 ;; If, when the resulting link is visited, there is no connection to a
49 ;; requested server then one will be created.
51 ;;; Code:
54 (eval-when-compile
55 (require 'cl))
57 (require 'org)
58 (require 'erc)
59 (require 'erc-log)
61 (defvar org-irc-client 'erc
62 "The IRC client to act on")
63 (defvar org-irc-link-to-logs nil
64 "non-nil will store a link to the logs, nil will store an irc: style link")
66 (defvar erc-default-port) ; dynamically scoped from erc.el
67 (defvar erc-session-port) ; dynamically scoped form erc-backend.el
68 (defvar erc-server-announced-name) ; dynamically scoped form erc-backend.el
70 ;; Generic functions/config (extend these for other clients)
72 (add-to-list 'org-store-link-functions
73 'org-irc-store-link)
75 (org-add-link-type "irc" 'org-irc-visit nil)
77 (defun org-irc-visit (link)
78 "Dispatch to the correct visit function based on the client"
79 (let ((link (org-irc-parse-link link)))
80 (cond
81 ((eq org-irc-client 'erc)
82 (org-irc-visit-erc link))
84 (error "erc only known client")))))
86 (defun org-irc-parse-link (link)
87 "Get a of irc link attributes where `link' looks like
88 server:port/chan/user (port, chan and user being optional)."
89 (let* ((parts (split-string link "/" t))
90 (len (length parts)))
91 (when (or (< len 1) (> len 3))
92 (error "Failed to parse link needed 1-3 parts, got %d." len))
93 (setcar parts (split-string (car parts) ":" t))
94 parts))
96 ;;;###autoload
97 (defun org-irc-store-link ()
98 "Dispatch to the appropreate function to store a link to
99 something IRC related"
100 (cond
101 ((eq major-mode 'erc-mode)
102 (org-irc-erc-store-link))))
104 (defun org-irc-elipsify-description (string &optional after)
105 "Strip starting and ending whitespace and replace any chars
106 that appear after the value in `after' with '...'"
107 (let* ((after (number-to-string (or after 30)))
108 (replace-map (list (cons "^[ \t]*" "")
109 (cons "[ \t]*$" "")
110 (cons (concat "^\\(.\\{" after
111 "\\}\\).*") "\\1..."))))
112 (mapc (lambda (x)
113 (when (string-match (car x) string)
114 (setq string (replace-match (cdr x) nil nil string))))
115 replace-map)
116 string))
118 ;; ERC specific functions
120 (defun org-irc-erc-get-line-from-log (erc-line)
121 "Find the most suitable line to link to from the erc logs. If
122 the user is on the erc-prompt then search backward for the first
123 non-blank line, otherwise return the current line. The result is
124 a cons of the filename and search string."
125 (erc-save-buffer-in-logs)
126 (with-current-buffer (find-file-noselect (erc-current-logfile))
127 (goto-char (point-max))
128 (list
129 (abbreviate-file-name buffer-file-name)
130 ;; can we get a '::' part?
131 (if (string= erc-line (erc-prompt))
132 (progn
133 (goto-char (point-at-bol))
134 (when (search-backward-regexp "^[^ ]" nil t)
135 (buffer-substring-no-properties (point-at-bol)
136 (point-at-eol))))
137 (when (search-backward erc-line nil t)
138 (buffer-substring-no-properties (point-at-bol)
139 (point-at-eol)))))))
141 (defun org-irc-erc-store-link ()
142 "Depending on the variable `org-irc-link-to-logs' store either
143 a link to the log file for the current session or an irc: link to
144 the session itself."
145 (if org-irc-link-to-logs
146 (let* ((erc-line (buffer-substring-no-properties
147 (point-at-bol) (point-at-eol)))
148 (parsed-line (org-irc-erc-get-line-from-log erc-line)))
149 (if (erc-logging-enabled nil)
150 (progn
151 (org-store-link-props
152 :type "file"
153 :description (concat "'" (org-irc-elipsify-description
154 (cadr parsed-line) 20)
155 "' from an IRC conversation")
156 :link (concat "file:" (car parsed-line) "::"
157 (cadr parsed-line)))
159 (error "This ERC session is not being logged")))
160 (let* ((link-text (org-irc-get-erc-link))
161 (link (org-irc-parse-link link-text)))
162 (if link-text
163 (progn
164 (org-store-link-props
165 :type "irc"
166 :link (org-make-link "irc:/" link-text)
167 :description (concat "irc session '" link-text "'")
168 :server (car (car link))
169 :port (or (cadr (pop link)) erc-default-port)
170 :nick (pop link))
172 (error "Failed to create ('irc:/' style) ERC link")))))
174 (defun org-irc-get-erc-link ()
175 "Return an org compatible irc:/ link from an ERC buffer"
176 (let ((link (concat erc-server-announced-name ":"
177 erc-session-port)))
178 (concat link "/"
179 (if (and (erc-default-target)
180 (erc-channel-p (erc-default-target))
181 (car (get-text-property (point) 'erc-data)))
182 ;; we can get a nick
183 (let ((nick (car (get-text-property (point) 'erc-data))))
184 (concat (erc-default-target) "/" nick))
185 (erc-default-target)))))
187 (defun org-irc-visit-erc (link)
188 "Visit an ERC buffer based on criteria from the followed link"
189 (let* ((server (car (car link)))
190 (port (or (cadr (pop link)) erc-default-port))
191 (server-buffer)
192 (buffer-list
193 (erc-buffer-filter
194 (lambda nil
195 (let ((tmp-server-buf (erc-server-buffer)))
196 (and tmp-server-buf
197 (with-current-buffer tmp-server-buf
198 (and
199 (string= erc-session-port port)
200 (string= erc-server-announced-name server)
201 (setq server-buffer tmp-server-buf)))))))))
202 (if buffer-list
203 (let ((chan-name (pop link)))
204 ;; if we got a channel name then switch to it or join it
205 (if chan-name
206 (let ((chan-buf (find-if
207 (lambda (x)
208 (string= (buffer-name x) chan-name))
209 buffer-list)))
210 (if chan-buf
211 (progn
212 (switch-to-buffer chan-buf)
213 ;; if we got a nick, and they're in the chan,
214 ;; then start a chat with them
215 (let ((nick (pop link)))
216 (when nick
217 (if (find nick (erc-get-server-nickname-list)
218 :test 'string=)
219 (progn
220 (goto-char (point-max))
221 (insert (concat nick ": ")))
222 (error "%s not found in %s" nick chan-name)))))
223 (progn
224 (switch-to-buffer server-buffer)
225 (erc-cmd-JOIN chan-name))))
226 (switch-to-buffer server-buffer)))
227 ;; no server match, make new connection
228 (erc-select :server server :port port))))
230 (provide 'org-irc)
232 ;;; org-irc.el ends here