-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgift-mode.el
228 lines (183 loc) · 9.29 KB
/
gift-mode.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
;;; gift-mode.el --- major mode for editing GIFT format quizzes
;; 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 this program. If not, see <http://www.gnu.org/licenses/>.
;; Copyright (C) 2017-2018 Christophe Rhodes <[email protected]>
;; Author: Christophe Rhodes <[email protected]>
;; URL: https://github.com/csrhodes/gift-mode
;; Version: 0.1
;;; Commentary:
;; A major mode for editing quiz questions in GIFT format.
;; GIFT format is an import format for quiz questions in the Moodle
;; virtual learning environment, supporting multiple-choice,
;; true-false, short answer, matching missing word, and numerical
;; questions.
;; This mode provides syntax highlighting and editing commands to make
;; rapid viewing and modification of large numbers of questions
;; easier.
;; More details on GIFT format are available from Moodle's
;; documentation at <https://docs.moodle.org/en/GIFT_format>.
;;; Code:
(require 'font-lock)
(require 'tex-mode)
(require 'outline)
(require 'newcomment)
(defvar
gift-imenu-comments-regexp
"\\(\\(^\\s-*//.*$\\)\\|\\(^\\s-*$\\)\\)+"
"Regex that captures empty lines and one-line comments in group 1.")
(defvar
gift-imenu-escaped-chars
'("~" "#" "{" "}" ":")
"List of special characters in gift format. They are escaped with a backslash (\\).")
(defvar gift-imenu-question-regexp
(let*
((escaped-chars (mapcar (lambda (arg) (concat "\\(\\\\" arg "\\)" )) gift-imenu-escaped-chars))
(regexp (mapconcat 'identity escaped-chars "\\|")))
(concat "^\\(\\([^\\\\}]\\|" regexp "\\)*?\\){" ))
"Regex that captures the beginning of a question in group 1, including its comments and title, up to the first bracket ({).")
(defvar
gift-imenu-title-regexp "::\\(.*\\)::"
"Regex than captures the title of a question in group 1. The matched string is already unescaped, so backslash can be ignored.")
(defun gift-imenu-unescape-title (title)
"Unescape special characters in TITLE, as defined in gift-imenu-escaped-chars."
(dolist (character gift-imenu-escaped-chars)
(let*
((regexp (concat "\\\\" character)))
(setq title (replace-regexp-in-string regexp character title t t))))
title)
(defun gift-imenu-sanitize-title (title)
"Cleans a question TITLE: empty lines, comments, spaces, extract title."
(let*
((nocoments (replace-regexp-in-string gift-imenu-comments-regexp " " (match-string 1) nil 'literal) )
(noextrablanks (replace-regexp-in-string "\\(\n\\|\\s-\\)+" " " nocoments nil 'literal) )
(unescaped (gift-imenu-unescape-title noextrablanks))
(trimmed (string-trim unescaped)))
(if (string-match gift-imenu-title-regexp trimmed)
(match-string 1 trimmed)
trimmed)))
(defun gift-imenu-index ()
"Return a table of contents for a gift buffer for use with Imenu."
(goto-char (point-min))
(let ((imenu-index)(position)(title-or-question)(entry))
(setq imenu-index (list) )
(while (< (point) (point-max))
(when (re-search-forward gift-imenu-question-regexp nil 1 1)
(setq position (point))
(setq title-or-question (gift-imenu-sanitize-title (match-string 1)))
(when (> (length title-or-question) 0)
(setq entry ( cons title-or-question position ) )
(push entry imenu-index))))
(reverse imenu-index)))
(defun gift-imenu-setup ()
"Setup the variables to support imenu."
(setq imenu-create-index-function 'gift-imenu-index))
(defvar gift-mode-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?/ ". 12b" table)
(modify-syntax-entry ?\n "> b" table)
;; maybe these should be in the font-lock-syntax-table instead?
(modify-syntax-entry ?~ "_ " table)
(modify-syntax-entry ?\\ "_ " table)
table))
(defgroup gift nil "Mode for editing GIFT-format quiz questions"
:tag "GIFT"
:group 'wp)
(defface gift-keyword '((t (:inherit font-lock-keyword-face)))
"keywords for GIFT quizzes")
(defface gift-category '((t (:inherit outline-1)))
"category for GIFT quizzes")
(defface gift-question-name '((t (:inherit outline-2)))
"question name in GIFT quizzes")
(defface gift-latex-math '((t (:inherit tex-math)))
"math notation in GIFT quizzes")
(defface gift-wrong '((t (:inherit error)))
"wrong answers in multiple-choice questions in GIFT quizzes")
(defface gift-right '((t (:inherit success)))
"right answers in multiple-choice questions in GIFT quizzes")
(defface gift-wrong-credit '((t (:inherit gift-wrong :weight normal)))
"credit for wrong answer in GIFT quizzes")
(defface gift-right-credit '((t (:inherit gift-right :weight normal)))
"credit for right answer in GIFT quizzes")
(defface gift-feedback '((t (:inherit font-lock-string-face)))
"feedback in GIFT quizzes")
;;; FIXME: I'd really like to highlight the background of right/wrong
;;; answers. However, the GIFT grammar is more context-sensitive than
;;; simple font-lock search will let me deal with directly: it seems
;;; to be valid to use $$foo = bar$$ in the question body, but in the
;;; answers it needs to be $$foo \= bar$$. (Only the second variant
;;; is context-free.) Supporting both is probably possible with some
;;; multiline magic; supporting just the escaped version is
;;; straightforward but might be surprising to users.
(defvar gift-font-lock-keywords
'(
("\\_<=\\(\\([^\\~=\n}#%]\\|\\\\[}~=#%]\\)*\\)\\(#\\(.*\\)\\)?" (1 'gift-right keep) (4 'gift-feedback keep t))
("{#\\(-?[0-9.:]+\\)" (1 'gift-right keep))
("\\_<~\\(%\\([0-9.]+\\)%\\)\\(\\([^\\~=%}\n#]\\|\\\\[A-Za-z0-9}~=#%]\\)*\\)\\(#\\(.*\\)\\)?" (2 'gift-right-credit) (3 'gift-right keep) (6 'gift-feedback keep t))
("\\_<~\\(%\\(-[0-9.]+\\)%\\)\\(\\([^\\~=%}\n#]\\|\\\\[A-Za-z0-9}~=#%]\\)*\\)\\(#\\(.*\\)\\)?" (2 'gift-wrong-credit) (3 'gift-wrong keep) (6 'gift-feedback keep t))
("\\_<~\\(\\([^\\~=%}\n#]\\|\\\\[}~=#%]\\)*\\)\\(#\\(.*\\)\\)?" (1 'gift-wrong keep) (4 'gift-feedback keep t))
("\\$\\$\\([^$]\\|\\\\$\\)*[^\\]\\$\\$" (0 'gift-latex-math t)) ; doesn't handle \$$$O(n)$$ correctly; think about font-lock-multiline
("\\(\\$CATEGORY\\):\s-*\\(\\$course\\$/?\\|\\)\\(.*?\\)\\(//\\|$\\)" (1 'gift-keyword) (2 'gift-keyword) (3 'gift-category))
("::\\([^:]\\|\\\\:\\)+::" . 'gift-question-name)))
(defvar gift-credit '("-100" "-50" "-33.3333" "-25" "-20" "-16.6667" "16.6667" "20" "25" "33.3333" "50" "100"))
(defun gift-decrease-credit ()
(interactive)
(save-excursion
(save-match-data
(let ((bol (save-excursion (beginning-of-line) (point)))
(eol (save-excursion (end-of-line) (point))))
(if (search-forward-regexp "~%\\(-?[0-9.]+\\)%" eol t)
(let* ((current (match-string 1))
(cpos (cl-position current gift-credit :test 'equal)))
(when (and cpos (> cpos 0))
(replace-match (elt gift-credit (1- cpos)) t t nil 1)))
(beginning-of-line)
(when (search-forward-regexp "~%\\(-?[0-9.]+\\)%" eol t)
(let* ((current (match-string 1))
(cpos (cl-position current gift-credit :test 'equal)))
(when (and cpos (> cpos 0))
(replace-match (elt gift-credit (1- cpos)) t t nil 1)))))))))
(defun gift-increase-credit ()
(interactive)
(save-excursion
(save-match-data
(let ((bol (save-excursion (beginning-of-line) (point)))
(eol (save-excursion (end-of-line) (point))))
(if (search-forward-regexp "~%\\(-?[0-9.]+\\)%" eol t)
(let* ((current (match-string 1))
(cpos (cl-position current gift-credit :test 'equal)))
(when (and cpos (< cpos (1- (length gift-credit))))
(replace-match (elt gift-credit (1+ cpos)) t t nil 1)))
(beginning-of-line)
(when (search-forward-regexp "~%\\(-?[0-9.]+\\)%" eol t)
(let* ((current (match-string 1))
(cpos (cl-position current gift-credit :test 'equal)))
(when (and cpos (< cpos (1- (length gift-credit))))
(replace-match (elt gift-credit (1+ cpos)) t t nil 1)))))))))
;;;###autoload
(define-derived-mode gift-mode text-mode "GIFT"
"Major mode for editing GIFT format quizzes.
\\{gift-mode-map}"
:group 'gift
:syntax-table gift-mode-syntax-table
(gift-imenu-setup)
(setq font-lock-defaults (list '(gift-font-lock-keywords) nil))
(set (make-local-variable 'outline-regexp) "\\(\\$CATEGORY\\|::\\)")
(set (make-local-variable 'outline-heading-end-regexp)
"\\(\\$CATEGORY.*\n\\|::[^:]+::\\(.\\|\n\\)\\)")
(set (make-local-variable 'outline-heading-alist)
'(("$CATEGORY" . 1) ("::" . 2)))
(set (make-local-variable 'comment-start) "//"))
(define-key gift-mode-map (kbd "<C-left>") 'gift-decrease-credit)
(define-key gift-mode-map (kbd "<C-right>") 'gift-increase-credit)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.gift\\'" . gift-mode))
(provide 'gift-mode)
;;; gift-mode.el ends here