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