1 ;;; riece-alias.el --- define aliases of names
2 ;; Copyright (C) 1998-2003 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: IRC, riece
7 ;; This file is part of Riece.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;; This add-on allows you to define aliases for IRC names.
26 ;; To use, add the following line to your ~/.riece/init.el:
27 ;; (add-to-list 'riece-addons 'riece-alias)
29 ;; For example, if you want to define an alias `#l' for `#Liece', you
30 ;; can customize riece-alias-alist as follows:
31 ;; (setq riece-alias-alist '(("#Liece" . "#l")))
35 (require 'riece-identity)
36 (require 'riece-signal)
38 (defgroup riece-alias nil
39 "Define aliases of names"
43 (defcustom riece-alias-percent-hack-mask "*.jp"
44 "The mask of local IRC network"
48 (defcustom riece-alias-enable-percent-hack t
49 "If non-nil, the target mask is abbreviated with `%'."
53 (defcustom riece-alias-alternate-separator "@"
54 "A string to separate prefix and server."
55 :type '(choice (const nil) string)
58 (defcustom riece-alias-alist nil
59 "An alist mapping aliases to names."
63 (defvar riece-alias-enabled nil)
65 (defconst riece-alias-description
66 "Define aliases of channel/user names")
68 (defun riece-alias-abbrev-percent-hack (string)
69 (if (string-match (concat "^#\\([^ ]+\\):"
70 (regexp-quote riece-alias-percent-hack-mask)
73 (replace-match "%\\1\\2" nil nil string)
76 (defun riece-alias-expand-percent-hack (string)
77 (if (string-match "^%\\([^ ]+\\)\\( .+\\|$\\)" string)
78 (replace-match (concat "#\\1:" riece-alias-percent-hack-mask "\\2")
82 (defun riece-alias-escape-alternate-separator (string)
84 (while (string-match (regexp-quote riece-alias-alternate-separator)
86 (setq index (1+ (match-end 0))
87 string (replace-match (concat riece-alias-alternate-separator
88 riece-alias-alternate-separator)
92 (defun riece-alias-abbrev-alternate-separator (string)
93 (if (string-match " " string)
94 (let ((prefix (substring string 0 (match-beginning 0)))
95 (server (substring string (match-end 0))))
96 (concat (riece-alias-escape-alternate-separator prefix)
97 riece-alias-alternate-separator
98 (riece-alias-escape-alternate-separator server)))
99 (riece-alias-escape-alternate-separator string)))
101 (defun riece-alias-expand-alternate-separator (string)
105 (while (and (null prefix)
107 (concat (regexp-quote riece-alias-alternate-separator)
108 (regexp-quote riece-alias-alternate-separator)
110 (regexp-quote riece-alias-alternate-separator)
113 (if (match-beginning 1) ;found a separator
114 (setq prefix (substring string 0 (match-beginning 1))
116 (setq string (replace-match riece-alias-alternate-separator
118 index (- (match-end 0)
119 (length riece-alias-alternate-separator)))))
122 (setq server (substring string index)
124 (if (equal server "")
125 (while (string-match (regexp-quote
126 (concat riece-alias-alternate-separator
127 riece-alias-alternate-separator))
129 (setq server (replace-match riece-alias-alternate-separator
131 index (- (match-end 0)
132 (length riece-alias-alternate-separator))))
133 (concat prefix " " server)))))
135 (defun riece-alias-abbrev-identity-string (string)
136 (if riece-alias-enable-percent-hack
137 (setq string (riece-alias-abbrev-percent-hack string)))
138 (if riece-alias-alternate-separator
139 (setq string (riece-alias-abbrev-alternate-separator string)))
140 (let ((alist riece-alias-alist))
142 (if (equal (car (car alist)) string)
143 (setq string (cdr (car alist))
145 (setq alist (cdr alist)))))
146 (copy-sequence string))
148 (defun riece-alias-expand-identity-string (string)
149 (let ((alist riece-alias-alist))
151 (if (equal (cdr (car alist)) string)
152 (setq string (car (car alist))
154 (setq alist (cdr alist)))))
155 (if riece-alias-alternate-separator
156 (setq string (riece-alias-expand-alternate-separator string)))
157 (if riece-alias-enable-percent-hack
158 (setq string (riece-alias-expand-percent-hack string)))
159 (copy-sequence string))
161 (defun riece-alias-insinuate ()
164 (defun riece-alias-enable ()
165 (setq riece-abbrev-identity-string-function
166 #'riece-alias-abbrev-identity-string
167 riece-expand-identity-string-function
168 #'riece-alias-expand-identity-string)
169 (riece-emit-signal 'channel-list-changed)
170 (setq riece-alias-enabled t))
172 (defun riece-alias-disable ()
173 (setq riece-abbrev-identity-string-function nil
174 riece-expand-identity-string-function nil)
175 (riece-emit-signal 'channel-list-changed)
176 (setq riece-alias-enabled nil))
178 (provide 'riece-alias)
180 ;;; riece-alias.el ends here