Function: quoted-printable-decode-region

quoted-printable-decode-region is an autoloaded, interactive and byte-compiled function defined in qp.el.gz.

Signature

(quoted-printable-decode-region FROM TO &optional CODING-SYSTEM)

Documentation

Decode quoted-printable in the region between FROM and TO, per RFC 2045.

If CODING-SYSTEM is non-nil, decode bytes into characters with that coding-system.

Interactively, you can supply the CODING-SYSTEM argument with C-x RET c (universal-coding-system-argument).

The CODING-SYSTEM argument is a historical hangover and is deprecated. QP encodes raw bytes and should be decoded into raw bytes. Decoding them into characters should be done separately.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/mail/qp.el.gz
;;; qp.el --- Quoted-Printable functions  -*- lexical-binding:t -*-

;; Copyright (C) 1998-2025 Free Software Foundation, Inc.

;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
;; Keywords: mail, extensions

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Functions for encoding and decoding quoted-printable text as
;; defined in RFC 2045.

;;; Code:

;;;###autoload
(defun quoted-printable-decode-region (from to &optional coding-system)
  "Decode quoted-printable in the region between FROM and TO, per RFC 2045.
If CODING-SYSTEM is non-nil, decode bytes into characters with that
coding-system.

Interactively, you can supply the CODING-SYSTEM argument
with \\[universal-coding-system-argument].

The CODING-SYSTEM argument is a historical hangover and is deprecated.
QP encodes raw bytes and should be decoded into raw bytes.  Decoding
them into characters should be done separately."
  (interactive
   ;; Let the user determine the coding system with "C-x RET c".
   (list (region-beginning) (region-end) coding-system-for-read))
  (when (and coding-system
	     (not (coding-system-p coding-system))) ; e.g. `ascii' from Gnus
    (setq coding-system nil))
  (save-excursion
    (save-restriction
      ;; RFC 2045:  ``An "=" followed by two hexadecimal digits, one
      ;; or both of which are lowercase letters in "abcdef", is
      ;; formally illegal. A robust implementation might choose to
      ;; recognize them as the corresponding uppercase letters.''
      (let ((case-fold-search t))
	(narrow-to-region from to)
	;; Do this in case we're called from Gnus, say, in a buffer
	;; which already contains non-ASCII characters which would
	;; then get doubly-decoded below.
	(if coding-system
	    (encode-coding-region (point-min) (point-max) coding-system))
	(goto-char (point-min))
	(while (and (skip-chars-forward "^=")
		    (not (eobp)))
	  (cond ((eq (char-after (1+ (point))) ?\n)
		 (delete-char 2))
		((looking-at "\\(=[0-9A-F][0-9A-F]\\)+")
		 ;; Decode this sequence at once; i.e. by a single
		 ;; deletion and insertion.
		 (let* ((n (/ (- (match-end 0) (point)) 3))
			(str (make-string n 0)))
		   (dotimes (i n)
                     (let ((n1 (char-after (1+ (point))))
                           (n2 (char-after (+ 2 (point)))))
                       (aset str i
                             (+ (* 16 (- n1 (if (<= n1 ?9) ?0
                                              (if (<= n1 ?F) (- ?A 10)
                                                (- ?a 10)))))
                                (- n2 (if (<= n2 ?9) ?0
                                        (if (<= n2 ?F) (- ?A 10)
                                          (- ?a 10)))))))
		     (forward-char 3))
		   (delete-region (match-beginning 0) (match-end 0))
		   (insert str)))
		(t
		 (message "Malformed quoted-printable text")
		 (forward-char)))))
      (if coding-system
	  (decode-coding-region (point-min) (point-max) coding-system)))))