Add define-project-type macro

This commit is contained in:
Henrik Lissner 2016-04-16 21:36:24 -04:00
parent dd3d51e750
commit 988f09365b
8 changed files with 142 additions and 128 deletions

View file

@ -82,36 +82,90 @@ Examples:
(if (listp hook) hook (list hook))))) funcs)
`(progn ,@forms)))
(cl-defmacro associate! (mode &key in
(cl-defmacro associate! (mode &key minor
&key in
&key match
&key files
&allow-other-keys)
"Associate a major or minor mode to certain patterns and project files."
(declare (indent 1))
(let* ((minor-p (assoc mode minor-mode-alist)))
`(progn
(,@(cond ((or files in)
(when (and files (not (or (listp files)
(stringp files))))
(user-error "associate! :files expects a string or list of strings"))
(let ((hook-name (intern (format "narf--init-mode-%s" mode))))
`(progn
(defun ,hook-name ()
(when (and (if ,match (string-match-p ,match (buffer-file-name)) t)
(or ,(not files)
(and (boundp ',mode)
(not ,mode)
(narf/project-has-files ,@(-list files)))))
(,mode 1)))
,@(if (and in (listp in))
(mapcar (lambda (h) `(add-hook ',h ',hook-name))
(mapcar (lambda (m) (intern (format "%s-hook" m))) in))
`((add-hook 'find-file-hook ',hook-name))))))
(match
`(add-to-list ',(if minor-p 'narf-auto-minor-mode-alist 'auto-mode-alist)
(cons ,match ',mode)))
(t (user-error "associate! invalid rules for mode [%s] (in %s) (match %s) (files %s)"
mode in match files)))))))
`(progn
(,@(cond ((or files in)
(when (and files (not (or (listp files) (stringp files))))
(user-error "associate! :files expects a string or list of strings"))
(let ((hook-name (intern (format "narf--init-mode-%s" mode))))
`(progn
(defun ,hook-name ()
(when (and ,(if match `(string-match-p ,match buffer-file-name) t)
(or ,(not files)
(and (boundp ',mode)
(not ,mode)
(narf/project-has-files ,@(-list files)))))
(,mode 1)))
,@(if (and in (listp in))
(mapcar (lambda (h) `(add-hook ',h ',hook-name))
(mapcar (lambda (m) (intern (format "%s-hook" m))) in))
`((add-hook 'find-file-hook ',hook-name))))))
(match
`(add-to-list ',(if minor 'narf-auto-minor-mode-alist 'auto-mode-alist)
(cons ,match ',mode)))
(t (user-error "associate! invalid rules for mode [%s] (in %s) (match %s) (files %s)"
mode in match files))))))
(defmacro define-project-type! (name lighter &rest body)
"Define a minor mode for a specific framework, library or project type. e.g.
(define-project-type! angularjs \"angjs\"
:modes (js2-mode)
:files (\"package.json\"))"
(declare (indent 2))
(let* ((mode-name (format "%s-project-mode" name))
(mode (intern mode-name))
(mode-map (intern (format "%s-map" mode-name))))
(let ((modes (plist-get body :modes))
(match (plist-get body :match))
(files (plist-get body :files))
(build (plist-get body :build))
(bind (plist-get body :bind))
elem)
(while (keywordp (car body))
(pop body)
(pop body))
`(progn
(define-minor-mode ,mode
"Auto-generated by `define-project-type!'"
:init-value nil
:lighter ,(concat " " lighter)
:keymap (make-sparse-keymap)
(after! yasnippet
(when (boundp 'yas--extra-modes)
(add-hook ',(intern (concat mode-name "-hook"))
(lambda ()
(if (symbol-value ',mode)
(yas-activate-extra-mode ',mode)
(yas-deactivate-extra-mode ',mode)))))))
(after! company-dict
(add-to-list 'company-dict-minor-mode-list ',mode))
,(when build
(when (listp build)
(setq build (car-safe (cdr-safe build))))
(let ((cmd (or (car-safe build) build))
(file (car-safe (cdr-safe build))))
`(define-builder! ,mode ,cmd ,file)))
,(when bind
`(map! :map ,mode-map ,bind))
(associate! ,mode
:minor t
:in ,modes
:match ,match
:files ,files)
,@body
',mode))))
(after! evil
;; Register keywords for proper indentation (see `map!')