(delete): Return nil when argument 'list' is nil. <cf. [tm-ja:4935]>
[elisp/apel.git] / poe-18.el
1 ;;; poe-18.el --- poe API implementation for Emacs 18.*
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: emulation, compatibility
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Note to developers:
28 ;; 
29 ;; If old (v18) compiler is used, top-level macros are expanded at
30 ;; *load-time*, not compile-time.  So, you cannot use macros defined
31 ;; in this file using `defmacro-maybe'.  In addition, due to this
32 ;; limitation, `eval-when-compile' and `eval-and-compile' provided by
33 ;; this file do not do compile-time evaluation at all.
34
35 ;;; Code:
36
37 (provide 'poe-18)                       ; beware of circular dependency.
38 (require 'poe)                          ; load definitions of `*-maybe'.
39
40 ;;; @ for EMACS 18.55
41 ;;;
42
43 (defvar-maybe buffer-undo-list nil)
44
45
46 ;;; @ Emacs 19 emulation
47 ;;;
48
49 (defvar-maybe data-directory exec-directory)
50
51
52 ;;; @ Lisp Language
53 ;;;
54
55 ;;; @@ list
56 ;;;
57
58 (defun delete (elt list)
59   "Delete by side effect any occurrences of ELT as a member of LIST.
60 The modified LIST is returned.  Comparison is done with `equal'.
61 If the first member of LIST is ELT, deleting it is not a side effect;
62 it is simply using a different list.
63 Therefore, write `(setq foo (delete element foo))'
64 to be sure of changing the value of `foo'.
65 \[poe-18.el; EMACS 19 emulating function]"
66   (if list
67       (if (equal elt (car list))
68           (cdr list)
69         (let ((rest list)
70               (rrest (cdr list)))
71           (while (and rrest (not (equal elt (car rrest))))
72             (setq rest rrest
73                   rrest (cdr rrest)))
74           (setcdr rest (cdr rrest))
75           list))))
76
77 (defun member (elt list)
78   "Return non-nil if ELT is an element of LIST.  Comparison done with EQUAL.
79 The value is actually the tail of LIST whose car is ELT.
80 \[poe-18.el; EMACS 19 emulating function]"
81   (while (and list (not (equal elt (car list))))
82     (setq list (cdr list)))
83   list)
84
85
86 ;;; @@ buffer-local variable
87 ;;;
88
89 (defun default-boundp (symbol)
90   "Return t if SYMBOL has a non-void default value.
91 This is the value that is seen in buffers that do not have their own values
92 for this variable.
93 \[poe-18.el; EMACS 19 emulating function]"
94   (condition-case error
95       (progn
96         (default-value symbol)
97         t)
98     (void-variable nil)))
99
100
101 ;;; @@ environment variable
102 ;;;
103
104 (autoload 'setenv "env"
105   "Set the value of the environment variable named VARIABLE to VALUE.
106 VARIABLE should be a string.  VALUE is optional; if not provided or is
107 `nil', the environment variable VARIABLE will be removed.  
108 This function works by modifying `process-environment'."
109   t)
110
111
112 ;;; @@ function
113 ;;;
114
115 (defun defalias (sym newdef)
116   "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.
117 Associates the function with the current load file, if any."
118   (fset sym newdef))
119
120 (defun byte-code-function-p (exp)
121   "T if OBJECT is a byte-compiled function object.
122 \[poe-18.el; EMACS 19 emulating function]"
123   (and (consp exp)
124        (let ((rest (cdr (cdr exp)))
125              elt)
126          (if (stringp (car rest))
127              (setq rest (cdr rest)))
128          (catch 'tag
129            (while rest
130              (setq elt (car rest))
131              (if (and (consp elt)
132                       (eq (car elt) 'byte-code))
133                  (throw 'tag t))
134              (setq rest (cdr rest)))))))
135
136
137 ;;; @ Compilation Features
138 ;;;
139
140 ;;; emulate all functions and macros of emacs-20.3/lisp/byte-run.el.
141 ;;; (note: jwz's original compiler and XEmacs compiler have some more
142 ;;;  macros; they are "nuked" by rms in FSF version.)
143
144 (put 'inline 'lisp-indent-hook 0)
145 (defalias-maybe 'inline 'progn)
146
147 (put 'defsubst 'lisp-indent-hook 'defun)
148 (put 'defsubst 'edebug-form-spec 'defun)
149 (defmacro-maybe defsubst (name arglist &rest body)
150   "Define an inline function.  The syntax is just like that of `defun'.
151
152 This emulating macro does not support function inlining because old \(v18\)
153 compiler does not support inlining feature.
154 \[poe-18.el; EMACS 19 emulating macro]"
155   (cons 'defun (cons name (cons arglist body))))
156
157 (defun-maybe make-obsolete (fn new)
158   "Make the byte-compiler warn that FUNCTION is obsolete.
159 The warning will say that NEW should be used instead.
160 If NEW is a string, that is the `use instead' message.
161
162 This emulating function does nothing because old \(v18\) compiler does not
163 support this feature.
164 \[poe-18.el; EMACS 19 emulating function]"
165   (interactive "aMake function obsolete: \nxObsoletion replacement: ")
166   fn)
167
168 (defun-maybe make-obsolete-variable (var new)
169   "Make the byte-compiler warn that VARIABLE is obsolete,
170 and NEW should be used instead.  If NEW is a string, then that is the
171 `use instead' message.
172
173 This emulating function does nothing because old \(v18\) compiler does not
174 support this feature.
175 \[poe-18.el; EMACS 19 emulating function]"
176   (interactive "vMake variable obsolete: \nxObsoletion replacement: ")
177   var)
178
179 (put 'dont-compile 'lisp-indent-hook 0)
180 (defmacro-maybe dont-compile (&rest body)
181   "Like `progn', but the body always runs interpreted \(not compiled\).
182 If you think you need this, you're probably making a mistake somewhere.
183 \[poe-18.el; EMACS 19 emulating macro]"
184   (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
185
186 (put 'eval-when-compile 'lisp-indent-hook 0)
187 (defmacro-maybe eval-when-compile (&rest body)
188   "Like progn, but evaluates the body at compile-time.
189
190 This emulating macro does not do compile-time evaluation at all because
191 of the limitation of old \(v18\) compiler.
192 \[poe-18.el; EMACS 19 emulating macro]"
193   (cons 'progn body))
194
195 (put 'eval-and-compile 'lisp-indent-hook 0)
196 (defmacro-maybe eval-and-compile (&rest body)
197   "Like progn, but evaluates the body at compile-time as well as at load-time.
198
199 This emulating macro does not do compile-time evaluation at all because
200 of the limitation of old \(v18\) compiler.
201 \[poe-18.el; EMACS 19 emulating macro]"
202   (cons 'progn body))
203
204
205 ;;; @ text property
206 ;;;
207
208 (defun set-text-properties (start end properties &optional object))
209
210 (defun remove-text-properties (start end properties &optional object))
211
212
213 ;;; @ file
214 ;;;
215
216 (defun make-directory-internal (dirname)
217   "Create a directory. One argument, a file name string.
218 \[poe-18.el; EMACS 19 emulating function]"
219  (let ((dir (expand-file-name dirname)))
220    (if (file-exists-p dir)
221        (error "Creating directory: %s is already exist" dir)
222      (call-process "mkdir" nil nil nil dir))))
223
224 (defun make-directory (dir &optional parents)
225   "Create the directory DIR and any nonexistent parent dirs.
226 The second (optional) argument PARENTS says whether
227 to create parent directories if they don't exist.
228 \[poe-18.el; EMACS 19 emulating function]"
229   (let ((len (length dir))
230         (p 0) p1 path)
231     (catch 'tag
232       (while (and (< p len) (string-match "[^/]*/?" dir p))
233         (setq p1 (match-end 0))
234         (if (= p1 len)
235             (throw 'tag nil))
236         (setq path (substring dir 0 p1))
237         (if (not (file-directory-p path))
238             (cond ((file-exists-p path)
239                    (error "Creating directory: %s is not directory" path))
240                   ((null parents)
241                    (error "Creating directory: %s is not exist" path))
242                   (t
243                    (make-directory-internal path))))
244         (setq p p1)))
245     (make-directory-internal dir)))
246
247 ;; Imported from files.el of EMACS 19.33.
248 (defun parse-colon-path (cd-path)
249   "Explode a colon-separated list of paths into a string list."
250   (and cd-path
251        (let (cd-prefix cd-list (cd-start 0) cd-colon)
252          (setq cd-path (concat cd-path path-separator))
253          (while (setq cd-colon (string-match path-separator cd-path cd-start))
254            (setq cd-list
255                  (nconc cd-list
256                         (list (if (= cd-start cd-colon)
257                                   nil
258                                 (substitute-in-file-name
259                                  (file-name-as-directory
260                                   (substring cd-path cd-start cd-colon)))))))
261            (setq cd-start (+ cd-colon 1)))
262          cd-list)))
263
264 ;; Imported from files.el of EMACS 19.33.
265 (defun file-relative-name (filename &optional directory)
266   "Convert FILENAME to be relative to DIRECTORY (default: default-directory)."
267   (setq filename (expand-file-name filename)
268         directory (file-name-as-directory (expand-file-name
269                                            (or directory default-directory))))
270   (let ((ancestor ""))
271     (while (not (string-match (concat "^" (regexp-quote directory)) filename))
272       (setq directory (file-name-directory (substring directory 0 -1))
273             ancestor (concat "../" ancestor)))
274     (concat ancestor (substring filename (match-end 0)))))
275
276 (or (fboundp 'si:directory-files)
277     (fset 'si:directory-files (symbol-function 'directory-files)))
278 (defun directory-files (directory &optional full match nosort)
279   "Return a list of names of files in DIRECTORY.
280 There are three optional arguments:
281 If FULL is non-nil, return absolute file names.  Otherwise return names
282  that are relative to the specified directory.
283 If MATCH is non-nil, mention only file names that match the regexp MATCH.
284 If NOSORT is dummy for compatibility.
285 \[poe-18.el; EMACS 19 emulating function]"
286   (si:directory-files directory full match))
287
288     
289 ;;; @ Display Features
290 ;;;
291
292 ;;; Imported from Emacs 19.30.
293 (defun force-mode-line-update (&optional all)
294   "Force the mode-line of the current buffer to be redisplayed.
295 With optional non-nil ALL, force redisplay of all mode-lines.
296 \[poe-18.el; Emacs 19 emulating function]"
297   (if all (save-excursion (set-buffer (other-buffer))))
298   (set-buffer-modified-p (buffer-modified-p)))
299
300
301 ;;; @ overlay
302 ;;;
303
304 (cond ((boundp 'NEMACS)
305        (defvar emu:available-face-attribute-alist
306          '(
307            ;;(bold      . inversed-region)
308            (italic    . underlined-region)
309            (underline . underlined-region)
310            ))
311
312        ;; by YAMATE Keiichirou 1994/10/28
313        (defun attribute-add-narrow-attribute (attr from to)
314          (or (consp (symbol-value attr))
315              (set attr (list 1)))
316          (let* ((attr-value (symbol-value attr))
317                 (len (car attr-value))
318                 (posfrom 1)
319                 posto)
320            (while (and (< posfrom len)
321                        (> from (nth posfrom attr-value)))
322              (setq posfrom (1+ posfrom)))
323            (setq posto posfrom)
324            (while (and (< posto len)
325                        (> to (nth posto attr-value)))
326              (setq posto (1+ posto)))
327            (if  (= posto posfrom)
328                (if (= (% posto 2) 1)
329                    (if (and (< to len)
330                             (= to (nth posto attr-value)))
331                        (set-marker (nth posto attr-value) from)
332                      (setcdr (nthcdr (1- posfrom) attr-value)
333                              (cons (set-marker-type (set-marker (make-marker)
334                                                                 from)
335                                                     'point-type)
336                                    (cons (set-marker-type
337                                           (set-marker (make-marker)
338                                                       to)
339                                           nil)
340                                          (nthcdr posto attr-value))))
341                      (setcar attr-value (+ len 2))))
342              (if (= (% posfrom 2) 0)
343                  (setq posfrom (1- posfrom))
344                (set-marker (nth posfrom attr-value) from))
345              (if (= (% posto 2) 0)
346                  nil
347                (setq posto (1- posto))
348                (set-marker (nth posto attr-value) to))
349              (setcdr (nthcdr posfrom attr-value)
350                      (nthcdr posto attr-value)))))
351        
352        (defalias 'make-overlay 'cons)
353
354        (defun overlay-put (overlay prop value)
355          (let ((ret (and (eq prop 'face)
356                          (assq value emu:available-face-attribute-alist))))
357            (if ret
358                (attribute-add-narrow-attribute (cdr ret)
359                                                (car overlay)(cdr overlay))))))
360       (t
361        (defun make-overlay (beg end &optional buffer type))
362        (defun overlay-put (overlay prop value))))
363
364 (defun overlay-buffer (overlay))
365
366
367 ;;; @ buffer
368 ;;;
369
370 (defun-maybe generate-new-buffer-name (name &optional ignore)
371   "Return a string that is the name of no existing buffer based on NAME.
372 If there is no live buffer named NAME, then return NAME.
373 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
374 until an unused name is found, and then return that name.
375 Optional second argument IGNORE specifies a name that is okay to use
376 \(if it is in the sequence to be tried)
377 even if a buffer with that name exists."
378   (if (get-buffer name)
379       (let ((n 2) new)
380         (while (get-buffer (setq new (format "%s<%d>" name n)))
381           (setq n (1+ n)))
382         new)
383     name))
384
385 (or (fboundp 'si:mark)
386     (fset 'si:mark (symbol-function 'mark)))
387 (defun mark (&optional force)
388   (si:mark))
389
390
391 ;;; @ end
392 ;;;
393
394 ;;; poe-18.el ends here