Commit | Line | Data |
---|---|---|
863e5e39 BW |
1 | ;;; mh-acros.el --- Macros used in MH-E |
2 | ||
e495eaec | 3 | ;; Copyright (C) 2004 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 | ;; | |
35 | ;; It is so named with a silent `m' so that it is compiled first. Otherwise, | |
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 () | |
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) | |
eccf9613 | 55 | `(require 'cl) |
863e5e39 BW |
56 | `(eval-when-compile (require 'cl)))) |
57 | ||
58 | ;;; Macros to generate correct code for different emacs variants | |
59 | ||
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) | |
64 | ||
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) | |
69 | ||
70 | (defmacro mh-funcall-if-exists (function &rest args) | |
71 | "Call FUNCTION with ARGS as parameters if it exists." | |
e495eaec BW |
72 | (when (fboundp function) |
73 | `(when (fboundp ',function) | |
74 | (funcall ',function ,@args)))) | |
863e5e39 BW |
75 | |
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))) | |
83 | ||
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)))) | |
95 | ||
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. | |
101 | ||
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)) | |
d103d8b3 | 121 | (list (quote ,struct-name) ,@field-names)) |
863e5e39 | 122 | (defun ,predicate (arg) |
d103d8b3 BW |
123 | (and (consp arg) (eq (car arg) (quote ,struct-name)))) |
124 | ,@(loop for x from 1 | |
863e5e39 BW |
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)))) | |
129 | ||
eccf9613 BW |
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)) | |
863e5e39 | 136 | |
9a51cf9e BW |
137 | (defmacro mh-assoc-ignore-case (key alist) |
138 | "Check if KEY is present in ALIST while ignoring case to do the comparison. | |
139 | Compatibility macro for Emacs versions that lack `assoc-string', introduced in | |
140 | Emacs 22." | |
141 | (if (fboundp 'assoc-string) | |
142 | `(assoc-string ,key ,alist t) | |
143 | `(assoc-ignore-case ,key ,alist))) | |
144 | ||
863e5e39 BW |
145 | (provide 'mh-acros) |
146 | ||
147 | ;;; Local Variables: | |
148 | ;;; no-byte-compile: t | |
149 | ;;; indent-tabs-mode: nil | |
150 | ;;; sentence-end-double-space: nil | |
151 | ;;; End: | |
152 | ||
b22103fe | 153 | ;; arch-tag: b383b49a-494f-4ed0-a30a-cb6d5d2da4ff |
863e5e39 | 154 | ;;; mh-acros.el ends here |