Sync ERC 5.3 (devel) from upstream
[bpt/emacs.git] / lisp / erc / erc-compat.el
1 ;;; erc-compat.el --- ERC compatibility code for XEmacs
2
3 ;; Copyright (C) 2002, 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/ERC
7
8 ;; This file is part of GNU Emacs.
9
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, or (at your option)
13 ;; any later version.
14
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.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; This mostly defines stuff that cannot be worked around easily.
28
29 ;;; Code:
30
31 (require 'format-spec)
32
33 ;;;###autoload (autoload 'erc-define-minor-mode "erc-compat")
34 (defalias 'erc-define-minor-mode 'define-minor-mode)
35 (put 'erc-define-minor-mode 'edebug-form-spec 'define-minor-mode)
36
37 (defun erc-decode-coding-string (s coding-system)
38 "Decode S using CODING-SYSTEM."
39 (decode-coding-string s coding-system t))
40
41 (defun erc-encode-coding-string (s coding-system)
42 "Encode S using CODING-SYSTEM.
43 Return the same string, if the encoding operation is trivial.
44 See `erc-encoding-coding-alist'."
45 (encode-coding-string s coding-system t))
46
47 (defalias 'erc-propertize 'propertize)
48 (defalias 'erc-view-mode-enter 'view-mode-enter)
49 (defalias 'erc-function-arglist 'help-function-arglist)
50 (defalias 'erc-delete-dups 'delete-dups)
51 (defalias 'erc-replace-regexp-in-string 'replace-regexp-in-string)
52
53 (defvar erc-emacs-build-time
54 (if (stringp emacs-build-time)
55 emacs-build-time
56 (format-time-string "%Y-%m-%d" emacs-build-time))
57 "Time at which Emacs was dumped out.")
58
59 ;; Emacs 21 and XEmacs do not have user-emacs-directory, but XEmacs
60 ;; has user-init-directory.
61 (defvar erc-user-emacs-directory
62 (cond ((boundp 'user-emacs-directory)
63 user-emacs-directory)
64 ((boundp 'user-init-directory)
65 user-init-directory)
66 (t "~/.emacs.d/"))
67 "Directory beneath which additional per-user Emacs-specific files
68 are placed.
69 Note that this should end with a directory separator.")
70
71 ;; XEmacs' `replace-match' does not replace matching subexpressions in strings.
72 (defun erc-replace-match-subexpression-in-string
73 (newtext string match subexp start &optional fixedcase literal)
74 "Replace the subexpression SUBEXP of the last match in STRING with NEWTEXT.
75 MATCH is the text which matched the subexpression (see `match-string').
76 START is the beginning position of the last match (see `match-beginning').
77 See `replace-match' for explanations of FIXEDCASE and LITERAL."
78 (cond ((featurep 'xemacs)
79 (string-match match string start)
80 (replace-match newtext fixedcase literal string))
81 (t (replace-match newtext fixedcase literal string subexp))))
82
83 (defalias 'erc-with-selected-window 'with-selected-window)
84 (defalias 'erc-cancel-timer 'cancel-timer)
85 (defalias 'erc-make-obsolete 'make-obsolete)
86 (defalias 'erc-make-obsolete-variable 'make-obsolete-variable)
87
88 ;; Provde an equivalent of `assert', based on the code from cl-macs.el
89 (defun erc-const-expr-p (x)
90 (cond ((consp x)
91 (or (eq (car x) 'quote)
92 (and (memq (car x) '(function function*))
93 (or (symbolp (nth 1 x))
94 (and (eq (and (consp (nth 1 x))
95 (car (nth 1 x))) 'lambda) 'func)))))
96 ((symbolp x) (and (memq x '(nil t)) t))
97 (t t)))
98
99 (put 'erc-assertion-failed 'error-conditions '(error))
100 (put 'erc-assertion-failed 'error-message "Assertion failed")
101
102 (defun erc-list* (arg &rest rest)
103 "Return a new list with specified args as elements, cons'd to last arg.
104 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
105 `(cons A (cons B (cons C D)))'."
106 (cond ((not rest) arg)
107 ((not (cdr rest)) (cons arg (car rest)))
108 (t (let* ((n (length rest))
109 (copy (copy-sequence rest))
110 (last (nthcdr (- n 2) copy)))
111 (setcdr last (car (cdr last)))
112 (cons arg copy)))))
113
114 (defmacro erc-assert (form &optional show-args string &rest args)
115 "Verify that FORM returns non-nil; signal an error if not.
116 Second arg SHOW-ARGS means to include arguments of FORM in message.
117 Other args STRING and ARGS... are arguments to be passed to `error'.
118 They are not evaluated unless the assertion fails. If STRING is
119 omitted, a default message listing FORM itself is used."
120 (let ((sargs
121 (and show-args
122 (delq nil (mapcar
123 (function
124 (lambda (x)
125 (and (not (erc-const-expr-p x)) x)))
126 (cdr form))))))
127 (list 'progn
128 (list 'or form
129 (if string
130 (erc-list* 'error string (append sargs args))
131 (list 'signal '(quote erc-assertion-failed)
132 (erc-list* 'list (list 'quote form) sargs))))
133 nil)))
134
135 ;; Provide a simpler replacement for `member-if'
136 (defun erc-member-if (predicate list)
137 "Find the first item satisfying PREDICATE in LIST.
138 Return the sublist of LIST whose car matches."
139 (let ((ptr list))
140 (catch 'found
141 (while ptr
142 (when (funcall predicate (car ptr))
143 (throw 'found ptr))
144 (setq ptr (cdr ptr))))))
145
146 ;; Provide a simpler replacement for `delete-if'
147 (defun erc-delete-if (predicate seq)
148 "Remove all items satisfying PREDICATE in SEQ.
149 This is a destructive function: it reuses the storage of SEQ
150 whenever possible."
151 ;; remove from car
152 (while (when (funcall predicate (car seq))
153 (setq seq (cdr seq))))
154 ;; remove from cdr
155 (let ((ptr seq)
156 (next (cdr seq)))
157 (while next
158 (when (funcall predicate (car next))
159 (setcdr ptr (if (consp next)
160 (cdr next)
161 nil)))
162 (setq ptr (cdr ptr))
163 (setq next (cdr ptr))))
164 seq)
165
166 ;; Provide a simpler replacement for `remove-if-not'
167 (defun erc-remove-if-not (predicate seq)
168 "Remove all items not satisfying PREDICATE in SEQ.
169 This is a non-destructive function; it makes a copy of SEQ to
170 avoid corrupting the original SEQ."
171 (let (newseq)
172 (dolist (el seq)
173 (when (funcall predicate el)
174 (setq newseq (cons el newseq))))
175 (nreverse newseq)))
176
177 ;; Copied from cl-extra.el
178 (defun erc-subseq (seq start &optional end)
179 "Return the subsequence of SEQ from START to END.
180 If END is omitted, it defaults to the length of the sequence.
181 If START or END is negative, it counts from the end."
182 (if (stringp seq) (substring seq start end)
183 (let (len)
184 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
185 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
186 (cond ((listp seq)
187 (if (> start 0) (setq seq (nthcdr start seq)))
188 (if end
189 (let ((res nil))
190 (while (>= (setq end (1- end)) start)
191 (push (pop seq) res))
192 (nreverse res))
193 (copy-sequence seq)))
194 (t
195 (or end (setq end (or len (length seq))))
196 (let ((res (make-vector (max (- end start) 0) nil))
197 (i 0))
198 (while (< start end)
199 (aset res i (aref seq start))
200 (setq i (1+ i) start (1+ start)))
201 res))))))
202
203 (provide 'erc-compat)
204
205 ;;; erc-compat.el ends here
206 ;;
207 ;; Local Variables:
208 ;; indent-tabs-mode: t
209 ;; tab-width: 8
210 ;; End:
211
212 ;; arch-tag: 8948ffe0-aff8-4ad8-a196-368ebbfd58ff