;;; sex-lsp.el --- Language server client for Sex -*- lexical-binding: t -*- ;; Copyright (C) 2025-2026 alex-eg ;; Author: alex-eg ;; Version: 0.1.0 ;; Package-Requires: ((emacs "28.1")) ;; Keywords: languages ;; URL: https://git.kotobank.ch/alex-eg/sex ;; This file is not part of GNU Emacs. ;;; Commentary: ;; Client for `sex-lsp', the Sex language server. ;; `sex-c-backend' set to `clangd' asks the server for C headers too. ;;; Code: (require 'sex-mode) (require 'thingatpt) (defgroup sex-lsp nil "Language server client for Sex." :prefix "sex-" :group 'sex-mode) (defcustom sex-lsp-program "sex-lsp" "Program that speaks LSP for Sex." :type 'string :group 'sex-lsp) (defcustom sex-sexc-program "sexc" "sexc, used by sexpand when no language server is running." :type 'string :group 'sex-lsp) (defcustom sex-c-backend nil "C header backend for `sex-lsp'. nil leaves C names to the C compiler. `clangd' asks clangd." :type '(choice (const nil) (const clangd)) :group 'sex-lsp) (defcustom sex-cflags nil "C compiler flags for the clangd probe, as one string of words." :type '(choice (const nil) string) :group 'sex-lsp) (defun sex-lsp-command (&optional _interactive) "The `sex-lsp' command line, including the C backend when one is set." (append (list sex-lsp-program) (and (eq sex-c-backend 'clangd) '("--c-backend=clangd")) (and sex-cflags (not (string-empty-p sex-cflags)) (list (concat "--cflags=" sex-cflags))))) (define-key sex-mode-map (kbd "C-c C-e") #'sexpand) (define-key sex-mode-map (kbd "C-c C-d") #'sexpand-defun) (define-key sex-mode-map (kbd "C-c C-b") #'sexpand-buffer) (define-key sex-mode-map (kbd "C-c C-r") #'sexpand-region) (defun sex-lsp-running-p () "Whether this buffer has an eglot or lsp-mode server." (or (and (fboundp 'eglot-current-server) (eglot-current-server)) (and (fboundp 'lsp-workspaces) (lsp-workspaces)))) (defun sex-buffer-uri () "The LSP URI of the current buffer." (cond ((and (fboundp 'eglot-path-to-uri) buffer-file-name) (eglot-path-to-uri buffer-file-name)) ((and (fboundp 'lsp--buffer-uri) (lsp--buffer-uri))) (buffer-file-name (concat "file://" buffer-file-name)))) (defun sex--field (object key) "KEY's value in a plist or hash-table OBJECT. KEY is a symbol." (cond ((hash-table-p object) (gethash (symbol-name key) object)) ((listp object) (plist-get object (intern (concat ":" (symbol-name key))))))) (defun sex-lsp-request (method params) "Send METHOD to whichever LSP client is managing this buffer. METHOD is a string, such as \"sex/expand\". PARAMS is a plist." (cond ((and (fboundp 'eglot-current-server) (eglot-current-server)) (jsonrpc-request (eglot-current-server) (intern (concat ":" method)) params)) ((and (fboundp 'lsp-workspaces) (lsp-workspaces)) (lsp-request method params)))) (defun sex-show-c (text) "Show TEXT in `*sexpand*' under a C mode." (let ((buffer (get-buffer-create "*sexpand*"))) (with-current-buffer buffer (let ((inhibit-read-only t)) (erase-buffer) (insert text) (goto-char (point-min)) (if (fboundp 'c-ts-mode) (c-ts-mode) (c-mode)))) (pop-to-buffer buffer))) (defun sex-expand-via-server (start end) "Ask the language server for the C of lines START through END. END nil is the whole buffer." (let ((uri (sex-buffer-uri))) (when (and uri (sex-lsp-running-p)) (let ((params (list :textDocument (list :uri uri) :startLine start))) (when end (setq params (append params (list :endLine end)))) (sex--field (sex-lsp-request "sex/expand" params) 'text))))) (defun sex-expand-via-cli (start end) "Run sexc --expand-lines on the buffer. END nil is the whole buffer." (let ((spec (if end (format "--expand-lines=%d:%d" start end) "--expand-lines=1")) (output (generate-new-buffer " *sexpand*"))) (unwind-protect (when (eq 0 (call-process-region (point-min) (point-max) sex-sexc-program nil output nil spec "-")) (with-current-buffer output (buffer-string))) (kill-buffer output)))) (defun sex-expand-lines (start end) "Show the C generated for lines START through END." (let ((text (or (ignore-errors (sex-expand-via-server start end)) (sex-expand-via-cli start end)))) (if text (sex-show-c text) (error "sexpand produced no C")))) ;;;###autoload (defun sexpand () "Show the C for the sexp at point." (interactive) (let ((bounds (bounds-of-thing-at-point 'sexp))) (unless bounds (error "No sexp at point")) (sex-expand-lines (line-number-at-pos (car bounds)) (line-number-at-pos (1- (cdr bounds)))))) ;;;###autoload (defun sexpand-defun () "Show the C for the top-level form at point." (interactive) (save-excursion (beginning-of-defun) (let ((start (line-number-at-pos))) (end-of-defun) (sex-expand-lines start (line-number-at-pos (1- (point))))))) ;;;###autoload (defun sexpand-region (begin end) "Show the C for the lines of the region." (interactive "r") (sex-expand-lines (line-number-at-pos begin) (line-number-at-pos end))) ;;;###autoload (defun sexpand-buffer () "Show the C for the whole buffer." (interactive) (sex-expand-lines 1 nil)) (with-eval-after-load 'eglot (add-to-list 'eglot-server-programs '(sex-mode . sex-lsp-command))) (with-eval-after-load 'lsp-mode (add-to-list 'lsp-language-id-configuration '(sex-mode . "sex")) (lsp-register-client (make-lsp-client :new-connection (lsp-stdio-connection #'sex-lsp-command) :activation-fn (lambda (_file-name _mode) (derived-mode-p 'sex-mode)) :major-modes '(sex-mode) :priority 1 :server-id 'sex-lsp))) (provide 'sex-lsp) ;;; sex-lsp.el ends here