0

I just discovered narrowing in Emacs, and it's great to be able to focus on a single function or block of code.

However, after I narrow with C-x n n, make some changes and then save manually with C-x C-s the buffer widens again while I am in Ruby mode.

How do I prevent save from triggering widening? (I can see how it's handy, but I'd like to turn it off for now.)

Thank you StackExchange!

2 Answers 2

2

I also tried [ (remove-hook 'after-save-hook 'ruby-mode-set-encoding) ], but it didn't work since, I think, the hook is buffer local?

Both add-hook and remove-hook have a LOCAL argument for modifying the buffer-local hook value. If that's how it's added, that's also how you can remove it (most likely via ruby-mode-hook).

And indeed if we look at ruby-mode itself this is exactly the case:

(define-derived-mode ruby-mode prog-mode "Ruby"
[...]
  (add-hook 'after-save-hook #'ruby-mode-set-encoding nil 'local)
  (add-hook 'electric-indent-functions #'ruby--electric-indent-p nil 'local)
  (add-hook 'flymake-diagnostic-functions #'ruby-flymake-auto nil 'local)

So you would then do this:

(add-hook 'ruby-mode-hook #'my-ruby-mode-hook)

(defun my-ruby-mode-hook ()
  "Custom `ruby-mode' behaviours."
  (remove-hook 'after-save-hook #'ruby-mode-set-encoding 'local))

That said, I imagine ruby-mode-set-encoding is doing something useful, and you probably don't actually want to inhibit that.

This looks to me like a bug in ruby-mode-set-encoding. You should M-x report-emacs-bug to suggest that save-restriction is used in that function. Maybe there's something else going on which needs the widening to happen, but if not then it seems like an easy fix.

1
  • Works brilliantly. Thanks you @phils. I think they do use save-restriction in that code, but the issue persists so I'll report it. Thanks for teaching me how to report it.
    – may
    Commented Jan 31, 2022 at 20:48
1

I eventually traced this to ruby-mode-set-encoding in ruby-mode.el.

I worked around it with the following in my .emacs:

(with-eval-after-load 'ruby-mode 
  (defun ruby-mode-set-encoding () "override the default" ()))

It's a terrible hack, but it allows me to move on with my daily work and I can disable it occasionally to make sure magic comments are still present.

I also tried this, but it didn't work since, I think, the hook is buffer local?

(with-eval-after-load 'ruby-mode
  (remove-hook 'after-save-hook 'ruby-mode-set-encoding))    

Open to ideas on how to fix this right, but thought I'd leave something for anyone who stumbles across this issue.

1
  • Both add-hook and remove-hook have a LOCAL argument for modifying the buffer-local hook value. If that's how it's added, that's also how you can remove it (most likely via ruby-mode-hook).
    – phils
    Commented Jan 29, 2022 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.