* riece-highlight.el: Require 'derived.
[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
37 (defgroup riece-alias nil
38   "Define aliases of names"
39   :prefix "riece-"
40   :group 'riece)
41
42 (defcustom riece-alias-percent-hack-mask "*.jp"
43   "The mask of local IRC network"
44   :type 'string
45   :group 'riece-alias)
46
47 (defcustom riece-alias-enable-percent-hack t
48   "If non-nil, the target mask is abbreviated with `%'."
49   :type 'boolean
50   :group 'riece-alias)
51
52 (defcustom riece-alias-alist nil
53   "An alist mapping aliases to names."
54   :type 'list
55   :group 'riece-alias)
56
57 (defvar riece-alias-enabled nil)
58
59 (defconst riece-alias-description
60   "Define aliases of channel/user names")
61
62 (defun riece-alias-abbrev-percent-hack (string)
63   (if (string-match (concat "^#\\([^ ]+\\):"
64                             (regexp-quote riece-alias-percent-hack-mask)
65                             "\\( .+\\|$\\)")
66                     string)
67       (replace-match "%\\1\\2" nil nil string)
68     string))
69
70 (defun riece-alias-expand-percent-hack (string)
71   (if (string-match "^%\\([^ ]+\\)\\( .+\\|$\\)" string)
72       (replace-match (concat "#\\1:" riece-alias-percent-hack-mask "\\2")
73                      nil nil string)
74     string))
75
76 (defun riece-alias-abbrev-identity-string (string)
77   (if riece-alias-enable-percent-hack
78       (setq string (riece-alias-abbrev-percent-hack string)))
79   (let ((alist riece-alias-alist))
80     (catch 'done
81       (while alist
82         (if (equal (car (car alist)) string)
83             (throw 'done (cdr (car alist))))
84         (setq alist (cdr alist)))
85       string)))
86
87 (defun riece-alias-expand-identity-string (string)
88   (if riece-alias-enable-percent-hack
89       (setq string (riece-alias-expand-percent-hack string)))
90   (let ((alist riece-alias-alist))
91     (catch 'done
92       (while alist
93         (if (equal (cdr (car alist)) string)
94             (throw 'done (car (car alist))))
95         (setq alist (cdr alist)))
96       string)))
97
98 (defun riece-alias-insinuate ()
99   )
100
101 (defun riece-alias-enable ()
102   (setq riece-abbrev-identity-string-function
103         #'riece-alias-abbrev-identity-string
104         riece-expand-identity-string-function
105         #'riece-alias-expand-identity-string)
106   (riece-emit-signal 'channel-list-changed)
107   (setq riece-alias-enabled t))
108
109 (defun riece-alias-disable ()
110   (setq riece-abbrev-identity-string-function nil
111         riece-expand-identity-string-function nil)
112   (riece-emit-signal 'channel-list-changed)
113   (setq riece-alias-enabled nil))
114
115 (provide 'riece-alias)
116
117 ;;; riece-alias.el ends here