Function: epa-decrypt-region

epa-decrypt-region is an autoloaded, interactive and byte-compiled function defined in epa.el.gz.

Signature

(epa-decrypt-region START END &optional MAKE-BUFFER-FUNCTION)

Documentation

Decrypt the current region between START and END.

If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer. It should return that buffer. If it copies the input, it should delete the text now being decrypted. It should leave point at the proper place to insert the plaintext.

Be careful about using this command in Lisp programs! Since this function operates on regions, it does some tricks such as coding-system detection and unibyte/multibyte conversion. If you are sure how the data in the region should be treated, you should consider using the string based counterpart epg-decrypt-string, or the file based counterpart epg-decrypt-file instead.

For example:

(let ((context (epg-make-context 'OpenPGP)))
  (decode-coding-string
    (epg-decrypt-string context (buffer-substring start end))
    'utf-8))

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/epa.el.gz
;;;###autoload
(defun epa-decrypt-region (start end &optional make-buffer-function)
  "Decrypt the current region between START and END.

If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
It should return that buffer.  If it copies the input, it should
delete the text now being decrypted.  It should leave point at the
proper place to insert the plaintext.

Be careful about using this command in Lisp programs!
Since this function operates on regions, it does some tricks such
as coding-system detection and unibyte/multibyte conversion.  If
you are sure how the data in the region should be treated, you
should consider using the string based counterpart
`epg-decrypt-string', or the file based counterpart
`epg-decrypt-file' instead.

For example:

\(let ((context (epg-make-context \\='OpenPGP)))
  (decode-coding-string
    (epg-decrypt-string context (buffer-substring start end))
    \\='utf-8))"
  (interactive "r")
  (save-excursion
    (let ((context (epg-make-context epa-protocol))
	  plain)
      (epg-context-set-passphrase-callback context
					   #'epa-passphrase-callback-function)
      (epg-context-set-progress-callback context
					 (cons
					  #'epa-progress-callback-function
					  "Decrypting..."))
      (message "Decrypting...")
      (condition-case error
	  (setq plain (epg-decrypt-string context (buffer-substring start end)))
	(error
	 (epa-display-error context)
	 (signal (car error) (cdr error))))
      (message "Decrypting...done")
      (setq plain (decode-coding-string
		   plain
		   (or coding-system-for-read
		       (get-text-property start 'epa-coding-system-used)
		       'undecided)))
      (if make-buffer-function
	  (with-current-buffer (funcall make-buffer-function)
	    (let ((inhibit-read-only t))
	      (insert plain)))
	(if (or (eq epa-replace-original-text t)
                (and epa-replace-original-text
                     (y-or-n-p "Replace the original text? ")))
	    (let ((inhibit-read-only t))
	      (delete-region start end)
	      (goto-char start)
	      (insert plain))
	  (with-output-to-temp-buffer "*Temp*"
	    (set-buffer standard-output)
	    (insert plain)
	    (epa-info-mode))))
      (if (epg-context-result-for context 'verify)
	  (epa-display-info (epg-verify-result-to-string
			     (epg-context-result-for context 'verify)))))))