emu.el (with-temp-file): New macro.
[elisp/apel.git] / emu.el
1 ;;; emu.el --- Emulation module for each Emacs variants
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, NEmacs, MULE, Emacs/mule, XEmacs
7
8 ;; This file is part of emu.
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 (defmacro defun-maybe (name &rest everything-else)
28   (or (and (fboundp name)
29            (not (get name 'defun-maybe))
30            )
31       (` (or (fboundp (quote (, name)))
32              (progn
33                (defun (, name) (,@ everything-else))
34                (put (quote (, name)) 'defun-maybe t)
35                ))
36          )))
37
38 (defmacro defsubst-maybe (name &rest everything-else)
39   (or (and (fboundp name)
40            (not (get name 'defsubst-maybe))
41            )
42       (` (or (fboundp (quote (, name)))
43              (progn
44                (defsubst (, name) (,@ everything-else))
45                (put (quote (, name)) 'defsubst-maybe t)
46                ))
47          )))
48
49 (defmacro defmacro-maybe (name &rest everything-else)
50   (or (and (fboundp name)
51            (not (get name 'defmacro-maybe))
52            )
53       (` (or (fboundp (quote (, name)))
54              (progn
55                (defmacro (, name) (,@ everything-else))
56                (put (quote (, name)) 'defmacro-maybe t)
57                ))
58          )))
59
60 (put 'defun-maybe 'lisp-indent-function 'defun)
61 (put 'defsubst-maybe 'lisp-indent-function 'defun)
62 (put 'defmacro-maybe 'lisp-indent-function 'defun)
63
64 (defmacro defconst-maybe (name &rest everything-else)
65   (or (and (boundp name)
66            (not (get name 'defconst-maybe))
67            )
68       (` (or (boundp (quote (, name)))
69              (progn
70                (defconst (, name) (,@ everything-else))
71                (put (quote (, name)) 'defconst-maybe t)
72                ))
73          )))
74
75
76 (defconst-maybe emacs-major-version (string-to-int emacs-version))
77 (defconst-maybe emacs-minor-version
78   (string-to-int
79    (substring emacs-version
80               (string-match (format "%d\\." emacs-major-version)
81                             emacs-version))))
82
83 (defvar running-emacs-18 (<= emacs-major-version 18))
84 (defvar running-xemacs (string-match "XEmacs" emacs-version))
85
86 (defvar running-mule-merged-emacs (and (not (boundp 'MULE))
87                                        (not running-xemacs) (featurep 'mule)))
88 (defvar running-xemacs-with-mule (and running-xemacs (featurep 'mule)))
89
90 (defvar running-emacs-19 (and (not running-xemacs) (= emacs-major-version 19)))
91 (defvar running-emacs-19_29-or-later
92   (or (and running-emacs-19 (>= emacs-minor-version 29))
93       (and (not running-xemacs)(>= emacs-major-version 20))))
94
95 (defvar running-xemacs-19 (and running-xemacs
96                                (= emacs-major-version 19)))
97 (defvar running-xemacs-20-or-later (and running-xemacs
98                                         (>= emacs-major-version 20)))
99 (defvar running-xemacs-19_14-or-later
100   (or (and running-xemacs-19 (>= emacs-minor-version 14))
101       running-xemacs-20-or-later))
102
103 (cond (running-xemacs
104        ;; for XEmacs
105        (require 'emu-xemacs)
106        (if (featurep 'mule)
107            ;; for XEmacs with MULE
108            (require 'emu-x20)
109          ;; for XEmacs without MULE
110          (require 'emu-latin1)
111          ))
112       (running-mule-merged-emacs
113        ;; for Emacs 20.1 and 20.2
114        (require 'emu-e20)
115        )
116       ((boundp 'MULE)
117        ;; for MULE 1.* and 2.*
118        (require 'emu-mule)
119        )
120       ((boundp 'NEMACS)
121        ;; for NEmacs and NEpoch
122        (require 'emu-nemacs)
123        )
124       (t
125        ;; for Emacs 19
126        (require 'emu-e19)
127        (require 'emu-latin1)
128        ))
129
130
131 ;;; @ MIME charset
132 ;;;
133
134 (defun charsets-to-mime-charset (charsets)
135   "Return MIME charset from list of charset CHARSETS.
136 This function refers variable `charsets-mime-charset-alist'
137 and `default-mime-charset'."
138   (if charsets
139       (or (catch 'tag
140             (let ((rest charsets-mime-charset-alist)
141                   cell)
142               (while (setq cell (car rest))
143                 (if (catch 'not-subset
144                       (let ((set1 charsets)
145                             (set2 (car cell))
146                             obj)
147                         (while set1
148                           (setq obj (car set1))
149                           (or (memq obj set2)
150                               (throw 'not-subset nil))
151                           (setq set1 (cdr set1)))
152                         t))
153                     (throw 'tag (cdr cell)))
154                 (setq rest (cdr rest)))))
155           default-mime-charset)))
156
157
158 ;;; @ Emacs 19 emulation
159 ;;;
160
161 (defun-maybe minibuffer-prompt-width ()
162   "Return the display width of the minibuffer prompt."
163   (save-excursion
164     (set-buffer (window-buffer (minibuffer-window)))
165     (current-column)))
166
167
168 ;;; @ Emacs 19.29 emulation
169 ;;;
170
171 (defvar path-separator ":"
172   "Character used to separate concatenated paths.")
173
174 (defun-maybe buffer-substring-no-properties (start end)
175   "Return the characters of part of the buffer, without the text properties.
176 The two arguments START and END are character positions;
177 they can be in either order. [Emacs 19.29 emulating function]"
178   (let ((string (buffer-substring start end)))
179     (set-text-properties 0 (length string) nil string)
180     string))
181
182 (defun-maybe match-string (num &optional string)
183   "Return string of text matched by last search.
184 NUM specifies which parenthesized expression in the last regexp.
185  Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
186 Zero means the entire text matched by the whole regexp or whole string.
187 STRING should be given if the last search was by `string-match' on STRING.
188 \[Emacs 19.29 emulating function]"
189   (if (match-beginning num)
190       (if string
191           (substring string (match-beginning num) (match-end num))
192         (buffer-substring (match-beginning num) (match-end num)))))
193
194 (or running-emacs-19_29-or-later
195     running-xemacs
196     ;; for Emacs 19.28 or earlier
197     (fboundp 'si:read-string)
198     (progn
199       (fset 'si:read-string (symbol-function 'read-string))
200       
201       (defun read-string (prompt &optional initial-input history)
202         "Read a string from the minibuffer, prompting with string PROMPT.
203 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
204 The third arg HISTORY, is dummy for compatibility. [emu.el]
205 See `read-from-minibuffer' for details of HISTORY argument."
206         (si:read-string prompt initial-input))
207       ))
208
209
210 ;;; @ Emacs 19.30 emulation
211 ;;;
212
213 ;; This function was imported Emacs 19.30.
214 (defun-maybe add-to-list (list-var element)
215   "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
216 If you want to use `add-to-list' on a variable that is not defined
217 until a certain package is loaded, you should put the call to `add-to-list'
218 into a hook function that will be run only after loading the package.
219 \[Emacs 19.30 emulating function]"
220   (or (member element (symbol-value list-var))
221       (set list-var (cons element (symbol-value list-var)))))
222
223 (cond ((fboundp 'insert-file-contents-literally)
224        )
225       ((boundp 'file-name-handler-alist)
226        (defun insert-file-contents-literally
227          (filename &optional visit beg end replace)
228          "Like `insert-file-contents', q.v., but only reads in the file.
229 A buffer may be modified in several ways after reading into the buffer due
230 to advanced Emacs features, such as file-name-handlers, format decoding,
231 find-file-hooks, etc.
232   This function ensures that none of these modifications will take place.
233 \[Emacs 19.30 emulating function]"
234          (let (file-name-handler-alist)
235            (insert-file-contents filename visit beg end replace)))
236        )
237       (t
238        (defalias 'insert-file-contents-literally 'insert-file-contents)
239        ))
240
241
242 ;;; @ Emacs 19.31 emulation
243 ;;;
244
245 (defun-maybe buffer-live-p (object)
246   "Return non-nil if OBJECT is a buffer which has not been killed.
247 Value is nil if OBJECT is not a buffer or if it has been killed.
248 \[Emacs 19.31 emulating function]"
249   (and object
250        (get-buffer object)
251        (buffer-name (get-buffer object))))
252
253 ;; This macro was imported Emacs 19.33.
254 (defmacro-maybe save-selected-window (&rest body)
255   "Execute BODY, then select the window that was selected before BODY.
256 \[Emacs 19.31 emulating function]"
257   (list 'let
258         '((save-selected-window-window (selected-window)))
259         (list 'unwind-protect
260               (cons 'progn body)
261               (list 'select-window 'save-selected-window-window))))
262
263
264 ;;; @ Emacs 20.1 emulation
265 ;;;
266
267 ;; This macro was imported Emacs 20.2.
268 (defmacro-maybe when (cond &rest body)
269   "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
270   (list 'if cond (cons 'progn body)))
271
272 (defmacro-maybe save-current-buffer (&rest body)
273   "Save the current buffer; execute BODY; restore the current buffer.
274 Executes BODY just like `progn'."
275   (` (let ((orig-buffer (current-buffer)))
276        (unwind-protect
277            (progn (,@ body))
278          (set-buffer orig-buffer)))))
279
280 ;; This macro was imported Emacs 20.2.
281 (defmacro-maybe with-current-buffer (buffer &rest body)
282   "Execute the forms in BODY with BUFFER as the current buffer.
283 The value returned is the value of the last form in BODY.
284 See also `with-temp-buffer'."
285   (` (save-current-buffer
286        (set-buffer (, buffer))
287        (,@ body))))
288
289 ;; This macro was imported Emacs 20.2.
290 (defmacro-maybe with-temp-file (file &rest forms)
291   "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
292 The value of the last form in FORMS is returned, like `progn'.
293 See also `with-temp-buffer'."
294   (let ((temp-file (make-symbol "temp-file"))
295         (temp-buffer (make-symbol "temp-buffer")))
296     `(let ((,temp-file ,file)
297            (,temp-buffer
298             (get-buffer-create (generate-new-buffer-name " *temp file*"))))
299        (unwind-protect
300            (prog1
301                (with-current-buffer ,temp-buffer
302                  ,@forms)
303              (with-current-buffer ,temp-buffer
304                (widen)
305                (write-region (point-min) (point-max) ,temp-file nil 0)))
306          (and (buffer-name ,temp-buffer)
307               (kill-buffer ,temp-buffer))))))
308
309 ;; This macro was imported Emacs 20.2.
310 (defmacro-maybe with-temp-buffer (&rest forms)
311   "Create a temporary buffer, and evaluate FORMS there like `progn'.
312 See also `with-temp-file' and `with-output-to-string'."
313   (let ((temp-buffer (make-symbol "temp-buffer")))
314     (` (let (((, temp-buffer)
315               (get-buffer-create (generate-new-buffer-name " *temp*"))))
316          (unwind-protect
317              (with-current-buffer (, temp-buffer)
318                (,@ forms))
319            (and (buffer-name (, temp-buffer))
320                 (kill-buffer (, temp-buffer))))))))
321
322 ;; This function was imported Emacs 20.3.
323 (defun-maybe last (x &optional n)
324   "Return the last link of the list X.  Its car is the last element.
325 If X is nil, return nil.
326 If N is non-nil, return the Nth-to-last link of X.
327 If N is bigger than the length of X, return X."
328   (if n
329       (let ((m 0) (p x))
330         (while (consp p)
331           (setq m (1+ m) p (cdr p)))
332         (if (<= n 0) p
333           (if (< n m) (nthcdr (- m n) x) x)))
334     (while (cdr x)
335       (setq x (cdr x)))
336     x))
337
338 ;; This function was imported Emacs 20.3. (cl function)
339 (defun-maybe butlast (x &optional n)
340   "Returns a copy of LIST with the last N elements removed."
341   (if (and n (<= n 0)) x
342     (nbutlast (copy-sequence x) n)))
343   
344 ;; This function was imported Emacs 20.3. (cl function)
345 (defun-maybe nbutlast (x &optional n)
346   "Modifies LIST to remove the last N elements."
347   (let ((m (length x)))
348     (or n (setq n 1))
349     (and (< n m)
350          (progn
351            (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
352            x))))
353
354 ;; This function was imported from XEmacs 21.
355 (defun-maybe split-string (string &optional pattern)
356   "Return a list of substrings of STRING which are separated by PATTERN.
357 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
358   (or pattern
359       (setq pattern "[ \f\t\n\r\v]+"))
360   ;; The FSF version of this function takes care not to cons in case
361   ;; of infloop.  Maybe we should synch?
362   (let (parts (start 0))
363     (while (string-match pattern string start)
364       (setq parts (cons (substring string start (match-beginning 0)) parts)
365             start (match-end 0)))
366     (nreverse (cons (substring string start) parts))))
367
368
369 ;;; @ Emacs 20.3 emulation
370 ;;;
371
372 (defmacro-maybe string-as-unibyte (string)
373   "Return a unibyte string with the same individual bytes as STRING.
374 If STRING is unibyte, the result is STRING itself.
375 \[Emacs 20.3 emulating macro]"
376   string)
377
378 (defmacro-maybe string-as-multibyte (string)
379   "Return a multibyte string with the same individual bytes as STRING.
380 If STRING is multibyte, the result is STRING itself.
381 \[Emacs 20.3 emulating macro]"
382   string)
383
384
385 ;;; @ XEmacs emulation
386 ;;;
387
388 (defun-maybe functionp (obj)
389   "Returns t if OBJ is a function, nil otherwise.
390 \[XEmacs emulating function]"
391   (or (subrp obj)
392       (byte-code-function-p obj)
393       (and (symbolp obj)(fboundp obj))
394       (and (consp obj)(eq (car obj) 'lambda))
395       ))
396
397 (defun-maybe point-at-eol (&optional arg buffer)
398   "Return the character position of the last character on the current line.
399 With argument N not nil or 1, move forward N - 1 lines first.
400 If scan reaches end of buffer, return that position.
401 This function does not move point. [XEmacs emulating function]"
402   (save-excursion
403     (if buffer
404         (set-buffer buffer)
405       )
406     (if arg
407         (forward-line (1- arg))
408       )
409     (end-of-line)
410     (point)))
411
412
413 ;;; @ for XEmacs 20
414 ;;;
415
416 (or (fboundp 'char-int)
417     (fset 'char-int (symbol-function 'identity))
418     )
419 (or (fboundp 'int-char)
420     (fset 'int-char (symbol-function 'identity))
421     )
422 (or (fboundp 'char-or-char-int-p)
423     (fset 'char-or-char-int-p (symbol-function 'integerp))
424     )
425
426
427 ;;; @ for text/richtext and text/enriched
428 ;;;
429
430 (cond ((fboundp 'richtext-decode)
431        ;; have richtext.el
432        )
433       ((or running-emacs-19_29-or-later running-xemacs-19_14-or-later)
434        ;; have enriched.el
435        (autoload 'richtext-decode "richtext")
436        (or (assq 'text/richtext format-alist)
437            (setq format-alist
438                  (cons
439                   (cons 'text/richtext
440                         '("Extended MIME text/richtext format."
441                           "Content-[Tt]ype:[ \t]*text/richtext"
442                           richtext-decode richtext-encode t enriched-mode))
443                   format-alist)))
444        )
445       (t
446        ;; don't have enriched.el
447        (autoload 'richtext-decode "tinyrich")
448        (autoload 'enriched-decode "tinyrich")
449        ))
450
451
452 ;;; @ end
453 ;;;
454
455 (provide 'emu)
456
457 ;;; emu.el ends here