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