summaryrefslogtreecommitdiffstats
path: root/.emacs.d/site-lisp/autosmiley.el
blob: 1037e432865486361330048e8409235737c770a0 (plain)
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
;;; autosmiley.el --- Convert smileys into their graphical representation

;; Author: Damyan Pepper (gmail account, username damyanp)
;; Created: 20060315

;; 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 2, 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 this program; if not, you can either send email to this
;; program's maintainer or write to: The Free Software Foundation,
;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Defines the minor mode autosmiley-mode that converts smileys like
;; :-) into their graphical representations on the fly.

;; Tested on:
;;
;; GNU Emacs 22.0.50.1 (i386-mingw-nt5.1.2600) of 2006-03-14 on W2ONE
;;
;; History:
;;
;; 20060315 - First Release



(require 'smiley)

(defun autosmiley-overlay-p (overlay)
  "Return whether OVERLAY is an overlay of autosmiley mode."
  (memq (overlay-get overlay 'category)
		'(autosmiley)))

(defun autosmiley-remove-smileys (beg end)
  (dolist (o (overlays-in beg end))
	(when (autosmiley-overlay-p o)
	  (delete-overlay o))))

(defvar *autosmiley-counter* 0
  "Each smiley needs to have a unique display string otherwise
  adjacent smileys will be merged into a single image.  So we put
  a counter on each one to make them unique")

(defun autosmiley-add-smiley (beg end image)  
  (let ((overlay (make-overlay beg end)))
	(overlay-put overlay 'category 'autosmiley)
	(overlay-put overlay 'display (append image (list :counter (incf *autosmiley-counter*))))))


(defun autosmiley-add-smileys (beg end)
  (save-excursion
	(dolist (entry smiley-cached-regexp-alist)
	  (let ((regexp (car entry))
			(group (nth 1 entry))
			(image (nth 2 entry)))
		(when image
		  (goto-char beg)
		  (while (re-search-forward regexp end t)
			(autosmiley-add-smiley (match-beginning group) (match-end group) image)))))))


(defun autosmiley-change (beg end &optional old-len)
  (let ((beg-line (save-excursion (goto-char beg) (line-beginning-position)))
		(end-line (save-excursion (goto-char end) (line-end-position))))
	(autosmiley-remove-smileys beg-line end-line)
	(autosmiley-add-smileys beg-line end-line)))
  

;;;###autoload
(define-minor-mode autosmiley-mode
  "Minor mode for automatically replacing smileys in text with
cute little graphical smileys."
  :group 'autosmiley :lighter " :)"
  (save-excursion
	(save-restriction
	  (widen)
	  (autosmiley-remove-smileys (point-min) (point-max))
	  (if autosmiley-mode
		  (progn
			(unless smiley-cached-regexp-alist
			  (smiley-update-cache))
			(jit-lock-register 'autosmiley-change))
		(jit-lock-unregister 'autosmiley-change)))))


(provide 'autosmiley)