If you want this to act in any buffer, not just a file-visiting buffer then find-file-hook
is not appropriate. (You said "all buffers", but you also spoke of editable/non-editable "files".)
If you want it to work in all buffers then this is one solution:
(defun my-show-trailing-ws ()
"Show trailing whitespace in the current buffer, unless it is read-only."
(setq-local show-trailing-whitespace (not buffer-read-only)))
(add-hook 'post-command-hook 'my-show-trailing-ws)
You can wrap that in a minor mode command, if you like.
Another possibility is to use an idle timer, but post-command-hook
seems fine for what you're looking for.
As far as I know, there is no hook that corresponds to a change in buffer-read-only
. However, if you use Emacs 26 or later then you can use function add-variable-watcher
to turn on/off showing trailing whitespace whenever variable buffer-read-only
is changed.
(add-variable-watcher 'buffer-read-only 'foo) ; Add watcher `foo'
(defun foo (symbol newval operation where) ; 100% untested...
"Show trailing whitespace in the current buffer, unless it is read-only."
(when (and (eq symbol 'buffer-read-only)
(memq operation '(set let))
(eq where (current-buffer)))
(setq-local show-trailing-whitespace (not newval))))