blob: e462febd1a19fd4def97e0c38d398bf64e8eef0f [file] [log] [blame]
external-reality8f34d232014-02-22 21:35:47 -05001;;; shm-evaporate.el --- Evaporating overlays
2
3;; Copyright (c) 2014 Chris Done. All rights reserved.
4
5;; This file is free software; you can redistribute it and/or modify
6;; it under the terms of the GNU General Public License as published by
7;; the Free Software Foundation; either version 3, or (at your option)
8;; any later version.
9
10;; This file is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13;; GNU General Public License for more details.
14
15;; You should have received a copy of the GNU General Public License
16;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18;;; Commentary:
19
20;; Support for evaporating pieces of code.
21
22;;; Code:
23
24(defface shm-evaporate-face
25 '((((class color)) :foreground "#666666"))
26 "Face for text that will evaporate when modified/overwritten."
27 :group 'shm-evaporate)
28
29(defun shm-evaporate (beg end)
30 "Make the region evaporate when typed over."
31 (interactive "r")
32 (let ((o (make-overlay beg end nil nil nil)))
33 (overlay-put o 'shm-evaporate-overlay t)
34 (overlay-put o 'face 'shm-evaporate-face)
35 (overlay-put o 'shm-evaporate t)
36 (overlay-put o 'priority 2)
37 (overlay-put o 'modification-hooks '(shm-evaporate-modification-hook))
38 (overlay-put o 'insert-in-front-hooks '(shm-evaporate-insert-before-hook))))
39
40(defun shm-evaporate-modification-hook (o changed beg end &optional len)
41 "Remove the overlay after a modification occurs."
42 (let ((inhibit-modification-hooks t))
43 (when (and changed
44 (overlay-start o))
45 (delete-region (overlay-start o)
46 (overlay-end o))
47 (delete-overlay o))))
48
49(defun shm-evaporate-insert-before-hook (o changed beg end &optional len)
50 "Remove the overlay before inserting something at the start."
51 (let ((inhibit-modification-hooks t))
52 (when (and (not changed)
53 (overlay-start o))
54 (delete-region (overlay-start o)
55 (overlay-end o))
56 (delete-overlay o))))
57
58(provide 'shm-evaporate)
59
60;;; shm-evaporate.el ends here