基于cc模式的Mathematica的Emacs主模式

Ran*_*its 5 emacs wolfram-mathematica elisp

****斯蒂芬的第1期解决方案 - 见下面的答案****

\在语法表中标记为转义字符,但是然后覆盖Mathematica语法元素的指定\[Infinity].这是我的syntax-propertize-function:

(defconst math-syntax-propertize-function
  (syntax-propertize-rules
   ("\\\\\\[\\([A-Z][A-Za-z]*\\)]" (0 "_"))))
Run Code Online (Sandbox Code Playgroud)

我从(defun math-node()函数中引用它是这样的:

  (set (make-local-variable 'syntax-propertize-function)
       math-syntax-propertize-function)
Run Code Online (Sandbox Code Playgroud)

在我的第一次尝试中,我没有使用该make-local-variable功能,当我的elisp缓冲区突出显示出错时,我感到很惊讶.

****第1期****的最终解决方案

我正在实现从cc模式派生的Emacs中的主模式,用于编辑Mathematica文件.目标是语法突出显示和缩进.我将在稍后与Mathematica内核进行接口连接.

我有基本的功能,但有一些困难点给我带来麻烦.

****问题1** - 该\字符用作转义字符,并为多字符,括号内的关键字添加前缀.**

像许多语言一样,Mathematica使用\角色逃脱",其他\角色是字符串.

数学一直是所谓的数学讲语法字符像\[Times],\[Element],\[Infinity]等等,代表运营商数学和常量.

而且,数学大量使用[]替代()对函数定义和调用等

因此,如果我\在语法表中标记为转义字符,那么在使用语法字符的任何地方,我的括号都会出现错误匹配.例如,

    If[x < \[Pi], True, False]
Run Code Online (Sandbox Code Playgroud)

当然,cc模式意图忽略之后的[权利\.鉴于Mathematica的功能特性,如果它不能与括号匹配,则模式几乎无用.认为lisp没有匹配.

如果我没有将\syntax-table作为转义字符放入,那么如何处理注释和字符串中的转义序列?

如果我可以将Times,Element,Infinity等放在关键字列表中并让一切正常工作,那就太好了.

****第2期** - Mathematica的语法与C,C++,Java,ObjC等不同,cc-mode的内置语法分析并不总能产生预期的结果.**

请考虑以下代码块:

    FooBar[expression1,
           expression2,
           expression3];
Run Code Online (Sandbox Code Playgroud)

这种格式很漂亮,因为表达式被识别为参数列表.

但是,如果列表作为参数传递,

    FooBar[{expression1,
                expression2,
                expression3}];
Run Code Online (Sandbox Code Playgroud)

结果并不漂亮,因为表达式被认为是{和中的单个语句的延续}.不幸的是,设置的简单的黑客c-continuation-offset0休息实际延续喜欢,

    addMe[x_Real, y_Real] :=
        Plus[x, y];
Run Code Online (Sandbox Code Playgroud)

你想要缩进.

问题在于Mathematica {}描述列表而不是代码块.

这是我正在使用的当前elisp文件:

(require 'cc-mode)

;; There are required at compile time to get the sources for the                                
;; language constants.                                                                          
(eval-when-compile
  (require 'cc-langs)
  (require 'cc-fonts))

;; Add math mode the the language constant system. This needs to be                             
;; done at compile time because that is when the language constants                             
;; are evaluated.                                                                               
(eval-and-compile
  (c-add-language 'math-mode 'c-mode))


;; Function names                                                                               
(c-lang-defconst c-cpp-matchers
  math (append
        (c-lang-const c-cpp-matchers c)
        ;; Abc[                                                                                 
        '(("\\<\\([A-Z][A-Za-z0-9]*\\)\\>\\[" 1 font-lock-type-face))
        ;; abc[                                                                                 
        '(("\\<\\([A-Za-z][A-Za-z0-9]*\\)\\>\\[" 1 font-lock-function-name-face))
        ;; Abc                                                                                  
        '(("\\<\\([A-Z][A-Za-z0-9]*\\)\\>" 1 font-lock-keyword-face))
        ;; abc_                                                                                 
        '(("\\<\\([a-z][A-Za-z0-9]*[_]\\)\\>" 1 font-lock-variable-name-face))
        ))

;; font-lock-comment-face                                                                       
;; font-lock-doc-face                                                                           
;; font-lock-string-face                                                                        
;; font-lock-keyword-fact                                                                       
;; font-lock-function-name-face                                                                 
;; font-lock-constant-face                                                                      
;; font-lock-type-face                                                                          
;; font-lock-builtin-face                                                                       
;; font-lock-reference-face                                                                     
;; font-lock-warning-face                                                                       


;; There is no line comment character.                                                          
(c-lang-defconst c-line-comment-starter
  math nil)

;; The block comment starter is (*.                                                             
(c-lang-defconst c-block-comment-starter
  math "(*")

;; The block comment ender is *).                                                               
(c-lang-defconst c-block-comment-ender
  math "*)")

;; The assignment operators.                                                                    
(c-lang-defconst c-assignment-operators
  math '("=" ":=" "+=" "-=" "*=" "/=" "->" ":>"))

;; The operators.                                                                               
(c-lang-defconst c-operators
  math `(
         ;; Unary.                                                                              
         (prefix "+" "-" "!")
         ;; Multiplicative.                                                                     
         (left-assoc "*" "/")
         ;; Additive.                                                                           
         (left-assoc "+" "-")
         ;; Relational.                                                                         
         (left-assoc "<" ">" "<=" ">=")
         ;; Equality.                                                                           
         (left-assoc "==" "=!=")  
         ;; Assignment.                                                                         
         (right-assoc ,@(c-lang-const c-assignment-operators))
         ;; Sequence.                                                                           
         (left-assoc ",")))


;; Syntax modifications necessary to recognize keywords with                                    
;; punctuation characters.                                                                      
;; (c-lang-defconst c-identifier-syntax-modifications                                           
;;   math (append '((?\\ . "w"))                                                                
;;             (c-lang-const c-identifier-syntax-modifications)))                               

;; Constants.                                                                                   
(c-lang-defconst c-constant-kwds
  math '( "False" "True" )) ;; "\\[Infinity]" "\\[Times]" "\\[Divide]" "\\[Sqrt]" "\\[Element]"\
))                                                                                              

(defcustom math-font-lock-extra-types nil
  "Extra types to recognize in math mode.")

(defconst math-font-lock-keywords-1 (c-lang-const c-matchers-1 math)
  "Minimal highlighting for math mode.")

(defconst math-font-lock-keywords-2 (c-lang-const c-matchers-2 math)
  "Fast normal highlighting for math mode.")

(defconst math-font-lock-keywords-3 (c-lang-const c-matchers-3 math)
  "Accurate normal highlighting for math mode.")

(defvar math-font-lock-keywords math-font-lock-keywords-3
  "Default expressions to highlight in math mode.")

(defvar math-mode-syntax-table nil
  "Syntax table used in math mode.")

(message "Setting math-mode-syntax-table to nil to force re-initialization")
(setq math-mode-syntax-table nil)

;; If a syntax table has not yet been set, allocate a new syntax table                          
;; and setup the entries.                                                                       
(unless math-mode-syntax-table
  (setq math-mode-syntax-table
        (funcall (c-lang-const c-make-mode-syntax-table math)))

  (message "Modifying the math-mode-syntax-table")

  ;; character (                                                                                
  ;; ( - open paren class                                                                       
  ;; ) - matching paren character                                                               
  ;; 1 - 1st character of comment delimitter (**)                                               
  ;; n - nested comments allowed                                                                
  (modify-syntax-entry ?\( "()1n" math-mode-syntax-table)

  ;; character )                                                                                
  ;; ) - close parent class                                                                     
  ;; ( - matching paren character                                                               
  ;; 4 - 4th character of comment delimitter (**)                                               
  ;; n - nested comments allowed                                                                
  (modify-syntax-entry ?\) ")(4n" math-mode-syntax-table)

  ;; character *                                                                                
  ;; . - punctuation class                                                                      
  ;; 2 - 2nd character of comment delimitter (**)    
  ;; 3 - 3rd character of comment delimitter (**)                                               
  (modify-syntax-entry ?\* ". 23n" math-mode-syntax-table)

  ;; character [                                                                                
  ;; ( - open paren class                                                                       
  ;; ] - matching paren character                                                               
  (modify-syntax-entry ?\[ "(]" math-mode-syntax-table)

  ;; character ]                                                                                
  ;; ) - close paren class                                                                      
  ;; [ - mathcing paren character                                                               
  (modify-syntax-entry ?\] ")[" math-mode-syntax-table)

  ;; character {                                                                                
  ;; ( - open paren class                                                                       
  ;; } - matching paren character                                                               
  (modify-syntax-entry ?\{ "(}" math-mode-syntax-table)

  ;; character }                                                                                
  ;; ) - close paren class                                                                      
  ;; { - matching paren character                                                               
  (modify-syntax-entry ?\} "){" math-mode-syntax-table)

  ;; The following characters are punctuation (i.e. they cannot appear                          
  ;; in identifiers).                                                                           
  ;;                                                                                            
  ;; / ' % & + - ^ < > = |                                                                      
  (modify-syntax-entry ?\/ "." math-mode-syntax-table)
  (modify-syntax-entry ?\' "." math-mode-syntax-table)
  (modify-syntax-entry ?% "." math-mode-syntax-table)
  (modify-syntax-entry ?& "." math-mode-syntax-table)
  (modify-syntax-entry ?+ "." math-mode-syntax-table)
  (modify-syntax-entry ?- "." math-mode-syntax-table)
  (modify-syntax-entry ?^ "." math-mode-syntax-table)
  (modify-syntax-entry ?< "." math-mode-syntax-table)
  (modify-syntax-entry ?= "." math-mode-syntax-table)
  (modify-syntax-entry ?> "." math-mode-syntax-table)
  (modify-syntax-entry ?| "." math-mode-syntax-table)

  ;; character $                                                                                
  ;; _ - word class (since $ is allowed in identifier names)                                    
  (modify-syntax-entry ?\$ "_" math-mode-syntax-table)

  ;; character \                                                                                
  ;; . - punctuation class (for now treat \ as punctuation                                      
  ;;     until we can fix the \[word] issue).                                                   
  (modify-syntax-entry ?\\ "." math-mode-syntax-table)

  ) ;; end of math-mode-syntax-table adjustments                                                

;;                                                                                              
;;                                                                                              
(defvar math-mode-abbrev-table nil
  "Abbrevation table used in math mode buffers.")

(defvar math-mode-map (let ((map (c-make-inherited-keymap)))
                        map)
  "Keymap used in math mode buffers.")

;; math-mode                                                                                    
;;                                                                                              
(defun math-mode ()
  "Major mode for editing Mathematica code."

  (interactive)
  (kill-all-local-variables)

  (c-initialize-cc-mode t)

  (set-syntax-table math-mode-syntax-table)

  (setq major-mode 'math-mode
        mode-name "Math"
        local-abbrev-table math-mode-abbrev-table
        abbrev-mode t)

  (use-local-map math-mode-map)

  (c-init-language-vars math-mode)
  (c-common-init 'math-mode)

  (run-hooks 'c-mode-common-hook)
  (run-hooks 'math-mode-hook)
  (c-update-modeline))

(provide 'math-mode)                   
Run Code Online (Sandbox Code Playgroud)

还有一些截图 格式化代码.

Ste*_*fan 5

虽然cc-mode的设计适用于各种语言,但我不确定它对Mathematica有用,因为语法与cc模式支持的语法相差太远.我建议尝试SMIE(一种出现在Emacs-23.4中的缩进引擎,它最初是为SML构建的,但目前用于各种语言).就像cc模式一样,SMIE也不适合所有语言,但如果它在你的情况下比cc模式更好,我也不会感到惊讶.

对于反斜杠问题,最好的办法是使用syntax-propertize-function更改特定反斜杠的转义性质(在语法表中设置\作为转义,然后将\ [foo]的\标记为非转义,或留下\作为语法表中的非转义,然后将那些\"和\\标记为转义).