Created
March 14, 2015 10:34
-
-
Save lelit/90b8660771cf16654473 to your computer and use it in GitHub Desktop.
Elisp script to adapt SpiderMonkey msgs to js2-mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; -*- coding: utf-8 -*- | |
;;; :Progetto: js2-mode -- Adapt spidermonkey msgs | |
;;; :Creato: sab 14 mar 2015 11:26:19 CET | |
;;; :Autore: Lele Gaifax <[email protected]> | |
;;; :Licenza: GNU General Public License version 3 or later | |
;;; | |
(defun js2--morph-spidermonkey-msgs (status) | |
(require 'levenshtein) | |
(let (done) | |
(kill-region (point) (progn | |
(forward-paragraph 3) | |
(forward-line) | |
(point))) | |
(insert ";;; -*- mode: emacs-lisp -*-\n;;;\n\n") | |
(while (re-search-forward "\\*/$" nil t) | |
(replace-match "" t t)) | |
(goto-char (point-min)) | |
(while (re-search-forward "^// \\|/\\*\\| \\*/?" nil t) | |
(replace-match ";;; " t t)) | |
(goto-char (point-min)) | |
(while (re-search-forward | |
"^MSG_DEF(JSMSG_\\([^,]+\\)[^\\\"]+\\\"\\(.+\\)\\\")" nil t) | |
(let* ((origid (match-substitute-replacement "\\1")) | |
(id (concat "msg." (downcase | |
(replace-regexp-in-string | |
"_" "." | |
origid)))) | |
(msg (replace-regexp-in-string | |
"{[0-9]}" "%s" | |
(match-substitute-replacement "\\2"))) | |
js2msg idcomment msgcomment) | |
(unless (gethash id js2-message-table) | |
(message "Looking for %s" msg) | |
(maphash (lambda (i m) | |
(if (equal m msg) | |
(progn | |
(setq idcomment (concat " ; originally JSMSG_" origid)) | |
(setq id i)) | |
(when (and (< (abs (- (length m) (length msg))) 5) | |
(< (levenshtein-distance m msg) 6)) | |
(setq idcomment (concat " ; originally JSMSG_" origid) | |
msgcomment (concat ";; \"" m "\" in js2-mode") | |
id i)))) | |
js2-message-table)) | |
(setq js2msg (concat "(js2-msg \"" id "\"")) | |
(unless (gethash id js2-message-table) | |
(setq idcomment " ; UNUSED in js2-mode")) | |
(setq js2msg (concat js2msg idcomment "\n \"" msg "\"" | |
(if msgcomment (concat "\n " | |
msgcomment | |
"\n ")) | |
")")) | |
(replace-match js2msg t t) | |
(push id done))) | |
(goto-char (point-max)) | |
(insert-string "\n;;; Added by js2-mode\n") | |
(let (aslist) | |
(maphash (lambda (i m) | |
(unless (member i done) | |
(push (cons i m) aslist)) js2-message-table) | |
js2-message-table) | |
(dolist (i (sort aslist (lambda (a b) | |
(let ((id1 (car a)) | |
(id2 (car b))) | |
(< (compare-strings id1 nil nil id2 nil nil) 0))))) | |
(insert-string (concat "(js2-msg \"" (car i) "\"\n" | |
" \"" (cdr i) "\")\n\n")))))) | |
(defun js2-msgs-from-spidermonkey () | |
(let ((url "http://hg.mozilla.org/mozilla-central/raw-file/default/js/src/js.msg")) | |
(switch-to-buffer (url-retrieve url #'js2--morph-spidermonkey-msgs)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment