Skip to content

Instantly share code, notes, and snippets.

@edmccard
Created February 28, 2012 19:14
Show Gist options
  • Save edmccard/1934459 to your computer and use it in GitHub Desktop.
Save edmccard/1934459 to your computer and use it in GitHub Desktop.
Python mark compound statement
(when (fboundp 'py-mark-block-or-clause)
;; Duplicating this for python.el would require a bit of work.
(defun er/mark-x-python-compound-statement ()
"Mark the current compound statement (if, while, for, try) and all clauses."
(interactive)
(let ((secondary-re
(save-excursion
(py-mark-block-or-clause)
(cond ((looking-at "if\\|for\\|while\\|else\\|elif") "else\\|elif")
((looking-at "try\\|except\\|finally") "except\\|finally"))))
start-col)
(when secondary-re
(py-mark-block-or-clause)
(setq start-col (current-column))
(while (looking-at secondary-re)
(previous-line) (back-to-indentation)
(while (> (current-column) start-col)
(previous-line) (back-to-indentation)))
(set-mark (point))
(py-goto-beyond-clause) (next-line) (back-to-indentation)
(while (and (looking-at secondary-re)
(>= (current-column) start-col))
(py-goto-beyond-clause) (next-line) (back-to-indentation))
(previous-line) (end-of-line)
(exchange-point-and-mark)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment