My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes  
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
;;; hxswfml-mode.el --- Mode for HaXe SWFMill files
;; Copyright (C) 2012 Oleg Sivokon

;; Author: Oleg Sivokon <olegsivokon@gmail.com>
;; Created: 08 November 2012
;; Keywords: languages, hxswfml, haxe, xml

;;; This file is not part of GNU Emacs.

;; 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 2
;; 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, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:

;; This package provides major mode `hxswfml-mode'
;; for editing HaXe SWFMill files.

;;; Code:

(require 'cl)
(require 'nxml-mode)
(require 'eieio)
(require 'thingatpt)
(require 'haxe-utils)

(defcustom hxswfml-compiler "hxswfml"
"The location of HXSwfML executable"
:type 'string
:group 'hxswfml-mode)

(defcustom hxswfml-genclass-pattern 'pascal
"The way to create class names when generating library files
from assets.
`pascal' means that the names will start with uppercase and
use uppercase letter for each subsequent word.
`underscore' means that the parts of the class name are
delimited by underscores and converted to lowercase.
`underscore-upper' similar to underscore, but the name is
converted to all upercase.
`underscore-mixed' keeps the case the same as it was in
original file names.
The default is `pascal'."
:type 'symbol :group 'hxswfml-mode)

(defcustom hxswfml-genclass-prefix nil
"This variable specifies how to prefix generated class
names when automatically generating them. Possible values are:
Any string - that string is prepended to the generated name.
A function, this function is called with the file name
relative to the directory, where the generation started.
If this variable is `nil' or `hxswfml-genclass-prefix-function'
Then it behaves like `hxswfml-genclass-prefix-function' would,
i.e. sub-directory names are taken to be parts of the package
names. For conversion rules see documentation for
`hxswfml-genclass-prefix-function'."
:group 'hxswfml-mode)

(defcustom hxswfml-genclass-prefix-substitution "_"
"The character used to replace invalid characters in
generated prefices for class names"
:type 'string
:group 'hxswfml-mode)

(defcustom hxswfml-assets-extensions
'((bitmapdata . "\\.\\(png\\|jpe?g\\|gif\\)$")
(sound . "\\.mp3$")
(font . "\\.ttf$")
(bytearray . "[^.]+\\..+")
(ignore . "~$\\|\\(^\\.\\(svn\\|git\\)$\\)"))
"The list of tags and the file extension for generating
library files."
:type 'list
:group 'hxswfml-mode)

(defcustom hxswfml-default-library-file "lib.xml"
"The default name for library when generating resources
library from directory, saved in the same directory as the
resources."
:type 'string
:group 'hxswfml-mode)

(defcustom hxswfml-default-superclasses
'((bitmapdata . "flash.display.BitmapData")
(sound . "flash.media.Sound")
(font . "flash.text.Font")
(bytearray . "flash.utils.ByteArray"))
"The superclasses for classes being generated, see also
`hxswfml-assets-extensions'."
:type 'list
:group 'hxswfml-mode)

(defvar hxswfml-install-dir
(file-name-directory load-file-name)
"The directory whre HaXe SWFMill mode is installed")

(defvar hxswfml-genclass-prefix-function
#'hxswfml--genclass-prefix-function
"The default function for processing directory names and
creating suffices based on them")

(defvar hxswfml-cli-options
'(xml2swf xml2lib xml2swc xml2abc abc2swf abc2swc
abc2xml abc2hxm xml2hxm ttf2swf ttf2hx
ttf2path flv2swf)
"Commands hxswfml understands")

(defvar hxswfml-class-template nil
"This variable is caching the contents of the class template
when generating library classes. You shouldn't need to change
its value.")

(defvar hxswfml-genclass-trie nil
"This variable is set to the trie for replacing in template
for generating classes. You shouldn't need to set it yourself.")

(defun hxswfml--genclass-prefix-function (dir)
"Converts the directory name into a package name using these
rules:
- split the directory name on slashes ignoring empty results.
- process every part as follows:
* if it starts with non-letter, replace the first character
with underscore.
* for every subsequent character, if it is not a letter or
a digit or a full stop, replace it with underscore.
* if the full stop is the last character, it is replaced
with underscore.
You may change what character is used for replacing invalid
characters in the prefix by customizing
`hxswfml-genclass-prefix-substitution' variable"
(let ((first-letter " ")
result)
(dolist (part (split-string dir "/" t)
(mapconcat #'identity (reverse result) "."))
(setf (aref first-letter 0) (aref part 0))
(unless (string-match "[[:alpha:]]" first-letter)
(setf (aref part 0) hxswfml-genclass-prefix-substitution))
(when (> (length part) 1)
(dotimes (i (1- (length part)))
(setf (aref first-letter 0) (aref part (1+ i)))
(unless (string-match "\\w\\|\\." first-letter)
(setf (aref part (1+ i))
(aref hxswfml-genclass-prefix-substitution 0))))
(when (char-equal (aref part (1- (length part))) ?\.)
(setf (aref part (1- (length part)))
(aref hxswfml-genclass-prefix-substitution 0))))
(push part result))))

(defun hxswfml--int-suffix (word)
"Returns position before the last digit in the WORD."
(let ((pos (1- (length word))))
(while (and
(> pos -1)
(member
(aref word pos)
'(?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?0)))
(decf pos))
(1+ pos)))

(defun hxswfml--ensure-unique-name (name hash)
"Ensure that the NAME is unique in HASH, i.e. generate a new
name, if one is in hash and return it. HASH is updated with the
new name."
(if (gethash name hash)
(let ((prefix
(substring name 0 (hxswfml--int-suffix name)))
(suffix 0) result)
(when (> (length result) (length prefix))
(setq suffix
(string-to-number
(substring result (length prefix)))))
(while (gethash (format "%s%d" prefix (incf suffix)) hash))
(puthash (setq result (format "%s%d" prefix (incf suffix)))
t hash)
result)
(puthash name t hash) name))

(defun hxswfml--pascal-classname (file &optional names)
"Translates the FILE into PascalCase while also removing
non-alphanumeric characters. Unless the first character is
not a letter, the `hxswfml-genclass-prefix-substitution'
is prepended to the name."
(let ((mask " ")
(result
(mapconcat #'capitalize
(split-string file "[^[:alnum:]]") "")))
(setf (aref mask 0) (aref result 0))
(unless (string-match "[[:alpha:]]" mask)
(setq result
(concat hxswfml-genclass-prefix-substitution result)))
(when names
(setq result (hxswfml--ensure-unique-name result names)))
result))

(defun hxswfml--underscore-classname (file &optional names kind)
"Replaces all non-aphanumeric characters in FILE by
`hxswfml-genclass-prefix-substitution'. Depending on KIND
will also either upcase, downcase or leave the case of the
remaining characters unchanged. Possible values for KIND are
`mixed' and `upper'. Everything else is treated as downcase.
If first characters is not a letter, then
`hxswfml-genclass-prefix-substitution' is prepended to the name.
If NAMES (a hash-map) already contains the name this function
would have generated otherwise, will append an integer, while
firstly searching for an integral part at the end of the generated
name."
(let ((mask " ")
(result
(mapconcat
#'identity
(mapcar
(cond
((eq kind 'mixed) #'identity)
((eq kind 'uper) #'upcase)
(t #'downcase))
(split-string file "[^[:alnum:]]"))
hxswfml-genclass-prefix-substitution)))
(setf (aref mask 0) (aref result 0))
(unless (string-match "[[:alpha:]]" mask)
(setq result
(concat hxswfml-genclass-prefix-substitution result)))
(when names
(setq result (hxswfml--ensure-unique-name result names)))
result))

(defun hxswfml--insert-directory (dir &optional names prefix genclass)
"Inserts all resources found in DIR directory into
current buffer."
(let ((ignored (cdr (assoc 'ignore hxswfml-assets-extensions)))
(prefix
(or prefix
(file-name-base (directory-file-name dir)))))
(dolist (file (directory-files dir))
(unless (or (string= file ".")
(string= file "..")
(and ignored (string-match ignored file)))
(if (file-directory-p (expand-file-name file dir))
(hxswfml--insert-directory
(expand-file-name file dir) names
(concat prefix "/" file) genclass)
(hxswfml--insert-resource file prefix names genclass))))))

(defun hxswfml--insert-resource (file dir &optional names genclass)
"Inserts a tag for file, if it matches any of descriptors in
`hxswfml-assets-extensions'"
(let ((prefix-generator
(or hxswfml-genclass-prefix
hxswfml-genclass-prefix-function))
(name-generator hxswfml-genclass-pattern)
(extensions hxswfml-assets-extensions)
prefix name kind)
(setq prefix
(cond
((stringp prefix-generator)
prefix-generator)
((fboundp prefix-generator)
(funcall prefix-generator dir))
(t nil))
name
(cond
((eq name-generator 'underscore)
(hxswfml--underscore-classname
(file-name-sans-extension file) names))
((eq name-generator 'underscore-mixed)
(hxswfml--underscore-classname
(file-name-sans-extension file) names 'mixed))
((eq name-generator 'underscore-upper)
(hxswfml--underscore-classname
(file-name-sans-extension file) names 'upper))
(t (hxswfml--pascal-classname
(file-name-sans-extension file) names))))
(catch 't
(while extensions
(setq kind (car extensions)
extensions (cdr extensions))
(unless (eq (car kind) 'ignore)
(when (string-match (cdr kind) file)
(when genclass
(hxswfml-class-from-template
genclass (concat dir file) name (car kind)))
(insert "<" (symbol-name (car kind))
" file=\"" (if dir (concat dir "/") "")
file "\" class=\""
(if prefix (concat prefix ".") "")
name "\"/>\n")
(throw 't nil)))))))

(defun hxswfml--generate-from-directory (dir target genclass)
"Does the job of `hxswfml-generate-from-directory'"
(with-temp-file target
(insert "<lib>\n")
(hxswfml--insert-directory
dir (make-hash-table :test #'equal) nil genclass)
(insert "</lib>")))

(defun hxswfml--file-package (name separator)
"Returns the directory part of the NAME with the directory
separator characters replaced by SEPARATOR character."
(haxe-replace-char
(substring
name 0
(or (position ?\/ name :from-end t) 0)) ?\/ separator))

(defun hxswfml--ensure-directory (dir)
(unless (and (file-exists-p dir) (file-directory-p dir))
;; if it was a file, where we wanted to create a directory
;; then let it throw.
(make-directory dir t)) dir)

;; TODO: This may be useful as a separate package.
(defun hxswfml-class-from-template (save-in file class-name tag)
"Generates the HaXe class with the name CLASS-NAME in directory
SAVE-IN followed by the package name of the class, inferred from the
FILE name (the prefix before the last slash).
What super-class is chosen for this class is decided based on the
value of TAG and it's corresponding value from
`hxswfml-default-superclasses'"
(unless hxswfml-class-template
(with-temp-buffer
(insert-file-contents-literally
(expand-file-name "templates/lib.tpl" hxswfml-install-dir))
(setq hxswfml-class-template (buffer-string))))
(let* ((package (hxswfml--file-package file ?\.))
(superclass (cdr (assoc tag hxswfml-default-superclasses)))
(dir-with-package
(concat save-in
(haxe-replace-char
(format "%s" package) ?\. ?\/))))
(if hxswfml-genclass-trie
(haxe-update-trie
hxswfml-genclass-trie
`(("$package" . ,package)
("$super_class" . ,superclass)
("$class" . ,class-name)))
(setq hxswfml-genclass-trie
(haxe-build-trie
`(("$package" . ,package)
("$super_class" . ,superclass)
("$class" . ,class-name)))))
(with-temp-file
(expand-file-name
(concat class-name ".hx")
(hxswfml--ensure-directory dir-with-package))
(message "Generating outline: <%s/%s>"
dir-with-package (concat class-name ".hx"))
(insert (haxe-replace-trie
hxswfml-class-template
(oref hxswfml-genclass-trie trie))))))

;;;###autoload
(defun hxswfml-generate-from-directory
(dir &optional lib-file generate-classes)
"Creates a library file TARGET by recursively searching
directory DIR and appending known types of assets to the
generated file.
The assets are identified by extension, the assoc list
specifying which asset extension match which tags can be
customized through `hxswfml-assets-extensions' variable.
If GENERATE-CLASSES is `t', then the classes are generated
for each resource mentioned in the generated library file.
All class-files are thus saved in sub-directory of parent
directory of DIR named \"generated\".
However, if it is a directory, then the classes are generated
in that directory.
The file name of the library being generated can be customized
by setting `hxswfml-default-library-file' variable.
The superclasses for generated classes can be customized through
setting `hxswfml-default-superclasses' variable."
(interactive
(let* ((dir-i
(read-directory-name
"Select the directory containing assets: "))
(lib-file-i
(completing-read
"Library XML will be generated in: "
(list (concat (file-name-directory
(directory-file-name dir-i))
hxswfml-default-library-file))
nil t (concat (file-name-directory
(directory-file-name dir-i))
hxswfml-default-library-file)))
genclass-i)
(when (yes-or-no-p "Generate outlines? ")
(setq genclass-i
(read-directory-name
"Directory to generate outlines: "
(concat (file-name-directory
(directory-file-name dir-i))
"generated/"))))
(list dir-i lib-file-i genclass-i)))
(let ((target
(concat
(file-name-directory
(file-name-nondirectory dir))
hxswfml-default-library-file)))
(catch 't
(when (file-exists-p target)
(unless
(yes-or-no-p
(format "File <%s> already exists, overwrite? "
target))
(throw 't nil)))
(message "Generating classes in <%s>" generate-classes)
(hxswfml--generate-from-directory
dir target generate-classes)
(find-file target)
(hxswfml-mode))))

(defun hxswfml-validate-files ()
"Searches for file paths in the current buffer and tries to
resolve them relatively to the file displayed in the buffer."
(save-excursion
(goto-char (point-min))
(let ((last-point (point)) token found-file)
(while (< (point) (point-max))
(setq last-point (forward-word)
token (buffer-substring-no-properties
(max (point-min) (- (point) 5))
(point)))
(when (string-match "\\<file$" token)
(unless (char-equal
(or (char-after) ?x) ?=)
(forward-whitespace 1))
(when (char-equal (or (char-after) ?x) ?=)
(forward-char)
(unless (or
(char-equal
(or (char-after) ?x) ?\")
(char-equal
(or (char-after) ?x) ?\'))
(forward-whitespace 1))
(setq token
(buffer-substring-no-properties
(point) (progn (forward-sexp) (point))))
(message "found file: <%s>" token)))))))

;;;###autoload
(define-derived-mode hxswfml-mode nxml-mode "HaXe SWFMill"
"Major mode for editing HaXe SWFMill files."
(add-to-list
'rng-schema-locating-files
(expand-file-name "hxswfml-schemas.xml" hxswfml-install-dir))
(rng-auto-set-schema))

(provide 'hxswfml-mode)

;; Local Variables:
;; byte-compile-warnings: (not cl-functions)
;; End:

;;; hxswfml-mode.el ends here

Change log

r36 by olegsivokon on Nov 16, 2012   Diff
Restructured and cleaned up a bit,
resolved some warnings (but not all)
caused by the last changes in the compiler
mode (still non-functional, but getting
close).
Go to: 
Project members, sign in to write a code review

Older revisions

r32 by olegsivokon on Nov 12, 2012   Diff
Started working on special mode for
interaction with HaXe compiler server,
fixed few minor syntax and dependency
issues in hxswfml.el
r31 by olegsivokon on Nov 12, 2012   Diff
Some fixes to outline generation in
hxswfml-mode
r30 by olegsivokon on Nov 11, 2012   Diff
Added (but not finished) trie-
replacing for templates generation for
HaXe SWFMill mode.
All revisions of this file

File info

Size: 17587 bytes, 461 lines
Powered by Google Project Hosting