2019-01-13 16:48:54 +00:00
|
|
|
;;; doom-modeline-env.el --- A environment parser for doom-modeline -*- lexical-binding: t -*-
|
2019-01-11 21:34:47 +00:00
|
|
|
|
2019-01-13 13:53:51 +00:00
|
|
|
;; Copyright (C) 2019 Justin Barclay, Vincent Zhang
|
2019-01-11 21:34:47 +00:00
|
|
|
|
2019-01-13 13:55:50 +00:00
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
2019-01-11 21:34:47 +00:00
|
|
|
;;
|
|
|
|
;; This program is free software; you can redistribute it and/or
|
|
|
|
;; modify it under the terms of the GNU General Public License as
|
|
|
|
;; published by the Free Software Foundation; either version 2, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;; General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program; see the file COPYING. If not, write to
|
|
|
|
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
|
|
|
|
;; Floor, Boston, MA 02110-1301, USA.
|
|
|
|
;;
|
2019-01-13 13:53:51 +00:00
|
|
|
;;; Commentary:
|
|
|
|
;;
|
2019-01-27 10:30:25 +00:00
|
|
|
;; Parse programming environment.
|
2019-01-13 13:53:51 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
2019-01-11 21:34:47 +00:00
|
|
|
|
|
|
|
(require 'subr-x)
|
2019-01-17 15:42:33 +00:00
|
|
|
(require 'doom-modeline-core)
|
|
|
|
|
2019-03-07 08:19:54 +00:00
|
|
|
;; Externals
|
|
|
|
(defvar python-shell-interpreter)
|
|
|
|
|
2019-01-17 15:42:33 +00:00
|
|
|
;; Show version string for multi-version managers like rvm, rbenv, pyenv, etc.
|
2019-02-18 10:57:16 +00:00
|
|
|
(defvar-local doom-modeline-env--version nil
|
2019-01-17 15:42:33 +00:00
|
|
|
"The version to display with major-mode in mode-line.
|
|
|
|
Example: \"2.6.0\"")
|
2019-02-18 10:57:16 +00:00
|
|
|
(defvar-local doom-modeline-env--command nil
|
2019-01-17 15:42:33 +00:00
|
|
|
"A program that we're looking to extract version information from.
|
|
|
|
Example: \"ruby\"")
|
2019-02-18 10:57:16 +00:00
|
|
|
(defvar-local doom-modeline-env--command-args nil
|
|
|
|
"A list of arguments to pass to `doom-modeline-env--command` to extract the version from.
|
2019-01-17 15:42:33 +00:00
|
|
|
Example: '(\"--version\") ")
|
2019-02-18 10:57:16 +00:00
|
|
|
(defvar-local doom-modeline-env--parser nil
|
2019-01-17 15:42:33 +00:00
|
|
|
"A function that returns version number from a programs --version (or similar) command.
|
|
|
|
Example: 'doom-modeline-env--ruby")
|
|
|
|
|
2019-03-07 01:46:02 +00:00
|
|
|
(defvar doom-modeline-load-string "..."
|
|
|
|
"What to dispaly as the version while a new one is being loaded.")
|
|
|
|
|
|
|
|
(defvar doom-modeline-after-update-env-hook nil
|
|
|
|
"Hooks that run after the modeline version string is updated.")
|
|
|
|
|
|
|
|
(defvar doom-modeline-before-update-env-hook nil
|
|
|
|
"Hooks that run before the modeline version string is updated.")
|
|
|
|
|
2019-01-17 15:42:33 +00:00
|
|
|
(defun doom-modeline-update-env ()
|
|
|
|
"Update environment info on mode-line."
|
2019-02-18 10:57:16 +00:00
|
|
|
(when (and doom-modeline-env-version
|
|
|
|
doom-modeline-env--command
|
|
|
|
doom-modeline-env--command-args
|
|
|
|
doom-modeline-env--parser)
|
2019-03-07 01:46:02 +00:00
|
|
|
(let ((default-directory (doom-modeline-project-root))
|
|
|
|
(buffer (current-buffer)))
|
|
|
|
(run-hooks 'doom-modeline-before-update-env-hook)
|
|
|
|
(setq doom-modeline-env--version doom-modeline-load-string)
|
|
|
|
(doom-modeline-env--get
|
|
|
|
doom-modeline-env--command
|
|
|
|
doom-modeline-env--command-args
|
|
|
|
(lambda (prog-version)
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(setq doom-modeline-env--version
|
|
|
|
(funcall doom-modeline-env--parser prog-version))
|
|
|
|
(run-hooks 'doom-modeline-after-update-env-hook)))))))
|
2019-01-17 15:42:33 +00:00
|
|
|
|
|
|
|
(add-hook 'find-file-hook #'doom-modeline-update-env)
|
|
|
|
(with-no-warnings
|
|
|
|
(if (boundp 'after-focus-change-function)
|
|
|
|
(add-function :after after-focus-change-function
|
|
|
|
(lambda ()
|
|
|
|
(if (frame-focus-state)
|
|
|
|
(doom-modeline-update-env))))
|
|
|
|
(add-hook 'focus-in-hook #'doom-modeline-update-env)))
|
|
|
|
|
|
|
|
(defun doom-modeline-env--get (prog args callback)
|
|
|
|
"Start a sub process using PROG and apply the ARGS to the sub process.
|
|
|
|
Once it recieves information from STDOUT, it closes off the subprocess and
|
|
|
|
passes on the information into the CALLBACK.
|
|
|
|
Example:
|
|
|
|
(doom-modeline-env--get
|
|
|
|
\"ruby\"
|
|
|
|
'(\"--version\")
|
|
|
|
(lambda (line)
|
|
|
|
(message (doom-modeline-parser--ruby line)))"
|
|
|
|
(let ((proc (apply 'start-process
|
|
|
|
;; Flaten process-args into a single list so we can handle
|
|
|
|
;; variadic length args
|
|
|
|
(append
|
|
|
|
(list "doom-modeline-env" nil prog)
|
|
|
|
args)))
|
|
|
|
(parser callback))
|
|
|
|
(set-process-filter proc
|
|
|
|
(lambda (_proc line)
|
|
|
|
(ignore-errors
|
|
|
|
(funcall parser line))))))
|
|
|
|
|
2019-03-07 01:46:02 +00:00
|
|
|
(cl-defmacro doom-modeline-def-env (name &key hooks command parser)
|
|
|
|
"Defines a handler for updating & displaying a version string for a language.
|
|
|
|
|
|
|
|
NAME is an unquoted symbol representing the handler's unique ID.
|
|
|
|
HOOKS is a list of hook symbols where this handler should be triggered.
|
|
|
|
COMMAND should be a function that returns a shell command and its arguments (as
|
|
|
|
a list). It is run on HOOKS. It takes no arguments.
|
|
|
|
PARSER should be a function for parsing COMMAND's output line-by-line, to
|
|
|
|
extract the version string."
|
|
|
|
(declare (indent defun))
|
|
|
|
(unless (and hooks command parser)
|
|
|
|
(error "'%s' env is missing either :hooks, :command or :parser" name))
|
|
|
|
(let ((parse-fn (intern (format "doom-modeline-env--%s-parse" name)))
|
|
|
|
(action-fn (intern (format "doom-modeline-env--%s-args" name)))
|
|
|
|
(setup-fn (intern (format "doom-modeline-env-setup-%s" name)))
|
|
|
|
(update-fn (intern (format "doom-modeline-env-update-%s" name)))
|
|
|
|
(enable-var (intern (format "doom-modeline-env-enable-%s" name)))
|
|
|
|
(command-var (intern (format "doom-modeline-env-%s-command" name)))
|
|
|
|
(parser-var (intern (format "doom-modeline-env-%s-parser-fn" name)))
|
|
|
|
(exe-var (intern (format "doom-modeline-env-%s-executable" name))))
|
|
|
|
(macroexp-progn
|
|
|
|
`((defvar ,enable-var t
|
|
|
|
(format "Whether to display the version string for %s buffers." ',name))
|
|
|
|
(defvar ,command-var ',action-fn
|
|
|
|
(concat "A function that returns the shell command and arguments (as a list) to\n"
|
|
|
|
"produce a version string."))
|
|
|
|
(defvar ,parser-var ',parse-fn
|
|
|
|
(format "The function for parsing each line of `%s's output." ',command-var))
|
|
|
|
(defvar ,exe-var nil
|
|
|
|
(format (concat "What executable to use for the version indicator in %s buffers.\n\n"
|
|
|
|
"If nil, the default binary for this language is used.")
|
|
|
|
',name))
|
|
|
|
(defalias ',parse-fn ,parser
|
|
|
|
(format "The line parser for %s buffers.\n\nUsed by `%s'."
|
|
|
|
',name ',update-fn))
|
|
|
|
(defalias ',action-fn ,command
|
|
|
|
(format "The command resolver for %s buffers.\n\nUsed by `%s'."
|
|
|
|
',name ',update-fn))
|
|
|
|
(defalias ',setup-fn
|
|
|
|
(lambda ()
|
|
|
|
(if enable-local-variables
|
|
|
|
(add-hook 'hack-local-variables-hook #',update-fn nil t)
|
|
|
|
(,update-fn)))
|
|
|
|
(format "Prepares the modeline to later display the %s version string."
|
|
|
|
',name))
|
|
|
|
(defalias ',update-fn
|
|
|
|
(lambda ()
|
|
|
|
(when ,enable-var
|
2019-03-12 20:20:30 +00:00
|
|
|
(when-let* ((command-list (funcall ,command-var))
|
|
|
|
(exe (executable-find (car command-list))))
|
|
|
|
(setq doom-modeline-env--command exe
|
|
|
|
doom-modeline-env--command-args (cdr command-list)
|
2019-03-07 01:46:02 +00:00
|
|
|
doom-modeline-env--parser ,parser-var)
|
|
|
|
(doom-modeline-update-env))))
|
|
|
|
(format "Updates the %s version string in the modeline." ',name))
|
2019-03-07 04:25:33 +00:00
|
|
|
(let ((hooks ',(eval hooks)))
|
|
|
|
(dolist (hook (if (listp hooks) hooks (list hooks)))
|
|
|
|
(add-hook hook #',setup-fn)))))))
|
2019-01-11 21:34:47 +00:00
|
|
|
|
2019-01-17 15:42:33 +00:00
|
|
|
|
|
|
|
;;
|
|
|
|
;; Bootstrap
|
|
|
|
;; Versions, support Python, Ruby, Perl and Golang, etc.
|
|
|
|
;;
|
|
|
|
|
2019-03-07 01:46:02 +00:00
|
|
|
;;;###autoload (autoload 'doom-modeline-env-setup-python "doom-modeline-env")
|
|
|
|
(doom-modeline-def-env python
|
|
|
|
:hooks 'python-mode-hook
|
|
|
|
:command (lambda () (cond ((and (fboundp 'pipenv-project-p)
|
|
|
|
(pipenv-project-p))
|
|
|
|
(list "pipenv" "run"
|
|
|
|
(or doom-modeline-env-python-executable
|
|
|
|
python-shell-interpreter
|
|
|
|
"python")
|
|
|
|
"--version"))
|
|
|
|
((list (or doom-modeline-env-python-executable
|
|
|
|
python-shell-interpreter
|
|
|
|
"python")
|
|
|
|
"--version"))))
|
|
|
|
:parser (lambda (line) (cadr (split-string line))))
|
|
|
|
|
|
|
|
;;;###autoload (autoload 'doom-modeline-env-setup-ruby "doom-modeline-env")
|
|
|
|
(doom-modeline-def-env ruby
|
|
|
|
:hooks '(ruby-mode-hook enh-ruby-mode-hook)
|
|
|
|
:command (lambda () (list (or doom-modeline-env-ruby-executable "ruby") "--version"))
|
|
|
|
:parser (lambda (line)
|
|
|
|
(car (split-string
|
|
|
|
(cadr
|
|
|
|
(split-string line))
|
|
|
|
"p"))))
|
|
|
|
|
|
|
|
;;;###autoload (autoload 'doom-modeline-env-setup-perl "doom-modeline-env")
|
|
|
|
(doom-modeline-def-env perl
|
|
|
|
:hooks 'perl-mode-hook
|
|
|
|
:command (lambda () (list (or doom-modeline-env-perl-executable "perl") "--version"))
|
|
|
|
:parser (lambda (line)
|
|
|
|
(cadr
|
|
|
|
(split-string
|
|
|
|
(car
|
|
|
|
(split-string
|
|
|
|
(cadr
|
|
|
|
(split-string line "("))
|
|
|
|
")"))
|
|
|
|
"v"))))
|
|
|
|
|
|
|
|
;;;###autoload (autoload 'doom-modeline-env-setup-go "doom-modeline-env")
|
|
|
|
(doom-modeline-def-env go
|
|
|
|
:hooks 'go-mode-hook
|
|
|
|
:command (lambda () (list (or doom-modeline-env-go-executable "go") "version"))
|
|
|
|
:parser (lambda (line)
|
|
|
|
(cadr
|
|
|
|
(split-string
|
|
|
|
(cadr
|
|
|
|
(cdr
|
|
|
|
(split-string line)))
|
|
|
|
"go"))))
|
|
|
|
|
|
|
|
;;;###autoload (autoload 'doom-modeline-env-setup-elixir "doom-modeline-env")
|
|
|
|
(doom-modeline-def-env elixir
|
|
|
|
:hooks 'elixir-mode-hook
|
|
|
|
:command (lambda () (list (or doom-modeline-env-elixir-executable "iex") "--version"))
|
2019-03-07 04:28:03 +00:00
|
|
|
:parser (lambda (line) (cadr (split-string line))))
|
2019-03-07 01:46:02 +00:00
|
|
|
|
|
|
|
;;;###autoload (autoload 'doom-modeline-env-setup-rust "doom-modeline-env")
|
|
|
|
(doom-modeline-def-env rust
|
|
|
|
:hooks 'rust-mode-hook
|
|
|
|
:command (lambda () (list (or doom-modeline-env-rust-executable "rustc") "--version"))
|
|
|
|
:parser (lambda (line)
|
|
|
|
(car
|
|
|
|
(split-string
|
|
|
|
(cadr
|
|
|
|
(split-string line))
|
|
|
|
"-"))))
|
2019-01-11 21:34:47 +00:00
|
|
|
|
2019-01-13 16:48:54 +00:00
|
|
|
(provide 'doom-modeline-env)
|
2019-01-11 21:34:47 +00:00
|
|
|
|
2019-01-13 16:48:54 +00:00
|
|
|
;;; doom-modeline-env.el ends here
|