smiley.el 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. ;;; smiley.el --- displaying smiley faces
  2. ;; Copyright (C) 2000-2015 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. ;; In batch mode, attributes can be unspecified.
  49. (condition-case nil
  50. (>= (face-attribute 'default :height) 160)
  51. (error nil)))
  52. (and (fboundp 'face-height)
  53. (>= (face-height 'default) 14)))
  54. 'medium
  55. 'low-color)
  56. "Smiley style."
  57. :type '(choice (const :tag "small, 3 colors" low-color) ;; 13x14
  58. (const :tag "medium, ~10 colors" medium) ;; 16x16
  59. (const :tag "dull, grayscale" grayscale));; 14x14
  60. :set (lambda (symbol value)
  61. (set-default symbol value)
  62. (setq smiley-data-directory (smiley-directory))
  63. (smiley-update-cache))
  64. :initialize 'custom-initialize-default
  65. :version "23.1" ;; No Gnus
  66. :group 'smiley)
  67. ;; For compatibility, honor the variable `smiley-data-directory' if the user
  68. ;; has set it.
  69. (defun smiley-directory (&optional style)
  70. "Return a the location of the smiley faces files.
  71. STYLE specifies which style to use, see `smiley-style'. If STYLE
  72. is nil, use `smiley-style'."
  73. (unless style (setq style smiley-style))
  74. (nnheader-find-etc-directory
  75. (concat "images/smilies"
  76. (cond ((eq smiley-style 'low-color) "")
  77. ((eq smiley-style 'medium) "/medium")
  78. ((eq smiley-style 'grayscale) "/grayscale")))))
  79. (defcustom smiley-data-directory (smiley-directory)
  80. "*Location of the smiley faces files."
  81. :set (lambda (symbol value)
  82. (set-default symbol value)
  83. (smiley-update-cache))
  84. :initialize 'custom-initialize-default
  85. :type 'directory
  86. :group 'smiley)
  87. ;; The XEmacs version has a baroque, if not rococo, set of these.
  88. (defcustom smiley-regexp-alist
  89. '(("\\(;-)\\)\\W" 1 "blink")
  90. ("[^;]\\(;)\\)\\W" 1 "blink")
  91. ("\\(:-]\\)\\W" 1 "forced")
  92. ("\\(8-)\\)\\W" 1 "braindamaged")
  93. ("\\(:-|\\)\\W" 1 "indifferent")
  94. ("\\(:-[/\\]\\)\\W" 1 "wry")
  95. ("\\(:-(\\)\\W" 1 "sad")
  96. ("\\(X-)\\)\\W" 1 "dead")
  97. ("\\(:-{\\)\\W" 1 "frown")
  98. ("\\(>:-)\\)\\W" 1 "evil")
  99. ("\\(;-(\\)\\W" 1 "cry")
  100. ("\\(:-D\\)\\W" 1 "grin")
  101. ;; "smile" must be come after "evil"
  102. ("\\(\\^?:-?)\\)\\W" 1 "smile"))
  103. "*A list of regexps to map smilies to images.
  104. The elements are (REGEXP MATCH IMAGE), where MATCH is the submatch in
  105. regexp to replace with IMAGE. IMAGE is the name of an image file in
  106. `smiley-data-directory'."
  107. :version "24.1"
  108. :type '(repeat (list regexp
  109. (integer :tag "Regexp match number")
  110. (string :tag "Image name")))
  111. :set (lambda (symbol value)
  112. (set-default symbol value)
  113. (smiley-update-cache))
  114. :initialize 'custom-initialize-default
  115. :group 'smiley)
  116. (defcustom gnus-smiley-file-types
  117. (let ((types (list "pbm")))
  118. (when (gnus-image-type-available-p 'xpm)
  119. (push "xpm" types))
  120. (when (gnus-image-type-available-p 'gif)
  121. (push "gif" types))
  122. types)
  123. "*List of suffixes on smiley file names to try."
  124. :version "24.1"
  125. :type '(repeat string)
  126. :group 'smiley)
  127. (defvar smiley-cached-regexp-alist nil)
  128. (defun smiley-update-cache ()
  129. (setq smiley-cached-regexp-alist nil)
  130. (dolist (elt (if (symbolp smiley-regexp-alist)
  131. (symbol-value smiley-regexp-alist)
  132. smiley-regexp-alist))
  133. (let ((types gnus-smiley-file-types)
  134. file type)
  135. (while (and (not file)
  136. (setq type (pop types)))
  137. (unless (file-exists-p
  138. (setq file (expand-file-name (concat (nth 2 elt) "." type)
  139. smiley-data-directory)))
  140. (setq file nil)))
  141. (when type
  142. (let ((image (gnus-create-image file (intern type) nil
  143. :ascent 'center)))
  144. (when image
  145. (push (list (car elt) (cadr elt) image)
  146. smiley-cached-regexp-alist)))))))
  147. ;; Not implemented:
  148. ;; (defvar smiley-mouse-map
  149. ;; (let ((map (make-sparse-keymap)))
  150. ;; (define-key map [down-mouse-2] 'ignore) ; override widget
  151. ;; (define-key map [mouse-2]
  152. ;; 'smiley-mouse-toggle-buffer)
  153. ;; map))
  154. ;;;###autoload
  155. (defun smiley-region (start end)
  156. "Replace in the region `smiley-regexp-alist' matches with corresponding images.
  157. A list of images is returned."
  158. (interactive "r")
  159. (when (gnus-graphic-display-p)
  160. (unless smiley-cached-regexp-alist
  161. (smiley-update-cache))
  162. (save-excursion
  163. (let ((beg (or start (point-min)))
  164. group image images string)
  165. (dolist (entry smiley-cached-regexp-alist)
  166. (setq group (nth 1 entry)
  167. image (nth 2 entry))
  168. (goto-char beg)
  169. (while (re-search-forward (car entry) end t)
  170. (setq string (match-string group))
  171. (goto-char (match-end group))
  172. (delete-region (match-beginning group) (match-end group))
  173. (when image
  174. (push image images)
  175. (gnus-add-wash-type 'smiley)
  176. (gnus-add-image 'smiley image)
  177. (gnus-put-image image string 'smiley))))
  178. images))))
  179. ;;;###autoload
  180. (defun smiley-buffer (&optional buffer)
  181. "Run `smiley-region' at the BUFFER, specified in the argument or
  182. interactively. If there's no argument, do it at the current buffer."
  183. (interactive "bBuffer to run smiley-region: ")
  184. (save-excursion
  185. (if buffer
  186. (set-buffer (get-buffer buffer)))
  187. (smiley-region (point-min) (point-max))))
  188. (defun smiley-toggle-buffer (&optional arg)
  189. "Toggle displaying smiley faces in article buffer.
  190. With arg, turn displaying on if and only if arg is positive."
  191. (interactive "P")
  192. (gnus-with-article-buffer
  193. (if (if (numberp arg)
  194. (> arg 0)
  195. (not (memq 'smiley gnus-article-wash-types)))
  196. (smiley-region (point-min) (point-max))
  197. (gnus-delete-images 'smiley))))
  198. (defun smiley-mouse-toggle-buffer (event)
  199. "Toggle displaying smiley faces.
  200. With arg, turn displaying on if and only if arg is positive."
  201. (interactive "e")
  202. (save-excursion
  203. (save-window-excursion
  204. (mouse-set-point event)
  205. (smiley-toggle-buffer))))
  206. (provide 'smiley)
  207. ;;; smiley.el ends here