aboutsummaryrefslogtreecommitdiffstats
path: root/oni/home/services/emacs/oni-dashboard.el
blob: 85a5608236dde150a25df2cd8d8f0d02011265d6 (plain)
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
;;; oni-dashboard.el --- Customization for the dashboard module  -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Tom Willemse

;; Author: Tom Willemse <tom@ryuslash.org>
;; Keywords: local

;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(defun oni-dashboard-insert-random-note (_)
  "Insert a link to a random note from my Roam database."
  (dashboard-insert-heading "Today's Note:" "n")
  (insert "\n    ")
  (dashboard-insert-shortcut 'random-note "n" "Today's Note:")
  (let ((element (seq-random-elt (org-roam-node-read--completions))))
    (widget-create 'link
                   :notify (lambda (&rest _)
                             (find-file (org-roam-node-file (cdr element)))
                             (goto-char (org-roam-node-point (cdr element))))
                   :button-prefix ""
                   :button-suffix ""
                   (string-trim-right (car element)))))

(defun oni-dashboard-insert-random-wiki-page (_)
  "Insert a link to a random Emacs Wiki page."
  (dashboard-insert-heading "Today's Wiki page:" "w")
  (insert "\n    ")
  (dashboard-insert-shortcut 'random-wiki-page "w" "Today's Wiki page:")
  (let ((text-beginning-marker (point-marker)))
    (insert "Loading...")
    (url-retrieve "https://www.emacswiki.org/emacs?action=random"
                  (lambda (&rest _args)
                    (let ((info (prog1 (cons (url-recreate-url url-current-object)
                                             (progn
                                               (search-forward "<title>")
                                               (let ((beginning (point)))
                                                 (search-forward "</title>")
                                                 (forward-char -8)
                                                 (buffer-substring-no-properties beginning (point)))))
                                  (kill-buffer))))
                      (with-current-buffer (marker-buffer text-beginning-marker)
                        (let ((inhibit-read-only t))
                          (save-excursion
                            (goto-char (marker-position text-beginning-marker))
                            (delete-char 10)
                            (widget-create 'url-link
                                           :format (format "%%[%s%%]" (cdr info))
                                           :button-suffix ""
                                           :button-prefix ""
                                           (car info)))))))
                  nil
                  t)))

(add-to-list 'dashboard-item-generators '(random-wiki-page . oni-dashboard-insert-random-wiki-page))
(add-to-list 'dashboard-items '(random-wiki-page))
(require 'org-roam)
(add-to-list 'dashboard-item-generators '(random-note . oni-dashboard-insert-random-note))
(add-to-list 'dashboard-items '(random-note))

(provide 'oni-dashboard)
;;; oni-dashboard.el ends here