a617687938e7cff802ebd440b81498e07d30e2c5
[riece] / lisp / riece-alias.el
1 ;;; riece-alias.el --- define aliases of names
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: IRC, riece
6
7 ;; This file is part of Riece.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;; This add-on allows you to define aliases for IRC names.
25
26 ;; To use, add the following line to your ~/.riece/init.el:
27 ;; (add-to-list 'riece-addons 'riece-alias)
28
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")))
32
33 ;;; Code:
34
35 (require 'riece-identity)
36 (require 'riece-signal)
37
38 (defgroup riece-alias nil
39   "Define aliases of names"
40   :prefix "riece-"
41   :group 'riece)
42
43 (defcustom riece-alias-percent-hack-mask "*.jp"
44   "The mask of local IRC network"
45   :type 'string
46   :group 'riece-alias)
47
48 (defcustom riece-alias-enable-percent-hack t
49   "If non-nil, the target mask is abbreviated with `%'."
50   :type 'boolean
51   :group 'riece-alias)
52
53 (defcustom riece-alias-use-atmark nil
54   "If non-nil, use atmark to separate prefix and server."
55   :type 'boolean
56   :group 'riece-alias)
57
58 (defcustom riece-alias-alist nil
59   "An alist mapping aliases to names."
60   :type 'list
61   :group 'riece-alias)
62
63 (defvar riece-alias-enabled nil)
64
65 (defconst riece-alias-description
66   "Define aliases of channel/user names")
67
68 (defun riece-alias-abbrev-percent-hack (string)
69   (if (string-match (concat "^#\\([^ ]+\\):"
70                             (regexp-quote riece-alias-percent-hack-mask)
71                             "\\( .+\\|$\\)")
72                     string)
73       (replace-match "%\\1\\2" nil nil string)
74     string))
75
76 (defun riece-alias-expand-percent-hack (string)
77   (if (string-match "^%\\([^ ]+\\)\\( .+\\|$\\)" string)
78       (replace-match (concat "#\\1:" riece-alias-percent-hack-mask "\\2")
79                      nil nil string)
80     string))
81
82 (defun riece-alias-escape-atmark (string)
83   (let ((index 0))
84     (while (string-match "@" string index)
85       (setq index (1+ (match-end 0))
86             string (replace-match "@@" nil nil string)))
87     string))
88
89 (defun riece-alias-abbrev-atmark (string)
90   (if (string-match " " string)
91       (let ((prefix (substring string 0 (match-beginning 0)))
92             (server (substring string (match-end 0))))
93         (concat (riece-alias-escape-atmark prefix) "@"
94                 (riece-alias-escape-atmark server)))
95     (concat (riece-alias-escape-atmark string) "@")))
96
97 (defun riece-alias-expand-atmark (string)
98   (let ((index 0)
99         prefix
100         server
101         length)
102     (while (and (null prefix)
103                 (string-match "@+" string index))
104       (setq length (- (match-end 0) (match-beginning 0))
105             string (replace-match (make-string (/ length 2) ?@)
106                                   nil nil string)
107             index (+ (match-beginning 0) (/ length 2)))
108       (unless (zerop (% length 2))
109         (setq prefix (substring string 0 index))))
110     (setq server (substring string index)
111           index 0)
112     (if (equal server "")
113         prefix
114       (while (string-match "@@" server index)
115         (setq server (replace-match "@" nil nil server)
116               index (1- (match-end 0))))
117       (concat prefix " " server))))
118
119 (defun riece-alias-abbrev-identity-string (string)
120   (if riece-alias-enable-percent-hack
121       (setq string (riece-alias-abbrev-percent-hack string)))
122   (if riece-alias-use-atmark
123       (setq string (riece-alias-abbrev-atmark string)))
124   (let ((alist riece-alias-alist))
125     (catch 'done
126       (while alist
127         (if (equal (car (car alist)) string)
128             (throw 'done (cdr (car alist))))
129         (setq alist (cdr alist)))
130       string)))
131
132 (defun riece-alias-expand-identity-string (string)
133   (if riece-alias-enable-percent-hack
134       (setq string (riece-alias-expand-percent-hack string)))
135   (if riece-alias-use-atmark
136       (setq string (riece-alias-expand-atmark string)))
137   (let ((alist riece-alias-alist))
138     (catch 'done
139       (while alist
140         (if (equal (cdr (car alist)) string)
141             (throw 'done (car (car alist))))
142         (setq alist (cdr alist)))
143       string)))
144
145 (defun riece-alias-insinuate ()
146   )
147
148 (defun riece-alias-enable ()
149   (setq riece-abbrev-identity-string-function
150         #'riece-alias-abbrev-identity-string
151         riece-expand-identity-string-function
152         #'riece-alias-expand-identity-string)
153   (riece-emit-signal 'channel-list-changed)
154   (setq riece-alias-enabled t))
155
156 (defun riece-alias-disable ()
157   (setq riece-abbrev-identity-string-function nil
158         riece-expand-identity-string-function nil)
159   (riece-emit-signal 'channel-list-changed)
160   (setq riece-alias-enabled nil))
161
162 (provide 'riece-alias)
163
164 ;;; riece-alias.el ends here