Resolve major mode for (cached)-nix-shell scripts

This commit is contained in:
Henrik Lissner 2020-02-08 07:14:40 -05:00
parent 014385a9f6
commit 49c7cf8172
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 23 additions and 0 deletions

View file

@ -39,3 +39,24 @@
((user-error "No search engine is enabled. Enable helm or ivy!"))) ((user-error "No search engine is enabled. Enable helm or ivy!")))
;; Tell lookup module to let us handle things from here ;; Tell lookup module to let us handle things from here
'deferred) 'deferred)
;;;###autoload
(defun +nix-shell-init-mode ()
"Resolve a (cached-)?nix-shell shebang to the correct major mode."
(save-excursion
(goto-char (point-min))
(save-match-data
(when (re-search-forward "#! *\\(?:cached-\\)?nix-shell +-i +\\([^ \n]+\\)" 256 t)
(let* ((interp (match-string 1))
(mode
(assoc-default
interp
(mapcar (lambda (e)
(cons (format "\\`%s\\'" (car e))
(cdr e)))
interpreter-mode-alist)
#'string-match-p)))
(when mode
(prog1 (set-auto-mode-0 mode)
(when (eq major-mode 'sh-mode)
(sh-set-shell interp)))))))))

View file

@ -1,6 +1,8 @@
;;; lang/nix/config.el -*- lexical-binding: t; -*- ;;; lang/nix/config.el -*- lexical-binding: t; -*-
(use-package! nix-mode (use-package! nix-mode
:interpreter ("cached-nix-shell" . +nix-shell-init-mode)
:interpreter ("nix-shell" . +nix-shell-init-mode)
:mode "\\.nix\\'" :mode "\\.nix\\'"
:config :config
(set-company-backend! 'nix-mode 'company-nixos-options) (set-company-backend! 'nix-mode 'company-nixos-options)