all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuri D'Elia <wavexx@thregr.org>
To: 21086@debbugs.gnu.org
Subject: bug#21086: 24.5; python.el: python-shell-buffer-substring fails under certain circumstances
Date: Sat, 18 Jul 2015 18:13:33 +0200	[thread overview]
Message-ID: <55AA7B2D.9060208@thregr.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 600 bytes --]

Assuming some python buffer like:

#....
if 1:
    print 1

where the selected region is "print \"1\"" (mark before "p"),
`python-shell-send-region' would fail with:

File "test.py", line 3
    print 1
        ^
IndentationError: expected an indented block

The reason lies within `python-shell-buffer-substring', which assumes
that a region always starts with the correct indentation.

The attached patch fixes the above behavior: when the region doesn't
start with indentation, but the current block does (looking at
(current-indentation)), we prefix the fillstr with the correct amount of
indent.

[-- Attachment #2: python-buffer-substring.patch --]
[-- Type: text/x-diff, Size: 1470 bytes --]

--- python.el.Orig	2015-07-18 18:00:49.367517569 +0200
+++ python.el	2015-07-18 18:03:01.203659502 +0200
@@ -2548,18 +2548,22 @@
                      (make-string
                       ;; Subtract 2 because of the coding cookie.
                       (- (line-number-at-pos start) 2) ?\n))))
-         (toplevel-block-p (save-excursion
-                             (goto-char start)
-                             (or (zerop (line-number-at-pos start))
-                                 (progn
-                                   (python-util-forward-comment 1)
-                                   (zerop (current-indentation)))))))
+         (block-param (save-excursion
+                        (goto-char start)
+                        (progn
+                          (python-util-forward-comment 1)
+                          (list (current-indentation)
+                                (/= (point) start)))))
+         (block-indentation (car block-param))
+         (starts-with-indentation-p (cadr block-param)))
     (with-temp-buffer
       (python-mode)
       (if fillstr (insert fillstr))
+      (when (and (> block-indentation 0) (not starts-with-indentation-p))
+        (insert (make-string block-indentation ?\s)))
       (insert substring)
       (goto-char (point-min))
-      (when (not toplevel-block-p)
+      (when (> block-indentation 0)
         (insert "if True:")
         (delete-region (point) (line-end-position)))
       (when nomain

             reply	other threads:[~2015-07-18 16:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-18 16:13 Yuri D'Elia [this message]
2015-08-23 23:01 ` bug#21086: 24.5; python.el: python-shell-buffer-substring fails under certain circumstances Fabián Ezequiel Gallina

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=55AA7B2D.9060208@thregr.org \
    --to=wavexx@thregr.org \
    --cc=21086@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.