help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Tips and tricks for using Emacs for Python coding


From: Sean Richards
Subject: Tips and tricks for using Emacs for Python coding
Date: Sat, 10 Jan 2004 11:39:35 +1300
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3.50 (gnu/linux)

Hi,

What cool tricks do you guys/girls use to make life easier when coding
Python with Emacs? I am interested to hear what other people find
helpful.  I know about python-mode and have scoured the web for snippets
of lisp that I thought were useful. As I use cua-mode I have made a
couple of crude functions that make it easy to left or right shift the
region. I am only just starting with elisp so they could be improved but
they seem to work OK. Some good code for outline-mode and Python came
from here http://cpbotha.net/thingies/python-outline.dot.emacs.txt
Otherwise that is about it. Here is the Python section of my .emacs,
would be good to see what you guys/girls use.
           
;; 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
          (lambda ()
            (set-variable 'py-indent-offset 4)
            (set-variable 'py-block-comment-prefix "#")
            (set-variable 'py-smart-indentation nil)
            (set-variable 'indent-tabs-mode nil)
            (local-set-key (kbd "S-<backspace>") 'my-py-leftshift-command)
            (local-set-key (kbd "<S-iso-lefttab>") 'my-py-rightshift-command)
            (local-set-key (kbd "<tab>") 'my-py-indent-command)
            (set (make-local-variable 'compile-command)
                 (concat "python "
                         (buffer-file-name)))
            (setq outline-regexp "[^ \t\n]\\|[ \t]*\\(def[ \t]+\\|class[ 
\t]+\\)")
            (setq outline-level 'my-py-outline-level)
            (outline-minor-mode t)
            (hide-body)
            (show-paren-mode 1)))

(defun my-py-rightshift-command ()
  "If region active shift region to right and keep highlighted"
  (interactive)    
  (save-excursion
    (if (eq t mark-active)
        (py-shift-region-right (region-beginning)(region-end)))
    (setq deactivate-mark nil)))

(defun my-py-leftshift-command ()
  "If region active shift region to left and keep highlighted" 
  (interactive)
  (save-excursion
    (if (eq t mark-active)
        (py-shift-region-left (region-beginning)(region-end)))
    (setq deactivate-mark nil)))

(defun my-py-indent-command ()
  "If region active indent region otherwise indent-or-complete"
  (interactive)
  (save-excursion
  (if (eq t mark-active)
      (progn
        (forward-line -1)
        (py-indent-region (region-beginning)(region-end))))
  (my-indent-or-complete)))

(defun my-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))))

;; Regex list for jumping to  Python errors with next-error
(require 'compile)
(add-to-list 'compilation-error-regexp-alist
             '(".*File \"\\(.+\\)\", line \\([0-9]+\\)" 1 2))

;;----------------------------------------------------------------------------

Sean

-- 
"Hver sin smak", sa vintapperen, han drakk mens de andre sloss.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]