Refactor macros

This commit is contained in:
Henrik Lissner 2016-05-12 02:53:31 -04:00
parent f2df8192ff
commit d751be7898
8 changed files with 53 additions and 40 deletions

View file

@ -231,6 +231,12 @@ Examples:
(setq i (1+ i)))
`(progn ,@(apply #'nconc (delete nil (delete (list nil) (reverse forms))))))))
(defmacro def-repeat! (command next-func prev-func)
"Repeat motions with SPC/S-SPC"
`(defadvice ,command
(before ,(intern (format "narf-space--%s" (symbol-name command))) activate)
(define-key evil-motion-state-map (kbd "SPC") ',next-func)
(define-key evil-motion-state-map (kbd "S-SPC") ',prev-func)))
;;
(defun narf|update-scratch-buffer-cwd (&optional dir) ; see core-editor.el

View file

@ -96,13 +96,6 @@
(add-hook! isearch-mode (setq echo-keystrokes 0))
(add-hook! isearch-mode-end (setq echo-keystrokes 0.02))
;; Repeat motions with SPC/S-SPC
(defmacro def-repeat! (command next-func prev-func)
`(defadvice ,command
(before ,(intern (format "narf-space--%s" (symbol-name command))) activate)
(define-key evil-motion-state-map (kbd "SPC") ',next-func)
(define-key evil-motion-state-map (kbd "S-SPC") ',prev-func)))
(after! evil-snipe
(def-repeat! evil-snipe-f evil-snipe-repeat evil-snipe-repeat-reverse)
(def-repeat! evil-snipe-F evil-snipe-repeat evil-snipe-repeat-reverse)

View file

@ -6,7 +6,7 @@
"Register a company backend for a mode."
(let* ((hooks (if (listp hooks) hooks (list hooks)))
(def-name (intern (format "narf--init-company-%s"
(s-join "-" (mapcar 'symbol-name hooks)))))
(mapconcat 'identity (mapcar 'symbol-name hooks) "-"))))
(quoted (eq (car-safe backends) 'quote)))
`(progn
(defun ,def-name ()

View file

@ -3,12 +3,11 @@
;;;###autoload
(defmacro def-electric! (modes &rest rest)
(declare (indent 1))
(let ((modes (if (listp modes) modes (list modes)))
(let ((modes (-list modes))
(chars (plist-get rest :chars))
(words (plist-get rest :words)))
(when (or chars words)
(let ((fn-name (intern (format "narf--electric-%s"
(s-join "-" (mapcar 'symbol-name modes))))))
(let ((fn-name (intern (format "narf--electric-%s" (s-join "-" (mapcar 'symbol-name modes))))))
`(progn
(defun ,fn-name ()
(electric-indent-local-mode +1)
@ -16,5 +15,21 @@
,(if words `(setq narf-electric-indent-words ',words)))
(add-hook! ,modes ',fn-name))))))
;;;###autoload
(defmacro def-rotate! (modes &rest rest)
(declare (indent 1))
(let ((modes (if (listp modes) modes (list modes)))
(symbols (plist-get rest :symbols))
(words (plist-get rest :words))
(patterns (plist-get rest :patterns)))
(when (or symbols words patterns)
(let ((fn-name (intern (format "narf--rotate-%s" (s-join "-" (mapcar 'symbol-name modes))))))
`(progn
(defun ,fn-name ()
,(if symbols `(setq-local rotate-text-local-symbols ',symbols))
,(if words `(setq-local rotate-text-local-words ',words))
,(if patterns `(setq-local rotate-text-local-patterns ',patterns)))
(add-hook! ,modes ',fn-name))))))
(provide 'macros-editor)
;;; macros-editor.el ends here

View file

@ -0,0 +1,21 @@
;;; macros-eval.el
;;;###autoload
(defmacro def-builder! (mode command &optional build-file)
"Register major/minor MODE with build COMMAND. If FILES are provided, do an additional
check to make sure they exist in the project root."
(let ((fn (intern (format "narf--init-builder-%s" mode))))
`(progn
(defun ,fn ()
(when (or (null ,build-file)
(narf/project-has-files ,build-file))
(setq narf--build-command '(,command . ,build-file))))
(add-hook! ,mode ',fn))))
;;;###autoload
(defmacro def-repl! (mode command)
"Define a REPL for a mode."
`(push '(,mode . ,command) rtog/mode-repl-alist))
(provide 'macros-eval)
;;; macros-eval.el ends here

View file

@ -14,7 +14,7 @@
;;;###autoload
(defmacro def-tmp-excmd! (cmd-on cmd-off &rest commands)
"Creates on-off defuns for a set of ex commands, named CMD-ON and CMD-OFF."
"Creates a toggle for a set of ex commands, named CMD-ON and CMD-OFF."
(declare (indent 2))
`(progn
(defun ,cmd-on (&rest _)
@ -24,19 +24,15 @@
(mapc (lambda (cmd) (narf/evil-ex-undefine-cmd (car cmd)))
',commands))))
;; Shortcuts for the evil expression register
;;;###autoload
(defmacro $= (str &rest args)
`(calc-eval (format ,str ,@args)))
(defmacro $= (str &rest args) `(calc-eval (format ,str ,@args)))
;;;###autoload
(defmacro $r (char)
`(evil-get-register ,char))
(defmacro $r (char) `(evil-get-register ,char))
;;;###autoload
(defmacro $expand (path)
`(evil-ex-replace-special-filenames ,path))
(defmacro $expand (path) `(evil-ex-replace-special-filenames ,path))
(provide 'macros-evil)
;;; macros-evil.el ends here

View file

@ -1,17 +0,0 @@
;;; macros-quickrun.el
;;;###autoload
(defmacro def-builder! (mode command &optional build-file)
"Register major/minor MODE with build COMMAND. If FILES are provided, do an
additional check to make sure they exist in the project root."
`(add-hook! ,mode
(when (or (null ,build-file)
(narf/project-has-files ,build-file))
(setq narf--build-command '(,command . ,build-file)))))
;;;###autoload
(defmacro def-repl! (mode command)
`(push '(,mode . ,command) rtog/mode-repl-alist))
(provide 'macros-quickrun)
;;; macros-quickrun.el ends here

View file

@ -1,18 +1,17 @@
;;; defuns-spaceline.el
;;;###autoload
(defmacro def-env-command! (mode command)
(defmacro def-version-cmd! (modes command)
"Define a COMMAND for MODE that will set `narf--env-command' when that mode is
activated, which should return the version number of the current environment. It is used
by `narf|spaceline-env-update' to display a version number in the modeline. For instance:
(def-env-command! ruby-mode \"ruby --version | cut -d' ' -f2\")
(def-version-cmd! ruby-mode \"ruby --version | cut -d' ' -f2\")
This will display the ruby version in the modeline in ruby-mode buffers. It is cached the
first time."
(add-hook! (focus-in find-file) 'narf|spaceline-env-update)
`(add-hook ',(intern (format "%s-hook" (symbol-name mode)))
(lambda () (setq narf--env-command ,command))))
`(add-hook! ,modes (setq narf--env-command ,command)))
(provide 'defuns-spaceline)
;;; defuns-spaceline.el ends here