emacs:如何编写类似于Algol的语言的语法色彩模式,该语言使用关键字stropping
我正在尝试为Imp设置语法着色,IMP是一种使用关键字划线的类似Algol的语言,即关键字是' %
''cartem oftherphabetics to任何非α,例如 %开始
。我发现 modify-syntax-entry 足以为IMP注释着色,但是我不得不使用Hi-Lock来颜色关键字。我喜欢的帮助是:如何从我的〜/.emacs 文件中加载hi-lock的正则是。每个单独的.IMP文件中的规则?到目前为止,这是我所拥有的:
(defconst my-imp-mode-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?' "\"" table)
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?! "<" table)
(modify-syntax-entry ?\n ">" table)
(modify-syntax-entry ?{ "<" table)
(modify-syntax-entry ?} ">" table)
(modify-syntax-entry ?\( "()" table)
(modify-syntax-entry ?\) ")(" table)
table))
(define-derived-mode my-imp-mode prog-mode "Simple Imp Mode"
:syntax-table my-imp-mode-syntax-table
(font-lock-fontify-buffer))
(add-to-list 'auto-mode-alist '("\\.i\\'" . my-imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp\\'" . my-imp-mode))
(global-hi-lock-mode 1)
(setq hi-lock-file-patterns-policy (lambda (pattern) t))
(defface imp-keyword
'((t (:weight bold :foreground "cyan")))
"Face for IMP keywords"
:group 'hi-lock-faces)
(defface imp-constant
'((t (:foreground "green")))
"Face for IMP numeric constants"
:group 'hi-lock-faces)
;; I would like to load these patterns on opening a .imp file:
;; (("\\%[A-Za-z]*" (0 (quote imp-keyword) prepend)))
;; (("\\<[\\-+]*[0-9]*\\.?[0-9]+\\(\\|@[\\-+]?[0-9]+\\)?\\>" (0 (quote imp-constant) prepend)))
这是一个IMP文件的示例:
! Hi-lock: (("\\%[A-Za-z]*" (0 (quote imp-keyword) prepend)))
! Hi-lock: (("\\<[\\-]?[0-9]*\\.?[0-9]+\\(\\@[\\-+]?[0-9]+\\)?\\>" (0 (quote imp-constant) prepend)))
%begin
! ackerman function - this is a whole-line comment
%integer x,y,j,k
%integerfn acker {short for ackerman - this is a bracketed comment by the way} (%integer m,n)
%if m = 0 %then %result = n+1
%if n = 0 %then %result = acker(m-1,1)
%result = acker(m-1, acker(m, n-1))
%end
prompt("Ackerman, First param (1..4)?"); read(x)
prompt(" Second param (1..7)?"); read(y)
write(acker(x,y), 4); newline
%endofprogram
通过从上方应用ELISP,您可以看到预期的突出显示的样子。 (着色的细节只是草稿,直到我弄清楚如何做。我可以调整颜色等)
I'm trying to set up syntax colouring for Imp, an Algol-like language that uses keyword stropping, i.e. a keyword is the '%
' character followed by alphabetics up to any non-alpha, such as %begin
. I found that modify-syntax-entry was sufficient to colour Imp comments, but I had to use Hi-lock to colour keywords. What I'ld like help with is: how can I load the regexps for hi-lock from my ~/.emacs file when opening any file with a .imp extension, rather than having to explicitly save the rules in every individual .imp file? Here's what I have so far:
(defconst my-imp-mode-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?' "\"" table)
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?! "<" table)
(modify-syntax-entry ?\n ">" table)
(modify-syntax-entry ?{ "<" table)
(modify-syntax-entry ?} ">" table)
(modify-syntax-entry ?\( "()" table)
(modify-syntax-entry ?\) ")(" table)
table))
(define-derived-mode my-imp-mode prog-mode "Simple Imp Mode"
:syntax-table my-imp-mode-syntax-table
(font-lock-fontify-buffer))
(add-to-list 'auto-mode-alist '("\\.i\\'" . my-imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp\\'" . my-imp-mode))
(global-hi-lock-mode 1)
(setq hi-lock-file-patterns-policy (lambda (pattern) t))
(defface imp-keyword
'((t (:weight bold :foreground "cyan")))
"Face for IMP keywords"
:group 'hi-lock-faces)
(defface imp-constant
'((t (:foreground "green")))
"Face for IMP numeric constants"
:group 'hi-lock-faces)
;; I would like to load these patterns on opening a .imp file:
;; (("\\%[A-Za-z]*" (0 (quote imp-keyword) prepend)))
;; (("\\<[\\-+]*[0-9]*\\.?[0-9]+\\(\\|@[\\-+]?[0-9]+\\)?\\>" (0 (quote imp-constant) prepend)))
Here is an example of an Imp file:
! Hi-lock: (("\\%[A-Za-z]*" (0 (quote imp-keyword) prepend)))
! Hi-lock: (("\\<[\\-]?[0-9]*\\.?[0-9]+\\(\\@[\\-+]?[0-9]+\\)?\\>" (0 (quote imp-constant) prepend)))
%begin
! ackerman function - this is a whole-line comment
%integer x,y,j,k
%integerfn acker {short for ackerman - this is a bracketed comment by the way} (%integer m,n)
%if m = 0 %then %result = n+1
%if n = 0 %then %result = acker(m-1,1)
%result = acker(m-1, acker(m, n-1))
%end
prompt("Ackerman, First param (1..4)?"); read(x)
prompt(" Second param (1..7)?"); read(y)
write(acker(x,y), 4); newline
%endofprogram
You can see what the expected highlighting looks like by applying the elisp from above. (The details of the colouring are just a draft until I work out how to do it. I can tweak the colours etc later)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要HI -Lock模块 - 可以使用标准语法着色机制为新语言编写语法着色,如下所示:
基于 http://xahlee.info/emacs/emacs/emacs/elisp_syntax_coloring.html
The Hi-lock module was not required - it is possible to write syntax colouring for a new language using the standard syntax colouring mechanism, as follows:
Based on the info from http://xahlee.info/emacs/emacs/elisp_syntax_coloring.html