12FF5B8

hico_horiuchiの技術系な覚え書き.

Powerlineフォントでmode-lineを改造

今まではemacs-powerlineを使ってきた。
ただ、矢印部分をXPMで実現しているのがイマイチだった。
powerline-shellなどでvim-powerlineのフォントを生成していたので、こちらを使うことにした。

フォントの準備

Emacsで使っているフォントに、vim-powerline/fontpatcher at develop · Lokaltog/vim-powerline · GitHubの指示通りパッチを当てれば良い。
今回は、以下の文字を使用する。

  • U+2B60: Branch symbol
  • U+2B61: LN (line) symbol
  • U+2B80: Hard right arrow
  • U+2B81: Soft right arrow

gitのブランチ名を表示する

http://d.hatena.ne.jp/syohex/20130201/1359731697:titileを参考に、mode-lineにgitのブランチ名を表示する。

(defun git-branch-mode-line ()
  (let* ((branch (replace-regexp-in-string
                  "[\r\n]+\\'" ""
                  (shell-command-to-string "git symbolic-ref -q HEAD")))
         (mode-line-str (if (string-match "^refs/heads/" branch)
                          (format " ⭠ %s ⮁" (substring branch 11)) "")))
    (propertize mode-line-str 'face 'mode-line-4-fg)))

これで、major-modeを変更した時に、git-branch-nameにブランチ名が格納(更新)される。
Gitリポジトリに居ない時は、空文字列になる。

mode-lineの設定

;;--------------------------------------------------------------------------------
;; モードライン
;;--------------------------------------------------------------------------------
(line-number-mode t)   ; 行番号表示
(column-number-mode t) ; 列番号表示
(setq-default mode-line-format '(
  (:propertize " %m "           face mode-line-1-fg)
  (:propertize "⮀"              face mode-line-1-bg)
  (:propertize minor-mode-alist face mode-line-2-fg)
  (:propertize " ⮁"             face mode-line-2-fg)
  (:propertize " %b "           face mode-line-3-fg)
  (:propertize "⮀"              face mode-line-3-bg)
  (:eval (git-branch-mode-line))
  (:propertize " %+ ⮁ %Z "      face mode-line-4-fg)
  (:propertize "⮀"              face mode-line-4-bg)
  (:propertize " %p "           face mode-line-2-fg)
  (:propertize "⮀"              face mode-line-5-bg)
  (:propertize " ⭡ "            face mode-line-5-fg)
  (:propertize "%l"             face mode-line-6-fg)
  (:propertize ":%c"            face mode-line-5-fg)))
;;--------------------------------------------------------------------------------

;;--------------------------------------------------------------------------------
;; モードラインの色
;;--------------------------------------------------------------------------------
(set-face-attribute 'mode-line nil
  :foreground "#005f5f" :background "#87d7ff" :box nil)
(set-face-attribute 'mode-line-inactive nil
  :foreground "#005f5f" :background "#87d7ff" :box nil)
(defun make/set-face (face-name fg-color bg-color weight)
  (make-face face-name)
  (set-face-attribute face-name nil
    :foreground fg-color :background bg-color :box nil :weight weight))
(make/set-face 'mode-line-1-fg "#005f5f" "#ffffff" 'bold)
(make/set-face 'mode-line-1-bg "#ffffff" "#0087af" 'normal)
(make/set-face 'mode-line-2-fg "#87d7ff" "#0087af" 'normal)
(make/set-face 'mode-line-3-fg "#ffffff" "#0087af" 'bold)
(make/set-face 'mode-line-3-bg "#0087af" "#005f87" 'normal)
(make/set-face 'mode-line-4-fg "#87d7ff" "#005f87" 'normal)
(make/set-face 'mode-line-4-bg "#005f87" "#0087af" 'normal)
(make/set-face 'mode-line-5-fg "#005f5f" "#87d7ff" 'normal)
(make/set-face 'mode-line-5-bg "#0087af" "#87d7ff" 'normal)
(make/set-face 'mode-line-6-fg "#005f5f" "#87d7ff" 'bold)
;;--------------------------------------------------------------------------------

mode-line-formatで設定している値については、%-Constructs - GNU Emacs Lisp Reference Manualを参考にした。
基本的には、vim-powerlineの設定を踏襲している。
また、色の設定では、make-faceとset-face-attributeを一度にやりたかったので、make/set-face関数を作った。

結果と感想


(mode-lineのモード情報をコンパクトに表示する - Life is very shortも参考にしています。)

行数などの部分を上手く右寄せできなかったので、左詰めにした。
フォントによる依存はあるが、格好良いし、視認性も上がったかな。