(defun my/buffer-has-major-mode-p (major-mode _sym buffer) "Return t if BUFFER has MAJOR-MODE set." (eq (buffer-local-value 'major-mode buffer) major-mode)) (defun my/restrict-symbol (mode symbols) "Restrict SYMBOLS to a certain major MODE. Ideally, packages should restrict their own `interactive' commands to their own mode (see the `interactive' help). However, this is not common practice so this little facility makes it easier to restrict symbols to a certain mode. Meaning, the command will not appear in the M-x menu as a possible completion. This may be handy if a command may be destructive for a major mode it wasn't meant for. Example: (my/restrict-symbol 'ledger-mode '(ledger-occur))" (dolist (sym symbols) (put sym 'completion-predicate (apply-partially #'my/buffer-has-major-mode-p mode))))