-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompany-64tass.el
71 lines (60 loc) · 2.82 KB
/
company-64tass.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
(require 'company)
(defvar 64tass-label-completions '()
"Variable containing a list of known labels/symbols")
(defun company-64tass-create-temp-file (file-name)
"Writes the current buffer to a temporary file in location provided by built-in `make-temp-file'"
(let ((tmp-file (expand-file-name (file-name-nondirectory file-name) (make-temp-file "_64tass" 'directory))))
(write-region nil nil tmp-file nil 0)
tmp-file))
(defun company-64tass-export-labels (&optional file-name)
"Exports all label symbols using the 64tass binary, passing the
location/directory as include path"
(interactive)
(let ((src-file (or file-name buffer-file-name)))
(let ((include-dir (file-name-directory buffer-file-name))
(labels-file (concat (file-name-directory file-name) (file-name-base src-file) ".labels")))
(call-process "64tass" nil nil nil
"--no-output"
(or file-name buffer-file-name)
"-I" include-dir
"-l" labels-file)
labels-file)))
(defun company-64tass-read-labels-file (file-name)
"Returns the contents of the file as string"
(if (file-exists-p file-name)
(with-temp-buffer
(insert-file-contents file-name)
(buffer-string))
""))
(defun company-64tass-parse-labels-file (contents)
"parse contents of labels file to a list of string label names"
(butlast (map 'list (lambda (row)
(car (split-string row "[[:blank:]=]+")))
(split-string contents "\n"))))
(defun company-64tass-after-change (beg end len)
"Dumps all labels and symbols of current file and any included files to `64tass-label-completions'"
(save-match-data
(if (and buffer-file-name
(> len 0))
(let ((tmp-file (company-64tass-create-temp-file buffer-file-name)))
(set '64tass-label-completions
(let ((parsed-labels (company-64tass-parse-labels-file
(company-64tass-read-labels-file
(company-64tass-export-labels tmp-file)))))
(or parsed-labels 64tass-label-completions)))
(delete-directory (file-name-directory tmp-file) t)))))
(defun 64tass-company-backend (command &optional arg &rest ignored)
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend '64tass-company-backend))
(prefix (and (eq major-mode 'paw64-mode)
(company-grab-symbol)))
(candidates
(cl-remove-if-not
(lambda (c) (string-prefix-p arg c))
64tass-label-completions))))
(defun company-64tass-setup ()
"Set up company-64tass and enable `company-mode' in the current buffer"
(add-hook 'after-change-functions 'company-64tass-after-change)
(setq-local company-backends '(64tass-company-backend))
(company-mode))