forked from skeeto/elfeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elfeed-show.el
167 lines (148 loc) · 6 KB
/
elfeed-show.el
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
;;; elfeed-show.el --- display feed entries -*- lexical-binding: t; -*-
;; This is free and unencumbered software released into the public domain.
;;; Code:
(require 'cl)
(require 'shr)
(require 'url-parse)
(require 'browse-url)
(require 'message) ; faces
(require 'elfeed-db)
(require 'elfeed-lib)
(require 'elfeed-search)
(defvar elfeed-show-entry nil
"The entry being displayed in this buffer.")
(defvar elfeed-show-mode-map
(let ((map (make-sparse-keymap)))
(prog1 map
(suppress-keymap map)
(define-key map "q" 'elfeed-kill-buffer)
(define-key map "g" 'elfeed-show-refresh)
(define-key map "n" 'elfeed-show-next)
(define-key map "p" 'elfeed-show-prev)
(define-key map "b" 'elfeed-show-visit)
(define-key map "y" 'elfeed-show-yank)
(define-key map "u" (elfeed-expose #'elfeed-show-tag 'unread))
(define-key map (kbd "SPC") 'scroll-up-command)
(define-key map (kbd "DEL") 'scroll-down-command)
(define-key map [mouse-2] 'shr-browse-url)))
"Keymap for `elfeed-show-mode'.")
(defun elfeed-show-mode ()
"Mode for displaying Elfeed feed entries.
\\{elfeed-show-mode-map}"
(interactive)
(kill-all-local-variables)
(use-local-map elfeed-show-mode-map)
(setq major-mode 'elfeed-show-mode
mode-name "elfeed-show"
buffer-read-only t)
(make-local-variable 'elfeed-show-entry)
(run-hooks 'elfeed-show-mode-hook))
(defun elfeed-insert-html (html &optional base-url)
"Converted HTML markup to a propertized string."
(shr-insert-document
(if (elfeed-libxml-supported-p)
(with-temp-buffer
;; insert <base> to work around libxml-parse-html-region bug
(insert (format "<base href=\"%s\">" base-url))
(insert html)
(libxml-parse-html-region (point-min) (point-max) base-url))
'(i () "Elfeed: libxml2 functionality is unavailable"))))
(defun* elfeed-insert-link (url &optional (content url))
"Insert a clickable hyperlink to URL titled CONTENT."
(when (> (length content) (- shr-width 8))
(let ((len (- (/ shr-width 2) 10)))
(setq content (format "%s[...]%s"
(substring content 0 len)
(substring content (- len))))))
(elfeed-insert-html (format "<a href=\"%s\">%s</a>" url content)))
(defun elfeed-compute-base (url)
"Return the base URL for URL, useful for relative paths."
(let ((obj (url-generic-parse-url url)))
(setf (url-filename obj) nil)
(setf (url-target obj) nil)
(url-recreate-url obj)))
(defun elfeed-show-refresh ()
"Update the buffer to match the selected entry."
(interactive)
(let* ((inhibit-read-only t)
(title (elfeed-entry-title elfeed-show-entry))
(date (seconds-to-time (elfeed-entry-date elfeed-show-entry)))
(link (elfeed-entry-link elfeed-show-entry))
(tags (elfeed-entry-tags elfeed-show-entry))
(tagsstr (mapconcat #'symbol-name tags ", "))
(nicedate (format-time-string "%a, %e %b %Y %T %Z" date))
(content (elfeed-deref (elfeed-entry-content elfeed-show-entry)))
(type (elfeed-entry-content-type elfeed-show-entry))
(feed (elfeed-entry-feed elfeed-show-entry))
(feed-title (elfeed-feed-title feed))
(base (and feed (elfeed-compute-base (elfeed-feed-url feed)))))
(erase-buffer)
(insert (format (propertize "Title: %s\n" 'face 'message-header-name)
(propertize title 'face 'message-header-subject)))
(insert (format (propertize "Date: %s\n" 'face 'message-header-name)
(propertize nicedate 'face 'message-header-other)))
(insert (format (propertize "Feed: %s\n" 'face 'message-header-name)
(propertize feed-title 'face 'message-header-other)))
(when tags
(insert (format (propertize "Tags: %s\n" 'face 'message-header-name)
(propertize tagsstr 'face 'message-header-other))))
(insert (propertize "Link: " 'face 'message-header-name))
(elfeed-insert-link link link)
(insert "\n")
(loop for enclosure in (elfeed-entry-enclosures elfeed-show-entry)
do (insert (propertize "Enclosure: " 'face 'message-header-name))
do (elfeed-insert-link (car enclosure))
do (insert "\n"))
(insert "\n")
(if content
(if (eq type 'html)
(elfeed-insert-html content base)
(insert content))
(insert (propertize "(empty)\n" 'face 'italic)))
(goto-char (point-min))))
(defun elfeed-show-entry (entry)
"Display ENTRY in the current buffer."
(let ((title (elfeed-entry-title entry)))
(switch-to-buffer (get-buffer-create (format "*elfeed %s*" title)))
(unless (eq major-mode 'elfeed-show-mode)
(elfeed-show-mode))
(setq elfeed-show-entry entry)
(elfeed-show-refresh)))
(defun elfeed-show-next ()
"Show the next item in the elfeed-search buffer."
(interactive)
(elfeed-kill-buffer)
(with-current-buffer (elfeed-search-buffer)
(call-interactively #'elfeed-search-show-entry)))
(defun elfeed-show-prev ()
"Show the previous item in the elfeed-search buffer."
(interactive)
(elfeed-kill-buffer)
(with-current-buffer (elfeed-search-buffer)
(forward-line -2)
(call-interactively #'elfeed-search-show-entry)))
(defun elfeed-show-visit ()
"Visit the current entry in the browser."
(interactive)
(let ((link (elfeed-entry-link elfeed-show-entry)))
(when link
(message "Sent to browser: %s" link)
(browse-url link))))
(defun elfeed-show-yank ()
"Copy the current entry link URL to the clipboard."
(interactive)
(let ((link (elfeed-entry-link elfeed-show-entry)))
(when link
(x-set-selection 'PRIMARY link)
(message "Yanked: %s" link))))
(defun elfeed-show-tag (&rest tags)
"Add TAGS to the displayed entry."
(let ((entry elfeed-show-entry))
(apply #'elfeed-tag entry tags)
(with-current-buffer (elfeed-search-buffer)
(elfeed-search-update-entry entry))))
(provide 'elfeed-show)
;; Local Variables:
;; byte-compile-warnings: (not cl-functions)
;; End:
;;; elfeed-show.el ends here