summaryrefslogtreecommitdiff
path: root/dashboard.el
blob: 663cf7aea652e5f21f3f3e1ab95f61cb401e8ef8 (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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
;;; dashboard.el --- My daashboard -*- lexical-binding: t; -*-

(defun dashboard-center-string (str)
  "Return a centered string."
  (let ((current-window-width (1- (window-width nil t)))
        (str-width (string-pixel-width str)))
    (cond
     ((display-graphic-p)
      (concat
       (propertize
        (string 32)
        'display
        (list
         'space
         :width
         (list (floor (- current-window-width str-width) 2))))
       str))
     (t
      (concat
       (make-string
        (round
         (- current-window-width str-width)
         (* 2 (string-pixel-width (string 32))))
        32)
       str)))))

;;;###autoload
(defvar dashboard-buffer-name "durand"
  "The name of the buffer for the dashboard.")

;; TODO: Change this ASCII art.

;;;###autoload
(defvar dashboard-cat-text
  (list
"                 IIIII              "
"                IIIIIII             "
"I=======||       IIIII              "
"I  =============I=====I=====I====== "
"I IIIIIII   -   I     I     I     I "
"I  IIIII   -I-========I=====I     I "
"I           --  I     I     I     I "
"I            \\\\\\I     I     I     I "
"I                  == I     I     I "
"I                 /  \\I     I     I "
"I                /  III     I     I "
"I                 \\\\==I     I     I "
"I                 \\   I     I     I "
"I                  ===I     I     I "
"I                 /  \\I     I     I "
"I                /  III     I     I "
"I                 \\\\==I     I     I "
"I                 III I     I     I "
"I              III   II     I     I "
"I               I  IIII     I     I "
"I               IIIIIII     I     I "
"I                  ===I     I     I "
"I                 //  I     I     I "
"I                =====I     I     I "
"I               //    I     I     I "
"\\\\               \\\\   \\\\    \\\\    \\\\"
   ;; "                                      "
   ;; "                       /\\             "
   ;; "                      /  \\            "
   ;; "                     /    =\\=         "
   ;; "                    /  -     \\=-----  "
   ;; "                   /  |.|          /  "
   ;; "                 \\\\    -   -      /   "
   ;; "                  \\\\      |.|    |    "
   ;; "                 / \\\\      -    /     "
   ;; "                /   \\\\         /      "
   ;; "   ||==========|     ========\\=|      "
   ;; "   ||==========| --            |      "
   ;; "               | ||     /====  |      "
   ;; "               == |    /     \\  \\     "
   ;; "                  |   |       \\  \\    "
   ;; "                   ====        ====   "
   )
  "A clumsy text representation of a cute cat.")

;;;###autoload
(defun dashboard (&optional force)
  "Create the dashboard buffer.
If FORCE is non-nil, re-gerenate the dashboard buffer."
  (interactive)
  (let ((dashboard-exists-p (get-buffer dashboard-buffer-name))
        (dashboard (get-buffer-create dashboard-buffer-name))
        (inhibit-read-only t))
    (cond
     ((and (not force) dashboard-exists-p)
      (switch-to-buffer dashboard))
     (t
      (with-current-buffer dashboard
        (erase-buffer)
        (cond
         ((display-graphic-p)
          (let ((image
                 (create-image
                  (expand-file-name
                   "बोधिचित्तं.png"
                   user-emacs-directory)
                  nil nil
                  :scale 0.5)))
            (insert
             (make-string 3 10)
             (dashboard-center-string
              (propertize
               " "
               'display image
               'rear-nonsticky '(display))))))
         ((let ((width
                 (with-temp-buffer 
                   (insert (mapconcat #'identity
                                      dashboard-cat-text
                                      (string 10)))
                   (car (buffer-text-pixel-size)))))
            (mapc
             (lambda (text)
               (insert
                (make-string
                 (floor
                  (- (window-width nil t)
                     (string-pixel-width (string 32))
                     width)
                  2)
                 32)
                text
                10))
             dashboard-cat-text))))
        (newline 5)
        (insert
         (dashboard-center-string
          (propertize
           "ॐ मणिपद्मे हूम्"
           'face (list :height 600))))
        (set-buffer-modified-p nil)
        (read-only-mode 1)
        (set
         'mode-line-format
         '("%e" (:eval (modeline-format-dashboard))))
        (set 'default-directory user-emacs-directory)
        (view-mode 1)
        (dashboard-mode))))
    dashboard))

(set 'initial-buffer-choice #'dashboard)

;;; Dedicated major mode

(define-derived-mode dashboard-mode nil "Dashboard"
  "Major mode for the dashboard buffer.
The main purpose is to provide a dedicated keymap to access
common functionalities more conveniently."
  (font-lock-mode -1)
  (setq buffer-undo-list t)
  (setq-local cursor-type nil))

(let ((m dashboard-mode-map))
  (define-key m (vector ?l) #'blist)
  (define-key m (vector ?L) #'rlist-list-registers)
  (define-key m (vector ?b) #'bookmark-jump)
  (define-key m (vector ?B) #'durand-jump-bookmark-new-tab)
  (define-key m (vector ?T) #'modeline-toggle)
  (define-key m (vector ?t) #'durand-view-timers-or-temps)
  (define-key m (vector ?o) #'durand-open-object)
  (define-key m (vector ?g) #'gnus)
  (define-key m (vector ?h) #'durand-ibuffer)
  (define-key m (vector ?f) #'find-file)
  (define-key m (vector ?F) #'find-file-other-tab)
  (define-key m (vector ?a) #'dashboard-org-agenda)
  (define-key m (vector ?c) #'dashboard-clear)
  (define-key m (vector ?C) #'dashboard-org-capture)
  (define-key m (vector ?p) #'project-switch-project)
  (define-key m (vector ?x) #'execute-extended-command))

;; load Org if not loaded already

(defmacro dashboard-preload (symbol file-to-load)
  "Define a dispatch function for the symbol."
  (list
   'defun (intern (format "dashboard-%s" (symbol-name symbol)))
   (list (intern "&rest") 'args)
   (format
    "A dispatch function for `%s'.
This function loads \"%s\" if `%s' is not already loaded, and
calls `%s' otherwise."
    (symbol-name symbol)
    file-to-load
    (symbol-name symbol)
    (symbol-name symbol))
   (list 'interactive (list #'list 'current-prefix-arg t))
   (list
    'cond
    (list (list 'not (list #'functionp `(quote ,symbol)))
          (list #'load-config file-to-load))
    (list (list #'funcall `(quote ,symbol) 'args)))))

(dashboard-preload org-agenda "org-conf.el")
(dashboard-preload org-capture "org-conf.el")
(dashboard-preload org-store-link "org-conf.el")

;; ibuffer clear

(defun dashboard-clear ()
  "Run `durand-ibuffer-clear' while in the dashboard."
  (interactive)
  (let ((inhibit-message t))
    (ibuffer)
    (durand-ibuffer-clear)))

;;; Modify killing buffers

;;;###autoload
(defun dont-kill-dashboard-fn ()
  "Don't kill the dashboard buffer."
  ;; The function message returns the message, so applying not will
  ;; produce nil as needed.
  ;; "Please don't kill the dashboard buffer"
  (not
   (cond
    ((eq (current-buffer) (get-buffer dashboard-buffer-name))
     (message "न निहन्याद्घाटीफलकनिधानकम्")))))

(add-hook 'kill-buffer-query-functions 'dont-kill-dashboard-fn)

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