c55d0458e49676d01dd281ab52e401e418928128
[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-alternate-separator nil
54   "A string to separate prefix and server."
55   :type '(choice (const nil) string)
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-alternate-separator (string)
83   (let ((index 0))
84     (while (string-match (regexp-quote riece-alias-alternate-separator)
85                          string index)
86       (setq index (1+ (match-end 0))
87             string (replace-match (concat riece-alias-alternate-separator
88                                           riece-alias-alternate-separator)
89                                   nil t string)))
90     string))
91
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)))
100
101 (defun riece-alias-expand-alternate-separator (string)
102   (let ((index 0)
103         prefix
104         server
105         length)
106     (while (and (null prefix)
107                 (string-match
108                  (concat "\\("
109                          (regexp-quote riece-alias-alternate-separator)
110                          "\\)+")
111                  string index))
112       (setq length (/ (- (match-end 0) (match-beginning 0))
113                       (length riece-alias-alternate-separator))
114             string (replace-match
115                     (mapconcat #'identity
116                                (make-list (/ length 2)
117                                           riece-alias-alternate-separator)
118                                "")
119                     nil t string)
120             index (+ (match-beginning 0)
121                      (* (/ length 2)
122                         (length riece-alias-alternate-separator))))
123       (unless (zerop (% length 2))
124         (setq prefix (substring string 0 index))))
125     (if (null prefix)
126         string
127       (setq server (substring string index)
128             index 0)
129       (if (equal server "")
130           (while (string-match (regexp-quote
131                                 (concat riece-alias-alternate-separator
132                                         riece-alias-alternate-separator))
133                                server index)
134             (setq server (replace-match
135                           riece-alias-alternate-separator
136                           nil t server)
137                   index (1- (match-end 0))))
138         (concat prefix " " server)))))
139
140 (defun riece-alias-abbrev-identity-string (string)
141   (if riece-alias-enable-percent-hack
142       (setq string (riece-alias-abbrev-percent-hack string)))
143   (if riece-alias-alternate-separator
144       (setq string (riece-alias-abbrev-alternate-separator string)))
145   (let ((alist riece-alias-alist))
146     (catch 'done
147       (while alist
148         (if (equal (car (car alist)) string)
149             (throw 'done (cdr (car alist))))
150         (setq alist (cdr alist)))
151       string)))
152
153 (defun riece-alias-expand-identity-string (string)
154   (if riece-alias-enable-percent-hack
155       (setq string (riece-alias-expand-percent-hack string)))
156   (if riece-alias-alternate-separator
157       (setq string (riece-alias-expand-alternate-separator string)))
158   (let ((alist riece-alias-alist))
159     (catch 'done
160       (while alist
161         (if (equal (cdr (car alist)) string)
162             (throw 'done (car (car alist))))
163         (setq alist (cdr alist)))
164       string)))
165
166 (defun riece-alias-insinuate ()
167   )
168
169 (defun riece-alias-enable ()
170   (setq riece-abbrev-identity-string-function
171         #'riece-alias-abbrev-identity-string
172         riece-expand-identity-string-function
173         #'riece-alias-expand-identity-string)
174   (riece-emit-signal 'channel-list-changed)
175   (setq riece-alias-enabled t))
176
177 (defun riece-alias-disable ()
178   (setq riece-abbrev-identity-string-function nil
179         riece-expand-identity-string-function nil)
180   (riece-emit-signal 'channel-list-changed)
181   (setq riece-alias-enabled nil))
182
183 (provide 'riece-alias)
184
185 ;;; riece-alias.el ends here