[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/nix-mode e1f2e24cc4 104/500: Move separate modes into own
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/nix-mode e1f2e24cc4 104/500: Move separate modes into own files. |
Date: |
Sat, 29 Jan 2022 08:26:40 -0500 (EST) |
branch: elpa/nix-mode
commit e1f2e24cc4406521ff0ed0efb9ebde6f10074ad6
Author: Matthew Bauer <mjbauer95@gmail.com>
Commit: Matthew Bauer <mjbauer95@gmail.com>
Move separate modes into own files.
---
nix-company.el | 64 ++++++++++++++++++++++
nix-format.el | 36 +++++++++++++
nix-mode.el | 167 ++-------------------------------------------------------
nix-repl.el | 88 ++++++++++++++++++++++++++++++
4 files changed, 192 insertions(+), 163 deletions(-)
diff --git a/nix-company.el b/nix-company.el
new file mode 100644
index 0000000000..aa78f20b6c
--- /dev/null
+++ b/nix-company.el
@@ -0,0 +1,64 @@
+;;; nix-company.el --- Company support for Nix
+;; -*- lexical-binding: t -*-
+
+;; This file is NOT part of GNU Emacs.
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'nix-repl)
+
+(defun company-nix (command &optional arg &rest ignored)
+ (interactive '(interactive))
+ (case command
+ (interactive (company-begin-backend 'company-nix))
+ (prefix (and (member major-mode '(nix-mode nix-repl-mode))
+ (nix-grab-attr-path)))
+ (candidates
+ (nix-get-completions (get-buffer-process (nix--get-company-buffer)) arg))
+ (sorted t)))
+
+(defun nix-grab-attr-path ()
+ (if (looking-at "[^a-zA-Z0-9'\\-_\\.]")
+ (buffer-substring (point) (save-excursion (skip-chars-backward
"a-zA-Z0-9'\\-_\\.")
+ (point)))
+ (unless (and (char-after)
+ (string-match "[a-zA-Z0-9'\\-_]" (char-to-string
(char-after)))
+ ""))))
+
+(defun nix--get-company-buffer (&optional buffer)
+ (let* ((buf (or buffer (current-buffer)))
+ (repl-buf (get-buffer "*Nix-REPL*")))
+ (if (or (equal buf "*Nix-REPL*") (equal buf repl-buf))
+ repl-buf
+ (nix--get-company-backend-buffer buf))))
+
+(defvar nix-company-backend-buffer-name " *nix-company-backend*")
+(defvar nix--company-last-buffer nil)
+
+(defun nix--get-company-backend-buffer (buffer)
+ (let* ((buf-file (buffer-file-name buffer))
+ (backend-buf (get-buffer-create nix-company-backend-buffer-name))
+ (last-buf nix--company-last-buffer)
+ (proc (get-buffer-process backend-buf)))
+ (with-current-buffer buffer
+ (if (and proc
+ (process-live-p proc))
+ (if (not (string= last-buf (buffer-name)))
+ (progn (quit-process proc)
+ (nix--make-repl-in-buffer backend-buf)
+ (nix--send-repl (concat ":l " buf-file "\n")
+ (get-buffer-process backend-buf) t)
+ (setq nix--company-last-buffer (buffer-name)))
+ (nix--send-repl ":r\n" proc t))
+ (progn (nix--make-repl-in-buffer backend-buf)
+ (nix--send-repl (concat ":l " buf-file "\n")
+ (get-buffer-process backend-buf) t)
+ (setq nix--company-last-buffer (buffer-name))))
+ backend-buf)))
+
+(add-to-list 'company-backends 'company-nix)
+
+(provide 'nix-company)
+;;; nix-company.el ends here
diff --git a/nix-format.el b/nix-format.el
new file mode 100644
index 0000000000..d3bd430364
--- /dev/null
+++ b/nix-format.el
@@ -0,0 +1,36 @@
+;;; nix-format.el --- Nix formatter
+;; -*- lexical-binding: t -*-
+
+;; This file is NOT part of GNU Emacs.
+
+;;; Commentary:
+
+;;; Code:
+
+(defcustom nix-nixfmt-bin "nixfmt"
+ "Path to nixfmt executable."
+ :group 'nix
+ :type 'string)
+
+(defun nix--format-call (buf)
+ "Format BUF using nixfmt."
+ (with-current-buffer (get-buffer-create "*nixfmt*")
+ (erase-buffer)
+ (insert-buffer-substring buf)
+ (if (zerop (call-process-region (point-min) (point-max) nix-nixfmt-bin t t
nil))
+ (progn
+ (if (not (string= (buffer-string) (with-current-buffer buf
(buffer-string))))
+ (copy-to-buffer buf (point-min) (point-max)))
+ (kill-buffer))
+ (error "Nixfmt failed, see *nixfmt* buffer for details"))))
+
+(defun nix-format-buffer ()
+ "Format the current buffer using nixfmt."
+ (interactive)
+ (unless (executable-find nix-nixfmt-bin)
+ (error "Could not locate executable \"%s\"" nix-nixfmt-bin))
+ (nix--format-call (current-buffer))
+ (message "Formatted buffer with rustfmt."))
+
+(provide 'nix-format)
+;;; nix-format.el ends here
diff --git a/nix-mode.el b/nix-mode.el
index 3e361194ff..04af745bda 100644
--- a/nix-mode.el
+++ b/nix-mode.el
@@ -22,6 +22,9 @@
"Set variable VAR to value VAL in current buffer."
`(set (make-local-variable ',var) ,val)))
+(require 'nix-format)
+(require 'nix-repl)
+
;;; Syntax coloring
(defconst nix-keywords
@@ -232,139 +235,6 @@
)
start end))
-
-;;; REPL
-
-(defvar nix-prompt-regexp "nix-repl> ")
-
-(define-derived-mode nix-repl-mode comint-mode "Nix-REPL"
- "Interactive prompt for Nix."
- (setq-local comint-prompt-regexp nix-prompt-regexp)
- (setq-local comint-prompt-read-only t)
- (add-to-list 'company-backends 'company-nix)
- (company-mode))
-
-(defun nix-repl-show ()
- "Load the Nix-REPL."
- (interactive)
- (pop-to-buffer-same-window
- (get-buffer-create "*Nix-REPL*"))
- (unless (comint-check-proc (current-buffer))
- (nix--make-repl-in-buffer (current-buffer))
- (nix-repl-mode)))
-
-(defun nix--make-repl-in-buffer (buffer)
- (make-comint-in-buffer "Nix-REPL" buffer "nix-repl"))
-
-;;; Company
-
-(defun company-nix (command &optional arg &rest ignored)
- (interactive '(interactive))
- (case command
- (interactive (company-begin-backend 'company-nix))
- (prefix (and (member major-mode '(nix-mode nix-repl-mode))
- (nix-grab-attr-path)))
- (candidates
- (nix-get-completions (get-buffer-process (nix--get-company-buffer)) arg))
- (sorted t)))
-
-(defun nix-grab-attr-path ()
- (if (looking-at "[^a-zA-Z0-9'\\-_\\.]")
- (buffer-substring (point) (save-excursion (skip-chars-backward
"a-zA-Z0-9'\\-_\\.")
- (point)))
- (unless (and (char-after)
- (string-match "[a-zA-Z0-9'\\-_]" (char-to-string
(char-after)))
- ""))))
-
-(defun nix--get-company-buffer (&optional buffer)
- (let* ((buf (or buffer (current-buffer)))
- (repl-buf (get-buffer "*Nix-REPL*")))
- (if (or (equal buf "*Nix-REPL*") (equal buf repl-buf))
- repl-buf
- (nix--get-company-backend-buffer buf))))
-
-(defvar nix-company-backend-buffer-name " *nix-company-backend*")
-(defvar nix--company-last-buffer nil)
-
-(defun nix--get-company-backend-buffer (buffer)
- (let* ((buf-file (buffer-file-name buffer))
- (backend-buf (get-buffer-create nix-company-backend-buffer-name))
- (last-buf nix--company-last-buffer)
- (proc (get-buffer-process backend-buf)))
- (with-current-buffer buffer
- (if (and proc
- (process-live-p proc))
- (if (not (string= last-buf (buffer-name)))
- (progn (quit-process proc)
- (nix--make-repl-in-buffer backend-buf)
- (nix--send-repl (concat ":l " buf-file "\n")
- (get-buffer-process backend-buf) t)
- (setq nix--company-last-buffer (buffer-name)))
- (nix--send-repl ":r\n" proc t))
- (progn (nix--make-repl-in-buffer backend-buf)
- (nix--send-repl (concat ":l " buf-file "\n")
- (get-buffer-process backend-buf) t)
- (setq nix--company-last-buffer (buffer-name))))
- backend-buf)))
-
-(defun nix-get-completions (proc prefix)
- (save-match-data
- (nix--with-temp-process-filter proc
- (goto-char (point-min))
- (process-send-string proc (concat prefix "\t\"" (nix--char-with-ctrl ?a)
"\"\n"))
- (setq i 0)
- (while (and (< (setq i (1+ i)) 100)
- (not (search-forward-regexp "\"\\([^\"]*\\)\"[\n]*nix-repl>"
nil t)))
- (sleep-for 0.01))
- (let ((new-prefix (match-string 1))
- (start-compl (point)))
- (if (string-suffix-p " " new-prefix)
- (list (substring new-prefix 0 -1))
- (process-send-string proc (concat new-prefix "\t\t"
(nix--char-with-ctrl ?u) "\n"))
- (goto-char start-compl)
- (setq i 0)
- (while (and (< (setq i (1+ i)) 100)
- (not (search-forward-regexp
- "[\n]+nix-repl>\\|Display all \\([0-9]+\\)" nil
t)))
- (sleep-for 0.01))
- (if (match-string 1)
- (progn
- (process-send-string proc "n")
- '())
- (search-backward "\n" nil t)
- (split-string (buffer-substring start-compl (1- (point))))))))))
-
-(defun nix--send-repl (input &optional process mute)
- (let ((proc (or process (get-buffer-process (current-buffer)))))
- (if mute
- (nix--with-temp-process-filter proc
- (process-send-string proc input))
- (process-send-string proc input))))
-
-(defun nix--char-with-ctrl (char)
- (char-to-string (logand #b10011111 char)))
-
-(defmacro nix--with-temp-process-filter (proc &rest body)
- (declare (indent defun))
- `(let* ((buf (generate-new-buffer " *temp-process-output*"))
- (proc-filter-saved (process-filter ,proc))
- (proc-marker (with-current-buffer buf (point-marker))))
- (set-process-filter ,proc (nix--process-filter buf proc-marker))
- (unwind-protect
- (with-current-buffer buf
- ,@body)
- (set-process-filter ,proc proc-filter-saved)
- (kill-buffer buf))))
-
-(defun nix--process-filter (buf marker)
- (lambda (proc string)
- (when (buffer-live-p buf)
- (with-current-buffer buf
- (save-excursion
- (goto-char marker)
- (insert string)
- (set-marker marker (point)))))))
-
;;; Indentation
(defun nix-indent-level-parens ()
@@ -499,33 +369,6 @@
(if (looking-at nix-re-file-path)
(find-file (match-string-no-properties 0)))))
-;; Formatting
-
-(defcustom nix-nixfmt-bin "nixfmt"
- "Path to nixfmt executable."
- :group 'nix
- :type 'string)
-
-(defun nix--format-call (buf)
- "Format BUF using nixfmt."
- (with-current-buffer (get-buffer-create "*nixfmt*")
- (erase-buffer)
- (insert-buffer-substring buf)
- (if (zerop (call-process-region (point-min) (point-max) nix-nixfmt-bin t t
nil))
- (progn
- (if (not (string= (buffer-string) (with-current-buffer buf
(buffer-string))))
- (copy-to-buffer buf (point-min) (point-max)))
- (kill-buffer))
- (error "Nixfmt failed, see *nixfmt* buffer for details"))))
-
-(defun nix-format-buffer ()
- "Format the current buffer using nixfmt."
- (interactive)
- (unless (executable-find nix-nixfmt-bin)
- (error "Could not locate executable \"%s\"" nix-nixfmt-bin))
- (nix--format-call (current-buffer))
- (message "Formatted buffer with rustfmt."))
-
;; Key maps
(defvar nix-mode-menu (make-sparse-keymap "Nix")
@@ -552,9 +395,7 @@
(when (featurep 'flycheck) (require 'nix-flycheck))
-(when (require 'company nil 'noerror) (add-to-list
- 'company-backends
- 'company-nix))
+(when (require 'company nil 'noerror) (require 'nix-company))
;;;###autoload
(define-derived-mode nix-mode prog-mode "Nix"
diff --git a/nix-repl.el b/nix-repl.el
new file mode 100644
index 0000000000..d32b09b89e
--- /dev/null
+++ b/nix-repl.el
@@ -0,0 +1,88 @@
+;;; nix-repl.el --- Nix repl
+;; -*- lexical-binding: t -*-
+
+;; This file is NOT part of GNU Emacs.
+
+;;; Commentary:
+
+;;; Code:
+
+(defvar nix-prompt-regexp "nix-repl> ")
+
+(define-derived-mode nix-repl-mode comint-mode "Nix-REPL"
+ "Interactive prompt for Nix."
+ (setq-local comint-prompt-regexp nix-prompt-regexp)
+ (setq-local comint-prompt-read-only t))
+
+(defun nix-repl-show ()
+ "Load the Nix-REPL."
+ (interactive)
+ (pop-to-buffer-same-window
+ (get-buffer-create "*Nix-REPL*"))
+ (unless (comint-check-proc (current-buffer))
+ (nix--make-repl-in-buffer (current-buffer))
+ (nix-repl-mode)))
+
+(defun nix--make-repl-in-buffer (buffer)
+ (make-comint-in-buffer "Nix-REPL" buffer "nix-repl"))
+
+(defun nix-get-completions (proc prefix)
+ (save-match-data
+ (nix--with-temp-process-filter proc
+ (goto-char (point-min))
+ (process-send-string proc (concat prefix "\t\"" (nix--char-with-ctrl ?a)
"\"\n"))
+ (setq i 0)
+ (while (and (< (setq i (1+ i)) 100)
+ (not (search-forward-regexp "\"\\([^\"]*\\)\"[\n]*nix-repl>"
nil t)))
+ (sleep-for 0.01))
+ (let ((new-prefix (match-string 1))
+ (start-compl (point)))
+ (if (string-suffix-p " " new-prefix)
+ (list (substring new-prefix 0 -1))
+ (process-send-string proc (concat new-prefix "\t\t"
(nix--char-with-ctrl ?u) "\n"))
+ (goto-char start-compl)
+ (setq i 0)
+ (while (and (< (setq i (1+ i)) 100)
+ (not (search-forward-regexp
+ "[\n]+nix-repl>\\|Display all \\([0-9]+\\)" nil
t)))
+ (sleep-for 0.01))
+ (if (match-string 1)
+ (progn
+ (process-send-string proc "n")
+ '())
+ (search-backward "\n" nil t)
+ (split-string (buffer-substring start-compl (1- (point))))))))))
+
+(defun nix--send-repl (input &optional process mute)
+ (let ((proc (or process (get-buffer-process (current-buffer)))))
+ (if mute
+ (nix--with-temp-process-filter proc
+ (process-send-string proc input))
+ (process-send-string proc input))))
+
+(defun nix--char-with-ctrl (char)
+ (char-to-string (logand #b10011111 char)))
+
+(defmacro nix--with-temp-process-filter (proc &rest body)
+ (declare (indent defun))
+ `(let* ((buf (generate-new-buffer " *temp-process-output*"))
+ (proc-filter-saved (process-filter ,proc))
+ (proc-marker (with-current-buffer buf (point-marker))))
+ (set-process-filter ,proc (nix--process-filter buf proc-marker))
+ (unwind-protect
+ (with-current-buffer buf
+ ,@body)
+ (set-process-filter ,proc proc-filter-saved)
+ (kill-buffer buf))))
+
+(defun nix--process-filter (buf marker)
+ (lambda (proc string)
+ (when (buffer-live-p buf)
+ (with-current-buffer buf
+ (save-excursion
+ (goto-char marker)
+ (insert string)
+ (set-marker marker (point)))))))
+
+(provide 'nix-repl)
+;;; nix-repl.el ends here
- [nongnu] elpa/nix-mode a0fc6db303 365/500: Fix testcase file name., (continued)
- [nongnu] elpa/nix-mode a0fc6db303 365/500: Fix testcase file name., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode f77ae8fb54 380/500: Add more builtins, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode e4e604ae3a 389/500: Release 1.4.4, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 228f9f9d39 387/500: Release 1.4.3, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode bdfe3b3ae3 391/500: Fix nix-get-completions, add to nix-repl-mode, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 70af0efc9f 076/500: Fix antiquote closing brace., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 59e9ca0abd 084/500: Improve fontification, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 68793d91c5 107/500: Add require 'cl for case statements., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 2221a09aea 108/500: Require 'cl for "case"., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode e4bc711d81 094/500: Add some more modes., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode e1f2e24cc4 104/500: Move separate modes into own files.,
ELPA Syncer <=
- [nongnu] elpa/nix-mode 56a87c8d81 087/500: Move nix-flycheck to separate file., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 199e20413e 102/500: Ignore comments for hanging let's., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode fcbaf3e054 106/500: Rename flycheck mode to "nix-flycheck"., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 542ae77358 110/500: buffer-substring can't bet <1., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 3e6fdd8311 112/500: Add back syntax coloring of antiquotes., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 66cc5478de 113/500: Merge branch 'master' into separate-files, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 0fd152e118 119/500: Merge pull request #22 from ljli/various, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 04a268a6fe 126/500: Remove "let" handling of indent., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 1a4c98fcf7 118/500: Cosmetics, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 23bdbdf21f 124/500: Add custom mmm-mode, ELPA Syncer, 2022/01/29