📜 ⬆️ ⬇️

Python and emacs, some handy solutions

I'll tell you a little about how to quickly turn emacs into a full-fledged python IDE. Let's start with an interesting way of code completion + as we go, we will add a couple of interesting and useful features. Surely, many people use something like rope (or some solutions of their own), and this is quite reasonable, but not very visual. I will tell about one additional method.

Now by pressing TAB we will get this:
image


1. And so, first of all, open ~ / .emacs and add (if not previously done) the script download directory (add-to-list 'load-path “~/.emacs.d/”) instead of ~ /. emacs.d / can use your own, but I prefer this one (do not close .emacs yet, we will need it later).
')
2. Download files for github.com/m2ym/auto-complete autocompletes and put them in the ~ / .emacs.d / folder or in the folder you specified earlier.
We connect in .emacs
(require 'auto-complete-config)
(global-auto-complete-mode t)


3. Next, download the latest version of yasnippet code.google.com/p/yasnippet/downloads/list we need the files YASnippet Bundle and YASnippet Release.
YASnippet is a template system for emacs. For example, to create a new function, you need to type “def” and press TAB, subsequent presses of TAB will move you inside the created template.

We connect in .emacs
(require 'yasnippet)
(yas/initialize)
(yas/load-directory “~/.emacs.d/snippets”)


4. Download python-mode.el (if suddenly you have not installed it yet). You can get the latest version here: launchpad.net/python-mode

I remind you that we throw everything downloaded into our download folder ~ / .emacs.d / .

5. Install Rope, Ropemacs and Pymacs.

sudo apt-get install pymacs

sudo apt-get install mercurial
mkdir / tmp / rope && cd / tmp / rope
hg clone http://bitbucket.org/agr/rope
hg clone http://bitbucket.org/agr/ropemacs
hg clone http://bitbucket.org/agr/ropemode

Now we need easy_install
wget http://peak.telecommunity.com/dist/ez_setup.py
sudo python ez_setup.py
sudo easy_install rope
ln -s ../ropemode/ropemode ropemacs /
sudo easy_install ropemacs

6. Install pyflakes for automatic syntax checking.
sudo apt-get install pyflakes

7. Create the init_python.el file in our ~ / .emacs.d / directory and paste this code there:

(autoload 'python-mode "python-mode" "Python Mode." t)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(add-to-list 'interpreter-mode-alist '("python" . python-mode))
(require 'python-mode)
(add-hook 'python-mode-hook
(lambda ()
(set-variable 'py-indent-offset 4)
;(set-variable 'py-smart-indentation nil)
(set-variable 'indent-tabs-mode nil)
(define-key py-mode-map (kbd "RET") 'newline-and-indent)
;(define-key py-mode-map [tab] 'yas/expand)
;(setq yas/after-exit-snippet-hook 'indent-according-to-mode)
(smart-operator-mode-on)
))
;; pymacs
(autoload 'pymacs-apply "pymacs")
(autoload 'pymacs-call "pymacs")
(autoload 'pymacs-eval "pymacs" nil t)
(autoload 'pymacs-exec "pymacs" nil t)
(autoload 'pymacs-load "pymacs" nil t)
;;(eval-after-load "pymacs"
;; '(add-to-list 'pymacs-load-path YOUR-PYMACS-DIRECTORY"))
(pymacs-load "ropemacs" "rope-")
(setq ropemacs-enable-autoimport t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Auto-completion
;;; Integrates:
;;; 1) Rope
;;; 2) Yasnippet
;;; all with AutoComplete.el
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun prefix-list-elements (list prefix)
(let (value)
(nreverse
(dolist (element list value)
(setq value (cons (format "%s%s" prefix element) value))))))
(defvar ac-source-rope
'((candidates
. (lambda ()
(prefix-list-elements (rope-completions) ac-target))))
"Source for Rope")
(defun ac-python-find ()
"Python `ac-find-function'."
(require 'thingatpt)
(let ((symbol (car-safe (bounds-of-thing-at-point 'symbol))))
(if (null symbol)
(if (string= "." (buffer-substring (- (point) 1) (point)))
(point)
nil)
symbol)))
(defun ac-python-candidate ()
"Python `ac-candidates-function'"
(let (candidates)
(dolist (source ac-sources)
(if (symbolp source)
(setq source (symbol-value source)))
(let* ((ac-limit (or (cdr-safe (assq 'limit source)) ac-limit))
(requires (cdr-safe (assq 'requires source)))
cand)
(if (or (null requires)
(>= (length ac-target) requires))
(setq cand
(delq nil
(mapcar (lambda (candidate)
(propertize candidate 'source source))
(funcall (cdr (assq 'candidates source)))))))
(if (and (> ac-limit 1)
(> (length cand) ac-limit))
(setcdr (nthcdr (1- ac-limit) cand) nil))
(setq candidates (append candidates cand))))
(delete-dups candidates)))
(add-hook 'python-mode-hook
(lambda ()
(auto-complete-mode 1)
(set (make-local-variable 'ac-sources)
(append ac-sources '(ac-source-rope)));;(append ac-sources '(ac-source-rope) '(ac-source-yasnippet)))
(set (make-local-variable 'ac-find-function) 'ac-python-find)
(set (make-local-variable 'ac-candidate-function) 'ac-python-candidate)
(set (make-local-variable 'ac-auto-start) nil)))
;;Ryan's python specific tab completion
(defun ryan-python-tab ()
; Try the following:
; 1) Do a yasnippet expansion
; 2) Do a Rope code completion
; 3) Do an indent
(interactive)
(if (eql (ac-start) 0)
(indent-for-tab-command)))
(defadvice ac-start (before advice-turn-on-auto-start activate)
(set (make-local-variable 'ac-auto-start) t))
(defadvice ac-cleanup (after advice-turn-off-auto-start activate)
(set (make-local-variable 'ac-auto-start) nil))
(define-key py-mode-map "\t" 'ryan-python-tab)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; End Auto Completion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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)
(provide 'init_python)


Add it to .emacs (load-library "init_python")

Everything! Now, using the tab button, we will receive a drop-down list with options for autocomplete.

Lastly, a nice little thing.
8. ipython instead of the standard console.

sudo easy_install ipython

Download the script ipython.scipy.org/dist/ipython.el to your favorite ~ / .emacs.d /
connect to .emacs
(require 'ipython)
(setq py-python-command-args '( "-colors" "Linux"))
(defadvice py-execute-buffer (around python-keep-focus activate)
"return focus to python code buffer"
(save-excursion ad-do-it))


Well, that's probably all. Is that maybe you want to change the color of the backlight to another, but about this you will find a bunch of articles on the Internet and on Habré. Good luck!

PS Article in English, based on which this article was written: www.enigmacurry.com/2009/01/21/autocompleteel-python-code-completion-in-emacs

Source: https://habr.com/ru/post/76370/


All Articles