tm 7.89.
[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.26 1996/09/21 16:29:25 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 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 ;;; Code:
27
28 (autoload 'setenv "env"
29   "Set the value of the environment variable named VARIABLE to VALUE.
30 VARIABLE should be a string.  VALUE is optional; if not provided or is
31 `nil', the environment variable VARIABLE will be removed.  
32 This function works by modifying `process-environment'."
33   t)
34
35
36 ;;; @ for EMACS 18.55
37 ;;;
38
39 (defvar buffer-undo-list nil)
40
41
42 ;;; @ hook
43 ;;;
44
45 ;; These function are imported from EMACS 19.28.
46 (defun add-hook (hook function &optional append)
47   "Add to the value of HOOK the function FUNCTION.
48 FUNCTION is not added if already present.
49 FUNCTION is added (if necessary) at the beginning of the hook list
50 unless the optional argument APPEND is non-nil, in which case
51 FUNCTION is added at the end.
52  
53 HOOK should be a symbol, and FUNCTION may be any valid function.  If
54 HOOK is void, it is first set to nil.  If HOOK's value is a single
55 function, it is changed to a list of functions.
56 \[emu-18.el; EMACS 19 emulating function]"
57   (or (boundp hook)
58       (set hook nil)
59       )
60   ;; If the hook value is a single function, turn it into a list.
61   (let ((old (symbol-value hook)))
62     (if (or (not (listp old))
63             (eq (car old) 'lambda))
64         (set hook (list old))
65       ))
66   (or (if (consp function)
67           ;; Clever way to tell whether a given lambda-expression
68           ;; is equal to anything in the hook.
69           (let ((tail (assoc (cdr function) (symbol-value hook))))
70             (equal function tail)
71             )
72         (memq function (symbol-value hook))
73         )
74       (set hook 
75            (if append
76                (nconc (symbol-value hook) (list function))
77              (cons function (symbol-value hook))
78              ))
79       ))
80
81 (defun remove-hook (hook function)
82   "Remove from the value of HOOK the function FUNCTION.
83 HOOK should be a symbol, and FUNCTION may be any valid function.  If
84 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
85 list of hooks to run in HOOK, then nothing is done.  See `add-hook'.
86 \[emu-18.el; EMACS 19 emulating function]"
87   (if (or (not (boundp hook))           ;unbound symbol, or
88           (null (symbol-value hook))    ;value is nil, or
89           (null function))              ;function is nil, then
90       nil                               ;Do nothing.
91     (let ((hook-value (symbol-value hook)))
92       (if (consp hook-value)
93           (setq hook-value (delete function hook-value))
94         (if (equal hook-value function)
95             (setq hook-value nil)
96           ))
97       (set hook hook-value)
98       )))
99
100
101 ;;; @ list
102 ;;;
103
104 (defun member (elt list)
105   "Return non-nil if ELT is an element of LIST.  Comparison done with EQUAL.
106 The value is actually the tail of LIST whose car is ELT.
107 \[emu-18.el; EMACS 19 emulating function]"
108   (while (and list (not (equal elt (car list))))
109     (setq list (cdr list)))
110   list)
111
112 (defun delete (elt list)
113   "Delete by side effect any occurrences of ELT as a member of LIST.
114 The modified LIST is returned.  Comparison is done with `equal'.
115 If the first member of LIST is ELT, deleting it is not a side effect;
116 it is simply using a different list.
117 Therefore, write `(setq foo (delete element foo))'
118 to be sure of changing the value of `foo'.
119 \[emu-18.el; EMACS 19 emulating function]"
120   (if (equal elt (car list))
121       (cdr list)
122     (let ((rest list)
123           (rrest (cdr list))
124           )
125       (while (and rrest (not (equal elt (car rrest))))
126         (setq rest rrest
127               rrest (cdr rrest))
128         )
129       (rplacd rest (cdr rrest))
130       list)))
131
132
133 ;;; @ function
134 ;;;
135
136 (defun defalias (sym newdef)
137   "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.
138 Associates the function with the current load file, if any.
139 \[emu-18.el; EMACS 19 emulating function]"
140   (fset sym newdef)
141   )
142
143 (defun byte-code-function-p (exp)
144   "T if OBJECT is a byte-compiled function object.
145 \[emu-18.el; EMACS 19 emulating function]"
146   (and (consp exp)
147        (let* ((rest (cdr (cdr exp))) elt)
148          (if (stringp (car rest))
149              (setq rest (cdr rest))
150            )
151          (catch 'tag
152            (while rest
153              (setq elt (car rest))
154              (if (and (consp elt)(eq (car elt) 'byte-code))
155                  (throw 'tag t)
156                )
157              (setq rest (cdr rest))
158              ))
159          )))
160
161
162 ;;; @ file
163 ;;;
164
165 (defun make-directory-internal (dirname)
166   "Create a directory. One argument, a file name string.
167 \[emu-18.el; EMACS 19 emulating function]"
168   (if (file-exists-p dirname)
169       (error "Creating directory: %s is already exist" dirname)
170     (if (not (= (call-process "mkdir" nil nil nil dirname) 0))
171         (error "Creating directory: no such file or directory, %s" dirname)
172       )))
173
174 (defun make-directory (dir &optional parents)
175   "Create the directory DIR and any nonexistent parent dirs.
176 The second (optional) argument PARENTS says whether
177 to create parent directories if they don't exist.
178 \[emu-18.el; EMACS 19 emulating function]"
179   (let ((len (length dir))
180         (p 0) p1 path)
181     (catch 'tag
182       (while (and (< p len) (string-match "[^/]*/?" dir p))
183         (setq p1 (match-end 0))
184         (if (= p1 len)
185             (throw 'tag nil)
186           )
187         (setq path (substring dir 0 p1))
188         (if (not (file-directory-p path))
189             (cond ((file-exists-p path)
190                    (error "Creating directory: %s is not directory" path)
191                    )
192                   ((null parents)
193                    (error "Creating directory: %s is not exist" path)
194                    )
195                   (t
196                    (make-directory-internal path)
197                    ))
198           )
199         (setq p p1)
200         ))
201     (make-directory-internal dir)
202     ))
203
204 ;; Imported from files.el of EMACS 19.33.
205 (defun parse-colon-path (cd-path)
206   "Explode a colon-separated list of paths into a string list."
207   (and cd-path
208        (let (cd-prefix cd-list (cd-start 0) cd-colon)
209          (setq cd-path (concat cd-path path-separator))
210          (while (setq cd-colon (string-match path-separator cd-path cd-start))
211            (setq cd-list
212                  (nconc cd-list
213                         (list (if (= cd-start cd-colon)
214                                    nil
215                                 (substitute-in-file-name
216                                  (file-name-as-directory
217                                   (substring cd-path cd-start cd-colon)))))))
218            (setq cd-start (+ cd-colon 1)))
219          cd-list)))
220
221 ;; Imported from files.el of EMACS 19.33.
222 (defun file-relative-name (filename &optional directory)
223   "Convert FILENAME to be relative to DIRECTORY (default: default-directory)."
224   (setq filename (expand-file-name filename)
225         directory (file-name-as-directory (expand-file-name
226                                            (or directory default-directory))))
227   (let ((ancestor ""))
228     (while (not (string-match (concat "^" (regexp-quote directory)) filename))
229       (setq directory (file-name-directory (substring directory 0 -1))
230             ancestor (concat "../" ancestor)))
231     (concat ancestor (substring filename (match-end 0)))))
232
233 (or (fboundp 'si:directory-files)
234     (fset 'si:directory-files (symbol-function 'directory-files)))
235 (defun directory-files (directory &optional full match nosort)
236   "Return a list of names of files in DIRECTORY.
237 There are three optional arguments:
238 If FULL is non-nil, return absolute file names.  Otherwise return names
239  that are relative to the specified directory.
240 If MATCH is non-nil, mention only file names that match the regexp MATCH.
241 If NOSORT is dummy for compatibility.
242 \[emu-18.el; EMACS 19 emulating function]"
243   (si:directory-files directory full match)
244   )
245
246     
247 ;;; @ mark
248 ;;;
249
250 (or (fboundp 'si:mark)
251     (fset 'si:mark (symbol-function 'mark)))
252 (defun mark (&optional force)
253   (si:mark)
254   )
255
256
257 ;;; @ mode-line
258 ;;;
259
260 ;;; Imported from Emacs 19.30.
261 (defun force-mode-line-update (&optional all)
262   "Force the mode-line of the current buffer to be redisplayed.
263 With optional non-nil ALL, force redisplay of all mode-lines.
264 \[emu-18.el; Emacs 19 emulating function]"
265   (if all (save-excursion (set-buffer (other-buffer))))
266   (set-buffer-modified-p (buffer-modified-p)))
267
268
269 ;;; @ text property
270 ;;;
271
272 (defun tl:set-text-properties (start end properties &optional object))
273 (defun tl:add-text-properties (start end properties &optional object)) 
274 (defun remove-text-properties (start end properties &optional object))
275 (defun tl:overlay-buffer (overlay))
276
277
278 ;;; @@ visible/invisible
279 ;;;
280
281 (defmacro enable-invisible ()
282   (`
283    (progn
284      (make-local-variable 'original-selective-display)
285      (setq original-selective-display selective-display)
286      (setq selective-display t)
287      )))
288
289 (defmacro end-of-invisible ()
290   (` (setq selective-display
291            (if (boundp 'original-selective-display)
292                original-selective-display))
293      ))
294
295 (defun invisible-region (start end)
296   (let ((buffer-read-only nil)          ;Okay even if write protected.
297         (modp (buffer-modified-p)))
298     (if (save-excursion
299           (goto-char (1- end))
300           (eq (following-char) ?\n)
301           )
302         (setq end (1- end))
303       )
304     (unwind-protect
305         (subst-char-in-region start end ?\n ?\^M t)
306       (set-buffer-modified-p modp)
307       )))
308
309 (defun visible-region (start end)
310   (let ((buffer-read-only nil)          ;Okay even if write protected.
311         (modp (buffer-modified-p)))
312     (unwind-protect
313         (subst-char-in-region start end ?\^M ?\n t)
314       (set-buffer-modified-p modp)
315       )))
316
317 (defun invisible-p (pos)
318   (save-excursion
319     (goto-char pos)
320     (eq (following-char) ?\^M)
321     ))
322
323 (defun next-visible-point (pos)
324   (save-excursion
325     (goto-char pos)
326     (end-of-line)
327     (if (eq (following-char) ?\n)
328         (forward-char)
329       )
330     (point)
331     ))
332
333
334 ;;; @ mouse
335 ;;;
336
337 (defvar mouse-button-1 nil)
338 (defvar mouse-button-2 nil)
339 (defvar mouse-button-3 nil)
340
341
342 ;;; @ string
343 ;;;
344
345 (defun char-list-to-string (char-list)
346   "Convert list of character CHAR-LIST to string. [emu-18.el]"
347   (mapconcat (function char-to-string) char-list "")
348   )
349
350
351 ;;; @ end
352 ;;;
353
354 (provide 'emu-18)
355
356 ;;; emu-18.el ends here