From 49c7cf8172093a19342d7581f0126f6898767cba Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sat, 8 Feb 2020 07:14:40 -0500 Subject: [PATCH] Resolve major mode for (cached)-nix-shell scripts --- modules/lang/nix/autoload.el | 21 +++++++++++++++++++++ modules/lang/nix/config.el | 2 ++ 2 files changed, 23 insertions(+) diff --git a/modules/lang/nix/autoload.el b/modules/lang/nix/autoload.el index bce624513..d0f950569 100644 --- a/modules/lang/nix/autoload.el +++ b/modules/lang/nix/autoload.el @@ -39,3 +39,24 @@ ((user-error "No search engine is enabled. Enable helm or ivy!"))) ;; Tell lookup module to let us handle things from here '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))))))))) diff --git a/modules/lang/nix/config.el b/modules/lang/nix/config.el index 8c3c3dc8c..f8c0a0a5e 100644 --- a/modules/lang/nix/config.el +++ b/modules/lang/nix/config.el @@ -1,6 +1,8 @@ ;;; lang/nix/config.el -*- lexical-binding: t; -*- (use-package! nix-mode + :interpreter ("cached-nix-shell" . +nix-shell-init-mode) + :interpreter ("nix-shell" . +nix-shell-init-mode) :mode "\\.nix\\'" :config (set-company-backend! 'nix-mode 'company-nixos-options)