Reserve 10 chars for weekday name in agenda.
[org-mode.git] / org-irc.el
blobf9e2b97ac2af4f5d6c46a7fe7e8bd1dbd5c48151
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.4
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:
53 (require 'org)
54 (require 'erc)
55 (require 'erc-log)
57 (defvar org-irc-client 'erc
58 "The IRC client to act on")
59 (defvar org-irc-link-to-logs nil
60 "non-nil will store a link to the logs, nil will store an irc: style link")
62 (defvar erc-default-port) ; dynamically scoped from erc.el
63 (defvar erc-session-port) ; dynamically scoped form erc-backend.el
64 (defvar erc-session-server) ; dynamically scoped form erc-backend.el
66 ;; Generic functions/config (extend these for other clients)
68 (add-to-list 'org-store-link-functions
69 'org-irc-store-link)
71 (org-add-link-type "irc" 'org-irc-visit nil)
73 (defun org-irc-visit (link)
74 "Dispatch to the correct visit function based on the client"
75 (let ((link (org-irc-parse-link link)))
76 (cond
77 ((eq org-irc-client 'erc)
78 (org-irc-visit-erc link))
80 (error "erc only known client")))))
82 (defun org-irc-parse-link (link)
83 "Get a of irc link attributes where `link' looks like
84 server:port/chan/user (port, chan and user being optional)."
85 (let* ((parts (split-string link "/" t))
86 (len (length parts)))
87 (when (or (< len 1) (> len 3))
88 (error "Failed to parse link needed 1-3 parts, got %d." len))
89 (setcar parts (split-string (car parts) ":" t))
90 parts))
92 ;;;###autoload
93 (defun org-irc-store-link ()
94 "Dispatch to the appropreate function to store a link to
95 something IRC related"
96 (cond
97 ((eq major-mode 'erc-mode)
98 (org-irc-erc-store-link))))
100 (defun org-irc-elipsify-description (string &optional after)
101 "Strip starting and ending whitespace and replace any chars
102 that appear after the value in `after' with '...'"
103 (let* ((after (number-to-string (or after 30)))
104 (replace-map (list (cons "^[ \t]*" "")
105 (cons "[ \t]*$" "")
106 (cons (concat "^\\(.\\{" after
107 "\\}\\).*") "\\1..."))))
108 (mapc (lambda (x)
109 (when (string-match (car x) string)
110 (setq string (replace-match (cdr x) nil nil string))))
111 replace-map)
112 string))
114 ;; ERC specific functions
116 (defun org-irc-erc-get-line-from-log (erc-line)
117 "Find the most suitable line to link to from the erc logs. If
118 the user is on the erc-prompt then search backward for the first
119 non-blank line, otherwise return the current line. The result is
120 a cons of the filename and search string."
121 (erc-save-buffer-in-logs)
122 (with-current-buffer (find-file-noselect (erc-current-logfile))
123 (goto-char (point-max))
124 (list
125 (abbreviate-file-name buffer-file-name)
126 ;; can we get a '::' part?
127 (if (string= erc-line (erc-prompt))
128 (progn
129 (goto-char (point-at-bol))
130 (when (search-backward-regexp "^[^ ]" nil t)
131 (buffer-substring-no-properties (point-at-bol)
132 (point-at-eol))))
133 (when (search-backward erc-line nil t)
134 (buffer-substring-no-properties (point-at-bol)
135 (point-at-eol)))))))
137 (defun org-irc-erc-store-link ()
138 "Depending on the variable `org-irc-link-to-logs' store either
139 a link to the log file for the current session or an irc: link to
140 the session itself."
141 (if org-irc-link-to-logs
142 (let* ((erc-line (buffer-substring-no-properties
143 (point-at-bol) (point-at-eol)))
144 (parsed-line (org-irc-erc-get-line-from-log erc-line)))
145 (if (erc-logging-enabled nil)
146 (progn
147 (org-store-link-props
148 :type "file"
149 :description (concat "'" (org-irc-elipsify-description
150 (cadr parsed-line) 20)
151 "' from an IRC conversation")
152 :link (concat "file:" (car parsed-line) "::"
153 (cadr parsed-line)))
155 (error "This ERC session is not being logged")))
156 (let* ((link-text (org-irc-get-erc-link))
157 (link (org-irc-parse-link link-text)))
158 (if link-text
159 (progn
160 (org-store-link-props
161 :type "irc"
162 :link (org-make-link "irc:/" link-text)
163 :description (concat "irc session '" link-text "'")
164 :server (car (car link))
165 :port (or (string-to-number (cadr (pop link))) erc-default-port)
166 :nick (pop link))
168 (error "Failed to create ('irc:/' style) ERC link")))))
170 (defun org-irc-get-erc-link ()
171 "Return an org compatible irc:/ link from an ERC buffer"
172 (let* ((session-port (if (numberp erc-session-port)
173 (number-to-string erc-session-port)
174 erc-session-port))
175 (link (concat erc-session-server ":" session-port)))
176 (concat link "/"
177 (if (and (erc-default-target)
178 (erc-channel-p (erc-default-target))
179 (car (get-text-property (point) 'erc-data)))
180 ;; we can get a nick
181 (let ((nick (car (get-text-property (point) 'erc-data))))
182 (concat (erc-default-target) "/" nick))
183 (erc-default-target)))))
185 (defun org-irc-get-current-erc-port ()
186 "Return the current port as a number. If there is not an
187 explicit port set then return the erc default."
188 (cond
189 ((stringp erc-session-port)
190 (string-to-number erc-session-port))
191 ((numberp erc-session-port)
192 erc-session-port)
194 erc-default-port)))
196 (defun org-irc-visit-erc (link)
197 "Visit an ERC buffer based on criteria from the followed link"
198 (let* ((server (car (car link)))
199 (port (or (string-to-number (cadr (pop link))) erc-default-port))
200 (server-buffer)
201 (buffer-list
202 (erc-buffer-filter
203 (lambda nil
204 (let ((tmp-server-buf (erc-server-buffer)))
205 (and tmp-server-buf
206 (with-current-buffer tmp-server-buf
207 (and
208 (eq (org-irc-get-current-erc-port) port)
209 (string= erc-session-server server)
210 (setq server-buffer tmp-server-buf)))))))))
211 (if buffer-list
212 (let ((chan-name (pop link)))
213 ;; if we got a channel name then switch to it or join it
214 (if chan-name
215 (let ((chan-buf (catch 'found
216 (dolist (x buffer-list)
217 (if (string= (buffer-name x) chan-name)
218 (throw 'found x))))))
219 (if chan-buf
220 (progn
221 (switch-to-buffer chan-buf)
222 ;; if we got a nick, and they're in the chan,
223 ;; then start a chat with them
224 (let ((nick (pop link)))
225 (when nick
226 (if (member nick (erc-get-server-nickname-list))
227 (progn
228 (goto-char (point-max))
229 (insert (concat nick ": ")))
230 (error "%s not found in %s" nick chan-name)))))
231 (progn
232 (switch-to-buffer server-buffer)
233 (erc-cmd-JOIN chan-name))))
234 (switch-to-buffer server-buffer)))
235 ;; no server match, make new connection
236 (erc-select :server server :port port))))
238 (provide 'org-irc)
240 ;; arch-tag: 018d7dda-53b8-4a35-ba92-6670939e525a
241 ;;; org-irc.el ends here