Dealing with Emacs 19.34.
[elisp/liece.git] / lisp / liece-compat.el
1 ;;; liece-compat.el --- Provide compatibility for various emacsen.
2 ;; Copyright (C) 1998-2000 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Revised: 1999-12-19
7 ;; Keywords: IRC, liece, APEL
8
9 ;; This file is part of Liece.
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26
27 ;;; Commentary:
28 ;; 
29
30 ;;; Code:
31
32 (eval-when-compile (require 'cl))
33
34 (require 'pcustom)
35 (require 'wid-edit)
36
37 (defalias 'liece-widget-convert-button 'widget-convert-button)
38 (defalias 'liece-widget-button-click 'widget-button-click)
39
40 (defun-maybe region-active-p ()
41   "Return non-nil if the region is active.
42 If `zmacs-regions' is true, this is equivalent to `region-exists-p'.
43 Otherwise, this function always returns false.
44 \[XEmacs emulating function]"
45   (static-if (and (boundp 'transient-mark-mode) (boundp 'mark-active))
46       (and transient-mark-mode mark-active)))
47
48 (defun liece-map-overlays (function)
49   "Map FUNCTION over the extents which overlap the current buffer."
50   (let* ((overlayss (overlay-lists))
51          (buffer-read-only nil)
52          (overlays (delq nil (nconc (car overlayss) (cdr overlayss)))))
53     (dolist (overlay overlays)
54       (funcall function overlay))))
55
56 (defun liece-kill-all-overlays ()
57   "Delete all overlays in the current buffer."
58   (liece-map-overlays #'delete-overlay))
59
60 (defmacro liece-get-buffer-window (buffer)
61   "Traverse all frames and return a window currently displaying BUFFER."
62   `(get-buffer-window ,buffer t))
63
64 (static-if (fboundp 'window-displayed-height)
65     (defalias 'liece-window-height 'window-displayed-height)
66   (defalias 'liece-window-height 'window-height))
67
68 (defalias 'liece-mode-line-buffer-identification 'identity)
69
70 (defun liece-suppress-mode-line-format ()
71   "Remove unnecessary information from `mode-line-format'."
72   (let ((value (rassq 'mode-line-modified mode-line-format)))
73     (if value
74         (setq mode-line-format (delq value (copy-sequence mode-line-format)))
75       mode-line-format)))
76
77 (defun liece-locate-data-directory (name &optional dir-list)
78   "Locate a directory in a search path DIR-LIST (a list of directories)."
79   (let ((dir-list
80          (or dir-list
81              (cons data-directory
82                    (mapcar (lambda (path) (concat path "etc/"))
83                            load-path))))
84         dir)
85     (while dir-list
86       (if (and (car dir-list)
87                (file-directory-p
88                 (setq dir (concat
89                            (file-name-directory
90                             (directory-file-name (car dir-list)))
91                            name "/"))))
92           (setq dir-list nil)
93         (setq dir-list (cdr dir-list))))
94     dir))
95
96 (defvar-maybe completion-display-completion-list-function
97   'display-completion-list)
98   
99 (defalias-maybe 'easy-menu-add-item 'ignore)
100   
101 ;; from XEmacs's minibuf.el
102 (defun-maybe temp-minibuffer-message (m)
103   (let ((savemax (point-max)))
104     (save-excursion
105       (goto-char (point-max))
106       (message nil)
107       (insert m))
108     (let ((inhibit-quit t))
109       (sit-for 2)
110       (delete-region savemax (point-max)))))
111
112 (defvar liece-read-passwd nil)
113 (defun liece-read-passwd (prompt)
114   (if (not liece-read-passwd)
115       (if (functionp 'read-passwd)
116           (setq liece-read-passwd 'read-passwd)
117         (if (load "passwd" t)
118             (setq liece-read-passwd 'read-passwd)
119           (autoload 'ange-ftp-read-passwd "ange-ftp")
120           (setq liece-read-passwd 'ange-ftp-read-passwd))))
121   (funcall liece-read-passwd prompt))
122
123 ;; from XEmacs's subr.el
124 (defun-maybe replace-in-string (str regexp newtext &optional literal)
125   "Replace all matches in STR for REGEXP with NEWTEXT string,
126  and returns the new string.
127 Optional LITERAL non-nil means do a literal replacement.
128 Otherwise treat \\ in NEWTEXT string as special:
129   \\& means substitute original matched text,
130   \\N means substitute match for \(...\) number N,
131   \\\\ means insert one \\."
132   (let ((rtn-str "")
133         (start 0)
134         (special)
135         match prev-start)
136     (while (setq match (string-match regexp str start))
137       (setq prev-start start
138             start (match-end 0)
139             rtn-str
140             (concat
141              rtn-str
142              (substring str prev-start match)
143              (cond (literal newtext)
144                    (t (mapconcat
145                        (lambda (c)
146                          (if special
147                              (progn
148                                (setq special nil)
149                                (cond ((eq c ?\\) "\\")
150                                      ((eq c ?&)
151                                       (substring str
152                                                  (match-beginning 0)
153                                                  (match-end 0)))
154                                      ((and (>= c ?0) (<= c ?9))
155                                       (if (> c (+ ?0 (length
156                                                       (match-data))))
157                                           ;; Invalid match num
158                                           (error "Invalid match num: %c" c)
159                                         (setq c (- c ?0))
160                                         (substring str
161                                                    (match-beginning c)
162                                                    (match-end c))))
163                                      (t (char-to-string c))))
164                            (if (eq c ?\\) (progn (setq special t) nil)
165                              (char-to-string c))))
166                        newtext ""))))))
167     (concat rtn-str (substring str start))))
168   
169 (provide 'liece-compat)
170
171 ;;; liece-compat.el ends here