(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / mh-e / mh-acros.el
blob455daf687a308c3b41b9d91645a6a757c40bdde7
1 ;;; mh-acros.el --- Macros used in MH-E
3 ;; Copyright (C) 2004 Free Software Foundation, Inc.
5 ;; Author: Satyaki Das <satyaki@theforce.stanford.edu>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
7 ;; Keywords: mail
8 ;; See: mh-e.el
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This file contains macros that would normally be in mh-utils.el except that
30 ;; their presence there would cause a dependency loop with mh-customize.el.
31 ;; This file must always be included like this:
33 ;; (eval-when-compile (require 'mh-acros))
35 ;; It is so named with a silent `m' so that it is compiled first. Otherwise,
36 ;; "make recompile" in Emacs 21.4 fails.
38 ;;; Change Log:
40 ;;; Code:
42 (require 'cl)
43 (require 'advice)
45 ;; The Emacs coding conventions require that the cl package not be required at
46 ;; runtime. However, the cl package in versions of Emacs prior to 21.4 left cl
47 ;; routines in their macro expansions. Use mh-require-cl to provide the cl
48 ;; routines in the best way possible.
49 (defmacro mh-require-cl ()
50 "Macro to load `cl' if needed.
51 Some versions of `cl' produce code for the expansion of
52 \(setf (gethash ...) ...) that uses functions in `cl' at run time. This macro
53 recognizes that and loads `cl' where appropriate."
54 (if (eq (car (macroexpand '(setf (gethash foo bar) baz))) 'cl-puthash)
55 `(require 'cl)
56 `(eval-when-compile (require 'cl))))
58 ;;; Macros to generate correct code for different emacs variants
60 (defmacro mh-do-in-gnu-emacs (&rest body)
61 "Execute BODY if in GNU Emacs."
62 (unless (featurep 'xemacs) `(progn ,@body)))
63 (put 'mh-do-in-gnu-emacs 'lisp-indent-hook 'defun)
65 (defmacro mh-do-in-xemacs (&rest body)
66 "Execute BODY if in GNU Emacs."
67 (when (featurep 'xemacs) `(progn ,@body)))
68 (put 'mh-do-in-xemacs 'lisp-indent-hook 'defun)
70 (defmacro mh-funcall-if-exists (function &rest args)
71 "Call FUNCTION with ARGS as parameters if it exists."
72 (when (fboundp function)
73 `(when (fboundp ',function)
74 (funcall ',function ,@args))))
76 (defmacro mh-make-local-hook (hook)
77 "Make HOOK local if needed.
78 XEmacs and versions of GNU Emacs before 21.1 require `make-local-hook' to be
79 called."
80 (when (and (fboundp 'make-local-hook)
81 (not (get 'make-local-hook 'byte-obsolete-info)))
82 `(make-local-hook ,hook)))
84 (defmacro mh-mark-active-p (check-transient-mark-mode-flag)
85 "A macro that expands into appropriate code in XEmacs and nil in GNU Emacs.
86 In GNU Emacs if CHECK-TRANSIENT-MARK-MODE-FLAG is non-nil then check if
87 variable `transient-mark-mode' is active."
88 (cond ((featurep 'xemacs) ;XEmacs
89 `(and (boundp 'zmacs-regions) zmacs-regions (region-active-p)))
90 ((not check-transient-mark-mode-flag) ;GNU Emacs
91 `(and (boundp 'mark-active) mark-active))
92 (t ;GNU Emacs
93 `(and (boundp 'transient-mark-mode) transient-mark-mode
94 (boundp 'mark-active) mark-active))))
96 (defmacro mh-defstruct (name-spec &rest fields)
97 "Replacement for `defstruct' from the `cl' package.
98 The `defstruct' in the `cl' library produces compiler warnings, and generates
99 code that uses functions present in `cl' at run-time. This is a partial
100 replacement, that avoids these issues.
102 NAME-SPEC declares the name of the structure, while FIELDS describes the
103 various structure fields. Lookup `defstruct' for more details."
104 (let* ((struct-name (if (atom name-spec) name-spec (car name-spec)))
105 (conc-name (or (and (consp name-spec)
106 (cadr (assoc :conc-name (cdr name-spec))))
107 (format "%s-" struct-name)))
108 (predicate (intern (format "%s-p" struct-name)))
109 (constructor (or (and (consp name-spec)
110 (cadr (assoc :constructor (cdr name-spec))))
111 (intern (format "make-%s" struct-name))))
112 (field-names (mapcar #'(lambda (x) (if (atom x) x (car x))) fields))
113 (field-init-forms (mapcar #'(lambda (x) (and (consp x) (cadr x)))
114 fields))
115 (struct (gensym "S"))
116 (x (gensym "X"))
117 (y (gensym "Y")))
118 `(progn
119 (defun* ,constructor (&key ,@(mapcar* #'(lambda (x y) (list x y))
120 field-names field-init-forms))
121 (list (quote ,struct-name) ,@field-names))
122 (defun ,predicate (arg)
123 (and (consp arg) (eq (car arg) (quote ,struct-name))))
124 ,@(loop for x from 1
125 for y in field-names
126 collect `(defmacro ,(intern (format "%s%s" conc-name y)) (z)
127 (list 'nth ,x z)))
128 (quote ,struct-name))))
130 (defadvice require (around mh-prefer-el activate)
131 "Modify `require' to load uncompiled MH-E files."
132 (or (featurep (ad-get-arg 0))
133 (and (string-match "^mh-" (symbol-name (ad-get-arg 0)))
134 (load (format "%s.el" (ad-get-arg 0)) t t))
135 ad-do-it))
137 (provide 'mh-acros)
139 ;;; Local Variables:
140 ;;; no-byte-compile: t
141 ;;; indent-tabs-mode: nil
142 ;;; sentence-end-double-space: nil
143 ;;; End:
145 ;; arch-tag: b383b49a-494f-4ed0-a30a-cb6d5d2da4ff
146 ;;; mh-acros.el ends here