-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathphits-mode.el
134 lines (107 loc) · 6.72 KB
/
phits-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
;; Generic mode for highlighting syntax for PHITS input files
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Latest version is available here:
;; https://github.com/kbat/mc-tools/blob/master/mctools/phits/phits-mode.el
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; How to use:
;; Either put -*-phits-mode-*- on the first line of your
;; input file to autoload this mode or use the .phits extention
;;
;; Your .emacs file should contain something like:
;; (setq load-path (cons (expand-file-name "/path/to/your/lispdirectory") load-path))
;; (global-font-lock-mode t)
;; (load "phits-mode")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'font-lock)
(require 'generic)
(make-face 'font-lock-section-face)
(set-face-foreground 'font-lock-section-face "red")
(set-face-attribute 'font-lock-section-face nil :weight 'bold)
(make-face 'font-lock-particle-face)
(set-face-foreground 'font-lock-particle-face "yellow")
(make-face 'font-lock-transformation-face)
(set-face-foreground 'font-lock-transformation-face "yellow")
(make-face 'font-lock-parameter-face)
(set-face-foreground 'font-lock-parameter-face "lightblue")
(make-face 'font-lock-angel-parameter-face)
(set-face-foreground 'font-lock-angel-parameter-face "lightgreen")
(make-face 'font-lock-constant-face)
(set-face-foreground 'font-lock-constant-face "lightgreen")
(make-face 'font-lock-function-face)
(set-face-foreground 'font-lock-function-face "orange")
(make-face 'font-lock-surface-face)
(set-face-foreground 'font-lock-surface-face "lightgreen")
;; from stackoverflow.com/questions/4549015:
;; how to add it in phits-mode ???
(defun my-c-mode-font-lock-if0 (limit)
(save-restriction
(widen)
(save-excursion
(goto-char (point-min))
(let ((depth 0) str start start-depth)
(while (re-search-forward "^\\s-*#\\s-*\\(if\\|else\\|endif\\)" limit 'move)
(setq str (match-string 1))
(if (string= str "if")
(progn
(setq depth (1+ depth))
(when (and (null start) (looking-at "\\s-+0"))
(setq start (match-end 0)
start-depth depth)))
(when (and start (= depth start-depth))
(c-put-font-lock-face start (match-beginning 0) 'font-lock-comment-face)
(setq start nil))
(when (string= str "endif")
(setq depth (1- depth)))))
(when (and start (> depth 0))
(c-put-font-lock-face start (point) 'font-lock-comment-face)))))
nil)
(defun my-c-mode-common-hook ()
(font-lock-add-keywords
nil
'((my-c-mode-font-lock-if0 (0 font-lock-comment-face prepend))) 'add-to-end))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
;; end from stackoverflow
(define-generic-mode 'phits-mode
;; comment-list (2 ways to comment in PHITS so do below)
nil
;; keyword-list (do below also)
nil
;; font-lock-list (additional expressions to highlight)
'(
("^c.*" . 'font-lock-comment-face) ;; a "c" followed by a blank in
("^ c .*" . 'font-lock-comment-face) ;; columns 1-5 is a comment line
("^ c .*" . 'font-lock-comment-face) ;; (the reg exp \{n,m\} does not
("^ c .*" . 'font-lock-comment-face) ;; seem to work here)
("^ c .*" . 'font-lock-comment-face)
("$.*" . 'font-lock-comment-face)
("!.*" . 'font-lock-comment-face)
("^#.*" . 'font-lock-comment-face)
("^ \\{0,4\\}\\[\\<\\(anatally\\|cell\\|end\\|importance\\|material\\|parameters\\|mat name color\\|reg name\\|source\\|surface\\|t-3dshow\\|t-heat\\|t-dpa\\|t-deposit2?\\|t-dchain\\|t-gshow\\|timer\\|title\\|t-cross\\|t-product\\|t-track\\|t-yield\\|temperature\\|transform\\|volume\\)\\>\\] ?.*" . 'font-lock-section-face) ;; ?.* in the end mean that we color also the section title (if any)
("^ *\\<\\(2d-type\\|amp\\|angel\\|[aelrstxyz]-type\\|axis\\|box\\|e-mode\\|dir\\|deltm\\|dmax\\|dom\\|dose\\|dump\\|e0\\|edel\\|[el]-dst\\|ejamnu\\|[el]-phi\\|epsout\\|[el]-the\\|eqmdmin\\|eqmdnu\\|factor\\|file\\|[rg]-show\\|heaven\\|gshow\\|iangform\\|icntl\\|icommat\\|icput\\|icrhi\\|icxsni\\|idivs\\|ielas\\|igamma\\|igchk\\|igcut\\|imout\\|imuint\\|inclg\\|incut\\|infl\\|info\\|inmed\\|igmuppd\\|ipnint\\|incohr\\|inucr\\|iphtout\\|ipltmode\\|ipreeq\\|iprofr\\|ireschk\\|ismm\\|isobar\\|itall\\|jmout\\|level\\|line\\|material\\|maxbch\\|maxcas\\|mesh\\|mirror\\|multiplier\\|n[aertxyz]\\|ndata\\|nedisp\\|negs\\|nevap\\|nlost\\|nspred\\|nucdata\\|output\\|outtime\\|part\\|phi\\|pnimul\\|pnint\\|product\\|proj\\|r0\\|reg\\|resol\\|rseed\\|shadow\\|source\\|timeevo\\|title\\|tmin\\|tmax\\|totfact\\|trcl\\|t[dnw]\\|unit\\|w-ang\\|w-dst\\|wgt\\|w-hgt\\|w-mn[hw]\\|w-wdt\\|width\\|[aerxyz]\\(max\\|min\\)\\|[xyz]\\([01]\\|max\\|min\\|-txt\\)\\|[xyz][xyz]\\)\\>" . 'font-lock-parameter-face) ;; note that there must be no other symbols (except space) before parameter
("\\<\\(area\\|a-curr\\|chart\\|color\\|current\\|deposit\\|dchain\\|dose\\|dpa\\|eng\\|heat\\|imp\\|flux\\|mat\\|mset[1-6]\\|[mM]t?[0-9]\\{1,4\\}\\|name\\|non\\|nucleus\\|off\\|q\\|qp\\|reg\\|r-from\\|r-in\\|r-out\\|rpp\\|r-to\\|r-z\\|set\\|size\\|source\\|t\\|tmp\\|vol\\|[xyz][xyz]?[xyz]?\\)\\>" . 'font-lock-keyword-face) ;; phits keywords
("\\<\\(all\\|alpha\\|electron\\|neutron\\|photon\\|positron\\|proton\\)\\>" . 'font-lock-particle-face) ;;
("\\<\\(a?cos\\|a?sin\\|a?tan\\|atan2\\)\\>" . 'font-lock-function-face) ;;
("\\<\\(ylin\\)\\>" . 'font-lock-angel-parameter-face) ;;
("\\<pi\\|c[1-9]\\>" . 'font-lock-constant-face)
("\\<\\(brown\\|black\\|blue\\|bluegreen\\|cyan\\|cyanblue\\|darkgray\\|darkgreen\\|gray\\|green\\|lightgray\\|lightgreen\\|magneta\\|matblack\\|mossgreen\\|orange\\|orangeyellow\\|pastelblue\\|pastelcyan\\|pastelpink\\|pastelpurple\\|pastelviolet\\|pastelyellow\\|pink\\|red\\|violet\\|white\\|yellow\\|yellowgreen\\)\\>" . 'font-lock-constant-face) ;; phits colors
("\\<\\(box\\|[ck]\/[xyz]\\|[ckpst][xyz]\\|[gs]q\\|hex\\|[ps]\\|rcc\\|rhp\\|rpp\\|so\\|sph\\|xy\\|zp\\)\\>" . 'font-lock-surface-face)
(" trans [0-9]+" . 'font-lock-transformation-face)
("^*?trcl[0-9]+" . 'font-lock-transformation-face)
("^*?tr[0-9]+" . 'font-lock-transformation-face)
("\\<\\(fill\\|u\\|lat\\|like\\|but\\)\\>" . 'font-lock-variable-name-face) ;; fill,universe,lat,trcl variables
)
;; auto-mode-list
'(".phits\\'")
;; function-list
nil
;; description
"generic mode for editing PHITS input files.")
;; case-insensitive keyword search
(defun case-insensitive-advice ()
(set (make-local-variable 'font-lock-keywords-case-fold-search) t))
(advice-add 'phits-mode :after #'case-insensitive-advice)
;;(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)