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