-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsc-vertical.el
189 lines (176 loc) · 5.57 KB
/
fsc-vertical.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
;;; fsc-verti.el --- Convert string into CJK vertical writing style -*- lexical-binding: t; -*-
;; http://en.wikipedia.org/wiki/Horizontal_and_vertical_writing_in_East_Asian_scripts
;; 就是把字串轉成直書。 縦書に文字列を変換する。
;; Copyright (C) 2014 kuanyui
;;
;; Author: kuanyui <[email protected]>
;; Keywords:
(require 'cl)
(defun fsc/vertical-minibuffer (text number)
"Rotate the text vertically from minibuffer."
(interactive "sInput text: \nnHow many characters a line: ")
(insert (fsc-vertical number text)))
(defun fsc/vertical-region (begin end number)
"Rotate the text of region to vertical. Notice that the
original text will be replaced."
(interactive "r\nnHow many characters a line: ")
(insert (fsc-vertical number (delete-and-extract-region begin end))))
(defun fsc/vertical-region-and-copy (begin end number)
"Rotate the text of region to vertical, and copy into kill-ring."
(interactive "r\nnHow many characters a line: ")
(kill-new (fsc-vertical number (buffer-substring-no-properties begin end)))
(message "Copied."))
;; 1. Convert halfwidth to fullwidth
;; 2. Split-string by newline ("第一行" "第二行")
;; 3. Fill every string with fullwidth space to minimum mutiple of NUM. e.g. NUM=5 ("第一行 " "第二行 ")
;; 4. Concat string, and list all characters ("第" "一" "行" " " " " "第" "二" "行" " " " ")
;; 5. Send this list to `fsc-vertical--rotate-internal' to process.
;; 6. mapconcat the result, and insert spaces between each vertical lines.
(defun fsc-vertical (num text)
(let* ((fin (mapcar (lambda (x)
(if (> (% (length x) num) 0)
(concat x (make-string (- num (% (length x) num)) (string-to-char " ")))
x))
(split-string (fsc-vertical-halfwidth-to-fullwidth text) "\n")))
(fin (fsc-vertical--rotate-internal num
(mapcar #'string
(string-to-list (apply #'concat fin))))))
(mapconcat (lambda (x)
(mapconcat (lambda (y) y)
x " "))
fin "\n")))
(defun fsc-vertical--rotate-internal (num string-list)
"This function should be called by `fsc-vertical'
NUM is characters amount a line.
STRING-LIST should be a list like this (NUM = 5):
(\"第\" \"一\" \"行\" \" \" \" \" \"第\" \"二\" \"行\" \" \" \" \")"
(mapcar (lambda (x) (reverse x))
(loop for l from 1 to num collect ; (/ (length string-list) num) is amount of lines in final output (length of each list).
(loop for i from 0 to (1- (/ (length string-list) num)) collect
(elt string-list (+ (1- l) (* i num)))))))
;; l1: 0 5 10 15 ...
;; l2: 1 6 12 16 ...
;; l3: 2 7 13 17 ...
;; l4: 3 8 14 18 ...
;; l5: 4 9 15 19 ...
;; After reversal, final output:
;; '(("第" "第")
;; ("二" "一")
;; ("行" "行")
;; (" " " ")
;; (" " " "))
(defun fsc-vertical-halfwidth-to-fullwidth (text)
""
(let (char)
(mapconcat
(lambda (x)
(if (setq char (cdr (assoc (string x) fsc-vertical-halfwidth-to-fullwidth-table)))
char
(string x)))
(string-to-list text) "")))
(setq fsc-vertical-halfwidth-to-fullwidth-table
'((" " . " ")
("#" . "#")
("$" . "$")
("%" . "%")
("^" . "︿")
("&" . "&")
("*" . "*")
("/" . "/")
("\\" . "\")
("." . "。")
("0" . "0")
("1" . "1")
("2" . "2")
("3" . "3")
("4" . "4")
("5" . "5")
("6" . "6")
("7" . "7")
("8" . "8")
("9" . "9")
(":" . ":")
(";" . ";")
("<" . "^")
("=" . "=")
(">" . "V")
("?" . "?")
("@" . "@")
("A" . "A")
("B" . "B")
("C" . "C")
("D" . "D")
("E" . "E")
("F" . "F")
("G" . "G")
("H" . "H")
("I" . "I")
("J" . "J")
("K" . "K")
("L" . "L")
("M" . "M")
("N" . "N")
("O" . "O")
("P" . "P")
("Q" . "Q")
("R" . "R")
("S" . "S")
("T" . "T")
("U" . "U")
("V" . "V")
("W" . "W")
("X" . "X")
("Y" . "Y")
("Z" . "Z")
("_" . "|")
("-" . "|")
("`" . "`")
("a" . "a")
("b" . "b")
("c" . "c")
("d" . "d")
("e" . "e")
("f" . "f")
("g" . "g")
("h" . "h")
("i" . "i")
("j" . "j")
("k" . "k")
("l" . "l")
("m" . "m")
("n" . "n")
("o" . "o")
("p" . "p")
("q" . "q")
("r" . "r")
("s" . "s")
("t" . "t")
("u" . "u")
("v" . "v")
("w" . "w")
("x" . "x")
("y" . "y")
("z" . "z")
("{" . "︷")
("|" . "-")
("}" . "︸")
("(" . "︵")
(")" . "︶")
("(" . "︵")
(")" . "︶")
("「" . "﹁")
("」" . "﹂")
("【" . "︻")
("】" . "︼")
("〔" . "︹")
("〕" . "︺")
("《" . "︽")
("》" . "︾")
("〈" . "︿")
("〉" . "﹀")
("『" . "﹃")
("』" . "﹄")
("[" . "﹇")
("]" . "﹈")
))
(provide 'fsc-vertical)