-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwakatime-mode.el
139 lines (113 loc) · 4.36 KB
/
wakatime-mode.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
;;; wakatime-mode.el --- Automatic time tracking extension based on WakaTime
;; Copyright (C) 2013 Gabor Torok <[email protected]>
;; Author: Gabor Torok <[email protected]>
;; Keywords: calendar, comm
;; Version: 0.1
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Enable WakaTime for the current buffer by invoking
;; `wakatime-mode'. If you wish to activate it globally, use
;; `global-wakatime-mode'.
;; Set variable `wakatime-api-key' to your API key. Point
;; `wakatime-cli-path' to the absolute path of the CLI script
;; (wakatime-cli.py).
;;; Code:
(defconst wakatime-user-agent "wakatime-mode")
(defconst wakatime-error-codes '((api . 102)))
(defgroup wakatime nil
"Customizations for WakaTime"
:group 'convenience
:prefix "wakatime-")
(defcustom wakatime-api-key nil
"API key for WakaTime."
:type 'string
:group 'wakatime)
(defcustom wakatime-cli-path nil
"Path of CLI client for WakaTime."
:type 'string
:group 'wakatime)
(defcustom wakatime-python-bin "/usr/bin/python"
"Path of Python binary."
:type 'string
:group 'wakatime)
(defun wakatime-client-command (savep)
"Return client command executable and arguments.
Set SAVEP to non-nil for write action."
(format "%s %s --file %s %s --plugin %s --key %s --time %.2f"
wakatime-python-bin
wakatime-cli-path
(buffer-file-name (current-buffer))
(if savep "--write" "")
wakatime-user-agent
wakatime-api-key
(float-time)))
(defun wakatime-init ()
(if (or (not wakatime-api-key)
(string= "" wakatime-api-key))
(let ((api-key (read-string "WakaTime API key: ")))
(customize-save-variable 'wakatime-api-key api-key)))
(if (or (not wakatime-cli-path)
(not (file-exists-p wakatime-cli-path)))
(let ((cli-path (read-string "WakaTime CLI script path: ")))
(customize-save-variable 'wakatime-cli-path cli-path))))
(defun wakatime-call (command)
"Call WakaTime COMMAND."
(let ((p (start-process-shell-command "wakatime" "*WakaTime messages*" command)))
(while (process-live-p p))
(let ((exit-status (process-exit-status p)))
(cond
((= (cdr (assoc 'api wakatime-error-codes)) exit-status)
(progn
(global-wakatime-mode -1)
(error "An error occured while connecting to WakaTime, check your API key! WakaTime mode has been disabled.")))
((< 0 exit-status)
(progn
(global-wakatime-mode -1)
(error "Unexpected WakaTime error occured (code %s)! WakaTime mode has been disabled." exit-status)))))))
(defun wakatime-ping ()
"Send ping notice to WakaTime."
(wakatime-call (wakatime-client-command nil)))
(defun wakatime-save ()
"Send save notice to WakaTime."
(wakatime-call (wakatime-client-command t)))
(defun wakatime-turn-on ()
"Turn on WakaTime."
(wakatime-init)
(add-hook 'after-save-hook 'wakatime-save nil t)
(add-hook 'auto-save-hook 'wakatime-save nil t)
(add-hook 'first-change-hook 'wakatime-ping nil t))
(defun wakatime-turn-off ()
"Turn off WakaTime."
(remove-hook 'after-save-hook 'wakatime-save t)
(remove-hook 'auto-save-hook 'wakatime-save t)
(remove-hook 'first-change-hook 'wakatime-ping t))
;;;###autoload
(define-minor-mode wakatime-mode
"Toggle WakaTime (WakaTime mode).
With a prefix argument ARG, enable Whitespace mode if ARG is
positive, and disable it otherwise. If called from Lisp, enable
the mode if ARG is omitted or nil."
:lighter " waka"
:init-value nil
:global nil
:group 'wakatime
(cond
(noninteractive
(setq wakatime-mode nil))
(wakatime-mode
(wakatime-turn-on))
(t
(wakatime-turn-off))))
;;;###autoload
(define-globalized-minor-mode global-wakatime-mode wakatime-mode (lambda () (wakatime-mode 1)))
(provide 'wakatime-mode)
;;; wakatime-mode.el ends here