8e6e1ec40ebddddc080de30ccddadd63c2ae0cdb
[~bandali/configs] / spacemacs / .emacs.d / private / aminb / packages.el
1 ;;; packages.el --- aminb layer packages file for Spacemacs.
2 ;;
3 ;; Copyright (c) 2016 Amin Bandali
4 ;;
5 ;; Author: Amin Bandali <amin@aminb.org>
6 ;; URL: https://github.com/aminb/dotfiles
7 ;;
8 ;; This file is not part of GNU Emacs.
9 ;;
10 ;;; License: GPLv3
11
12 ;;; Commentary:
13
14 ;; This file is a collection of my settings and customizations on top of
15 ;; spacemacs.
16
17 ;;; Code:
18
19 (defconst aminb-packages
20 '(creamsody-theme
21 crux
22 ;; mu4e has to be installed manually,
23 ;; and make sure it's in load-path
24 (mu4e :location site)
25 (mu4e-contrib :location site)
26 smtpmail
27 writeroom-mode
28 znc)
29 "The list of Lisp packages required by the aminb layer.")
30
31 (defun aminb/init-creamsody-theme ())
32
33 (defun aminb/init-crux ()
34 (use-package crux
35 :defer t
36 :bind (("C-c d" . crux-duplicate-current-line-or-region)
37 ("C-c M-d" . crux-duplicate-and-comment-current-line-or-region)
38 )))
39
40 (defun aminb/init-writeroom-mode ()
41 (use-package writeroom-mode ; Distraction-free editing
42 :defer t
43 :config (setq writeroom-width 82)
44 :bind (("C-c W" . writeroom-mode)
45 ("s-?" . writeroom-toggle-mode-line))))
46
47 (defun aminb/init-znc ()
48 (use-package znc
49 :defer t
50 :init
51 (spacemacs/set-leader-keys
52 "aiz" 'znc-erc)
53 :config
54 (progn
55 ;; Set the erc nick completion postfix to ", "
56 (setq erc-pcomplete-nick-postfix ", ")
57
58 ;; Restore channel buffers from logs
59 (setq erc-log-insert-log-on-open t)
60
61 (defun vbe:znc-add-server (server port user networks)
62 "Add a server to the list of ZNC servers.
63 We use SSL inconditionaly. Moreover, we don't store the password
64 but put nil instead. At least, we tweak the username to contain
65 the network name later, this will be separated again."
66 (add-to-list 'znc-servers
67 (list server
68 port
69 t ; SSL enabled
70 (mapcar (function (lambda (slug) (list slug
71 (format "%s/%s" user slug)
72 nil)))
73 networks))))
74
75 (defun vbe:znc-erc-ssl-connector (&rest R)
76 "Connect to ERC using SSL and retrieve password with `auth-source-search'.
77 Moreover, handle multiple networks by sending the password with
78 the appropriate network slug that we extract from the nick."
79 (let* ((user (nth 0 (split-string (plist-get R :nick) "/")))
80 (slug (nth 1 (split-string (plist-get R :nick) "/")))
81 (found (nth 0 (auth-source-search :host (plist-get R :server)
82 :user user
83 :require '(:user :secret)
84 :max 1))))
85 (if found
86 (let ((password (let ((secret (plist-get found :secret)))
87 (if (functionp secret)
88 (funcall secret)
89 secret))))
90 (plist-put R :password (format "%s/%s:%s" user slug password))
91 (plist-put R :nick user)
92 (apply 'erc-tls R)))))
93 (setq znc-erc-ssl-connector 'vbe:znc-erc-ssl-connector)
94
95 ;; Define networks
96 (vbe:znc-add-server "nix.aminb.org" 6669 "amin"
97 '(freenode mozilla))
98
99 ;; https://www.emacswiki.org/emacs/ErcBar
100 ;; Display a bar before unread messages
101 (eval-after-load 'erc-track
102 '(progn
103 (defun erc-bar-move-back (n)
104 "Moves back n message lines. Ignores wrapping, and server messages."
105 (interactive "nHow many lines ? ")
106 (re-search-backward "^.*<.*>" nil t n))
107
108 (defun erc-bar-update-overlay ()
109 "Update the overlay for current buffer, based on the content of
110 erc-modified-channels-alist. Should be executed on window change."
111 (interactive)
112 (let* ((info (assq (current-buffer) erc-modified-channels-alist))
113 (count (cadr info)))
114 (if (and info (> count erc-bar-threshold))
115 (save-excursion
116 (end-of-buffer)
117 (when (erc-bar-move-back count)
118 (let ((inhibit-field-text-motion t))
119 (move-overlay erc-bar-overlay
120 (line-beginning-position)
121 (line-end-position)
122 (current-buffer)))))
123 (delete-overlay erc-bar-overlay))))
124
125 (defvar erc-bar-threshold 1
126 "Display bar when there are more than erc-bar-threshold unread messages.")
127 (defvar erc-bar-overlay nil
128 "Overlay used to set bar")
129 (setq erc-bar-overlay (make-overlay 0 0))
130 (overlay-put erc-bar-overlay 'face '(:underline "purple"))
131 ;;put the hook before erc-modified-channels-update
132 (defadvice erc-track-mode (after erc-bar-setup-hook
133 (&rest args) activate)
134 ;;remove and add, so we know it's in the first place
135 (remove-hook 'window-configuration-change-hook 'erc-bar-update-overlay)
136 (add-hook 'window-configuration-change-hook 'erc-bar-update-overlay))
137 (add-hook 'erc-send-completed-hook (lambda (str)
138 (erc-bar-update-overlay)))))
139
140 )))
141
142 (defun aminb/post-init-mu4e ()
143 (setq maildir "~/mail")
144 (use-package mu4e
145 :defer t
146 :config
147 (progn
148 (setq mu4e-maildir maildir
149 mu4e-get-mail-command "mbsync -a"
150 mu4e-update-interval nil
151 mu4e-view-show-addresses t
152 mu4e-headers-include-related t
153 mu4e-enable-notifications t
154 mu4e-enable-mode-line t
155 mu4e-compose-signature-auto-include t
156 mu4e-compose-signature
157 (concat
158 "Amin Bandali\n"
159 "<aminb.org>\n")
160 message-kill-buffer-on-exit t ; don't keep message buffers around
161 mu4e-attachment-dir "~/dls"
162 mu4e-sent-folder "/amin/Sent"
163 mu4e-drafts-folder "/amin/Drafts"
164 mu4e-trash-folder "/amin/Trash"
165 user-full-name "Amin Bandali"
166 user-mail-address "amin@aminb.org"
167 mu4e-context-policy 'pick-first
168 mu4e-contexts
169 (list (make-mu4e-context
170 :name "Personal"
171 :enter-func (lambda () (mu4e-message "Switch to the Personal context"))
172 :match-func (lambda (msg)
173 (when msg
174 (s-prefix? "/amin/" (mu4e-message-field msg :maildir))))
175 :vars '((user-mail-address . "amin@aminb.org")
176 (mu4e-sent-folder . "/amin/Sent")
177 (mu4e-drafts-folder . "/amin/Drafts")
178 (mu4e-trash-folder . "/amin/Trash")
179 (mu4e-sent-messages-behavior . sent)
180 (smtpmail-default-smtp-server . "nix.aminb.org")
181 (smtpmail-smtp-server . "nix.aminb.org")
182 (smtpmail-stream-type . starttls)
183 (smtpmail-smtp-service . 587)))
184 (make-mu4e-context
185 :name "BB"
186 :enter-func (lambda () (mu4e-message "Switch to the BB context"))
187 :match-func (lambda (msg)
188 (when msg
189 (s-prefix? "/gmail/" (mu4e-message-field msg :maildir))))
190 :vars '((user-mail-address . "amin.bandali@gmail.com")
191 (mu4e-sent-folder . "/gmail/Sent")
192 (mu4e-drafts-folder . "/gmail/Drafts")
193 (mu4e-trash-folder . "/gmail/Trash")
194 (mu4e-sent-messages-behavior . delete)
195 (mu4e-compose-signature . nil)
196 (smtpmail-default-smtp-server . "smtp.gmail.com")
197 (smtpmail-smtp-server . "smtp.gmail.com")
198 (smtpmail-stream-type . starttls)
199 (smtpmail-smtp-service . 587)))
200 (make-mu4e-context
201 :name "GNU"
202 :enter-func (lambda () (mu4e-message "Switch to the GNU context"))
203 :match-func (lambda (msg)
204 (when msg
205 (s-prefix? "/gnu/" (mu4e-message-field msg :maildir))))
206 :vars '((user-mail-address . "aminb@gnu.org")
207 (mu4e-sent-folder . "/gnu/Sent")
208 (mu4e-drafts-folder . "/gnu/Drafts")
209 (mu4e-trash-folder . "/gnu/Trash")
210 (mu4e-get-mail-command . "getmail")
211 (mu4e-sent-messages-behavior . sent)
212 (smtpmail-default-smtp-server . "fencepost.gnu.org")
213 (smtpmail-smtp-server . "fencepost.gnu.org")
214 (smtpmail-stream-type . starttls)
215 (smtpmail-smtp-service . 587)))))
216 (with-eval-after-load 'mu4e-alert
217 ;; Enable Desktop notifications
218 (mu4e-alert-set-default-style 'notifications))))
219
220 (use-package gnus-dired
221 ;; A special version of the gnus-dired-mail-buffers function
222 ;; that understands mu4e buffers as well
223 :defer t
224 :config
225 (progn
226 ;; make the `gnus-dired-mail-buffers' function also work on
227 ;; message-mode derived modes, such as mu4e-compose-mode
228 (defun gnus-dired-mail-buffers ()
229 "Return a list of active message buffers."
230 (let (buffers)
231 (save-current-buffer
232 (dolist (buffer (buffer-list t))
233 (set-buffer buffer)
234 (when (and (derived-mode-p 'message-mode)
235 (null message-sent-message-via))
236 (push (buffer-name buffer) buffers))))
237 (nreverse buffers)))
238 (setq gnus-dired-mail-mode 'mu4e-user-agent)
239 (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode))
240 )
241
242 ;; Exclude the files in maildir from recents
243 (use-package recentf
244 :defer t
245 :config
246 (add-to-list 'recentf-exclude (expand-file-name maildir)))
247
248 (spacemacs/set-leader-keys
249 "am" 'mu4e)
250 )
251
252 (defun aminb/init-mu4e-contrib ()
253 (use-package mu4e-contrib
254 :defer t
255 :config
256 (setq mu4e-html2text-command 'mu4e-shr2text
257 mu4e-view-html-plaintext-ratio-heuristic 10
258 mu4e-view-prefer-html t)))
259
260 (defun aminb/init-smtpmail ()
261 (use-package smtpmail
262 :defer t
263 :config
264 (setq smtpmail-default-smtp-server "nix.aminb.org"
265 smtpmail-local-domain "aminb.org"
266 smtpmail-smtp-server "nix.aminb.org"
267 smtpmail-stream-type 'starttls
268 smtpmail-smtp-service 587
269 send-mail-function 'smtpmail-send-it)))
270
271 ;;; packages.el ends here