(require 'ew-util)
(provide 'ew-line)
-(put 'ew-line-generic 'lisp-indent-function 1)
-(put 'ew-line-convert 'lisp-indent-function 1)
+(put 'ew-crlf-line-generic 'lisp-indent-function 1)
+(put 'ew-crlf-line-convert 'lisp-indent-function 1)
+(put 'ew-lf-line-generic 'lisp-indent-function 1)
+(put 'ew-lf-line-convert 'lisp-indent-function 1)
(defun ew-lf-to-crlf (str)
(let ((i 0) (j 0) (l (length str)) result)
(substring str i))))
(apply 'concat (nreverse result))))
-(defun ew-cut-cr-lf (str)
+(defun ew-cut-generic (str chars)
(let ((i 0) (j 0) (l (length str)) result)
(while (< j l)
- (when (member (aref str j) '(?\r ?\n))
+ (when (member (aref str j) chars)
(setq result (ew-rcons*
result
(substring str i j))
(substring str i))))
(apply 'concat (nreverse result))))
-(defmacro ew-line-generic-define ()
+(defun ew-cut-cr-lf (str) (ew-cut-generic str '(?\r ?\n)))
+(defun ew-cut-cr (str) (ew-cut-generic str '(?\r)))
+(defun ew-cut-lf (str) (ew-cut-generic str '(?\n)))
+
+(defmacro ew-crlf-line-generic-define ()
(let ((str (make-symbol "str"))
(others-fun (make-symbol "others-fun"))
(fold-fun (make-symbol "fold-fun"))
(p (make-symbol "p"))
(q (make-symbol "q"))
(r (make-symbol "r")))
- `(defun ew-line-generic
+ `(defun ew-crlf-line-generic
(,str ,others-fun ,fold-fun ,crlf-fun ,bare-cr-fun ,bare-lf-fun)
(let ((,p 0) (,q (length ,str)) ,r)
(while (< ,p ,q)
(() (error "something wrong"))))
,q))))
-(ew-line-generic-define)
+(ew-crlf-line-generic-define)
-(defmacro ew-line-convert-define ()
+(defmacro ew-crlf-line-convert-define ()
(let ((str (make-symbol "str"))
(others-fun (make-symbol "others-fun"))
(fold-fun (make-symbol "fold-fun"))
(result (make-symbol "result"))
(start (make-symbol "starx"))
(end (make-symbol "end")))
- `(defun ew-line-convert
+ `(defun ew-crlf-line-convert
(,str ,others-fun ,fold-fun ,crlf-fun ,bare-cr-fun ,bare-lf-fun)
(let ((,index 0) ,result)
- (when (> (ew-line-generic
+ (when (> (ew-crlf-line-generic
,str
,@(mapcar
(lambda (fun)
(substring ,str ,index))))
(apply 'concat (nreverse ,result))))))
-(ew-line-convert-define)
+(ew-crlf-line-convert-define)
+
+(defmacro ew-lf-line-generic-define ()
+ (let ((str (make-symbol "str"))
+ (others-fun (make-symbol "others-fun"))
+ (fold-fun (make-symbol "fold-fun"))
+ (lf-fun (make-symbol "lf-fun"))
+ (p (make-symbol "p"))
+ (q (make-symbol "q"))
+ (r (make-symbol "r")))
+ `(defun ew-lf-line-generic
+ (,str ,others-fun ,fold-fun ,lf-fun)
+ (let ((,p 0) (,q (length ,str)) ,r)
+ (while (< ,p ,q)
+ (setq ,r ,p)
+ (lex-scan-unibyte ,str ,p ,q
+ ((+ [^ "\n"]) (when ,others-fun (funcall ,others-fun ,r ,p)))
+ ((?\n [" \t"]) (when ,fold-fun (funcall ,fold-fun ,r ,p)))
+ ((?\n) (when ,lf-fun (funcall ,lf-fun ,r ,p)))
+ (() (error "something wrong"))))
+ ,q))))
+
+(ew-lf-line-generic-define)
+
+(defmacro ew-lf-line-convert-define ()
+ (let ((str (make-symbol "str"))
+ (others-fun (make-symbol "others-fun"))
+ (fold-fun (make-symbol "fold-fun"))
+ (lf-fun (make-symbol "lf-fun"))
+ (index (make-symbol "index"))
+ (result (make-symbol "result"))
+ (start (make-symbol "starx"))
+ (end (make-symbol "end")))
+ `(defun ew-lf-line-convert
+ (,str ,others-fun ,fold-fun ,lf-fun)
+ (let ((,index 0) ,result)
+ (when (> (ew-lf-line-generic
+ ,str
+ ,@(mapcar
+ (lambda (fun)
+ `(when ,fun
+ (lambda (,start ,end)
+ (when (< ,index ,start)
+ (setq ,result
+ (ew-rcons* ,result
+ (substring ,str ,index ,start))))
+ (setq ,result
+ (ew-rcons* ,result
+ (funcall ,fun
+ (substring ,str ,start ,end)))
+ ,index ,end))))
+ (list others-fun fold-fun lf-fun)))
+ ,index)
+ (setq ,result
+ (ew-rcons* ,result
+ (substring ,str ,index))))
+ (apply 'concat (nreverse ,result))))))
+
+(ew-lf-line-convert-define)
(list string start-column max-column must-unfold))
(or max-column
(setq max-column fill-column))
- (let ((c start-column)
- (tokens (eword-lexical-analyze string must-unfold))
- (result "")
- token)
- (while (and (setq token (car tokens))
- (setq tokens (cdr tokens)))
- (let* ((type (car token)))
- (if (eq type 'spaces)
- (let* ((next-token (car tokens))
- (next-str (eword-decode-token next-token))
- (next-len (string-width next-str))
- (next-c (+ c next-len 1)))
- (if (< next-c max-column)
- (setq result (concat result " " next-str)
- c next-c)
- (setq result (concat result "\n " next-str)
- c (1+ next-len)))
- (setq tokens (cdr tokens))
- )
- (let* ((str (eword-decode-token token)))
- (setq result (concat result str)
- c (+ c (string-width str)))
- ))))
- (if token
- (concat result (eword-decode-token token))
- result)))
+ (let* ((ew-decode-field-default-syntax '(ew-scan-unibyte-std11))
+ (decoded (ew-decode-field (make-string (1- start-column) ?X)
+ (ew-lf-crlf-to-crlf string)
+ (if must-unfold 'ew-cut-cr-lf)))
+ column)
+ (setq decoded (ew-crlf-to-lf decoded))
+ (setq column 0)
+ (ew-lf-line-convert decoded
+ (lambda (line)
+ (if (<= (length line) max-column)
+ line
+ (let ((start 0) index)
+ (catch 'loop
+ (while (< (+ column start) max-column)
+ (if (string-match " " decoded start)
+ (progn
+ (setq start (match-end 0))
+ (when (< (match-beginning 0) max-column)
+ (setq index (match-beginning 0))))
+ (throw 'loop nil)))
+ (setq index (string-match " " decoded start)))
+ (if index
+ (concat (substring decoded 0 index)
+ "\n"
+ (substring decoded index))
+ decoded))))
+ (lambda (str) (setq column 1) str)
+ (lambda (str) (setq column 0) str))))
(defun eword-decode-and-unfold-structured-field (string)
"Decode and unfold STRING as structured field body.
decode the charset included in it, it is not decoded."
(rotate-memo args-eword-decode-and-unfold-structured-field
(list string))
- (let ((tokens (eword-lexical-analyze string 'must-unfold))
- (result ""))
- (while tokens
- (let* ((token (car tokens))
- (type (car token)))
- (setq tokens (cdr tokens))
- (setq result
- (if (eq type 'spaces)
- (concat result " ")
- (concat result (eword-decode-token token))
- ))))
- result))
+ (let* ((ew-decode-field-default-syntax '(ew-scan-unibyte-std11))
+ (decoded (ew-decode-field ""
+ (ew-lf-crlf-to-crlf string)
+ 'ew-cut-cr-lf)))
+ (ew-cut-cr-lf decoded)))
(defun eword-decode-structured-field-body (string &optional must-unfold
start-column max-column)