newst-ticker.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ;; newst-ticker.el --- modeline ticker for newsticker.
  2. ;; Copyright (C) 2003-2012 Free Software Foundation, Inc.
  3. ;; Author: Ulf Jasper <ulf.jasper@web.de>
  4. ;; Filename: newst-ticker.el
  5. ;; URL: http://www.nongnu.org/newsticker
  6. ;; Keywords: News, RSS, Atom
  7. ;; Time-stamp: "6. Dezember 2009, 19:16:00 (ulf)"
  8. ;; Package: newsticker
  9. ;; ======================================================================
  10. ;; This file is part of GNU Emacs.
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;; ======================================================================
  22. ;;; Commentary:
  23. ;; See newsticker.el
  24. ;; ======================================================================
  25. ;;; Code:
  26. (require 'newst-backend)
  27. (defvar newsticker--ticker-timer nil
  28. "Timer for newsticker ticker.")
  29. ;;;###autoload
  30. (defun newsticker-ticker-running-p ()
  31. "Check whether newsticker's actual ticker is running.
  32. Return t if ticker is running, nil otherwise. Newsticker is
  33. considered to be running if the newsticker timer list is not
  34. empty."
  35. (timerp newsticker--ticker-timer))
  36. ;; customization group ticker
  37. (defgroup newsticker-ticker nil
  38. "Settings for the headline ticker."
  39. :group 'newsticker)
  40. (defun newsticker--set-customvar-ticker (symbol value)
  41. "Set newsticker-variable SYMBOL value to VALUE.
  42. Calls all actions which are necessary in order to make the new
  43. value effective."
  44. (if (or (not (boundp symbol))
  45. (equal (symbol-value symbol) value))
  46. (set symbol value)
  47. ;; something must have changed -- restart ticker
  48. (when (newsticker-running-p)
  49. (message "Restarting ticker")
  50. (newsticker-stop-ticker)
  51. (newsticker--ticker-text-setup)
  52. (newsticker-start-ticker)
  53. (message ""))))
  54. (defcustom newsticker-ticker-interval
  55. 0.3
  56. "Time interval for displaying news items in the echo area (seconds).
  57. If equal or less than 0 no messages are shown in the echo area. For
  58. smooth display (see `newsticker-scroll-smoothly') a value of 0.3 seems
  59. reasonable. For non-smooth display a value of 10 is a good starting
  60. point."
  61. :type 'number
  62. :set 'newsticker--set-customvar-ticker
  63. :group 'newsticker-ticker)
  64. (defcustom newsticker-scroll-smoothly
  65. t
  66. "Decides whether to flash or scroll news items.
  67. If t the news headlines are scrolled (more-or-less) smoothly in the echo
  68. area. If nil one headline after another is displayed in the echo area.
  69. The variable `newsticker-ticker-interval' determines how fast this
  70. display moves/changes and whether headlines are shown in the echo area
  71. at all. If you change `newsticker-scroll-smoothly' you should also change
  72. `newsticker-ticker-interval'."
  73. :type 'boolean
  74. :group 'newsticker-ticker)
  75. (defcustom newsticker-hide-immortal-items-in-echo-area
  76. t
  77. "Decides whether to show immortal/non-expiring news items in the ticker.
  78. If t the echo area will not show immortal items. See also
  79. `newsticker-hide-old-items-in-echo-area'."
  80. :type 'boolean
  81. :set 'newsticker--set-customvar-ticker
  82. :group 'newsticker-ticker)
  83. (defcustom newsticker-hide-old-items-in-echo-area
  84. t
  85. "Decides whether to show only the newest news items in the ticker.
  86. If t the echo area will show only new items, i.e. only items which have
  87. been added between the last two retrievals."
  88. :type 'boolean
  89. :set 'newsticker--set-customvar-ticker
  90. :group 'newsticker-ticker)
  91. (defcustom newsticker-hide-obsolete-items-in-echo-area
  92. t
  93. "Decides whether to show obsolete items items in the ticker.
  94. If t the echo area will not show obsolete items. See also
  95. `newsticker-hide-old-items-in-echo-area'."
  96. :type 'boolean
  97. :set 'newsticker--set-customvar-ticker
  98. :group 'newsticker-ticker)
  99. (defun newsticker--display-tick ()
  100. "Called from the display timer.
  101. This function calls a display function, according to the variable
  102. `newsticker-scroll-smoothly'."
  103. (if newsticker-scroll-smoothly
  104. (newsticker--display-scroll)
  105. (newsticker--display-jump)))
  106. (defsubst newsticker--echo-area-clean-p ()
  107. "Check whether somebody is using the echo area / minibuffer.
  108. Return t if echo area and minibuffer are unused."
  109. (not (or (active-minibuffer-window)
  110. (and (current-message)
  111. (not (string= (current-message)
  112. newsticker--prev-message))))))
  113. (defun newsticker--display-jump ()
  114. "Called from the display timer.
  115. This function displays the next ticker item in the echo area, unless
  116. there is another message displayed or the minibuffer is active."
  117. (let ((message-log-max nil));; prevents message text from being logged
  118. (when (newsticker--echo-area-clean-p)
  119. (setq newsticker--item-position (1+ newsticker--item-position))
  120. (when (>= newsticker--item-position (length newsticker--item-list))
  121. (setq newsticker--item-position 0))
  122. (setq newsticker--prev-message
  123. (nth newsticker--item-position newsticker--item-list))
  124. (message "%s" newsticker--prev-message))))
  125. (defun newsticker--display-scroll ()
  126. "Called from the display timer.
  127. This function scrolls the ticker items in the echo area, unless
  128. there is another message displayed or the minibuffer is active."
  129. (when (newsticker--echo-area-clean-p)
  130. (let* ((width (- (frame-width) 1))
  131. (message-log-max nil);; prevents message text from being logged
  132. (i newsticker--item-position)
  133. subtext
  134. (s-text newsticker--scrollable-text)
  135. (l (length s-text)))
  136. ;; don't show anything if there is nothing to show
  137. (unless (< (length s-text) 1)
  138. ;; repeat the ticker string if it is shorter than frame width
  139. (while (< (length s-text) width)
  140. (setq s-text (concat s-text s-text)))
  141. ;; get the width of the printed string
  142. (setq l (length s-text))
  143. (cond ((< i (- l width))
  144. (setq subtext (substring s-text i (+ i width))))
  145. (t
  146. (setq subtext (concat
  147. (substring s-text i l)
  148. (substring s-text 0 (- width (- l i)))))))
  149. ;; Take care of multibyte strings, for which (string-width) is
  150. ;; larger than (length).
  151. ;; Actually, such strings may be smaller than (frame-width)
  152. ;; because return values of (string-width) are too large:
  153. ;; (string-width "<japanese character>") => 2
  154. (let ((t-width (1- (length subtext))))
  155. (while (> (string-width subtext) width)
  156. (setq subtext (substring subtext 0 t-width))
  157. (setq t-width (1- t-width))))
  158. ;; show the ticker text and save current position
  159. (message "%s" subtext)
  160. (setq newsticker--prev-message subtext)
  161. (setq newsticker--item-position (1+ i))
  162. (when (>= newsticker--item-position l)
  163. (setq newsticker--item-position 0))))))
  164. ;;;###autoload
  165. (defun newsticker-start-ticker ()
  166. "Start newsticker's ticker (but not the news retrieval).
  167. Start display timer for the actual ticker if wanted and not
  168. running already."
  169. (interactive)
  170. (if (and (> newsticker-ticker-interval 0)
  171. (not newsticker--ticker-timer))
  172. (setq newsticker--ticker-timer
  173. (run-at-time newsticker-ticker-interval
  174. newsticker-ticker-interval
  175. 'newsticker--display-tick))))
  176. (defun newsticker-stop-ticker ()
  177. "Stop newsticker's ticker (but not the news retrieval)."
  178. (interactive)
  179. (when newsticker--ticker-timer
  180. (cancel-timer newsticker--ticker-timer)
  181. (setq newsticker--ticker-timer nil)))
  182. ;; ======================================================================
  183. ;;; Manipulation of ticker text
  184. ;; ======================================================================
  185. (defun newsticker--ticker-text-setup ()
  186. "Build the ticker text which is scrolled or flashed in the echo area."
  187. ;; reset scrollable text
  188. (setq newsticker--scrollable-text "")
  189. (setq newsticker--item-list nil)
  190. (setq newsticker--item-position 0)
  191. ;; build scrollable text from cache data
  192. (let ((have-something nil))
  193. (mapc
  194. (lambda (feed)
  195. (let ((feed-name (symbol-name (car feed))))
  196. (let ((num-new (newsticker--stat-num-items (car feed) 'new))
  197. (num-old (newsticker--stat-num-items (car feed) 'old))
  198. (num-imm (newsticker--stat-num-items (car feed) 'immortal))
  199. (num-obs (newsticker--stat-num-items (car feed) 'obsolete)))
  200. (when (or (> num-new 0)
  201. (and (> num-old 0)
  202. (not newsticker-hide-old-items-in-echo-area))
  203. (and (> num-imm 0)
  204. (not newsticker-hide-immortal-items-in-echo-area))
  205. (and (> num-obs 0)
  206. (not newsticker-hide-obsolete-items-in-echo-area)))
  207. (setq have-something t)
  208. (mapc
  209. (lambda (item)
  210. (let ((title (replace-regexp-in-string
  211. "[\r\n]+" " "
  212. (newsticker--title item)))
  213. (age (newsticker--age item)))
  214. (unless (string= title newsticker--error-headline)
  215. (when
  216. (or (eq age 'new)
  217. (and (eq age 'old)
  218. (not newsticker-hide-old-items-in-echo-area))
  219. (and (eq age 'obsolete)
  220. (not
  221. newsticker-hide-obsolete-items-in-echo-area))
  222. (and (eq age 'immortal)
  223. (not
  224. newsticker-hide-immortal-items-in-echo-area)))
  225. (setq title (newsticker--remove-whitespace title))
  226. ;; add to flash list
  227. (add-to-list 'newsticker--item-list
  228. (concat feed-name ": " title) t)
  229. ;; and to the scrollable text
  230. (setq newsticker--scrollable-text
  231. (concat newsticker--scrollable-text
  232. " " feed-name ": " title " +++"))))))
  233. (cdr feed))))))
  234. newsticker--cache)
  235. (when have-something
  236. (setq newsticker--scrollable-text
  237. (concat "+++ "
  238. (format-time-string "%A, %H:%M"
  239. newsticker--latest-update-time)
  240. " ++++++" newsticker--scrollable-text)))))
  241. (defun newsticker--ticker-text-remove (feed title)
  242. "Remove the item of FEED with TITLE from the ticker text."
  243. ;; reset scrollable text
  244. (setq newsticker--item-position 0)
  245. (let ((feed-name (symbol-name feed))
  246. (t-title (replace-regexp-in-string "[\r\n]+" " " title)))
  247. ;; remove from flash list
  248. (setq newsticker--item-list (remove (concat feed-name ": " t-title)
  249. newsticker--item-list))
  250. ;; and from the scrollable text
  251. (setq newsticker--scrollable-text
  252. (replace-regexp-in-string
  253. (regexp-quote (concat " " feed-name ": " t-title " +++"))
  254. ""
  255. newsticker--scrollable-text))
  256. (if (string-match (concat "^\\+\\+\\+ [A-Z][a-z]+, "
  257. "[012]?[0-9]:[0-9][0-9] \\+\\+\\+\\+\\+\\+$")
  258. newsticker--scrollable-text)
  259. (setq newsticker--scrollable-text ""))))
  260. (provide 'newst-ticker)
  261. ;;; newst-ticker.el ends here