Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coalton mode #1093

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
Move emacs files to a separate dir; start addind process integration
jbouwman committed Apr 23, 2024

Verified

This commit was signed with the committer’s verified signature.
gorhill Raymond Hill
commit 79592c91444e4539c4cee55e58efd8698487a727
57 changes: 51 additions & 6 deletions coalton-mode.el → emacs/coalton-mode.el
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
;;; coalton-mode.el --- Major mode for working with Coalton -*- lexical-binding: t; -*-
;;
;; This file contains functions for in-Emacs structural operations on
;; Coalton code, including syntax highlighting, indentation and
;; navigation, and command integration with the in-CL operations
;; defined in `inferior-coalton.el'.

(require 'treesit)

(defvar coalton-ts-repo
"https://github.com/jbouwman/tree-sitter-coalton.git")

(defvar coalton-mode-map
(make-sparse-keymap))

@@ -30,6 +38,7 @@
(defvar coalton--debug t
"Enable debugging.")


;; Fontification

(defun coalton--font-lock-settings ()
@@ -47,23 +56,57 @@
:language 'coalton
'((comment) @font-lock-comment-face)))


;; Indentation

(defun coalton--indent-rules ()
"Return rules for `treesit-simple-indent-rules'."
`((coalton
((parent-is "list") parent 2))))

;; Mode initialization

;; Indexing and navigation

(defun node-type-p (type node)
"Does NODE have tree-sitter TYPE?"
(string-equal type (treesit-node-type node)))

(defun coalton--list-p (node)
"Is NODE a list?"
(node-type-p "list" node))

(defun coalton--symbol-p (node)
"Is NODE a symbol?"
(node-type-p "symbol" node))

(defun coalton--symbol-name (node)
"Get the name of a symbol NODE."
(treesit-node-text (treesit-node-child-by-field-name node "name")))

(defun coalton--symbol-name-p (name node)
"Is NODE a symbol named NAME?"
(and (coalton--symbol-p node)
(string-equal name (coalton--symbol-name node))))

(defun coalton--definition-p (type node)
"Is NODE a definition of TYPE?"
(and (coalton--list-p node)
(coalton--symbol-p type (treesit-node-child node 0 t))))

(defvar coalton--imenu-settings
`(("Definition" "list" coalton--definition-p))
"The value for `treesit-simple-imenu-settings'.")


;; Initialization

(defun coalton--load-grammar ()
"Install grammar."
(let ((grammars '((coalton "https://github.com/jbouwman/tree-sitter-coalton.git" "main"))))
(let ((grammars `((coalton ,coalton-ts-repo "main"))))
(dolist (grammar grammars)
(let ((language (car grammar)))
(unless (treesit-language-available-p language nil)
(let ((treesit-language-source-alist grammars))
(treesit-install-language-grammar language)))))))
(unless (treesit-language-available-p (car grammar) nil)
(let ((treesit-language-source-alist grammars))
(treesit-install-language-grammar language))))))

(defun coalton-mode-variables ()
"Initialize buffer-local vars."
@@ -96,3 +139,5 @@
(treesit-major-mode-setup)))

(add-to-list 'auto-mode-alist '("\\.coalton\\'" . coalton-mode))

(provide 'coalton-mode)
37 changes: 37 additions & 0 deletions emacs/inferior-coalton.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
;;; inferior-coalton.el --- coalton-mode lisp integration -*- lexical-binding: t; -*-
;;
;; Slime extension via `define-slime-contrib' for interactive with a
;; Coalton instance running in a Slime/Swank-managed Lisp subprocess.

(require 'slime)

(defun slime-coalton-mode-hook ()
(slime-mode 1))

(defun slime-coalton-indentation-update (symbol indent packages)
;; Does the symbol have an indentation value that we set?
(when (equal (get symbol 'coalton-indent-function)
(get symbol 'slime-coalton-indent))
(put symbol 'slime-coalton-indent indent)
(put symbol 'coalton-indent-function indent)))


;;; Initialization

(defun slime-coalton-init ()
(add-hook 'coalton-mode-hook 'slime-coalton-mode-hook)
(add-to-list 'slime-lisp-modes 'coalton-mode))

(defun slime-coalton-unload ()
(remove-hook 'coalton-mode-hook 'slime-coalton-mode-hook)
(setq slime-lisp-modes (remove 'coalton-mode slime-lisp-modes)))

(define-slime-contrib slime-coalton
"Support Coalton language"
(:authors "Jesse Bouwman <[email protected]>")
(:slime-dependencies slime-coalton)
(:swank-dependencies swank-coalton)
(:on-load
(slime-coalton-init)))

(provide 'coalton)