-
Notifications
You must be signed in to change notification settings - Fork 2
/
nx-ace.lisp
143 lines (132 loc) · 4.71 KB
/
nx-ace.lisp
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
;;;; SPDX-FileCopyrightText: Atlas Engineer LLC
;;;; SPDX-License-Identifier: BSD-3-Clause
(in-package :nx-ace)
(define-mode ace-mode (editor-mode passthrough-mode)
"Mode for usage with the Ace editor."
((style
#+nyxt-2
(cl-css:css
'(("#editor"
:position "absolute"
:top "0"
:right "0"
:bottom "0"
:left "0")))
#+nyxt-3-pre-release-1
(theme:themed-css (theme *browser*)
("#editor"
:position "absolute"
:top "0"
:right "0"
:bottom "0"
:left "0"))
#-(or nyxt-2 nyxt-3-pre-release-1)
(theme:themed-css (theme *browser*)
`("#editor"
:position "absolute"
:top "0"
:right "0"
:bottom "0"
:left "0")))
(extensions
nil
:type list
:documentation "A list of links to scripts enhancing Ace, like keybindings or themes.
Example of the value:
'(\"https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/keybinding-emacs.min.js\"
\"https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/mode-c_cpp.min.js\"
\"https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/theme-tomorrow_night_eighties.min.js\"))")
(theme
nil
:type (maybe string)
:documentation "Name of the theme to enable by default when opening Ace.
Should be of the \"ace/theme/name\" format and should be loaded as part of `extensions', unless built into Ace.")
(keybindings
nil
:type (maybe string)
:documentation "Name of the keybinding mode to enable in Ace.
Should be of the \"ace/keyboard/name\" format and should be loaded as part of `extensions', unless built into Ace.")
(epilogue
nil
:type (maybe string)
:documentation "JavaScript code to run after setting the file contents.
Put your extension-specific configuration here.")
#+nyxt-2
(constructor
(lambda (mode)
(initialize-display mode)))))
#+nyxt-2
(defmethod initialize-display ((ace ace-mode))
(let* ((content (markup:markup
(:head (:style (style ace)))
(:body
(:div :id "editor" "")
(:script :src "https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/ace.min.js"
:type "text/javascript"
:charset "utf-8" "")
(:script (markup:raw (ps:ps (defparameter editor (ps:chain ace (edit "editor")))))))))
(insert-content (ps:ps (ps:chain document
(write (ps:lisp content))))))
(ffi-buffer-evaluate-javascript-async (buffer ace) insert-content)))
#+(or nyxt-3 nyxt-4)
(defmethod markup
((ace ace-mode))
(spinneret:with-html-string
(:head
(:style (style ace)))
(:body
(:script
:src "https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/ace.min.js"
:integrity "sha512-U2JKYiHG3ixOjmdycNbi4Xur8q4Nv73CscCGEopBeiVyzDR5ErC6jmHNr0pOB8CUVWb0aQXLgL0wYXhoMU6iqw=="
:crossorigin "anonymous"
:type "text/javascript"
:charset "utf-8"
"")
(dolist (ext (extensions ace))
(:script
:src (quri:render-uri (quri:uri ext))
:crossorigin "anonymous"
:type "text/javascript"
:charset "utf-8"
""))
(:div :id "editor" "")
(:script
(:raw
(ps:ps
(defparameter editor (ps:chain ace (edit "editor")))
(when (ps:lisp (theme ace))
(ps:chain editor (set-theme (ps:lisp (theme ace)))))
(ps:chain editor (set-keyboard-handler
(ps:@ (require (ps:lisp (keybindings ace))) handler))))))
(:script
(:raw (epilogue ace))))))
(defmethod set-content
((ace ace-mode) content)
#+(or nyxt-3 nyxt-4)
(ps-eval :buffer (buffer ace)
(ps:chain editor session (set-value (ps:lisp content))))
#+nyxt-2
(with-current-buffer (buffer ace)
(pflet ((set-content (content)
(ps:chain editor session (set-value (ps:lisp content)))))
(set-content content))))
(defmethod get-content ((ace ace-mode))
#+(or nyxt-3 nyxt-4)
(ps-eval :buffer (buffer ace) (ps:chain editor (get-value)))
#+nyxt-2
(with-current-buffer (buffer ace)
(pflet ((get-content ()
(ps:chain editor (get-value))))
(get-content))))
(defmethod set-option ((ace ace-mode) option value)
#+(or nyxt-3 nyxt-4)
(ps-eval :buffer (buffer ace)
(ps:chain editor (set-option (ps:lisp option) (ps:lisp value))))
#+nyxt-2
(with-current-buffer (buffer ace)
(pflet ((set-option (option value)
(ps:chain editor (set-option (ps:lisp option) (ps:lisp value)))))
(set-option option value))))
(defun options ()
"Get the list of option names currently present in the editor."
(alex:hash-table-keys (ps-eval (ps:chain editor (get-options)))))