Guest User

Untitled

a guest
Aug 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. (defun nix-mode-make-regexp (parts)
  2. (declare (indent defun))
  3. (string-join parts "\\|"))
  4.  
  5. (defun nix-mode-search-backward ()
  6. (re-search-backward nix-mode-combined-regexp nil t))
  7.  
  8. (setq nix-mode-caps '(" =[ \n]" "\(" "\{" "\\[" "\\bwith " "\\blet\\b"))
  9. (setq nix-mode-ends '(";" "\)" "\\]" "\}" "\\bin\\b"))
  10. (setq nix-mode-quotes '("''" "\""))
  11. (setq nix-mode-caps-regexp (nix-mode-make-regexp nix-mode-caps))
  12. (setq nix-mode-ends-regexp (nix-mode-make-regexp nix-mode-ends))
  13. (setq nix-mode-quotes-regexp (nix-mode-make-regexp nix-mode-quotes))
  14. (setq nix-mode-combined-regexp (nix-mode-make-regexp (append nix-mode-caps nix-mode-ends nix-mode-quotes)))
  15.  
  16. (defun fixed-nix-indent-expression-start ()
  17. (let* ((ends 0)
  18. (once nil)
  19. (done nil)
  20. (indent (current-indentation)))
  21. (save-excursion
  22. ;; we want to indent this line, so we don't care what it contains
  23. ;; skip to the beginning so reverse searching doesn't find any matches within
  24. (beginning-of-line)
  25. ;; search backward until an unbalanced cap is found or no cap or end is found
  26. (while (and (not done) (nix-mode-search-backward))
  27. (cond
  28. ((looking-at nix-mode-quotes-regexp)
  29. ;; skip over strings entirely
  30. (re-search-backward nix-mode-quotes-regexp nil t))
  31. ((looking-at nix-mode-ends-regexp)
  32. ;; count the matched end
  33. ;; this means we expect to find at least one more cap
  34. (setq ends (+ ends 1)))
  35. ((looking-at nix-mode-caps-regexp)
  36. ;; we found at least one cap
  37. ;; this means our function will return true
  38. ;; this signals to the caller we handled the indentation
  39. (setq once t)
  40. (if (> ends 0)
  41. ;; this cap corresponds to a previously matched end
  42. ;; reduce the number of unbalanced ends
  43. (setq ends (- ends 1))
  44. ;; no unbalanced ends correspond to this cap
  45. ;; this means we have found the expression that contains our line
  46. ;; we want to indent relative to this line
  47. (setq indent (current-indentation))
  48. ;; signal that the search loop should exit
  49. (setq done t))))))
  50. ;; done is t when we found an unbalanced expression cap
  51. (when done
  52. ;; indent relative to the indentation of the expression containing our line
  53. (indent-line-to (+ tab-width indent)))
  54. ;; return t to the caller if we found at least one cap
  55. ;; this signals that we handled the indentation
  56. once))
  57.  
  58. (defun nix-mode-format ()
  59. "Format the entire nix-mode buffer"
  60. (interactive)
  61. (when (eq major-mode 'nix-mode)
  62. (save-excursion
  63. (beginning-of-buffer)
  64. (while (not (equal (point) (point-max)))
  65. (if (equal (string-match-p "^[\s-]*$" (thing-at-point 'line)) 0)
  66. (delete-horizontal-space)
  67. (nix-indent-line))
  68. (next-line)))))
  69.  
  70. (use-package nix-mode
  71. :straight (nix-mode :type git :host github :repo "NixOS/nix-mode")
  72. :config
  73. (add-to-list 'auto-mode-alist '("\\.nix?\\'" . nix-mode))
  74. (add-hook 'before-save-hook #'nix-mode-format)
  75. (define-key nix-mode-map (kbd "TAB") 'nix-indent-line)
  76. (setq nix-indent-function 'nix-indent-line)
  77. (defalias
  78. #'nix-indent-expression-start
  79. #'fixed-nix-indent-expression-start))
Add Comment
Please, Sign In to add comment