Advertisement
Guest User

Untitled

a guest
Mar 29th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 5.62 KB | None | 0 0
  1. ;;; highlight-parentheses.el --- highlight surrounding parentheses
  2. ;;
  3. ;; Copyright (C) 2007 Nikolaj Schumacher
  4. ;;
  5. ;; Author: Nikolaj Schumacher <bugs * nschum de>
  6. ;; Version: 1.0
  7. ;; Keywords: faces, matching
  8. ;; URL: http://nschum.de/src/emacs/highlight-parentheses/
  9. ;; Compatibility: GNU Emacs 22.x
  10. ;;
  11. ;; This file is NOT part of GNU Emacs.
  12. ;;
  13. ;; This program is free software; you can redistribute it and/or
  14. ;; modify it under the terms of the GNU General Public License
  15. ;; as published by the Free Software Foundation; either version 2
  16. ;; of the License, or (at your option) any later version.
  17. ;;
  18. ;; This program is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ;; GNU General Public License for more details.
  22. ;;
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with this program; if not, write to the Free Software
  25. ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  26. ;;
  27. ;;; Commentary:
  28. ;;
  29. ;; Add the following to your .emacs file:
  30. ;; (require 'highlight-parentheses)
  31. ;;
  32. ;; Enable `highlight-symbol-mode'.
  33. ;;
  34. ;;; Changes Log:
  35. ;;
  36. ;; 2007-07-30 (1.0)
  37. ;;    Added background highlighting and faces.
  38. ;;
  39. ;; 2007-05-15 (0.9.1)
  40. ;;    Support for defcustom.  Changed from vector to list.
  41. ;;
  42. ;; 2007-04-26 (0.9)
  43. ;;    Initial Release.
  44. ;;
  45. ;;; Code:
  46.  
  47. (eval-when-compile (require 'cl))
  48.  
  49. (defgroup highlight-parentheses nil
  50.   "Highlight surrounding parentheses"
  51.   :group 'faces
  52.   :group 'matching)
  53.  
  54. (defvar hl-paren-overlays nil
  55.   "This buffers currently active overlays.")
  56. (make-variable-buffer-local 'hl-paren-overlays)
  57.  
  58. (defcustom hl-paren-colors
  59.   '("firebrick1" "IndianRed4" "IndianRed")
  60.   "*List of colors for the highlighted parentheses.
  61. The list starts with the the inside parentheses and moves outwards."
  62.   :type '(repeat color)
  63.   :group 'highlight-parentheses)
  64.  
  65. (defcustom hl-paren-background-colors nil
  66.   "*List of colors for the background highlighted parentheses.
  67. The list starts with the the inside parentheses and moves outwards."
  68.   :type '(repeat color)
  69.   :group 'highlight-parentheses)
  70.  
  71. (defface hl-paren-face nil
  72.   "*Face used for highlighting parentheses.
  73. Color attributes might be overriden by `hl-paren-colors' and
  74. `hl-paren-background-colors'."
  75.   :group 'highlight-parentheses)
  76.  
  77. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  78.  
  79. (defvar hl-paren-last-point 0
  80.   "The last point for which parentheses were highlighted.
  81. This is used to prevent analyzing the same context over and over.")
  82. (make-variable-buffer-local 'hl-paren-last-point)
  83.  
  84. (defun hl-paren-highlight ()
  85.   "Highlight the parentheses around point."
  86.   (unless (= (point) hl-paren-last-point)
  87.    (save-excursion
  88.       (let ((pos (point))
  89.             (match-pos (point))
  90.             (level -1)
  91.             (max (1- (length hl-paren-overlays))))
  92.         (while (and match-pos (< level max))
  93.           (setq match-pos
  94.                 (when (setq pos (cadr (syntax-ppss pos)))
  95.                   (ignore-errors (scan-sexps pos 1))))
  96.           (when match-pos
  97.             (hl-paren-put-overlay (incf level) pos 'hl-paren-face)
  98.             (hl-paren-put-overlay (incf level) (1- match-pos) 'hl-paren-face)))
  99.         (while (< level max)
  100.           (hl-paren-put-overlay (incf level) nil nil))))
  101.     (setq hl-paren-last-point (point))))
  102.  
  103. (defun hl-paren-put-overlay (n pos face)
  104.   "Move or create the N'th overlay so its shown at POS."
  105.   (let ((ov (elt hl-paren-overlays n)) end)
  106.     (if (null pos)
  107.         (when ov
  108.           (delete-overlay ov)
  109.           (aset hl-paren-overlays n nil))
  110.       (if (atom pos)
  111.           (setq end (1+ pos))
  112.         (setq end (cdr pos))
  113.         (setq pos (car pos)))
  114.       (if ov
  115.           (move-overlay ov pos end)
  116.         (let ((face-attributes (face-attr-construct face))
  117.               (color-value (nth (/ n 2) hl-paren-colors))
  118.               (background-value (nth (/ n 2) hl-paren-background-colors)))
  119.           (when color-value
  120.             (let ((attribute (memq :foreground face-attributes)))
  121.               (if attribute
  122.                   (setcar (cdr attribute) color-value)
  123.                 (push color-value face-attributes)
  124.                 (push :foreground face-attributes))))
  125.           (when background-value
  126.             (let ((attribute (memq :background face-attributes)))
  127.               (if attribute
  128.                   (setcar (cdr attribute) background-value)
  129.                 (push background-value face-attributes)
  130.                 (push :background face-attributes))))
  131.           (setq ov (make-overlay pos end))
  132.           (aset hl-paren-overlays n ov)
  133.           (overlay-put ov 'face face-attributes))))))
  134.  
  135. ;;;###autoload
  136. (define-minor-mode highlight-parentheses-mode
  137.   "Minor mode to highlight the surrounding parentheses."
  138.   nil "" nil
  139.   (if highlight-parentheses-mode
  140.       (progn
  141.         (setq hl-paren-overlays
  142.               (make-vector (* 2 (max (length hl-paren-colors)
  143.                                      (length hl-paren-background-colors))) nil))
  144.         (add-hook 'post-command-hook 'hl-paren-highlight nil t))
  145.     (let (ov)
  146.       (dotimes (i (length hl-paren-overlays))
  147.         (when (setq ov (elt hl-paren-overlays i))
  148.           (delete-overlay ov))))
  149.     (kill-local-variable 'hl-paren-overlays)
  150.     (kill-local-variable 'hl-paren-point)
  151.     (remove-hook 'post-command-hook 'hl-paren-highlight t)))
  152.  
  153. ;;highlight-parentheses is a buffer-local minor mode : create a global
  154. ;;minor mode of our own
  155. (define-globalized-minor-mode global-highlight-parentheses-mode
  156.   highlight-parentheses-mode
  157.   (lambda ()
  158.     (highlight-parentheses-mode t)))
  159.  
  160. (provide 'highlight-parentheses)
  161.  
  162. ;;; highlight-parentheses.el ends here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement