new Emacs cython-mode by Georg Brandl
authorStefan Behnel <scoder@users.berlios.de>
Wed, 17 Feb 2010 21:00:30 +0000 (22:00 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Wed, 17 Feb 2010 21:00:30 +0000 (22:00 +0100)
Tools/cython-mode.el

index ae1af7f3bf3d51fda68f3be6d6c5255a7531d8a9..53487da16c41118d4b051ab138666e028d50bde9 100644 (file)
@@ -1 +1,63 @@
-;;;; `Cython' mode.\r\r(add-to-list 'auto-mode-alist '("\\.pyx\\'" . cython-mode))\r\r(define-derived-mode cython-mode python-mode "Cython"\r  (font-lock-add-keywords\r   nil\r   `((,(concat "\\<\\(NULL"\r         "\\|c\\(def\\|har\\|typedef\\)"\r        "\\|e\\(num\\|xtern\\)"\r        "\\|float"\r             "\\|in\\(clude\\|t\\)"\r         "\\|object\\|public\\|struct\\|type\\|union\\|void"\r            "\\)\\>")\r      1 font-lock-keyword-face t))))\r
\ No newline at end of file
+;; Cython mode
+
+(require 'python-mode)
+
+(add-to-list 'auto-mode-alist '("\\.pyx\\'" . cython-mode))
+(add-to-list 'auto-mode-alist '("\\.pxd\\'" . cython-mode))
+(add-to-list 'auto-mode-alist '("\\.pxi\\'" . cython-mode))
+
+
+(defun cython-compile ()
+  "Compile the file via Cython."
+  (interactive)
+  (let ((cy-buffer (current-buffer)))
+    (with-current-buffer
+        (compile compile-command)
+      (set (make-local-variable 'cython-buffer) cy-buffer)
+      (add-to-list (make-local-variable 'compilation-finish-functions)
+                   'cython-compilation-finish)))
+  )
+
+(defun cython-compilation-finish (buffer how)
+  "Called when Cython compilation finishes."
+  ;; XXX could annotate source here
+  )
+
+(defvar cython-mode-map
+  (let ((map (make-sparse-keymap)))
+    ;; Will inherit from `python-mode-map' thanks to define-derived-mode.
+    (define-key map "\C-c\C-c" 'cython-compile)
+    map)
+  "Keymap used in `cython-mode'.")
+
+(defvar cython-font-lock-keywords
+  `(;; new keywords in Cython language
+    (,(regexp-opt '("by" "cdef" "cimport" "cpdef" "ctypedef" "enum" "except?"
+                    "extern" "gil" "include" "nogil" "property" "public"
+                    "readonly" "struct" "union" "DEF" "IF" "ELIF" "ELSE") 'words)
+     1 font-lock-keyword-face)
+    ;; C and Python types (highlight as builtins)
+    (,(regexp-opt '("NULL" "bint" "char" "dict" "double" "float" "int" "list"
+                    "long" "object" "Py_ssize_t" "short" "size_t" "void") 'words)
+     1 font-lock-builtin-face)
+    ;; cdef is used for more than functions, so simply highlighting the next
+    ;; word is problematic. struct, enum and property work though.
+    ("\\<\\(?:struct\\|enum\\)[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
+     1 py-class-name-face)
+    ("\\<property[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
+     1 font-lock-function-name-face))
+  "Additional font lock keywords for Cython mode.")
+
+(define-derived-mode cython-mode python-mode "Cython"
+  "Major mode for Cython development, derived from Python mode.
+
+\\{cython-mode-map}"
+  (setcar font-lock-defaults
+          (append python-font-lock-keywords cython-font-lock-keywords))
+  (set (make-local-variable 'compile-command)
+       (concat "cython -a " buffer-file-name))
+  (add-to-list (make-local-variable 'compilation-finish-functions)
+               'cython-compilation-finish)
+)
+
+(provide 'cython-mode)