forked from k1LoW/emacs-drill-instructor
/
drill-instructor.el
203 lines (172 loc) · 6.64 KB
/
drill-instructor.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
197
198
199
200
201
202
203
;;; drill-instructor.el --- Enforce key-bind of Emacs.
;; -*- Mode: Emacs-Lisp -*-
;; Copyright (C) 2008-2009 by 101000code/101000LAB
;;
;; 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 2 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, write to the Free Software
;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
;;
;; Version: 1.1.4
;; Author: k1LoW (Kenichirou Oyama), <k1lowxb [at] gmail [dot] com> <k1low [at] 101000lab [dot] org>
;; URL: http://code.101000lab.org, http://trac.codechecki.in
;;; Install
;; Put this file into load-path'ed directory, and byte compile it if
;; desired. And put the following expression into your ~/.emacs.
;;
;; (require 'drill-instructor)
;;
;; If you call drill instructor for Emacs, M-x drill-instructor.
;;
;; If you call him always put the following expression
;; (setq drill-instructor-global t)
;;; Change Log
;; 1.1.4 Enforce TAB to C-i. (use window-system)
;; 1.1.3 keymap bug fix.
;; 1.1.2 bug fix drill-instructor-switch.
;; 1.1.1 add defadvice pop-to-buffer.
;; 1.1.0 New valiables drill-instructor-unset-major-mode-list.
;; 1.0.2 Applied id:tomisima patch. Enfoece Enter to C-m.
;; 1.0.1 Enforce C-h to delete-backward-char.
;; 1.0.0 First released.
;;; Code:
;; mode
(defvar drill-instructor nil
"Enforce key-bind of Emacs.")
(defvar drill-instructor-global nil
"drill-instructor-global")
(defvar drill-instructor-unset-major-mode-list '(term-mode)
"Drill instructor unset list")
(defvar drill-instructor-alert-expression 'message
"Drill instructor print method")
;from http://bc.tech.coop/blog/071226.html
(defvar drill-instructor-cuss-file "~/hartman.txt"
"The file that cuss words come from.")
(defvar drill-instructor-cuss-strings nil
"The cuss words in the cuss file.")
;from http://d.hatena.ne.jp/MIZUNO/20110222/1298392472
(defface drill-instructor-pos-tip
'((t
:foreground "white"
:background "red"))
"Face for description in drill-instructor-pos-tip's tooltip.")
(defun drill-instructor-cuss-open-file (file)
(find-file file)
(if (null drill-instructor-cuss-strings)
(let ((strings nil)
(prev 1))
(goto-char (point-min))
(while (re-search-forward "^%$" (point-max) t)
(push (buffer-substring-no-properties prev (- (point) 1))
strings)
(setq prev (1+ (point))))
(push (buffer-substring-no-properties prev (point-max)) strings)
(setq drill-instructor-cuss-strings (apply 'vector strings)))))
(defun drill-instructor-cuss ()
"Get a cuss words to display."
(interactive)
(when (null drill-instructor-cuss-strings)
(drill-instructor-cuss-open-file drill-instructor-cuss-file)
(kill-buffer (current-buffer)))
(cond
((< 2 (length drill-instructor-cuss-strings))
(let* ((n (random (length drill-instructor-cuss-strings)))
(string (aref drill-instructor-cuss-strings n)))
string))
(t nil)))
;; setq minor-mode-alist
(if (not (assq 'drill-instructor minor-mode-alist))
(setq minor-mode-alist
(cons '(drill-instructor " Drill")
minor-mode-alist)))
;; defun drill-instructor
(defun drill-instructor (&optional arg)
"Enforce key-bind of Emacs."
(interactive)
;; mode variable settings
(cond
((< (prefix-numeric-value arg) 0)
(setq drill-instructor nil))
(arg
(setq drill-instructor t))
(t
(setq drill-instructor (not drill-instructor))))
;; content
(if drill-instructor
(progn
(if window-system
(progn
(define-key drill-instructor-key-map [return] 'drill-instructor-alert-return)
(define-key drill-instructor-key-map [tab] 'drill-instructor-alert-tab)))
(add-to-list 'minor-mode-map-alist (cons 'drill-instructor drill-instructor-key-map)))
nil)
)
(defun drill-instructor-switch ()
"drill-instructor-switch"
(if drill-instructor-global
(if (memq major-mode drill-instructor-unset-major-mode-list)
(setq drill-instructor nil)
(drill-instructor t)
)))
(defadvice switch-to-buffer (after drill-instructor-switch-to-buffer activate)
"drill-instructor-switch-to-buffer"
(drill-instructor-switch))
(defadvice kill-buffer (after drill-instructor-kill-buffer activate)
"drill-instructor-kill-buffer"
(drill-instructor-switch))
(defadvice other-window (after drill-instructor-other-window activate)
"drill-instructor-other-window"
(drill-instructor-switch))
(defadvice delete-window (after drill-instructor-delete-window activate)
"drill-instructor-delete-window"
(drill-instructor-switch))
;; key-map
(defvar drill-instructor-key-map
(let ((map (make-sparse-keymap)))
(define-key map [up] 'drill-instructor-alert-up)
(define-key map [down] 'drill-instructor-alert-down)
(define-key map [right] 'drill-instructor-alert-right)
(define-key map [left] 'drill-instructor-alert-left)
(define-key map (kbd "DEL") 'drill-instructor-alert-del)
(define-key map "\C-h" 'delete-backward-char);C-h -> delete-backward-char
map))
(defun drill-instructor-alert (msg)
(cond
((and (fboundp 'pos-tip-show) (equal drill-instructor-alert-expression 'pos-tip))
(pos-tip-show (concat msg "\n" (drill-instructor-cuss)) 'drill-instructor-pos-tip nil nil))
(t
(message (concat msg "\n" (drill-instructor-cuss))))))
(defun drill-instructor-alert-up ()
(interactive)
(drill-instructor-alert "Don't use up-key!!! Press C-p!! M-p!!!"))
(defun drill-instructor-alert-down ()
(interactive)
(drill-instructor-alert "Don't use down-key!!! Press C-n!! M-n!!!"))
(defun drill-instructor-alert-right ()
(interactive)
(drill-instructor-alert "Don't use right-key!!! Press C-f!! C-f!!!"))
(defun drill-instructor-alert-left ()
(interactive)
(drill-instructor-alert "Don't use left-key!!! Press C-b!! C-b!!!"))
(defun drill-instructor-alert-del ()
(interactive)
(drill-instructor-alert "Don't use DEL!!! Press C-h!! C-h!!!"))
(defun drill-instructor-alert-return ()
(interactive)
(drill-instructor-alert "Don't use RETURN!!! Press C-m!! C-m!!!"))
(defun drill-instructor-alert-tab ()
(interactive)
(drill-instructor-alert "Don't use TAB!!! Press C-i!! C-i!!!"))
;; mode provide
(provide 'drill-instructor)
;;; end
;;; drill-instructor.el ends here