; .emacs
;;; uncomment this line to disable loading of "default.el" at startup
;; (setq inhibit-default-init t)

;;; basic configuration
(line-number-mode 1)
(column-number-mode 1)
(global-font-lock-mode t)
(show-paren-mode t)
(transient-mark-mode t) ; enable visual feedback on selections
(global-set-key "\C-x\C-b" 'electric-buffer-list)
(global-set-key "\C-cg" 'goto-line)
(ansi-color-for-comint-mode-on) ; shellモードで色をつける
(add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt) ; shellモードでパスワードを伏字にする

;;; M-pで履歴を補完
(add-hook 'comint-mode-hook
	  (lambda ()
;	    (define-key comint-mode-map "\C-a" 'tel-comint-beginning-of-line)
	    (define-key comint-mode-map "\M-p" 'comint-previous-matching-input-from-input)
	    (define-key comint-mode-map "\M-n" 'comint-next-matching-input-from-input)

(setq make-backup-files t) ; backup-file 作る
(defun make-backup-file-name (filename)
   (concat                 ; backup-file名を
    "~/.emacs-backup/"     ; ~/.emacs-backup/filename~にする 
    (file-name-nondirectory filename) 
   (file-name-directory filename)))

;; autoinsert(型紙)
(load "autoinsert")                             ;;新しいEmacsなら(auto-insert-mode 1)
(add-hook 'find-file-hooks 'auto-insert)
(setq auto-insert-alist
    (append '(                                  ;;テンプレートファイルのファイル名のリスト
        ("\\.tex" . "tex-insert.tex")    
        ("\\.htm" . "html-insert.html")
        ("\\.html" . "html-insert.html")
        ("\\.pl". "template.pl")
	("\\.c". "template.c")
        ) auto-insert-alist))
(setq auto-insert-directory "~/.emacs.d/template/")  ;;テンプレートファイルのディレクトリ

;; nnで「ん」
(setq enable-double-n-syntax t)

;; C-zを押してもアイコン化しない
(cond (window-system
        (global-unset-key "\C-z")

(if (boundp 'window-system)
    (setq initial-frame-alist
	  (append (list
		   '(width . 90)
		   '(height . 65)
(setq default-frame-alist initial-frame-alist)

;; key bind
(if (boundp 'MULE)
    (define-key global-map "\C-x\e" 'repeat-complex-command))
(define-key global-map "\C-x\C-b" 'electric-buffer-list)
 (define-key global-map "\C-h"   'backward-delete-char)
 (define-key global-map "\C-c\C-h" 'help-for-help)
 ((eq window-system 'x)
  (define-key global-map [delete] 'delete-char))
 ((not window-system)
  (load "term/keyswap")))

;; 画面から出たとき一行だけスクロールさせる
(setq scroll-conservatively 1)

;; スクリプトを保存する時,自動的に chmod +x を行なうようにする
(defun make-file-executable ()
  "Make the file of this buffer executable, when it is a script source."
    (if (string= "#!" (buffer-substring-no-properties 1 (min 3 (point-max))))
        (let ((name (buffer-file-name)))
          (or (equal ?. (string-to-char (file-name-nondirectory name)))
              (let ((mode (file-modes name)))
                (set-file-modes name (logior mode (logand (/ mode 4) 73)))
                (message (concat "Wrote " name " (+x)"))))))))
(add-hook 'after-save-hook 'make-file-executable)

;;; Default coding
(set-language-environment "Japanese")
(set-default-coding-systems 'euc-jp)
(set-terminal-coding-system 'euc-jp)
(set-keyboard-coding-system 'euc-jp)
(set-buffer-file-coding-system 'euc-jp)

(setq load-path
       (list (expand-file-name "~/mylisp")) load-path))

(defun my-sort-buffer-by (cbuf slst)
  ;; bury-buffer を使って、カレントバッファが先頭に来るようにソートする
  (cond ((eq slst '()) '())
        ((eq cbuf (car slst)) (my-sort-buffer-by cbuf (reverse (cdr slst))))
        (t (bury-buffer (car slst))
           (my-sort-buffer-by cbuf (cdr slst)))))

(defun my-sort-buffer-by-name (x y)
  (if (or (buffer-file-name y) (buffer-file-name x))
      ;; どちらか特殊バッファならば buffer-name でソートする
      (string< (buffer-file-name y) (buffer-file-name x))
    (string< (buffer-name y) (buffer-name x))))

(defun my-sort-buffer ()
  "Sort buffer list by name"
  (let* ((cbuf (current-buffer))
         (blst (copy-sequence (buffer-list)))
         (slst (sort blst 'my-sort-buffer-by-name)))
    (my-sort-buffer-by cbuf slst)))

;;(define-key global-map "\C-_:" 'undo)
(define-key global-map "\C-c;" 'comment-region)      ; コメントアウト
(define-key global-map "\C-c:" 'uncomment-region)    ; コメント解除

;; http://qiita.com/fujii_0v0/items/24f9427220d9ac9f7f88
(define-key global-map [(C +)]   'text-scale-increase)
(define-key global-map [(C -)] 'text-scale-decrease)