Emacs 配置

2013-10-12 配置了两个夜晚,终于把emacs python 给配置好了

以下是配置文件

;;;;;;;;;;;;;;;;;el-get;;;;;;;;;;;;;;;;;;;
(add-to-list 'load-path "~/.emacs.d/el-get/el-get")
(unless (require 'el-get nil 'noerror)
  (with-current-buffer
      (url-retrieve-synchronously
       "https://raw.github.com/dimitri/el-get/master/el-get-install.el")
    (let (el-get-master-branch)
      (goto-char (point-max))
      (eval-print-last-sexp))))
(el-get 'sync)


;;;;;;;;;;;;;;;;常规设置;;;;;;;;;;;;;;;;;;;
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(default ((t (:family "YaHei Consolas Hybrid" :foundry "microsoft" :slant normal :weight normal :height 120 :width normal)))))

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(current-language-environment "UTF-8")
 '(custom-enabled-themes (quote (tango-dark)))
 '(scroll-bar-mode nil)
 '(show-paren-mode t)
 '(size-indication-mode t)
 '(tool-bar-mode nil))

;;http://emacswiki.org/emacs/InteractivelyDoThings 
(require 'ido)
(ido-mode t)

;;;tabbar
(add-to-list 'load-path "~/.emacs.d")
(require 'tabbar)
(tabbar-mode t)
(set-face-foreground 'minibuffer-prompt "Green")
(menu-bar-mode -1)
(defun tabbar-buffer-groups ()
   "Return the list of group names the current buffer belongs to.
 This function is a custom function for tabbar-mode's tabbar-buffer-groups.
 This function group all buffers into 3 groups:
 Those Dired, those user buffer, and those emacs buffer.
 Emacs buffer are those starting with" 
   (list
    (cond
     ((string-equal "*" (substring (buffer-name) 0 1))
      "Emacs Buffer"
      )
     ((eq major-mode 'dired-mode)
      "Dired"
      )
     (t
      "User Buffer"
      )
     ))) ;; from Xah Lee


;;start fullscreen
(defun toggle-fullscreen ()
  (interactive)
  (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
                 '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
  (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
                 '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
)
(toggle-fullscreen)

;;auto complete;;
(add-to-list 'load-path "/home/shalei/.emacs.d/el-get/auto-complete")    ; This may not be appeared if you have already added.
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "/home/shalei/.emacs.d/el-get/auto-complete/ac-dict")
(ac-config-default)

;;enable linum mode
(add-hook 'python-mode-hook 'linum-mode)

;;disable splash screen and minibuffer messages
(setq inhibit-splash-screen t)
(setq inhibit-startup-echo-area-message t)

;;hilight matched parens
(show-paren-mode t) 

;;enable charset autodetection
(require 'unicad)
(unicad-enable)

;;;;auto save
(require 'desktop)
  (desktop-save-mode 1)
  (defun my-desktop-save ()
    (interactive)
    ;; Don't call desktop-save-in-desktop-dir, as it prints a message.
    (if (eq (desktop-owner) (emacs-pid))
        (desktop-save desktop-dirname)))
  (add-hook 'auto-save-hook 'my-desktop-save)


;;;;;;;;;;;;;;;;;;;;;;python相关;;;;;;;;;;;;;;;;;;;;;;
(add-to-list 'load-path "~/.emacs.d/")
(require 'python)

;;set ipython as default python shell
(setq
 python-shell-interpreter "ipython"
 python-shell-interpreter-args ""
 python-shell-prompt-regexp "In \\[[0-9]+\\]: "
 python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
 python-shell-completion-setup-code
   "from IPython.core.completerlib import module_completion"
 python-shell-completion-module-string-code
   "';'.join(module_completion('''%s'''))\n"
 python-shell-completion-string-code
   "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")

;;add colors to shell
(setq ansi-term-color-vector [unspecified "#3f3f3f" "#cc9393" "#7f9f7f" "#f0dfaf" "#DD6600" "#dc8cc3" "#93e0e3" "#dcdccc"])

;(setq py-install-directory "/home/shalei/.emacs.d/python-mode/")
;(add-to-list 'load-path py-install-directory)
;(require 'python-mode)
  
(add-hook 'python-mode-hook 'jedi:setup)
(setq jedi:setup-keys t)                      ; optional
(setq jedi:complete-on-dot t)

(add-to-list 'load-path "/home/shalei/.emacs.d/el-get/package/elpa/autopair-0.3")
(require 'autopair)
(autopair-global-mode) ;; to enable in all buffers

;;Auto Syntax Error Hightlight
(when (load "flymake" t)
  (defun flymake-pyflakes-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 "pyflakes" (list local-file))))
  (add-to-list 'flymake-allowed-file-name-masks
               '("\\.py\\'" flymake-pyflakes-init)));

(add-hook 'find-file-hook 'flymake-find-file-hook)


;;;;折叠代码;;;;;;;;;;;;
(add-hook 'python-mode-hook 'my-python-hook)
(defun py-outline-level ()
"This is so that `current-column` DTRT in otherwise-hidden text"
;; from ada-mode.el
(let (buffer-invisibility-spec)
    (save-excursion
      (skip-chars-forward "\t ")
      (current-column))))
; this fragment originally came from the web somewhere, but the outline-regexp
; was horribly broken and is broken in all instances of this code floating
; around. Finally fixed by Charl P. Botha <<a href="http://cpbotha.net/">http://cpbotha.net/</a>>
(defun my-python-hook ()
(setq outline-regexp "[^ \t\n]\\|[ \t]*\\(def[ \t]+\\|class[ \t]+\\)")
; enable our level computation
(setq outline-level 'py-outline-level)
; do not use their \C-c@ prefix, too hard to type. Note this overides
;some python mode bindings
(setq outline-minor-mode-prefix "\C-f")
; turn on outline mode
(outline-minor-mode t)
; initially hide all but the headers
(hide-body)
(show-paren-mode 1))

 

插件安装,详见http://caisah.info/emacs-for-python/

 

caisah

Internet Addict

Skip to content

Emacs text-editor setup for Python development plus a total beginner’s guide

Last week I decided it was time to switch to a more serious text editor. I already had my research done so all I had to do is start using Emacs. I knew that it will have a steep learning curve so I was prepared for what was to come. Of course in a few days, all I could get were a few information, but it got me to the stage where I can customize it and use it on a regular basis. Long story short I’m not an expert but I got things done.

I’ll split this article into 2 parts. The first one is for people who use Emacs but only need a list of packages, and the second is for newbies.

 

I. Python specific packages for people already familiar to Emacs

I had a little help from Jessica Hamrick and John D. Cook so here is the list of the packages to install or activate:

If you have any other suggestions feel free to add them in the comments. There was also a discussion on G+ Python Group.

 

II. How to customize Emacs(24) for Python – a total beginner’s tutorial

Learn how to use the shortcuts

If you don’t already know this, Emacs is optimized for keyboard use. So you won’t need the mouse. :)
A common subject of misunderstanding for newcomers is the keyboard shortcuts usage. All you have to know is how to read them(the shortcuts). So C-h is Ctrl+h and M-x is Alt(Command)+x. (M stands for meta). For example, a command like C-h t is Ctrl+h followed by t and RETurn(Enter). M-x quit-window means you have to press Alt(Command)+x and type quit-window after that, followed by RET(Enter).

Read the Emacs tutorial

Emacs has a great tutorial that teaches you how to navigate and some other basic things. Don’t try to be smart and skip it. It was written/improved over the years and it is a must for all Emacs newbies.
So press C-h t and read it!

Learn about Emacs customization

Emacs customization file is named .emacs and it’s being located in the /home/[your-name] folder(aka your home folder) on Ubuntu or other Gnu/Linux OS. There is also a .emacs.d folder where all the packages are being installed.

For Windows I cite from the official website:

On Windows, the .emacs file may be called _emacs for backward compatibility with DOS and FAT filesystems where filenames could not start with a dot. Some users prefer to continue using such a name, because Explorer cannot create a file with a name starting with a dot, even though the filesystem and most other programs can handle it. In Emacs 22 and later, the init file may also be called .emacs.d/init.el. Many of the other files that are created by lisp packages are now stored in the .emacs.d directory too, so this keeps all your Emacs related files in one place.

All the files mentioned above should go in your HOME directory. The HOME directory is determined by following the steps below:

  1. If the environment variable HOME is set, use the directory it indicates.
  2. If the registry entry HKCU\SOFTWARE\GNU\Emacs\HOME is set, use the directory it indicates.
  3. If the registry entry HKLM\SOFTWARE\GNU\Emacs\HOME is set, use the directory it indicates. Not recommended, as it results in users sharing the same HOME directory.
  4. If C:\.emacs exists, then use C:/. This is for backward compatibility, as previous versions defaulted to C:/ if HOME was not set.
  5. Use the user’s AppData directory, usually a directory called Application Data under the user’s profile directory, the location of which varies according to Windows version and whether the computer is part of a domain.

Within Emacs, <~> at the beginning of a file name is expanded to your HOME directory, so you can always find your .emacs file with C-x C-f ~/.emacs.

Add package archive links

For those used with Ubuntu this may seem familiar. Basically you add the links for package installation.
Add to your .emacs file the following lines:

(require'package)
(setq package-archives '(("gnu"."http://elpa.gnu.org/packages/")("marmalade"."http://marmalade-repo.org/packages/")("melpa"."http://melpa.milkbox.net/packages/")))

These are the links to Elpa, Marmelade and Melpa.

Now you can see a list of packages by using M-x package-list-packages.

If you wand to install a new package type M-x package-install RET, and type the name of the package. If you don’t remember the exact name of the package you wanted to install press TAB (M-x package-install RET and TAB afterwards). You will be prompted a window with all the possible  completions. [You can use TAB anytime you don't know what to type next.]

Choose your Emacs theme

Use M-x customize-themes and try some themes. Chose one you like. If you don’t like any of the default ones, you can also install your theme using package.

Some popular themes are Zenburn themes. To install them, press M-x package-install zenburn-theme (First M-x Package-install, press Enter and then zenburn-theme. From now on this will be the preferred way of writing composed key shortcuts.) To load it automatically on Emacs startup, add this to your .emacs file:

(load-theme 'zenburn t)

[later edit] If you have problems loading the theme take a look at Santiago’s comment.

Set Emacs to start fullscreen

By default Emacs starts in a minimized window. To change this add the following lines to the .emacs file:

(defun toggle-fullscreen ()(interactive)(x-send-client-message nil0nil"_NET_WM_STATE"32'(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
  (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
	    		 '(2"_NET_WM_STATE_MAXIMIZED_HORZ"0)))(toggle-fullscreen)

Set Emacs to save buffers on exit

Emacs starts a new session every time you open it. To remember your buffers(files) after restart add the following to your .emacs file:

(require'desktop)
  (desktop-save-mode 1)
  (defun my-desktop-save ()
    (interactive)
    ;; Don't call desktop-save-in-desktop-dir,as it prints a message.(if(eq (desktop-owner)(emacs-pid))(desktop-save desktop-dirname)))(add-hook 'auto-save-hook 'my-desktop-save)

Enable ido-mode

Ido-mode(InteractivelyDoThings) is installed by default. It helps you with auto-completion when you want to change between buffers and other things. If you want to find out more read the docs. :D
To enable it add these lines to your .emacs file:

(require'ido)
(ido-mode t)

Color your shell text

If you use Emacs there is a very high probability you use your terminal often(or you intend to do). If you want your Emacs terminal emulator to have some colors add to your .emacs file the line:

(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

To open your terminal in a new buffer type: M-x term. It is worth mentioning that Emacs supports multiple different shells. Read this article from Mastering Emacs to find out more about it.

Install el-get

El-get is similar to package.el(package) except for the fact that it automatically installs all the dependencies. So you don’t have to care about anything else when you install a package.
To install it, change to your scratch buffer by typing C-x b scratch RET. Paste this code in by yanking (type C-y):

;;So the idea is that you copy/paste this code into your *scratch* buffer,;; hit C-j,and you have a working developper edition of el-get.(url-retrieve
 "https://raw.github.com/dimitri/el-get/master/el-get-install.el"(lambda(s)(let(el-get-master-branch)(goto-char(point-max))(eval-print-last-sexp))))

Hit C-j to execute.
Add these lines to your .emacs file:

(add-to-list 'load-path "~/.emacs.d/el-get/el-get")
(unless (require 'el-getnil'noerror)
  (with-current-buffer
      (url-retrieve-synchronously
       "https://raw.github.com/dimitri/el-get/master/el-get-install.el")
    (let (el-get-master-branch)
      (goto-char (point-max))
      (eval-print-last-sexp))))
(el-get 'sync)

You’re done. To list all the packages M-x el-get-list-packages. If you want to install something from el-get repo just M-x el-get-install and type the name of the package.

Install Jedi.el

Jedi.el is a Python auto-completion package for Emacs.
To install it you first have to install virtualenv. If ou are on Ubuntu:

sudo apt-get install python-virtualenv

If you use Windows check the documentation or first install pip and after that

pip install virtualenv

Now you can install Jedi using el-get: M-x el-get-install jedi
To enable Jedi add to your .emacs file:

(add-hook 'python-mode-hook 'auto-complete-mode)(add-hook 'python-mode-hook 'jedi:ac-setup)

If you encounter any problems follow the instructions from the troubleshooting section. Jedi has a very good documentation and the developer behind this project is very helpful.

Install Flycheck

Flycheck (aka “Flymake done right”) is a modern on-the-fly syntax checking extension for GNU Emacs 24.
Install it using package: M-x package-install flycheck.
Add to your .emacs file:

(add-hook 'after-init-hook #'global-flycheck-mode)

Install Autopair

Autopair is an extension to the Emacs text editor that automatically pairs braces and quotes.
<strong">Install it using package: M-x package-install autopair.
Add to your .emacs file:

(require'autopair)
(autopair-global-mode) ;; to enable in all buffers

Horray! You’re done.
This is my personal Emacs configuration kept up to date.
If you get in any trouble, your package doesn’t work, you don’t know how to use it or customize it, check the documentation. If that doesn’t help search or ask StackOverflow. Someone will help you eventually or you will be able to solve the problem by yourself after reading a few answers.

 

 
posted @ 2013-10-13 10:39  小沙  阅读(768)  评论(0编辑  收藏  举报