From ac6daa748eb20f01c3ac87f69ee33dc709838772 Mon Sep 17 00:00:00 2001 From: Pavel Kulyov Date: Wed, 11 Nov 2026 07:58:11 +0300 Subject: [PATCH] emacs: load the server beside sex-mode --- Readme.org | 41 +++++++++--- sex-lsp.el | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++ sex-mode.el | 12 ++-- 3 files changed, 219 insertions(+), 12 deletions(-) create mode 100644 sex-lsp.el diff --git a/Readme.org b/Readme.org index 0e073c83..c67066f0 100644 --- a/Readme.org +++ b/Readme.org @@ -123,6 +123,23 @@ Alex Hello, Alex! #+end_src +* Editor +[[file:sex-mode.el][sex-mode.el]] is the editing mode, [[file:sex-lsp.el][sex-lsp.el]] is the language-server client. +The install form under "sex-mode.el" below fetches both, and its ~:config~ +line loads the client. + +With that line, either client starts ~sex-lsp~. The hook is +~eglot-ensure~ or ~lsp-deferred~. Eldoc, xref, +and diagnostics are the ordinary LSP ones. ~sexpand~, ~sexpand-defun~, +~sexpand-region~, and ~sexpand-buffer~ show the generated C in a C +buffer. Without a server they call ~sexc --expand-lines~. + +~sex-c-backend~ set to ~clangd~, as that form does, asks the server for +C headers too: completion, prototypes, and definitions of names Sex +itself does not declare. Flags are a one-line ~sex-cflags~ file, or +~sex-cflags~ in Emacs, the same words that go after ~sexc~'s ~--~. +Leave out ~:custom~, ~:hook~, and ~:config~ for the editing mode alone. + * Features ** Full C interoperability Just ~(include "Your/Favourite/Library.h")~ and use it as you would @@ -319,13 +336,21 @@ To harness the power of sex-mode, add the following lines to your ~$HOME/.config/emacs/init.el~: #+begin_src emacs-lisp -(use-package sex-mode - :ensure nil - :quelpa - (sex-mode :fetcher git - :url "https://git.kotobank.ch/alex-eg/sex.git" - :files ("sex-mode.el") - :upgrade t)) + (use-package sex-mode + :ensure nil + :quelpa + (sex-mode :fetcher git + :url "https://git.kotobank.ch/alex-eg/sex.git" + :files ("sex-mode.el" "sex-lsp.el") + :upgrade t) + :custom (sex-c-backend 'clangd) + :hook (sex-mode . eglot-ensure) ; if you use eglot + :hook (sex-mode . lsp-deferred) ; if you use lsp-mode + :config (require 'sex-lsp)) #+end_src -~.sex~ files then open in ~sex-mode~. +~.sex~ files then open in ~sex-mode~. The ~:config~ line loads the +client and registers it with whichever of eglot and lsp-mode is +installed. The hook is what starts one of them: ~lsp-deferred~ or +~eglot-ensure~. Without ~:custom~, ~:hook~, and ~:config~, only the +editing mode is loaded. diff --git a/sex-lsp.el b/sex-lsp.el new file mode 100644 index 00000000..aa1660bd --- /dev/null +++ b/sex-lsp.el @@ -0,0 +1,178 @@ +;;; 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 diff --git a/sex-mode.el b/sex-mode.el index 7d8d0631..88d28afa 100644 --- a/sex-mode.el +++ b/sex-mode.el @@ -15,16 +15,20 @@ ;; The mode derives from `lisp-data-mode' and fontifies Sex ;; declarations and keywords. ;; -;; `sex-mode' is one file in the Sex repository. Quelpa installs it -;; from that git remote: +;; `sex-mode' edits. `sex-lsp' talks to the language server. Quelpa +;; installs both files. The hook is `eglot-ensure' or `lsp-deferred'. +;; Leave out :custom, :hook, and :config for the editing mode alone. ;; ;; (use-package sex-mode ;; :ensure nil ;; :quelpa ;; (sex-mode :fetcher git ;; :url "https://git.kotobank.ch/alex-eg/sex.git" -;; :files ("sex-mode.el") -;; :upgrade t)) +;; :files ("sex-mode.el" "sex-lsp.el") +;; :upgrade t) +;; :custom (sex-c-backend 'clangd) +;; :hook (sex-mode . lsp-deferred) +;; :config (require 'sex-lsp)) ;; ;; Visiting a .sex file turns the mode on.