Simplify.
[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
36 (eval-when-compile (require 'wid-edit))
37
38 (eval-and-compile (autoload 'widget-convert-button "wid-edit"))
39
40 (defalias 'liece-widget-convert-button 'widget-convert-button)
41 (defalias 'liece-widget-button-click 'widget-button-click)
42
43 (defun-maybe region-active-p ()
44   "Return non-nil if the region is active.
45 If `zmacs-regions' is true, this is equivalent to `region-exists-p'.
46 Otherwise, this function always returns false.
47 \[XEmacs emulating function]"
48   (static-if (and (boundp 'transient-mark-mode) (boundp 'mark-active))
49       (and transient-mark-mode mark-active)))
50
51 (defun liece-map-overlays (function)
52   "Map FUNCTION over the extents which overlap the current buffer."
53   (let* ((overlayss (overlay-lists))
54          (buffer-read-only nil)
55          (overlays (delq nil (nconc (car overlayss) (cdr overlayss)))))
56     (dolist (overlay overlays)
57       (funcall function overlay))))
58
59 (defun liece-kill-all-overlays ()
60   "Delete all overlays in the current buffer."
61   (liece-map-overlays #'delete-overlay))
62
63 (defmacro liece-get-buffer-window (buffer)
64   "Traverse all frames and return a window currently displaying BUFFER."
65   `(get-buffer-window ,buffer t))
66
67 (static-if (fboundp 'window-displayed-height)
68     (defalias 'liece-window-height 'window-displayed-height)
69   (defalias 'liece-window-height 'window-height))
70
71 (defalias 'liece-mode-line-buffer-identification 'identity)
72
73 (defun liece-suppress-mode-line-format ()
74   "Remove unnecessary information from `mode-line-format'."
75   (let ((value (rassq 'mode-line-modified mode-line-format)))
76     (if value
77         (setq mode-line-format (delq value (copy-sequence mode-line-format)))
78       mode-line-format)))
79
80 (defun liece-locate-data-directory (name &optional dir-list)
81   "Locate a directory in a search path DIR-LIST (a list of directories)."
82   (let ((dir-list
83          (or dir-list
84              (cons data-directory
85                    (mapcar (lambda (path) (concat path "etc/"))
86                            load-path))))
87         dir)
88     (while dir-list
89       (if (and (car dir-list)
90                (file-directory-p
91                 (setq dir (concat
92                            (file-name-directory
93                             (directory-file-name (car dir-list)))
94                            name "/"))))
95           (setq dir-list nil)
96         (setq dir-list (cdr dir-list))))
97     dir))
98
99 (defvar-maybe completion-display-completion-list-function
100   'display-completion-list)
101   
102 (defalias-maybe 'easy-menu-add-item 'ignore)
103   
104 ;; from XEmacs's minibuf.el
105 (defun-maybe temp-minibuffer-message (m)
106   (let ((savemax (point-max)))
107     (save-excursion
108       (goto-char (point-max))
109       (message nil)
110       (insert m))
111     (let ((inhibit-quit t))
112       (sit-for 2)
113       (delete-region savemax (point-max)))))
114
115 ;; from XEmacs's subr.el
116 (defun-maybe replace-in-string (str regexp newtext &optional literal)
117   "Replace all matches in STR for REGEXP with NEWTEXT string,
118  and returns the new string.
119 Optional LITERAL non-nil means do a literal replacement.
120 Otherwise treat \\ in NEWTEXT string as special:
121   \\& means substitute original matched text,
122   \\N means substitute match for \(...\) number N,
123   \\\\ means insert one \\."
124   (let ((rtn-str "")
125         (start 0)
126         (special)
127         match prev-start)
128     (while (setq match (string-match regexp str start))
129       (setq prev-start start
130             start (match-end 0)
131             rtn-str
132             (concat
133              rtn-str
134              (substring str prev-start match)
135              (cond (literal newtext)
136                    (t (mapconcat
137                        (lambda (c)
138                          (if special
139                              (progn
140                                (setq special nil)
141                                (cond ((eq c ?\\) "\\")
142                                      ((eq c ?&)
143                                       (substring str
144                                                  (match-beginning 0)
145                                                  (match-end 0)))
146                                      ((and (>= c ?0) (<= c ?9))
147                                       (if (> c (+ ?0 (length
148                                                       (match-data))))
149                                           ;; Invalid match num
150                                           (error "Invalid match num: %c" c)
151                                         (setq c (- c ?0))
152                                         (substring str
153                                                    (match-beginning c)
154                                                    (match-end c))))
155                                      (t (char-to-string c))))
156                            (if (eq c ?\\) (progn (setq special t) nil)
157                              (char-to-string c))))
158                        newtext ""))))))
159     (concat rtn-str (substring str start))))
160   
161 (provide 'liece-compat)
162
163 ;;; liece-compat.el ends here