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