smiley.el 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. ;;; smiley.el --- displaying smiley faces
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Dave Love <fx@gnu.org>
  4. ;; Keywords: news mail multimedia
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs 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. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; A re-written, simplified version of Wes Hardaker's XEmacs smiley.el
  18. ;; which might be merged back to smiley.el if we get an assignment for
  19. ;; that. We don't have assignments for the images smiley.el uses, but
  20. ;; I'm not sure we need that degree of rococoness and defaults like a
  21. ;; yellow background. Also, using PBM means we can display the images
  22. ;; more generally. -- fx
  23. ;; `smiley.el' was replaced by `smiley-ems.el' on 2002-01-26 (after fx'
  24. ;; comment).
  25. ;; Test smileys:
  26. ;; smile ^:-) ^:)
  27. ;; blink ;-) ;)
  28. ;; forced :-]
  29. ;; braindamaged 8-)
  30. ;; indifferent :-|
  31. ;; wry :-/ :-\
  32. ;; sad :-(
  33. ;; frown :-{
  34. ;; evil >:-)
  35. ;; cry ;-(
  36. ;; dead X-)
  37. ;; grin :-D
  38. ;;; Code:
  39. (eval-when-compile (require 'cl))
  40. (require 'nnheader)
  41. (require 'gnus-art)
  42. (defgroup smiley nil
  43. "Turn :-)'s into real images."
  44. :group 'gnus-visual)
  45. (defvar smiley-data-directory)
  46. (defcustom smiley-style
  47. (if (or (and (fboundp 'face-attribute)
  48. (>= (face-attribute 'default :height) 160))
  49. (and (fboundp 'face-height)
  50. (>= (face-height 'default) 14)))
  51. 'medium
  52. 'low-color)
  53. "Smiley style."
  54. :type '(choice (const :tag "small, 3 colors" low-color) ;; 13x14
  55. (const :tag "medium, ~10 colors" medium) ;; 16x16
  56. (const :tag "dull, grayscale" grayscale));; 14x14
  57. :set (lambda (symbol value)
  58. (set-default symbol value)
  59. (setq smiley-data-directory (smiley-directory))
  60. (smiley-update-cache))
  61. :initialize 'custom-initialize-default
  62. :version "23.1" ;; No Gnus
  63. :group 'smiley)
  64. ;; For compatibility, honor the variable `smiley-data-directory' if the user
  65. ;; has set it.
  66. (defun smiley-directory (&optional style)
  67. "Return a the location of the smiley faces files.
  68. STYLE specifies which style to use, see `smiley-style'. If STYLE
  69. is nil, use `smiley-style'."
  70. (unless style (setq style smiley-style))
  71. (nnheader-find-etc-directory
  72. (concat "images/smilies"
  73. (cond ((eq smiley-style 'low-color) "")
  74. ((eq smiley-style 'medium) "/medium")
  75. ((eq smiley-style 'grayscale) "/grayscale")))))
  76. (defcustom smiley-data-directory (smiley-directory)
  77. "*Location of the smiley faces files."
  78. :set (lambda (symbol value)
  79. (set-default symbol value)
  80. (smiley-update-cache))
  81. :initialize 'custom-initialize-default
  82. :type 'directory
  83. :group 'smiley)
  84. ;; The XEmacs version has a baroque, if not rococo, set of these.
  85. (defcustom smiley-regexp-alist
  86. '(("\\(;-)\\)\\W" 1 "blink")
  87. ("[^;]\\(;)\\)\\W" 1 "blink")
  88. ("\\(:-]\\)\\W" 1 "forced")
  89. ("\\(8-)\\)\\W" 1 "braindamaged")
  90. ("\\(:-|\\)\\W" 1 "indifferent")
  91. ("\\(:-[/\\]\\)\\W" 1 "wry")
  92. ("\\(:-(\\)\\W" 1 "sad")
  93. ("\\(X-)\\)\\W" 1 "dead")
  94. ("\\(:-{\\)\\W" 1 "frown")
  95. ("\\(>:-)\\)\\W" 1 "evil")
  96. ("\\(;-(\\)\\W" 1 "cry")
  97. ("\\(:-D\\)\\W" 1 "grin")
  98. ;; "smile" must be come after "evil"
  99. ("\\(\\^?:-?)\\)\\W" 1 "smile"))
  100. "*A list of regexps to map smilies to images.
  101. The elements are (REGEXP MATCH IMAGE), where MATCH is the submatch in
  102. regexp to replace with IMAGE. IMAGE is the name of an image file in
  103. `smiley-data-directory'."
  104. :version "24.1"
  105. :type '(repeat (list regexp
  106. (integer :tag "Regexp match number")
  107. (string :tag "Image name")))
  108. :set (lambda (symbol value)
  109. (set-default symbol value)
  110. (smiley-update-cache))
  111. :initialize 'custom-initialize-default
  112. :group 'smiley)
  113. (defcustom gnus-smiley-file-types
  114. (let ((types (list "pbm")))
  115. (when (gnus-image-type-available-p 'xpm)
  116. (push "xpm" types))
  117. (when (gnus-image-type-available-p 'gif)
  118. (push "gif" types))
  119. types)
  120. "*List of suffixes on smiley file names to try."
  121. :version "24.1"
  122. :type '(repeat string)
  123. :group 'smiley)
  124. (defvar smiley-cached-regexp-alist nil)
  125. (defun smiley-update-cache ()
  126. (setq smiley-cached-regexp-alist nil)
  127. (dolist (elt (if (symbolp smiley-regexp-alist)
  128. (symbol-value smiley-regexp-alist)
  129. smiley-regexp-alist))
  130. (let ((types gnus-smiley-file-types)
  131. file type)
  132. (while (and (not file)
  133. (setq type (pop types)))
  134. (unless (file-exists-p
  135. (setq file (expand-file-name (concat (nth 2 elt) "." type)
  136. smiley-data-directory)))
  137. (setq file nil)))
  138. (when type
  139. (let ((image (gnus-create-image file (intern type) nil
  140. :ascent 'center)))
  141. (when image
  142. (push (list (car elt) (cadr elt) image)
  143. smiley-cached-regexp-alist)))))))
  144. ;; Not implemented:
  145. ;; (defvar smiley-mouse-map
  146. ;; (let ((map (make-sparse-keymap)))
  147. ;; (define-key map [down-mouse-2] 'ignore) ; override widget
  148. ;; (define-key map [mouse-2]
  149. ;; 'smiley-mouse-toggle-buffer)
  150. ;; map))
  151. ;;;###autoload
  152. (defun smiley-region (start end)
  153. "Replace in the region `smiley-regexp-alist' matches with corresponding images.
  154. A list of images is returned."
  155. (interactive "r")
  156. (when (gnus-graphic-display-p)
  157. (unless smiley-cached-regexp-alist
  158. (smiley-update-cache))
  159. (save-excursion
  160. (let ((beg (or start (point-min)))
  161. group image images string)
  162. (dolist (entry smiley-cached-regexp-alist)
  163. (setq group (nth 1 entry)
  164. image (nth 2 entry))
  165. (goto-char beg)
  166. (while (re-search-forward (car entry) end t)
  167. (setq string (match-string group))
  168. (goto-char (match-end group))
  169. (delete-region (match-beginning group) (match-end group))
  170. (when image
  171. (push image images)
  172. (gnus-add-wash-type 'smiley)
  173. (gnus-add-image 'smiley image)
  174. (gnus-put-image image string 'smiley))))
  175. images))))
  176. ;;;###autoload
  177. (defun smiley-buffer (&optional buffer)
  178. "Run `smiley-region' at the BUFFER, specified in the argument or
  179. interactively. If there's no argument, do it at the current buffer."
  180. (interactive "bBuffer to run smiley-region: ")
  181. (save-excursion
  182. (if buffer
  183. (set-buffer (get-buffer buffer)))
  184. (smiley-region (point-min) (point-max))))
  185. (defun smiley-toggle-buffer (&optional arg)
  186. "Toggle displaying smiley faces in article buffer.
  187. With arg, turn displaying on if and only if arg is positive."
  188. (interactive "P")
  189. (gnus-with-article-buffer
  190. (if (if (numberp arg)
  191. (> arg 0)
  192. (not (memq 'smiley gnus-article-wash-types)))
  193. (smiley-region (point-min) (point-max))
  194. (gnus-delete-images 'smiley))))
  195. (defun smiley-mouse-toggle-buffer (event)
  196. "Toggle displaying smiley faces.
  197. With arg, turn displaying on if and only if arg is positive."
  198. (interactive "e")
  199. (save-excursion
  200. (save-window-excursion
  201. (mouse-set-point event)
  202. (smiley-toggle-buffer))))
  203. (provide 'smiley)
  204. ;;; smiley.el ends here