Function: allout-toggle-subtree-encryption
allout-toggle-subtree-encryption is an interactive and byte-compiled
function defined in allout.el.gz.
Signature
(allout-toggle-subtree-encryption &optional KEYMODE-CUE)
Documentation
Encrypt clear text or decrypt encoded topic contents (body and subtopics.)
Entry encryption defaults to symmetric key mode unless keypair
recipients are associated with the file (see
epa-file-encrypt-to) or the function is invoked with a
(KEYMODE-CUE) universal argument greater than 1.
When encrypting, KEYMODE-CUE universal argument greater than 1 causes prompting for recipients for public-key keypair encryption. Selecting no recipients results in symmetric key encryption.
Further, encrypting with a KEYMODE-CUE universal argument greater than 4 - eg, preceded by a doubled Ctrl-U - causes association of the specified recipients with the file, replacing those currently associated with it. This can be used to dissociate any recipients with the file, by selecting no recipients in the dialog.
Encryption and decryption uses the Emacs epg library.
Encrypted text will be ascii-armored.
See allout-toggle-current-subtree-encryption for more details.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/allout.el.gz
;;;_ > allout-toggle-subtree-encryption (&optional keymode-cue)
(defun allout-toggle-subtree-encryption (&optional keymode-cue)
"Encrypt clear text or decrypt encoded topic contents (body and subtopics.)
Entry encryption defaults to symmetric key mode unless keypair
recipients are associated with the file (see
`epa-file-encrypt-to') or the function is invoked with a
\(KEYMODE-CUE) universal argument greater than 1.
When encrypting, KEYMODE-CUE universal argument greater than 1
causes prompting for recipients for public-key keypair
encryption. Selecting no recipients results in symmetric key
encryption.
Further, encrypting with a KEYMODE-CUE universal argument greater
than 4 - eg, preceded by a doubled Ctrl-U - causes association of
the specified recipients with the file, replacing those currently
associated with it. This can be used to dissociate any
recipients with the file, by selecting no recipients in the
dialog.
Encryption and decryption uses the Emacs `epg' library.
Encrypted text will be ascii-armored.
See `allout-toggle-current-subtree-encryption' for more details."
(interactive "P")
(save-excursion
(allout-end-of-prefix t)
(if (= allout-recent-depth 1)
(error (concat "Cannot encrypt or decrypt level 1 topics -"
" shift it in to make it encryptable")))
(let* ((allout-buffer (current-buffer))
;; for use with allout-auto-save-temporarily-disabled, if necessary:
(was-buffer-saved-size buffer-saved-size)
;; Assess location:
(bullet-pos allout-recent-prefix-beginning)
(after-bullet-pos (point))
(was-encrypted
(progn (if (= (point-max) after-bullet-pos)
(error "No body to encrypt"))
(allout-encrypted-topic-p)))
(was-collapsed (if (not (search-forward "\n" nil t))
nil
(backward-char 1)
(allout-hidden-p)))
(subtree-beg (1+ (point)))
(subtree-end (allout-end-of-subtree))
(subject-text (buffer-substring-no-properties subtree-beg
subtree-end))
(subtree-end-char (char-after (1- subtree-end)))
(subtree-trailing-char (char-after subtree-end))
;; kluge -- result-text needs to be nil, but we also want to
;; check for the error condition
(result-text (if (or (string= "" subject-text)
(string= "\n" subject-text))
(error "No topic contents to %scrypt"
(if was-encrypted "de" "en"))
nil))
;; Assess key parameters:
(was-coding-system buffer-file-coding-system))
(when (not was-encrypted)
;; ensure that non-ascii chars pending encryption are noticed before
;; they're encrypted, so the coding system is set to accommodate
;; them.
(setq buffer-file-coding-system
(select-safe-coding-system subtree-beg subtree-end))
;; if the coding system for the text being encrypted is different
;; from that prevailing, then there a real risk that the coding
;; system can't be noticed by emacs when the file is visited. to
;; mitigate that, offer to preserve the coding system using a file
;; local variable.
(if (and (not (equal buffer-file-coding-system
was-coding-system))
(yes-or-no-p
(format (concat "Register coding system %s as file local"
" var? Necessary when only encrypted text"
" is in that coding system. ")
buffer-file-coding-system)))
(allout-adjust-file-variable "buffer-file-coding-system"
buffer-file-coding-system)))
(setq result-text
(allout-encrypt-string subject-text was-encrypted
(current-buffer) keymode-cue))
;; Replace the subtree with the processed product.
(allout-unprotected
(progn
(set-buffer allout-buffer)
(delete-region subtree-beg subtree-end)
(insert result-text)
(if was-collapsed
(allout-flag-region (1- subtree-beg) (point) t))
;; adjust trailing-blank-lines to preserve topic spacing:
(if (not was-encrypted)
(if (and (= subtree-end-char ?\n)
(= subtree-trailing-char ?\n))
(insert subtree-trailing-char)))
;; Ensure that the item has an encrypted-entry bullet:
(if (not (string= (buffer-substring-no-properties
(1- after-bullet-pos) after-bullet-pos)
allout-topic-encryption-bullet))
(progn (goto-char (1- after-bullet-pos))
(delete-char 1)
(insert allout-topic-encryption-bullet)))
(if was-encrypted
;; Remove the is-encrypted bullet qualifier:
(progn (goto-char after-bullet-pos)
(delete-char 1))
;; Add the is-encrypted bullet qualifier:
(goto-char after-bullet-pos)
(insert "*"))))
;; adjust buffer's auto-save eligibility:
(if was-encrypted
(allout-inhibit-auto-save-info-for-decryption was-buffer-saved-size)
(allout-maybe-resume-auto-save-info-after-encryption))
(run-hook-with-args 'allout-structure-added-functions
bullet-pos subtree-end))))