* pces.el: Require `pces-raw' if file-coding feature is not
[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 (defmacro inline (&rest body)
146   "Eval BODY forms sequentially and return value of last one.
147
148 This emulating macro does not support function inlining because old \(v18\)
149 compiler does not support inlining feature.
150 \[poe-18.el; EMACS 19 emulating macro]"
151   (` (progn (,@ body))))
152
153 (put 'defsubst 'lisp-indent-hook 'defun)
154 (put 'defsubst 'edebug-form-spec 'defun)
155 (defmacro-maybe defsubst (name arglist &rest body)
156   "Define an inline function.  The syntax is just like that of `defun'.
157
158 This emulating macro does not support function inlining because old \(v18\)
159 compiler does not support inlining feature.
160 \[poe-18.el; EMACS 19 emulating macro]"
161   (cons 'defun (cons name (cons arglist body))))
162
163 (defun-maybe make-obsolete (fn new)
164   "Make the byte-compiler warn that FUNCTION is obsolete.
165 The warning will say that NEW should be used instead.
166 If NEW is a string, that is the `use instead' message.
167
168 This emulating function does nothing because old \(v18\) compiler does not
169 support this feature.
170 \[poe-18.el; EMACS 19 emulating function]"
171   (interactive "aMake function obsolete: \nxObsoletion replacement: ")
172   fn)
173
174 (defun-maybe make-obsolete-variable (var new)
175   "Make the byte-compiler warn that VARIABLE is obsolete,
176 and NEW should be used instead.  If NEW is a string, then that is the
177 `use instead' message.
178
179 This emulating function does nothing because old \(v18\) compiler does not
180 support this feature.
181 \[poe-18.el; EMACS 19 emulating function]"
182   (interactive "vMake variable obsolete: \nxObsoletion replacement: ")
183   var)
184
185 (put 'dont-compile 'lisp-indent-hook 0)
186 (defmacro-maybe dont-compile (&rest body)
187   "Like `progn', but the body always runs interpreted \(not compiled\).
188 If you think you need this, you're probably making a mistake somewhere.
189 \[poe-18.el; EMACS 19 emulating macro]"
190   (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
191
192 (put 'eval-when-compile 'lisp-indent-hook 0)
193 (defmacro-maybe eval-when-compile (&rest body)
194   "Like progn, but evaluates the body at compile-time.
195
196 This emulating macro does not do compile-time evaluation at all because
197 of the limitation of old \(v18\) compiler.
198 \[poe-18.el; EMACS 19 emulating macro]"
199   (cons 'progn body))
200
201 (put 'eval-and-compile 'lisp-indent-hook 0)
202 (defmacro-maybe eval-and-compile (&rest body)
203   "Like progn, but evaluates the body at compile-time as well as at load-time.
204
205 This emulating macro does not do compile-time evaluation at all because
206 of the limitation of old \(v18\) compiler.
207 \[poe-18.el; EMACS 19 emulating macro]"
208   (cons 'progn body))
209
210
211 ;;; @ text property
212 ;;;
213
214 (defun set-text-properties (start end properties &optional object))
215
216 (defun remove-text-properties (start end properties &optional object))
217
218
219 ;;; @ file
220 ;;;
221
222 (defun make-directory-internal (dirname)
223   "Create a directory. One argument, a file name string.
224 \[poe-18.el; EMACS 19 emulating function]"
225  (let ((dir (expand-file-name dirname)))
226    (if (file-exists-p dir)
227        (error "Creating directory: %s is already exist" dir)
228      (call-process "mkdir" nil nil nil dir))))
229
230 (defun make-directory (dir &optional parents)
231   "Create the directory DIR and any nonexistent parent dirs.
232 The second (optional) argument PARENTS says whether
233 to create parent directories if they don't exist.
234 \[poe-18.el; EMACS 19 emulating function]"
235   (let ((len (length dir))
236         (p 0) p1 path)
237     (catch 'tag
238       (while (and (< p len) (string-match "[^/]*/?" dir p))
239         (setq p1 (match-end 0))
240         (if (= p1 len)
241             (throw 'tag nil))
242         (setq path (substring dir 0 p1))
243         (if (not (file-directory-p path))
244             (cond ((file-exists-p path)
245                    (error "Creating directory: %s is not directory" path))
246                   ((null parents)
247                    (error "Creating directory: %s is not exist" path))
248                   (t
249                    (make-directory-internal path))))
250         (setq p p1)))
251     (make-directory-internal dir)))
252
253 ;; Imported from files.el of EMACS 19.33.
254 (defun parse-colon-path (cd-path)
255   "Explode a colon-separated list of paths into a string list."
256   (and cd-path
257        (let (cd-prefix cd-list (cd-start 0) cd-colon)
258          (setq cd-path (concat cd-path path-separator))
259          (while (setq cd-colon (string-match path-separator cd-path cd-start))
260            (setq cd-list
261                  (nconc cd-list
262                         (list (if (= cd-start cd-colon)
263                                   nil
264                                 (substitute-in-file-name
265                                  (file-name-as-directory
266                                   (substring cd-path cd-start cd-colon)))))))
267            (setq cd-start (+ cd-colon 1)))
268          cd-list)))
269
270 ;; Imported from files.el of EMACS 19.33.
271 (defun file-relative-name (filename &optional directory)
272   "Convert FILENAME to be relative to DIRECTORY (default: default-directory)."
273   (setq filename (expand-file-name filename)
274         directory (file-name-as-directory (expand-file-name
275                                            (or directory default-directory))))
276   (let ((ancestor ""))
277     (while (not (string-match (concat "^" (regexp-quote directory)) filename))
278       (setq directory (file-name-directory (substring directory 0 -1))
279             ancestor (concat "../" ancestor)))
280     (concat ancestor (substring filename (match-end 0)))))
281
282 (or (fboundp 'si:directory-files)
283     (fset 'si:directory-files (symbol-function 'directory-files)))
284 (defun directory-files (directory &optional full match nosort)
285   "Return a list of names of files in DIRECTORY.
286 There are three optional arguments:
287 If FULL is non-nil, return absolute file names.  Otherwise return names
288  that are relative to the specified directory.
289 If MATCH is non-nil, mention only file names that match the regexp MATCH.
290 If NOSORT is dummy for compatibility.
291 \[poe-18.el; EMACS 19 emulating function]"
292   (si:directory-files directory full match))
293
294     
295 ;;; @ Display Features
296 ;;;
297
298 ;;; Imported from Emacs 19.30.
299 (defun force-mode-line-update (&optional all)
300   "Force the mode-line of the current buffer to be redisplayed.
301 With optional non-nil ALL, force redisplay of all mode-lines.
302 \[poe-18.el; Emacs 19 emulating function]"
303   (if all (save-excursion (set-buffer (other-buffer))))
304   (set-buffer-modified-p (buffer-modified-p)))
305
306
307 ;;; @ overlay
308 ;;;
309
310 (cond ((boundp 'NEMACS)
311        (defvar emu:available-face-attribute-alist
312          '(
313            ;;(bold      . inversed-region)
314            (italic    . underlined-region)
315            (underline . underlined-region)
316            ))
317
318        ;; by YAMATE Keiichirou 1994/10/28
319        (defun attribute-add-narrow-attribute (attr from to)
320          (or (consp (symbol-value attr))
321              (set attr (list 1)))
322          (let* ((attr-value (symbol-value attr))
323                 (len (car attr-value))
324                 (posfrom 1)
325                 posto)
326            (while (and (< posfrom len)
327                        (> from (nth posfrom attr-value)))
328              (setq posfrom (1+ posfrom)))
329            (setq posto posfrom)
330            (while (and (< posto len)
331                        (> to (nth posto attr-value)))
332              (setq posto (1+ posto)))
333            (if  (= posto posfrom)
334                (if (= (% posto 2) 1)
335                    (if (and (< to len)
336                             (= to (nth posto attr-value)))
337                        (set-marker (nth posto attr-value) from)
338                      (setcdr (nthcdr (1- posfrom) attr-value)
339                              (cons (set-marker-type (set-marker (make-marker)
340                                                                 from)
341                                                     'point-type)
342                                    (cons (set-marker-type
343                                           (set-marker (make-marker)
344                                                       to)
345                                           nil)
346                                          (nthcdr posto attr-value))))
347                      (setcar attr-value (+ len 2))))
348              (if (= (% posfrom 2) 0)
349                  (setq posfrom (1- posfrom))
350                (set-marker (nth posfrom attr-value) from))
351              (if (= (% posto 2) 0)
352                  nil
353                (setq posto (1- posto))
354                (set-marker (nth posto attr-value) to))
355              (setcdr (nthcdr posfrom attr-value)
356                      (nthcdr posto attr-value)))))
357        
358        (defalias 'make-overlay 'cons)
359
360        (defun overlay-put (overlay prop value)
361          (let ((ret (and (eq prop 'face)
362                          (assq value emu:available-face-attribute-alist))))
363            (if ret
364                (attribute-add-narrow-attribute (cdr ret)
365                                                (car overlay)(cdr overlay))))))
366       (t
367        (defun make-overlay (beg end &optional buffer type))
368        (defun overlay-put (overlay prop value))))
369
370 (defun overlay-buffer (overlay))
371
372
373 ;;; @ buffer
374 ;;;
375
376 (defun-maybe generate-new-buffer-name (name &optional ignore)
377   "Return a string that is the name of no existing buffer based on NAME.
378 If there is no live buffer named NAME, then return NAME.
379 Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
380 until an unused name is found, and then return that name.
381 Optional second argument IGNORE specifies a name that is okay to use
382 \(if it is in the sequence to be tried)
383 even if a buffer with that name exists."
384   (if (get-buffer name)
385       (let ((n 2) new)
386         (while (get-buffer (setq new (format "%s<%d>" name n)))
387           (setq n (1+ n)))
388         new)
389     name))
390
391 (or (fboundp 'si:mark)
392     (fset 'si:mark (symbol-function 'mark)))
393 (defun mark (&optional force)
394   (si:mark))
395
396
397 ;;; @ end
398 ;;;
399
400 ;;; poe-18.el ends here