(py|rb)env version in modeline + define-builder macro

This commit is contained in:
Henrik Lissner 2015-10-01 03:40:09 -04:00
parent 979472385b
commit c27dfd5540
10 changed files with 89 additions and 38 deletions

View file

@ -3,6 +3,7 @@
(when window-system (when window-system
(fringe-mode '(1 . 8)) (fringe-mode '(1 . 8))
(set-frame-font narf-default-font)
(setq frame-title-format '(buffer-file-name "%f" ("%b")))) (setq frame-title-format '(buffer-file-name "%f" ("%b"))))
;; Highlight matching parens ;; Highlight matching parens
@ -17,6 +18,7 @@
(size-indication-mode -1) (size-indication-mode -1)
(setq-default (setq-default
line-spacing 1
;; Multiple cursors across buffers cause a strange redraw delay for ;; Multiple cursors across buffers cause a strange redraw delay for
;; some things, like auto-complete or evil-mode's cursor color ;; some things, like auto-complete or evil-mode's cursor color
;; switching. ;; switching.
@ -104,39 +106,47 @@
(setq-default (setq-default
powerline-default-separator 'wave powerline-default-separator 'wave
powerline-height 18) powerline-height 18)
(require 'spaceline-segments)
;; Modeline caches ;; Modeline cache
(defvar narf--spaceline-file-path nil) (defvar narf--spaceline-file-path nil)
(make-variable-buffer-local 'narf--spaceline-file-path) (make-variable-buffer-local 'narf--spaceline-file-path)
(add-hook! focus-in (setq narf--spaceline-file-path nil))
(defvar narf--spaceline-vc nil) (defface mode-line-is-modified nil "Face for mode-line modified symbol")
(make-variable-buffer-local 'narf--spaceline-vc) (defface mode-line-buffer-path nil "Face for mode-line buffer directory")
(add-hook! before-save (setq narf--spaceline-vc nil))
;; Custom modeline segments ;; Custom modeline segments
(spaceline-define-segment narf-buffer-path (spaceline-define-segment narf-buffer-path
"Name of buffer." "Base filename of buffer."
(concat (or narf--spaceline-file-path (concat (file-name-nondirectory buffer-file-name)
(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)))))
(if (buffer-modified-p) (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) :tight-right t)
(spaceline-define-segment narf-buffer-encoding-abbrev (spaceline-define-segment narf-buffer-encoding-abbrev
@ -156,24 +166,40 @@
(spaceline-define-segment narf-vc (spaceline-define-segment narf-vc
"Version control info" "Version control info"
(or narf--spaceline-vc (let ((vc (vc-working-revision buffer-file-name)))
(replace-regexp-in-string (regexp-quote (symbol-name (vc-deduce-backend))) (when vc
"" (s-trim (powerline-vc)) t t)) (format "%s %s%s" (char-to-string #xe0a0) vc
:when (powerline-vc)) (case (vc-state buffer-file-name) ('edited "+") ('conflict "!!!") (t "")))))
:when (and active vc-mode))
(spaceline-define-segment narf-hud (spaceline-define-segment narf-hud
"A HUD that shows which part of the buffer is currently visible." "A HUD that shows which part of the buffer is currently visible."
(powerline-hud highlight-face default-face) (powerline-hud highlight-face default-face)
:tight t) :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 ;; Initialize modeline
(spaceline-install (spaceline-install
;; Left side ;; Left side
'(((narf-buffer-path :face other-face) remote-host) '((narf-buffer-dir :face other-face)
((flycheck-error flycheck-warning flycheck-info) :when active) (narf-buffer-path remote-host)
(narf-vc :face other-face :when active)) (narf-vc)
((flycheck-error flycheck-warning flycheck-info) :face other-face :when active))
;; Right side ;; Right side
'(selection-info '(selection-info
narf-env-version
narf-buffer-encoding-abbrev narf-buffer-encoding-abbrev
(major-mode (minor-modes :separator " ") process :when active) (major-mode (minor-modes :separator " ") process :when active)
(global :when active) (global :when active)

View file

@ -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

View file

@ -1,7 +1,7 @@
;;; macros-quickrun.el ;;; macros-quickrun.el
;;;###autoload ;;;###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 "Register major/minor MODE with build COMMAND. If FILES are provided, do an
additional check to make sure they exist in the project root." additional check to make sure they exist in the project root."
`(add-hook! ,mode `(add-hook! ,mode

View file

@ -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

View file

@ -5,8 +5,8 @@
:interpreter "go" :interpreter "go"
:init :init
(add-hook! go-mode '(emr-initialize flycheck-mode)) (add-hook! go-mode '(emr-initialize flycheck-mode))
(build-for! go-mode "go build")
:config :config
(define-builder! go-mode "go build")
(bind! :map go-mode-map (bind! :map go-mode-map
:n "gd" 'godef-jump :n "gd" 'godef-jump
:n "gD" 'godef-describe :n "gD" 'godef-describe

View file

@ -17,7 +17,7 @@
:keymap (make-sparse-keymap) :keymap (make-sparse-keymap)
(add-yas-minor-mode! 'love-mode)) (add-yas-minor-mode! 'love-mode))
(associate! love-mode :files ("main.lua") :in (lua-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) (provide 'module-lua)
;;; module-lua.el ends here ;;; module-lua.el ends here

View file

@ -10,6 +10,7 @@
python-environment-directory narf-temp-dir python-environment-directory narf-temp-dir
python-shell-interpreter "ipython") python-shell-interpreter "ipython")
:config :config
(define-env-command! python-mode "pyenv version | cut -d' ' -f1")
(define-key python-mode-map (kbd "DEL") nil)) ; interferes with smartparens (define-key python-mode-map (kbd "DEL") nil)) ; interferes with smartparens
(use-package nose (use-package nose
@ -28,6 +29,7 @@
:n "tV" 'nosetests-pdb-module))) :n "tV" 'nosetests-pdb-module)))
(use-package anaconda-mode (use-package anaconda-mode
:diminish anaconda-mode
:defines (anaconda-mode-map anaconda-nav-mode-map) :defines (anaconda-mode-map anaconda-nav-mode-map)
:functions (anaconda-mode-running-p) :functions (anaconda-mode-running-p)
:init (add-hook! python-mode '(anaconda-mode eldoc-mode)) :init (add-hook! python-mode '(anaconda-mode eldoc-mode))

View file

@ -9,8 +9,9 @@
:interpreter "ruby" :interpreter "ruby"
:init :init
(add-hook! ruby-mode '(narf|enable-tab-width-2 flycheck-mode)) (add-hook! ruby-mode '(narf|enable-tab-width-2 flycheck-mode))
(build-for! ruby-mode "rake %s" "Rakefile")
:config :config
(define-builder! ruby-mode "rake %s" "Rakefile")
(define-env-command! ruby-mode "rbenv version | cut -d' ' -f1")
;; Formatting ;; Formatting
(setq ruby-indent-level 2 (setq ruby-indent-level 2
ruby-deep-indent-paren t) ruby-deep-indent-paren t)

View file

@ -11,7 +11,7 @@
(use-package rust-mode (use-package rust-mode
:mode "\\.rs$" :mode "\\.rs$"
:config :config
(build-for! rust-mode "cargo run" "Cargo.toml") (define-builder! rust-mode "cargo run" "Cargo.toml")
(use-package flycheck-rust (use-package flycheck-rust
:config (add-hook! rust-mode 'flycheck-mode)) :config (add-hook! rust-mode 'flycheck-mode))

View file

@ -1,8 +1,7 @@
;;; module-vim.el ;;; module-vim.el
(use-package vimrc-mode (use-package vimrc-mode
:mode (("/\\.?g?vimrc$" . vimrc-mode) :mode ("/\\.?g?vimrc$" "\\.vim$" "/\\.vim/rc/.+$"))
("\\.vim$" . vimrc-mode)))
(provide 'module-vim) (provide 'module-vim)
;;; module-vim.el ends here ;;; module-vim.el ends here