-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgl.el
146 lines (119 loc) · 4.7 KB
/
gl.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
;;; gl.el --- OpenGL bindings for Emacs -*- lexical-binding: t; -*-
;; Copyright (C) 2020-2021 by Jin Xue
;;
;; Author: Jin Xue <[email protected]>
;; Version: 0.0.1
;; URL: https://github.com/Jimx-/emacs-gl
;; Keywords: graphics
;; Package-Requires: ((emacs "28.0"))
;; This file is not part of GNU Emacs.
;; This file 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 2, or (at your option)
;; any later version.
;; This file 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;;
;;; OpenGL bindings for Emacs Lisp. It enables drawing to Emacs buffers
;;; with OpenGL through a GLArea xwidget embedded in the buffers.
;;; Code:
(require 'xwidget)
(unless (require 'gl-module nil t)
(error "GL will not work unless `gl-module' is compiled!"))
(require 'gl-decl)
(defvar-local gl--xwidget nil
"Current GLArea xwidget.")
(defun gl--self-insert ()
"Send invoking key to GL module."
(interactive)
(when gl--xwidget
(let* ((modifiers (event-modifiers last-input-event))
(shift (memq 'shift modifiers))
(meta (memq 'meta modifiers))
(ctrl (memq 'control modifiers)))
(when-let ((key (key-description (vector (event-basic-type last-input-event)))))
(gl-send-key key shift meta ctrl)))))
(defun gl-send-key (key &optional shift meta ctrl)
"Send KEY to GL module with optional modifiers SHIFT, META and CTRL."
(when gl--xwidget
(let ((inhibit-redisplay t)
(inhibit-read-only t))
(when (and (not (symbolp last-input-event)) shift (not meta) (not ctrl))
(setq key (upcase key)))
(gl-helper-ui-send-key key shift meta ctrl)
(xwidget-queue-redraw gl--xwidget))))
(defun gl-send-backspace ()
"Send `<backspace>' to GL module."
(interactive)
(gl-send-key "<backspace>"))
(defun gl-send-delete ()
"Send `<delete>' to GL module."
(interactive)
(gl-send-key "<delete>"))
(defun gl-send-tab ()
"Send `<tab>' to GL module."
(interactive)
(gl-send-key "<tab>"))
(defun gl-send-return ()
"Send `<return>' to GL module."
(interactive)
(gl-send-key "<return>"))
(defun gl-send-left ()
"Send `<left>' to GL module."
(interactive)
(gl-send-key "<left>"))
(defun gl-send-right ()
"Send `<right>' to GL module."
(interactive)
(gl-send-key "<right>"))
(defun gl-send-up ()
"Send `<up>' to GL module."
(interactive)
(gl-send-key "<up>"))
(defun gl-send-down ()
"Send `<down>' to GL module."
(interactive)
(gl-send-key "<down>"))
(defvar gl-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [tab] #'gl-send-tab)
(define-key map (kbd "TAB") #'gl-send-tab)
(define-key map [return] #'gl-send-return)
(define-key map (kbd "RET") #'gl-send-return)
(define-key map [backspace] #'gl-send-backspace)
(define-key map (kbd "DEL") #'gl-send-backspace)
(define-key map [delete] #'gl-send-delete)
(define-key map [left] #'gl-send-left)
(define-key map [right] #'gl-send-right)
(define-key map [up] #'gl-send-up)
(define-key map [down] #'gl-send-down)
(define-key map [home] #'gl--self-insert)
(define-key map [end] #'gl--self-insert)
(define-key map [C-home] #'gl--self-insert)
(define-key map [C-end] #'gl--self-insert)
(define-key map [escape] #'gl--self-insert)
(define-key map [remap self-insert-command] #'gl--self-insert)
map))
(define-derived-mode gl-mode fundamental-mode "GL"
"Major mode for GL buffer."
(buffer-disable-undo)
(setq gl--xwidget (xwidget-at (point-min)))
(setq buffer-read-only t))
(defun glarea-new (&rest args)
"Create a new GLArea xwidget with a new buffer."
(let ((buffer (generate-new-buffer (or (plist-get args :name) "*glarea*"))))
(with-current-buffer buffer
(insert " ")
(goto-char 1)
(let ((id (make-xwidget 'glarea nil 1 1 args (buffer-name))))
(put-text-property (point) (+ 1 (point))
'display (list 'xwidget ':xwidget id)))
(gl-mode)
(switch-to-buffer buffer))))
(provide 'gl)
;;; gl.el ends here