Add define-project-type macro
This commit is contained in:
parent
dd3d51e750
commit
988f09365b
8 changed files with 142 additions and 128 deletions
|
@ -82,36 +82,90 @@ Examples:
|
||||||
(if (listp hook) hook (list hook))))) funcs)
|
(if (listp hook) hook (list hook))))) funcs)
|
||||||
`(progn ,@forms)))
|
`(progn ,@forms)))
|
||||||
|
|
||||||
(cl-defmacro associate! (mode &key in
|
(cl-defmacro associate! (mode &key minor
|
||||||
|
&key in
|
||||||
&key match
|
&key match
|
||||||
&key files
|
&key files
|
||||||
&allow-other-keys)
|
&allow-other-keys)
|
||||||
"Associate a major or minor mode to certain patterns and project files."
|
"Associate a major or minor mode to certain patterns and project files."
|
||||||
(declare (indent 1))
|
(declare (indent 1))
|
||||||
(let* ((minor-p (assoc mode minor-mode-alist)))
|
`(progn
|
||||||
`(progn
|
(,@(cond ((or files in)
|
||||||
(,@(cond ((or files in)
|
(when (and files (not (or (listp files) (stringp files))))
|
||||||
(when (and files (not (or (listp files)
|
(user-error "associate! :files expects a string or list of strings"))
|
||||||
(stringp files))))
|
(let ((hook-name (intern (format "narf--init-mode-%s" mode))))
|
||||||
(user-error "associate! :files expects a string or list of strings"))
|
`(progn
|
||||||
(let ((hook-name (intern (format "narf--init-mode-%s" mode))))
|
(defun ,hook-name ()
|
||||||
`(progn
|
(when (and ,(if match `(string-match-p ,match buffer-file-name) t)
|
||||||
(defun ,hook-name ()
|
(or ,(not files)
|
||||||
(when (and (if ,match (string-match-p ,match (buffer-file-name)) t)
|
(and (boundp ',mode)
|
||||||
(or ,(not files)
|
(not ,mode)
|
||||||
(and (boundp ',mode)
|
(narf/project-has-files ,@(-list files)))))
|
||||||
(not ,mode)
|
(,mode 1)))
|
||||||
(narf/project-has-files ,@(-list files)))))
|
,@(if (and in (listp in))
|
||||||
(,mode 1)))
|
(mapcar (lambda (h) `(add-hook ',h ',hook-name))
|
||||||
,@(if (and in (listp in))
|
(mapcar (lambda (m) (intern (format "%s-hook" m))) in))
|
||||||
(mapcar (lambda (h) `(add-hook ',h ',hook-name))
|
`((add-hook 'find-file-hook ',hook-name))))))
|
||||||
(mapcar (lambda (m) (intern (format "%s-hook" m))) in))
|
(match
|
||||||
`((add-hook 'find-file-hook ',hook-name))))))
|
`(add-to-list ',(if minor 'narf-auto-minor-mode-alist 'auto-mode-alist)
|
||||||
(match
|
(cons ,match ',mode)))
|
||||||
`(add-to-list ',(if minor-p 'narf-auto-minor-mode-alist 'auto-mode-alist)
|
(t (user-error "associate! invalid rules for mode [%s] (in %s) (match %s) (files %s)"
|
||||||
(cons ,match ',mode)))
|
mode in match files))))))
|
||||||
(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
|
(after! evil
|
||||||
;; Register keywords for proper indentation (see `map!')
|
;; Register keywords for proper indentation (see `map!')
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
;;; module-ansible.el
|
;;; module-ansible.el
|
||||||
|
|
||||||
(after! company-dict
|
(define-project-type! ansible-mode "ans"
|
||||||
(add-to-list 'company-dict-minor-mode-list 'ansible-mode))
|
:modes (yaml-mode)
|
||||||
|
:files ("roles/"))
|
||||||
(define-minor-mode ansible-mode
|
|
||||||
:lighter " ans" :keymap (make-sparse-keymap))
|
|
||||||
(associate! ansible-mode :in (yaml-mode) :files ("roles/"))
|
|
||||||
|
|
||||||
(use-package company-ansible
|
(use-package company-ansible
|
||||||
:commands (company-ansible)
|
:commands (company-ansible)
|
||||||
|
|
|
@ -7,18 +7,11 @@
|
||||||
;; LaunchBar: https://www.obdev.at/products/launchbar
|
;; LaunchBar: https://www.obdev.at/products/launchbar
|
||||||
;;
|
;;
|
||||||
|
|
||||||
(define-minor-mode lb6-mode
|
(define-project-type! lb6 "lb6"
|
||||||
"Launchbar development mode."
|
:match "\\.lb\\(action\\|ext\\)/.+$"
|
||||||
:init-value nil
|
:build (lambda ()
|
||||||
:lighter " lb6"
|
(awhen (f-traverse-upwards (lambda (f) (f-ext? f "lbaction")))
|
||||||
(add-yas-minor-mode! 'lb6-mode))
|
(shell-command (format "open '%s'" it)))))
|
||||||
(define-builder! lb6-mode narf-lb6-reload)
|
|
||||||
(associate! lb6-mode :match "\\.lb\\(action\\|ext\\)/.+$")
|
|
||||||
|
|
||||||
(defun narf-lb6-reload ()
|
|
||||||
(interactive)
|
|
||||||
(let ((dir (f-traverse-upwards (lambda (f) (string-suffix-p ".lbaction" f)))))
|
|
||||||
(shell-command (format "open '%s'" dir))))
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
|
|
|
@ -4,10 +4,6 @@
|
||||||
:mode "\\.js$"
|
:mode "\\.js$"
|
||||||
:interpreter "node"
|
:interpreter "node"
|
||||||
:init
|
:init
|
||||||
(use-package nodejs-repl
|
|
||||||
:commands (nodejs-repl)
|
|
||||||
:config (evil-set-initial-state 'nodejs-repl-mode 'emacs))
|
|
||||||
|
|
||||||
(define-repl! js2-mode nodejs-repl)
|
(define-repl! js2-mode nodejs-repl)
|
||||||
(define-docset! js2-mode "js,javascript,nodejs,angularjs,express,jquery,mongoose")
|
(define-docset! js2-mode "js,javascript,nodejs,angularjs,express,jquery,mongoose")
|
||||||
|
|
||||||
|
@ -87,17 +83,9 @@
|
||||||
(forward-slurp "forward slurp" nil)
|
(forward-slurp "forward slurp" nil)
|
||||||
(forward-barf "forward barf" nil)))))
|
(forward-barf "forward barf" nil)))))
|
||||||
|
|
||||||
(define-minor-mode nodejs-mode
|
(use-package nodejs-repl
|
||||||
:lighter " node" :keymap (make-sparse-keymap)
|
:commands (nodejs-repl)
|
||||||
(add-yas-minor-mode! 'nodejs-mode))
|
:config (evil-set-initial-state 'nodejs-repl-mode 'emacs))
|
||||||
(associate! nodejs-mode :files ("package.json") :in (js2-mode))
|
|
||||||
|
|
||||||
(define-minor-mode electron-mode
|
|
||||||
:lighter " electron" :keymap (make-sparse-keymap)
|
|
||||||
(add-yas-minor-mode! 'electron-mode))
|
|
||||||
(associate! electron-mode
|
|
||||||
:files ("package.json" "app/index.html" "app/main.js")
|
|
||||||
:in (web-mode js2-mode markdown-mode json-mode coffee-mode))
|
|
||||||
|
|
||||||
(use-package unityjs-mode
|
(use-package unityjs-mode
|
||||||
:mode "/Assets/.*\\.js$"
|
:mode "/Assets/.*\\.js$"
|
||||||
|
@ -107,5 +95,20 @@
|
||||||
:mode "\\.coffee$"
|
:mode "\\.coffee$"
|
||||||
:config (setq-default coffee-indent-like-python-mode t))
|
:config (setq-default coffee-indent-like-python-mode t))
|
||||||
|
|
||||||
|
;;
|
||||||
|
(define-project-type! nodejs "node"
|
||||||
|
:modes (js2-mode)
|
||||||
|
:files ("package.json"))
|
||||||
|
|
||||||
|
|
||||||
|
(define-project-type! electron "electron"
|
||||||
|
:modes (web-mode js-mode js2-mode markdown-mode json-mode coffee-mode scss-mode sass-mode)
|
||||||
|
:files ("package.json" "app/index.html" "app/main.js"))
|
||||||
|
;; TODO electron-compile support
|
||||||
|
|
||||||
|
;; TODO angular
|
||||||
|
;; TODO react
|
||||||
|
;; TODO express
|
||||||
|
|
||||||
(provide 'module-js)
|
(provide 'module-js)
|
||||||
;;; module-js.el ends here
|
;;; module-js.el ends here
|
||||||
|
|
|
@ -63,8 +63,9 @@
|
||||||
"define-builder" "narf-space-setup"
|
"define-builder" "narf-space-setup"
|
||||||
"define-env-command" "define-text-object"
|
"define-env-command" "define-text-object"
|
||||||
"add-yas-minor-mode" "define-docset"
|
"add-yas-minor-mode" "define-docset"
|
||||||
"define-org-link!" "define-company-backend"
|
"define-org-link" "define-company-backend"
|
||||||
"define-org-section" "define-temp-ex-cmd"))
|
"define-org-section" "define-temp-ex-cmd"
|
||||||
|
"define-project-type"))
|
||||||
"!\\)")
|
"!\\)")
|
||||||
(1 font-lock-keyword-face append))
|
(1 font-lock-keyword-face append))
|
||||||
;; Ert
|
;; Ert
|
||||||
|
@ -75,26 +76,19 @@
|
||||||
(1 font-lock-keyword-face)
|
(1 font-lock-keyword-face)
|
||||||
(2 font-lock-function-name-face))))
|
(2 font-lock-function-name-face))))
|
||||||
|
|
||||||
;; Real go-to-definition for elisp
|
|
||||||
(map! :map emacs-lisp-mode-map
|
|
||||||
:m "gd" 'narf/elisp-find-function-at-pt
|
|
||||||
:m "gD" 'narf/elisp-find-function-at-pt-other-window)
|
|
||||||
|
|
||||||
(define-minor-mode emacs-ert-mode
|
|
||||||
"Ert test file minor mode"
|
|
||||||
:lighter " Ert" :keymap (make-sparse-keymap)
|
|
||||||
(add-yas-minor-mode! 'emacs-ert-mode))
|
|
||||||
(associate! emacs-ert-mode :match "/test/.+-test\\.el$")
|
|
||||||
|
|
||||||
(map! :map emacs-lisp-mode-map
|
|
||||||
(:localleader
|
|
||||||
:n "tr" 'narf/ert-rerun-test
|
|
||||||
:n "ta" 'narf/ert-run-all-tests
|
|
||||||
:n "ts" 'narf/ert-run-test))
|
|
||||||
|
|
||||||
(use-package slime :defer t
|
(use-package slime :defer t
|
||||||
:config
|
:config (setq inferior-lisp-program "clisp"))
|
||||||
(setq inferior-lisp-program "clisp"))
|
|
||||||
|
;; Real go-to-definition for elisp
|
||||||
|
(map! :map emacs-lisp-mode-map :m "gd" 'narf/elisp-find-function-at-pt)
|
||||||
|
|
||||||
|
(define-project-type! emacs-ert "ert"
|
||||||
|
:modes (emacs-lisp-mode)
|
||||||
|
:match "/test/.+-test\\.el$"
|
||||||
|
:bind (:localleader
|
||||||
|
:n "tr" 'narf/ert-rerun-test
|
||||||
|
:n "ta" 'narf/ert-run-all-tests
|
||||||
|
:n "ts" 'narf/ert-run-test))
|
||||||
|
|
||||||
(provide 'module-lisp)
|
(provide 'module-lisp)
|
||||||
;;; module-elisp.el ends here
|
;;; module-elisp.el ends here
|
||||||
|
|
|
@ -7,8 +7,6 @@
|
||||||
(define-repl! lua-mode narf/inf-lua)
|
(define-repl! lua-mode narf/inf-lua)
|
||||||
(define-company-backend! lua-mode (yasnippet))
|
(define-company-backend! lua-mode (yasnippet))
|
||||||
(add-hook 'lua-mode-hook 'flycheck-mode)
|
(add-hook 'lua-mode-hook 'flycheck-mode)
|
||||||
(after! company-dict
|
|
||||||
(add-to-list 'company-dict-minor-mode-list 'love-mode))
|
|
||||||
(add-hook! lua-mode
|
(add-hook! lua-mode
|
||||||
(electric-indent-local-mode +1)
|
(electric-indent-local-mode +1)
|
||||||
(setq narf-electric-indent-words '("else" "end")))
|
(setq narf-electric-indent-words '("else" "end")))
|
||||||
|
@ -29,26 +27,16 @@
|
||||||
;; inline functions
|
;; inline functions
|
||||||
(sp-local-pair "function " " end" :unless '(sp-point-after-bol-p))))
|
(sp-local-pair "function " " end" :unless '(sp-point-after-bol-p))))
|
||||||
|
|
||||||
(define-minor-mode love-mode
|
;;
|
||||||
"Buffer local minor mode for Love2D"
|
(define-project-type! love "♥"
|
||||||
:init-value nil
|
:modes (lua-mode markdown-mode json-mode)
|
||||||
:lighter " ♥"
|
:files ("main.lua" "conf.lua")
|
||||||
:keymap (make-sparse-keymap)
|
:build ("open -a love.app '%s'" "main.lua"))
|
||||||
(add-yas-minor-mode! 'love-mode))
|
|
||||||
(associate! love-mode
|
|
||||||
:in (lua-mode markdown-mode json-mode)
|
|
||||||
:files ("main.lua" "conf.lua"))
|
|
||||||
(define-builder! love-mode "open -a love.app '%s'" "main.lua")
|
|
||||||
|
|
||||||
(define-minor-mode hammerspoon-mode
|
(define-project-type! hammerspoon "hammer"
|
||||||
:init-value nil
|
:modes (lua-mode markdown-mode)
|
||||||
:lighter " hammer"
|
:match "/\\.?hammerspoon/.+\\.lua$"
|
||||||
:keymap (make-sparse-keymap)
|
:build "open hammerspoon://reload")
|
||||||
(add-yas-minor-mode! 'hammerspoon-mode))
|
|
||||||
(associate! hammerspoon-mode
|
|
||||||
:in (lua-mode markdown-mode)
|
|
||||||
:match "/\\.?hammerspoon/.+\\.lua$")
|
|
||||||
(define-builder! hammerspoon-mode "open hammerspoon://reload")
|
|
||||||
|
|
||||||
(provide 'module-lua)
|
(provide 'module-lua)
|
||||||
;;; module-lua.el ends here
|
;;; module-lua.el ends here
|
||||||
|
|
|
@ -63,14 +63,9 @@
|
||||||
:init (define-repl! php-mode php-boris)
|
:init (define-repl! php-mode php-boris)
|
||||||
:config (evil-set-initial-state 'php-boris-mode 'emacs))
|
:config (evil-set-initial-state 'php-boris-mode 'emacs))
|
||||||
|
|
||||||
(define-minor-mode php-laravel-mode
|
;;
|
||||||
""
|
(define-project-type! laravel "laravel"
|
||||||
:init-value nil
|
:modes (php-mode json-mode yaml-mode web-mode nxml-mode js2-mode scss-mode)
|
||||||
:lighter " Laravel"
|
|
||||||
:keymap (make-sparse-keymap)
|
|
||||||
(add-yas-minor-mode! 'php-laravel-mode))
|
|
||||||
(associate! php-laravel-mode
|
|
||||||
:in (php-mode json-mode yaml-mode web-mode nxml-mode js2-mode scss-mode)
|
|
||||||
:files ("artisan" "server.php"))
|
:files ("artisan" "server.php"))
|
||||||
|
|
||||||
(provide 'module-php)
|
(provide 'module-php)
|
||||||
|
|
|
@ -100,31 +100,21 @@
|
||||||
:i "M-e" 'emmet-expand-yas
|
:i "M-e" 'emmet-expand-yas
|
||||||
:i "M-E" 'emmet-expand-line))
|
:i "M-E" 'emmet-expand-line))
|
||||||
|
|
||||||
(define-minor-mode jekyll-mode
|
;;
|
||||||
"Jekyll development mode."
|
(define-project-type! jekyll ":{"
|
||||||
:init-value nil
|
:modes (web-mode scss-mode html-mode markdown-mode yaml-mode)
|
||||||
:lighter " :{"
|
|
||||||
:keymap (make-sparse-keymap)
|
|
||||||
(add-yas-minor-mode! 'jekyll-mode))
|
|
||||||
(associate! jekyll-mode
|
|
||||||
:match "/\\(\\(css\\|_\\(layouts\\|posts\\|sass\\)\\)/.+\\|.+.html\\)$"
|
:match "/\\(\\(css\\|_\\(layouts\\|posts\\|sass\\)\\)/.+\\|.+.html\\)$"
|
||||||
:files ("config.yml" "_layouts/")
|
:files ("config.yml" "_layouts/")
|
||||||
:in (web-mode scss-mode html-mode markdown-mode yaml-mode))
|
(add-hook! it
|
||||||
(add-hook! jekyll-mode
|
(when (eq major-mode 'web-mode)
|
||||||
(when (eq major-mode 'web-mode)
|
(web-mode-set-engine "django"))))
|
||||||
(web-mode-set-engine "django")))
|
|
||||||
(after! company-dict (add-to-list 'company-dict-minor-mode-list 'jekyll-mode))
|
|
||||||
|
|
||||||
(define-minor-mode wordpress-mode
|
(define-project-type! wordpress "wp"
|
||||||
"Wordpress development mode."
|
:modes (php-mode web-mode css-mode scss-mode sass-mode)
|
||||||
:init-value nil
|
|
||||||
:lighter " wp"
|
|
||||||
:keymap (make-sparse-keymap)
|
|
||||||
(add-yas-minor-mode! 'wordpress-mode))
|
|
||||||
(associate! wordpress-mode
|
|
||||||
:match "/wp-\\(\\(content\\|admin\\|includes\\)/\\)?.+$"
|
:match "/wp-\\(\\(content\\|admin\\|includes\\)/\\)?.+$"
|
||||||
:files ("wp-config.php" "wp-content/"))
|
:files ("wp-config.php" "wp-content/"))
|
||||||
(after! company-dict (add-to-list 'company-dict-minor-mode-list 'wordpress-mode))
|
|
||||||
|
;; TODO Add stylus-mode
|
||||||
|
|
||||||
(provide 'module-web)
|
(provide 'module-web)
|
||||||
;;; module-web.el ends here
|
;;; module-web.el ends here
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue