File: slime-highlight-edits.el

package info (click to toggle)
slime 1:20080223.dfsg-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,600 kB
  • ctags: 3,345
  • sloc: lisp: 30,707; sh: 163; makefile: 119; awk: 10
file content (99 lines) | stat: -rw-r--r-- 3,410 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
;;; slime-higlight-edits --- highlight edited, i.e. not yet compiled, code 
;;
;; Author: William Bland <doctorbill.news@gmail.com> and others
;; License: GNU GPL (same license as Emacs)
;;
;;; Installation: 
;; 
;; Add something like this your .emacs: 
;;
;;   (add-to-list 'load-path "<contrib-dir>")
;;   (autoload 'slime-highlight-edits-mode "slime-highlight-edits")
;;   (add-hook 'slime-mode-hook (lambda () (slime-highlight-edits-mode 1)))

(defface slime-highlight-edits-face
    `((((class color) (background light))
       (:background "lightgray"))
      (((class color) (background dark))
       (:background "dimgray"))
      (t (:background "yellow")))
  "Face for displaying edit but not compiled code."
  :group 'slime-mode-faces)

(define-minor-mode slime-highlight-edits-mode 
  "Minor mode to highlight not-yet-compiled code." nil)

(add-hook 'slime-highlight-edits-mode-on-hook
          'slime-highlight-edits-init-buffer)

(add-hook 'slime-highlight-edits-mode-off-hook
          'slime-highlight-edits-reset-buffer)

(defun slime-highlight-edits-init-buffer ()
  (make-local-variable 'after-change-functions)
  (add-to-list 'after-change-functions 
               'slime-highlight-edits)
  (add-to-list 'slime-before-compile-functions
               'slime-highlight-edits-compile-hook))

(defun slime-highlight-edits-reset-buffer ()
  (setq after-change-functions  
        (remove 'slime-highlight-edits after-change-functions))
  (slime-remove-edits (point-min) (point-max)))

;; FIXME: what's the LEN arg for?
(defun slime-highlight-edits (beg end &optional len) 
  (save-match-data
    (when (and (slime-connected-p)
               (not (slime-inside-comment-p beg end))
               (not (slime-only-whitespace-p beg end)))
      (let ((overlay (make-overlay beg end)))
        (overlay-put overlay 'face 'slime-highlight-edits-face)
        (overlay-put overlay 'slime-edit t)))))

(defun slime-remove-edits (start end)
  "Delete the existing Slime edit hilights in the current buffer."
  (save-excursion
    (goto-char start)
    (while (< (point) end)
      (dolist (o (overlays-at (point)))
        (when (overlay-get o 'slime-edit)
          (delete-overlay o)))
      (goto-char (next-overlay-change (point))))))

(defun slime-highlight-edits-compile-hook (start end)
  (when slime-highlight-edits-mode
    (let ((start (save-excursion (goto-char start) 
				 (skip-chars-backward " \t\n\r")
				 (point)))
	  (end (save-excursion (goto-char end) 
			       (skip-chars-forward " \t\n\r")
			       (point))))
      (slime-remove-edits start end))))

(defun slime-inside-comment-p (beg end)
  "Is the region from BEG to END in a comment?"
  (save-excursion
    (goto-char beg)
    (let* ((hs-c-start-regexp ";\\|#|")
           (comment (hs-inside-comment-p)))
      (and comment
           (destructuring-bind (cbeg cend) comment
             (<= end cend))))))

(defun slime-only-whitespace-p (beg end)
  "Contains the region from BEG to END only whitespace?"
  (save-excursion
    (goto-char beg)
    (skip-chars-forward " \n\t\r" end)
    (<= end (point))))

(defun slime-highlight-edits-mode-on () (slime-highlight-edits-mode 1))

(defun slime-highlight-edits-init ()
  (add-hook 'slime-mode-hook 'slime-highlight-edits-mode-on))

(defun slime-highlight-edits-unload ()
  (remove-hook 'slime-mode-hook 'slime-highlight-edits-mode-on))

(provide 'slime-highlight-edits)