5 (put 'ew-line-generic 'lisp-indent-function 1)
6 (put 'ew-line-convert 'lisp-indent-function 1)
8 (defun ew-lf-to-crlf (str)
9 (let ((i 0) (j 0) (l (length str)) result)
11 (when (equal (aref str j) ?\n)
12 (setq result (ew-rcons*
19 (setq result (ew-rcons*
22 (apply 'concat (nreverse result))))
24 (defun ew-crlf-to-lf (str)
25 (let* ((i 0) (j 0) (l (length str)) (l- (1- l)) result)
27 (when (and (equal (aref str j) ?\r)
28 (equal (aref str (1+ j)) ?\n))
29 (setq result (ew-rcons*
36 (setq result (ew-rcons*
39 (apply 'concat (nreverse result))))
41 (defun ew-lf-crlf-to-crlf (str)
42 (let* ((i 0) (j 0) (l (length str)) (l- (1- l)) result)
46 (equal (aref str j) ?\r)
47 (equal (aref str (1+ j)) ?\n))
49 ((equal (aref str j) ?\n)
50 (setq result (ew-rcons*
57 (setq result (ew-rcons*
60 (apply 'concat (nreverse result))))
62 (defun ew-crlf-unfold (str)
63 (let* ((i 0) (j 0) (l (length str)) (l- (- l 2)) result)
65 (when (and (equal (aref str j) ?\r)
66 (equal (aref str (1+ j)) ?\n)
67 (member (aref str (+ j 2)) '(?\t ?\ )))
68 (setq result (ew-rcons*
75 (setq result (ew-rcons*
78 (apply 'concat (nreverse result))))
80 (defun ew-lf-unfold (str)
81 (let* ((i 0) (j 0) (l (length str)) (l- (- l 1)) result)
83 (when (and (equal (aref str j) ?\n)
84 (member (aref str (+ j 1)) '(?\t ?\ )))
85 (setq result (ew-rcons*
92 (setq result (ew-rcons*
95 (apply 'concat (nreverse result))))
97 (defun ew-cut-cr-lf (str)
98 (let ((i 0) (j 0) (l (length str)) result)
100 (when (member (aref str j) '(?\r ?\n))
101 (setq result (ew-rcons*
107 (setq result (ew-rcons*
110 (apply 'concat (nreverse result))))
112 (defmacro ew-line-generic-define ()
113 (let ((str (make-symbol "str"))
114 (others-fun (make-symbol "others-fun"))
115 (fold-fun (make-symbol "fold-fun"))
116 (crlf-fun (make-symbol "crlf-fun"))
117 (bare-cr-fun (make-symbol "bare-cr-fun"))
118 (bare-lf-fun (make-symbol "bare-lf-fun"))
119 (p (make-symbol "p"))
120 (q (make-symbol "q"))
121 (r (make-symbol "r")))
122 `(defun ew-line-generic
123 (,str ,others-fun ,fold-fun ,crlf-fun ,bare-cr-fun ,bare-lf-fun)
124 (let ((,p 0) (,q (length ,str)) ,r)
127 (lex-scan-unibyte ,str ,p ,q
128 ((+ [^ "\r\n"]) (when ,others-fun (funcall ,others-fun ,r ,p)))
129 ((?\r ?\n [" \t"]) (when ,fold-fun (funcall ,fold-fun ,r ,p)))
130 ((?\r ?\n) (when ,crlf-fun (funcall ,crlf-fun ,r ,p)))
131 ((?\r) (when ,bare-cr-fun (funcall ,bare-cr-fun ,r ,p)))
132 ((?\n) (when ,bare-lf-fun (funcall ,bare-lf-fun ,r ,p)))
133 (() (error "something wrong"))))
136 (ew-line-generic-define)
138 (defmacro ew-line-convert-define ()
139 (let ((str (make-symbol "str"))
140 (others-fun (make-symbol "others-fun"))
141 (fold-fun (make-symbol "fold-fun"))
142 (crlf-fun (make-symbol "crlf-fun"))
143 (bare-cr-fun (make-symbol "bare-cr-fun"))
144 (bare-lf-fun (make-symbol "bare-lf-fun"))
145 (index (make-symbol "index"))
146 (result (make-symbol "result"))
147 (start (make-symbol "starx"))
148 (end (make-symbol "end")))
149 `(defun ew-line-convert
150 (,str ,others-fun ,fold-fun ,crlf-fun ,bare-cr-fun ,bare-lf-fun)
151 (let ((,index 0) ,result)
152 (when (> (ew-line-generic
157 (lambda (,start ,end)
158 (when (< ,index ,start)
161 (substring ,str ,index ,start))))
165 (substring ,str ,start ,end)))
167 (list others-fun fold-fun crlf-fun bare-cr-fun bare-lf-fun)))
171 (substring ,str ,index))))
172 (apply 'concat (nreverse ,result))))))
174 (ew-line-convert-define)