-(defun q-encoding-encode-string-for-text (str)
- (mapconcat (function
- (lambda (chr)
- (cond ((eq chr 32) "_")
- ((or (< chr 32) (< 126 chr) (eq chr ?=))
- (quoted-printable-quote-char chr)
- )
- (t (char-to-string chr))
- )))
- str ""))
-
-(defun q-encoding-encode-string-for-comment (str)
- (mapconcat (function
- (lambda (chr)
- (cond ((eq chr 32) "_")
- ((or (< chr 32) (< 126 chr)
- (memq chr '(?= ?\( ?\) ?\\))
- )
- (quoted-printable-quote-char chr)
- )
- (t (char-to-string chr))
- )))
- str ""))
-
-(defun q-encoding-encode-string-for-phrase (str)
- (mapconcat (function
- (lambda (chr)
- (cond ((eq chr 32) "_")
- ((or (and (<= ?A chr)(<= chr ?Z))
- (and (<= ?a chr)(<= chr ?z))
- (and (<= ?0 chr)(<= chr ?9))
- (memq chr '(?! ?* ?+ ?- ?/))
- )
- (char-to-string chr)
- )
- (t (quoted-printable-quote-char chr))
- )))
- str ""))
-
-(defun q-encoding-encode-string (str &optional mode)
- (cond ((eq mode 'text)
- (q-encoding-encode-string-for-text str)
- )
- ((eq mode 'comment)
- (q-encoding-encode-string-for-comment str)
- )
- (t
- (q-encoding-encode-string-for-phrase str)
- )))
-
-(defun q-encoding-decode-string (str)
+(defconst q-encoding-special-chars-alist
+ '((text ?= ?? ?_)
+ (comment ?= ?? ?_ ?\( ?\) ?\\)
+ (phrase ?= ?? ?_ ?\( ?\) ?\\ ?\" ?# ?$ ?% ?& ?' ?, ?. ?/
+ ?: ?\; ?< ?> ?@ ?\[ ?\] ?^ ?` ?{ ?| ?} ?~)
+ ))
+
+(defun q-encoding-encode-string (string &optional mode)
+ "Encode STRING to Q-encoding of encoded-word, and return the result.
+MODE allows `text', `comment', `phrase' or nil. Default value is
+`phrase'."
+ (let ((specials (cdr (or (assq mode q-encoding-special-chars-alist)
+ (assq 'phrase q-encoding-special-chars-alist)
+ ))))
+ (mapconcat (function
+ (lambda (chr)
+ (cond ((eq chr ? ) "_")
+ ((or (< chr 32) (< 126 chr)
+ (memq chr specials)
+ )
+ (quoted-printable-quote-char chr)
+ )
+ (t
+ (char-to-string chr)
+ ))
+ ))
+ string "")
+ ))
+
+(defun q-encoding-decode-string (string)
+ "Decode STRING which is encoded in Q-encoding and return the result."