core-lib: refactor associate!

This commit is contained in:
Henrik Lissner 2017-01-28 02:04:47 -05:00
parent c71b8ac438
commit fb53c8fda7

View file

@ -63,13 +63,12 @@ Examples:
(match (plist-get rest :match)) (match (plist-get rest :match))
(files (plist-get rest :files)) (files (plist-get rest :files))
(pred (plist-get rest :when))) (pred (plist-get rest :when)))
`(progn (cond ((or files in pred)
(,@(cond ((or files in pred)
(when (and files (not (or (listp files) (stringp files)))) (when (and files (not (or (listp files) (stringp files))))
(user-error "associate! :files expects a string or list of strings")) (user-error "associate! :files expects a string or list of strings"))
(let ((hook-name (intern (format "doom--init-mode-%s" mode)))) (let ((hook-name (intern (format "doom--init-mode-%s" mode))))
`(progn (macroexp-progn
(defun ,hook-name () (list `(defun ,hook-name ()
(when (and ,(if match `(if buffer-file-name (string-match-p ,match buffer-file-name)) t) (when (and ,(if match `(if buffer-file-name (string-match-p ,match buffer-file-name)) t)
(or ,(not files) (or ,(not files)
(and (boundp ',mode) (and (boundp ',mode)
@ -78,15 +77,16 @@ Examples:
(or (not ,pred) (or (not ,pred)
(funcall ,pred buffer-file-name))) (funcall ,pred buffer-file-name)))
(,mode 1))) (,mode 1)))
,@(if (and in (listp in)) (if (and in (listp in))
(macroexp-progn
(mapcar (lambda (h) `(add-hook ',h ',hook-name)) (mapcar (lambda (h) `(add-hook ',h ',hook-name))
(mapcar (lambda (m) (intern (format "%s-hook" m))) in)) (mapcar (lambda (m) (intern (format "%s-hook" m))) in)))
`((add-hook 'find-file-hook ',hook-name)))))) `(add-hook 'find-file-hook ',hook-name))))))
(match (match
`(add-to-list ',(if minor 'doom-auto-minor-mode-alist 'auto-mode-alist) `(add-to-list ',(if minor 'doom-auto-minor-mode-alist 'auto-mode-alist)
(cons ,match ',mode))) (cons ,match ',mode)))
(t (user-error "associate! invalid rules for mode [%s] (in %s) (match %s) (files %s)" (t (user-error "associate! invalid rules for mode [%s] (in %s) (match %s) (files %s)"
mode in match files))))))) mode in match files)))))
;; Register keywords for proper indentation (see `map!') ;; Register keywords for proper indentation (see `map!')
(put ':prefix 'lisp-indent-function 'defun) (put ':prefix 'lisp-indent-function 'defun)