diff options
| author | Fabián Ezequiel Gallina | 2012-05-17 00:03:21 -0300 |
|---|---|---|
| committer | Fabián Ezequiel Gallina | 2012-05-17 00:03:21 -0300 |
| commit | ecf24fd762c38e0a588198bc702b2f5866957f86 (patch) | |
| tree | c7705b66022fbf54a65510d575b9deade15d7da4 /lisp/progmodes/python.el | |
| parent | 33d0aec16e93b6993283c0d4b46f21a1600f9e8f (diff) | |
| download | emacs-ecf24fd762c38e0a588198bc702b2f5866957f86.tar.gz emacs-ecf24fd762c38e0a588198bc702b2f5866957f86.zip | |
Backported triple quote syntax from Emacs 24
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 66 |
1 files changed, 26 insertions, 40 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 51f0cfa80eb..247deec6d3e 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -395,26 +395,15 @@ | |||
| 395 | (set-match-data nil))))) | 395 | (set-match-data nil))))) |
| 396 | (1 font-lock-variable-name-face nil nil)))) | 396 | (1 font-lock-variable-name-face nil nil)))) |
| 397 | 397 | ||
| 398 | ;; Fixme: Is there a better way? | ||
| 399 | (defconst python-font-lock-syntactic-keywords | 398 | (defconst python-font-lock-syntactic-keywords |
| 399 | ;; Make outer chars of matching triple-quote sequences into generic | ||
| 400 | ;; string delimiters. Fixme: Is there a better way? | ||
| 400 | ;; First avoid a sequence preceded by an odd number of backslashes. | 401 | ;; First avoid a sequence preceded by an odd number of backslashes. |
| 401 | `((,(rx (not (any ?\\)) | 402 | `((,(concat "\\(?:\\([RUru]\\)[Rr]?\\|^\\|[^\\]\\(?:\\\\.\\)*\\)" ;Prefix. |
| 402 | ?\\ (* (and ?\\ ?\\)) | 403 | "\\(?:\\('\\)'\\('\\)\\|\\(?2:\"\\)\"\\(?3:\"\\)\\)") |
| 403 | (group (syntax string-quote)) | 404 | (3 (python-quote-syntax))))) |
| 404 | (backref 1) | 405 | |
| 405 | (group (backref 1))) | 406 | (defun python-quote-syntax () |
| 406 | (2 ,(string-to-syntax "\""))) ; dummy | ||
| 407 | (,(rx (group (optional (any "uUrR"))) ; prefix gets syntax property | ||
| 408 | (optional (any "rR")) ; possible second prefix | ||
| 409 | (group (syntax string-quote)) ; maybe gets property | ||
| 410 | (backref 2) ; per first quote | ||
| 411 | (group (backref 2))) ; maybe gets property | ||
| 412 | (1 (python-quote-syntax 1)) | ||
| 413 | (2 (python-quote-syntax 2)) | ||
| 414 | (3 (python-quote-syntax 3)))) | ||
| 415 | "Make outer chars of triple-quote strings into generic string delimiters.") | ||
| 416 | |||
| 417 | (defun python-quote-syntax (n) | ||
| 418 | "Put `syntax-table' property correctly on triple quote. | 407 | "Put `syntax-table' property correctly on triple quote. |
| 419 | Used for syntactic keywords. N is the match number (1, 2 or 3)." | 408 | Used for syntactic keywords. N is the match number (1, 2 or 3)." |
| 420 | ;; Given a triple quote, we have to check the context to know | 409 | ;; Given a triple quote, we have to check the context to know |
| @@ -432,28 +421,25 @@ Used for syntactic keywords. N is the match number (1, 2 or 3)." | |||
| 432 | ;; x '"""' x """ \"""" x | 421 | ;; x '"""' x """ \"""" x |
| 433 | (save-excursion | 422 | (save-excursion |
| 434 | (goto-char (match-beginning 0)) | 423 | (goto-char (match-beginning 0)) |
| 435 | (cond | 424 | (let ((syntax (save-match-data (syntax-ppss)))) |
| 436 | ;; Consider property for the last char if in a fenced string. | 425 | (cond |
| 437 | ((= n 3) | 426 | ((eq t (nth 3 syntax)) ; after unclosed fence |
| 438 | (let* ((font-lock-syntactic-keywords nil) | 427 | ;; Consider property for the last char if in a fenced string. |
| 439 | (syntax (syntax-ppss))) | 428 | (goto-char (nth 8 syntax)) ; fence position |
| 440 | (when (eq t (nth 3 syntax)) ; after unclosed fence | 429 | (skip-chars-forward "uUrR") ; skip any prefix |
| 441 | (goto-char (nth 8 syntax)) ; fence position | 430 | ;; Is it a matching sequence? |
| 442 | (skip-chars-forward "uUrR") ; skip any prefix | 431 | (if (eq (char-after) (char-after (match-beginning 2))) |
| 443 | ;; Is it a matching sequence? | 432 | (put-text-property (match-beginning 3) (match-end 3) |
| 444 | (if (eq (char-after) (char-after (match-beginning 2))) | 433 | 'syntax-table (string-to-syntax "|")))) |
| 445 | (eval-when-compile (string-to-syntax "|")))))) | 434 | ((match-end 1) |
| 446 | ;; Consider property for initial char, accounting for prefixes. | 435 | ;; Consider property for initial char, accounting for prefixes. |
| 447 | ((or (and (= n 2) ; leading quote (not prefix) | 436 | (put-text-property (match-beginning 1) (match-end 1) |
| 448 | (= (match-beginning 1) (match-end 1))) ; prefix is null | 437 | 'syntax-table (string-to-syntax "|"))) |
| 449 | (and (= n 1) ; prefix | 438 | (t |
| 450 | (/= (match-beginning 1) (match-end 1)))) ; non-empty | 439 | ;; Consider property for initial char, accounting for prefixes. |
| 451 | (let ((font-lock-syntactic-keywords nil)) | 440 | (put-text-property (match-beginning 2) (match-end 2) |
| 452 | (unless (eq 'string (syntax-ppss-context (syntax-ppss))) | 441 | 'syntax-table (string-to-syntax "|")))) |
| 453 | (eval-when-compile (string-to-syntax "|"))))) | 442 | ))) |
| 454 | ;; Otherwise (we're in a non-matching string) the property is | ||
| 455 | ;; nil, which is OK. | ||
| 456 | ))) | ||
| 457 | 443 | ||
| 458 | (defvar python-mode-syntax-table | 444 | (defvar python-mode-syntax-table |
| 459 | (let ((table (make-syntax-table))) | 445 | (let ((table (make-syntax-table))) |