-
Notifications
You must be signed in to change notification settings - Fork 2
/
dilbert.el
213 lines (160 loc) · 5.66 KB
/
dilbert.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
;;; dilbert.el --- View Dilbert comics -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Daniils Petrovs
;; Author: Daniils Petrovs <thedanpetrov@gmail.com>
;; URL: https://github.com/DaniruKun/dilbert-el
;; Version: 0.2
;; Package-Requires: ((emacs "26.1") (enlive "0.0.1") (dash "2.19.1"))
;; Keywords: multimedia news
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Read Dilbert comics from the comfort of Emacs.
;;;; Installation
;;;;; MELPA
;; If you installed from MELPA, you're done.
;;;;; Manual
;; Install these required packages:
;; + enlive
;; + dash
;; Then put this file in your load-path, and put this in your init
;; file:
;; (require 'dilbert)
;;;; Usage
;; Run one of these commands:
;; `dilbert': Get the latest Dilbert comic strip.
;;;; Tips
;; + You can customize settings in the `dilbert' group.
;;;; Credits
;; This package would not have been possible without the following
;; packages: xkcd[1].
;;
;; [1] https://github.com/vibhavp/emacs-xkcd
;;; Code:
;;;; Requirements
(require 'url)
(require 'image)
(require 'enlive)
(require 'dash)
(require 'browse-url)
(define-derived-mode dilbert-mode special-mode "dilbert"
"Major mode for viewing Dilbert (https://dilbert.com) comics."
:group 'dilbert)
;;;; Customization
(defgroup dilbert nil
"Settings for `dilbert'."
:group 'multimedia
:link '(url-link "https://github.com/DaniruKun/dilbert-el"))
(defcustom dilbert-cache-dir (let ((dir (concat user-emacs-directory "dilbert/")))
(make-directory dir :parents)
dir)
"Directory for caching comics and other files."
:group 'dilbert
:type 'directory)
(defcustom dilbert-cache-latest (concat dilbert-cache-dir "latest")
"File to store the latest cached dilbert number in.
Should preferably be located in `dilbert-cache-dir'."
:group 'dilbert
:type 'file)
;;;; Variables
(defconst dilbert-home-url "https://dilbert.com")
;;;; Structs
(defstruct dilbert-comic
"Defines a single Dilbert comic strip entity to
be used for presentation."
title url)
;;;;; Keymaps
(defvar dilbert-map
;; This makes it easy and much less verbose to define keys
(let ((map (make-sparse-keymap "dilbert map"))
(maps (list
;; Mappings go here, e.g.:
"q" #'dilbert-kill-buffer)))
(cl-loop for (key fn) on maps by #'cddr
do (progn
(when (stringp key)
(setq key (kbd key)))
(define-key map key fn)))
map))
;;;; Commands
;;;###autoload
(defun dilbert-view-latest ()
"View the latest Dilbert comic strip."
(interactive)
(get-buffer-create "*dilbert*")
(switch-to-buffer "*dilbert*")
(dilbert-prep-buffer)
(let (buffer-read-only)
(erase-buffer)
(let* ((comic (dilbert-latest-comic))
(url (dilbert-comic-url comic))
(title (dilbert-comic-title comic))
(file (dilbert-download url)))
(message "Getting comic...")
(center-line)
(insert "\n")
(dilbert-insert-image file)
(message "%s" title))))
;;;###autoload
(defalias 'dilbert #'dilbert-view-latest)
;;;; Functions
;;;; Private
(defun dilbert-fetch-homepage ()
"Fetch the Dilbert homepage containing the latest comic strip."
(enlive-fetch dilbert-home-url))
(defun dilbert-latest-comic ()
"Returns the latest Dilbert comic."
(->> (dilbert-latest-comics) (first)))
(defun dilbert-download (img-url)
"Download the comic strip image at IMG-URL.
If exists, just return the full img path."
(let* ((img-hash (-> img-url (split-string "/") (last) (first)))
(file-name (format "%s%s.gif" dilbert-cache-dir img-hash)))
(if (file-exists-p file-name)
file-name
(url-copy-file img-url file-name))
file-name))
(defun dilbert-insert-image (file)
"Insert image described by FILE in buffer with the title-text.
If the image is a gif, animate it."
(let ((image (create-image file 'gif))
(start (point)))
(insert-image image)
(if (or
(and (fboundp 'image-multi-frame-p)
(image-multi-frame-p image))
(and (fboundp 'image-animated-p)
(image-animated-p image)))
(image-animate image 0 t))
(add-text-properties start (point) '(help-echo "Alt"))))
(defun dilbert-kill-buffer ()
"Kill the dilbert buffer."
(interactive)
(kill-buffer "*dilbert*"))
(defun dilbert-prep-buffer ()
"Prepare the dilbert buffer for presentation by toggling modes."
(dilbert-mode)
(display-line-numbers-mode 0))
(defun dilbert-latest-comics ()
"Fetches and returns the latest comic strips on the homepage."
(let* ((homepage (dilbert-fetch-homepage))
(img-selector [img.img-comic])
(img->comic (lambda (img)
(let ((attrs (second img)))
(make-dilbert-comic
:title (->> attrs (assoc 'alt) (cdr))
:url (->> attrs (assoc 'src) (cdr)))))))
(->> img-selector
(enlive-query-all homepage)
(-map img->comic))))
;;;; Footer
(provide 'dilbert)
;;; dilbert.el ends here