>From db7c83f864971ce457ac8319d02c4579f3b76e0a Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Sat, 29 Oct 2022 17:30:13 +0200 Subject: [PATCH] Have 'rot13-region' gracefully handle read-only buffers * etc/NEWS: Mention new behaviour. * lisp/rot13.el (rot13-region): Use 'display-message-or-buffer' --- etc/NEWS | 4 ++++ lisp/rot13.el | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 19c9014116..750d9c76ba 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -828,6 +828,10 @@ wheel reports. Unlike 'pixel-scroll-mode', this mode scrolls the display pixel-by-pixel, as opposed to only animating line-by-line scrolls. +** 'rot13-region' handles read-only text +If a buffer is read-only, 'rot13-region' will display the "decrypted" +text in the echo area, or if necessary in a separate buffer. + ** Terminal Emacs --- diff --git a/lisp/rot13.el b/lisp/rot13.el index c063725de8..ba27a3a074 100644 --- a/lisp/rot13.el +++ b/lisp/rot13.el @@ -85,9 +85,19 @@ rot13-string ;;;###autoload (defun rot13-region (start end) - "ROT13 encrypt the region between START and END in current buffer." + "ROT13 encrypt the region between START and END in current buffer. +If invoked interactively and the buffer is read-only, the buffer +is either displayed in a the echo area if the text is short +enough (as determined by `resize-mini-windows' and +`max-mini-window-height'), or in a separate buffer." (interactive "r") - (translate-region start end rot13-translate-table)) + (condition-case nil + (translate-region start end rot13-translate-table) + (buffer-read-only + (when (called-interactively-p 'interactive) + (let* ((encrypted (buffer-substring-no-properties start end)) + (decrypted (rot13-string encrypted))) + (display-message-or-buffer decrypted "*Rot 13*")))))) ;;;###autoload (defun rot13-other-window () -- 2.38.0