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