-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircuitpython-mode.el
More file actions
162 lines (127 loc) · 5.81 KB
/
circuitpython-mode.el
File metadata and controls
162 lines (127 loc) · 5.81 KB
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
;;; circuitpython-mode.el --- Minor mode for CircuitPython -*- lexical-binding: t; -*-
;; Author: Lee Nelson <lnelson@nelnet.org>
;; Package-Requires: ((emacs "24.1"))
;; Package-Version: 0.6.5
;; Keywords: languages, tools
;; URL: https://github.com/nelnet-team/circuitpython-mode
;; SPDX-License-Identifier: MIT
;;; Commentary:
;; 1 circuitpython-mode.el
;; ═══════════════════════
;; 1.1 This is in early development.
;; ─────────────────────────────────
;; Only working code will be committed to MELPA Stable, but the current
;; functionality is limited
;; 1.2 Description
;; ───────────────
;; This package provides a minor mode called `circuitpython-mode'. This
;; mode is intended to help with the workflow in circuitpython
;; development. It is invoked by typing:
;; ┌────
;; │ M-x circuitpython-mode
;; └────
;; 1.3 Usage
;; ─────────
;; 1.3.1 Compile (copy file to board)
;; ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
;; A function, `circuitpython-compile-copy', is added that is invoked
;; each time the buffer is saved (after-save-hook). When invoked, the new
;; command will update the compile-command to be a shell command that
;; will copy the current file to a different directory (ie the board).
;; The destination directory needs to be defined as a *file-local* or
;; *dir-local* variable. After each file save (**[C-x C-s]**, **[C-x
;; w]**, etc), the default compile-command will be something like:
;; ┌────
;; │ cp filename.py /some/path/some/where/
;; └────
;; This does not modify the behavior of `compile' itself, it only changes
;; the command that will presented as the default compile command when
;; invoking `compile' (usually **[C-c c]** )
;; 1.3.2 Compile .mpy
;; ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
;; Additionally, a new command, `circuitpython-mpy-compile', is added and
;; bound to **[C-c !]**. If the variable `mpy-compiler' is defined (ie
;; as file-local or dir-local), then that command will be used.
;; Otherwise the command "mpy-cross" is used. One potential reason to
;; specify the mpy-compiler is if it is not in $PATH. This will define
;; the compile-command to be something like:
;; ┌────
;; │ mpy-cross filename.py
;; └────
;; There is also a command, `circuitpython-set-mpy-compiler', bound to
;; **[C-c @]** that allows the user to override the process described
;; above and provide an alternative command.
;; After the mpy compile command is defined, `compile' is called, after
;; which `circuitpython-compile-copy' (see above) is called. This means
;; that after compiling an .mpy, the compile command will revert back to
;; being the command to copy the file to the board.
;; 1.4 Example directory local variables
;; ─────────────────────────────────────
;; `.dir-locals.el'
;; ┌────
;; │ ((nil . ((circuitpython-copy-path . "/mnt/chromeos/removable/CIRCPY/"))))
;; └────
;;; Code:
(defvar circuitpython-current-mpy-compiler "mpy-cross")
(make-variable-buffer-local 'compile-command)
(make-local-variable 'circuitpython-copy-path)
(defun circuitpython-compile-copy ()
"Set up 'compile-command' to copy script to board.
This should set 'compile-command' to something like:
cp somefile.py /mnt/foo/bar/CIRCPY/"
(set 'compile-command
(concat
"cp "
(file-name-nondirectory (buffer-file-name (current-buffer)))
" "
circuitpython-copy-path)))
(defun circuitpython-set-mpy-compiler (new-command)
"Allow the user to interactively set the mpy compiler.
This will provide the existing value of 'circuitpython-current-mpy-compiler'
as a suggestion. NEW-COMMAND is the actual compile command that will be set"
(interactive (list
(read-string
(format "New mpy compile command (%s): "
(symbol-value 'circuitpython-current-mpy-compiler))
nil nil
(symbol-value 'circuitpython-current-mpy-compiler))))
(setq circuitpython-current-mpy-compiler new-command)
(message "mpy compile command set to %s" circuitpython-current-mpy-compiler))
(defun circuitpython-get-mpy-compiler ()
"Return value for the mpy compiler.
If the variable is already defined (maybe file-local
or dir-local) then return that value.
Otherwise, return the value 'mpy-cross'"
(if (boundp 'mpy-compiler)
(symbol-value 'mpy-compiler)
(symbol-value 'circuitpython-current-mpy-compiler)))
(defun circuitpython-mpy-compile ()
"Alternate compile for circuitpython.
Sets 'compile-command' to use mpy-cross then calls compile
after which it calls 'circuitpython-compile-copy' to restore
the original form. This should set 'compile-command' to be
Something like:
mpy-cross filename.py"
(interactive)
(set 'compile-command
(concat
(circuitpython-get-mpy-compiler)
" "
(file-name-nondirectory (buffer-file-name (current-buffer)))))
(compile compile-command)
(circuitpython-compile-copy))
(define-minor-mode circuitpython-mode
"Minor mode for CircuitPython"
:lighter " circpy"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c !") 'circuitpython-mpy-compile)
(define-key map (kbd "C-c #") 'circuitpython-set-mpy-compiler)
map))
;; If the variable circuitpython-copy-path is defined
;; (usually as a file-local or dir-local variable), then
;; add a hook to set the compile command after each save
(if (boundp 'circuitpython-copy-path)
(add-hook 'after-save-hook #'circuitpython-compile-copy)
nil)
(provide 'circuitpython-mode)
;;; circuitpython-mode.el ends here