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