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