Skip to content

Instantly share code, notes, and snippets.

@nickpascucci
Last active December 18, 2015 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nickpascucci/5842987 to your computer and use it in GitHub Desktop.
Save nickpascucci/5842987 to your computer and use it in GitHub Desktop.
A minor mode to highlight the current scope in Lisp-like languages. Doesn't handle strings very well at the moment, but that's in the works.
;;; parscope-mode.el --- Minor mode for showing the current scope in Lisp-like languages.
;; Copyright (C) 2013 Nick Pascucci
;; Author: Nick Pascucci
;; Created: 22 Jun 2013
;; Keywords: tools
;; Version: 0.1
;; URL: https://gist.github.com/nickpascucci/5842987
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 3
;; of the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Inspired by https://www.youtube.com/watch?feature=player_embedded&v=wBl0rYXQdGg
;;; Code:
(defvar-local ps-overlay nil "Overlay highlighting the current scope.")
(define-minor-mode parscope-mode
"Minor mode that highlights the current sexp."
nil "ParScope" nil
(ps/enable-disable))
(defface parscope-overlay-face
'((default :background "#07272D"))
"Face used to highlight the current scope."
:group 'parscope)
(defun ps/enable-disable ()
(if parscope-mode
(ps/init)
(ps/detach-hooks)
(delete-overlay ps-overlay)))
(defun ps/init ()
(when (not (null ps-overlay))
(delete-overlay ps-overlay))
(setq ps-overlay (make-overlay 3 3 (current-buffer)))
(overlay-put ps-overlay 'face 'ps-overlay-face)
(ps/set-scope-overlay)
(ps/attach-hooks))
(defun ps/set-scope-overlay ()
(interactive)
(condition-case nil
(save-excursion
(backward-up-list)
(let ((start (point)))
(forward-sexp)
(move-overlay ps-overlay start (point))
(list start (point))))
(error
(move-overlay ps-overlay (point) (point)))))
(defun ps/update-scope ()
(when parscope-mode
(ps/set-scope-overlay)))
(defun ps/attach-hooks ()
(add-hook 'post-command-hook #'ps/update-scope nil t))
(defun ps/detach-hooks ()
(remove-hook 'post-command-hook #'ps/update-scope t))
@purcell
Copy link

purcell commented Jun 27, 2013

I'm interested in adding this to MELPA. How about promoting it to a full repository so it has a more stable home and can receive pull requests (e.g. for the missing trailing ;;; parscope.el ends here line and autoload cookies)?

-Steve

@purcell
Copy link

purcell commented Jun 27, 2013

P.S. To solve the string problem, you probably want to either use paredit's functions if available, or use a fix like this one: https://github.com/purcell/emacs.d/blob/master/init-editing-utils.el#L225

@nickpascucci
Copy link
Author

Sure thing, I'll migrate it to its own repo tonight. An updated version is up on Marmalade: http://marmalade-repo.org/packages/parscope, but I'd love to have it on more repos!

I'll definitely take a look at those string utils as well.

@nickpascucci
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment