-
Notifications
You must be signed in to change notification settings - Fork 1
/
system-specific-settings.el
139 lines (118 loc) · 4.62 KB
/
system-specific-settings.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
;;; system-specific-settings.el --- Apply settings only on certain systems
;;
;; Filename: system-specific-settings.el
;; Author: Ryan C. Thompson
;; Created: Sun Aug 17 11:34:34 2014 (-0700)
;; Version: 0.2
;; Package-Requires: ()
;; URL: https://github.com/DarwinAwardWinner/emacs-system-specific-settings
;; Keywords: configuration
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; This package defines some macros that you can use in your Emacs
;; configuration to evaluate certain settings only on specific
;; operating systems or specific hosts.
;;
;; All the relevant macros are autoloaded, so if you install this
;; through package.el, you can just use them in your initialization
;; file.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or (at
;; your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(defun system-specific-settings-do-match (value spec)
"Match VALUE against SPEC.
Returns a boolean indicating whether VALUE matches SPEC. Matching is done as follows:
* If SPEC is a function, it is called with VALUE as its only
argument and the result is returned.
* If SPEC is a cons cell with a car of `regexp' and a cdr that is
a string, the string will be matched against VALUE using
`string-match-p'. VALUE will be converted from a symbol to a
string if necessary.
* If SPEC is a conse cell with a car of `not', then VALUE will be
matched against the cdr and the result will be inverted.
* If SPEC is a list, VALUE is matched recursively against each
element of SPEC, and this returns non-nil if any element of
SPEC results in a match.
* If SPEC and VALUE are both either symbols or strings, they are
compared using `string=' (which compares symbols using their
print names).
* If SPEC is anything else, it is compared to VALUE using `equal'.
For example, if VALUE is a symbol and SPEC is a list of symbols,
this will return TRUE if VALUE occurs in SPEC.
Note that since matching is done using several different
functions, a match may not always return `t', but it will return
some non-nil value."
(cond
((functionp spec)
(funcall spec value))
((and (consp spec)
(eq (car spec) 'not))
(not (system-specific-settings-do-match value (cdr spec))))
((and (consp spec)
(eq (car spec) 'regexp)
(stringp (cdr spec)))
(when (symbolp value)
(setq value (symbol-name value)))
(string-match-p (cdr spec) value))
((ignore-errors (string= value spec)))
((listp spec)
(cl-some (apply-partially #'system-specific-settings-do-match
value)
spec))
(t
(equal value spec))))
;;;###autoload
(defmacro if-system-type-match (cond then &rest else)
"If COND matches `system-type', do THEN, else do ELSE.
Matching is done using `system-specific-settings-do-match'."
(declare (indent 2))
`(if (system-specific-settings-do-match system-type ,cond)
,then
,@else))
;;;###autoload
(defmacro if-system-name-match (cond then &rest else)
"If COND matches `system-name', do THEN, else do ELSE.
Matching is done using `system-specific-settings-do-match'."
(declare (indent 2))
`(if (system-specific-settings-do-match system-name ,cond)
,then
,@else))
;;;###autoload
(defmacro when-system-type-match (cond &rest body)
"Eval BODY only if `system-type' matches COND.
Matching is done using `system-specific-settings-do-match'."
(declare (indent 1))
`(if-system-type-match ,cond
(progn ,@body)))
;;;###autoload
(defmacro when-system-name-match (cond &rest body)
"Eval BODY only if `system-name' matches COND.
Matching is done using `system-specific-settings-do-match'."
(declare (indent 1))
`(if-system-name-match ,cond
(progn ,@body)))
(define-obsolete-function-alias 'eval-on-system-type 'when-system-type-match
"0.2")
(define-obsolete-function-alias 'eval-on-host 'when-system-name-match
"0.2")
(provide 'system-specific-settings)
;;; system-specific-settings.el ends here