-
Notifications
You must be signed in to change notification settings - Fork 11
/
navi2ch-util.el
1525 lines (1383 loc) · 54.2 KB
/
navi2ch-util.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; navi2ch-util.el --- useful utilities for navi2ch -*- coding: iso-2022-7bit; -*-
;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
;; 2009 by Navi2ch Project
;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 Free
;; Software Foundation, Inc.
;; Author: Taiki SUGAWARA <[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-util)
(defconst navi2ch-util-ident
"$Id$")
(eval-when-compile (require 'cl))
(require 'timezone)
(require 'browse-url)
(require 'base64)
(require 'navi2ch-vars)
(defun navi2ch-alist-to-hash (alist &rest keywords-args)
(let ((table (apply #'make-hash-table :size (length alist) keywords-args)))
(dolist (x alist table)
(puthash (car x) (cdr x) table))))
(defvar navi2ch-mode-line-identification nil)
(make-variable-buffer-local 'navi2ch-mode-line-identification)
(defvar navi2ch-replace-html-tag-alist
'((">" . ">")
("<" . "<")
(""" . "\"")
(" " . " ")
("&" . "&")
("<br>" . "\n")
("<hr>" . "\n--\n"))
"$BCV49$9$k(B html $B$N%?%0$NO"A[%j%9%H(B ($B@55,I=8=$O;H$($J$$(B)$B!#(B")
(defvar navi2ch-replace-html-tag-regexp-alist
'(("</?[?!a-zA-Z][^<>]*>" . "")
("&[a-z][a-z0-9]*;?" . navi2ch-entity-reference-to-str)
("&#[0-9]+;?" . navi2ch-numeric-reference-to-str)
("&#[xX][0-9a-fA-f]+;?" . navi2ch-hexadecimal-reference-to-str))
"$BCV49$9$k(B html $B$N%?%0$NO"A[%j%9%H(B($B@55,I=8=(B)
$BCV49@h$,4X?t$@$H!"CV4985$r0z?t$H$7$F$=$N4X?t$r8F$S$@$7$?$b$N$GCV$-BX$($k!#(B
$B@55,I=8=$,I,MW$J$$>l9g$O(B `navi2ch-replace-html-tag-alist' $B$KF~$l$k(B")
(defvar navi2ch-replace-html-tag-regexp-internal nil
"$BCV49$9$k(B html $B$N%?%0$N@55,I=8=!#(B")
(defvar navi2ch-entity-table
(navi2ch-alist-to-hash
'(("iexcl" . 161) ("cent" . 162) ("pound" . 163)
("curren" . 164) ("yen" . 165) ("brvbar" . 166)
("sect" . 167) ("uml" . 168) ("copy" . 169)
("ordf" . 170) ("laquo" . 171) ("not" . 172)
("shy" . 173) ("reg" . 174) ("macr" . 175)
("deg" . 176) ("plusmn" . 177) ("sup2" . 178)
("sup3" . 179) ("acute" . 180) ("micro" . 181)
("para" . 182) ("middot" . 183) ("cedil" . 184)
("sup1" . 185) ("ordm" . 186) ("raquo" . 187)
("frac14" . 188) ("frac12" . 189) ("frac34" . 190)
("iquest" . 191) ("Agrave" . 192) ("Aacute" . 193)
("Acirc" . 194) ("Atilde" . 195) ("Auml" . 196)
("Aring" . 197) ("AElig" . 198) ("Ccedil" . 199)
("Egrave" . 200) ("Eacute" . 201) ("Ecirc" . 202)
("Euml" . 203) ("Igrave" . 204) ("Iacute" . 205)
("Icirc" . 206) ("Iuml" . 207) ("ETH" . 208)
("Ntilde" . 209) ("Ograve" . 210) ("Oacute" . 211)
("Ocirc" . 212) ("Otilde" . 213) ("Ouml" . 214)
("times" . 215) ("Oslash" . 216) ("Ugrave" . 217)
("Uacute" . 218) ("Ucirc" . 219) ("Uuml" . 220)
("Yacute" . 221) ("THORN" . 222) ("szlig" . 223)
("agrave" . 224) ("aacute" . 225) ("acirc" . 226)
("atilde" . 227) ("auml" . 228) ("aring" . 229)
("aelig" . 230) ("ccedil" . 231) ("egrave" . 232)
("eacute" . 233) ("ecirc" . 234) ("euml" . 235)
("igrave" . 236) ("iacute" . 237) ("icirc" . 238)
("iuml" . 239) ("eth" . 240) ("ntilde" . 241)
("ograve" . 242) ("oacute" . 243) ("ocirc" . 244)
("otilde" . 245) ("ouml" . 246) ("divide" . 247)
("oslash" . 248) ("ugrave" . 249) ("uacute" . 250)
("ucirc" . 251) ("uuml" . 252) ("yacute" . 253)
("thorn" . 254) ("yuml" . 255) ("fnof" . 402)
("Alpha" . 913) ("Beta" . 914) ("Gamma" . 915)
("Delta" . 916) ("Epsilon" . 917) ("Zeta" . 918)
("Eta" . 919) ("Theta" . 920) ("Iota" . 921)
("Kappa" . 922) ("Lambda" . 923) ("Mu" . 924)
("Nu" . 925) ("Xi" . 926) ("Omicron" . 927)
("Pi" . 928) ("Rho" . 929) ("Sigma" . 931)
("Tau" . 932) ("Upsilon" . 933) ("Phi" . 934)
("Chi" . 935) ("Psi" . 936) ("Omega" . 937)
("alpha" . 945) ("beta" . 946) ("gamma" . 947)
("delta" . 948) ("epsilon" . 949) ("zeta" . 950)
("eta" . 951) ("theta" . 952) ("iota" . 953)
("kappa" . 954) ("lambda" . 955) ("mu" . 956)
("nu" . 957) ("xi" . 958) ("omicron" . 959)
("pi" . 960) ("rho" . 961) ("sigmaf" . 962)
("sigma" . 963) ("tau" . 964) ("upsilon" . 965)
("phi" . 966) ("chi" . 967) ("psi" . 968)
("omega" . 969) ("thetasym" . 977) ("upsih" . 978)
("piv" . 982) ("bull" . 8226) ("hellip" . 8230)
("prime" . 8242) ("Prime" . 8243) ("oline" . 8254)
("frasl" . 8260) ("weierp" . 8472) ("image" . 8465)
("real" . 8476) ("trade" . 8482) ("alefsym" . 8501)
("larr" . 8592) ("uarr" . 8593) ("rarr" . 8594)
("darr" . 8595) ("harr" . 8596) ("crarr" . 8629)
("lArr" . 8656) ("uArr" . 8657) ("rArr" . 8658)
("dArr" . 8659) ("hArr" . 8660) ("forall" . 8704)
("part" . 8706) ("exist" . 8707) ("empty" . 8709)
("nabla" . 8711) ("isin" . 8712) ("notin" . 8713)
("ni" . 8715) ("prod" . 8719) ("sum" . 8721)
("minus" . 8722) ("lowast" . 8727) ("radic" . 8730)
("prop" . 8733) ("infin" . 8734) ("ang" . 8736)
("and" . 8743) ("or" . 8744) ("cap" . 8745)
("cup" . 8746) ("int" . 8747) ("there4" . 8756)
("sim" . 8764) ("cong" . 8773) ("asymp" . 8776)
("ne" . 8800) ("equiv" . 8801) ("le" . 8804)
("ge" . 8805) ("sub" . 8834) ("sup" . 8835)
("nsub" . 8836) ("sube" . 8838) ("supe" . 8839)
("oplus" . 8853) ("otimes" . 8855) ("perp" . 8869)
("sdot" . 8901) ("lceil" . 8968) ("rceil" . 8969)
("lfloor" . 8970) ("rfloor" . 8971) ("lang" . 9001)
("rang" . 9002) ("loz" . 9674) ("spades" . 9824)
("clubs" . 9827) ("hearts" . 9829) ("diams" . 9830)
("OElig" . 338) ("oelig" . 339) ("Scaron" . 352)
("scaron" . 353) ("Yuml" . 376) ("circ" . 710)
("tilde" . 732) ("ensp" . 8194) ("emsp" . 8195)
("thinsp" . 8201) ("zwnj" . 8204) ("zwj" . 8205)
("lrm" . 8206) ("rlm" . 8207) ("ndash" . 8211)
("mdash" . 8212) ("lsquo" . 8216) ("rsquo" . 8217)
("sbquo" . 8218) ("ldquo" . 8220) ("rdquo" . 8221)
("bdquo" . 8222) ("dagger" . 8224) ("Dagger" . 8225)
("permil" . 8240) ("lsaquo" . 8249) ("rsaquo" . 8250)
("euro" . 8364))
:test 'equal))
(defconst navi2ch-uuencode-begin-delimiter-regexp
"^begin \\([0-7]+\\) \\([^ \n]+\\)$"
"uuencode $B$5$l$?%3!<%I$NA0$N%G%j%_%?$K%^%C%A$9$k@55,I=8=!#(B")
(defconst navi2ch-uuencode-end-delimiter-regexp
"^end\\([ \t]*\\)$"
"uuencode $B$5$l$?%3!<%I$N8e$N%G%j%_%?$K%^%C%A$9$k@55,I=8=!#(B")
(defconst navi2ch-uuencode-line-regexp
"^[!-`]+$"
"uuencode $B$5$l$?%3!<%I$N$_$,4^$^$l$k9T$K%^%C%A$9$k@55,I=8=!#(B")
(defconst navi2ch-base64-begin-delimiter "----BEGIN BASE64----"
"base64 $B%3!<%I$NA0$KA^F~$9$k%G%j%_%?!#(B")
(defconst navi2ch-base64-end-delimiter "----END BASE64----"
"base64 $B%3!<%I$N8e$KA^F~$9$k%G%j%_%?!#(B")
(defconst navi2ch-base64-begin-delimiter-regexp
(format "^%s\\((\\([^\)]+\\))\\)?.*$"
(regexp-quote navi2ch-base64-begin-delimiter))
"base64 $B%3!<%I$NA0$N%G%j%_%?$K%^%C%A$9$k@55,I=8=!#(B")
(defconst navi2ch-base64-end-delimiter-regexp
(format "^%s.*$" (regexp-quote navi2ch-base64-end-delimiter))
"base64 $B%3!<%I$N8e$N%G%j%_%?$K%^%C%A$9$k@55,I=8=!#(B")
(defconst navi2ch-base64-susv3-begin-delimiter-regexp
"^begin-base64 \\([0-7]+\\) \\([^ \n]+\\)$"
"SUSv3 $B$N(B uuencode $B$G:n@.$5$l$k(B base64 $B%3!<%I$NA0$N%G%j%_%?$K%^%C%A$9$k@55,I=8=(B")
(defconst navi2ch-base64-susv3-end-delimiter-regexp
"^====$"
"SUSv3 $B$N(B uuencode $B$G:n@.$5$l$k(B base64 $B%3!<%I$N8e$N%G%j%_%?$K%^%C%A$9$k@55,I=8=(B")
(defconst navi2ch-base64-line-regexp
(concat
"^\\([+/0-9A-Za-z][+/0-9A-Za-z][+/0-9A-Za-z][+/0-9A-Za-z]\\)*"
"[+/0-9A-Za-z][+/0-9A-Za-z][+/0-9A-Za-z=][+/0-9A-Za-z=] *$")
"base64 $B%3!<%I$N$_$,4^$^$l$k9T$K%^%C%A$9$k@55,I=8=!#(B")
(defvar navi2ch-offline nil "$B%*%U%i%$%s%b!<%I$+$I$&$+!#(B")
(defvar navi2ch-online-indicator "[ON] ")
(defvar navi2ch-offline-indicator "[--] ")
(defvar navi2ch-modeline-online navi2ch-online-indicator)
(defvar navi2ch-modeline-offline navi2ch-offline-indicator)
(defvar navi2ch-modeline-be2ch-login "[BE] ")
(defvar navi2ch-modeline-be2ch-logout "")
(put 'navi2ch-modeline-online 'risky-local-variable t)
(put 'navi2ch-modeline-offline 'risky-local-variable t)
(put 'navi2ch-modeline-be2ch-login 'risky-local-variable t)
(put 'navi2ch-modeline-be2ch-logout 'risky-local-variable t)
;; shut up XEmacs warnings
(eval-when-compile
(defvar minibuffer-allow-text-properties))
;;;; macros
(defmacro navi2ch-ifxemacs (then &rest else)
"If on XEmacs, do THEN, else do ELSE.
Like \"(if (featurep 'xemacs) THEN ELSE)\", but expanded at
compilation time. Because byte-code of XEmacs is not compatible with
GNU Emacs's one, this macro is very useful."
(if (featurep 'xemacs)
then
(cons 'progn else)))
;; Navi2ch$B$N%3!<%I$r%O%/$9$k?M$O"-$r(B~/.emacs$B$K$bF~$l$H$-$^$7$g$&!#(B
(put 'navi2ch-ifxemacs 'lisp-indent-function 1)
(defmacro navi2ch-ifemacsce (then &rest else)
"If on EmacsCE, do THEN, else do ELSE.
Expanded at compilation time."
`(if (string-match "windowsce" system-configuration)
,then
(progn ,@else)))
(put 'navi2ch-ifemacsce 'lisp-indent-function 1)
;; from apel
(eval-and-compile
(defmacro navi2ch-defalias-maybe (symbol definition)
"Define SYMBOL as an alias for DEFINITION if SYMBOL is not defined.
See also the function `defalias'."
(setq symbol (eval symbol))
(or (and (fboundp symbol)
(not (get symbol 'defalias-maybe)))
`(or (fboundp (quote ,symbol))
(prog1
(defalias (quote ,symbol) ,definition)
;; `defalias' updates `load-history' internally.
(put (quote ,symbol) 'defalias-maybe t))))))
(defmacro navi2ch-with-default-file-modes (mode &rest body)
"default-file-modes $B$r(B MODE $B$K$7$F(B BODY $B$r<B9T$9$k!#(B"
(let ((temp (make-symbol "--file-modes-temp--")))
`(let ((,temp (default-file-modes)))
(unwind-protect
(progn
(set-default-file-modes
(navi2ch-ifxemacs
(if (integerp ,mode)
,mode
(char-to-int ,mode))
,mode))
,@body)
(set-default-file-modes ,temp)))))
(put 'navi2ch-with-default-file-modes 'lisp-indent-function 1)
(defsubst navi2ch-cache-limit (cache)
(elt cache 0))
(defsubst navi2ch-cache-hash-table (cache)
(elt cache 1))
(defmacro navi2ch-cache-get (key value cache)
`(or (gethash ,key (navi2ch-cache-hash-table ,cache))
(navi2ch-cache-put ,key ,value ,cache)))
;;;; other misc stuff
(defun navi2ch-mouse-key (num)
(navi2ch-ifxemacs
(intern (format "button%d" num))
(vector (intern (format "mouse-%d" num)))))
(defun navi2ch-define-mouse-key (map num command)
(define-key map (navi2ch-mouse-key num) command))
(defvar navi2ch-delete-keys
(list "\d" [del] [delete] [backspace]
(navi2ch-ifxemacs
[(shift space)]
[(shift ? )])))
(defun navi2ch-define-delete-keys (map command)
(dolist (key navi2ch-delete-keys)
(define-key map key command)))
(eval-and-compile
(defalias 'navi2ch-set-buffer-multibyte
(if (fboundp 'set-buffer-multibyte)
#'set-buffer-multibyte
#'identity))
(defalias 'navi2ch-match-string-no-properties
(if (fboundp 'match-string-no-properties)
#'match-string-no-properties
#'match-string)))
(defun navi2ch-no-logging-message (fmt &rest args)
(navi2ch-ifxemacs
(apply #'lmessage 'no-log fmt args)
(let ((message-log-max nil))
(apply #'message fmt args))))
(defun navi2ch-replace-string (regexp rep string
&optional all fixedcase literal)
"STRING $B$K4^$^$l$k(B REGEXP $B$r(B REP $B$GCV49$9$k!#(B
REP $B$,4X?t$N>l9g$O!"%^%C%A$7$?J8;zNs$r0z?t$K$7$F$=$N4X?t$r8F$S=P$9!#(B
FIXEDCASE$B!"(BLITERAL $B$O(B `replace-match' $B$K$=$N$^$^EO$5$l$k!#(B
ALL $B$,(B non-nil $B$J$i$P!"%^%C%A$7$?%F%-%9%H$r$9$Y$FCV49$9$k!#(Bnil $B$J$i(B
$B:G=i$N(B1$B$D$@$1$rCV49$9$k!#(B
REGEXP $B$,8+$D$+$i$J$$>l9g!"(BSTRING $B$r$=$N$^$^JV$9!#(B"
(save-match-data
(if all
;; Emacs 21 $B$N(B replace-regexp-in-string $B$N%Q%/$j!#(B
(let ((start 0)
(l (length string))
mb me str matches)
(while (and (< start l)
(string-match regexp string start))
(setq mb (match-beginning 0)
me (match-end 0))
(if (= mb me)
(setq me (min l (1+ mb))))
(string-match regexp (setq str (substring string mb me)))
(setq matches
(cons (replace-match (if (stringp rep)
rep
(funcall rep (match-string 0 str)))
fixedcase literal str)
(cons (substring string start mb)
matches)))
(setq start me))
(apply #'concat (nreverse (cons (substring string start l)
matches))))
(when (string-match regexp string)
(setq string (replace-match (if (stringp rep)
rep
(funcall rep (match-string 0 string)))
fixedcase literal string)))
string)))
(defun navi2ch-replace-string-regexp-alist
(regexp-alist string &optional all fixedcase literal)
"STRING $BCf$+$i!"(BREGEXP-ALIST $B$N3FMWAG$N(B car $B$r@55,I=8=$H$7!"(Bcdr $B$GCV49$9$k!#(B
cdr $B$,4X?t$N>l9g$O!"%^%C%A$7$?J8;zNs$r0z?t$K$7$F$=$N4X?t$r8F$S=P$9!#(B
FIXEDCASE$B!"(BLITERAL $B$O(B `replace-match' $B$K$=$N$^$^EO$5$l$k!#(B
ALL $B$,(B non-nil $B$J$i$P!"%^%C%A$7$?%F%-%9%H$r$9$Y$FCV49$9$k!#(Bnil $B$J$i(B
$B:G=i$N(B1$B$D$@$1$rCV49$9$k!#(B
REGEXP $B$,8+$D$+$i$J$$>l9g!"(BSTRING $B$r$=$N$^$^JV$9!#(B"
(save-match-data
(let ((internal (navi2ch-regexp-alist-to-internal regexp-alist))
match rep)
(if all
;; Emacs 21 $B$N(B replace-regexp-in-string $B$N%Q%/$j!#(B
(let ((start 0)
(l (length string))
mb me str matches)
(while (and (< start l)
(setq match (navi2ch-string-match-regexp-alist
internal string start)))
(setq mb (match-beginning 0)
me (match-end 0))
(if (= mb me)
(setq me (min l (1+ mb))))
(string-match
(car match)
(setq str (substring string mb me)))
(setq rep (cdr match))
(setq matches
(cons (replace-match (if (stringp rep)
rep
(funcall rep (match-string 0 str)))
fixedcase literal str)
(cons (substring string start mb)
matches)))
(setq start me))
(apply #'concat (nreverse (cons (substring string start l)
matches))))
(when (navi2ch-string-match-regexp-alist internal string)
(setq rep (cdr match))
(setq string (replace-match (if (stringp rep)
rep
(funcall rep (match-string 0 string)))
fixedcase literal string)))
string))))
(defun navi2ch-insert-file-contents (file &optional begin end coding-system)
(setq coding-system (or coding-system navi2ch-coding-system))
(let ((coding-system-for-read coding-system)
(coding-system-for-write coding-system))
(insert-file-contents file nil begin end)))
(defun navi2ch-expand-file-name (file)
(let ((result (expand-file-name
(mapconcat (lambda (ch)
(if (memq ch navi2ch-file-name-reserved-char-list)
(format "%%%02X" ch)
(char-to-string ch)))
(append file)
"")
navi2ch-directory)))
(if (string-match (concat "^"
(regexp-quote (file-name-as-directory
(expand-file-name navi2ch-directory))))
result)
result
(error "Wrong file name"))))
(eval-when-compile
(navi2ch-defalias-maybe 'assoc-string 'ignore))
(defun navi2ch-replace-html-tag (str)
(let ((case-fold-search t))
(navi2ch-replace-string-regexp-alist
navi2ch-replace-html-tag-regexp-internal
str t nil t)))
(defun navi2ch-replace-html-tag-with-buffer ()
(goto-char (point-min))
(let ((case-fold-search t)
match replace)
(while (setq match (navi2ch-re-search-forward-regexp-alist
navi2ch-replace-html-tag-regexp-internal nil t))
(setq replace (cdr match))
(replace-match (if (functionp replace)
(funcall replace (match-string 0))
replace)
nil t))))
(defun navi2ch-replace-html-tag-with-temp-buffer (str)
(with-temp-buffer
(insert str)
(navi2ch-replace-html-tag-with-buffer)
(buffer-string)))
(defun navi2ch-entity-reference-to-str (ref)
"$BJ8;z<BBN;2>H$r%G%3!<%I!#(B"
(save-match-data
(if (and navi2ch-decode-character-references
(string-match "&\\([^;]+\\)" ref))
(let ((code (gethash (match-string 1 ref) navi2ch-entity-table)))
(or (and code (navi2ch-ucs-to-str code))
ref))
ref)))
(defun navi2ch-numeric-reference-to-str (ref)
"$B?tCMJ8;z;2>H$r%G%3!<%I!#(B"
(save-match-data
(if (and navi2ch-decode-character-references
(string-match "&#\\([^;]+\\)" ref))
(or (navi2ch-ucs-to-str (string-to-number (match-string 1 ref))) "$B".(B")
ref)))
(defun navi2ch-hexadecimal-reference-to-str (ref)
"16$B?J?tCMJ8;z;2>H$r%G%3!<%I!#(B"
(save-match-data
(if (and navi2ch-decode-character-references
(string-match "&#[xX]\\([^;]+\\)" ref))
(let ((num))
(setq num (string-to-number (match-string 1 ref) 16))
(or (and num
(navi2ch-ucs-to-str num))
"$B".(B"))
ref)))
;; shut up byte-compile warnings
(eval-when-compile
(navi2ch-defalias-maybe 'unicode-to-char 'ignore)
(navi2ch-defalias-maybe 'decode-char 'ignore))
(eval-and-compile
;; (autoload 'ucs-to-char "unicode")
(defalias 'navi2ch-char-valid-p
(if (fboundp 'characterp) #'characterp #'char-valid-p)))
(defun navi2ch-ucs-to-str (code)
(let ((c (cond
((featurep 'un-define)
(ucs-to-char code))
((and (fboundp 'unicode-to-char)
(subrp (symbol-function 'unicode-to-char)))
(unicode-to-char code))
(navi2ch-on-emacs21
(decode-char 'ucs code)))))
(if (navi2ch-char-valid-p c)
(char-to-string c)
nil)))
(defun navi2ch-read-char (&optional prompt)
"PROMPT (non-nil $B$N>l9g(B) $B$rI=<($7$F(B `read-char' $B$r8F$S=P$9!#(B"
(let ((cursor-in-echo-area t)
c)
(if prompt
(navi2ch-no-logging-message "%s" prompt))
(setq c (read-char))
(if (and prompt
(navi2ch-char-valid-p c))
(navi2ch-no-logging-message "%s%c" prompt c))
c))
(defun navi2ch-read-char-with-retry (prompt retry-prompt list)
"PROMPT $B$rI=<((B (non-nil $B$N>l9g(B) $B$7$F(B `read-char' $B$r8F$S=P$9!#(B
$BF~NO$5$l$?J8;z$,(B LIST $B$K4^$^$l$J$$>l9g!"(BRETRY-PROMPT (nil $B$N>l9g$O(B
PROMPT) $B$rI=<($7$F:FEY(B `read-char' $B$r8F$V!#(B"
(let ((retry t) c)
(while retry
(setq c (navi2ch-read-char prompt))
(cond ((memq c list)
(setq retry nil))
((eq c 12)
(recenter))
(t
(ding)
(setq prompt (or retry-prompt prompt)))))
c))
(defun navi2ch-read-event (&optional prompt)
"PROMPT (non-nil $B$N>l9g(B) $B$rI=<($7$F(B event $B$rFI$`!#(B"
(let ((cursor-in-echo-area t)
e)
(if prompt
(navi2ch-no-logging-message "%s" prompt))
(navi2ch-ifxemacs
(setq e (next-command-event nil prompt))
(setq e (read-event prompt)))
(if prompt
(navi2ch-no-logging-message "%s%s" prompt (single-key-description e)))
e))
(defun navi2ch-y-or-n-p (prompt &optional quit-symbol)
(let* ((prompt (concat prompt "(y, n, or q) "))
(c (navi2ch-read-char-with-retry
prompt
(concat "Please answer y, n, or q. " prompt)
'(?q ?Q ?y ?Y ?\ ?n ?N ?\177))))
(cond ((memq c '(?q ?Q))
(or quit-symbol nil))
((memq c '(?y ?Y ?\ ))
t)
((memq c '(?n ?N ?\177))
nil))))
(eval-when-compile
(defvar browse-url-new-window-flag)
(defvar browse-url-new-window-p)
(defun navi2ch-net-send-request
(url method &optional other-header content))
(defun navi2ch-net-get-status (proc)))
(defun navi2ch-browse-url-internal (url &rest args)
(let ((browse-url-browser-function (or navi2ch-browse-url-browser-function
browse-url-browser-function))
(new-window-flag (cond ((boundp 'browse-url-new-window-flag)
browse-url-new-window-flag)
((boundp 'browse-url-new-window-p)
browse-url-new-window-p)))
proc status)
(if (eq browse-url-browser-function 'navi2ch-browse-url)
(error "Set navi2ch-browse-url-browser-function correctly"))
;;sssp$B$r(Bhttp$B$K=q$-49$((B
(when (string= (substring url 0 4) "sssp")
(store-substring url 0 "http"))
;;$BL5BL$r>J$/$?$a%V%i%&%:$9$kA0$K%?!<%2%C%H$N>uBV3NG'$9$k!#(B
;;$B$A$g$C$H87$7$$$h$&$@$,!"(B302$B$@$HBgDq(B404$B$KHt$P$5$l$k$N$G!#(B
(when navi2ch-enable-status-check
(setq proc (navi2ch-net-send-request url "HEAD"))
(setq status (navi2ch-net-get-status proc))
(if (or (string= status "404")
(string= status "403")
(string= status "503")
(string= status "302"))
(error "$B%V%i%&%:$9$k$N$d$a$^$7$?(B return code %s" status)))
(cond ((and navi2ch-browse-url-image-program ; images
(file-name-extension url)
(member (downcase (file-name-extension url))
navi2ch-browse-url-image-extentions))
(navi2ch-browse-url-image url))
(t ; others
(setq args (or args (list new-window-flag)))
(apply 'browse-url url args)))))
(defun navi2ch-browse-url-image (url &optional new-window)
;; new-window ignored
"Ask the WWW browser defined by `browse-url-image-program' to load URL.
Default to the URL around or before point. A fresh copy of the
browser is started up in a new process with possible additional arguments
`navi2ch-browse-url-image-args'. This is appropriate for browsers which
don't offer a form of remote control."
(interactive (browse-url-interactive-arg "URL: "))
(if (not navi2ch-browse-url-image-program)
(error "No browser defined (`navi2ch-browse-url-image-program')"))
(apply 'start-process (concat navi2ch-browse-url-image-program url) nil
navi2ch-browse-url-image-program
(append navi2ch-browse-url-image-args (list url))))
;; from apel
(defsubst navi2ch-put-alist (item value alist)
"Modify ALIST to set VALUE to ITEM.
If there is a pair whose car is ITEM, replace its cdr by VALUE.
If there is not such pair, create new pair (ITEM . VALUE) and
return new alist whose car is the new pair and cdr is ALIST.
\[tomo's ELIS like function]"
(let ((pair (assoc item alist)))
(if pair
(progn
(setcdr pair value)
alist)
(cons (cons item value) alist))))
(defun navi2ch-next-property (point prop)
(setq point (next-single-property-change point prop))
(when (and point
(null (get-text-property point prop)))
(setq point (next-single-property-change point prop)))
point)
(defun navi2ch-previous-property (point prop)
(when (> point (point-min))
(when (eq (get-text-property point prop)
(get-text-property (1- point) prop))
(setq point (previous-single-property-change point prop)))
(when (and point
(null (get-text-property (1- point) prop)))
(setq point (previous-single-property-change point prop)))
(when point
(or (previous-single-property-change point prop) (point-min)))))
(defun navi2ch-set-minor-mode (mode name map)
(make-variable-buffer-local mode)
(unless (assq mode minor-mode-alist)
(setq minor-mode-alist
(cons (list mode name) minor-mode-alist)))
(unless (assq mode minor-mode-map-alist)
(setq minor-mode-map-alist
(cons (cons mode map) minor-mode-map-alist))))
(defsubst navi2ch-default-directory ()
(cond ((file-directory-p navi2ch-directory)
(file-name-as-directory navi2ch-directory))
((file-directory-p (expand-file-name "~/"))
(expand-file-name "~/"))
(t temporary-file-directory)))
(defun navi2ch-call-process-buffer (program &rest args)
"$B:#$N(B buffer $B$G(B PROGRAM $B$r8F$s$GJQ99$9$k!#(B"
(let ((default-directory (navi2ch-default-directory)))
(apply 'call-process-region (point-min) (point-max) program t t nil args)))
(defun navi2ch-alist-list-to-alist (list key1 &optional key2)
(mapcar
(lambda (x)
(cons (cdr (assq key1 x))
(if key2
(cdr (assq key2 x))
x)))
list))
(defun navi2ch-write-region (begin end filename)
(write-region begin end filename nil 'no-msg))
(defun navi2ch-get-major-mode (buffer)
(when (get-buffer buffer)
(with-current-buffer buffer
major-mode)))
(defun navi2ch-set-mode-line-identification ()
(let ((offline '(navi2ch-offline navi2ch-modeline-offline navi2ch-modeline-online))
(belogin '(navi2ch-be2ch-login-flag navi2ch-modeline-be2ch-login
navi2ch-modeline-be2ch-logout)))
(unless navi2ch-mode-line-identification
(setq navi2ch-mode-line-identification
(default-value 'mode-line-buffer-identification)))
(setq mode-line-buffer-identification
(list offline
belogin
'navi2ch-message-samba24-mode-string
'navi2ch-mode-line-identification)))
(force-mode-line-update t))
(defun navi2ch-end-of-buffer ()
"$B%P%C%U%!$N:G=*9T$K0\F0!#(B"
(interactive)
(call-interactively 'end-of-buffer)
(when (eobp) (forward-line -1)))
(defun navi2ch-uudecode-region (start end &optional filename)
"START $B$H(B END $B$N4V$N%j!<%8%g%s$r(B uudecode $B$9$k!#(B
FILENAME $B$,;XDj$5$l$k$H!"(BFILENAME $B$K$b=q$-=P$9!#(B"
(interactive "r")
(let* ((coding-system-for-read 'binary)
(coding-system-for-write 'binary)
(mode "600")
(file (expand-file-name
(or filename
(make-temp-name (navi2ch-temp-directory)))))
(default-directory (file-name-directory file))
(buf (current-buffer))
rc)
(unwind-protect
(progn
(with-temp-buffer
(insert-buffer-substring buf start end)
(goto-char (point-min))
(when (re-search-forward navi2ch-uuencode-begin-delimiter-regexp
nil t)
(setq mode (navi2ch-match-string-no-properties 1))
(forward-line)
(delete-region (point-min) (point)))
(insert (format "begin %s %s\n"
mode (file-name-nondirectory file)))
(goto-char (point-max))
(when (re-search-backward navi2ch-uuencode-end-delimiter-regexp
nil t)
(delete-region (match-beginning 0) (point-max)))
(insert "end\n")
(setq rc (apply 'call-process-region
(point-min) (point-max)
navi2ch-uudecode-program
nil nil nil
navi2ch-uudecode-args)))
(when (and (= rc 0)
(file-exists-p file))
(delete-region start end)
(insert-file-contents-literally file)
(when filename
(message "Wrote %s" filename))))
(ignore-errors (unless filename (delete-file file))))
(when (not (= rc 0))
(error "uudecode error"))))
(eval-and-compile
(defalias 'navi2ch-line-beginning-position
(if (fboundp 'point-at-bol)
#'point-at-bol
#'line-beginning-position))
(defalias 'navi2ch-line-end-position
(if (fboundp 'point-at-eol)
#'point-at-eol
#'line-end-position)))
(defun navi2ch-uudecode-write-region (start end &optional filename)
"START $B$H(B END $B$N4V$N%j!<%8%g%s$r(B uudecode $B$7!"(BFILENAME $B$K=q$-=P$9!#(B
$B%j!<%8%g%sFb$K(B `navi2ch-uuencode-begin-delimiter-regexp' $B$K%^%C%A$9$k9T$,$"$k(B
$B>l9g$O$=$l0JA0$rL5;k$7!"(B`navi2ch-uuencode-end-delimiter-regexp' $B$K%^%C%A$9$k9T(B
$B$,$"$k>l9g$O:G8e$N$=$l0J9_$rL5;k$9$k!#(B
$B$5$i$K!"(Buuencode $B$N%U%)!<%^%C%H$K=>$C$F$$$J$$9T$bL5;k$9$k!#(B"
(interactive "r")
(let ((buf (current-buffer))
(default-filename nil))
(save-excursion
(goto-char start)
(when (re-search-forward navi2ch-uuencode-begin-delimiter-regexp end t)
(setq start (match-beginning 0)
default-filename (match-string 2)))
(goto-char end)
(when (re-search-backward navi2ch-uuencode-end-delimiter-regexp start t)
;; exclude "end"
(setq end (match-beginning 0))))
(unless filename
(setq filename (expand-file-name
(read-file-name
(if default-filename
(format "Uudecode to file (default `%s'): "
default-filename)
"Uudecode to file: ")
nil default-filename))))
(when (file-directory-p filename)
(if default-filename
(setq filename (expand-file-name default-filename filename))
(error "%s is a directory" filename)))
(when (or (not (file-exists-p filename))
(y-or-n-p (format "File `%s' exists; overwrite? "
filename)))
(with-temp-buffer
(insert-buffer-substring buf start end)
(goto-char (point-min))
(while (search-forward "$B!)(B" nil t) ;for 2ch
(replace-match "&#" nil t))
(goto-char (point-min))
(forward-line)
(while (not (eobp))
(let* ((char (char-after))
(len (- (navi2ch-line-beginning-position 2) (point))))
(when (char-equal char ?`)
(setq char ? ))
(if (and (looking-at navi2ch-uuencode-line-regexp)
(< len 63)
(= len (- (* (/ char 3) 4) 38)))
(forward-line)
(delete-region (point) (navi2ch-line-beginning-position 2)))))
(insert "end\n")
(navi2ch-uudecode-region (point-min) (point-max) filename)))))
(defun navi2ch-base64-write-region (start end &optional filename)
"START $B$H(B END $B$N4V$N%j!<%8%g%s$r(B base64 $B%G%3!<%I$7!"(BFILENAME $B$K=q$-=P$9!#(B
$B%j!<%8%g%sFb$K(B `navi2ch-base64-begin-delimiter-regexp' $B$+(B
`navi2ch-base64-susv3-begin-delimiter-regexp' $B$K%^%C%A$9$k9T$,$"$k>l9g$O(B
$B$=$l0JA0$rL5;k$7!"(B
`navi2ch-base64-end-delimiter-regexp' $B$+(B
`navi2ch-base64-susv3-end-delimiter-regexp' $B$K%^%C%A$9$k9T(B
$B$,$"$k>l9g$O:G8e$N$=$l0J9_$rL5;k$9$k!#(B
$B$5$i$K!"(B`navi2ch-base64-line-regexp' $B$K%^%C%A$7$J$$9T$bL5;k$9$k!#(B
base64$B%G%3!<%I$9$Y$-FbMF$,$J$$>l9g$O%(%i!<$K$J$k!#(B"
(interactive "r")
(save-excursion
(let ((buf (current-buffer))
(default-filename nil)
(mode nil)
(susv3 nil))
;; insert$B$7$?8e$K:o$k$N$OL5BL$J$N$G$"$i$+$8$a9J$j9~$s$G$*$/(B
(goto-char start)
(cond
((re-search-forward navi2ch-base64-begin-delimiter-regexp end t)
(setq default-filename (match-string 2))
(goto-char (match-end 0)))
((re-search-forward navi2ch-base64-susv3-begin-delimiter-regexp end t)
(setq default-filename (match-string 2)
mode (string-to-number (match-string 1) 8)
susv3 t)
(goto-char (match-end 0))))
(if (re-search-forward navi2ch-base64-line-regexp end t)
(setq start (match-beginning 0))
(error "No base64 data"))
(goto-char end)
(if (or (and susv3 (re-search-backward
navi2ch-base64-susv3-end-delimiter-regexp start t))
(re-search-backward navi2ch-base64-end-delimiter-regexp start t))
(goto-char (match-beginning 0)))
(if (re-search-backward navi2ch-base64-line-regexp start t)
(setq end (match-end 0)))
(unless filename
(setq filename (expand-file-name
(read-file-name
(if default-filename
(format "Base64-decode to file (default `%s'): "
default-filename)
"Base64-decode to file: ")
nil default-filename))))
(when (file-directory-p filename)
(if default-filename
(setq filename (expand-file-name default-filename filename))
(error "%s is a directory" filename)))
(when (or (not (file-exists-p filename))
(y-or-n-p (format "File `%s' exists; overwrite? "
filename)))
(with-temp-buffer
(let ((buffer-file-coding-system 'binary)
(coding-system-for-write 'binary)
;; auto-compress-mode$B$r(Bdisable$B$K$9$k(B
(inhibit-file-name-operation 'write-region)
(inhibit-file-name-handlers (cons 'jka-compr-handler
inhibit-file-name-handlers)))
(insert-buffer-substring buf start end)
(goto-char (point-min))
(while (not (eobp))
(if (looking-at navi2ch-base64-line-regexp)
(forward-line)
(delete-region (point) (navi2ch-line-beginning-position 2))))
(base64-decode-region (point-min) (point-max))
(write-region (point-min) (point-max) filename)
(if (and susv3 mode)
(condition-case nil
;; 511 = (string-to-number "0777" 8)
(set-file-modes filename (logand mode 511))
(error nil)))))))))
(defun navi2ch-base64-insert-file (filename)
"FILENAME $B$r(B base64 $B%(%s%3!<%I$7!"8=:_$N%]%$%s%H$KA^F~$9$k!#(B"
(interactive "fEncode and insert file: ")
(save-excursion
(let ((str nil))
(with-temp-buffer
(let ((buffer-file-coding-system 'binary))
(insert-file-contents-literally filename)
(base64-encode-region (point-min) (point-max))
(goto-char (point-min))
(while (search-forward "\n" nil t)
(replace-match ""))
(goto-char (point-min))
(insert (format "%s(%s)\n" navi2ch-base64-begin-delimiter
(file-name-nondirectory filename)))
(while (= (move-to-column navi2ch-base64-fill-column)
navi2ch-base64-fill-column)
(insert "\n"))
(goto-char (point-max))
(insert (format "\n%s\n" navi2ch-base64-end-delimiter))
(setq str (buffer-string))))
(insert str))))
(defun navi2ch-url-to-host (url)
(when url
(cond
((string-match "^http://\\([^/]+\\)" url)
(match-string 1 url))
((string-match "^x-localbbs://" url)
"localhost"))))
(defun navi2ch-read-string (prompt &optional initial-input history)
(let ((minibuffer-allow-text-properties nil))
(read-string prompt initial-input history)))
(defun navi2ch-temp-directory ()
(let ((dir (expand-file-name "tmp" navi2ch-directory)))
(or (file-directory-p dir)
(make-directory dir))
dir))
(defun navi2ch-strip-properties (obj)
"OBJ $BCf$NJ8;zNs$r:F5"E*$KC5$7!"%F%-%9%HB0@-$r30$7$?%*%V%8%'%/%H$rJV$9!#(B
$B85$N(B OBJ $B$OJQ99$7$J$$!#(B"
(cond
((consp obj)
(let* ((ret (cons (car obj) (cdr obj)))
(seq ret))
;; $BD9$$%j%9%H$r%3%T!<$9$k:]$K%9%?%C%/%*!<%P!<%U%m!<$K$J$k$N$G(B
;; $B:F5"$r%k!<%W$KE83+!#(B
(while (consp seq)
(setcar seq (navi2ch-strip-properties (car seq)))
(if (consp (cdr seq))
(setcdr seq (cons (cadr seq) (cddr seq)))
(setcdr seq (navi2ch-strip-properties (cdr seq))))
(setq seq (cdr seq)))
ret))
((stringp obj)
(let ((str (copy-sequence obj)))
(set-text-properties 0 (length str) nil str)
str))
((vectorp obj)
(vconcat (mapcar 'navi2ch-strip-properties obj)))
(t obj)))
(defun navi2ch-update-html-tag-regexp ()
"`navi2ch-replace-html-tag-regexp-internal' $B$r99?7$9$k!#(B"
(setq navi2ch-replace-html-tag-regexp-internal
(navi2ch-regexp-alist-to-internal
(nconc (mapcar (lambda (x)
(cons (regexp-quote (car x))
(cdr x)))
navi2ch-replace-html-tag-alist)
navi2ch-replace-html-tag-regexp-alist))))
(defun navi2ch-add-replace-html-tag (tag value)
"TAG $B$rI=<($9$k:]$K(B VALUE $B$GCV$-49$($k!#(B"
(let ((as-regexp (condition-case nil
(progn
;; $BJ8;zNs$K$h$C$F$O(B regexp-opt-group() $B$,L58B(B
;; $B:F5"$K$J$k(B
(regexp-opt (list "$B$"(B" tag))
nil)
(error t))))
(if as-regexp
(navi2ch-add-replace-html-tag-regexp (regexp-quote tag) value)
(add-to-list 'navi2ch-replace-html-tag-alist
(cons tag value))
(navi2ch-update-html-tag-regexp))))
(defun navi2ch-add-replace-html-tag-regexp (regexp value)
"REGEXP $B$K%^%C%A$9$k(B tag $B$rI=<($9$k:]$K(B VALUE $B$GCV$-49$($k!#(B"
(add-to-list 'navi2ch-replace-html-tag-regexp-alist
(cons regexp value))
(navi2ch-update-html-tag-regexp))
(defsubst navi2ch-filename-to-url (filename)
(concat "file://" (expand-file-name filename)))
(defun navi2ch-chop-/ (dirname)
(save-match-data
(if (string-match "/\\'" dirname)
(replace-match "" nil t dirname)
dirname)))
(defsubst navi2ch-rename-file (file newname &optional ok-if-already-exists)
(rename-file (navi2ch-chop-/ file)
(navi2ch-chop-/ newname) ok-if-already-exists))
(eval-and-compile
(defalias 'navi2ch-set-keymap-default-binding
(if (fboundp 'set-keymap-default-binding)
#'set-keymap-default-binding
(lambda (map command)
"$B%-!<%^%C%W$N%G%U%)%k%H%P%$%s%I$r@_Dj$9$k!#(B"