From c27dfd5540d9e722156912d854a3523562fcd611 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Thu, 1 Oct 2015 03:40:09 -0400 Subject: [PATCH] (py|rb)env version in modeline + define-builder macro --- core/core-ui.el | 88 +++++++++++++++++++++++------------- core/lib/defuns-spaceline.el | 13 ++++++ core/lib/macros-quickrun.el | 2 +- core/lib/macros-spaceline.el | 10 ++++ modules/module-go.el | 2 +- modules/module-lua.el | 2 +- modules/module-python.el | 2 + modules/module-ruby.el | 3 +- modules/module-rust.el | 2 +- modules/module-vim.el | 3 +- 10 files changed, 89 insertions(+), 38 deletions(-) create mode 100644 core/lib/defuns-spaceline.el create mode 100644 core/lib/macros-spaceline.el diff --git a/core/core-ui.el b/core/core-ui.el index 746080699..453989a68 100644 --- a/core/core-ui.el +++ b/core/core-ui.el @@ -3,6 +3,7 @@ (when window-system (fringe-mode '(1 . 8)) + (set-frame-font narf-default-font) (setq frame-title-format '(buffer-file-name "%f" ("%b")))) ;; Highlight matching parens @@ -17,6 +18,7 @@ (size-indication-mode -1) (setq-default + line-spacing 1 ;; Multiple cursors across buffers cause a strange redraw delay for ;; some things, like auto-complete or evil-mode's cursor color ;; switching. @@ -104,39 +106,47 @@ (setq-default powerline-default-separator 'wave powerline-height 18) - (require 'spaceline-segments) - ;; Modeline caches + ;; Modeline cache (defvar narf--spaceline-file-path nil) (make-variable-buffer-local 'narf--spaceline-file-path) + (add-hook! focus-in (setq narf--spaceline-file-path nil)) - (defvar narf--spaceline-vc nil) - (make-variable-buffer-local 'narf--spaceline-vc) - (add-hook! before-save (setq narf--spaceline-vc nil)) + (defface mode-line-is-modified nil "Face for mode-line modified symbol") + (defface mode-line-buffer-path nil "Face for mode-line buffer directory") ;; Custom modeline segments (spaceline-define-segment narf-buffer-path - "Name of buffer." - (concat (or narf--spaceline-file-path - (setq narf--spaceline-file-path - (let ((buffer-path (buffer-file-name))) - (if (and buffer-path (file-exists-p buffer-path)) - (progn - (let* ((max-length (/ (window-width) 2)) - (project-path (narf/project-root)) - (path (file-relative-name - buffer-path (file-name-directory (if (string-match "/+\\'" project-path) - (replace-match "" t t project-path) - project-path))))) - (if (> (length path) max-length) - (concat "…" (replace-regexp-in-string - "^.*?/" "/" - (let ((l (length path))) (substring path (- l max-length) l)))) - path))) - (powerline-buffer-id))))) + "Base filename of buffer." + (concat (file-name-nondirectory buffer-file-name) (if (buffer-modified-p) - (propertize "*" 'font-lock-face `(:inherit ,other-face :foreground "orange"))) + (propertize "*" 'face 'mode-line-is-modified)) " ") + :tight t) + + (spaceline-define-segment narf-buffer-dir + "Buffer file directory." + (propertize + (or narf--spaceline-file-path + (setq narf--spaceline-file-path + (if (and buffer-file-name (file-exists-p buffer-file-name)) + (progn + (let* ((max-length (/ (window-width) 2)) + (project-path (narf/project-root)) + (path (file-name-directory + (file-relative-name buffer-file-name + (file-name-directory (if (string-match "/+\\'" project-path) + (replace-match "" t t project-path) + project-path)))))) + (if (> (length path) max-length) + (concat "…" (replace-regexp-in-string + "^.*?/" "/" + (let ((l (length path))) (substring path (- l max-length) l)))) + path))) + (powerline-buffer-id)))) + 'face (if (powerline-selected-window-active) + 'mode-line-buffer-path + 'mode-line-inactive)) :tight-right t) (spaceline-define-segment narf-buffer-encoding-abbrev @@ -156,24 +166,40 @@ (spaceline-define-segment narf-vc "Version control info" - (or narf--spaceline-vc - (replace-regexp-in-string (regexp-quote (symbol-name (vc-deduce-backend))) - "" (s-trim (powerline-vc)) t t)) - :when (powerline-vc)) + (let ((vc (vc-working-revision buffer-file-name))) + (when vc + (format "%s %s%s" (char-to-string #xe0a0) vc + (case (vc-state buffer-file-name) ('edited "+") ('conflict "!!!") (t ""))))) + :when (and active vc-mode)) (spaceline-define-segment narf-hud "A HUD that shows which part of the buffer is currently visible." (powerline-hud highlight-face default-face) :tight t) + ;; Display version string + (defvar narf--env-version nil) + (defvar narf--env-command nil) + (make-variable-buffer-local 'narf--env-version) + (make-variable-buffer-local 'narf--env-command) + + (spaceline-define-segment narf-env-version + "A HUD that shows which part of the buffer is currently visible." + (unless narf--env-version + (narf|spaceline-env-update)) + narf--env-version + :when (and narf--env-version (memq major-mode '(ruby-mode enh-ruby-mode python-mode)))) + ;; Initialize modeline (spaceline-install ;; Left side - '(((narf-buffer-path :face other-face) remote-host) - ((flycheck-error flycheck-warning flycheck-info) :when active) - (narf-vc :face other-face :when active)) + '((narf-buffer-dir :face other-face) + (narf-buffer-path remote-host) + (narf-vc) + ((flycheck-error flycheck-warning flycheck-info) :face other-face :when active)) ;; Right side '(selection-info + narf-env-version narf-buffer-encoding-abbrev (major-mode (minor-modes :separator " ") process :when active) (global :when active) diff --git a/core/lib/defuns-spaceline.el b/core/lib/defuns-spaceline.el new file mode 100644 index 000000000..ea54f14e4 --- /dev/null +++ b/core/lib/defuns-spaceline.el @@ -0,0 +1,13 @@ +;;; defuns-spaceline.el + +;;;###autoload +(defun narf|spaceline-env-update () + (when narf--env-command + (let* ((command (format "cd '%s' && %s" (narf/project-root) narf--env-command)) + (s (shell-command-to-string command))) + (setq narf--env-version (if (string-match "[ \t\n\r]+\\'" s) + (replace-match "" t t s) + s))))) + +(provide 'defuns-spaceline) +;;; defuns-spaceline.el ends here diff --git a/core/lib/macros-quickrun.el b/core/lib/macros-quickrun.el index 242719e04..c3b16e7c9 100644 --- a/core/lib/macros-quickrun.el +++ b/core/lib/macros-quickrun.el @@ -1,7 +1,7 @@ ;;; macros-quickrun.el ;;;###autoload -(defmacro build-for! (mode command &optional build-file) +(defmacro define-builder! (mode command &optional build-file) "Register major/minor MODE with build COMMAND. If FILES are provided, do an additional check to make sure they exist in the project root." `(add-hook! ,mode diff --git a/core/lib/macros-spaceline.el b/core/lib/macros-spaceline.el new file mode 100644 index 000000000..0cc9afad6 --- /dev/null +++ b/core/lib/macros-spaceline.el @@ -0,0 +1,10 @@ +;;; defuns-spaceline.el + +;;;###autoload +(defmacro define-env-command! (mode command) + (add-hook! (focus-in find-file) 'narf|spaceline-env-update) + `(add-hook ',(intern (format "%s-hook" (symbol-name mode))) + (lambda () (setq narf--env-command ,command)))) + +(provide 'defuns-spaceline) +;;; defuns-spaceline.el ends here diff --git a/modules/module-go.el b/modules/module-go.el index 9e1607ced..c067c67a6 100644 --- a/modules/module-go.el +++ b/modules/module-go.el @@ -5,8 +5,8 @@ :interpreter "go" :init (add-hook! go-mode '(emr-initialize flycheck-mode)) - (build-for! go-mode "go build") :config + (define-builder! go-mode "go build") (bind! :map go-mode-map :n "gd" 'godef-jump :n "gD" 'godef-describe diff --git a/modules/module-lua.el b/modules/module-lua.el index 4cc296e35..436da178a 100644 --- a/modules/module-lua.el +++ b/modules/module-lua.el @@ -17,7 +17,7 @@ :keymap (make-sparse-keymap) (add-yas-minor-mode! 'love-mode)) (associate! love-mode :files ("main.lua") :in (lua-mode)) -(build-for! love-mode "open -a love.app '%s'" "main.lua") +(define-builder! love-mode "open -a love.app '%s'" "main.lua") (provide 'module-lua) ;;; module-lua.el ends here diff --git a/modules/module-python.el b/modules/module-python.el index e5b6d276a..0b3d0d314 100644 --- a/modules/module-python.el +++ b/modules/module-python.el @@ -10,6 +10,7 @@ python-environment-directory narf-temp-dir python-shell-interpreter "ipython") :config + (define-env-command! python-mode "pyenv version | cut -d' ' -f1") (define-key python-mode-map (kbd "DEL") nil)) ; interferes with smartparens (use-package nose @@ -28,6 +29,7 @@ :n "tV" 'nosetests-pdb-module))) (use-package anaconda-mode + :diminish anaconda-mode :defines (anaconda-mode-map anaconda-nav-mode-map) :functions (anaconda-mode-running-p) :init (add-hook! python-mode '(anaconda-mode eldoc-mode)) diff --git a/modules/module-ruby.el b/modules/module-ruby.el index e9c4a24ae..01ad21b5b 100644 --- a/modules/module-ruby.el +++ b/modules/module-ruby.el @@ -9,8 +9,9 @@ :interpreter "ruby" :init (add-hook! ruby-mode '(narf|enable-tab-width-2 flycheck-mode)) - (build-for! ruby-mode "rake %s" "Rakefile") :config + (define-builder! ruby-mode "rake %s" "Rakefile") + (define-env-command! ruby-mode "rbenv version | cut -d' ' -f1") ;; Formatting (setq ruby-indent-level 2 ruby-deep-indent-paren t) diff --git a/modules/module-rust.el b/modules/module-rust.el index 24ff5e810..78957ad23 100644 --- a/modules/module-rust.el +++ b/modules/module-rust.el @@ -11,7 +11,7 @@ (use-package rust-mode :mode "\\.rs$" :config - (build-for! rust-mode "cargo run" "Cargo.toml") + (define-builder! rust-mode "cargo run" "Cargo.toml") (use-package flycheck-rust :config (add-hook! rust-mode 'flycheck-mode)) diff --git a/modules/module-vim.el b/modules/module-vim.el index 42038edf6..021983cdf 100644 --- a/modules/module-vim.el +++ b/modules/module-vim.el @@ -1,8 +1,7 @@ ;;; module-vim.el (use-package vimrc-mode - :mode (("/\\.?g?vimrc$" . vimrc-mode) - ("\\.vim$" . vimrc-mode))) + :mode ("/\\.?g?vimrc$" "\\.vim$" "/\\.vim/rc/.+$")) (provide 'module-vim) ;;; module-vim.el ends here