* mh-comp.el (mh-show-buffer-message-number): Replace (car
[bpt/emacs.git] / lisp / mh-e / mh-acros.el
CommitLineData
863e5e39
BW
1;;; mh-acros.el --- Macros used in MH-E
2
549afb31 3;; Copyright (C) 2004, 2006 Free Software Foundation, Inc.
863e5e39
BW
4
5;; Author: Satyaki Das <satyaki@theforce.stanford.edu>
6;; Maintainer: Bill Wohler <wohler@newt.com>
7;; Keywords: mail
8;; See: mh-e.el
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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
3a35cf56
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
863e5e39
BW
26
27;;; Commentary:
28
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:
32;;
33;; (eval-when-compile (require 'mh-acros))
34;;
5a4aad03 35;; It is so named with a silent "m" so that it is compiled first. Otherwise,
863e5e39
BW
36;; "make recompile" in Emacs 21.4 fails.
37
38;;; Change Log:
39
40;;; Code:
41
42(require 'cl)
e495eaec 43(require 'advice)
863e5e39
BW
44
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 ()
5a4aad03
BW
50 "Macro to load \"cl\" if needed.
51Some versions of \"cl\" produce code for the expansion of
52\(setf (gethash ...) ...) that uses functions in \"cl\" at run
53time. This macro recognizes that and loads \"cl\" where
2dcf34f9 54appropriate."
863e5e39 55 (if (eq (car (macroexpand '(setf (gethash foo bar) baz))) 'cl-puthash)
eccf9613 56 `(require 'cl)
863e5e39
BW
57 `(eval-when-compile (require 'cl))))
58
cee9f5c6 59;; Macros to generate correct code for different emacs variants
863e5e39
BW
60
61(defmacro mh-do-in-gnu-emacs (&rest body)
62 "Execute BODY if in GNU Emacs."
63 (unless (featurep 'xemacs) `(progn ,@body)))
64(put 'mh-do-in-gnu-emacs 'lisp-indent-hook 'defun)
65
66(defmacro mh-do-in-xemacs (&rest body)
67 "Execute BODY if in GNU Emacs."
68 (when (featurep 'xemacs) `(progn ,@body)))
69(put 'mh-do-in-xemacs 'lisp-indent-hook 'defun)
70
71(defmacro mh-funcall-if-exists (function &rest args)
72 "Call FUNCTION with ARGS as parameters if it exists."
e495eaec
BW
73 (when (fboundp function)
74 `(when (fboundp ',function)
75 (funcall ',function ,@args))))
863e5e39 76
549afb31
BW
77(defmacro mh-defun-compat (function arg-list &rest body)
78 "This is a macro to define functions which are not defined.
79It is used for functions which were added to Emacs recently.
80If FUNCTION is not defined then it is defined to have argument
81list, ARG-LIST and body, BODY."
82 (let ((defined-p (fboundp function)))
83 (unless defined-p
84 `(defun ,function ,arg-list ,@body))))
85(put 'mh-defun-compat 'lisp-indent-function 'defun)
86
87(defmacro mh-defmacro-compat (function arg-list &rest body)
88 "This is a macro to define functions which are not defined.
89It is used for macros which were added to Emacs recently.
90If FUNCTION is not defined then it is defined to have argument
91list, ARG-LIST and body, BODY."
92 (let ((defined-p (fboundp function)))
93 (unless defined-p
94 `(defmacro ,function ,arg-list ,@body))))
95(put 'mh-defmacro-compat 'lisp-indent-function 'defun)
96
863e5e39
BW
97(defmacro mh-make-local-hook (hook)
98 "Make HOOK local if needed.
2dcf34f9
BW
99XEmacs and versions of GNU Emacs before 21.1 require
100`make-local-hook' to be called."
863e5e39
BW
101 (when (and (fboundp 'make-local-hook)
102 (not (get 'make-local-hook 'byte-obsolete-info)))
103 `(make-local-hook ,hook)))
104
105(defmacro mh-mark-active-p (check-transient-mark-mode-flag)
106 "A macro that expands into appropriate code in XEmacs and nil in GNU Emacs.
2dcf34f9
BW
107In GNU Emacs if CHECK-TRANSIENT-MARK-MODE-FLAG is non-nil then
108check if variable `transient-mark-mode' is active."
863e5e39
BW
109 (cond ((featurep 'xemacs) ;XEmacs
110 `(and (boundp 'zmacs-regions) zmacs-regions (region-active-p)))
111 ((not check-transient-mark-mode-flag) ;GNU Emacs
112 `(and (boundp 'mark-active) mark-active))
113 (t ;GNU Emacs
114 `(and (boundp 'transient-mark-mode) transient-mark-mode
115 (boundp 'mark-active) mark-active))))
116
117(defmacro mh-defstruct (name-spec &rest fields)
5a4aad03
BW
118 "Replacement for `defstruct' from the \"cl\" package.
119The `defstruct' in the \"cl\" library produces compiler warnings,
120and generates code that uses functions present in \"cl\" at
2dcf34f9
BW
121run-time. This is a partial replacement, that avoids these
122issues.
123
124NAME-SPEC declares the name of the structure, while FIELDS
125describes the various structure fields. Lookup `defstruct' for
126more details."
863e5e39
BW
127 (let* ((struct-name (if (atom name-spec) name-spec (car name-spec)))
128 (conc-name (or (and (consp name-spec)
129 (cadr (assoc :conc-name (cdr name-spec))))
130 (format "%s-" struct-name)))
131 (predicate (intern (format "%s-p" struct-name)))
132 (constructor (or (and (consp name-spec)
133 (cadr (assoc :constructor (cdr name-spec))))
134 (intern (format "make-%s" struct-name))))
135 (field-names (mapcar #'(lambda (x) (if (atom x) x (car x))) fields))
136 (field-init-forms (mapcar #'(lambda (x) (and (consp x) (cadr x)))
137 fields))
138 (struct (gensym "S"))
139 (x (gensym "X"))
140 (y (gensym "Y")))
141 `(progn
142 (defun* ,constructor (&key ,@(mapcar* #'(lambda (x y) (list x y))
143 field-names field-init-forms))
d103d8b3 144 (list (quote ,struct-name) ,@field-names))
863e5e39 145 (defun ,predicate (arg)
d103d8b3
BW
146 (and (consp arg) (eq (car arg) (quote ,struct-name))))
147 ,@(loop for x from 1
863e5e39
BW
148 for y in field-names
149 collect `(defmacro ,(intern (format "%s%s" conc-name y)) (z)
150 (list 'nth ,x z)))
151 (quote ,struct-name))))
152
4501e6fb
BW
153;; A better solution would be to use Stefan's change in bytecomp.el.
154;; If it were checked in, we can drop the advice to require and it
155;; will make things nicer elsewhere too.
eccf9613
BW
156(defadvice require (around mh-prefer-el activate)
157 "Modify `require' to load uncompiled MH-E files."
158 (or (featurep (ad-get-arg 0))
159 (and (string-match "^mh-" (symbol-name (ad-get-arg 0)))
160 (load (format "%s.el" (ad-get-arg 0)) t t))
161 ad-do-it))
863e5e39 162
9a51cf9e
BW
163(defmacro mh-assoc-ignore-case (key alist)
164 "Check if KEY is present in ALIST while ignoring case to do the comparison.
2dcf34f9
BW
165Compatibility macro for Emacs versions that lack `assoc-string',
166introduced in Emacs 22."
9a51cf9e
BW
167 (if (fboundp 'assoc-string)
168 `(assoc-string ,key ,alist t)
169 `(assoc-ignore-case ,key ,alist)))
170
863e5e39
BW
171(provide 'mh-acros)
172
cee9f5c6
BW
173;; Local Variables:
174;; no-byte-compile: t
175;; indent-tabs-mode: nil
176;; sentence-end-double-space: nil
177;; End:
863e5e39 178
b22103fe 179;; arch-tag: b383b49a-494f-4ed0-a30a-cb6d5d2da4ff
863e5e39 180;;; mh-acros.el ends here