-
Notifications
You must be signed in to change notification settings - Fork 2
/
anybar.el
153 lines (132 loc) · 4.38 KB
/
anybar.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
;;; anybar.el --- Control AnyBar from Emacs
;; Copyright (c) 2016 Christopher Shea
;; Author: Christopher Shea <cmshea@gmail.com>
;; Version: 0.1.1
;; Keywords: anybar
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at https://mozilla.org/MPL/2.0/.
;;; Commentary:
;; AnyBar is an application that puts an indicator in the menubar in
;; OS X. This package lets you interact with that indicator from
;; Emacs. See: https://github.com/tonsky/AnyBar
;; Basic usage:
;;
;; (require 'anybar)
;;
;; Start AnyBar:
;;
;; (anybar-start)
;;
;; Set indicator to a color:
;;
;; (anybar-set "red")
;;
;; Quit AnyBar:
;;
;; (anybar-quit)
;;
;; Those functions also take an optional argument to specify a port
;; number, if you want to run multiple instances or use a different
;; port than AnyBar's default, 1738.
;;
;; `anybar-set' will complain if you try to set the indicator to an
;; invalid style, which is anything outside of the default styles (see
;; `anybar-styles') or any custom images set in "~/.AnyBar". To
;; refresh the list of images anybar.el knows about, call
;; `anybar-images-reset'.
;;
;; These functions may be called interactively.
;;
;; If you have installed AnyBar to a location other than
;; /Applications/AnyBar.app, you'll need to customize
;; `anybar-executable-location' so that `anybar-start' may succeed.
;;
;; Enjoy!
;;; Code:
(defgroup anybar nil
"Control AnyBar from Emacs"
:group 'external
:link '(url-link "https://github.com/tie-rack/anybar-el"))
(defcustom anybar-executable-location
"/Applications/AnyBar.app"
"The location of the AnyBar.app"
:type 'string
:safe #'stringp)
(defconst anybar-default-port
1738
"The default port AnyBar runs on.")
(defconst anybar-styles
(list "white"
"red"
"orange"
"yellow"
"green"
"cyan"
"blue"
"purple"
"black"
"question"
"exclamation")
"The built-in styles for AnyBar.")
(defvar anybar-images nil
"Images available to set as the AnyBar style.")
(defun anybar-images-reset ()
"Sets anybar-images to a list of images available for AnyBar."
(interactive)
(setq anybar-images
(and (file-directory-p "~/.AnyBar")
(mapcar
(lambda (filename)
(save-match-data
(and (string-match "\\(.*?\\)\\(_alt\\)?\\(@2x\\)?.png$" filename)
(match-string 1 filename))))
(directory-files "~/.AnyBar" nil "\.png$"))))
(delete-dups anybar-images))
(anybar-images-reset)
(defun anybar--read-style ()
(completing-read "Style: "
(append anybar-styles anybar-images)))
(defun anybar--read-port ()
(read-number "Port: " anybar-default-port))
;;;###autoload
(defun anybar-send (command &optional port)
"Sends the command to the AnyBar instance running on port."
(interactive (list (read-string "Command: ")
(anybar--read-port)))
(let* ((port (or port anybar-default-port))
(conn (make-network-process
:name "anybar"
:type 'datagram
:host 'local
:service port)))
(process-send-string conn command)
(delete-process conn)))
;;;###autoload
(defun anybar-set (style &optional port)
"Sets the AnyBar running on the specified port to style. Will
warn if the style is not valid."
(interactive (list (anybar--read-style)
(anybar--read-port)))
(let ((port (or port anybar-default-port))
(available-styles (append anybar-styles anybar-images)))
(if (member style available-styles)
(anybar-send style port)
(display-warning "AnyBar" (format "Not a style: %s" style)))))
;;;###autoload
(defun anybar-quit (&optional port)
"Quit the AnyBar instance running on the specified port."
(interactive (list (anybar--read-port)))
(let ((port (or port anybar-default-port)))
(anybar-send "quit" port)))
;;;###autoload
(defun anybar-start (&optional port)
"Start an instance of AnyBar on the specified port."
(interactive (list (anybar--read-port)))
(let* ((port (or port anybar-default-port))
(command (format "ANYBAR_PORT=%d open -n %s"
port
anybar-executable-location)))
(shell-command command)))
(provide 'anybar)
;;; anybar.el ends here