1 ;;; org-irc.el --- Store Links to IRC Sessions -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2008-2017 Free Software Foundation, Inc.
5 ;; Author: Philip Jackson <emacs@shellarchive.co.uk>
6 ;; Keywords: erc, irc, link, org
8 ;; This file is part of GNU Emacs.
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 of the License, or
13 ;; (at your option) 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. If not, see <https://www.gnu.org/licenses/>.
25 ;; This file implements links to an IRC session from within Org mode.
26 ;; Org mode loads this module by default - if this is not what you want,
27 ;; configure the variable `org-modules'.
29 ;; Please customize the variable `org-modules' to select
30 ;; extensions you would like to use, and to deselect those which you don't
33 ;; Please note that at the moment only ERC is supported. Other clients
34 ;; shouldn't be difficult to add though.
36 ;; Then set `org-irc-link-to-logs' to non-nil if you would like a
37 ;; file:/ type link to be created to the current line in the logs or
38 ;; to t if you would like to create an irc:/ style link.
40 ;; Links within an org buffer might look like this:
42 ;; [[irc:/irc.freenode.net/#emacs/bob][chat with bob in #emacs on freenode]]
43 ;; [[irc:/irc.freenode.net/#emacs][#emacs on freenode]]
44 ;; [[irc:/irc.freenode.net/]]
46 ;; If, when the resulting link is visited, there is no connection to a
47 ;; requested server then one will be created.
53 (declare-function erc-buffer-filter
"erc" (predicate &optional proc
))
54 (declare-function erc-channel-p
"erc" (channel))
55 (declare-function erc-cmd-JOIN
"erc" (channel &optional key
))
56 (declare-function erc-current-logfile
"erc-log" (&optional buffer
))
57 (declare-function erc-default-target
"erc" ())
58 (declare-function erc-get-server-nickname-list
"erc" ())
59 (declare-function erc-logging-enabled
"erc-log" (&optional buffer
))
60 (declare-function erc-prompt
"erc" ())
61 (declare-function erc-save-buffer-in-logs
"erc-log" (&optional buffer
))
62 (declare-function erc-server-buffer
"erc" ())
64 (defvar org-irc-client
'erc
65 "The IRC client to act on.")
67 (defvar org-irc-link-to-logs nil
68 "Non-nil will store a link to the logs, nil will store an irc: style link.")
70 (defvar erc-default-port
) ; dynamically scoped from erc.el
71 (defvar erc-session-port
) ; dynamically scoped form erc-backend.el
72 (defvar erc-session-server
) ; dynamically scoped form erc-backend.el
74 ;; Generic functions/config (extend these for other clients)
76 (org-link-set-parameters "irc"
77 :follow
#'org-irc-visit
78 :store
#'org-irc-store-link
79 :export
#'org-irc-export
)
81 (defun org-irc-visit (link)
82 "Parse LINK and dispatch to the correct function based on the client found."
83 (let ((link (org-irc-parse-link link
)))
85 ((eq org-irc-client
'erc
)
86 (org-irc-visit-erc link
))
88 (error "ERC only known client")))))
90 (defun org-irc-parse-link (link)
91 "Parse an IRC LINK and return the attributes found.
92 Parse a LINK that looks like server:port/chan/user (port, chan
93 and user being optional) and return any of the port, channel or user
94 attributes that are found."
95 (let* ((parts (split-string link
"/" t
))
97 (when (or (< len
1) (> len
3))
98 (error "Failed to parse link needed 1-3 parts, got %d" len
))
99 (setcar parts
(split-string (car parts
) ":" t
))
103 (defun org-irc-store-link ()
104 "Dispatch to the appropriate function to store a link to an IRC session."
106 ((eq major-mode
'erc-mode
)
107 (org-irc-erc-store-link))))
109 (defun org-irc-ellipsify-description (string &optional after
)
110 "Remove unnecessary white space from STRING and add ellipses if necessary.
111 Strip starting and ending white space from STRING and replace any
112 chars that the value AFTER with `...'"
113 (let* ((after (number-to-string (or after
30)))
114 (replace-map (list (cons "^[ \t]*" "")
116 (cons (concat "^\\(.\\{" after
117 "\\}\\).*") "\\1..."))))
118 (dolist (x replace-map string
)
119 (when (string-match (car x
) string
)
120 (setq string
(replace-match (cdr x
) nil nil string
))))))
122 ;; ERC specific functions
124 (defun org-irc-erc-get-line-from-log (erc-line)
125 "Find the best line to link to from the ERC logs given ERC-LINE as a start.
126 If the user is on the ERC-prompt then search backward for the
127 first non-blank line, otherwise return the current line. The
128 result is a cons of the filename and search string."
129 (erc-save-buffer-in-logs)
131 (with-current-buffer (find-file-noselect (erc-current-logfile))
132 (goto-char (point-max))
134 (abbreviate-file-name buffer-file-name
)
135 ;; can we get a '::' part?
136 (if (string= erc-line
(erc-prompt))
138 (goto-char (point-at-bol))
139 (when (search-backward-regexp "^[^ ]" nil t
)
140 (buffer-substring-no-properties (point-at-bol)
142 (when (search-backward erc-line nil t
)
143 (buffer-substring-no-properties (point-at-bol)
146 (defun org-irc-erc-store-link ()
147 "Store a link to the IRC log file or the session itself.
148 Depending on the variable `org-irc-link-to-logs' store either a
149 link to the log file for the current session or an irc: link to
152 (if org-irc-link-to-logs
153 (let* ((erc-line (buffer-substring-no-properties
154 (point-at-bol) (point-at-eol)))
155 (parsed-line (org-irc-erc-get-line-from-log erc-line
)))
156 (if (erc-logging-enabled nil
)
158 (org-store-link-props
160 :description
(concat "'" (org-irc-ellipsify-description
161 (cadr parsed-line
) 20)
162 "' from an IRC conversation")
163 :link
(concat "file:" (car parsed-line
) "::"
166 (error "This ERC session is not being logged")))
167 (let* ((link-text (org-irc-get-erc-link))
168 (link (org-irc-parse-link link-text
)))
171 (org-store-link-props
173 :link
(concat "irc:/" link-text
)
174 :description
(concat "irc session `" link-text
"'")
175 :server
(car (car link
))
176 :port
(or (string-to-number (cadr (pop link
))) erc-default-port
)
179 (error "Failed to create ('irc:/' style) ERC link")))))
181 (defun org-irc-get-erc-link ()
182 "Return an org compatible irc:/ link from an ERC buffer."
183 (let* ((session-port (if (numberp erc-session-port
)
184 (number-to-string erc-session-port
)
186 (link (concat erc-session-server
":" session-port
)))
188 (if (and (erc-default-target)
189 (erc-channel-p (erc-default-target))
190 (car (get-text-property (point) 'erc-data
)))
192 (let ((nick (car (get-text-property (point) 'erc-data
))))
193 (concat (erc-default-target) "/" nick
))
194 (erc-default-target)))))
196 (defun org-irc-get-current-erc-port ()
197 "Return the current port as a number.
198 Return the current port number or, if none is set, return the ERC
201 ((stringp erc-session-port
)
202 (string-to-number erc-session-port
))
203 ((numberp erc-session-port
)
208 (defun org-irc-visit-erc (link)
209 "Visit an ERC buffer based on criteria found in LINK."
212 (let* ((server (car (car link
)))
213 (port (let ((p (cadr (pop link
))))
214 (if p
(string-to-number p
) erc-default-port
)))
219 (let ((tmp-server-buf (erc-server-buffer)))
221 (with-current-buffer tmp-server-buf
223 (eq (org-irc-get-current-erc-port) port
)
224 (string= erc-session-server server
)
225 (setq server-buffer tmp-server-buf
)))))))))
227 (let ((chan-name (pop link
)))
228 ;; if we got a channel name then switch to it or join it
230 (let ((chan-buf (catch 'found
231 (dolist (x buffer-list
)
232 (if (string= (buffer-name x
) chan-name
)
233 (throw 'found x
))))))
236 (pop-to-buffer-same-window chan-buf
)
237 ;; if we got a nick, and they're in the chan,
238 ;; then start a chat with them
239 (let ((nick (pop link
)))
241 (if (member nick
(erc-get-server-nickname-list))
243 (goto-char (point-max))
244 (insert (concat nick
": ")))
245 (error "%s not found in %s" nick chan-name
)))))
247 (pop-to-buffer-same-window server-buffer
)
248 (erc-cmd-JOIN chan-name
))))
249 (pop-to-buffer-same-window server-buffer
)))
250 ;; no server match, make new connection
251 (erc-select :server server
:port port
))))
253 (defun org-irc-export (link description format
)
255 See `org-link-parameters' for details about LINK, DESCRIPTION and
257 (let ((desc (or description link
)))
259 (`html
(format "<a href=\"irc:%s\">%s</a>" link desc
))
260 (`md
(format "[%s](irc:%s)" desc link
))
266 ;; generated-autoload-file: "org-loaddefs.el"
269 ;;; org-irc.el ends here