]> code.delx.au - gnu-emacs/blob - lisp/erc/erc-compat.el
Merge from emacs--devo--0
[gnu-emacs] / lisp / erc / erc-compat.el
1 ;;; erc-compat.el --- ERC compatibility code for XEmacs
2
3 ;; Copyright (C) 2002, 2003, 2005, 2006, 2007, 2008 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 (defun erc-set-write-file-functions (new-val)
54 (set (make-local-variable 'write-file-functions) new-val))
55
56 (defvar erc-emacs-build-time
57 (if (stringp emacs-build-time)
58 emacs-build-time
59 (format-time-string "%Y-%m-%d" emacs-build-time))
60 "Time at which Emacs was dumped out.")
61
62 ;; Emacs 21 and XEmacs do not have user-emacs-directory, but XEmacs
63 ;; has user-init-directory.
64 (defvar erc-user-emacs-directory
65 (cond ((boundp 'user-emacs-directory)
66 user-emacs-directory)
67 ((boundp 'user-init-directory)
68 user-init-directory)
69 (t "~/.emacs.d/"))
70 "Directory beneath which additional per-user Emacs-specific files
71 are placed.
72 Note that this should end with a directory separator.")
73
74 ;; XEmacs' `replace-match' does not replace matching subexpressions in strings.
75 (defun erc-replace-match-subexpression-in-string
76 (newtext string match subexp start &optional fixedcase literal)
77 "Replace the subexpression SUBEXP of the last match in STRING with NEWTEXT.
78 MATCH is the text which matched the subexpression (see `match-string').
79 START is the beginning position of the last match (see `match-beginning').
80 See `replace-match' for explanations of FIXEDCASE and LITERAL."
81 (cond ((featurep 'xemacs)
82 (string-match match string start)
83 (replace-match newtext fixedcase literal string))
84 (t (replace-match newtext fixedcase literal string subexp))))
85
86 (defalias 'erc-with-selected-window 'with-selected-window)
87 (defalias 'erc-cancel-timer 'cancel-timer)
88 (defalias 'erc-make-obsolete 'make-obsolete)
89 (defalias 'erc-make-obsolete-variable 'make-obsolete-variable)
90
91 ;; Provde an equivalent of `assert', based on the code from cl-macs.el
92 (defun erc-const-expr-p (x)
93 (cond ((consp x)
94 (or (eq (car x) 'quote)
95 (and (memq (car x) '(function function*))
96 (or (symbolp (nth 1 x))
97 (and (eq (and (consp (nth 1 x))
98 (car (nth 1 x))) 'lambda) 'func)))))
99 ((symbolp x) (and (memq x '(nil t)) t))
100 (t t)))
101
102 (put 'erc-assertion-failed 'error-conditions '(error))
103 (put 'erc-assertion-failed 'error-message "Assertion failed")
104
105 (defun erc-list* (arg &rest rest)
106 "Return a new list with specified args as elements, cons'd to last arg.
107 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
108 `(cons A (cons B (cons C D)))'."
109 (cond ((not rest) arg)
110 ((not (cdr rest)) (cons arg (car rest)))
111 (t (let* ((n (length rest))
112 (copy (copy-sequence rest))
113 (last (nthcdr (- n 2) copy)))
114 (setcdr last (car (cdr last)))
115 (cons arg copy)))))
116
117 (defmacro erc-assert (form &optional show-args string &rest args)
118 "Verify that FORM returns non-nil; signal an error if not.
119 Second arg SHOW-ARGS means to include arguments of FORM in message.
120 Other args STRING and ARGS... are arguments to be passed to `error'.
121 They are not evaluated unless the assertion fails. If STRING is
122 omitted, a default message listing FORM itself is used."
123 (let ((sargs
124 (and show-args
125 (delq nil (mapcar
126 (function
127 (lambda (x)
128 (and (not (erc-const-expr-p x)) x)))
129 (cdr form))))))
130 (list 'progn
131 (list 'or form
132 (if string
133 (erc-list* 'error string (append sargs args))
134 (list 'signal '(quote erc-assertion-failed)
135 (erc-list* 'list (list 'quote form) sargs))))
136 nil)))
137
138 ;; Provide a simpler replacement for `member-if'
139 (defun erc-member-if (predicate list)
140 "Find the first item satisfying PREDICATE in LIST.
141 Return the sublist of LIST whose car matches."
142 (let ((ptr list))
143 (catch 'found
144 (while ptr
145 (when (funcall predicate (car ptr))
146 (throw 'found ptr))
147 (setq ptr (cdr ptr))))))
148
149 ;; Provide a simpler replacement for `delete-if'
150 (defun erc-delete-if (predicate seq)
151 "Remove all items satisfying PREDICATE in SEQ.
152 This is a destructive function: it reuses the storage of SEQ
153 whenever possible."
154 ;; remove from car
155 (while (when (funcall predicate (car seq))
156 (setq seq (cdr seq))))
157 ;; remove from cdr
158 (let ((ptr seq)
159 (next (cdr seq)))
160 (while next
161 (when (funcall predicate (car next))
162 (setcdr ptr (if (consp next)
163 (cdr next)
164 nil)))
165 (setq ptr (cdr ptr))
166 (setq next (cdr ptr))))
167 seq)
168
169 ;; Provide a simpler replacement for `remove-if-not'
170 (defun erc-remove-if-not (predicate seq)
171 "Remove all items not satisfying PREDICATE in SEQ.
172 This is a non-destructive function; it makes a copy of SEQ to
173 avoid corrupting the original SEQ."
174 (let (newseq)
175 (dolist (el seq)
176 (when (funcall predicate el)
177 (setq newseq (cons el newseq))))
178 (nreverse newseq)))
179
180 ;; Copied from cl-extra.el
181 (defun erc-subseq (seq start &optional end)
182 "Return the subsequence of SEQ from START to END.
183 If END is omitted, it defaults to the length of the sequence.
184 If START or END is negative, it counts from the end."
185 (if (stringp seq) (substring seq start end)
186 (let (len)
187 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
188 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
189 (cond ((listp seq)
190 (if (> start 0) (setq seq (nthcdr start seq)))
191 (if end
192 (let ((res nil))
193 (while (>= (setq end (1- end)) start)
194 (push (pop seq) res))
195 (nreverse res))
196 (copy-sequence seq)))
197 (t
198 (or end (setq end (or len (length seq))))
199 (let ((res (make-vector (max (- end start) 0) nil))
200 (i 0))
201 (while (< start end)
202 (aset res i (aref seq start))
203 (setq i (1+ i) start (1+ start)))
204 res))))))
205
206 (provide 'erc-compat)
207
208 ;;; erc-compat.el ends here
209 ;;
210 ;; Local Variables:
211 ;; indent-tabs-mode: t
212 ;; tab-width: 8
213 ;; End:
214
215 ;; arch-tag: 8948ffe0-aff8-4ad8-a196-368ebbfd58ff