Add ErcBar, also restore channel buffers from logs
[~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 '(crux writeroom-mode znc)
21 "The list of Lisp packages required by the aminb layer.")
22
23 (defun aminb/init-crux ()
24 (use-package crux
25 :defer t
26 :bind (("C-c d" . crux-duplicate-current-line-or-region)
27 ("C-c M-d" . crux-duplicate-and-comment-current-line-or-region)
28 )))
29
30 (defun aminb/init-writeroom-mode ()
31 (use-package writeroom-mode ; Distraction-free editing
32 :defer t
33 :config (setq writeroom-width 82)
34 :bind (("C-c W" . writeroom-mode)
35 ("s-?" . writeroom-toggle-mode-line))))
36
37 (defun aminb/init-znc ()
38 (use-package znc
39 :defer t
40 :init
41 (spacemacs/set-leader-keys
42 "aiz" 'znc-erc)
43 :config
44 (progn
45 ;; Set the erc nick completion postfix to ", "
46 (setq erc-pcomplete-nick-postfix ", ")
47
48 ;; Restore channel buffers from logs
49 (setq erc-log-insert-log-on-open t)
50
51 (defun vbe:znc-add-server (server port user networks)
52 "Add a server to the list of ZNC servers.
53 We use SSL inconditionaly. Moreover, we don't store the password
54 but put nil instead. At least, we tweak the username to contain
55 the network name later, this will be separated again."
56 (add-to-list 'znc-servers
57 (list server
58 port
59 t ; SSL enabled
60 (mapcar (function (lambda (slug) (list slug
61 (format "%s/%s" user slug)
62 nil)))
63 networks))))
64
65 (defun vbe:znc-erc-ssl-connector (&rest R)
66 "Connect to ERC using SSL and retrieve password with `auth-source-search'.
67 Moreover, handle multiple networks by sending the password with
68 the appropriate network slug that we extract from the nick."
69 (let* ((user (nth 0 (split-string (plist-get R :nick) "/")))
70 (slug (nth 1 (split-string (plist-get R :nick) "/")))
71 (found (nth 0 (auth-source-search :host (plist-get R :server)
72 :user user
73 :require '(:user :secret)
74 :max 1))))
75 (if found
76 (let ((password (let ((secret (plist-get found :secret)))
77 (if (functionp secret)
78 (funcall secret)
79 secret))))
80 (plist-put R :password (format "%s/%s:%s" user slug password))
81 (plist-put R :nick user)
82 (apply 'erc-tls R)))))
83 (setq znc-erc-ssl-connector 'vbe:znc-erc-ssl-connector)
84
85 ;; Define networks
86 (vbe:znc-add-server "nix.aminb.org" 6669 "amin"
87 '(freenode mozilla))
88
89 ;; https://www.emacswiki.org/emacs/ErcBar
90 ;; Display a bar before unread messages
91 (eval-after-load 'erc-track
92 '(progn
93 (defun erc-bar-move-back (n)
94 "Moves back n message lines. Ignores wrapping, and server messages."
95 (interactive "nHow many lines ? ")
96 (re-search-backward "^.*<.*>" nil t n))
97
98 (defun erc-bar-update-overlay ()
99 "Update the overlay for current buffer, based on the content of
100 erc-modified-channels-alist. Should be executed on window change."
101 (interactive)
102 (let* ((info (assq (current-buffer) erc-modified-channels-alist))
103 (count (cadr info)))
104 (if (and info (> count erc-bar-threshold))
105 (save-excursion
106 (end-of-buffer)
107 (when (erc-bar-move-back count)
108 (let ((inhibit-field-text-motion t))
109 (move-overlay erc-bar-overlay
110 (line-beginning-position)
111 (line-end-position)
112 (current-buffer)))))
113 (delete-overlay erc-bar-overlay))))
114
115 (defvar erc-bar-threshold 1
116 "Display bar when there are more than erc-bar-threshold unread messages.")
117 (defvar erc-bar-overlay nil
118 "Overlay used to set bar")
119 (setq erc-bar-overlay (make-overlay 0 0))
120 (overlay-put erc-bar-overlay 'face '(:underline "purple"))
121 ;;put the hook before erc-modified-channels-update
122 (defadvice erc-track-mode (after erc-bar-setup-hook
123 (&rest args) activate)
124 ;;remove and add, so we know it's in the first place
125 (remove-hook 'window-configuration-change-hook 'erc-bar-update-overlay)
126 (add-hook 'window-configuration-change-hook 'erc-bar-update-overlay))
127 (add-hook 'erc-send-completed-hook (lambda (str)
128 (erc-bar-update-overlay)))))
129
130 )))
131
132 ;;; packages.el ends here