;;; Thanks to Tim Peters and Barry A. Warsaw for the python-mode package. ;;; This file, dlr-python-mode.el is the work of David LaRose, dlr@cs.cmu.edu ;; We define one variable. This controls whether or not we ;; do automatic code block delimiting with comments. (defvar *dlr-py-auto-punctuate* t "Non-nil means auto-insert code block delimiting comments when in 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) ;; (load "python-mode") ;; I like long lines, so I prefer not to indent too deeply. (custom-set-variables '(py-indent-offset 2)) ;; Avoid tab/space ambiguity by only using spaces (custom-set-variables '(indent-tabs-mode nil)) ;; Not exactly sure what this does... :-/ (custom-set-faces) ;; Stuff I want to happen when python-mode is loaded goes here (defun my-python-mode-hook () ;; Turn on font colorizing (font-lock-mode) ;; I'm going to change the key binding for SPC. Better ;; remember it now so I can call it from within ;; dlr-py-electric-spc (let ((oldfunc (key-binding " "))) (if (not (eq oldfunc 'dlr-py-electric-spc)) (setq *dlr-py-old-spc-func* oldfunc))) ;; keybindings for python mode: ;; don't bash my auto-completion key (define-key py-mode-map "\C-j" nil) ;; add my custom space function (define-key py-mode-map " " 'dlr-py-electric-spc) ) (add-hook 'python-mode-hook 'my-python-mode-hook) ;; Here's my redefinition of the space key binding. (defun dlr-py-electric-spc (arg) "Insert a space and check if the last thing typed was a keyword. Call appropriate functions to complete the syntax for any keywords identified. The space is inserted by the function bound to *dlr-py-old-spc-func*, and keywords are completed by functions like dlr-py-insert-for. Completion is only done if the variable *dlr-py-auto-punctuate* is non-nil." (interactive "p") ;; Put in the a space using whatever was originally bound to the ;; space key (funcall *dlr-py-old-spc-func* arg) ;; We'll need some local variables (let* ((here (point)) (pos (- (point-max) here)) (search-bound (max (- here 40) 0)) mbeg mend) (if *dlr-py-auto-punctuate* (cond ((and (re-search-backward "\n[ \t]*class " search-bound t) (progn (setq mbeg (match-beginning 0) mend (match-end 0)) (goto-char here) (= mend here))) (dlr-py-insert-keyword "class")) ((and (re-search-backward "\n[ \t]*def " search-bound t) (progn (setq mbeg (match-beginning 0) mend (match-end 0)) (goto-char here) (= mend here))) (dlr-py-insert-keyword "def")) ((and (re-search-backward "\n[ \t]*if " search-bound t) (progn (setq mbeg (match-beginning 0) mend (match-end 0)) (goto-char here) (= mend here))) (dlr-py-insert-keyword "if")) ((and (re-search-backward "\n[ \t]*for " search-bound t) (progn (setq mbeg (match-beginning 0) mend (match-end 0)) (goto-char here) (= mend here))) (dlr-py-insert-keyword "for")) ((and (re-search-backward "\n[ \t]*while " search-bound t) (progn (setq mbeg (match-beginning 0) mend (match-end 0)) (goto-char here) (= mend here))) (dlr-py-insert-keyword "while")) ((and (re-search-backward "\n[ \t]*try " search-bound t) (progn (setq mbeg (match-beginning 0) mend (match-end 0)) (goto-char here) (= mend here))) (dlr-py-insert-keyword "try")) (t (goto-char (- (point-max) pos)))))) ) (defun dlr-py-insert-keyword (keyword) "Insert a _for_ clause with appropriate punctuation." ; (dlr-py-indent) ; (insert keyword " ") ; (dlr-py-indent) (setq pos (point)) (insert "\n# end " keyword) (dlr-py-indent) (goto-char pos) ) (defun dlr-py-indent () "Indent the current line by calling whatever function is bound to the TAB key" (funcall (key-binding "\t")) ) (defun dlr-py-dedent () "Dedent the current line by calling whatever function is bound to the BS key." ; I wanted this to be independent of python mode, but I've had ; to put in the argument 1 to placate py-electric-backspace ; possibly this will be a problem when we get a backspace ; function which doesn't want an arg. Does such a function ; exist? (funcall (key-binding "\177") 1) )