-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherc-gitter-notifications.el
196 lines (161 loc) · 6.46 KB
/
erc-gitter-notifications.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
;;; erc-gitter-notifications.el --- Gitter-Notifications module -*- lexical-binding: t; -*-
;; Copyright (C) 2014
;; Author: Jonathan Leech-Pepin <jonathan.leechpepin AT gmail.com>
;; Keywords: tools, extension
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'erc)
(require 'erc-button)
;;;; Variables
;;;;; Customizable
(defcustom erc-gitter-bot-handling 't
"How to handle messages from gitter integration.
'buffer will send messages to a separate buffer
nil will do nothing with it.
non-nil values will set `gitter' as a fool."
:type '(choice (const :tag "Do nothing" nil)
(const :tag "Separate buffer" 'buffer)
(symbol :tag "Treat as Fool" 't))
:group 'erc-gitter)
(defface erc-gn-notifications-face
'((t :inherit erc-current-nick-face))
""
:group 'erc-gitter-faces)
;;;;; Internal
(defvar erc-gn-pending 0
"How many notifications are pending in the \"*Gitter
Notifications*\" buffer.")
(defvar erc-gn-unread-overlay nil
"Overlay variable for unread notifications")
;;;; Gitter-bot notification handling
(defun erc-gitter-gitter-is-fool ()
"Add the gitter-bot to the list of fools.
It will be treated as any other fool."
(interactive)
(add-to-list 'erc-fools "[email protected]"))
(defun erc-gitter-gitter-is-no-fool ()
"Remove the gitter-bot from the list of fools."
(interactive)
(setq erc-fools (delete "[email protected]" erc-fools)))
;;;; Gitter Notifications Buffer
(defun erc-gitter-bot-to-buffer (match-type nickuserhost message)
(when (and (eq match-type 'fool)
(string= "[email protected]" nickuserhost))
(let* ((buf (erc-gn-make-buffer))
(len (- (length message) 2))
(cb (get-text-property len 'erc-callback message))
(data (get-text-property len 'erc-data message)))
(with-current-buffer buf
(save-excursion
(let ((end (point-max)))
(goto-char end)
(insert message)
(put-text-property end (point) 'erc-data data)
(put-text-property end (point) 'erc-callback cb))))
(erc-gn-update)
(erc-hide-fools match-type nickuserhost message))))
(defun erc-gn-make-buffer ()
(when (eq erc-gitter-bot-handling 'buffer)
(unless (get-buffer "*Gitter Notifications*")
(let ((buf (get-buffer-create "*Gitter Notifications*")))
(with-current-buffer buf
(gitter-notifications-mode)
(unless erc-gn-unread-overlay
(let ((over (make-overlay (point-min) (point-max)
(current-buffer) nil t)))
(overlay-put over 'face
'(t :inherit header-line :box nil))
(setq erc-gn-unread-overlay over))))))
(get-buffer "*Gitter Notifications*")))
(defvar gitter-notifications-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map special-mode-map)
(define-key map "g" nil) ; nothing to revert
(define-key map "q" #'bury-buffer)
;; (define-key map (kbd "<return>") #'erc-gn-visit)
(define-key map "p" #'erc-gn-previous)
(define-key map "n" #'erc-gn-next)
(define-key map [remap next-line] #'erc-gn-next)
(define-key map [remap previous-line] #'erc-gn-previous)
(define-key map (kbd "<return>") #'erc-button-press-button)
map))
(define-derived-mode gitter-notifications-mode special-mode
"Gitter-Notifications"
"Major mode used in the \"*Gitter Notifications*\" buffer."
:group 'erc-gitter)
(defun erc-gn-switch-to-notif ()
(interactive)
(if (eq erc-gitter-bot-handling 'buffer)
(progn
(switch-to-buffer (erc-gn-make-buffer))
(when erc-gn-pending
(goto-char (overlay-start erc-gn-unread-overlay)))
(erc-gn-update 0))
(user-error "Gitter notifications are not being tracked.")))
(defun erc-gn-next ()
(interactive)
;; Test if current notification is unread, if so mark unread when
;; moving.
(let ((unread (>= (point) (overlay-start erc-gn-unread-overlay))))
(condition-case nil
(progn
(forward-char 1)
(re-search-forward "^\\[.*?\\]" nil)
(beginning-of-line 1)
(when unread
(move-overlay erc-gn-unread-overlay (point)
(overlay-end erc-gn-unread-overlay))))
(error (and (move-overlay erc-gn-unread-overlay (point-max)
(point-max))
(beginning-of-line 1)))))
(erc-gn-update 0))
(defun erc-gn-previous ()
(interactive)
(re-search-backward "^\\[.*?\\]" nil 'noerror)
(erc-gn-update 0))
;;;; Gitter Notification Mode-Line
(defvar gitter-notifications-mode-line-map
(let ((map (make-sparse-keymap)))
(define-key map [mode-line mouse-1] #'erc-gn-switch-to-notif)
(define-key map [mode-line mouse-3] #'erc-gn-clear-notif)
map))
(defun erc-gn-notifications ()
(setq erc-gn-notifications
(if (> erc-gn-pending 0)
(list "["
`(:propertize
(:eval (format "G:%d" erc-gn-pending))
help-echo
(format "%d notifications pending from Gitter\n\
mouse-1: Display \"*Gitter Notifications*\" buffer\n\
mouse-3: Clear pending notifications"
erc-gn-pending)
local-map ,gitter-notifications-mode-line-map
face erc-gn-notifications-face)
"]")
"")))
(defvar erc-gn-notifications nil
"Gitter Notification string for modeline.")
(put 'erc-gn-notifications 'risky-local-variable t)
(defun erc-gn-update (&optional count)
(if count
(setq erc-gn-pending count)
(setq erc-gn-pending (1+ erc-gn-pending)))
(erc-gn-notifications))
(defun erc-gn-clear-notif ()
(interactive)
(move-overlay erc-gn-unread-overlay (point-min) (point-max))
(erc-gn-update 0))
(provide 'erc-gitter-notifications)
;;; erc-gitter-notifications.el ends here