Function: mail-unquote-printable-region

mail-unquote-printable-region is an autoloaded, interactive and byte-compiled function defined in mail-utils.el.gz.

Signature

(mail-unquote-printable-region BEG END &optional WRAPPER NOERROR UNIBYTE)

Documentation

Undo the "quoted printable" encoding in buffer from BEG to END.

If the optional argument WRAPPER is non-nil, we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=. On encountering malformed quoted-printable text, exits with an error, unless NOERROR is non-nil, in which case it continues, and returns nil when finished. Returns non-nil on successful completion. If UNIBYTE is non-nil, insert converted characters as unibyte. That is useful if you are going to character code decoding afterward, as Rmail does.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/mail/mail-utils.el.gz
;; FIXME Gnus for some reason has `quoted-printable-decode-region' in qp.el.
;;;###autoload
(defun mail-unquote-printable-region (beg end &optional wrapper noerror
					  unibyte)
  "Undo the \"quoted printable\" encoding in buffer from BEG to END.
If the optional argument WRAPPER is non-nil,
we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=.
On encountering malformed quoted-printable text, exits with an error,
unless NOERROR is non-nil, in which case it continues, and returns nil
when finished.  Returns non-nil on successful completion.
If UNIBYTE is non-nil, insert converted characters as unibyte.
That is useful if you are going to character code decoding afterward,
as Rmail does."
  ;; FIXME: `unibyte' should always be non-nil, and the iso-latin-1
  ;; specific handling should be removed (or moved elsewhere and generalized).
  (interactive "r\nP")
  (let (failed)
    (save-match-data
      (save-excursion
	(save-restriction
	  (narrow-to-region beg end)
	  (goto-char (point-min))
	  (when (and wrapper
		     (looking-at "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?"))
	    (delete-region (match-end 1) end)
	    (delete-region (point) (match-beginning 1)))
	  (while (re-search-forward "=\\(\\([0-9A-F][0-9A-F]\\)\\|[=\n]\\|..\\)" nil t)
	    (goto-char (match-end 0))
	    (cond ((= (char-after (match-beginning 1)) ?\n)
		   (replace-match ""))
		  ((= (char-after (match-beginning 1)) ?=)
		   (replace-match "="))
		  ((match-beginning 2)
		   (let ((char (+ (* 16 (mail-unquote-printable-hexdigit
					 (char-after (match-beginning 2))))
				  (mail-unquote-printable-hexdigit
				   (char-after (1+ (match-beginning 2)))))))
		     (if unibyte
			 (progn
			   (replace-match "")
			   ;; insert-byte will insert this as a
			   ;; corresponding eight-bit character.
			   (insert-byte char 1))
		       (replace-match (make-string 1 char) t t))))
		  (noerror
		   (setq failed t))
		  (t
		   (error "Malformed MIME quoted-printable message"))))
	  (not failed))))))