unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Tips and tricks for using Emacs for Python coding
@ 2004-01-09 22:39 Sean Richards
  0 siblings, 0 replies; only message in thread
From: Sean Richards @ 2004-01-09 22:39 UTC (permalink / raw)



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.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-01-09 22:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-01-09 22:39 Tips and tricks for using Emacs for Python coding Sean Richards

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).