File: tc-bushu.el

package info (click to toggle)
t-code 1%3A2.0beta9-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,632 kB
  • ctags: 507
  • sloc: lisp: 7,176; sh: 488; ansic: 440; perl: 270; makefile: 116
file content (452 lines) | stat: -rw-r--r-- 14,876 bytes parent folder | download
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
;;; tc-bushu.el --- bushu henkan on T-Code

;; Copyright (C) 1996, 97 Kaoru Maeda, Yasushi Saito and Akira Kitajima.

;; Author: Kaoru Maeda <maeda@src.ricoh.co.jp>
;;	Yasushi Saito <yasushi@is.s.u-tokyo.ac.jp>
;;	Akira Kitajima <kitajima@ics.es.osaka-u.ac.jp>
;; Maintainer: Akira Kitajima
;; Created: 10 Dec 1996
;; Version: $Id: tc-bushu.el,v 2.0.5.0 1998/08/22 06:56:40 kitajima Exp $
;; Keywords: wp

;; 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.

;;; Code:

(require 'tc)

(defconst tcode-bushu-reverse-buffer-name " *bushu-reverse-dictionary*")

(defvar tcode-bushu-reverse-dictionary-name
  (concat tcode-data-directory "bushu.rev")
  "* հ󼭽Υѥ̾")


(defun tcode-bushu-init (level)
  "ѴνԤʤ
LEVELѿtcode-bushu-on-demand꾮äԤʤʤ"
  (interactive (list 999))
  (or (get-buffer tcode-bushu-buffer-name)
      (< level tcode-bushu-on-demand)
      (progn
	(tcode-read-bushu-dictionary)
	(run-hooks 'tcode-bushu-ready-hook))))

(defun tcode-bushu-search (str)
  "ߤΥХåեΡ STR ǻϤޤԤΤǽΤΤ򸫤Ĥ롣"
  (let ((min (point-min))
	(max (point-max))
	kouho)
    (or (catch 'found
	  (and (eobp)
	       (forward-line -1))
	  (while (< min max)
	    (cond ((string< (setq kouho
				  (buffer-substring (progn
						      (beginning-of-line)
						      (point))
						    (save-excursion
						      (end-of-line)
						      (point))))
			    str)
		   (forward-line 1)
		   (goto-char (/ (+ (setq min (point)) max) 2)))
		  ((string< str kouho)
		   (goto-char (/ (+ min (setq max (point))) 2)))
		  ((throw 'found t)))))
	(looking-at str))))

(defun tcode-read-bushu-dictionary (&optional force)
  "Ѵɤ߹"
  (interactive "P")
  (save-excursion
    (let ((dic-buf (set-buffer (get-buffer-create tcode-bushu-buffer-name)))
	  (rev-buf (get-buffer-create tcode-bushu-reverse-buffer-name)))
      (or (and (null force)
	       (> (buffer-size) 0))
	  (progn
	    (erase-buffer)
	    (message "󼭽ɤ߹...")
	    (insert-file-contents tcode-bushu-dictionary-name)

	    (if (file-newer-than-file-p tcode-bushu-dictionary-name
					tcode-bushu-reverse-dictionary-name)
		;; հ
		(let ((count 0)
		      character
		      component)
		  (while (not (eobp))
		    (message "հ(%d)..." count)
		    (setq count (1+ count)
			  component (buffer-substring
				     (point)
				     (progn (tcode-forward-char 2) (point)))
			  character (buffer-substring
				     (point)
				     (progn (tcode-forward-char 1) (point))))
		    (save-excursion
		      (set-buffer rev-buf)
		      (let ((entry (concat character component)))
			(or (tcode-bushu-search entry)
			    (insert entry ?\n))))
		    (forward-line 1))
		  (save-excursion
		    (set-buffer rev-buf)
		    (write-region (point-min) (point-max)
				  (expand-file-name
				   tcode-bushu-reverse-dictionary-name)))
		  (message "հ(%d)...λ" count))
	      (set-buffer rev-buf)
	      (erase-buffer)
	      (insert-file-contents tcode-bushu-reverse-dictionary-name))
	    (message "󼭽ɤ߹...λ"))))))


(defun tcode-bushu-lookup (str1 str2)
  (or (cdr (assoc str2 (get (intern-soft str1 tcode-stroke-table) 'bushu)))
      (save-excursion
	(set-buffer (get-buffer tcode-bushu-buffer-name))
	(let ((str (concat str1 str2)))
	  (tcode-bushu-search str)
	  (and (looking-at (concat "^" str "\\(.\\)"))
	       (let ((composed (buffer-substring (match-beginning 1)
						 (match-end 1))))
		 (put (intern str1 tcode-stroke-table)
		      'bushu
		      (cons (cons str2 composed)
			    (get (intern-soft str1 tcode-stroke-table)
				 'bushu)))
		 composed))))))

(defun tcode-bushu-reverse-lookup (str)
  (or (get (intern-soft str tcode-stroke-table) 'composition)
      (save-excursion
	(set-buffer (get-buffer tcode-bushu-reverse-buffer-name))
	(tcode-bushu-search str)
	(and (looking-at (concat "^" str "\\(.\\)\\(.\\)"))
	     (let ((elm (cons (buffer-substring (match-beginning 1)
						(match-end 1))
			      (buffer-substring (match-beginning 2)
						(match-end 2)))))
	       (put (intern str tcode-stroke-table) 'composition elm))))))

(defun tcode-start-bushu ()
  "ѴϤ"
  (tcode-bushu-init 2)
  (or (get-buffer tcode-bushu-buffer-name)
      (error "Bushu dictionary not ready."))
  (setq tcode-bushu-nest (1+ tcode-bushu-nest))
  (insert ""))

(defun tcode-end-bushu ()
  "¦Ѵλ"
  (setq tcode-bushu-nest (1- tcode-bushu-nest))
  (let ((line-beginning
	 (save-excursion (beginning-of-line)
			 (point))))
    (save-excursion
      (if (search-backward "" line-beginning t)
	  (delete-char 1)))))

(defun tcode-find-another-first-char (kanji another-kanji)
  "1ܤ KANJI ǤꡢĹ夬ľϲǽʻ롣"
  (save-excursion
    (set-buffer (get-buffer tcode-bushu-buffer-name))
    (tcode-bushu-search kanji)
    (catch 'found
      (let ((re (concat "^" kanji "\\(.\\)\\(.\\)"))
	    this-kanji)
	(while (looking-at re)
	  (setq this-kanji (buffer-substring (match-beginning 2)
					     (match-end 2)))
	  (and (tcode-stroke-for-char this-kanji)
	       (not (tcode-lookup-outset-sub this-kanji another-kanji))
	       (throw 'found this-kanji))
	  (forward-line 1))))))

(defun tcode-find-another-second-char (kanji another-kanji)
  "2ܤ KANJI ǤꡢĹ夬ľϲǽʻ롣"
  (save-excursion
    (set-buffer (get-buffer tcode-bushu-buffer-name))
    (goto-char (point-min))
    (catch 'found
      (let ((re (concat "^\\(.\\)" kanji "\\(.\\)"))
	    this-kanji)
	(while (re-search-forward re nil t)
	  (setq this-kanji (buffer-substring (match-beginning 2)
					     (match-end 2)))
	  (and (tcode-stroke-for-char this-kanji)
	       (not (tcode-lookup-outset-sub this-kanji another-kanji))
	       (throw 'found this-kanji)))))))

(defun tcode-decompose-char-indirect (kanji)
  "KANJI2Ĥʬ򤹤롣KANJI ľʬǤʤȤ롣
֤ͤ2Ĥλ cons 롣
ʬǤʤˤnil֤"
  (and (not (string= kanji 'N))
       (save-excursion
	 (set-buffer (get-buffer tcode-bushu-buffer-name))
	 (goto-char (point-min))
	 (let (1st 2nd 3rd can-direct-1st can-direct-2nd can-direct-3rd alist
		   (re (concat "^.?" kanji)))
	   (or (catch 'found
		 (while (re-search-forward re nil t)
		   (beginning-of-line)
		   (looking-at "^\\(.\\)\\(.\\)\\(.\\)")
		   (forward-line 1)
		   (setq 1st (buffer-substring (match-beginning 1)
					       (match-end 1))
			 2nd (buffer-substring (match-beginning 2)
					       (match-end 2))
			 3rd (buffer-substring (match-beginning 3)
					       (match-end 3))
			 can-direct-1st (tcode-stroke-for-char 1st)
			 can-direct-2nd (tcode-stroke-for-char 2nd)
			 can-direct-3rd (tcode-stroke-for-char 3rd))
		   (cond ((string= 1st kanji)
			  ;; ܤ KANJI
			  (and (not (string= 2nd kanji))
			       (not (string= 3rd kanji))
			       (not (tcode-lookup-outset-sub 2nd 3rd))
			       (cond ((and can-direct-2nd can-direct-3rd)
				      ;; ξľϤǤ
				      (throw 'found (cons 2nd 3rd)))
				     ((or (null alist)
					  can-direct-2nd can-direct-3rd)
				      (setq alist (cons 2nd 3rd))))))
			 ((string= 2nd kanji)
			  ;; ܤ KANJI
			  (and (not (string= 3rd kanji))
			       (not (tcode-lookup-outset-sub 1st 3rd))
			       (cond ((and can-direct-1st can-direct-3rd)
				      ;; ξľϤǤ
				      (throw 'found (cons 1st 3rd)))
				     ((or (null alist)
					  can-direct-1st can-direct-3rd)
				      (setq alist (cons 1st 3rd)))))))))
	       alist)))))

(defun tcode-decompose-char (kanji &optional for-help)
  "KANJI2Ĥʬ򤹤롣
֤ͤ2Ĥλ cons 롣
ʬǤʤˤnil֤"
  (tcode-bushu-init 2)
  (let ((sym (tcode-bushu-reverse-lookup kanji)))
    (if sym
	(if (and for-help
		 tcode-strict-help)
	    (let ((kanji1 (car sym))
		  (kanji2 (cdr sym))
		  ch)
	      (cond ((or (tcode-stroke-for-char kanji) ; ľϤǤ
			 (and (tcode-stroke-for-char kanji1)
			      (tcode-stroke-for-char kanji2))
			 (and (tcode-stroke-for-char
			       (setq kanji1
				     (tcode-outset-alternative-reverse
				      kanji1)))
			      (tcode-stroke-for-char
			       (setq kanji2
				     (tcode-outset-alternative-reverse
				      kanji2))))))
		    ;; ʸܤľϤǤʸܤǤʤ
		    ((and (tcode-stroke-for-char kanji1)
			  (not (tcode-stroke-for-char kanji2))
			  (cond ((string= kanji2 'N))
				((setq ch
				       (tcode-find-another-second-char
					kanji2 kanji1))
				 (setq kanji2 ch))
				((setq ch
				       (tcode-find-another-first-char
					kanji2 kanji1))
				 (setq kanji2 ch)))))
		    ;; ʸܤľϤǤưʸܤǤʤ
		    ((and (not (tcode-stroke-for-char kanji1))
			  (tcode-stroke-for-char kanji2)
			  (cond ((string= kanji1 'N))
				((setq ch
				       (tcode-find-another-first-char
					kanji1 kanji2))
				 (setq kanji1 ch))
				((setq ch
				       (tcode-find-another-second-char
					kanji1 kanji2))
				 (setq kanji1 ch)))))
		    ;; ξľϤǤʤ
		    ((or (let ((alist
				(tcode-decompose-char-indirect kanji)))
			   (and alist
				(setq kanji1 (car alist)
				      kanji2 (cdr alist)))))))
	      (cons kanji1 kanji2))
	  sym)
      (and for-help
	   tcode-strict-help
	   (tcode-decompose-char-indirect kanji)))))

(defun tcode-lookup-outset-sub (tchar-1 tchar-2)
  "TCHAR-1  TCHAR-2 ľȤ߹碌ƤǤ볰ܤ
Ĥʤäˤ nil ֤"
  (and tchar-1
       tchar-2
       (or (tcode-bushu-lookup tchar-1 tchar-2)
	   (tcode-bushu-lookup tchar-2 tchar-1))))

(defun tcode-outset-alternative-reverse (char)
  "EQCHAR ʸ CHAR ֤äˤʤ EQCHAR Ȥ֤
EQCHAR  CHAR ǤȤ NL + EQCHAR = CHAR Ƥ
ȤǤ롣"
  (save-excursion
    (set-buffer (get-buffer tcode-bushu-buffer-name))
    (tcode-bushu-search "N")
    (let (eqchar this-char)
      (or (catch 'found
	    (while (looking-at "^N\\(.\\)\\(.\\)")
	      (setq eqchar (buffer-substring
			    (match-beginning 1)
			    (match-end 1))
		    this-char (buffer-substring
			       (match-beginning 2)
			       (match-end 2)))
	      (and (string= this-char char)
		   (throw 'found eqchar))
	      (forward-line 1)))
	  char))))

(defun tcode-outset-alternative (char)
  "CHAR ʸ EQCHAR ֤äˤʤ CHAR Ȥ֤
CHAR ǤȤ NL + EQCHAR = CHAR Ƥ뤳ȤǤ롣"
  (let ((eqchar (tcode-decompose-char char)))
    (if (and eqchar (string= (car eqchar) 'N))
	(cdr eqchar)
      char)))

(defun tcode-lookup-outset (char-1 char-2)
  "CHAR-1  CHAR-2 Ȥ߹碌ƤǤ볰ܤ
Ĥʤäˤ nil ֤"
  (and char-1
       char-2
       (or (tcode-lookup-outset-sub (setq char-1 (tcode-2-to-1 char-1))
				    (setq char-2 (tcode-2-to-1 char-2)))
	   (let ((alt-char-1 (tcode-outset-alternative char-1))
		 (alt-char-2 (tcode-outset-alternative char-2)))
	     (prog1
		 (and (or (not (string= char-1 alt-char-1))
			  (not (string= char-2 alt-char-2)))
		      (tcode-lookup-outset-sub alt-char-1 alt-char-2))
	       (setq char-1 alt-char-1
		     char-2 alt-char-2)))
	   (let* ((decomposed-1 (tcode-decompose-char char-1))
		  (decomposed-2 (tcode-decompose-char char-2))
		  (tc11 (car decomposed-1))
		  (tc12 (cdr decomposed-1))
		  (tc21 (car decomposed-2))
		  (tc22 (cdr decomposed-2)))
	     (and (string= tc11 'N) (setq tc11 nil))
	     (and (string= tc12 'N) (setq tc12 nil))
	     (and (string= tc21 'N) (setq tc21 nil))
	     (and (string= tc22 'N) (setq tc22 nil))
	     (or (and (string= tc11 char-2)
		      (not (string= tc12 char-1))
		      (not (string= tc12 char-2))
		      tc12)		; subtraction
		 (and (string= tc12 char-2)
		      (not (string= tc11 char-1))
		      (not (string= tc11 char-2))
		      tc11)
		 (and (string= tc21 char-1)
		      (not (string= tc22 char-1))
		      (not (string= tc22 char-2))
		      tc22)
		 (and (string= tc22 char-1)
		      (not (string= tc21 char-1))
		      (not (string= tc21 char-2))
		      tc21)
		 (let ((loop '((char-1 . tc22) (char-2 . tc11)
			       ;; swaped by KITAJIMA     ^^^^
			       (char-1 . tc21) (char-2 . tc12)
			       (tc12 . tc22) (tc21 . tc12)
			       (tc11 . tc22) (tc21 . tc11))))
		   (catch 'found
		     (while loop
		       (let* ((i (car loop))
			      (i1 (eval (car i)))
			      (i2 (eval (cdr i)))
			      (res (and i1 i2
					(tcode-lookup-outset-sub i1 i2))))
			 (or (null res)
			     (string= res char-1)
			     (string= res char-2)
			     (throw 'found res)))
		       (setq loop (cdr loop)))))
		 ;; new subtraction
		 (and tc11 (string= tc11 tc21)
		      (not (string= tc12 char-1))
		      (not (string= tc12 char-2))
		      tc12)
		 (and tc11 (string= tc11 tc22)
		      (not (string= tc12 char-1))
		      (not (string= tc12 char-2))
		      tc12)
		 (and tc12 (string= tc12 tc21)
		      (not (string= tc11 char-1))
		      (not (string= tc11 char-2))
		      tc11)
		 (and tc12 (string= tc12 tc22)
		      (not (string= tc11 char-1))
		      (not (string= tc11 char-2))
		      tc11))))))

(defun tcode-compose-chars ()
  "ݥȤʸ"
  (interactive "*")
  (tcode-bushu-init 3)
  (let* ((p (point))
	 (prev-char (tcode-get-prev-nonspace))
	 (prev-prev-char (tcode-get-prev-nonspace))
	 (kanji (tcode-lookup-outset prev-prev-char prev-char))
	 (p2 (point)))
    (goto-char p)
    (if kanji
	(progn
	  (setq tcode-bushu-occurrence (1+ tcode-bushu-occurrence))
	  (delete-region p2 p)
	  (insert kanji)
	  (and tcode-auto-help
	       (tcode-display-direct-stroke kanji)
	       (tcode-auto-remove-help-char))
	  (setq tcode-help-char kanji))
      (beep)
      (message ""))))

(defun tcode-bushu-henkan ()
  "Ѵ򳫻Ϥ롣"
  (interactive)
  (if tcode-use-postfix-bushu-as-default
      (tcode-compose-chars)
    (tcode-start-bushu)))

(defun tcode-bushu-another-henkan ()
  "`tcode-use-postfix-bushu-as-default' ȤϵդѴ򳫻Ϥ롣"
  (interactive)
  (if tcode-use-postfix-bushu-as-default
      (tcode-start-bushu)
    (tcode-compose-chars)))

(provide 'tc-bushu)

;;; tc-bushu.el ends here