Skip to content

Commit

Permalink
Use compilation-filter as subprocess filter
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankSpruce committed Jun 29, 2022
1 parent f4d123c commit ebed9dd
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions justl.el
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
;; Keywords: just justfile tools processes
;; URL: https://github.com/psibi/justl.el
;; License: GNU General Public License >= 3
;; Package-Requires: ((transient "0.1.0") (emacs "25.3") (xterm-color "2.0") (s "1.2.0") (f "0.20.0"))
;; Package-Requires: ((transient "0.1.0") (emacs "25.3") (s "1.2.0") (f "0.20.0"))

;;; Commentary:

Expand Down Expand Up @@ -60,12 +60,23 @@
;;
;; You can also control the width of the RECIPE column in the justl
;; buffer via `justl-recipe width`. By default it has a value of 20.
;;
;; You can enable coloring of the output by adding proper hook to
;; `compilation-filter-hook`. For Emacs older than 28.1 you need to
;; define it like this:
;; (require 'ansi-color)
;; (defun my-color-compilation-filter ()
;; (let ((inhibit-read-only t))
;; (ansi-color-apply-on-region compilation-filter-start (point))))
;; (add-hook 'compilation-filter-hook 'my-color-compilation-filter)
;; For Emacs 28.1 or newer it's as simple as:
;; (require 'ansi-color)
;; (add-hook 'compilation-filter-hook 'ansi-color-compilation-filter)

;;; Code:

(require 'transient)
(require 'cl-lib)
(require 'xterm-color)
(require 'eshell)
(require 'esh-mode)
(require 's)
Expand Down Expand Up @@ -283,21 +294,6 @@ CMD is the just command as a list."
process-name
err))))))

(defun justl--xterm-color-filter (proc string)
"Filter function for PROC handling colors.
STRING is the data returned by the PROC"
(when (buffer-live-p (process-buffer proc))
(with-current-buffer (process-buffer proc)
(let ((inhibit-read-only t)
(moving (= (point) (process-mark proc))))
(save-excursion
;; Insert the text, advancing the process marker.
(goto-char (process-mark proc))
(insert (xterm-color-filter string))
(set-marker (process-mark proc) (point)))
(if moving (goto-char (process-mark proc)))))))

(defun justl-compilation-setup-buffer (buf dir mode &optional no-mode-line)
"Setup the compilation buffer for just-compile-mode.
Expand Down Expand Up @@ -349,7 +345,7 @@ ARGS is a plist that affects how the process is run.

(setq-local justl--justfile (justl--justfile-from-arg (elt command 1)))
(run-hook-with-args 'compilation-start-hook process)
(set-process-filter process 'justl--xterm-color-filter)
(set-process-filter process 'compilation-filter)
(set-process-sentinel process sentinel)
(set-process-coding-system process 'utf-8-emacs-unix 'utf-8-emacs-unix)
(pop-to-buffer buf)))))
Expand Down Expand Up @@ -430,7 +426,7 @@ ARGS is a ist of arguments."
(justl--log-command process-name cmd)
(make-process :name process-name
:buffer buffer-name
:filter 'justl--xterm-color-filter
:filter 'compilation-filter
:sentinel #'justl--sentinel
:file-handler t
:stderr nil
Expand Down

0 comments on commit ebed9dd

Please sign in to comment.