Subject: 27.1.50; ruby-mode widens buffer when saving

From: Eric Hanchrow <[email-masked]>
Date: Sun, 20 Dec 2020 18:59:58 -0800

put the following three lines of ruby code into /tmp/wat.rb

#####
def hey_you
  puts "So what"
end
####

start emacs with "emacs -nw"
visit the file:
  C-x C-f /tmp/wat.rb RET
add some spaces to the end of the second line:
  C-n C-e SPC SPC SPC
narrow to that second line:
  C-SPC
  M-m
  C-x n n
save the buffer:
  C-x C-s

You'll see that the buffer is no longer narrowed.

The fix is to simply add a "save-restriction" in the function
`ruby-mode-set-encoding`, like this

    modified   lisp/progmodes/ruby-mode.el
    @@ -794,6 +794,7 @@ ruby--encoding-comment-required-p
     (defun ruby-mode-set-encoding ()
       "Insert a magic comment header with the proper encoding if
necessary."
       (save-excursion
    +    (save-restriction
           (widen)
           (goto-char (point-min))
           (when (ruby--encoding-comment-required-p)
    @@ -811,7 +812,7 @@ ruby-mode-set-encoding
                       (t (when ruby-insert-encoding-magic-comment
                            (ruby--insert-coding-comment coding-system))))
                 (when (buffer-modified-p)
    -            (basic-save-buffer-1)))))))
    +              (basic-save-buffer-1))))))))

     (defvar ruby--electric-indent-chars '(?. ?\) ?} ?\]))

In GNU Emacs 27.1.50 (build 2, x86_64-pc-linux-gnu)
 of 2020-12-04 built on ip-10-0-0-79
Repository revision: a4dd03ebe918ebc9cfbbec70d8c8910fae822b42
Repository branch: emacs-27
System Description: Amazon Linux AMI 2018.03

Recent messages:
Saving file /mnt3/git-repos/3rd-party/emacs/lisp/progmodes/ruby-mode.el...
Wrote /mnt3/git-repos/3rd-party/emacs/lisp/progmodes/ruby-mode.el
ruby-mode-set-encoding
Mark set
Saving file /tmp/wat.rb...
Wrote /tmp/wat.rb
Mark set [2 times]
Saving file /tmp/wat.rb...
Wrote /tmp/wat.rb
Mark saved where search started

Configured using:
 'configure --without-x'

Configured features:
SOUND NOTIFY INOTIFY LIBSELINUX GNUTLS LIBXML2 ZLIB XIM MODULES THREADS
PDUMPER GMP

Important settings:
  value of $LC_ALL: en_US.UTF-8
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Emacs-Lisp

Minor modes in effect:
  bug-reference-prog-mode: t
  magit-auto-revert-mode: t
  global-git-commit-mode: t
  async-bytecomp-package-mode: t
  projectile-mode: t
  savehist-mode: t
  elisp-slime-nav-mode: t
  shell-dirtrack-mode: t
  ws-butler-mode: t
  global-auto-revert-mode: t
  recentf-mode: t
  ido-everywhere: t
  tracking-mode: t
  override-global-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug sendmail log-view vc vc-dispatcher debug
backtrace cus-start cus-load eieio-opt speedbar sb-image ezimage dframe
rect gud ffap tabify man jka-compr ruby-mode smie iso-transl ix grapnel
bug-reference cl-print help-fns radix-tree mule-util dabbrev misearch
multi-isearch apropos perl6-detect lui-track-bar lui-track
magit-bookmark magit-submodule magit-obsolete magit-blame magit-stash
magit-reflog magit-bisect magit-push magit-pull magit-fetch magit-clone
magit-remote magit-commit magit-sequence magit-notes magit-worktree
magit-tag magit-merge magit-branch magit-reset magit-files magit-refs
magit-status magit magit-repos magit-apply magit-wip magit-log
which-func magit-diff smerge-mode diff magit-core magit-autorevert
magit-margin magit-transient magit-process magit-mode git-commit
transient magit-git magit-section magit-utils crm log-edit message
rfc822 mml mml-sec epa epg epg-config mm-decode mm-bodies mm-encode
mail-parse rfc2231 mailabbrev gmm-utils mailheader pcvs-util add-log
with-editor async-bytecomp async server projectile cursor-sensor
term/xterm xterm savehist twisted-mode org ob ob-tangle ob-ref ob-lob
ob-table ob-exp org-macro org-footnote org-src ob-comint org-pcomplete
org-list org-faces org-entities org-version ob-emacs-lisp ob-core
ob-eval org-table ol org-keys org-compat org-macs org-loaddefs
work-log-mode elisp-slime-nav etags fileloop generator bookmark
flymake-proc flymake mwheel warnings python tramp-sh tramp
tramp-loaddefs trampver tramp-integration files-x tramp-compat shell
pcomplete parse-time iso8601 ls-lisp edit-list pp browse-kill-ring
delsel eww mm-url gnus nnheader gnus-util rmail rmail-loaddefs rfc2047
rfc2045 ietf-drums time-date mail-utils mm-util mail-prsvr url-queue url
url-proxy url-privacy url-expand url-methods url-history mailcap shr
text-property-search url-cookie url-domsuf image svg xml dom format-spec
ws-butler wat derived autorevert vc-git hl-line ibuf-ext ibuffer
ibuffer-loaddefs ibuf-macs cl lsp-mode lsp-protocol xref project
url-util spinner pcase network-stream nsm rmc markdown-mode color
noutline outline lv inline imenu ht filenotify f s ewoc dash-functional
dash bindat dired-x dired dired-loaddefs grep compile comint ansi-color
find-func recentf tree-widget wid-edit ido circe-chanop diary-lib
diary-loaddefs cal-menu calendar cal-loaddefs circe-new-day-notifier
circe advice diff-mode lui-irc-colors irc gnutls puny lcs lui-format lui
tracking shorten thingatpt paren flyspell ispell ring circe-compat
edmacro kmacro adaptive-wrap cl-extra help-mode face-remap
alphabetize-json use-package use-package-ensure use-package-delight
use-package-diminish use-package-bind-key bind-key easy-mmode
use-package-core finder-inf gh-common marshal eieio-compat regexp-opt rx
info tool-bar package easymenu browse-url url-handlers url-parse
auth-source cl-seq eieio eieio-core cl-macs eieio-loaddefs
password-cache json subr-x map url-vars seq byte-opt gv bytecomp
byte-compile cconv cl-loaddefs cl-lib tooltip eldoc electric uniquify
ediff-hook vc-hooks lisp-float-type tabulated-list replace newcomment
text-mode elisp-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch timer select mouse jit-lock font-lock syntax
facemenu font-core term/tty-colors frame minibuffer cl-generic cham
georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice loaddefs button faces cus-face macroexp files
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote threads inotify
multi-tty make-network-process emacs)

Memory information:
((conses 16 2175918 349601)
 (symbols 48 57407 6)
 (strings 32 270955 36472)
 (string-bytes 1 7444009)
 (vectors 16 62351)
 (vector-slots 8 1111470 121082)
 (floats 8 3152 11089)
 (intervals 56 132518 8142)
 (buffers 1000 55)
 (heap 1024 88222 6475))