1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2025-01-07 15:21:46 +00:00

* lisp/progmodes/python.el (python-mode-map): Remove binding for ":".

(python-indent-electric-colon): Remove command.
(python-indent-post-self-insert-function): Integrate the previous code
of python-indent-electric-colon.  Make it conditional on
electric-indent-mode.
(python-mode): Add ?: to electric-indent-chars.
Move python-indent-post-self-insert-function to the end of
post-self-insert-hook.
This commit is contained in:
Stefan Monnier 2013-11-28 21:03:39 -05:00
parent 7cdf484be3
commit bd15d9d1ad
2 changed files with 48 additions and 42 deletions

View File

@ -1,3 +1,14 @@
2013-11-29 Stefan Monnier <monnier@iro.umontreal.ca>
* progmodes/python.el (python-mode-map): Remove binding for ":".
(python-indent-electric-colon): Remove command.
(python-indent-post-self-insert-function): Integrate the previous code
of python-indent-electric-colon. Make it conditional on
electric-indent-mode.
(python-mode): Add ?: to electric-indent-chars.
Move python-indent-post-self-insert-function to the end of
post-self-insert-hook.
2013-11-28 Stefan Monnier <monnier@iro.umontreal.ca> 2013-11-28 Stefan Monnier <monnier@iro.umontreal.ca>
* doc-view.el (doc-view-goto-page): Update mode-line. * doc-view.el (doc-view-goto-page): Update mode-line.

View File

@ -40,9 +40,9 @@
;; Indentation: Automatic indentation with indentation cycling is ;; Indentation: Automatic indentation with indentation cycling is
;; provided, it allows you to navigate different available levels of ;; provided, it allows you to navigate different available levels of
;; indentation by hitting <tab> several times. Also when inserting a ;; indentation by hitting <tab> several times. Also electric-indent-mode
;; colon the `python-indent-electric-colon' command is invoked and ;; is supported such that when inserting a colon the current line is
;; causes the current line to be dedented automatically if needed. ;; dedented automatically if needed.
;; Movement: `beginning-of-defun' and `end-of-defun' functions are ;; Movement: `beginning-of-defun' and `end-of-defun' functions are
;; properly implemented. There are also specialized ;; properly implemented. There are also specialized
@ -248,7 +248,6 @@
(define-key map (kbd "<backtab>") 'python-indent-dedent-line) (define-key map (kbd "<backtab>") 'python-indent-dedent-line)
(define-key map "\C-c<" 'python-indent-shift-left) (define-key map "\C-c<" 'python-indent-shift-left)
(define-key map "\C-c>" 'python-indent-shift-right) (define-key map "\C-c>" 'python-indent-shift-right)
(define-key map ":" 'python-indent-electric-colon)
;; Skeletons ;; Skeletons
(define-key map "\C-c\C-tc" 'python-skeleton-class) (define-key map "\C-c\C-tc" 'python-skeleton-class)
(define-key map "\C-c\C-td" 'python-skeleton-def) (define-key map "\C-c\C-td" 'python-skeleton-def)
@ -1058,48 +1057,43 @@ the lines in which START and END lie."
(list (region-beginning) (region-end) current-prefix-arg) (list (region-beginning) (region-end) current-prefix-arg)
(list (line-beginning-position) (line-end-position) current-prefix-arg))) (list (line-beginning-position) (line-end-position) current-prefix-arg)))
(let ((deactivate-mark nil)) (let ((deactivate-mark nil))
(if count (setq count (if count (prefix-numeric-value count)
(setq count (prefix-numeric-value count)) python-indent-offset))
(setq count python-indent-offset))
(indent-rigidly start end count))) (indent-rigidly start end count)))
(defun python-indent-electric-colon (arg)
"Insert a colon and maybe de-indent the current line.
With numeric ARG, just insert that many colons. With
\\[universal-argument], just insert a single colon."
(interactive "*P")
(self-insert-command (if (not (integerp arg)) 1 arg))
(when (and (not arg)
(eolp)
(not (equal ?: (char-after (- (point-marker) 2))))
(not (python-syntax-comment-or-string-p)))
(let ((indentation (current-indentation))
(calculated-indentation (python-indent-calculate-indentation)))
(python-info-closing-block-message)
(when (> indentation calculated-indentation)
(save-excursion
(indent-line-to calculated-indentation)
(when (not (python-info-closing-block-message))
(indent-line-to indentation)))))))
(put 'python-indent-electric-colon 'delete-selection t)
(defun python-indent-post-self-insert-function () (defun python-indent-post-self-insert-function ()
"Adjust closing paren line indentation after a char is added. "Adjust indentation after insertion of some characters.
This function is intended to be added to the This function is intended to be added to the
`post-self-insert-hook.' If a line renders a paren alone, after `post-self-insert-hook.' If a line renders a paren alone, after
adding a char before it, the line will be re-indented adding a char before it, the line will be re-indented
automatically if needed." automatically if needed."
(when (and (eq (char-before) last-command-event) (when (and electric-indent-mode
(not (bolp)) (eq (char-before) last-command-event))
(memq (char-after) '(?\) ?\] ?\}))) (cond
(save-excursion ((and (not (bolp))
(goto-char (line-beginning-position)) (memq (char-after) '(?\) ?\] ?\})))
;; If after going to the beginning of line the point (save-excursion
;; is still inside a paren it's ok to do the trick (goto-char (line-beginning-position))
(when (python-syntax-context 'paren) ;; If after going to the beginning of line the point
(let ((indentation (python-indent-calculate-indentation))) ;; is still inside a paren it's ok to do the trick
(when (< (current-indentation) indentation) (when (python-syntax-context 'paren)
(indent-line-to indentation))))))) (let ((indentation (python-indent-calculate-indentation)))
(when (< (current-indentation) indentation)
(indent-line-to indentation))))))
((and (eq ?: last-command-event)
(memq ?: electric-indent-chars)
(not current-prefix-arg)
(eolp)
(not (equal ?: (char-before (1- (point)))))
(not (python-syntax-comment-or-string-p)))
(let ((indentation (current-indentation))
(calculated-indentation (python-indent-calculate-indentation)))
(python-info-closing-block-message)
(when (> indentation calculated-indentation)
(save-excursion
(indent-line-to calculated-indentation)
(when (not (python-info-closing-block-message))
(indent-line-to indentation)))))))))
;;; Navigation ;;; Navigation
@ -3619,6 +3613,7 @@ list is returned as is."
(set (make-local-variable 'indent-region-function) #'python-indent-region) (set (make-local-variable 'indent-region-function) #'python-indent-region)
;; Because indentation is not redundant, we cannot safely reindent code. ;; Because indentation is not redundant, we cannot safely reindent code.
(setq-local electric-indent-inhibit t) (setq-local electric-indent-inhibit t)
(setq-local electric-indent-chars (cons ?: electric-indent-chars))
;; Add """ ... """ pairing to electric-pair-mode. ;; Add """ ... """ pairing to electric-pair-mode.
(add-hook 'post-self-insert-hook (add-hook 'post-self-insert-hook
@ -3626,7 +3621,7 @@ list is returned as is."
(set (make-local-variable 'paragraph-start) "\\s-*$") (set (make-local-variable 'paragraph-start) "\\s-*$")
(set (make-local-variable 'fill-paragraph-function) (set (make-local-variable 'fill-paragraph-function)
'python-fill-paragraph) #'python-fill-paragraph)
(set (make-local-variable 'beginning-of-defun-function) (set (make-local-variable 'beginning-of-defun-function)
#'python-nav-beginning-of-defun) #'python-nav-beginning-of-defun)
@ -3634,10 +3629,10 @@ list is returned as is."
#'python-nav-end-of-defun) #'python-nav-end-of-defun)
(add-hook 'completion-at-point-functions (add-hook 'completion-at-point-functions
'python-completion-complete-at-point nil 'local) #'python-completion-complete-at-point nil 'local)
(add-hook 'post-self-insert-hook (add-hook 'post-self-insert-hook
'python-indent-post-self-insert-function nil 'local) #'python-indent-post-self-insert-function 'append 'local)
(set (make-local-variable 'imenu-create-index-function) (set (make-local-variable 'imenu-create-index-function)
#'python-imenu-create-index) #'python-imenu-create-index)