Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

electric-pair-mode and Python triple quotes

Tags:

python

emacs

Is there a way to enable auto-pairing Python triple quotes in electric-pair-mode?

This can be configured in autopair-mode using autopair-python-triple-quote-action. Is there a similar way to enable this in electric-pair-mode?

like image 638
calvinyoung Avatar asked Oct 30 '13 07:10

calvinyoung


People also ask

What does 3 quotation marks mean in Python?

Note: Triple quotes, according to official Python documentation are docstrings, or multi-line docstrings and are not considered comments. Anything inside triple quotes is read by the interpreter. When the interpreter encounters the hash symbol, it ignores everything after that. That is what a comment is defined to be.

How do you do triple quotes in Python?

The syntax for triple quotes consists of three consecutive single or double quotes.

How do you escape triple quotes in Python?

Escaping the quote characters with a backslash always works.


1 Answers

You can do the following:

(defun python-electric-pair-string-delimiter ()
  (when (and electric-pair-mode
             (memq last-command-event '(?\" ?\'))
             (let ((count 0))
               (while (eq (char-before (- (point) count)) last-command-event)
                 (setq count (1+ count)))
               (= count 3)))
    (save-excursion (insert (make-string 3 last-command-event)))))

(add-hook 'python-mode-hook
          (lambda ()
            (add-hook 'post-self-insert-hook
                      #'python-electric-pair-string-delimiter 'append t)))

It will be included in the next release of Emacs.

like image 150
Stefan Avatar answered Sep 26 '22 01:09

Stefan