Switch to recommended form of GPLv3 permissions notice.
[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, 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 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This mostly defines stuff that cannot be worked around easily.
26
27 ;;; Code:
28
29 (require 'format-spec)
30
31 ;;;###autoload (autoload 'erc-define-minor-mode "erc-compat")
32 (defalias 'erc-define-minor-mode 'define-minor-mode)
33 (put 'erc-define-minor-mode 'edebug-form-spec 'define-minor-mode)
34
35 (defun erc-decode-coding-string (s coding-system)
36 "Decode S using CODING-SYSTEM."
37 (decode-coding-string s coding-system t))
38
39 (defun erc-encode-coding-string (s coding-system)
40 "Encode S using CODING-SYSTEM.
41 Return the same string, if the encoding operation is trivial.
42 See `erc-encoding-coding-alist'."
43 (encode-coding-string s coding-system t))
44
45 (defalias 'erc-propertize 'propertize)
46 (defalias 'erc-view-mode-enter 'view-mode-enter)
47 (defalias 'erc-function-arglist 'help-function-arglist)
48 (defalias 'erc-delete-dups 'delete-dups)
49 (defalias 'erc-replace-regexp-in-string 'replace-regexp-in-string)
50
51 (defun erc-set-write-file-functions (new-val)
52 (set (make-local-variable 'write-file-functions) new-val))
53
54 (defvar erc-emacs-build-time
55 (if (stringp emacs-build-time)
56 emacs-build-time
57 (format-time-string "%Y-%m-%d" emacs-build-time))
58 "Time at which Emacs was dumped out.")
59
60 ;; Emacs 21 and XEmacs do not have user-emacs-directory, but XEmacs
61 ;; has user-init-directory.
62 (defvar erc-user-emacs-directory
63 (cond ((boundp 'user-emacs-directory)
64 user-emacs-directory)
65 ((boundp 'user-init-directory)
66 user-init-directory)
67 (t "~/.emacs.d/"))
68 "Directory beneath which additional per-user Emacs-specific files
69 are placed.
70 Note that this should end with a directory separator.")
71
72 ;; XEmacs' `replace-match' does not replace matching subexpressions in strings.
73 (defun erc-replace-match-subexpression-in-string
74 (newtext string match subexp start &optional fixedcase literal)
75 "Replace the subexpression SUBEXP of the last match in STRING with NEWTEXT.
76 MATCH is the text which matched the subexpression (see `match-string').
77 START is the beginning position of the last match (see `match-beginning').
78 See `replace-match' for explanations of FIXEDCASE and LITERAL."
79 (cond ((featurep 'xemacs)
80 (string-match match string start)
81 (replace-match newtext fixedcase literal string))
82 (t (replace-match newtext fixedcase literal string subexp))))
83
84 (defalias 'erc-with-selected-window 'with-selected-window)
85 (defalias 'erc-cancel-timer 'cancel-timer)
86 (defalias 'erc-make-obsolete 'make-obsolete)
87 (defalias 'erc-make-obsolete-variable 'make-obsolete-variable)
88
89 ;; Provide a simpler replacement for `member-if'
90 (defun erc-member-if (predicate list)
91 "Find the first item satisfying PREDICATE in LIST.
92 Return the sublist of LIST whose car matches."
93 (let ((ptr list))
94 (catch 'found
95 (while ptr
96 (when (funcall predicate (car ptr))
97 (throw 'found ptr))
98 (setq ptr (cdr ptr))))))
99
100 ;; Provide a simpler replacement for `delete-if'
101 (defun erc-delete-if (predicate seq)
102 "Remove all items satisfying PREDICATE in SEQ.
103 This is a destructive function: it reuses the storage of SEQ
104 whenever possible."
105 ;; remove from car
106 (while (when (funcall predicate (car seq))
107 (setq seq (cdr seq))))
108 ;; remove from cdr
109 (let ((ptr seq)
110 (next (cdr seq)))
111 (while next
112 (when (funcall predicate (car next))
113 (setcdr ptr (if (consp next)
114 (cdr next)
115 nil)))
116 (setq ptr (cdr ptr))
117 (setq next (cdr ptr))))
118 seq)
119
120 ;; Provide a simpler replacement for `remove-if-not'
121 (defun erc-remove-if-not (predicate seq)
122 "Remove all items not satisfying PREDICATE in SEQ.
123 This is a non-destructive function; it makes a copy of SEQ to
124 avoid corrupting the original SEQ."
125 (let (newseq)
126 (dolist (el seq)
127 (when (funcall predicate el)
128 (setq newseq (cons el newseq))))
129 (nreverse newseq)))
130
131 ;; Copied from cl-extra.el
132 (defun erc-subseq (seq start &optional end)
133 "Return the subsequence of SEQ from START to END.
134 If END is omitted, it defaults to the length of the sequence.
135 If START or END is negative, it counts from the end."
136 (if (stringp seq) (substring seq start end)
137 (let (len)
138 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
139 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
140 (cond ((listp seq)
141 (if (> start 0) (setq seq (nthcdr start seq)))
142 (if end
143 (let ((res nil))
144 (while (>= (setq end (1- end)) start)
145 (push (pop seq) res))
146 (nreverse res))
147 (copy-sequence seq)))
148 (t
149 (or end (setq end (or len (length seq))))
150 (let ((res (make-vector (max (- end start) 0) nil))
151 (i 0))
152 (while (< start end)
153 (aset res i (aref seq start))
154 (setq i (1+ i) start (1+ start)))
155 res))))))
156
157 (provide 'erc-compat)
158
159 ;;; erc-compat.el ends here
160 ;;
161 ;; Local Variables:
162 ;; indent-tabs-mode: t
163 ;; tab-width: 8
164 ;; End:
165
166 ;; arch-tag: 8948ffe0-aff8-4ad8-a196-368ebbfd58ff