-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgitstatus-eshell.el
126 lines (99 loc) · 3.9 KB
/
gitstatus-eshell.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
;;; gitstatus-eshell.el --- Front-end for `eshell' and `gitstatusd' -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Igor Epstein
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; `eshell' front-end for `gitstatusd'.
;; This is an extra package to allow adding the information to the `eshell' prompt.
;;; Code:
(require 'gitstatusd)
(require 'gitstatus)
(require 'em-prompt)
(eval-when-compile (require 'cl-lib))
;;; Customizable variables
(defgroup gitstatus-eshell nil
"`eshell' front-end for `gitstatusd'."
:group 'gitstatus)
(defcustom gitstatus-eshell-neighbour-regex "\\( [$#]\\)"
"Neighbour of the `gitstatus' in `eshell' prompt."
:type 'string
:group 'gitstatus-eshell)
(defcustom gitstatus-eshell-is-neighbour-append nil
"Whether to append (or prepend) the `gitstatus' to the `eshell' prompt."
:type 'boolean
:group 'gitstatus-eshell)
(defcustom gitstatus-eshell-prompt-lines 1
"Search for `gitstatus-eshell-neighbour-regex' in this many lines."
:type 'integer
:group 'gitstatus-eshell)
;;; Internal variables
(defvar-local gitstatus-eshell--req-id nil "`gitstatusd' request ID.")
;;; Public interface
;;;###autoload
(defun gitstatus-eshell-start ()
"Run `gitstatusd' to get the `gitstatus' information."
(when gitstatus-eshell--req-id
(gitstatusd-remove-callback gitstatus-eshell--req-id))
(setq gitstatus-eshell--req-id
(gitstatusd-get-status default-directory #'gitstatus-eshell-build)))
;;;###autoload
(defun gitstatus-eshell-build (res)
"Build `eshell' prompt based on `gitstatusd' result, represented by RES."
(let ((buf (gitstatus-eshell--get-buffer res)))
(when buf
(with-current-buffer buf
(save-mark-and-excursion
(let ((msg (gitstatus-build-str res)))
(when (gitstatus--string-not-empty-p msg)
(save-match-data
(let ((place (gitstatus-eshell--find-place)))
(when place
(forward-char place)
(let* ((pos (point))
(inhibit-read-only t))
(insert " " msg)
(add-text-properties pos (+ 1 pos (length msg))
'(read-only t
front-sticky (face read-only)
rear-nonsticky (face read-only))))))))))))))
;;; Utility functions
(defun gitstatus-eshell--get-buffer (res)
"Return the buffer the request came from with result RES."
(let ((res-id (gitstatusd-req-id res)))
(cl-dolist (buf (buffer-list))
(when (string-equal res-id
(buffer-local-value 'gitstatus-eshell--req-id buf))
(cl-return buf)))))
(defun gitstatus-eshell--find-place ()
"Find the right place in `eshell' prompt."
(goto-char (point-max))
(re-search-backward eshell-prompt-regexp nil t 1)
(let ((cnt gitstatus-eshell-prompt-lines)
(mstart (match-beginning 0))
(place))
(when (and mstart (> cnt 0))
(while (> cnt 0)
(let* ((start (if (= cnt gitstatus-eshell-prompt-lines) mstart (line-beginning-position)))
(end (if (= cnt gitstatus-eshell-prompt-lines) (match-end 0) (line-end-position))))
(string-match gitstatus-eshell-neighbour-regex (buffer-substring start end))
(setq place
(if gitstatus-eshell-is-neighbour-append
(match-end 1)
(match-beginning 1)))
(if place
(setq cnt 0)
(forward-line -1)
(beginning-of-line)
(setq cnt (1- cnt))))))
place))
(provide 'gitstatus-eshell)
;;; gitstatus-eshell.el ends here