forked from idris-hackers/idris-mode
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidris-common-utils.el
167 lines (150 loc) · 8.26 KB
/
idris-common-utils.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
;;; idris-common-utils.el --- Useful utilities -*- lexical-binding: t -*-
;; Copyright (C) 2013 Hannes Mehnert
;; Author: Hannes Mehnert <[email protected]>
;; License:
;; Inspiration is taken from SLIME/DIME (http://common-lisp.net/project/slime/) (https://github.com/dylan-lang/dylan-mode)
;; Therefore license is GPL
;; 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 3, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
(require 'idris-core)
(require 'cl-lib)
(defun idris-buffer-name (type)
(cl-assert (keywordp type))
(concat (format "*idris-%s*" (substring (symbol-name type) 1))))
(defun idris-kill-buffer (buffer)
(let ((buf (cond
((symbolp buffer)
(get-buffer (idris-buffer-name buffer)))
((stringp buffer)
(get-buffer buffer))
((bufferp buffer)
buffer)
(t (message "don't know how to kill buffer")))))
(when (and buf (buffer-live-p buf)) (kill-buffer buf))))
(defun idris-minibuffer-respecting-message (text &rest args)
"Display TEXT as a message, without hiding any minibuffer contents."
(let ((mtext (format " [%s]" (apply #'format text args))))
(if (minibuffer-window-active-p (minibuffer-window))
(minibuffer-message mtext)
(message "%s" mtext))))
(defun idris-same-line-p (pos1 pos2)
"Return t if buffer positions POS1 and POS2 are on the same line."
(save-excursion (goto-char (min pos1 pos2))
(<= (max pos1 pos2) (line-end-position))))
(defmacro idris-save-marker (marker &rest body)
(let ((pos (cl-gensym "pos")))
`(let ((,pos (marker-position ,marker)))
(prog1 (progn . ,body)
(set-marker ,marker ,pos)))))
(defmacro idris-propertize-region (props &rest body)
"Execute BODY and add PROPS to all the text it inserts.
More precisely, PROPS are added to the region between the point's
positions before and after executing BODY."
(let ((start (cl-gensym)))
`(let ((,start (point)))
(prog1 (progn ,@body)
(add-text-properties ,start (point) ,props)))))
(defmacro idris-propertize-spans (spans &rest body)
"Execute BODY and add the properties indicated by SPANS to the
inserted text (that is, relative to point prior to insertion)."
(let ((start (cl-gensym)))
`(let ((,start (point)))
(prog1 (progn ,@body)
(cl-loop for (begin length props) in ,spans
do (add-text-properties (+ ,start begin)
(+ ,start begin length)
props))))))
(defun idris-repl-semantic-text-props (highlighting)
(cl-loop for (start length props) in highlighting
collecting (list start
length
(let* ((name (assoc :name props))
(implicit (assoc :implicit props))
(decor (assoc :decor props))
(unique-val (cl-gensym)) ; HACK to stop consecutive mouse-faces from interfering
(implicit-face (if (and implicit (equal (cadr implicit) :True))
'(idris-semantic-implicit-face)
nil))
(text-face (pcase (assoc :text-formatting props)
(`(:text-formatting :bold)
'bold)
(`(:text-formatting :italic)
'italic)
(`(:text-formatting :underline)
't)
(_ '())))
(decor-face (if decor
(cdr (assoc (cadr decor)
'((:type idris-semantic-type-face)
(:data idris-semantic-data-face)
(:function idris-semantic-function-face)
(:keyword idris-keyword-face)
(:metavar idris-metavariable-face)
(:bound idris-semantic-bound-face))))
nil))
(doc-overview (pcase (assoc :doc-overview props)
(`(:doc-overview ,docs) (concat "\n" docs))
(_ "")))
(type (pcase (assoc :type props)
(`(:type ,ty) (concat " : " ty))
(_ "")))
(mousable-face (if (and (not (equal (cadr decor) :bound)) ;non-bound becomes clickable
name)
`((:inherit ,decor-face :box t :hack ,unique-val))
nil))
(mouse-help (if (and (not (equal (cadr decor) :bound)) ;non-bound becomes clickable
name)
"\n<mouse-3> context menu"
"")))
`(,@(if name
(append `(help-echo (concat ,(cadr name)
,type
,doc-overview
,mouse-help))
(if (and (not (equal (cadr decor) :bound))
name)
`(idris-ref ,(cadr name)
keymap ,(idris-make-ref-menu-keymap (cadr name)))
nil))
nil)
,@(if mousable-face
(list 'mouse-face mousable-face)
())
face ,(cons text-face (append implicit-face decor-face)))))))
;;; Dispatching of events and helpers
(defmacro destructure-case (value &rest patterns)
"Dispatch VALUE to one of PATTERNS.
A cross between `case' and `destructuring-bind'.
The pattern syntax is:
((HEAD . ARGS) . BODY)
The list of patterns is searched for a HEAD `eq' to the car of
VALUE. If one is found, the BODY is executed with ARGS bound to the
corresponding values in the CDR of VALUE."
(let ((operator (cl-gensym "op-"))
(operands (cl-gensym "rand-"))
(tmp (cl-gensym "tmp-")))
`(let* ((,tmp ,value)
(,operator (car ,tmp))
(,operands (cdr ,tmp)))
(case ,operator
,@(mapcar (lambda (clause)
(if (eq (car clause) t)
`(t ,@(cdr clause))
(cl-destructuring-bind ((op &rest rands) &rest body) clause
`(,op (destructuring-bind ,rands ,operands
. ,body)))))
patterns)
,@(if (eq (caar (last patterns)) t)
'()
`((t (error "ELISP destructure-case failed: %S" ,tmp))))))))
(provide 'idris-common-utils)