Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for highlighting at previous prompts (default t) #23

Merged
merged 6 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion eshell-syntax-highlighting-tests.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; eshell-syntax-highlighting-tests.el --- Tests for eshell highlighting -*- lexical-binding:t -*-

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (25.1)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (25.2)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (25.3)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (26.1)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (26.2)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (26.3)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (27.1)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (27.2)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (28.1)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (28.2)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (29.1)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (29.2)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (29.3)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

Check warning on line 1 in eshell-syntax-highlighting-tests.el

View workflow job for this annotation

GitHub Actions / build (release-snapshot)

Possibly using local file with different prefix `eshell-syntax-highlighting.el'

;; Copyright (C) 2024 Alex Kreisher

Expand Down Expand Up @@ -91,7 +91,9 @@
(esht-test "echo $(message \"hello world\")"))

(ert-deftest esht-test-lisp ()
(esht-test "(use-package eshell-syntax-highlighting :ensure t)"))
(esht-test "(use-package eshell-syntax-highlighting :ensure t)")
(esht-test "ls && (defun my-function () (message \"hello\")); echo 'hello'")
(esht-test "echo (message \"hello\")"))

(ert-deftest esht-test-for-loop ()
(esht-test "for i in #'(1 2 3) {echo \"i: $i\"}"))
Expand Down
55 changes: 36 additions & 19 deletions eshell-syntax-highlighting.el
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@
:type 'boolean
:group 'eshell-syntax-highlighting)


(defcustom eshell-syntax-highlighting-highlight-in-remote-dirs nil
"Whether to perform syntax highlighting in remote directories."
:type 'boolean
:group 'eshell-syntax-highlighting)

(defcustom eshell-syntax-highlighting-highlight-previous-prompts t
"Whether to perform syntax highlighting on previous prompts."
:type 'boolean
:group 'eshell-syntax-highlighting)

(defface eshell-syntax-highlighting-default-face
'((t :inherit default))
Expand Down Expand Up @@ -520,29 +523,43 @@
'eshell-current-command))


(defun eshell-syntax-highlighting--enabled-p ()
"Return whether syntax highlighting should be enabled in the current buffer."
(and (eq major-mode 'eshell-mode)
(not eshell-non-interactive-p)
(not mark-active)
(not (eshell-syntax-highlighting--command-running-p))
(or
eshell-syntax-highlighting-highlight-in-remote-dirs
(not (file-remote-p default-directory)))))
(defmacro eshell-syntax-highlighting--bol ()
"Go to beginning of line, skipping prompt."
(if (>= emacs-major-version 30)
'(beginning-of-line)
'(eshell-bol)))


(defun eshell-syntax-highlighting--enable-highlighting ()
"Parse and highlight the command at the last Eshell prompt."
(let ((beg (point))
(non-essential t))
(when (eshell-syntax-highlighting--enabled-p)
(let ((non-essential t))
(when (and (eq major-mode 'eshell-mode)
(not eshell-non-interactive-p)
(not (eshell-syntax-highlighting--command-running-p))
(or
eshell-syntax-highlighting-highlight-in-remote-dirs
(not (file-remote-p default-directory))))
(with-silent-modifications
(save-excursion
(goto-char (point-max))
(eshell-previous-prompt 0)
(eshell-syntax-highlighting--parse-and-highlight 'command (point-max))))
;; save-excursion marker is deleted when highlighting elisp,
;; so explicitly pop back to initial point.
(goto-char beg))))
(let ((pos (point)))
(if (>= pos eshell-last-output-end)
;; Jump to eshell-last-output-end, which should be at prompt end.
(goto-char eshell-last-output-end)
(let (begin)
;; Check if at a prompt prior to the current one.
(if (and eshell-syntax-highlighting-highlight-previous-prompts
(setq begin
(save-excursion
(eshell-syntax-highlighting--bol)
(and (not (bolp)) (point))))
(>= pos begin)
(<= pos (line-end-position)))
(goto-char begin)
;; Fallback to going to the end of the buffer and highlighting
;; the current prompt.
(goto-char (point-max))
(eshell-previous-prompt 0)))))
(eshell-syntax-highlighting--parse-and-highlight 'command (point-max)))))))


;;;###autoload
Expand Down