forked from magnars/expand-region.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python-el-expansions.el
92 lines (72 loc) · 3.16 KB
/
python-el-expansions.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
;;; python-el-expansions.el --- Python-specific expansions for expand-region
;; Copyright (C) 2012 Ivan Andrus
;; Authors: Ivan Andrus, Felix Geller, @edmccard
;; Based on js-mode-expansions by: Magnar Sveen <[email protected]>
;; Keywords: marking region python
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; For python.el included with GNU Emacs
;; - Mark functionality taken from python.el:
;; - `python-mark-block'
;; - Additions implemented here:
;; - `er/mark-python-statement'
;; - `er/mark-inside-python-string'
;; - `er/mark-outside-python-string'
;; - Supports multi-line strings
;; There is no need for a er/mark-python-defun since
;; er/mark-python-block will mark it
;; Feel free to contribute any other expansions for Python at
;;
;; https://github.com/magnars/expand-region.el
;;; Code:
(require 'expand-region-core)
(require 'python)
(declare-function python-beginning-of-string "python-mode")
(defvar er--python-string-delimiter "'\"")
(defun er/mark-python-statement ()
"Marks one Python statement, eg. x = 3"
(interactive)
(python-nav-end-of-statement)
(set-mark (point))
(python-nav-beginning-of-statement))
(defun er/mark-outside-python-string ()
"Marks region outside a (possibly multi-line) Python string"
(interactive)
(python-beginning-of-string)
(set-mark (point))
(forward-sexp)
(exchange-point-and-mark))
(defun er/mark-inside-python-string ()
"Marks region inside a (possibly multi-line) Python string"
(interactive)
(when (eq 'string (syntax-ppss-context (syntax-ppss)))
(python-beginning-of-string)
(let ((string-beginning (point)))
(forward-sexp)
(skip-chars-backward er--python-string-delimiter)
(set-mark (point))
(goto-char string-beginning)
(skip-chars-forward er--python-string-delimiter))))
(defun er/add-python-mode-expansions ()
"Adds Python-specific expansions for buffers in python-mode"
(let ((try-expand-list-additions '(er/mark-python-statement
er/mark-inside-python-string
er/mark-outside-python-string
python-mark-block)))
(set (make-local-variable 'expand-region-skip-whitespace) nil)
(set (make-local-variable 'er/try-expand-list)
(remove 'er/mark-inside-quotes
(remove 'er/mark-outside-quotes
(append er/try-expand-list try-expand-list-additions))))))
(er/enable-mode-expansions 'python-mode 'er/add-python-mode-expansions)
(provide 'python-el-expansions)
;; python-el-expansions.el ends here