;; /os, multi-os config tool
;; https://github.com/raxod502/radian/blob/develop/emacs/radian.el#L169-L188
(defmacro operating-system-p (os)
"Return non-nil if OS corresponds to the current operating system.
Allowable values for OS (not quoted) are `macOS', `osx',
`windows', `linux', `unix'."
(pcase os
(`unix `(not (memq system-type '(ms-dos windows-nt cygwin))))
((or `macOS `osx) `(eq system-type 'darwin))
(`linux `(not (memq system-type
'(darwin ms-dos windows-nt cygwin))))
(`windows `(memq system-type '(ms-dos windows-nt cygwin)))))
(defmacro eval-with/os (os &rest body)
"If OS corresponds to the current operating system, eval and return BODY.
If not, return nil.
Allowable values for OS (not quoted) are `macOS', `osx',
`windows', `linux', `unix'."
(declare (indent 1))
`(when (operating-system-p ,os)
,@body))
多设备则依赖载入的常量,目前是
;; /device, multi-device config tool
(require 'cl-macs)
;; please name your device other place before
;; such as mac1~100 (hopefully)
(defvar *current-device* 'unknow
"Not `unknow' if you name it" )
(cl-defmacro value/device (&key mac-a pc-a linux-a server-a default)
"Return Value depended on `*current-device*', each clause using a keyword"
(pcase *current-device*
;; maintain device list here
(`mac-a mac-a)
(`pc-a pc-a)
(`linux-a inux-a)
(`server-a server-a)
((or `default `unknow) default)))
(defmacro defconst/device (id &rest rhs)
"Define constant based on device name"
`(defconst ,id (value/device ,@rhs)))
(defmacro defvar/device (id &rest rhs)
"Define variable based on device name"
`(defvar ,id (value/device ,@rhs)))
;;; test
*current-device*
;; => "unknow"
(value/device :mac-a "test")
;; => nil
;; because :default nil
(defconst/device test :default "default value")
(message test)
;; => "default value"
(defmacro select-default (defatult &rest cells)
"Select Value base on `os-name'"
(declare (indent 1))
`(let ((re (read/os '(,@cells))))
(if re re ,defatult)))
(defmacro select-specific (default &rest cells)
"Select Value base on `device-name'(priority) and `os-name'"
(declare (indent 1))
`(let ((re (read/device '(,@cells))))
(if re re (select-default ,@default ,@cells))))
最内层,处理 alist (得到想要的 symbol),手动 eval 之后返回。
(defmacro read/os (alist)
"Get value from alist base on `os-name' "
(declare (indent 1))
(eval `(cdr (assq os-name ,alist))))
(defmacro read/device (alist)
"Get value from alist base on `os-name' "
(declare (indent 1))
(eval `(cdr (assq device-name ,alist))))
(defconst os-name
(cond
;; Translate system-type to os-name
((eq system-type 'darwin) 'macOS)
((memq system-type '(ms-dos windows-nt cygwin)) 'windows)
(t 'linux))
"Value base on `system-type'.
Three candidates `macOS', `windows' and `linux'.")
(defconst device-name
;; Translate system-name to device-name
(cdr (assq (intern system-name) *device-alist*))
"Value base on `system-name' and `device-alist'")
(defconst *device-alist*
'(;; name your device here
(ingtshans-MacBook-Pro.local . 19mbp)
;; end
)
"Naming your device base on `system-name'.
You can't use any candidate of `os-name'")
反向整理就得到一个还可用的脚手架了。
同时也符合,配置信息灵活按需添加的原则。比如
;; version 1
(defconst font-size-int 13)
;; version 2
;; macOS need adapt
(defconst font-size-int
(select-default 13
(macOS . 15)))
;; version 3
;; win need adapt
(defconst font-size-int
(select-default 13
(macOS . 15)
(windows . 17)))