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

时间:2013-03-11 04:36:39

标签: emacs wolfram-mathematica elisp

**** Stephan的问题解决方案 - 请参阅下面的答案****

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

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

我从(defun math-node()函数引用它,如下所示:

  (set (make-local-variable 'syntax-propertize-function)
       math-syntax-propertize-function)

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

****结束问题的解决方案1 ​​***

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

我的基本功能有效,但有几个问题让我感到麻烦。

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

与许多语言一样,Mathematica使用\字符来转义",其他\字符是字符串。

Mathematic在Mathematica中称为语法字符,如\[Times]\[Element]\[Infinity]等,代表mathematica运算符和常量。

而且,Mathematica大量使用[]代替()进行函数定义和调用等。

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

    If[x < \[Pi], True, False]

当然,cc-mode意图在[之后忽略\。鉴于Mathematica的功能特性,如果它不能与括号匹配,则模式几乎无用。想想没有匹配的lisp。

如果我没有将\作为转义字符放在语法表中,那么如何处理注释和字符串中的转义序列?

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

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

考虑以下代码块:

    FooBar[expression1,
           expression2,
           expression3];

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

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

    FooBar[{expression1,
                expression2,
                expression3}];

结果并不漂亮,因为表达式被认为是{}中单个语句的延续。不幸的是,将c-continuation-offset设置为0的简单方法会破坏实际的延续,例如,

    addMe[x_Real, y_Real] :=
        Plus[x, y];

你想要缩进。

问题在于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)                   

一些formatted code的屏幕截图。

1 个答案:

答案 0 :(得分:5)

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

对于反斜杠问题,最好的办法是使用syntax-propertize-function来更改特定反斜杠的转义性质(在语法表中将其设置为转义,然后将\ [foo]的\标记为非转义,或者在语法表中保留\作为非转义,然后将\“和\”标记为转义。