Skip to content

Latest commit

 

History

History
228 lines (196 loc) · 7.72 KB

starter-kit-python.org

File metadata and controls

228 lines (196 loc) · 7.72 KB

Starter Kit Python

This is part of the Emacs Starter Kit.

Starter kit Python

Support for the Python programming language.

Configuration for python mode

  • Python uses indentation instead of braces to structure programs so that it’s crucial to determine at which level of indentation the cursor is. Use highlight-indentation-current-column-mode to fulfil this purpose. Another minor mode highlight-indentation-mode is too heavy taste and not use it.
(setq python-shell-completion-native-enable nil)
(defun my-python-conf ()
  (highlight-indentation-current-column-mode)
  (sphinx-doc-mode 1)
  (when jedi-is-installed
    (anaconda-eldoc-mode 1)
    (define-key python-mode-map (kbd "C-h f") #'anaconda-mode-show-doc))
  (modify-syntax-entry ?_ "w"))

(with-eval-after-load 'python
  (add-hook 'python-mode-hook 'my-python-conf t))

Use Cython mode

(add-to-list 'auto-mode-alist '("\\.pyx\\'" . cython-mode))
(add-to-list 'auto-mode-alist '("\\.pxd\\'" . cython-mode))
(add-to-list 'auto-mode-alist '("\\.pxi\\'" . cython-mode))

Set up for SConstruct

SCons is a software construction tool written in python. The input card or script for SCons is SConstruct, which can be treated as Python script.

(setq auto-mode-alist
      (append '(("SConstruct\\'" . python-mode)
		("SConscript\\'" . python-mode))
              auto-mode-alist))

IPython set up

Now native python shell support completion. I don’t use IPython in Emacs any longer.

Activate IPython in ansi-term.

(defun run-ipython ()
  "Activate IPython shell"
  (interactive)
  ;; Get buffer index
  (setq my-ipython-shell-index 0)
  (while (get-buffer (format "*IPython <%d>*" my-ipython-shell-index))
    (setq my-ipython-shell-index (+ 1 my-ipython-shell-index)))
  ;; Create new ansi term
  (ansi-term "ipython" (format "IPython <%d>" my-ipython-shell-index))
  (switch-to-buffer (format "*IPython <%d>*" my-ipython-shell-index)))

Or you can use M-x C-u run-python command to run Python interpreter. Here C-u prefix enables you input the Python interpreter such IPython. IPython 5 has a new terminal interface, which is not compatible with Emacs’ inferior shells. To fix it, add the –simple-prompt flag to the IPython command. See StackExchange.

(defun my-ipython-conf ()
  (setq ipython-version
        (shell-command-to-string "ipython --version"))
  (setq
   python-shell-interpreter "ipython"
   python-shell-interpreter-args
   (if (string-match-p "^5\\." ipython-version)
       "--simple-prompt -i"
     "")
   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"))
(with-eval-after-load 'python
  (add-hook 'python-mode-hook 'my-ipython-conf t))

To yank in inferior-python-mode, use helm-show-kill-ring.

Run Windows Python interpreter in Cygwin platform

Don’t do this. You will fall into a pit. If you have to, there are several ways:

You are under directory where script to run exists
/path/to/windows/python script.py [args]
    
You are not there
cat /path/to/script.py | /path/to/windows/python - [args]
    

- means python interpreter will read from stdin. Note that you can’t do this in eshell for that eshell can’t handle stdin redirection.

You are editing the script
Make a symbolic to Windows Python:
ln -s /path/to/windows/python ~/bin/winpy
    

Then define quickrun like this. Finally run quickrun.

You are in dired
Run M-x dired-open-w32-prog-at-point or C-u M-x dired-open-w32-prog-at-point as defined here.

There is no perfect way. The first and second are tricky to handle the path. The quickrun will be confused by #!/usr/bin/env python and have no idea whether to run winpy or python. The path problems also exist for quickrun. The dired way is perfect only if you don’t use matplotlib. Matplotlib can not create configuration directory for some reason.

Rst in Python doc

(defun indirect-region-py-rst ()
  "Edit doc string in indirect buffer in rst-mode."
  (interactive)
  (let (start end)
    (save-excursion
      (setq start (search-backward-regexp "[\"']\\{3\\}" nil t)))
    (when start
      (setq start (+ start 3)))
    (save-excursion
      (setq end (search-forward-regexp "[\"']\\{3\\}" nil t)))
    (when end
      (setq end (- end 3)))
    (when (and start end)
      (indirect-region start end 'rst-mode))))
(eval-after-load "python"
  `(define-key python-mode-map (kbd "C-c '") 'indirect-region-py-rst))

Python shell send do what I mean

python-shell-send-region is sometimes strange, don’t use it.

(defun python-shell-send-dwim (arg)
  "Send region or function or class or buffer or statement to python shell.

- If a piece of region is selected, send the active retion.

- With C-u prefix, send function or class definition.

- With C-u C-u prefix, send the whole buffer.

- If at beginning of class or function definition, send the definition
  structure.

- Finally, try to send the statement at point."
  (interactive "P")
  (let (b0 b1)
    (cond ((region-active-p)
           (python-shell-send-region (region-beginning) (region-end)))
          ((equal arg '(4))
           (python-shell-send-defun (y-or-n-p "Don't send decorators?")))
          ((equal arg '(16))
           (python-shell-send-buffer))
          ((save-excursion
             (beginning-of-line)
             (looking-at "[ \t]*\\(class \\|def \\)"))
           (python-shell-send-defun (y-or-n-p "Don't send decorators?")))
          (t
           (save-excursion
             (setq b0 (progn
                        (python-nav-beginning-of-statement)
                        (point)))
             (setq b1 (progn
                        (python-nav-end-of-statement)
                        (point))))
           (python-shell-send-string
            (buffer-substring-no-properties b0 b1))))))
(eval-after-load "python"
  `(define-key python-mode-map (kbd "C-c C-c") 'python-shell-send-dwim))

Look up python documentation

(defun look-up-pydoc (module)
  (interactive
   (list
    (let* ((b (bounds-of-thing-at-point 'symbol))
           (b0 (car b))
           (b1 (cdr b))
           (default-mod
             (when b
               (buffer-substring-no-properties b0 b1))))
      (read-string
       (format "module (default \"%s\"): " default-mod)
       nil nil default-mod))))
  (let ((buf (get-buffer-create (format "*pydoc:%s*" module))))
    (with-current-buffer buf
      (erase-buffer)
      (call-process "python" nil t nil "-m" "pydoc" module)
      (setq imenu-generic-expression
            '(("Class" "^    class *\\(.+\\)$" 1)
              ("Function" "^    \\([a-zA-Z0-9_]+\\)(.*)$" 1)))
      (setq buffer-read-only t))
    (pop-to-buffer buf)
    (goto-char (point-min))))