首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EMACS:如何为使用关键字选择的类似Algol的语言编写语法着色模式

EMACS:如何为使用关键字选择的类似Algol的语言编写语法着色模式
EN

Stack Overflow用户
提问于 2022-06-19 07:54:29
回答 1查看 51关注 0票数 0

我正在尝试为Imp设置语法着色,Imp是一种类似Algol的语言,使用关键字行,即关键字是'%‘字符,后面跟着字母,直到任何非alpha字符,如%begin.。我发现修改-语法条目足以为Imp注释着色,但我必须使用Hi来显示关键字。我喜欢帮助的是:在打开任何扩展名为.imp的文件时,我如何从~/..emacs文件加载解锁的regexp,而不是在每个单独的.imp文件中显式保存规则?到目前为止,我的情况如下:

代码语言:javascript
复制
(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文件的示例:

代码语言:javascript
复制
! 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,您可以看到预期的高亮显示是什么样子。(着色的细节只是一份草稿,直到我想出如何做为止。我以后可以调整颜色等)

EN

回答 1

Stack Overflow用户

发布于 2022-06-19 13:41:45

不需要Hi模块--可以使用标准语法着色机制为新语言编写语法着色,如下所示:

代码语言:javascript
复制
(defconst imp-mode-syntax-table
  (let ((table (make-syntax-table)))
    ;; turn off ' and " from being string delimiters
    (modify-syntax-entry ?'  "-" table)
    (modify-syntax-entry ?\" "-" table)
    table))

;; Highlight comments, %stropped keywords, and numbers:
(setq imp-highlights
      '(
        ;; many forms of comments including after labels
        (";[ ]*!" ".*"      nil nil (0 font-lock-comment-face))
        ("^[ ]*!" ".*;"     nil nil (0 font-lock-comment-face))
        ("^[ ]*!" ".*"      nil nil (0 font-lock-comment-face))
        ("%c\\([ ]+[%]+\\|[%]*\\)*o\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*e\\([ ]+[%]+\\|[%]*\\)*n\\([ ]+[%]+\\|[%]*\\)*t" ".*;"   nil nil
(0 font-lock-comment-face))
        ("%c\\([ ]+[%]+\\|[%]*\\)*o\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*e\\([ ]+[%]+\\|[%]*\\)*n\\([ ]+[%]+\\|[%]*\\)*t" "[^$]*" nil nil
(0 font-lock-comment-face))
        (":[ ]*!" "[^$]*"   nil nil (0 font-lock-comment-face))
        ("{[^}]*}"                  . 'font-lock-comment-face )
        ("{.*$"                     . 'font-lock-comment-face )
        ;; char constants, old-style strings, and old-style based constants
        ("[MBOXER]?'[^']*'"          . 'font-lock-constant-face)
        ("\"[^\"]*\""               . 'font-lock-string-face  )
        ;; Based IMP constants
        ("\\<[0-9][ ]*[0-9 ]*[ ]*_[ ]*[0-9A-Fa-f][0-9A-Fa-f ]*\\>" . 'font-lock-constant-face)
        ;; Decimal IMP constants
        ("\\<[0-9][0-9 ]*\\.[ ]*[0-9][0-9 ]*@[ ]*[-+]?[ ]*[0-9][0-9 ]*\\>" . 'font-lock-constant-face)
        ("\\<[0-9][0-9 ]*\\.[ ]*[0-9][0-9 ]*\\>" . 'font-lock-constant-face)
        ;; Integer IMP constants
        ("\\<[0-9][ ]*[0-9][0-9 ]*\\>" . 'font-lock-constant-face)
        ("\\<[0-9][ ]*\\>" . 'font-lock-constant-face)
        ;; stropped keywords
        ("\\%[A-Za-z][A-Za-z]*"       . 'font-lock-keyword-face )
        ))

(define-derived-mode imp-mode prog-mode "Simple Imp Mode"
   :syntax-table imp-mode-syntax-table
  (setq font-lock-defaults '(imp-highlights))
  (font-lock-fontify-buffer)
  )

;; Apply automatically to Imp source files:
(add-to-list 'auto-mode-alist '("\\.i\\'" . imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp\\'" . imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp77\\'" . imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp80\\'" . imp-mode))

;; enter <esc>xfun<cr> to disable imp-mode if unwanted during an edit.

基于http://xahlee.info/emacs/emacs/elisp_syntax_coloring.html的信息

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72675185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档