Skip to content

Instantly share code, notes, and snippets.

@balibali
Created February 28, 2011 04:39
Show Gist options
  • Save balibali/846942 to your computer and use it in GitHub Desktop.
Save balibali/846942 to your computer and use it in GitHub Desktop.
init.el @balibali
;;; ~/.emacs.d/init.el
;;
;; Emacs 23.3 on Mac OS X 10.6 のことしか考えていません
;;
;;; Emacs ビルド
;;
;; Homebrew で自分用のパッチを当ててビルドする
;; https://gist.github.com/857132
;;
;; $ curl "https://gist.github.com/raw/857132/_install.sh" | sh
;;
;;; Emacs Lisp
;;
;; auto-install と color-theme は以下のスクリプトであらかじめ入れておく
;; https://gist.github.com/857132
;;
;; $ curl "https://gist.github.com/raw/857132/_install_elisp.sh" | sh
;; (~/.emacs.d が存在しない状態でないと動かない)
;;
;; 終わったら Emacs 起動すると自動的に auto-install 連発でだいたい入る
;; 例外: gtags.el
;;
;;; KeyRemap4MacBook
;;
;; https://gist.github.com/846892
;;
;; * EISUU to Option_L (only in Emacs)
;; * EISUU to Escape (except Emacs)
;;
;;; QuickSilver
;;
;; * Trigger: Open Emacs.app for Command+E
;;
;;; パスとかの設定
;; load-path & auto-install
(let ((default-directory "~/.emacs.d/lisp"))
;; load-path に ~/.emacs.d/lisp とそのサブディレクトリを追加
(add-to-list 'load-path default-directory)
(normal-top-level-add-subdirs-to-load-path)
;; auto-install.el
;; (auto-install-from-emacswiki "auto-install.el")
(require 'auto-install)
(setq auto-install-directory default-directory))
;; exec-path にシェルの $PATH を追加
(dolist (path (reverse (split-string (shell-command-to-string "echo -n $PATH") ":")))
(add-to-list 'exec-path path))
;;; require-or-install, autoload-or-install
;;
;; ファイルがあれば require/autoload なければ install
;;
(defvar balibali-install-list nil)
(defvar balibali-install-list-all nil)
(defun balibali-install ()
(when balibali-install-list
(defun balibali-install-init ()
(setq auto-install-save-confirm nil)
(setq balibali-install-list (reverse balibali-install-list))
(defadvice auto-install-cleanup (after balibali-install-next activate)
(balibali-install-next))
(balibali-install-next))
(defun balibali-install-next ()
(if balibali-install-list
(let ((next (car balibali-install-list)))
(setq balibali-install-list (cdr balibali-install-list))
(eval next))
(balibali-install-term)))
(defun balibali-install-term ()
(setq auto-install-save-confirm t)
;; TODO: ちょっと早くて消されちゃうのでタイミング調整
(message "Balibali installation is completed."))
;; auto-install-from-emacswiki のパッケージ名選択で止まる対策
(defadvice auto-install-update-emacswiki-package-list (after install activate)
(balibali-install-init))
(auto-install-update-emacswiki-package-name)))
(add-hook 'after-init-hook 'balibali-install)
(defun balibali-install-all ()
(interactive)
(add-to-list 'balibali-install-list-all '(auto-install-from-emacswiki "auto-install.el"))
(setq balibali-install-list balibali-install-list-all)
(balibali-install))
(defmacro require-or-install (feature install &rest body)
`(progn
(add-to-list 'balibali-install-list-all ',install)
(eval-after-load (symbol-name ,feature) '(progn ,@body))
(or (require ,feature nil t)
(add-to-list 'balibali-install-list ',install))))
(put 'require-or-install 'lisp-indent-function 2)
(defmacro autoload-or-install (function file install &rest body)
`(progn
(add-to-list 'balibali-install-list-all ',install)
(eval-after-load ,file '(progn ,@body))
(if (locate-library ,file)
(autoload ,function ,file)
(add-to-list 'balibali-install-list ',install))))
(put 'autoload-or-install 'lisp-indent-function 'defun)
;;; いろいろ設定
;; バックアップファイルイラネ
(setq make-backup-files nil)
;; 自動保存は auto-save-buffers.el を使うので無効にしておく
(setq auto-save-default nil)
;; C-k (kill-line) で改行ごと削除
(setq kill-whole-line t)
;; 選択範囲を DEL でキルリングに入れずに削除
(delete-selection-mode t)
;; キルリングとクリップボードの連携
(setq x-select-enable-clipboard t)
;; "yes or no" を "y or n" に
(fset 'yes-or-no-p 'y-or-n-p)
;; セーブする時に自動で行末の空白を削除する
;; ただし、auto-save-buffers と併用するためカレント行以外を削除する
;; http://stackoverflow.com/questions/3533703/emacs-delete-trailing-whitespace-except-current-line
(defun delete-trailing-whitespace-except-current-line ()
(interactive)
(let ((begin (line-beginning-position))
(end (line-end-position)))
(save-excursion
(when (< (point-min) begin)
(save-restriction
(narrow-to-region (point-min) (1- begin))
(delete-trailing-whitespace)))
(when (> (point-max) end)
(save-restriction
(narrow-to-region (1+ end) (point-max))
(delete-trailing-whitespace))))))
(add-hook 'before-save-hook 'delete-trailing-whitespace-except-current-line)
;; emacsclient 使う
(require 'server)
(unless (server-running-p) (server-start))
;; ファイル名が重複した場合にディレクトリ名を添える
(require 'uniquify)
(setq uniquify-buffer-name-style 'post-forward-angle-brackets)
(setq uniquify-ignore-buffers-re "*[^*]+*")
;; byte compile エラーで止まるのでとりあえず回避
;; Symbol's value as variable is void: warning-suppress-types
(setq warning-suppress-types nil)
;; cua-mode を矩形選択で使う
(cua-mode t)
(setq cua-enable-cua-keys nil)
;;; 表示関連
;; スタートアップメッセージイラネ
(setq inhibit-startup-screen t)
;; スクロールバーイラネ
(scroll-bar-mode 0)
;; モードラインに時刻表示
(setq display-time-string-forms
'(month "/" day " " dayname " " 24-hours ":" minutes))
(display-time)
;; 対応する括弧を強調表示
(setq show-paren-delay 0)
(show-paren-mode t)
;; 末尾の空白を表示
(setq-default show-trailing-whitespace t)
;; color-theme
;; http://www.nongnu.org/color-theme/
;; http://code.google.com/p/gnuemacscolorthemetest/
(when (require 'color-theme nil t)
(color-theme-initialize)
(color-theme-clarity))
;; term の色設定 (色は visor 設定から持ってきた)
(setq ansi-term-color-vector
[unspecified
"black"
"#ff6c60" ; Red
"#a8ff60" ; Green
"#ffffb6" ; Yellow
"#96cbfe" ; Blue
"#ff73fd" ; Magenta
"#9dffff" ; Cyan
"white"])
;; Mac用文字コード設定
(set-language-environment "Japanese")
(require 'ucs-normalize)
(prefer-coding-system 'utf-8-hfs)
(setq file-name-coding-system 'utf-8-hfs)
(setq local-coding-system 'utf-8-hfs)
;; フォント設定
(set-face-attribute 'default nil
:family "Monaco"
:height 130)
(let ((ja-font-family "Hiragino_Kaku_Gothic_Pro"))
(set-fontset-font nil 'japanese-jisx0208 (font-spec :family ja-font-family))
(set-fontset-font nil 'japanese-jisx0212 (font-spec :family ja-font-family))
(set-fontset-font nil 'katakana-jisx0201 (font-spec :family ja-font-family)))
(add-to-list 'face-font-rescale-alist
'(".*Hiragino_Kaku_Gothic_Pro.*" . 1.2))
;; フレーム透過
(set-frame-parameter (selected-frame) 'alpha '(85 75))
;; はじめからフルスクリーンにしておく
(add-hook 'window-setup-hook 'ns-toggle-fullscreen)
;;; キーバインド
;; C-h を BackSpace に
(keyboard-translate ?\C-h ?\C-?)
;; M-v 苦手なので Vim で慣れ親しんだ C-u を scroll-down に
(global-set-key (kbd "C-u") 'scroll-down)
;; C-u の代わりに C-q を universal-argument に
(global-set-key (kbd "C-q") 'universal-argument)
;; バッファの先頭、末尾に移動(M-<, M-> は押しにくいので)
(global-set-key (kbd "C-,") 'beginning-of-buffer)
(global-set-key (kbd "C-.") 'end-of-buffer)
;; M-g で Goto line
(global-set-key (kbd "M-g") 'goto-line)
;; C-t で動的略語展開
(global-set-key (kbd "C-t") 'dabbrev-expand)
;; C-m でインデントも
(global-set-key (kbd "C-m") 'newline-and-indent)
;; M-RET でフルスクリーン
(global-set-key (kbd "M-RET") 'ns-toggle-fullscreen)
;; フォントサイズ変更
(global-set-key (kbd "s-;") (lambda () (interactive) (text-scale-increase 1)))
(global-set-key (kbd "s-+") (lambda () (interactive) (text-scale-increase 1)))
(global-set-key (kbd "s--") (lambda () (interactive) (text-scale-decrease 1)))
(global-set-key (kbd "s-0") (lambda () (interactive) (text-scale-increase 0)))
;; ファイルをドラッグアンドドロップしたときに insert ではなく find
(global-set-key [ns-drag-file] 'ns-find-file)
;; key-chord.el
(require-or-install 'key-chord
(auto-install-from-emacswiki "key-chord.el")
(setq key-chord-two-keys-delay 0.04)
(setq key-chord-one-key-delay 0.15)
(key-chord-mode 1)
(key-chord-define-global "jk" 'view-mode)
(key-chord-define-global "gg" 'beginning-of-buffer)
(key-chord-define-global "GG" 'end-of-buffer))
;;; view-mode
;; read-only ファイルを view-mode で開く
(setq view-read-only t)
;; キーバインド
(defun view-mode-hooks ()
;; vi like
(define-key view-mode-map (kbd "h") 'backward-char)
(define-key view-mode-map (kbd "l") 'forward-char)
(define-key view-mode-map (kbd "j") 'next-line)
(define-key view-mode-map (kbd "k") 'previous-line)
(define-key view-mode-map (kbd "v") 'set-mark-command)
;; less like
(define-key view-mode-map (kbd "f") 'View-scroll-page-forward)
(define-key view-mode-map (kbd "b") 'View-scroll-page-backward)
(define-key view-mode-map (kbd "g") 'beginning-of-buffer)
(define-key view-mode-map (kbd "G") 'end-of-buffer))
(add-hook 'view-mode-hook 'view-mode-hooks)
;; viewer.el
(require-or-install 'viewer
(auto-install-from-emacswiki "viewer.el")
;; 書き込みできないファイルで view-mode から抜けないようにする
(viewer-stay-in-setup)
;; modeline の色設定
(setq viewer-modeline-color-unwritable "tomato"
viewer-modeline-color-view "orange")
(viewer-change-modeline-color-setup)
;; すべてのファイルを view-mode で開く
;; viewer-aggressive-writable が使われてないので advice
(defadvice aggressive-view-mode (after aggressive-writable activate)
(when (and view-mode
(not viewer-aggressive-writable))
(setq buffer-read-only t)))
(setq viewer-aggressive-writable nil)
(viewer-aggressive-setup t))
;;; coding style
;; php sf1 (symfony 1, OpenPNE3)
(defun coding-style-php-sf1 ()
(c-set-style "bsd")
(setq tab-width 2
c-basic-offset 2
indent-tabs-mode nil))
;; php standard (PEAR, Zend, OpenPNE2)
(defun coding-style-php-standard ()
(c-set-style "k&r")
(setq tab-width 4
c-basic-offset 4
indent-tabs-mode nil))
(defun php-mode-hook-for-coding-style ()
;; anything-project がある場合はプロジェクトを特定してコーディングスタイルを変更
(if (functionp 'ap:get-root-directory)
(destructuring-bind (root-dir key) (ap:get-root-directory)
(cond
((eq key 'symfony) (coding-style-php-sf1))
(t (coding-style-php-standard))))
(coding-style-php-standard)))
(add-hook 'php-mode-hook 'php-mode-hook-for-coding-style)
;; CSS
(defun coding-style-css ()
(setq css-indent-offset 2))
(add-hook 'css-mode-hook 'coding-style-css)
;;; elisp
;; aything.el
(require-or-install 'anything-startup
(auto-install-batch "anything")
(setq anything-idle-delay 0.3
anything-quick-update t
anything-su-or-sudo "sudo")
;; anything を左右分割で使う
;; http://d.hatena.ne.jp/kitokitoki/20110102/p2
(defadvice anything-default-display-buffer (around my-anything-default-display-buffer activate)
(delete-other-windows)
(split-window (selected-window) nil t)
(pop-to-buffer buf))
(defun my-anything ()
(interactive)
(anything-other-buffer
'(anything-c-source-buffers+
anything-c-source-recentf
anything-c-source-files-in-current-dir+
anything-c-source-extended-command-history
anything-c-source-emacs-commands)
"*my-anything*"))
(global-set-key (kbd "C-;") 'my-anything)
(define-key anything-map (kbd "C-;") 'abort-recursive-edit)
(global-set-key (kbd "C-x h") 'anything-apropos)
(require 'anything-gtags nil t)
;; anything-project.el
(require-or-install 'anything-project
(auto-install-from-url "https://github.com/imakado/anything-project/raw/master/anything-project.el")
;; symfony project の場合 ^cache を anything-project-grep の検索対象から除外
(defadvice ap:build-grep-command
(after ap:build-grep-command-ignore-cache (key) activate)
(if (eq key 'symfony)
(let ((commands (split-string ad-return-value " | "))
(egrep-command (ap:get-egrep-command)))
(setcdr commands
(cons (concat egrep-command " -v \"^cache\"")
(cdr commands)))
(setq ad-return-value (mapconcat 'identity commands " | ")))))
;;; anything-project-grep-definition: PHPの定義っぽい部分だけを検索する
;; ap:do-project-grep 内で使われる read-string の結果を変更する advice
(defadvice read-string
(after read-string-with-definition-pattern disable)
(setq ad-return-value
(concat "(class|function|const|interface|namespace) +" ad-return-value "\\b")))
;; anything-project-grep-definition の中でだけ advice を有効にする
(defun anything-project-grep-definition ()
(interactive)
(ad-enable-advice 'read-string 'after 'read-string-with-definition-pattern)
(ad-activate 'read-string)
(anything-project-grep)
(ad-deactivate 'read-string)
(ad-disable-advice 'read-string 'after 'read-string-with-definition-pattern))
;; キーバインド設定
(global-set-key (kbd "C-:") 'anything-project)
(global-set-key (kbd "C-@") 'anything-project-grep)
(global-set-key (kbd "<f8>") 'anything-project-grep-definition)
;; プロジェクト設定
;; symfony 1
(ap:add-project
:name 'symfony
:look-for 'ap:symfony-root-detector
:grep-extensions '("\\.php" "\\.yml"))
;; OpenPNE2
(ap:add-project
:name 'openpne2
:look-for 'ap:openpne2-root-detector
:grep-extensions '("\\.php" "\\.inc" "\\.ini"))
(defun ap:openpne2-root-detector (files)
(ap:all-files-exist '("config.php.sample" "public_html" "webapp") files))
))
;; auto-complete.el
(require-or-install 'auto-complete
(auto-install-from-emacswiki "auto-complete.el")
(global-auto-complete-mode t)
(setq ac-dwim t)
(define-key ac-complete-mode-map (kbd "C-n") 'ac-next)
(define-key ac-complete-mode-map (kbd "C-p") 'ac-previous)
;; mode line に表示しない
(setcdr (assq 'auto-complete-mode minor-mode-alist) '("")))
;; php-mode.el
(require-or-install 'php-mode
(auto-install-from-url "http://php-mode.svn.sourceforge.net/svnroot/php-mode/tags/php-mode-1.5.0/php-mode.el"))
;; php-completion.el
(autoload-or-install 'php-completion-mode "php-completion"
(auto-install-batch "php-completion"))
(defun php-mode-hook-for-php-completion ()
(when (require 'php-completion nil t)
(php-completion-mode t)
(define-key php-mode-map (kbd "C-o") 'phpcmp-complete)
(when (require 'auto-complete nil t)
(make-variable-buffer-local 'ac-sources)
(add-to-list 'ac-sources 'ac-source-php-completion)
(auto-complete-mode t))))
(add-hook 'php-mode-hook 'php-mode-hook-for-php-completion)
;; yaml-mode.el
(autoload-or-install 'yaml-mode "yaml-mode"
(auto-install-from-url "https://github.com/yoshiki/yaml-mode/raw/master/yaml-mode.el"))
(add-to-list 'auto-mode-alist '("\\.yml$" . yaml-mode))
;; gtags.el
;; $ brew install global
;; $ cp $(brew --prefix global)/share/gtags/gtags.el ~/.emacs.d/elisp
(require 'gtags nil t)
(defun enable-gtags-mode () (gtags-mode t))
(add-hook 'php-mode-hook 'enable-gtags-mode)
;; egg.el
(when (executable-find "git")
(require-or-install 'egg
(auto-install-from-url "https://github.com/byplayer/egg/raw/master/egg.el")))
;; redo+.el
(require-or-install 'redo+
(auto-install-from-emacswiki "redo+.el")
(global-set-key (kbd "C-_") 'redo))
;; undohist.el
(require-or-install 'undohist
(auto-install-from-url "http://cx4a.org/pub/undohist.el")
(undohist-initialize))
;; undo-tree.el
(require-or-install 'undo-tree
(auto-install-from-url "http://www.dr-qubit.org/undo-tree/undo-tree.el")
(global-undo-tree-mode)
;; mode line に表示しない
(setq undo-tree-mode-lighter ""))
;; point-undo.el
(require-or-install 'point-undo
(auto-install-from-emacswiki "point-undo.el")
(global-set-key (kbd "<f7>") 'point-undo)
(global-set-key (kbd "S-<f7>") 'point-redo))
;; auto-save-buffers.el
(require-or-install 'auto-save-buffers
(prog2
(prefer-coding-system 'euc-jp)
(auto-install-from-url "http://homepage3.nifty.com/oatu/emacs/archives/auto-save-buffers.el")
(prefer-coding-system 'utf-8-hfs))
(run-with-idle-timer 0.5 t 'auto-save-buffers))
;; savekill.el
(require-or-install 'savekill
(auto-install-from-emacswiki "savekill.el"))
;; multi-term.el
;; 要 terminfo 作成
;; tic -o ~/.terminfo $(brew --prefix emacs)/share/emacs/23.2/etc/e/eterm-color.ti
(require-or-install 'multi-term
(auto-install-from-emacswiki "multi-term.el")
(setq multi-term-program shell-file-name))
;; js2-mode
(autoload-or-install 'js2-mode "js2-20090723b"
(auto-install-from-url "http://js2-mode.googlecode.com/files/js2-20090723b.el"))
(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))
(add-to-list 'auto-mode-alist '("\\.js\\.src$" . js2-mode))
;;; 使わなくなった設定 (あとで別ファイルか何かに移す)
;; ;; ElScreen
;; ;; http://www.morishima.net/~naoto/software/elscreen/index.php.ja
;; ;; APEL (依存)
;; ;; http://www.kanji.zinbun.kyoto-u.ac.jp/~tomo/elisp/APEL/index.html.ja
;; (when (require 'elscreen nil t)
;; (setq elscreen-prefix-key (kbd "C-z"))
;; (setq elscreen-display-tab nil)
;; (define-key elscreen-map (kbd "SPC") 'elscreen-next)
;; (define-key elscreen-map (kbd "C-SPC") 'elscreen-next))
;; ;; twittering-mode
;; ;; https://github.com/hayamiz/twittering-mode/
;; (when (require 'twittering-mode nil t)
;; ;; 暗号化に GnuPG が必要
;; ;; $ brew install gnupg
;; (setq twittering-use-master-password t)
;; ;; icon-mode
;; ;; size 48 以外だと縮小に convert コマンドが必要
;; ;; $ brew install imagemagick
;; (when (executable-find "convert")
;; (twittering-icon-mode 1)
;; (setq twittering-convert-fix-size 24)))
;; ;; gist.el
;; ;; (auto-install-from-url "https://github.com/defunkt/gist.el/raw/master/gist.el")
;; ;; anything-gist.el
;; ;; (auto-install-from-gist "https://gist.github.com/467982")
;; (when (require 'gist nil t)
;; (require 'anything-gist nil t))
;; ;;; This was installed by package-install.el.
;; ;;; This provides support for the package system and
;; ;;; interfacing with ELPA, the package archive.
;; ;;; Move this code earlier if you want to reference
;; ;;; packages in your .emacs.
;; (when
;; (load
;; (expand-file-name "~/.emacs.d/elpa/package.el"))
;; (package-initialize))
;; ;; jaspace.el
;; ;; (auto-install-from-url "http://homepage3.nifty.com/satomii/software/jaspace.el")
;; (when (require 'jaspace nil t)
;; (setq jaspace-highlight-tabs t)
;; ;; mode line に表示しない
;; (setq jaspace-mode-string ""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment