pythonを幸せに書くための.emacs

何が出来るか

  • flymakeとpyflakesを連携させて、オンザフライで構文チェックやらをしてくれて、ヤバげなところをハイライトしてくれる
  • 別にpythonだけに限らないけれど、prefixを入力してtab押すと、開いているファイル内のtokenを補完してくれる
  • pythonコードだと、文字コード指定がない場合勝手に先頭に文字コード指定を挿入する
  • あと色とか弄ってる。Emacs.appで幸せになれる
  • anything.elはろくに育ててない

先に入れておくもの

  • pyflakes

emacs拡張

  • anything.el
  • auto-complete.el
  • python-mode

emacs 23系

python-modeはlunchpadからダウンロード。anythingはauto-installで入ると楽。auto-completeは公式から安定版を落としてきてmake

emacs24系

python-modeはlunchpadからダウンロード。anythingとauto-completeはelpaで入れる。

.emacs

(show-paren-mode)

;; 編集行を目立たせる(現在行をハイライト表示する)
(defface hlline-face
  '((((class color)
      (background dark))
     (:background "dark slate gray"))
    (((class color)
      (background light))
                                        ;(:background "ForestGreen"))
     (:background "LemonChiffon1"))
    (t
     ()))
  "*Face used by hl-line.")
(defface hlline-face-term
  '((((class color)
      (background dark))
     (:background "dark slate gray"))
    (((class color)
      (background light))
     (:background "dark slate gray"))
    (t
     ()))
  "*Face used by hl-line.")
(if window-system
    (setq hl-line-face 'hlline-face)
  (setq hl-line-face 'hlline-face-term))

;; (setq hl-line-face 'underline) ; 下線
(global-hl-line-mode)

;; tab
(setq default-tab-width 4)
(setq-default tab-width 4 indent-tabs-mode nil)

(require 'flymake) 

;; python-mode
(require 'python-mode)
(setq auto-mode-alist (cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist (cons '("python" . python-mode)
                                   interpreter-mode-alist))
(autoload 'python-mode "python-mode" "Python editing mode." t)
 
(add-hook 'python-mode-hook
          (function (lambda ()
                      (setq py-indent-offset 4)
                      (setq indent-tabs-mode nil)
                      (flymake-python-load))))
 
;; flymake python
(defun flymake-python-init ()
  (let* ((temp-file (flymake-init-create-temp-buffer-copy
                     'flymake-create-temp-inplace))
         (local-file (file-relative-name
                      temp-file
                      (file-name-directory buffer-file-name))))
    (list "/opt/local/bin/pyflakes" (list local-file))))

(defconst flymake-allowed-python-file-name-masks '(("\\.py$"
                                                    flymake-python-init)))
(defvar flymake-python-err-line-patterns
  '(("\\(.*\\):\\([0-9]+\\):\\(.*\\)" 1 2 nil 3)))

(defun flymake-python-load ()
  (interactive)
  (defadvice flymake-post-syntax-check
    (before flymake-force-check-was-interrupted)
    (setq flymake-check-was-interrupted t))
  (ad-activate 'flymake-post-syntax-check)
  (setq flymake-allowed-file-name-masks
        (append flymake-allowed-file-name-masks
                flymake-allowed-python-file-name-masks))
  (setq flymake-err-line-patterns flymake-python-err-line-patterns)
  (flymake-mode t))

;;flymake-ler(file line type text &optional full-file)
(defun show-fly-err-at-point ()
  "If the cursor is sitting on a flymake error, display the
message in the minibuffer"
  (interactive)
  (let ((line-no (line-number-at-pos)))
    (dolist (elem flymake-err-info)
      (if (eq (car elem) line-no)
          (let ((err (car (second elem))))
            (message "%s" (fly-pyflake-determine-message err)))))))

(defun fly-pyflake-determine-message (err)
  "pyflake is flakey if it has compile problems, this adjusts the 
message to display, so there is one "
  (cond ((not (or (eq major-mode 'Python) (eq major-mode 'python-mode) t)))
        ((null (flymake-ler-file err))
         ;; normal message do your thing
         (flymake-ler-text err))
        (t ;; could not compile err
         (format "compile error, problem on line %s" (flymake-ler-line err)))))
 
(defadvice flymake-goto-next-error (after display-message activate compile)
  "Display the error in the mini-buffer rather than having to mouse over it"
  (show-fly-err-at-point))
 (global-set-key (kbd "C-c e") 'flymake-goto-next-error)

 (defadvice flymake-goto-prev-error (after display-message activate compile)
   "Display the error in the mini-buffer rather than having to mouse over it"
   (show-fly-err-at-point))
 
(defadvice flymake-mode (before post-command-stuff activate compile)
 "Add functionality to the post command hook so that if the
 cursor is sitting on a flymake error the error information is
 displayed in the minibuffer (rather than having to mouse over
 it)"
  (set (make-local-variable 'post-command-hook)
       (cons 'show-fly-err-at-point post-command-hook)))

;;anything
(require 'anything-startup)

;;.pyファイルを開いた際に、文字コード指定がなければ文字コード指定文字列を追加
(defun my-short-buffer-file-coding-system (&optional default-coding)
  (let ((coding-str (format "%S" buffer-file-coding-system)))
   (cond ((string-match "shift-jis" coding-str) 'shift_jis)
         ((string-match "euc-jp" coding-str) 'euc-jp)
         ((string-match "utf-8" coding-str) 'utf-8)
         (t (or default-coding 'utf-8)))))

(defun my-insert-file-local-coding ()
  "ファイルの先頭に `coding:' を自動挿入する"
  (interactive)
  (save-excursion
    (goto-line 2) (end-of-line) ; 2行目の行末の移動
    (let ((limit (point)))
      (goto-char (point-min))
      (unless (search-forward "coding:" limit t) ; 2行目以内に `coding:'がない
        (goto-char (point-min))
        ;; #!で始まる場合2行目に記述
        (when (and (< (+ 2 (point-min)) (point-max))
                   (string= (buffer-substring (point-min) (+ 2 (point-min))) "#!"))
          (unless (search-forward "\n" nil t) ; `#!'で始まり末尾に改行が無い場合
            (insert "\n")))                   ; 改行を挿入
        (let ((st (point)))
          (insert (format "-*- coding: %S -*-\n" (my-short-buffer-file-coding-system)))
          (comment-region st (point)))))))

(add-hook 'python-mode-hook 'my-insert-file-local-coding)

;; auto-complete.el
(require 'auto-complete)
(require 'auto-complete-config)
(global-auto-complete-mode t)
(setq ac-auto-start nil)
;; TABで補完起動
(ac-set-trigger-key "TAB")
                                        ;"M-/"で補完中止
                                        ;(define-key ac-completing-map "\M-/" 'ac-stop)
                                        ;補完メニュー表示時のみC-n/C-pで補完候補を選択する
(setq ac-use-menu-map t)
(define-key ac-menu-map "\C-n" 'ac-next)
(define-key ac-menu-map "\C-p" 'ac-previous)


  トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2014-04-23 (水) 18:14:12 (1277d)