mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-17 c4001ccd1864293b64aa37d83a9d9457eb875e70
commit | author | age
5cb5f7 1 ;;; smartparens-python.el --- Additional configuration for Python based modes.  -*- lexical-binding: t; -*-
C 2
3 ;; Copyright (C) 2015-2016 Matus Goljer
4
5 ;; Author: Matus Goljer <matus.goljer@gmail.com>
6 ;; Maintainer: Matus Goljer <matus.goljer@gmail.com>
7 ;; Created: 8 February 2015
8 ;; Keywords: abbrev convenience editing
9 ;; URL: https://github.com/Fuco1/smartparens
10
11 ;; This file is not part of GNU Emacs.
12
13 ;;; License:
14
15 ;; This file is part of Smartparens.
16
17 ;; Smartparens is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
21
22 ;; Smartparens is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 ;; GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with Smartparens.  If not, see <http://www.gnu.org/licenses/>.
29
30 ;;; Commentary:
31
32 ;; This file provides some additional configuration for Python based
33 ;; modes.  To use it, simply add:
34 ;;
35 ;; (require 'smartparens-python)
36 ;;
37 ;; into your configuration.  You can use this in conjunction with the
38 ;; default config or your own configuration.
39 ;;
40 ;; If you have good ideas about what should be added please file an
41 ;; issue on the github tracker.
42 ;;
43 ;; For more info, see github readme at
44 ;; https://github.com/Fuco1/smartparens
45
46 ;;; Code:
47
48 (require 'smartparens)
49
50 ;; Python has no sexp suffices.  This fixes slurping
51 ;; (|sys).path.append---the dot should not travel with the closing
52 ;; paren
53 (--each '(python-mode inferior-python-mode)
54   (add-to-list 'sp-sexp-suffix (list it 'regexp "")))
55
56 (sp-with-modes 'python-mode
57   (sp-local-pair "'" "'" :unless '(sp-in-comment-p sp-in-string-quotes-p) :post-handlers '(:add sp-python-fix-tripple-quotes))
58   (sp-local-pair "\"" "\"" :post-handlers '(:add sp-python-fix-tripple-quotes))
59   (sp-local-pair "'''" "'''")
60   (sp-local-pair "\\'" "\\'")
61   (sp-local-pair "\"\"\"" "\"\"\""))
62
63 (defun sp-python-fix-tripple-quotes (id action _context)
64   "Properly rewrap tripple quote pairs.
65
66 When the user rewraps a tripple quote pair to the other pair
67 type (i.e. ''' to \") we check if the old pair was a
68 tripple-quote pair and if so add two pairs to beg/end of the
69 newly formed pair (which was a single-quote \"...\" pair)."
70   (when (eq action 'rewrap-sexp)
71     (let ((old (plist-get sp-handler-context :parent)))
72       (when (or (and (equal old "'''") (equal id "\""))
73                 (and (equal old "\"\"\"") (equal id "'")))
74         (save-excursion
75           (sp-get sp-last-wrapped-region
76             (goto-char :end-in)
77             (insert (make-string 2 (aref id 0)))
78             (goto-char :beg)
79             (insert (make-string 2 (aref id 0)))))))))
80
81 (defadvice python-indent-dedent-line-backspace
82     (around sp-backward-delete-char-advice activate)
83   "Fix indend."
84   (if smartparens-strict-mode
85       (cl-letf (((symbol-function 'delete-backward-char)
86                  (lambda (arg &optional killp)
87                    (sp-backward-delete-char arg))))
88         ad-do-it)
89     ad-do-it))
90
91 (provide 'smartparens-python)
92 ;;; smartparens-python.el ends here