-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpjb-oldies.el
296 lines (248 loc) · 9.82 KB
/
pjb-oldies.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
;;;; -*- mode:emacs-lisp;coding:utf-8 -*-
;;;;****************************************************************************
;;;;FILE: pjb-oldies.el
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: POSIX
;;;;USER-INTERFACE: NONE
;;;;DESCRIPTION
;;;;
;;;; This mode combines paper-mode, caps-mode and upcase-lisp and
;;;; downcase-lisp in find-file-hook and
;;;; before-save-hook/after-save-hook to let the user work with
;;;; uppercase like in old times.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal Bourguignon <[email protected]>
;;;;MODIFICATIONS
;;;; 2021-09-15 <PJB> Created.
;;;;BUGS
;;;;LEGAL
;;;; GPL
;;;;
;;;; Copyright Pascal Bourguignon 2004 - 202021
;;;;
;;;; 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., 59 Temple Place, Suite 330,
;;;; Boston, MA 02111-1307 USA
;;;;****************************************************************************
(defconst oldies:+computer-paper-colors+ '("azure" "PaleTurquoise" "LightCyan1"
"LightBlue" "LightCyan" "PowderBlue"))
(defun oldies:delete-all-overlays ()
(interactive)
(mapc (lambda (item)
(if (listp item)
(mapc (function delete-overlay) item)
(delete-overlay item)))
(overlay-lists)))
(defun oldies:put-computer-paper-overlay (modulo block)
(oldies:delete-all-overlays)
(goto-char (point-min))
(let ((backf (make-vector oldies:+computer-paper-colors+ nil))
(line 0)
(i -1))
(dolist (color oldies:+computer-paper-colors+)
(setf (aref backf (incf i))
(let* ((facesym (intern (concatenate 'string color "-face")))
(face (make-face facesym)))
(copy-face 'default face)
(set-face-foreground face "black")
(set-face-background face color)
face)))
(while (< (point) (point-max))
(let ((extent (make-overlay (progn (beginning-of-line) (point))
(progn (forward-line block)
(beginning-of-line) (point)))))
(overlay-put extent 'evaporate t)
(overlay-put extent 'face (aref backf (mod line modulo)))
(incf line)))))
(defun oldies:computer-paper ()
(interactive)
(let (modulo
block
(ncolors (length oldies:+computer-paper-colors+))
(cond
((integerp current-prefix-arg)
(setf modulo (min current-prefix-arg ncolors)
block 1))
((null current-prefix-arg)
(setf modulo (min 2 ncolors)
block 1))
((consp current-prefix-arg)
(setf modulo (min (read-minibuffer "Modulo: " "2") ncolors)
block (read-minibuffer "Block: " "1")))
(t (error "Invalid prefix %S" current-prefix-arg)))
(assert (<= 1 block))
(assert (and (<= 2 modulo) (<= modulo ncolors)))
(oldies:put-computer-paper-overlay modulo block)))
(defvar oldies:*line-length* 132) ; or 80
(defun oldies:complete-line ()
(interactive)
(let ((length (- (progn (end-of-line) (point))
(progn (beginning-of-line) (point)))))
(cond
((< length oldies:*line-length*)
(end-of-line)
(insert (make-string (- oldies:*line-length* length) 32))
(next-line))
((> length oldies:*line-length*)
(beginning-of-line)
(forward-char oldies:*line-length*)
(insert "\n")))))
(defun oldies:complete-lines-in-region (start end)
(interactive "r")
(let ((endm (make-marker)))
(set-marker endm end)
(goto-char start)
(while (< (point) endm)
(oldies:complete-line))))
(defun oldies:complete-lines-of-buffer ()
(interactive)
(oldies:complete-lines-in-region (point-min) (point-max)))
(defun skip-to-next-sexp ()
(interactive)
(while (or
(looking-at "\\([ \n\t\v\f\r]+\\)") ; spaces
(looking-at "\\(;.*$\\)") ; ;xxx comment
(looking-at "\\(#|\\([^|]\\||[^#]\\)*|#\\)")) ; #|xxx|# comment
(goto-char (match-end 0))))
(defun cl-looking-at-what ()
(cond
((looking-at "[ \n\t\v\f\r]") :space)
((looking-at ";") :semicolon-comment) ; ;xxx
((looking-at "#|") :sharp-comment) ; #|xxx|#
((looking-at "\"") :string) ; "xx\"x"
((looking-at "(") :beginning-of-list)
((looking-at ")") :end-of-list)
((looking-at ",@") :comma-at)
((looking-at ",") :comma)
((looking-at "'") :quote)
((looking-at "`") :backquote)
(t :atom)))
(defun cl-skip-over-sharp-comment ()
(let ((start (match-beginning 0)))
(goto-char (match-end 0))
(loop named :search do
(re-search-forward "\\(#|\\||#\\)")
(if (string= "#|" (match-string 0))
(progn
(cl-skip-over-sharp-comment)
(goto-char (match-end 0)))
(let ((end (match-end 0)))
(set-match-data (list start (point)))
(return-from :search))))))
(defun cl-skip-over (&optional what)
(setf what (or what (cl-looking-at-what)))
(case what
((:space) (looking-at "[ \n\t\v\f\r]+"))
((:semicolon-comment) (looking-at ";.*$"))
((:sharp-comment) (when (looking-at "#|")
(cl-skip-over-sharp-comment)
t))
((:string) (looking-at "\"\\(\\(\\|\\\\.\\|\\\\\n\\)[^\\\\\"]*\\)*\""))
((:beginning-of-list) (looking-at "("))
((:end-of-list) (looking-at ")"))
((:quote) (looking-at "'"))
((:backquote) (looking-at "`"))
((:comma) (looking-at ","))
((:comma-at) (looking-at ",@"))
((:atom) (looking-at
"\\(|[^|]*|\\|\\\\.\\|#[^|]\\|[^\"\\#|;()'`, \n\t\v\f\r\\]\\)+"))
(otherwise (error "cannot skip over %s" what)))
(goto-char (match-end 0)))
(defun cl-forward (&optional n)
(interactive "p")
(setf n (or n 1))
(dotimes (i n t)
(cl-skip-over)))
(defun cl-what-is-at-point ()
(interactive)
(message "%s" (cl-looking-at-what)))
(defun case-lisp-region (start end transform)
"
do: applies transform on all subregions from start to end that are not
a quoted character, a quote symbol, a comment (;... or #|...|#),
or a string.
"
(save-excursion
(goto-char start)
(while (< (point) end)
(while (and (< (point) end)
(or (looking-at "[^\"#|;\\\\]+")
(and (looking-at "#")
(not (looking-at "#|")))))
(goto-char (match-end 0)))
(funcall transform start (min end (point)))
(cl-skip-over)
(setq start (point)))))
(defun oldies:upcase-lisp-region (start end)
"
DO: From the start to end, converts to upcase all symbols.
Does not touch string literals, comments starting with ';' and
symbols quoted with '|' or with '\\'.
"
(interactive "*r")
(oldies: case-lisp-region start end (function upcase-region))
(message "Upcase LISP Done."))
(defun upcase-lisp ()
"
DO: From the (point) to (point-max), converts to upcase all symbols.
Does not touch string literals, comments starting with ';' and
symbols quoted with '|' or with '\\'.
"
(interactive "*")
(upcase-lisp-region (point) (point-max)))
(defun downcase-lisp-region (start end)
"
DO: From the start to end, converts to low-case all symbols.
Does not touch string literals, comments starting with ';' and
symbols quoted with '|' or with '\\'.
"
(interactive "*r")
(case-lisp-region start end (function downcase-region))
(message "Downcase LISP Done."))
(defun downcase-lisp ()
"
DO: From the (point) to (point-max), converts to lowcase all symbols.
Does not touch string literals, comments starting with ';' and
symbols quoted with '|' or with '\\'.
"
(interactive "*")
(downcase-lisp-region (point) (point-max)))
(defun caps-mode-self-insert-command (&optional n)
"Like `self-insert-command', but uppercase the the typed character."
(interactive "p")
(insert-char (upcase last-command-event) n))
(defvar *caps-mode-map* nil)
(when (fboundp 'define-minor-mode)
(define-minor-mode oldies-mode
"Toggle oldies mode
With no argument, this command toggles the mode.
Non-null prefix argument turns on the mode.
Null prefix argument turns off the mode."
:init-value nil
:lighter " Old"
;; Caps Mode:
(setq *caps-mode-map*
(let ((map (make-sparse-keymap)))
(substitute-key-definition 'self-insert-command
'caps-mode-self-insert-command
map global-map)
map))
(if oldies-mode
(add-to-list 'minor-mode-map-alist (cons 'oldies-mode *caps-mode-map*))
(setq minor-mode-map-alist
(delete (assoc 'oldies-mode minor-mode-map-alist)
minor-mode-map-alist)))
(computer-paper)))
;;;; THE END ;;;;