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)
|
|
|
|
|
|
|
|
;; Show version string for multi-version managers like rvm, rbenv, pyenv, etc.
|
|
|
|
(defvar-local doom-modeline-env-version nil
|
|
|
|
"The version to display with major-mode in mode-line.
|
|
|
|
Example: \"2.6.0\"")
|
|
|
|
(defvar-local doom-modeline-env-command nil
|
|
|
|
"A program that we're looking to extract version information from.
|
|
|
|
Example: \"ruby\"")
|
|
|
|
(defvar-local doom-modeline-env-command-args nil
|
|
|
|
"A list of arguments to pass to `doom-modeline-env-command` to extract the version from.
|
|
|
|
Example: '(\"--version\") ")
|
|
|
|
(defvar-local doom-modeline-env-parser nil
|
|
|
|
"A function that returns version number from a programs --version (or similar) command.
|
|
|
|
Example: 'doom-modeline-env--ruby")
|
|
|
|
|
|
|
|
(defun doom-modeline-update-env ()
|
|
|
|
"Update environment info on mode-line."
|
|
|
|
(when (and doom-modeline-version
|
|
|
|
doom-modeline-env-command
|
|
|
|
(executable-find doom-modeline-env-command)
|
|
|
|
doom-modeline-env-command-args
|
|
|
|
doom-modeline-env-parser)
|
|
|
|
(let ((default-directory (doom-modeline-project-root)))
|
|
|
|
(doom-modeline-env--get doom-modeline-env-command
|
|
|
|
doom-modeline-env-command-args
|
|
|
|
(lambda (prog-version)
|
|
|
|
(setq doom-modeline-env-version
|
|
|
|
(funcall doom-modeline-env-parser prog-version)))))))
|
|
|
|
|
|
|
|
(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))))))
|
|
|
|
|
|
|
|
;;
|
|
|
|
;; Parser
|
|
|
|
;;
|
2019-01-11 21:34:47 +00:00
|
|
|
|
2019-01-13 16:48:54 +00:00
|
|
|
(defun doom-modeline-env--ruby (line)
|
2019-01-13 13:53:51 +00:00
|
|
|
"Parse Ruby version from LINE."
|
2019-01-11 21:34:47 +00:00
|
|
|
(car (split-string
|
|
|
|
(cadr
|
|
|
|
(split-string line))
|
|
|
|
"p")))
|
|
|
|
|
2019-01-13 16:48:54 +00:00
|
|
|
(defun doom-modeline-env--elixir (line)
|
2019-01-13 13:53:51 +00:00
|
|
|
"Parse Elixir version from LINE."
|
2019-01-11 21:34:47 +00:00
|
|
|
(cadr
|
|
|
|
(split-string line)))
|
|
|
|
|
2019-01-13 16:48:54 +00:00
|
|
|
(defun doom-modeline-env--rustc (line)
|
2019-01-13 13:53:51 +00:00
|
|
|
"Parse Rust version from LINE."
|
2019-01-11 21:34:47 +00:00
|
|
|
(car
|
|
|
|
(split-string
|
|
|
|
(cadr
|
|
|
|
(split-string line))
|
|
|
|
"-")))
|
|
|
|
|
2019-01-13 16:48:54 +00:00
|
|
|
(defun doom-modeline-env--go (line)
|
2019-01-13 13:53:51 +00:00
|
|
|
"Parse Go version from LINE."
|
2019-01-11 21:34:47 +00:00
|
|
|
(cadr
|
|
|
|
(split-string
|
|
|
|
(cadr
|
|
|
|
(cdr
|
|
|
|
(split-string
|
|
|
|
line)))
|
|
|
|
"go")))
|
|
|
|
|
2019-01-13 16:48:54 +00:00
|
|
|
(defun doom-modeline-env--perl (line)
|
2019-01-13 13:53:51 +00:00
|
|
|
"Parse Perl version from LINE."
|
2019-01-11 21:34:47 +00:00
|
|
|
(cadr
|
|
|
|
(split-string
|
|
|
|
(car
|
|
|
|
(split-string
|
|
|
|
(cadr
|
|
|
|
(split-string line "("))
|
|
|
|
")"))
|
|
|
|
"v")))
|
|
|
|
|
2019-01-13 16:48:54 +00:00
|
|
|
(defun doom-modeline-env--python (line)
|
2019-01-13 13:53:51 +00:00
|
|
|
"Parse Python version from LINE."
|
2019-01-11 21:34:47 +00:00
|
|
|
(cadr
|
|
|
|
(split-string line)))
|
|
|
|
|
2019-01-17 15:42:33 +00:00
|
|
|
|
|
|
|
;;
|
|
|
|
;; Bootstrap
|
|
|
|
;; Versions, support Python, Ruby, Perl and Golang, etc.
|
|
|
|
;;
|
|
|
|
|
|
|
|
(add-hook 'python-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(cond ((and (fboundp 'pipenv-project-p) (pipenv-project-p) (executable-find "pipenv"))
|
|
|
|
(setq doom-modeline-env-parser 'doom-modeline-env--python)
|
|
|
|
(setq doom-modeline-env-command "pipenv")
|
|
|
|
(setq doom-modeline-env-command-args '("run" "python" "--version")))
|
|
|
|
((and doom-modeline-python-executable
|
|
|
|
(executable-find doom-modeline-python-executable))
|
|
|
|
(setq doom-modeline-env-parser 'doom-modeline-env--python)
|
|
|
|
(setq doom-modeline-env-command doom-modeline-python-executable)
|
|
|
|
(setq doom-modeline-env-command-args '("--version"))))))
|
|
|
|
(add-hook 'ruby-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(setq doom-modeline-env-command "ruby")
|
|
|
|
(setq doom-modeline-env-command-args '("--version"))
|
|
|
|
(setq doom-modeline-env-parser 'doom-modeline-env--ruby)))
|
|
|
|
(add-hook 'perl-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(setq doom-modeline-env-command "perl")
|
|
|
|
(setq doom-modeline-env-command-args '("--version"))
|
|
|
|
(setq doom-modeline-env-parser 'doom-modeline-env--perl)))
|
|
|
|
(add-hook 'go-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(setq doom-modeline-env-command "go")
|
|
|
|
(setq doom-modeline-env-command-args '("version"))
|
|
|
|
(setq doom-modeline-env-parser 'doom-modeline-env--go)))
|
|
|
|
|
|
|
|
(add-hook 'elixir-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(setq doom-modeline-env-command "iex")
|
|
|
|
(setq doom-modeline-env-command-args '("--version"))
|
|
|
|
(setq doom-modeline-env-parser 'doom-modeline-env--elixir)))
|
|
|
|
(add-hook 'rust-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(setq doom-modeline-env-command "rustc")
|
|
|
|
(setq doom-modeline-env-command-args '("--version"))
|
|
|
|
(setq doom-modeline-env-parser 'doom-modeline-env--rustc)))
|
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
|