tm 7.83.
[elisp/apel.git] / emu-18.el
1 ;;; emu-18.el --- Emacs 19.* emulation module for Emacs 18.*
2
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: emu-18.el,v 7.22 1996/09/02 14:34:00 morioka Exp $
7 ;; Keywords: emulation, compatibility
8
9 ;; This file is part of tl (Tiny Library).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 ;;; @ for EMACS 18.55
29 ;;;
30
31 (defvar buffer-undo-list nil)
32
33
34 ;;; @ hook
35 ;;;
36
37 ;; These function are imported from Emacs 19.28.
38 (defun add-hook (hook function &optional append)
39   "Add to the value of HOOK the function FUNCTION.
40 FUNCTION is not added if already present.
41 FUNCTION is added (if necessary) at the beginning of the hook list
42 unless the optional argument APPEND is non-nil, in which case
43 FUNCTION is added at the end.
44  
45 HOOK should be a symbol, and FUNCTION may be any valid function.  If
46 HOOK is void, it is first set to nil.  If HOOK's value is a single
47 function, it is changed to a list of functions.
48 \[emu-18.el; Emacs 19 emulating function]"
49   (or (boundp hook)
50       (set hook nil)
51       )
52   ;; If the hook value is a single function, turn it into a list.
53   (let ((old (symbol-value hook)))
54     (if (or (not (listp old))
55             (eq (car old) 'lambda))
56         (set hook (list old))
57       ))
58   (or (if (consp function)
59           ;; Clever way to tell whether a given lambda-expression
60           ;; is equal to anything in the hook.
61           (let ((tail (assoc (cdr function) (symbol-value hook))))
62             (equal function tail)
63             )
64         (memq function (symbol-value hook))
65         )
66       (set hook 
67            (if append
68                (nconc (symbol-value hook) (list function))
69              (cons function (symbol-value hook))
70              ))
71       ))
72
73 (defun remove-hook (hook function)
74   "Remove from the value of HOOK the function FUNCTION.
75 HOOK should be a symbol, and FUNCTION may be any valid function.  If
76 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
77 list of hooks to run in HOOK, then nothing is done.  See `add-hook'.
78 \[emu-18.el; Emacs 19 emulating function]"
79   (if (or (not (boundp hook))           ;unbound symbol, or
80           (null (symbol-value hook))    ;value is nil, or
81           (null function))              ;function is nil, then
82       nil                               ;Do nothing.
83     (let ((hook-value (symbol-value hook)))
84       (if (consp hook-value)
85           (setq hook-value (delete function hook-value))
86         (if (equal hook-value function)
87             (setq hook-value nil)
88           ))
89       (set hook hook-value)
90       )))
91
92
93 ;;; @ list
94 ;;;
95
96 (defun member (elt list)
97   "Return non-nil if ELT is an element of LIST.  Comparison done with EQUAL.
98 The value is actually the tail of LIST whose car is ELT.
99 \[emu-18.el; Emacs 19 emulating function]"
100   (while (and list (not (equal elt (car list))))
101     (setq list (cdr list)))
102   list)
103
104 (defun delete (elt list)
105   "Delete by side effect any occurrences of ELT as a member of LIST.
106 The modified LIST is returned.  Comparison is done with `equal'.
107 If the first member of LIST is ELT, deleting it is not a side effect;
108 it is simply using a different list.
109 Therefore, write `(setq foo (delete element foo))'
110 to be sure of changing the value of `foo'.
111 \[emu-18.el; Emacs 19 emulating function]"
112   (if (equal elt (car list))
113       (cdr list)
114     (let ((rest list)
115           (rrest (cdr list))
116           )
117       (while (and rrest (not (equal elt (car rrest))))
118         (setq rest rrest
119               rrest (cdr rrest))
120         )
121       (rplacd rest (cdr rrest))
122       list)))
123
124
125 ;;; @ function
126 ;;;
127
128 (defun defalias (sym newdef)
129   "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.
130 Associates the function with the current load file, if any.
131 \[emu-18.el; Emacs 19 emulating function]"
132   (fset sym newdef)
133   )
134
135 (defun byte-code-function-p (exp)
136   "T if OBJECT is a byte-compiled function object.
137 \[emu-18.el; Emacs 19 emulating function]"
138   (and (consp exp)
139        (let* ((rest (cdr (cdr exp))) elt)
140          (if (stringp (car rest))
141              (setq rest (cdr rest))
142            )
143          (catch 'tag
144            (while rest
145              (setq elt (car rest))
146              (if (and (consp elt)(eq (car elt) 'byte-code))
147                  (throw 'tag t)
148                )
149              (setq rest (cdr rest))
150              ))
151          )))
152
153
154 ;;; @ directory
155 ;;;
156
157 (defun make-directory-internal (dirname)
158   "Create a directory. One argument, a file name string.
159 \[emu-18.el; Emacs 19 emulating function]"
160   (if (file-exists-p dirname)
161       (error "Creating directory: %s is already exist" dirname)
162     (if (not (= (call-process "mkdir" nil nil nil dirname) 0))
163         (error "Creating directory: no such file or directory, %s" dirname)
164       )))
165
166 (defun make-directory (dir &optional parents)
167   "Create the directory DIR and any nonexistent parent dirs.
168 The second (optional) argument PARENTS says whether
169 to create parent directories if they don't exist.
170 \[emu-18.el; Emacs 19 emulating function]"
171   (let ((len (length dir))
172         (p 0) p1 path)
173     (catch 'tag
174       (while (and (< p len) (string-match "[^/]*/?" dir p))
175         (setq p1 (match-end 0))
176         (if (= p1 len)
177             (throw 'tag nil)
178           )
179         (setq path (substring dir 0 p1))
180         (if (not (file-directory-p path))
181             (cond ((file-exists-p path)
182                    (error "Creating directory: %s is not directory" path)
183                    )
184                   ((null parents)
185                    (error "Creating directory: %s is not exist" path)
186                    )
187                   (t
188                    (make-directory-internal path)
189                    ))
190           )
191         (setq p p1)
192         ))
193     (make-directory-internal dir)
194     ))
195
196 ;; Imported from files.el of EMACS 19.33.
197 (defun file-relative-name (filename &optional directory)
198   "Convert FILENAME to be relative to DIRECTORY (default: default-directory)."
199   (setq filename (expand-file-name filename)
200         directory (file-name-as-directory (expand-file-name
201                                            (or directory default-directory))))
202   (let ((ancestor ""))
203     (while (not (string-match (concat "^" (regexp-quote directory)) filename))
204       (setq directory (file-name-directory (substring directory 0 -1))
205             ancestor (concat "../" ancestor)))
206     (concat ancestor (substring filename (match-end 0)))))
207
208
209 ;;; @ mark
210 ;;;
211
212 (or (fboundp 'si:mark)
213     (fset 'si:mark (symbol-function 'mark)))
214 (defun mark (&optional force)
215   (si:mark)
216   )
217
218
219 ;;; @ mode-line
220 ;;;
221
222 ;;; Imported from Emacs 19.30.
223 (defun force-mode-line-update (&optional all)
224   "Force the mode-line of the current buffer to be redisplayed.
225 With optional non-nil ALL, force redisplay of all mode-lines.
226 \[emu-18.el; Emacs 19 emulating function]"
227   (if all (save-excursion (set-buffer (other-buffer))))
228   (set-buffer-modified-p (buffer-modified-p)))
229
230
231 ;;; @ text property
232 ;;;
233
234 (defun tl:set-text-properties (start end properties &optional object))
235 (defun tl:add-text-properties (start end properties &optional object)) 
236 (defun remove-text-properties (start end properties &optional object))
237 (defun tl:overlay-buffer (overlay))
238
239
240 ;;; @@ visible/invisible
241 ;;;
242
243 (defmacro enable-invisible ()
244   (`
245    (progn
246      (make-local-variable 'original-selective-display)
247      (setq original-selective-display selective-display)
248      (setq selective-display t)
249      )))
250
251 (defmacro end-of-invisible ()
252   (` (setq selective-display
253            (if (boundp 'original-selective-display)
254                original-selective-display))
255      ))
256
257 (defun invisible-region (start end)
258   (let ((buffer-read-only nil)          ;Okay even if write protected.
259         (modp (buffer-modified-p)))
260     (if (save-excursion
261           (goto-char (1- end))
262           (eq (following-char) ?\n)
263           )
264         (setq end (1- end))
265       )
266     (unwind-protect
267         (subst-char-in-region start end ?\n ?\^M t)
268       (set-buffer-modified-p modp)
269       )))
270
271 (defun visible-region (start end)
272   (let ((buffer-read-only nil)          ;Okay even if write protected.
273         (modp (buffer-modified-p)))
274     (unwind-protect
275         (subst-char-in-region start end ?\^M ?\n t)
276       (set-buffer-modified-p modp)
277       )))
278
279 (defun invisible-p (pos)
280   (save-excursion
281     (goto-char pos)
282     (eq (following-char) ?\^M)
283     ))
284
285 (defun next-visible-point (pos)
286   (save-excursion
287     (goto-char pos)
288     (end-of-line)
289     (if (eq (following-char) ?\n)
290         (forward-char)
291       )
292     (point)
293     ))
294
295
296 ;;; @ mouse
297 ;;;
298
299 (defvar mouse-button-1 nil)
300 (defvar mouse-button-2 nil)
301 (defvar mouse-button-3 nil)
302
303
304 ;;; @ string
305 ;;;
306
307 (defun char-list-to-string (char-list)
308   "Convert list of character CHAR-LIST to string. [emu-18.el]"
309   (mapconcat (function char-to-string) char-list "")
310   )
311
312
313 ;;; @ end
314 ;;;
315
316 (provide 'emu-18)
317
318 ;;; emu-18.el ends here