forked from hiredman/javap-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
javap-mode.el
121 lines (103 loc) · 5.13 KB
/
javap-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
;;; javap-mode.el --- Javap major mode
;;; Version: 9
;;; URL: http://github.com/elp-revive/javap-mode
;; Copyright (C) 2011 Kevin Downey
;; Copyright (C) 2022 Jen-Chieh Shen
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
(defconst javap-font-lock-keywords
(eval-when-compile
`(
("line [0-9]+: [0-9]+" . font-lock-comment-face)
("\\<[a-zA-Z]+\\.[a-zA-Z0-9._]*[A-Z]+[a-zA-Z0-9/.$]*\\>" . font-lock-type-face) ;; borrowed from clojure-mode
("\\<[a-zA-Z]+/[a-zA-Z0-9/_]*[A-Z]+[a-zA-Z0-9/$]*\\>" . font-lock-type-face)
("[0-9]+:" . font-lock-comment-face)
("\\(#.+\\)" . font-lock-comment-face)
("\\(\\w\\|_\\)+(" . font-lock-preprocessor-face)
(")" . font-lock-preprocessor-face)
("\\(invoke\\w+\\)" . font-lock-function-name-face)
(,(regexp-opt '("boolean" "int" "void" "char"))
. font-lock-type-face)
(,(regexp-opt '("Exception table"
"LocalVariableTable"
"LineNumberTable")) . font-lock-warning-face)
(".load_\\w+" . font-lock-keyword-face)
(".load" . font-lock-keyword-face)
(".store_\\w+" . font-lock-keyword-face)
(".const_[0-9]+" . font-lock-keyword-face)
(".return" . font-lock-keyword-face)
(,(regexp-opt
'("ifne" "athrow" "new" "dup" "aastore" "anewarray" "ifnull" "ifeq" "ifnonnull"
"getstatic" "putfield" "getfield" "checkcast" "astore" "aload" "ldc" "goto" "putstatic"
"pop" "instanceof" "ldc_w" "sipush" "bipush" "aaload" "bastore" "baload" "arraylength"
"castore" "saload" "lastore" "daload" "dastore" "ifle" "istore" "lookupswitch" "iinc"
"if_icmpge" "isub" "if_icmpgt" "if_acmpne" "iflt" "if_icmplt" "if_icmple" "dcmpg"
"dcmpl" "ldc2_w" "lcmp" "fcmpg" "fcmpl" "ifge" "jsr" "ifgt" "ret" "aconst_null" "swap"
"if_acmpeq" "dup_x2"))
. font-lock-keyword-face)
(".add" . font-lock-keyword-face)
(,(regexp-opt
'("public" "static" "final" "volatile" ";" "transient" "class" "extends" "implements"
"synchronized" "protected" "private" "abstract" "interface" "Code:" "throws"))
. font-lock-comment-face)
;; ("\\(\\w+\\)" . font-lock-keyword-face)
))
"Default expressions to highlight in javap mode.")
(defvar javap-mode-syntax-table′ (make-syntax-table)
"Syntax table for use in javap-mode.")
;;;###autoload
(define-derived-mode javap-mode fundamental-mode "javap"
"A major mode for viewing javap files."
:syntax-table javap-mode-syntax-table′
(modify-syntax-entry ?_ "w" javap-mode-syntax-table′)
(modify-syntax-entry ?# "<" javap-mode-syntax-table′)
(modify-syntax-entry ?\n ">" javap-mode-syntax-table′)
(set (make-local-variable 'comment-start) "#")
(set (make-local-variable 'comment-start-skip) "#")
(set (make-local-variable 'font-lock-defaults) '(javap-font-lock-keywords)))
;;;###autoload
(defun javap-buffer ()
"run javap on contents of buffer"
(interactive)
(let* ((b-name (file-name-nondirectory (buffer-file-name)))
(b-name (substring b-name 0 (- (length b-name) 6)))
(new-b-name (concat "*javap " b-name ".class" "*"))
(new-buf (get-buffer new-b-name))
(old-buf (buffer-name))
(done (lambda (&rest _)
(interactive)
(progn
(kill-buffer (current-buffer))
(kill-buffer old-buf)))))
(if new-buf
(switch-to-buffer new-buf)
(let ((new-buf (get-buffer-create new-b-name)))
(progn
(switch-to-buffer new-buf)
(call-process "javap" nil new-buf nil "-c" "-l" "-classpath" "." b-name)
;; (call-process "javap" nil new-buf nil "-c" "-l" "-package" "-protected" "-private" "-classpath" "." b-name)
(setq buffer-read-only 't)
(set-window-point (selected-window) 0))))
(javap-mode)
(local-set-key [(q)] done)))
;;;###autoload
(add-hook 'find-file-hook
(lambda (&rest _)
(when (and (buffer-file-name)
(string= ".class" (substring (buffer-file-name) -6 nil)))
(javap-buffer))))
(provide 'javap-mode)
;;; javap-mode.el ends here