-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbox-pointer.el
88 lines (72 loc) · 3.04 KB
/
box-pointer.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
;;; box-pointer.el --- Box pointer notation in Emacs -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Daniel Nicolai
;; Author: Daniel Nicolai <[email protected]>
;; Keywords: tools
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
;; TODO create svg group element (see e.g. sketch-mode)
(defcustom bp-box-size 60
"Box size")
(defun bp-boxes (list)
(let (positions)
(cl-labels ((box-positions (tree x y)
(cond ((consp tree)
(push (list 'dbox x y) positions)
(box-positions (car tree)
x
(+ y (/ (* 3 bp-box-size)
2)))
(box-positions (cdr tree)
(+ x (/ (* 5 bp-box-size) 2))
y))
;; (t (box-positions (car tree) x y)))))
(t (push (list 'box x y tree) positions)))))
(let ((start (/ bp-box-size 2)))
(box-positions list start start)))
positions))
(defun svg-box (svg x y &optional label)
(let ((half-size (/ bp-box-size 2))
(offset (/ bp-box-size 6)))
(svg-rectangle svg
(- x half-size) (- y half-size)
bp-box-size bp-box-size)
(svg-text svg (symbol-name label)
:x (- x offset) :y (+ y offset)
:font-size 20
:fill "black"
:font-family "impact")))
(defun svg-dbox (svg x y &optional label)
(let ((half-size (/ bp-box-size 2)))
(svg-rectangle svg
(- x half-size) (- y half-size)
(* 2 bp-box-size) bp-box-size)
(svg-line svg
(+ x half-size) (- y half-size)
(+ x half-size) (+ y half-size))
(svg-circle svg x y 2 :fill "black")
(svg-line svg x y x (+ y bp-box-size))
(svg-circle svg (+ x bp-box-size) y 2 :fill "black")
(svg-line svg (+ x bp-box-size) y (+ x (* 2 bp-box-size)) y)))
(defun bp-draw (list)
(let* ((boxes (bp-boxes list))
(svg (svg-create 600 400 :fill "white" :stroke-color "black")))
(svg-rectangle svg 0 0 600 400)
(dolist (b (print boxes))
(case (car b)
('box (apply #'svg-box svg (cdr b)))
('dbox (apply #'svg-dbox svg (cdr b)))))
(svg-insert-image svg)))
(bp-draw '(a (b c)))
(provide 'box-pointer)
;;; box-pointer.el ends here