doomemacs/modules/defuns/defuns-quickrun.el

98 lines
3.7 KiB
EmacsLisp
Raw Normal View History

2015-06-15 15:05:52 +08:00
;;; defuns-quickrun.el
;;;; Code building ;;;;;;;;;;;;;;;;;;;;;;
2015-06-21 04:23:42 +08:00
;;;###autoload
2016-05-21 10:37:30 +08:00
(defvar doom--build-command '("make %s" . "Makefile"))
(make-variable-buffer-local 'doom--build-command)
2015-06-15 15:05:52 +08:00
;;;###autoload
2016-05-21 10:37:30 +08:00
(defun doom/set-build-command (command &optional file)
2015-06-15 15:05:52 +08:00
(when (or (null file)
2016-05-21 10:37:30 +08:00
(doom/project-has-files file))
(setq doom--build-command `(,command . ,file))))
2015-06-15 15:05:52 +08:00
2016-05-21 10:37:30 +08:00
;;;###autoload (autoload 'doom:build "defuns-quickrun" nil t)
(evil-define-command doom:build (arg)
"Call a build command in the current directory. If ARG is nil this function calls
`recompile', otherwise it calls `compile' passing ARG as build command."
2015-06-15 15:05:52 +08:00
(interactive "<sh>")
2016-05-21 10:37:30 +08:00
(when (null doom--build-command)
2015-06-15 15:05:52 +08:00
(user-error "No build command was set"))
2016-05-21 10:37:30 +08:00
(let ((build-file (cdr doom--build-command))
(build-cmd (car doom--build-command))
(project-dir (doom/project-root)))
(if (or (null build-file) (f-exists? (f-expand build-file project-dir)))
(if (or (symbolp build-cmd) (functionp build-cmd))
(if (commandp build-cmd)
(call-interactively build-cmd)
(funcall build-cmd))
(let ((default-directory project-dir))
(compile (format build-cmd (or arg "")))))
(error "Could not build!"))))
2015-06-15 15:05:52 +08:00
;;;; Code running ;;;;;;;;;;;;;;;;;;;;;;
2016-05-21 10:37:30 +08:00
;;;###autoload (autoload 'doom:eval-buffer "defuns-quickrun" nil t)
(evil-define-command doom:eval-buffer ()
"Evaluate the whole buffer."
2016-04-19 14:08:48 +08:00
:move-point nil :repeat nil
2015-06-15 15:05:52 +08:00
(interactive)
(cond ((eq major-mode 'emacs-lisp-mode)
2016-05-21 10:37:30 +08:00
(doom:eval-region (point-min) (point-max)))
2015-06-15 15:05:52 +08:00
(t (quickrun))))
2016-05-21 10:37:30 +08:00
;;;###autoload (autoload 'doom:eval-region "defuns-quickrun" nil t)
(evil-define-operator doom:eval-region (beg end)
"Evaluate a region and, if large enough, prints its output to a popup buffer (if an
elisp buffer). Otherwise forward the region to Quickrun."
2016-04-19 14:08:48 +08:00
:move-point nil :repeat nil
2015-06-15 15:05:52 +08:00
(interactive "<r>")
(cond ((eq major-mode 'emacs-lisp-mode)
2016-04-19 14:08:48 +08:00
(require 'pp)
2016-06-07 07:37:51 +08:00
(let ((result (eval (read (buffer-substring-no-properties beg end))))
lines)
(let ((buf (get-buffer-create "*eval*")))
(with-current-buffer buf
(read-only-mode -1)
(setq-local scroll-margin 0)
(erase-buffer)
(prin1 result buf)
(emacs-lisp-mode)
(pp-buffer)
(read-only-mode 1)
(setq lines (count-lines (point-min) (point-max)))
(goto-char (point-min))
(when (< lines 5)
(message "%s" (buffer-substring (point-min) (point-max)))
(kill-buffer buf)))
2016-06-07 07:37:51 +08:00
(unless (< lines 5)
(doom/popup-buffer buf)))))
2015-06-15 15:05:52 +08:00
(t (quickrun-region beg end))))
2016-05-21 10:37:30 +08:00
;;;###autoload (autoload 'doom:eval-region-and-replace "defuns-quickrun" nil t)
(evil-define-operator doom:eval-region-and-replace (beg end)
2015-06-15 15:05:52 +08:00
(interactive "<r>")
(cond ((eq major-mode 'emacs-lisp-mode)
(kill-region beg end)
(condition-case nil
(prin1 (eval (read (current-kill 0)))
(current-buffer))
(error (message "Invalid expression")
(insert (current-kill 0)))))
(t (quickrun-replace-region beg end))))
2016-06-07 11:49:12 +08:00
;;;###autoload
(defun doom*quickrun-close-popup (&optional _ _ _ _)
"Allows us to re-run quickrun from inside the quickrun buffer."
(awhen (get-buffer-window quickrun/buffer-name)
(shut-up! (quickrun/kill-running-process))
(doom/popup-close it nil)))
2016-06-07 11:49:12 +08:00
;;;###autoload
(defun doom|quickrun-after-run ()
"Ensures window is scrolled to BOF"
(with-selected-window (get-buffer-window quickrun/buffer-name)
(goto-char (point-min))))
2015-06-15 15:05:52 +08:00
(provide 'defuns-quickrun)
;;; defuns-quickrun.el ends here