The variable mode-name
is a buffer-local variable that is also bound inside your function sandric/kill-buffers-by-major-mode
. Under dynamic scoping, that means that the value of mode-name
seen by your code will change when you change buffers (in this case, when you kill the current buffer). See the warning here about let
bindings; it also applies to function arguments.
This problem goes away for me if the function is defined using lexical scope, but according to the manual, this is undefined behavior and using a special variable such as mode-name
as a function argument is discouraged.
So the correct solution is just to change the name of your function argument from mode-name
to another symbol that is not defined as buffer-local.
(Thanks to Tobias for explaining the details of the problematic interaction in the comments; my previous explanation was very sketchy, and I made some unfounded assumptions about what was causing the problem.)