-
Notifications
You must be signed in to change notification settings - Fork 11
/
navi2ch-auto-modify.el
255 lines (228 loc) · 8.2 KB
/
navi2ch-auto-modify.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
;;; navi2ch-auto-modify.el --- auto file modification module for navi2ch -*- coding: iso-2022-7bit; -*-
;; Copyright (C) 2003, 2005, 2006 by Navi2ch Project
;; Author: extra <[email protected]>
;; Keywords: network, 2ch
;; This file 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, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;;; Code:
(provide 'navi2ch-auto-modify)
(defconst navi2ch-auto-modify-ident
"$Id$")
(eval-when-compile (require 'cl))
(require 'navi2ch-vars)
(require 'navi2ch-util)
(defvar navi2ch-auto-modify-variable-list nil
"$B@_Dj$r<+F0E*$KJQ99$7$FJ]B8$9$kJQ?tL>$N%j%9%H!#(B")
(add-hook 'navi2ch-exit-hook 'navi2ch-auto-modify-save)
(defun navi2ch-auto-modify-subr (body)
(prog2 (setq navi2ch-auto-modify-variable-list nil)
(eval (cons 'progn body))
(let (added)
(dolist (sexp body)
(when (memq (car-safe sexp) '(setq setq-default))
(setq sexp (cdr sexp))
(while sexp
(unless (or (memq (car sexp) navi2ch-auto-modify-variable-list)
(memq (car sexp) added))
(setq added (cons (car sexp) added)))
(setq sexp (cddr sexp)))))
(when added
(setq navi2ch-auto-modify-variable-list
(append navi2ch-auto-modify-variable-list
(nreverse added)))))))
(defmacro navi2ch-auto-modify (&rest body)
"`navi2ch-auto-modify-file' $B$G;XDj$5$l$?%U%!%$%k$K5-=R$9$k$H!"(B
$B$=$NCf$K4^$^$l$kJQ?t$N@_Dj$r<+F0E*$KJQ99$7$FJ]B8$9$k!#(B
$BNc$($P2<5-$N$h$&$K5-=R$9$k$H!"(B
$BJQ?t(B `navi2ch-article-message-filter-by-id-alist' $B$H(B
`navi2ch-article-message-filter-by-message-alist' $B$N@_DjCM$O!"(B
Navi2ch $B=*N;;~$K<+F0E*$KJQ99!&J]B8$5$l$k!#(B
\(navi2ch-auto-modify
(setq navi2ch-article-message-filter-by-id-alist
...)
(setq navi2ch-article-message-filter-by-message-alist
...))"
`(navi2ch-auto-modify-subr ',body))
(put 'navi2ch-auto-modify 'lisp-indent-function 0)
(defun navi2ch-auto-modify-variables (variables)
(let (added)
(dolist (var variables)
(unless (or (memq var navi2ch-auto-modify-variable-list)
(memq var added))
(setq added (cons var added))))
(when added
(setq navi2ch-auto-modify-variable-list
(append navi2ch-auto-modify-variable-list (nreverse added)))))
(navi2ch-auto-modify-save))
(eval-when-compile
(defmacro default-major-mode ()
(if (and (<= 23 emacs-major-version)
(<= 1 emacs-minor-version))
''major-mode
''default-major-mode)))
(defun navi2ch-auto-modify-save ()
(run-hooks 'navi2ch-auto-modify-save-hook)
(navi2ch-auto-modify-truncate-lists)
(when navi2ch-auto-modify-variable-list
(let ((navi2ch-auto-modify-file
(if (eq navi2ch-auto-modify-file t)
(locate-library (expand-file-name navi2ch-init-file
navi2ch-directory))
navi2ch-auto-modify-file)))
(when navi2ch-auto-modify-file
(let ((inhibit-read-only t)
(require-final-newline (eq require-final-newline t))
(value-buffer (current-buffer))
(exist-buffer (get-file-buffer navi2ch-auto-modify-file)))
(save-current-buffer
(let ((default-major-mode 'fundamental-mode))
(set-buffer (find-file-noselect navi2ch-auto-modify-file)))
(save-excursion
(save-restriction
(widen)
(navi2ch-auto-modify-narrow)
(navi2ch-auto-modify-save-variables value-buffer)))
(unless exist-buffer
(basic-save-buffer)
(kill-buffer (current-buffer))))))
(navi2ch-auto-modify-customize-variables))))
(defun navi2ch-auto-modify-skip-comments ()
(while (and (not (eobp))
(forward-comment 1))))
(defun navi2ch-auto-modify-narrow ()
(goto-char (point-min))
(navi2ch-auto-modify-skip-comments)
;; Test for scan errors.
(save-excursion
(while (not (eobp))
(forward-sexp)))
(catch 'loop
(let ((standard-input (current-buffer)))
(while (not (eobp))
(condition-case nil
(let ((beg (point))
(sexp (read)))
(when (consp sexp)
(if (eq (car sexp) 'navi2ch-auto-modify)
(progn
(narrow-to-region beg (point))
(throw 'loop nil))
(when (re-search-backward "\\<navi2ch-auto-modify\\>"
(1+ beg) t)
(goto-char (1+ beg))))))
(invalid-read-syntax nil))
(navi2ch-auto-modify-skip-comments)))
(unless (bobp)
(skip-chars-backward "\n" (1- (point)))
(let ((count (save-excursion (skip-chars-backward "\n"))))
(when (> count -2)
(insert-char ?\n (+ count 2))))
(narrow-to-region (point) (point)))
(insert "(navi2ch-auto-modify)")))
(defun navi2ch-auto-modify-save-variables (&optional buffer)
(goto-char (1+ (point-min))) ; "\\`("
(forward-sexp) ; "navi2ch-auto-modify"
(navi2ch-auto-modify-skip-comments)
(let ((standard-input (current-buffer))
(standard-output (current-buffer))
(print-length nil)
(print-level nil)
modified)
(condition-case nil
(while (not (eobp))
(let ((beg (point))
(sexp (read)))
(when (memq (car-safe sexp) '(setq setq-default))
(save-excursion
(goto-char (1+ beg)) ; "("
(forward-sexp) ; "setq\\(-default\\)?"
(navi2ch-auto-modify-skip-comments)
(condition-case nil
(while (not (eobp))
(let ((var (read))
start end)
(navi2ch-auto-modify-skip-comments)
(setq start (point))
(forward-sexp)
(delete-region start (point))
(pp (navi2ch-quote-maybe
(if (and buffer
(local-variable-p var buffer))
(with-current-buffer buffer
(symbol-value var))
(symbol-value var))))
(setq end (point-marker))
(goto-char start)
(indent-sexp)
(forward-sexp)
(delete-region (point) end)
(unless (memq var modified)
(setq modified (cons var modified))))
(navi2ch-auto-modify-skip-comments))
(invalid-read-syntax nil))))) ; ")"
(navi2ch-auto-modify-skip-comments))
(invalid-read-syntax nil)) ; ")\\'"
(backward-char)
(dolist (var navi2ch-auto-modify-variable-list)
(unless (memq var modified)
(unless (navi2ch-auto-modify-customize-variable-p var)
(insert ?\n)
(lisp-indent-line)
(let ((start (point))
end)
(pp (list (if (local-variable-if-set-p var (current-buffer))
'setq-default
'setq)
var
(navi2ch-quote-maybe
(if (and buffer
(local-variable-p var buffer))
(with-current-buffer buffer
(symbol-value var))
(symbol-value var)))))
(setq end (point-marker))
(goto-char start)
(indent-sexp)
(forward-sexp)
(delete-region (point) end)))
(setq modified (cons var modified))))
(setq navi2ch-auto-modify-variable-list (nreverse modified))))
(defun navi2ch-auto-modify-customize-variable-p (variable)
(or (null navi2ch-auto-modify-file)
(get variable 'saved-value) ; From `customize-saved'
(get variable 'saved-variable-comment))) ; For XEmacs
(defun navi2ch-auto-modify-customize-variables ()
(let (customized)
(dolist (var navi2ch-auto-modify-variable-list)
(when (navi2ch-auto-modify-customize-variable-p var)
(customize-set-variable var (symbol-value var))
(setq customized t)))
(when customized
(customize-save-customized))))
(defun navi2ch-auto-modify-truncate-lists ()
(when navi2ch-auto-modify-truncate-list-alist
(let (added)
(dolist (slot navi2ch-auto-modify-truncate-list-alist)
(when (> (length (symbol-value (car slot))) (cdr slot))
(if (zerop (cdr slot))
(set (car slot) nil)
(setcdr (nthcdr (1- (cdr slot)) (symbol-value (car slot))) nil))
(unless (or (memq (car slot) navi2ch-auto-modify-variable-list)
(memq (car slot) added))
(setq added (cons (car slot) added)))))
(when added
(setq navi2ch-auto-modify-variable-list
(append navi2ch-auto-modify-variable-list (nreverse added)))))))
;;; navi2ch-auto-modify.el ends here