-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmsg-clean.el
More file actions
180 lines (145 loc) · 5.28 KB
/
msg-clean.el
File metadata and controls
180 lines (145 loc) · 5.28 KB
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
;;; msg-clean.el --- Keep messages buffer clean -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2026 Shen, Jen-Chieh
;; Created date 2022-02-17 16:16:50
;; Author: Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/jcs-elpa/msg-clean
;; Version: 0.1.0
;; Package-Requires: ((emacs "26.1") (msgu "0.1.0"))
;; Keywords: convenience messages clean
;; 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:
;;
;; Keep messages buffer clean.
;;
;;; Code:
(eval-when-compile
(require 'msgu)
(require 'cl-lib)
(require 'subr-x))
(defgroup msg-clean nil
"Keep messages buffer clean."
:prefix "msg-clean-"
:group 'convenience
:link '(url-link :tag "Repository" "https://github.com/jcs-elpa/msg-clean"))
(defcustom msg-clean-mute-commands
nil
"List of commands to mute completely."
:type '(list symbol)
:group 'msg-clean)
(defcustom msg-clean-echo-commands
nil
"List of commands to inhibit log to *Messages* buffer."
:type '(list symbol)
:group 'msg-clean)
(defcustom msg-clean-minor-mode nil
"Echo/Mute to all minor-mode."
:type '(choice (const :tag "Mute minor-mode enable/disable" mute)
(const :tag "Echo minor-mode enable/disable" echo)
(const :tag "Does nothing" nil))
:group 'msg-clean)
;;
;; (@* "Util" )
;;
(defun msg-clean--ad-add (lst fnc)
"Do `advice-add' for LST with FNC."
(dolist (cmd lst) (advice-add cmd :around fnc)))
(defun msg-clean--ad-remove (lst fnc)
"Do `advice-remove' for LST with FNC."
(dolist (cmd lst) (advice-remove cmd fnc)))
(defun msg-clean--function-symbol (symbol)
"Return function name by SYMBOL."
(cl-case symbol
(`mute #'msg-clean--mute)
(`echo #'msg-clean--echo)))
;;
;; (@* "Util" )
;;
(defun msg-clean--re-enable-mode (modename)
"Re-enable the MODENAME."
(msgu-silent
(funcall-interactively modename -1) (funcall-interactively modename 1)))
(defun msg-clean--re-enable-mode-if-was-enabled (modename)
"Re-enable the MODENAME if was enabled."
(when (boundp modename)
(when (symbol-value modename) (msg-clean--re-enable-mode modename))
(symbol-value modename)))
(defun msg-clean--listify (obj)
"Turn OBJ to list."
(if (listp obj) obj (list obj)))
;;
;; (@* "Core" )
;;
(defun msg-clean--apply (inter fnc &rest args)
"Apply (FNC, ARGS); INTER non-nil call it interactively."
(if inter
(apply #'funcall-interactively (append (list fnc) args))
(apply fnc args)))
(defun msg-clean--mute (fnc &rest args)
"Mute any commands (FNC, ARGS)."
(msgu-silent
(let ((inter (called-interactively-p 'interactive)))
(apply #'msg-clean--apply inter fnc args))))
(defun msg-clean--echo (fnc &rest args)
"Mute any commands (FNC, ARGS)."
(msgu-inhibit-log
(let ((inter (called-interactively-p 'interactive)))
(apply #'msg-clean--apply inter fnc args))))
(defun msg-clean--minor-mode-ad-add (&rest _)
"Apply `advice-add' mute/echo to all minor-mode."
(when-let* ((func (msg-clean--function-symbol msg-clean-minor-mode)))
(msg-clean--ad-add minor-mode-list func)))
(defun msg-clean--minor-mode-ad-remove (&rest _)
"Apply `advice-remove' mute/echo to all minor-mode."
(when-let* ((func (msg-clean--function-symbol msg-clean-minor-mode)))
(msg-clean--ad-remove minor-mode-list func)))
(defun msg-clean--enable ()
"Enable function `msg-clean-mode'."
(msg-clean--ad-add msg-clean-mute-commands #'msg-clean--mute)
(msg-clean--ad-add msg-clean-echo-commands #'msg-clean--echo)
(msg-clean--minor-mode-ad-add)
(advice-add 'add-minor-mode :after #'msg-clean--minor-mode-ad-add))
(defun msg-clean--disable ()
"Disable function `msg-clean-mode'."
(msg-clean--ad-remove msg-clean-mute-commands #'msg-clean--mute)
(msg-clean--ad-remove msg-clean-echo-commands #'msg-clean--echo)
(advice-remove 'add-minor-mode #'msg-clean--minor-mode-ad-add)
(msg-clean--minor-mode-ad-remove))
;;;###autoload
(define-minor-mode msg-clean-mode
"Minor mode `msg-clean-mode'."
:global t
:require 'msg-clean-mode
:group 'msg-clean
(if msg-clean-mode (msg-clean--enable) (msg-clean--disable)))
;;
;; (@* "Users" )
;;
(defun msg-clean--add-commands (command lst)
"Add COMMAND to LST."
(let ((commands (msg-clean--listify command)))
(nconc lst commands)
(msg-clean--re-enable-mode-if-was-enabled #'msg-clean-mode)))
;;;###autoload
(defun msg-clean-add-echo-commands (command)
"Add COMMAND to echo list."
(msg-clean--add-commands command msg-clean-echo-commands))
;;;###autoload
(defun msg-clean-add-mute-commands (command)
"Add COMMAND to mute list."
(msg-clean--add-commands command msg-clean-mute-commands))
(provide 'msg-clean-mode)
;; Local Variables:
;; coding: utf-8
;; no-byte-compile: t
;; End:
;;; msg-clean.el ends here