forked from magnars/expand-region.el
/
python-el-fgallina-expansions.el
78 lines (61 loc) · 2.64 KB
/
python-el-fgallina-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
;;; python-el-fgallina-expansions.el --- fgallina/python.el-specific expansions for expand-region
;; Copyright (C) 2012 Felix Geller
;; Author: Felix Geller
;; 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:
;;
;; - Additions implemented here:
;; - `er/mark-inside-python-string'
;; - `er/mark-outside-python-string'
;; - `er/mark-python-sentence'
;; - Supports multi-line strings
;;; Code:
(require 'expand-region-core)
(defvar er--python-string-delimiter "'\"")
(defun er/mark-inside-python-string ()
(interactive)
(let ((beginning-of-string (python-info-ppss-context 'string (syntax-ppss))))
(when beginning-of-string
(goto-char beginning-of-string)
(forward-sexp)
(skip-chars-backward er--python-string-delimiter)
(set-mark (point))
(goto-char beginning-of-string)
(skip-chars-forward er--python-string-delimiter))))
(defun er/mark-outside-python-string ()
(interactive)
(let ((beginning-of-string (python-info-ppss-context 'string (syntax-ppss))))
(when beginning-of-string
(goto-char beginning-of-string)
(set-mark (point))
(forward-sexp)
(exchange-point-and-mark))))
(defun er/mark-python-sentence ()
(interactive)
(python-nav-sentence-end)
(set-mark (point))
(python-nav-sentence-start))
(defun er/add-python-mode-expansions ()
"Adds python-mode-specific expansions for buffers in python-mode"
(let ((try-expand-list-additions '(
er/mark-inside-python-string
er/mark-outside-python-string
er/mark-python-sentence
)))
(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))))))
(add-hook 'python-mode-hook 'er/add-python-mode-expansions)
(provide 'python-el-fgallina-expansions)
;; python-el-fgallina-expansions.el ends here