0

What's the preferred way to not enter lsp-mode for certain buffers such as Emacs' backup files named for instance, app.c.~304fb1fe3650a354c4da16352f6c7be8bffd88cb~.

I still want such files to open in c-mode or c-ts-mode.

Personally, I believe that backup files should not enter lsp-mode by default.

This is my attempt so far but it doesn't work:

(defun lsp-disable-in-backup-buffer ()
  (let ((name (buffer-name)))
    (when (backup-file-name-p name)
      (lsp-mode -1)
      (message "Disabled `lsp-mode' in backup buffer %s"
               (faze name 'buffer)))))
(add-hook 'lsp-after-open-hook 'lsp-disable-in-backup-buffer)

Currently I'm enabling lsp-mode via the prog-mode-hook as

(use-package lsp-mode
  :ensure t
  :hook (prog-mode . lsp-deferred)
)

2 Answers 2

0

Simply don't call lsp-deferred when the file is a backup file:

(defun my-lsp-filter ()
  (unless (backup-file-name-p buffer-file-name)
    (lsp-deferred)))

(use-package lsp-mode
  :ensure t
  :hook (prog-mode . my-lsp-filter))

I haven't tested it, but I guess this should do the trick.

0

Using

(defun lsp-disable-in-backup-buffer ()
  (let ((name (buffer-name)))
    (when (backup-file-name-p name)
      (lsp-mode -1)
      (message "Disabled `lsp-mode' in backup buffer %s"
               (faze name 'buffer)))))
(add-hook 'lsp-after-open-hook 'lsp-disable-in-backup-buffer)

Currently I'm enabling lsp-mode via the prog-mode-hook as

(use-package lsp-mode
  :ensure t
  :hook (prog-mode . lsp-deferred)
)

suffices for now.

1
  • Wjhat's the difference between the code in the answer and the code in the question?
    – NickD
    Feb 7 at 20:00

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.