1 ;;; newst-reader.el --- Generic RSS reader functions.
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Ulf Jasper <ulf.jasper@web.de>
7 ;; Filename: newst-reader.el
8 ;; URL: http://www.nongnu.org/newsticker
9 ;; Time-stamp: "6. Dezember 2009, 19:16:38 (ulf)"
11 ;; ======================================================================
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; ======================================================================
33 ;; ======================================================================
36 (require 'newst-backend
)
38 ;; ======================================================================
40 ;; ======================================================================
41 (defun newsticker--set-customvar-formatting (symbol value
)
42 "Set newsticker-variable SYMBOL value to VALUE.
43 Calls all actions which are necessary in order to make the new
45 (if (or (not (boundp symbol
))
46 (equal (symbol-value symbol
) value
))
48 ;; something must have changed
50 (when (fboundp 'newsticker--forget-preformatted
)
51 (newsticker--forget-preformatted))))
53 ;; ======================================================================
55 (defgroup newsticker-reader nil
56 "Settings for the feed reader."
59 (defcustom newsticker-frontend
61 "Newsticker frontend for reading news.
62 This must be one of the functions `newsticker-plainview' or
63 `newsticker-treeview'."
64 :type
'(choice :tag
"Frontend"
65 (const :tag
"Single buffer (plainview)" newsticker-plainview
)
66 (const :tag
"Tree view (treeview)" newsticker-treeview
))
67 :group
'newsticker-reader
)
69 ;; image related things
70 (defcustom newsticker-enable-logo-manipulations
72 "If non-nil newsticker manipulates logo images.
73 This enables the following image properties: heuristic mask for all
74 logos, and laplace-conversion for images without new items."
76 :group
'newsticker-reader
)
78 (defcustom newsticker-justification
80 "How to fill item descriptions.
81 If non-nil newsticker calls `fill-region' to wrap long lines in
82 item descriptions. However, if an item description contains HTML
83 text and `newsticker-html-renderer' is non-nil, filling is not
85 :type
'(choice :tag
"Justification"
86 (const :tag
"No filling" nil
)
87 (const :tag
"Left" left
)
88 (const :tag
"Right" right
)
89 (const :tag
"Center" center
)
90 (const :tag
"Full" full
))
91 :set
'newsticker--set-customvar-formatting
92 :group
'newsticker-reader
)
94 (defcustom newsticker-use-full-width
96 "Decides whether to use the full window width when filling.
97 If non-nil newsticker sets `fill-column' so that the whole
98 window is used when filling. See also `newsticker-justification'."
100 :set
'newsticker--set-customvar-formatting
101 :group
'newsticker-reader
)
103 (defcustom newsticker-html-renderer
105 "Function for rendering HTML contents.
106 If non-nil, newsticker.el will call this function whenever it finds
107 HTML-like tags in item descriptions. Possible functions are, for
108 example, `w3m-region', `w3-region', and (if you have htmlr.el installed)
109 `newsticker-htmlr-render'.
111 In order to make sure that the HTML renderer is loaded when you
112 run newsticker, you should add one of the following statements to
113 your .emacs. If you use w3m,
115 (autoload 'w3m-region \"w3m\"
116 \"Render region in current buffer and replace with result.\" t)
118 (autoload 'w3m-toggle-inline-image \"w3m\"
119 \"Toggle the visibility of an image under point.\" t)
128 :type
'(choice :tag
"Function"
129 (const :tag
"None" nil
)
130 (const :tag
"w3" w3-region
)
131 (const :tag
"w3m" w3m-region
)
132 (const :tag
"htmlr" newsticker-htmlr-render
))
133 :set
'newsticker--set-customvar-formatting
134 :group
'newsticker-reader
)
136 (defcustom newsticker-date-format
138 "Format for the date part in item and feed lines.
139 See `format-time-string' for a list of valid specifiers."
141 :set
'newsticker--set-customvar-formatting
142 :group
'newsticker-reader
)
144 (defgroup newsticker-faces nil
145 "Settings for the faces of the feed reader."
146 :group
'newsticker-reader
)
148 (defface newsticker-feed-face
149 '((((class color
) (background dark
))
150 (:family
"helvetica" :bold t
:height
1.2 :foreground
"misty rose"))
151 (((class color
) (background light
))
152 (:family
"helvetica" :bold t
:height
1.2 :foreground
"black")))
153 "Face for news feeds."
154 :group
'newsticker-faces
)
156 (defface newsticker-extra-face
157 '((((class color
) (background dark
))
158 (:italic t
:foreground
"gray50" :height
0.8))
159 (((class color
) (background light
))
160 (:italic t
:foreground
"gray50" :height
0.8)))
161 "Face for newsticker dates."
162 :group
'newsticker-faces
)
164 (defface newsticker-enclosure-face
165 '((((class color
) (background dark
))
166 (:bold t
:background
"orange"))
167 (((class color
) (background light
))
168 (:bold t
:background
"orange")))
169 "Face for enclosed elements."
170 :group
'newsticker-faces
)
172 ;; ======================================================================
173 ;;; Utility functions
174 ;; ======================================================================
175 (defun newsticker--insert-enclosure (item keymap
)
176 "Insert enclosure element of a news ITEM into the current buffer.
177 KEYMAP will be applied."
178 (let ((enclosure (newsticker--enclosure item
))
181 (let ((url (cdr (assoc 'url enclosure
)))
182 (length (string-to-number (or (cdr (assoc 'length enclosure
))
184 (type (cdr (assoc 'type enclosure
))))
185 (cond ((> length
1048576)
186 (insert (format "Enclosed file (%s, %1.2f MBytes)" type
187 (/ length
1048576))))
189 (insert (format "Enclosed file (%s, %1.2f KBytes)" type
192 (insert (format "Enclosed file (%s, %1.2f Bytes)" type
195 (insert (format "Enclosed file (%s, unknown size)" type
))))
196 (add-text-properties beg
(point)
197 (list 'mouse-face
'highlight
200 "mouse-2: visit (%s)" url
)
206 (defun newsticker--print-extra-elements (item keymap
)
207 "Insert extra-elements of ITEM in a pretty form into the current buffer.
209 (let ((ignored-elements '(items link title description content
210 content
:encoded dc
:subject
211 dc
:date entry item guid pubDate
214 (left-column-width 1))
215 (mapc (lambda (extra-element)
216 (when (listp extra-element
) ;; take care of broken xml
218 (unless (memq (car extra-element
) ignored-elements
)
219 (setq left-column-width
(max left-column-width
221 (car extra-element
))))))))
222 (newsticker--extra item
))
223 (mapc (lambda (extra-element)
224 (when (listp extra-element
) ;; take care of broken xml
226 (unless (memq (car extra-element
) ignored-elements
)
227 (newsticker--do-print-extra-element extra-element
230 (newsticker--extra item
))))
232 (defun newsticker--do-print-extra-element (extra-element width keymap
)
233 "Actually print an EXTRA-ELEMENT using the given WIDTH.
235 (let ((name (symbol-name (car extra-element
))))
236 (insert (format "%s: " name
))
237 (insert (make-string (- width
(length name
)) ?
)))
238 (let (;;(attributes (cadr extra-element)) ;FIXME!!!!
239 (contents (cddr extra-element
)))
240 (cond ((listp contents
)
243 (string-match "^http://.*" i
))
245 (insert i
" ") ; avoid self-reference from the
249 (list 'mouse-face
'highlight
252 (format "mouse-2: visit (%s)" i
)
254 (insert (format "%s" i
))))
257 (insert (format "%s" contents
))))
260 (defun newsticker--image-read (feed-name-symbol disabled
)
261 "Read the cached image for FEED-NAME-SYMBOL from disk.
262 If DISABLED is non-nil the image will be converted to a disabled look
263 \(unless `newsticker-enable-logo-manipulations' is not t\).
265 (let ((image-name (concat (newsticker--images-dir)
266 (symbol-name feed-name-symbol
)))
268 (when (file-exists-p image-name
)
269 (condition-case error-data
270 (setq img
(create-image
272 :conversion
(and newsticker-enable-logo-manipulations
275 :mask
(and newsticker-enable-logo-manipulations
279 (message "Error: cannot create image for %s: %s"
280 feed-name-symbol error-data
))))
283 ;; the functions we need for retrieval and display
285 (defun newsticker-show-news ()
286 "Start reading news. You may want to bind this to a key."
288 (newsticker-start t
) ;; will start only if not running
289 (funcall newsticker-frontend
))
291 ;; ======================================================================
293 ;; ======================================================================
294 (defconst newsticker--next-item-image
295 (and (fboundp 'image-type-available-p
)
296 (image-type-available-p 'xpm
)
297 (create-image "/* XPM */
298 static char * next_xpm[] = {
368 "Image for the next item button.")
370 (defconst newsticker--previous-item-image
371 (and (fboundp 'image-type-available-p
)
372 (image-type-available-p 'xpm
)
373 (create-image "/* XPM */
374 static char * previous_xpm[] = {
441 "Image for the previous item button.")
443 (defconst newsticker--previous-feed-image
444 (and (fboundp 'image-type-available-p
)
445 (image-type-available-p 'xpm
)
446 (create-image "/* XPM */
447 static char * prev_feed_xpm[] = {
512 \" .&. .~[}>{*=-. \",
527 "Image for the previous feed button.")
529 (defconst newsticker--next-feed-image
530 (and (fboundp 'image-type-available-p
)
531 (image-type-available-p 'xpm
)
532 (create-image "/* XPM */
533 static char * next_feed_xpm[] = {
603 \" .&*=12'3~. .-. \",
618 "Image for the next feed button.")
620 (defconst newsticker--mark-read-image
621 (and (fboundp 'image-type-available-p
)
622 (image-type-available-p 'xpm
)
623 (create-image "/* XPM */
624 static char * mark_read_xpm[] = {
696 "Image for the mark read button.")
698 (defconst newsticker--mark-immortal-image
699 (and (fboundp 'image-type-available-p
)
700 (image-type-available-p 'xpm
)
701 (create-image "/* XPM */
702 static char * mark_immortal_xpm[] = {
801 \" $ @ % & * * = - + + \",
802 \" @ ; > , ' ) ' ! * - ~ @ \",
803 \" @ { > ! ] ^ / / ( ' * ; _ : \",
804 \" < _ ; [ ) / } | } / ] , 1 2 3 4 \",
805 \" 5 6 7 , ] 8 9 9 9 } ^ ! = ~ 0 a \",
806 \" b c 6 - , ] 8 9 9 9 } ^ ! % ~ 0 d 5 \",
807 \" : e _ ; * ) / 8 } } / ] , 1 2 3 f 5 \",
808 \" : g h { = i j ^ / ^ ] ! * ; k e l m \",
809 \" : f n o ; > , ' ) ' ! * - 2 0 p q r \",
810 \" : s g 0 6 ; % > * * = - ~ h t l u r \",
811 \" v u w x y k ~ z A z B o C D E F G b \",
812 \" 5 H I J e 0 h K h C c x L M N . \",
813 \" 4 O P q Q d g x g J L R H S T < \",
814 \" @ T U P F q V q M H N W X + \",
815 \" @ Y T O W G G W O X Y @ \",
816 \" 4 Z ` Y Y Y .` 4 4 \",
823 "Image for the mark immortal button.")
825 (defconst newsticker--narrow-image
826 (and (fboundp 'image-type-available-p
)
827 (image-type-available-p 'xpm
)
828 (create-image "/* XPM */
829 static char * narrow_xpm[] = {
882 \" .................. \",
883 \" .+@#$%&*=*-;>,')!. \",
884 \" .................. \",
887 \" .................. \",
888 \" .~{]^/(___:<[}|12. \",
889 \" .................. \",
892 \" .................. \",
893 \" .!3@45>666789'0ab. \",
894 \" .................. \",
897 \" .................. \",
898 \" .cccdefghhgficccc. \",
899 \" .................. \",
905 "Image for the narrow image button.")
907 (defconst newsticker--get-all-image
908 (and (fboundp 'image-type-available-p
)
909 (image-type-available-p 'xpm
)
910 (create-image "/* XPM */
911 static char * get_all_xpm[] = {
987 \" ................... \",
988 \" .+@#$%&*=*&-;>,')!. \",
989 \" ................... \",
991 \" ................... \",
992 \" .~{]^/(___:<[}|123. \",
993 \" ................... \",
995 \" ................... \",
996 \" .45678909abcdefg. \",
997 \" .h5icj7jklmeno. \",
1009 "Image for the get all image button.")
1011 (defconst newsticker--update-image
1012 (and (fboundp 'image-type-available-p
)
1013 (image-type-available-p 'xpm
)
1014 (create-image "/* XPM */
1015 static char * update_xpm[] = {
1080 "Image for the update button.")
1082 (defconst newsticker--browse-image
1083 (and (fboundp 'image-type-available-p
)
1084 (image-type-available-p 'xpm
)
1085 (create-image "/* XPM */
1086 static char * visit_xpm[] = {
1135 \" .++.%%%#&.++. \",
1136 \" .++.$%%%#*=.++. \",
1137 \" .++.-@;##$*>,.++. \",
1138 \" .++.')!&@@*=~{].++. \",
1139 \" .++.^{~>---)/(_:<.++. \",
1140 \" .++.^[,~/~'(_}|.++. \",
1141 \" .++.]_1[12^:|.++. \",
1142 \" .++.:}33:45.++. \",
1143 \" .++.<5567.++. \",
1153 "Image for the browse button.")
1155 (defun newsticker-browse-url-item (feed item
)
1156 "Convert FEED ITEM to html and call `browse-url' on result."
1158 (let ((t-file (make-temp-file "newsticker")))
1159 (with-temp-file t-file
1160 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>
1161 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
1162 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
1163 <html xmlns=\"http://www.w3.org/1999/xhtml\">
1165 (insert "<h1>" feed
": " (newsticker--title item
) "</h1>")
1166 (insert (format-time-string newsticker-date-format
1167 (newsticker--time item
)))
1169 (insert (or (newsticker--desc item
) "[No Description]"))
1170 (when (newsticker--enclosure item
)
1171 (insert "<br/><hr/><i>")
1172 (newsticker--insert-enclosure item nil
)
1174 (when (newsticker--extra item
)
1175 (insert "<br/><hr/><tt>")
1176 (newsticker--print-extra-elements item nil
)
1178 (insert "</body></html>"))
1179 (browse-url t-file
)))
1181 (provide 'newst-reader
)
1183 ;; arch-tag: c604b701-bdf1-4fc1-8d05-5fabd1939533
1184 ;;; newst-reader.el ends here