From: João Távora Date: Wed, 20 Jun 2018 17:47:45 +0000 (+0100) Subject: Improve eglot-ensure and mention it in readme.md X-Git-Tag: emacs-29.0.90~1616^2~524^2~4^2~498 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=30d3874723f7f56615a32d44ca2e19870f783bb5;p=emacs.git Improve eglot-ensure and mention it in readme.md * README.md (Installation and Usage): Mention eglot-ensure. * eglot.el (eglot-ensure): No-op for non-file buffers. (eglot--connect): Don't fallback to 'eglot-lsp-server here. (eglot--guess-contact): Error if something can't be guessed. GitHub-reference: close https://github.com/joaotavora/eglot/issues/25 --- diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index 1aa3661d4d2..ecbd509b003 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -299,7 +299,7 @@ class SERVER-CLASS." server connect-success) (setq server (make-instance - (or server-class 'eglot-lsp-server) + server-class :process proc :major-mode managed-major-mode :project project :contact contact :name name :project-nickname nickname @@ -341,23 +341,28 @@ class SERVER-CLASS." (defun eglot--guess-contact (&optional interactive) "Helper for `eglot'. -Return (MANAGED-MODE PROJECT CONTACT CLASS). -If INTERACTIVE, maybe prompt user." +Return (MANAGED-MODE PROJECT CLASS CONTACT). If INTERACTIVE is +non-nil, maybe prompt user, else error as soon as something can't +be guessed." (let* ((guessed-mode (if buffer-file-name major-mode)) (managed-mode (cond - ((or (>= (prefix-numeric-value current-prefix-arg) 16) - (not guessed-mode)) + ((and interactive + (or (>= (prefix-numeric-value current-prefix-arg) 16) + (not guessed-mode))) (intern (completing-read "[eglot] Start a server to manage buffers of what major mode? " (mapcar #'symbol-name (eglot--all-major-modes)) nil t (symbol-name guessed-mode) nil (symbol-name guessed-mode) nil))) + ((not guessed-mode) + (eglot--error "Can't guess mode to manage for `%s'" (current-buffer))) (t guessed-mode))) (project (or (project-current) `(transient . ,default-directory))) (guess (cdr (assoc managed-mode eglot-server-programs))) - (class (and (consp guess) (symbolp (car guess)) - (prog1 (car guess) (setq guess (cdr guess))))) + (class (or (and (consp guess) (symbolp (car guess)) + (prog1 (car guess) (setq guess (cdr guess)))) + 'eglot-lsp-server)) (program (and (listp guess) (stringp (car guess)) (car guess))) (base-prompt (and interactive @@ -374,16 +379,18 @@ If INTERACTIVE, maybe prompt user." (format ", but I can't find `%s' in PATH!" program) "\n" base-prompt))))) (contact - (if prompt - (let ((s (read-shell-command - prompt - (if program (combine-and-quote-strings guess)) - 'eglot-command-history))) - (if (string-match "^\\([^\s\t]+\\):\\([[:digit:]]+\\)$" - (string-trim s)) - (list (match-string 1 s) (string-to-number (match-string 2 s))) - (split-string-and-unquote s))) - guess))) + (or (and prompt + (let ((s (read-shell-command + prompt + (if program (combine-and-quote-strings guess)) + 'eglot-command-history))) + (if (string-match "^\\([^\s\t]+\\):\\([[:digit:]]+\\)$" + (string-trim s)) + (list (match-string 1 s) + (string-to-number (match-string 2 s))) + (split-string-and-unquote s)))) + guess + (eglot--error "Couldn't guess for `%s'!" managed-mode)))) (list managed-mode project class contact))) ;;;###autoload @@ -470,7 +477,8 @@ INTERACTIVE is t if called interactively." (eglot--name server) major-mode (eglot--project-nickname server))))))) - (add-hook 'post-command-hook #'maybe-connect 'append nil)))) + (when buffer-file-name + (add-hook 'post-command-hook #'maybe-connect 'append nil))))) (defun eglot--process-sentinel (proc change) "Called when PROC undergoes CHANGE."