-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patheelll.el
1301 lines (1204 loc) · 41.8 KB
/
eelll.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
;;; eelll.el --- EELLL (ELisp implemented CATTT)
;; Copyright (C) 1991--2002 Kaoru Maeda, Yasushi Saito and Akira Kitajima
;; Author: Kaoru Maeda <[email protected]>
;; Yasushi Saito <[email protected]>
;; Akira Kitajima <[email protected]>
;; YAGI Tatsuya <[email protected]>
;; Maintainer: Akira Kitajima
;; $Id: eelll.el,v 1.29 2003/05/18 08:37:08 kitajima Exp $
;; 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)
(require 'tc-bushu)
(require 'tc-help)
;; Version
(defun eelll-version ()
"EELLL のバージョンを表示する。"
(interactive)
(if (called-interactively-p 'interactive)
(message "EELLL version %s" (eelll-version))
(substring "$Revision: 1.29 $" 11 -2)))
;;;
;;; Variable
;;;
(defgroup eelll nil
"EELLL (A T-Code trainer)."
:group 'tcode)
(defcustom eelll-expert nil
"Tコードの上級者かどうか。"
:type 'boolean :group 'eelll)
(defcustom eelll-display-help-threshold 10
"ヘルプの表示/非表示を切り替えるエラー率のしきい値(%)。"
:type 'integer :group 'eelll)
(defvar eelll-text "EELLLTXT"
"EELLLの練習テキストファイル")
(defvar eelll-move-cursor nil
"*non-nilにするとキー入力のたびにカーソルを進める。")
(defvar eelll-configuration-file-name (concat tcode-data-directory
"eelll-conf.el")
"*EELLLの設定ファイル名。
このファイルは自動的に書き換えられるので、
ユーザーは内容を変更してはいけない。")
(defvar eelll-last-lesson nil
"最後に練習したレッスン番号。")
(defvar eelll-last-lesson-alist nil
"テキストのファイル名と最後に練習したレッスン番号のalist。")
(defconst eelll-buffer-name "*EELLL*"
"EELLLで練習するバッファ名")
(defconst eelll-text-buffer " *eelll: text*"
"EELLLの練習テキストを入れておくバッファ名")
(defconst eelll-current-text-buffer " *eelll: current text*"
"EELLLで練習中のテキストを入れておくバッファ名")
(defvar eelll-help-remove-interval 25
"*EELLLの説明を消すまでの時間(秒)")
(defconst eelll-help-buffer-name " *EELLL-Help*"
"EELLLで打ち方を表示するバッファ名")
(defvar eelll-help-window-margin
(if (and (>= emacs-major-version 21)
window-system)
3
1)
"*EEELLの打ち方を表示するときのウィンドウの高さのマージン")
(defvar eelll-by-text-dummy-char "・"
"*直接入力できない文字の代わりに入力させる文字列")
(defvar eelll-by-text-max-line 30)
(defvar eelll-by-text-fill-column 60)
(when (and (fboundp 'defface)
window-system)
(defface eelll
(if (featurep 'meadow)
'((t (:font "default"
:foreground "black"
:background "white")))
'((t (:font "-*-fixed-medium-r-normal-*-16-*-*-*-*-*-*-*"
:foreground "black"
:background "white"))))
"*一行ヘルプを使った EELLL でのヘルプのフェイス"
:group 'eelll))
(defvar eelll-previous-error-rate 0)
(defvar eelll-original-window-configuration nil)
(defvar eelll-window-configuration nil)
(defvar eelll-total-time 0)
(defvar eelll-total-stroke 0)
(defvar eelll-total-error 0)
(defvar eelll-lesson-no nil)
(defvar eelll-first-hand nil)
(defvar eelll-second-hand nil)
(defvar eelll-prefix-stroke nil)
(defvar eelll-upper-row nil)
(defvar eelll-lesson-chars nil)
(defvar eelll-text-line nil)
(defvar eelll-start-time nil)
(defvar eelll-strokes 0)
(defvar eelll-error-strokes 0)
(defvar eelll-lesson-string nil)
(defvar eelll-lesson-pattern-string nil)
(defvar eelll-key-table nil)
(defvar eelll-mode-hook nil
"*EELLLを実行するときに実行されるフック。")
(defvar eelll-exit-hook (if (featurep 'frame)
'eelll-remove-frame))
;;; for `eelll-other-frame'
(defvar eelll-frame-parameters
(if (featurep 'meadow)
'((name . "EELLL")
(font . "default"))
'((name . "EELLL")
(font . "-*-fixed-medium-r-normal-*-16-*-*-*-*-*-*-*")))
"*`eelll-other-frame'で用いるフレームのパラメータ。")
(defvar eelll-bushu-stroke-alist
'((tutcode 20 28 20)
(tcode 26 23))
"部首合成を表すストローク")
(defvar eelll-use-column-for-help
(or (not window-system)
(> 22 emacs-major-version))
"一行ヘルプを表示する時に桁揃えをカラム単位で行うか否か。
この変数の値が non-nil で emacs22 を window-system で使っている場合に
ピクセル単位の計算をする。")
(defvar eelll-frame nil)
(defvar eelll-comment-line nil)
(defvar eelll-current-text nil)
(defvar eelll-random-mode nil)
(defvar eelll-random-max-line 4
"*EELLLのランダムモードで1回に選択される行数の最大値。")
;;;
;;; Text
;;;
(defun eelll-random-text (max-line)
"現在のバッファの行をランダムに並べ替える。
最大MAX-LINEまで残す。"
(let ((lines (count-lines (point-min) (point-max)))
line-list)
(goto-char (point-min))
(while (search-forward-regexp "^;" nil t)
(end-of-line)
(insert "\r")
(delete-char 1))
(goto-char (point-min))
(while (and (> max-line 0)
(> lines 0))
(goto-line (1+ (random lines)))
(if (eolp)
(progn
(unless (eobp)
(delete-char 1))
(setq lines (1- lines)))
(let ((point-eol (save-excursion (end-of-line) (point))))
(setq line-list (cons (buffer-substring (point) point-eol) line-list)
max-line (1- max-line)
lines (1- lines))
(delete-region (point) (1+ point-eol)))))
(erase-buffer)
(insert (mapconcat 'identity line-list "\n") "\n")
(goto-char (point-min))
(while (search-forward "\r" nil t)
(delete-char -1)
(insert "\n"))
))
(defun eelll-prepare-text (num)
"練習テキストNUMを用意する。NUMがnilならば次のレッスンを用意する。"
(save-excursion
(tcode-set-work-buffer eelll-text-buffer eelll-text)
(widen)
(goto-char (point-min))
(or (if num
(re-search-forward
(concat "\f\nLesson \\(" (int-to-string num) "\\):") nil t)
(and (or (null eelll-last-lesson)
(re-search-forward (concat "\f\nLesson \\("
(int-to-string eelll-last-lesson)
"\\):")
nil t))
(re-search-forward "\f\nLesson \\([0-9]+\\):" nil t)))
(error "練習テキスト%sはありません。" (if num (int-to-string num) "")))
(setq eelll-lesson-string (buffer-substring (match-beginning 1)
(match-end 1))
eelll-lesson-no (string-to-number eelll-lesson-string))
(setq eelll-prefix-stroke
(let ((l tcode-special-prefix-alist) ret)
(while l
(if (not (looking-at (concat (regexp-quote (nth 2 (car l)))
"\\([RrLl]\\)\\([RrLl]\\)"
"\\(!?\\)$")))
(setq l (cdr l))
(goto-char (match-beginning 1))
(setq ret (car (car l))
l nil)
))
ret))
(setq eelll-first-hand (looking-at "[Rr]"))
(setq eelll-second-hand (looking-at ".[Rr]"))
(setq eelll-upper-row (looking-at "..!"))
(forward-line 1)
(if (looking-at "^Lesson-chars: ")
(let ((eol (save-excursion (end-of-line 1) (point))))
(skip-chars-forward "^ " eol)
(setq eelll-lesson-chars (buffer-substring (1+ (point)) eol))
(forward-line 1)))
(let ((text (buffer-substring (point)
(save-excursion
(forward-page 1)
(1- (point))))))
(set-buffer (get-buffer-create eelll-current-text-buffer))
(erase-buffer)
(insert text)
(eelll-decompose-chars)
(if eelll-random-mode
(eelll-random-text eelll-random-max-line))
(goto-char (point-min))))
(setq eelll-last-lesson eelll-lesson-no))
(defun eelll-decompose-chars ()
(goto-char (point-min))
(let (c stroke cell (str "▲"))
(while (not (eobp))
(if (looking-at "^;\\|$")
(forward-line)
(setq c (tcode-following-char)
stroke (or (eelll-tcode-encode c)
(eelll-tcode-another-stroke (char-to-string c))))
(if stroke
(forward-char)
(delete-char 1)
(setq cell (or (tcode-bushu-help-lookup (char-to-string c))
(tcode-decompose-char (char-to-string c) t)))
(if (not (and (consp cell)
(stringp (car cell))
(stringp (cdr cell))))
(insert eelll-by-text-dummy-char)
(if tcode-use-postfix-bushu-as-default
(insert (car cell) (cdr cell) str)
(insert str (car cell) (cdr cell)))))))))
(defun eelll-lesson-line ()
"練習テキストの次の行をとってくる。終わりならnilを返す。
eelll-text-line: 印字イメージ"
(with-current-buffer eelll-current-text-buffer
(skip-chars-forward " \t\n\f" (point-max))
(let ((p (point)))
(while (looking-at "^;")
(forward-line 1))
(setq eelll-comment-line (buffer-substring p (point))))
(and (not (eobp))
(let ((p (point)))
(forward-line 1)
(setq eelll-text-line (buffer-substring p (1- (point))))
eelll-text-line))))
(defun eelll-tcode-encode (ch)
(if (eq ch (tcode-string-to-char "▲"))
(or (cdr (assq tcode-input-method eelll-bushu-stroke-alist))
'(26 23))
(tcode-encode ch)))
(defun eelll-tcode-another-stroke (ch)
(let ((i (1- (length tcode-another-table))))
(catch 'found
(while (>= i 0)
(let ((c (aref tcode-another-table i)))
(if (string= ch (if (and c (symbolp c))
(eval c)
c))
(throw 'found i)))
(setq i (1- i))))))
(defun eelll-stroke-for-char (ch)
"CH(全角一文字)の打ち方を返す。Tコードで入力できなければnilを返す。"
(let* ((char (tcode-string-to-char ch))
(strokes (eelll-tcode-encode char))
(dat (tcode-stroke-prefix-match strokes)))
(when eelll-prefix-stroke
(if (equal eelll-prefix-stroke (car (car dat)))
(setq strokes (cdr dat))
(setq strokes nil)))
(if (and strokes
(= (length strokes) 2))
strokes
nil)))
(defun eelll-inputs-for-char (ch)
"CH (全角1文字)の打ち方(文字列のリスト)を返す。
Tコードで入力できなければnilを返す。"
(let ((strokes (eelll-tcode-encode (tcode-string-to-char ch))))
(cond (strokes
(mapcar (lambda (key)
(aref eelll-key-table key))
strokes))
(tcode-another-table
(list (aref eelll-key-table (eelll-tcode-another-stroke ch))
? ))
((string= ch " ")
(list ? )))))
;;;
;;; Stroke chart
;;;
(defun eelll-draw-chart ()
"練習テキストの練習対象となる文字のストローク表を作る。"
(with-current-buffer (get-buffer-create eelll-help-buffer-name)
(widen)
(erase-buffer)
(goto-char (point-min))
(let ((i 0)
j k
(c (if eelll-second-hand 1 4)))
(while (< i 4)
(setq i (1+ i) j 0)
(while (< j 4)
(insert " ")
(setq j (1+ j) k 0)
(while (< k 5)
(setq k (1+ k))
(insert "----- ")
(if (= k c) (insert " ")))
(delete-horizontal-space)
(insert "\n"))
(if (< i 4) (insert "\n"))))
(mapcar
(lambda (c)
(let ((stroke (eelll-stroke-for-char (char-to-string c))))
(when (and stroke
(= (length stroke) 2))
(let* ((second (car (cdr stroke)))
(fc (% (car stroke) 5))
(fr (/ (car stroke) 10))
(sc (% second 5))
(sr (/ second 10)))
(goto-line (+ (* sr 5) fr 1))
(move-to-column (+ 4 (* 12 sc) (* 2 fc)
(if (> sc (if eelll-second-hand 0 3)) 2 0)))
(tcode-delete-char 1)
(insert (char-to-string c))))))
(string-to-list eelll-lesson-chars))
(goto-char (point-min))
(forward-line 1)
(delete-region (point-min) (point))
(unless eelll-upper-row
(forward-line 5)
(delete-region (point-min) (point))
(forward-line 4)
(delete-region (point)
(save-excursion (forward-line 1)
(point)))
(forward-line 4)
(delete-region (point)
(save-excursion (forward-line 1)
(point))))
(setq eelll-lesson-pattern-string
(concat (when eelll-prefix-stroke
(nth 2 (car (tcode-stroke-prefix-match
eelll-prefix-stroke))))
(if eelll-first-hand "R" "L")
"->"
(if eelll-second-hand "R" "L")))
(set-buffer-modified-p nil)))
;;;
;;; String Matching
;;;
(defun eelll-subsequence (seq n)
;; return first N elements of SEQ
(let ((seq (copy-sequence seq)))
(if (> (length seq) n)
(setcdr (nthcdr (1- n) seq) nil))
(if (<= n 0)
nil
seq)))
(defun eelll-match (string quest)
"QUESTをお手本として入力された文字列STRINGの採点をする。
2要素のリスト(RESULT ERROR)を返す。"
(let* ( ;; template は ((文字 . ストローク数) . ストローク) のリスト
(template (delq nil
(mapcar
(lambda (c)
(let ((stroke (eelll-inputs-for-char
(char-to-string c))))
(and stroke
(cons (cons c (length stroke))
stroke))))
(string-to-list quest))))
;; template-length は template の長さ
(template-length (length template))
;; vtemplate は (文字 . ストローク数) のベクター
(vtemplate (vconcat (mapcar 'car template)))
;; key-list は入力された文字のリスト
(key-list (string-to-list string))
(key-length (length string))
(link (list '(0 . 0)))
(last (1- (length string)))
(j 0))
(while template
(let ((correct (mapconcat 'char-to-string (cdr (car template)) nil))
(correct-length (cdr (car (car template))))
(inputted key-list))
(while inputted
(when (string= correct
(mapconcat 'char-to-string
(eelll-subsequence inputted correct-length)
nil))
(let* ((l link)
(next-input (nthcdr correct-length inputted))
(next-i (- key-length (length next-input)))
(next-template (cdr template))
(next-j (- template-length (length next-template)))
pair)
(catch 'finish
(while l
(if (and (>= (- key-length (length inputted))
(car (car l)))
(>= (- template-length (length template))
(cdr (car l))))
(throw 'finish nil)
(setq pair (car l)
l (cdr l)))))
(if pair
(let ((dif (+ (- (car pair) next-i) (- (cdr pair) next-j))))
(when (or (> dif 0)
(and (= dif 0)
(> next-i (car pair))))
(setcar pair next-i)
(setcdr pair next-j)))
(setq link (cons (cons next-i next-j) link)))))
(setq inputted (cdr inputted))))
(setq template (cdr template)))
(let* ((res (vconcat (string-to-list string)))
(pi (length string))
(pj template-length)
(err 0)
(l link)
(i (car (car l))))
(while (> i 0)
(let* ((j (cdr (car l)))
(correct (aref vtemplate (1- j))))
(aset res (- i 1) (car correct))
(let ((n (1- (cdr correct))))
(while (>= n 1)
(aset res (- i 1 n) nil)
(setq n (1- n))))
(setq err (+ err (max (- pi i) (- pj j)) (- (cdr correct))))
(setq pi i
pj j
l (cdr l)
i (car (car l)))))
(list (mapconcat (lambda (c) (if c (char-to-string c)))
res nil)
(+ err (max pi pj))))))
;;;
;;; Mode setup
;;;
(defvar eelll-mode-map nil
"EELLL モードで使うキーマップ")
(if eelll-mode-map
()
(setq eelll-mode-map (make-keymap))
(substitute-key-definition nil 'eelll-undefined eelll-mode-map)
(define-key eelll-mode-map "\177" 'eelll-delete-char)
(define-key eelll-mode-map "\e" 'ESC-prefix)
(define-key eelll-mode-map "\C-c" 'mode-specific-command-prefix)
(define-key eelll-mode-map "\C-j" 'eelll-return)
(define-key eelll-mode-map "\C-m" 'eelll-return)
(define-key eelll-mode-map "\C-g" 'eelll-confirm-quit)
(define-key eelll-mode-map "\C-l" 'eelll-redisplay)
(define-key eelll-mode-map "\C-u" 'universal-argument)
(define-key eelll-mode-map "\C-x" 'Control-X-prefix)
(define-key eelll-mode-map "\C-]" 'abort-recursive-edit)
(define-key eelll-mode-map "?" 'eelll-help)
(define-key eelll-mode-map " " 'eelll-key))
(defun eelll-mode ()
"EELLL は Emacs Lisp で実現されたTコード練習プログラムです。
画面に表示された文字列をそのまま入力してください。
1行の入力が終わったらリターンキーを打ってください。
画面の上半分には今のレッスンで習う文字のストローク表が表示されています。
EELLL 内ではほとんどのコマンドが禁止されています。
まず \\[switch-to-buffer] で他のバッファに移ってから\
コマンドを実行してください。
なお、\\[eelll-confirm-quit] で EELLL を中断します。"
(use-local-map eelll-mode-map)
(setq major-mode 'eelll-mode)
(setq mode-name "EELLL")
(setq mode-line-format '("-----EELLL"
(eelll-lesson-string
(": lesson " eelll-lesson-string))
(eelll-random-mode "(random)")
"%-"))
(run-hooks 'eelll-mode-hook))
(defun eelll-help ()
(interactive)
(if tcode-help-with-real-keys
(progn
(forward-line -2)
(while (looking-at "^;")
(forward-line -1))
(insert "\n")
(eelll-insert-line-help eelll-text-line))
(setq eelll-upper-row t)
(if (null eelll-second-hand)
(setq eelll-second-hand t
eelll-first-hand eelll-first-hand)
(setq eelll-second-hand nil
eelll-first-hand (not eelll-first-hand)))
(let ((eelll-lesson-chars (eelll-select-chars eelll-text-line))
(cur (selected-window)))
(eelll-draw-chart)
(delete-other-windows)
(split-window-vertically
(with-current-buffer eelll-help-buffer-name
(setq mode-line-format
'("-----EELLL Help"
(eelll-lesson-string
(": lesson " eelll-lesson-string))
(eelll-lesson-pattern-string
(" (" eelll-lesson-pattern-string ")"))
"%-"))
(+ (count-lines (point-min) (point-max))
eelll-help-window-margin)))
(switch-to-buffer eelll-help-buffer-name)
(other-window 1))
(setq eelll-window-configuration (current-window-configuration)))
(eelll-redisplay))
(defun eelll-select-chars (text)
(let ((ret nil))
(mapcar (lambda (c)
(let* ((stroke (eelll-stroke-for-char (char-to-string c)))
(1st (car stroke))
(2nd (car (cdr stroke)))
(lks '(0 1 2 3 4
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34))
(rks '(5 6 7 8 9
15 16 17 18 19
25 26 27 28 29
35 36 37 38 39)))
(and (memq 1st (if eelll-first-hand rks lks))
(memq 2nd (if eelll-second-hand rks lks))
(setq ret (cons c ret)))))
(string-to-list text))
(if ret
(mapconcat 'char-to-string ret nil)
"")))
(defun eelll-undefined ()
(interactive)
(message (substitute-command-keys
"\\[switch-to-buffer] SOME-BUFFER first.")))
(defun eelll-delete-char ()
(interactive)
(message "間違いを気にせずどんどん入力してください。"))
;;;
;;; 専用の入力補完付きのcompleting-read
;;;
;;; "?"で一覧が見れます。
(defun eelll-completing-read ()
"入力補完付きで、練習テキスト番号をミニバッファから入力する。
Emacs内部のcompletionの実装上の問題のため、「?」を
入力した時にしか一覧は見えない。"
(unless (featurep 'tcode-ready)
(tcode-use-package (or tcode-current-package
tcode-default-input-method))
(tcode-inactivate))
(load eelll-configuration-file-name t)
(if current-prefix-arg
(setq eelll-text
(completing-read "text for eelll: "
(mapcar 'list
(nconc (and tcode-data-directory
(directory-files
tcode-data-directory
nil "^EELLLTXT"))
(directory-files
tcode-site-data-directory
nil "^EELLLTXT")))
nil t (or eelll-text "EELLLTXT"))))
(if (not (equal eelll-current-text eelll-text))
(let ((config (assoc eelll-text eelll-last-lesson-alist)))
(setq eelll-last-lesson (if config
(cdr config)
nil))))
(let (lesson-alist key val orig-minibuffer-completion-help)
(save-excursion
(if (and (not (equal eelll-current-text eelll-text))
(get-buffer eelll-text-buffer))
(kill-buffer eelll-text-buffer))
(tcode-set-work-buffer eelll-text-buffer eelll-text)
(setq eelll-current-text eelll-text)
(widen)
(goto-char (point-min))
(while (re-search-forward "^Lesson \\([0-9]+\\):.*[rRlL]+!?$" nil t)
(setq key (buffer-substring (match-beginning 1) (match-end 1)))
(forward-line 2)
(setq val (buffer-substring (point)
(save-excursion (end-of-line) (point))))
(setq lesson-alist (cons (cons key val) lesson-alist))))
(setq orig-minibuffer-completion-help
(symbol-function 'minibuffer-completion-help))
(unwind-protect
(progn
(fset 'minibuffer-completion-help
(symbol-function 'eelll-minibuffer-completion-help))
(let* ((hist (mapcar 'car lesson-alist))
(pos (if eelll-last-lesson
(- (length hist)
(length (member (format "%d" eelll-last-lesson)
hist))
-1)))
(str (cond ((tcode-xemacs-p)
(completing-read
"練習テキスト[`?'で一覧] "
lesson-alist nil t
(if eelll-last-lesson
(format "%d" eelll-last-lesson)
"")
(if pos (cons 'hist (list pos)) (list 'hist))))
(t
(completing-read
"練習テキスト[`?'で一覧] "
lesson-alist nil t
(if eelll-last-lesson
(format "%d" eelll-last-lesson)
"")
(if pos (cons 'hist pos) 'hist))))))
(if (string= str "")
nil
(list (string-to-number str)))))
(fset 'minibuffer-completion-help orig-minibuffer-completion-help))))
(defun eelll-minibuffer-completion-help ()
"EELLL用の補完候補を表示する。
`minibuffer-completion-help'を置き換える。"
(interactive)
(with-output-to-temp-buffer "*Completions*"
(eelll-display-completion-list
(all-completions (save-excursion
(buffer-substring (progn (beginning-of-line) (point))
(progn (end-of-line) (point))))
minibuffer-completion-table))))
(defun eelll-display-completion-list (x)
"練習テキスト一覧を表示する。
`display-completion-list'を置き換える。"
(princ " ---- 練習テキスト一覧 ----\n")
(setq x (sort x (lambda (x y)
(< (string-to-number x) (string-to-number y)))))
(while x
(princ (car x))
(princ ":")
(princ (cdr (assoc (car x) lesson-alist)))
(princ "\n")
(setq x (cdr x))))
;;;; 一行ヘルプの表示
(defun eelll-insert-with-face (str)
(let ((beg (point)))
(insert str)
(when (and (fboundp 'put-text-property)
window-system)
(put-text-property beg (point) 'face 'eelll-face))))
(defun eelll-put-help-char (c)
(if (or eelll-use-column-for-help
(not window-system)
(> 22 emacs-major-version))
(eelll-put-help-char-by-column c)
(eelll-put-help-char-by-pixel c)))
(defun eelll-put-help-char-by-column (c)
(let* ((ch (char-to-string c))
(stroke (eelll-tcode-encode c))
(another (eelll-tcode-another-stroke ch))
(s (cond (stroke
(tcode-stroke-to-string stroke))
(another (concat (let ((tcode-stroke-to-string-closer
tcode-stroke-to-string-separator))
(tcode-stroke-to-string (list another)))
"SPC"))
(t "-- --")))
(ex-col (string-width s)))
(if (<= (+ help-column (max ex-col 5) 1)
(* (frame-width) 0.80))
(setq help-column (+ help-column (max ex-col 5) 1))
(insert "\n\n")
(setq help-column (1+ (max ex-col 5))))
(forward-line -1)
(end-of-line)
(if (>= ex-col 5)
(eelll-insert-with-face s)
(setq ex-col (- 5 ex-col))
(eelll-insert-with-face (concat (make-string (- ex-col (/ ex-col 2)) 32)
s
(make-string (/ ex-col 2) 32)))
(setq ex-col 5))
(eelll-insert-with-face "|")
(forward-line)
(end-of-line)
(if (equal ch " ")
(setq ch "SPC"))
(setq ex-col (- ex-col (string-width ch)))
(eelll-insert-with-face (concat (make-string (- ex-col (/ ex-col 2)) 32)
ch
(make-string (/ ex-col 2) 32)))
(eelll-insert-with-face "|")
))
(defun eelll-put-help-char-by-pixel (c)
(let* ((ch (char-to-string c))
(stroke (eelll-tcode-encode c))
(another (eelll-tcode-another-stroke ch))
(s (cond (stroke
(tcode-stroke-to-string stroke))
(another (concat (let ((tcode-stroke-to-string-closer
tcode-stroke-to-string-separator))
(tcode-stroke-to-string (list another)))
"SPC"))
(t "-- --")))
(p1 (progn (forward-line -1) (end-of-line) (point))) p1e p2 p2e
sw cw dw)
(if (equal ch " ") (setq ch "SPC"))
(narrow-to-region (point) (point))
(set-window-start (selected-window) (point))
(eelll-insert-with-face s)
(eelll-insert-with-face "|")
(setq p1e (point))
(setq sw (car (pos-visible-in-window-p nil nil t)))
(widen)
(forward-line)
(end-of-line)
(setq p2 (point))
(narrow-to-region (point) (point))
(set-window-start (selected-window) (point))
(eelll-insert-with-face ch)
(eelll-insert-with-face "|")
(setq p2e (point))
(setq cw (car (pos-visible-in-window-p nil nil t)))
(widen)
(setq dw (max cw sw))
(if (<= (+ help-column dw)
(* (let ((l (window-inside-pixel-edges))) (- (nth 2 l) (car l)))
0.90))
(setq help-column (+ help-column dw))
(let (s1 s2)
(setq s2 (buffer-substring p2 (point)))
(delete-region p2 (point))
(setq s1 (buffer-substring p1 p1e))
(delete-region p1 p1e)
(goto-char p1)
(forward-line 2)
(setq p1 (point))
(insert s1 "\n")
(setq p1e (1- (point)))
(setq p2 (point))
(insert s2 "\n")
(setq p2e (1- (point)))
(forward-char -1)
(setq help-column dw)))
(let* ((w (abs (- cw sw)))
(padl (/ w 2))
(padr (- w padl)))
(when (< 0 padr)
(goto-char (1- (if (<= cw sw) p2e p1e)))
(insert 32)
(put-text-property (1- (point)) (point)
'display `(space :width (,padr))))
(when (< 0 padl)
(goto-char (if (<= cw sw) p2 p1))
(insert 32)
(put-text-property (1- (point)) (point)
'display `(space :width (,padl))))
)
(goto-char p2e)
(end-of-line)))
(defun eelll-insert-line-help (string)
(or (bolp) (insert "\n"))
(let ((chars (string-to-list string))
(help-column 0)
(beg (progn
(insert "\n\n")
(forward-char -1)
(point))))
(mapcar 'eelll-put-help-char chars)
(forward-line)))
;;;
;;; Main
;;;
(defun eelll-init ()
(tcode-use-package (or tcode-current-package
tcode-default-input-method))
(tcode-inactivate)
(unless eelll-key-table
;; initialize `eelll-key-table' and `eelll-keymap-table'
(let ((n (1- (length tcode-key-translation-rule-table))))
(setq eelll-key-table (make-vector 80 nil))
(while (>= n 0)
(let ((key (aref tcode-key-translation-rule-table n)))
(when (>= key 0)
(aset eelll-key-table key (+ n ? ))
(define-key eelll-mode-map (char-to-string (+ n ? )) 'eelll-key))
(setq n (1- n))))))
(setq eelll-original-window-configuration (current-window-configuration))
(setq eelll-previous-error-rate (if eelll-expert 0 100))
(setq eelll-current-text eelll-text)
(if (not (stringp lesson))
(eelll-prepare-text lesson)
(setq eelll-current-text nil
eelll-lesson-string "Temporary"
eelll-lesson-no 0
eelll-lesson-chars "")
(with-current-buffer (get-buffer-create eelll-current-text-buffer)
(erase-buffer)
(insert lesson)
(if eelll-random-mode
(eelll-random-text eelll-random-max-line))
(eelll-normalize-text-buffer)))
(eelll-setup-lesson)
(message (substitute-command-keys "\\[eelll-help] でヘルプ")))
(defun eelll-normalize-text-buffer ()
;; delete white space
(goto-char (point-min))
(while (re-search-forward "[ \t]+" nil t)
(replace-match ""))
;; delete null line
(goto-char (point-min))
(while (re-search-forward "\n\n+" nil t)
(replace-match "\n"))
(goto-char (point-min))
(let ((line 0) c stroke cell (str "▲")
(bol-kinsoku
(string-to-list ")!?、。,.・:;?!゛゜´`¨^\
‾_ヽヾゝゞ〃仝々〆〇ー—‐\
/\〜‖|…‥’”)〕]}〉》」』】°′″℃\
ぁぃぅぇぉっゃゅょゎァィゥェォッャュョヮヵヶ"))
(eol-kinsoku (string-to-list "(‘“(〔[{〈《「『【°′″℃@§")))
(while (and (< line eelll-by-text-max-line)
(not (eobp)))
(setq c (tcode-following-char)
stroke (or (eelll-tcode-encode c)
(eelll-tcode-another-stroke c)))
(when (and (bolp) (not (bobp))
(memq c bol-kinsoku))
(delete-char -1))
(if (or stroke (eolp))
(if (eolp) (delete-char 1) (forward-char))
(delete-char 1)
(setq cell (or (tcode-bushu-help-lookup (char-to-string c))
(tcode-decompose-char (char-to-string c) t)))
(if (not (and (consp cell)
(stringp (car cell))
(stringp (cdr cell))))
(insert eelll-by-text-dummy-char)
(if tcode-use-postfix-bushu-as-default
(insert (car cell) (cdr cell) str)
(insert str (car cell) (cdr cell)))))
(when (and (< eelll-by-text-fill-column (current-column))
(not (memq c eol-kinsoku)))
(insert ?\n)
(setq line (1+ line)))))
(or (bolp) (insert ?\n))
(delete-region (point) (point-max))
(goto-char (point-min)))
;;;###autoload
(defun eelll (&optional lesson text)
"EELLL を始める。詳しくは `eelll-mode' の解説を見よ。"
(interactive (eelll-completing-read))
(if text (setq eelll-text text))
(setq eelll-random-mode nil)
(eelll-init))
;;;###autoload
(defun eelll-random (&optional lesson text)
"ランダムモードで EELLL を始める。
指定したテキストの中からランダムに
数行(`eelll-random-max-line'で指定した行数)選択される。"
(interactive (eelll-completing-read))
(if text (setq eelll-text text))
(setq eelll-random-mode t)
(eelll-init))
;;;###autoload
(defun eelll-region (beg end)
"リージョン内のテキストで EELLL を始める。"
(interactive "r")
(unless (featurep 'tcode-ready)
(tcode-use-package (or tcode-current-package
tcode-default-input-method))
(tcode-inactivate))
(load eelll-configuration-file-name t)
(let ((lesson (buffer-substring beg end)))
(setq eelll-random-mode nil)
(eelll-init)))
(defun eelll-setup-lesson ()
(eelll-draw-chart)
(switch-to-buffer eelll-buffer-name)
(buffer-disable-undo)
(eelll-mode)
(widen)
(erase-buffer)
(delete-other-windows)
(if tcode-help-with-real-keys
nil
(when (>= eelll-previous-error-rate eelll-display-help-threshold)
(split-window-vertically
(with-current-buffer eelll-help-buffer-name
(setq mode-line-format
'("-----EELLL Help"
(eelll-lesson-string
(": lesson " eelll-lesson-string))
(eelll-lesson-pattern-string
(" (" eelll-lesson-pattern-string ")"))
"%-"))
(+ (count-lines (point-min) (point-max))
eelll-help-window-margin)))
(switch-to-buffer eelll-help-buffer-name)
(goto-char (point-min))
(other-window 1)))
(setq eelll-window-configuration (current-window-configuration))
(setq eelll-start-time nil
eelll-error-strokes 0
eelll-strokes 0)
(insert "リターンキーを打てば始まります。 "))
(defun eelll-key ()
(interactive)
(with-current-buffer " *eelll: strokes*"
(insert (char-to-string last-command-event)))
(if eelll-move-cursor
(insert " ")))
(defun eelll-return ()
(interactive)
(if eelll-start-time
(progn
(delete-region (point) (progn (beginning-of-line 1) (point)))
(let* ((str (with-current-buffer " *eelll: strokes*"
(buffer-string)))