forked from wanderlust/apel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyrich.el
169 lines (155 loc) · 4.05 KB
/
tinyrich.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
;;;
;;; $Id$
;;;
;;; by MORIOKA Tomohiko <[email protected]>
;;; modified by YAMATE Keiichirou <[email protected]>
;;;
(defvar mime-viewer/face-list-for-text/enriched
(cond ((and (>= emacs-major-version 19) window-system)
'(bold italic fixed underline)
)
((and (boundp 'NEMACS) NEMACS)
'("bold" "italic" "underline")
)))
(defun enriched-decode (beg end)
(interactive "*r")
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char beg)
(while (re-search-forward "[ \t]*\\(\n+\\)[ \t]*" nil t)
(let ((str (buffer-substring (match-beginning 1)
(match-end 1))))
(if (string= str "\n")
(replace-match " ")
(replace-match (substring str 1))
)))
(goto-char beg)
(let (cmd sym str (fb (point)) fe b e)
(while (re-search-forward "<\\(<\\|[^<>\n\r \t]+>\\)" nil t)
(setq b (match-beginning 0))
(setq cmd (buffer-substring b (match-end 0)))
(if (string= cmd "<<")
(replace-match "<")
(replace-match "")
(setq cmd (downcase (substring cmd 1 (- (length cmd) 1))))
)
(setq sym (intern cmd))
(cond ((eq sym 'param)
(setq b (point))
(save-excursion
(save-restriction
(if (search-forward "</param>" nil t)
(progn
(replace-match "")
(setq e (point))
)
(setq e end)
)))
(delete-region b e)
)
((memq sym mime-viewer/face-list-for-text/enriched)
(setq b (point))
(save-excursion
(save-restriction
(if (re-search-forward (concat "</" cmd ">") nil t)
(progn
(replace-match "")
(setq e (point))
)
(setq e end)
)))
(tm:set-face-region b e sym)
)))
(goto-char (point-max))
(if (not (eq (preceding-char) ?\n))
(insert "\n")
)
))))
;;; @ text/richtext <-> text/enriched converter
;;;
(defun richtext-to-enriched-region (beg end)
"Convert the region of text/richtext style to text/enriched style."
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(let (b e i)
(while (re-search-forward "[ \t]*<comment>" nil t)
(setq b (match-beginning 0))
(delete-region b
(if (re-search-forward "</comment>[ \t]*" nil t)
(match-end 0)
(point-max)
))
)
(goto-char (point-min))
(while (re-search-forward "\n\n+" nil t)
(replace-match "\n")
)
(goto-char (point-min))
(while (re-search-forward "[ \t\n]*<nl>[ \t\n]*" nil t)
(setq b (match-beginning 0))
(setq e (match-end 0))
(setq i 1)
(while (looking-at "[ \t\n]*<nl>[ \t\n]*")
(setq e (match-end 0))
(setq i (1+ i))
(goto-char e)
)
(delete-region b e)
(while (>= i 0)
(insert "\n")
(setq i (1- i))
))
(goto-char (point-min))
(while (search-forward "<lt>" nil t)
(replace-match "<<")
)
))))
(defun enriched-to-richtext-region (beg end)
"Convert the region of text/enriched style to text/richtext style."
(save-excursion
(save-restriction
(goto-char beg)
(and (search-forward "text/enriched")
(replace-match "text/richtext"))
(search-forward "\n\n")
(narrow-to-region (match-end 0) end)
(let (str n)
(goto-char (point-min))
(while (re-search-forward "\n\n+" nil t)
(setq str (buffer-substring (match-beginning 0)
(match-end 0)))
(setq n (1- (length str)))
(setq str "")
(while (> n 0)
(setq str (concat str "<nl>\n"))
(setq n (1- n))
)
(replace-match str)
)
(goto-char (point-min))
(while (search-forward "<<" nil t)
(replace-match "<lt>")
)
))))
;;; @ encoder and decoder
;;;
(defun richtext-decode (beg end)
(save-restriction
(narrow-to-region beg end)
(richtext-to-enriched-region beg (point-max))
(enriched-decode beg (point-max))
))
;; (defun richtext-encode (beg end)
;; (save-restriction
;; (narrow-to-region beg end)
;; (enriched-encode beg (point-max))
;; (enriched-to-richtext-region beg (point-max))
;; ))
;;; @ end
;;;
(require 'product)
(product-provide (provide 'tinyrich) (require 'apel-ver))
;; tinyrich.el ends here.