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