3 ;; Load python-mode if available, otherwise use builtin emacs python package
4 (when (not(require 'python-mode nil t))
7 (add-to-list 'auto-mode-alist '("\\.pyx\\'" . cython-mode))
8 (add-to-list 'auto-mode-alist '("\\.pxd\\'" . cython-mode))
9 (add-to-list 'auto-mode-alist '("\\.pxi\\'" . cython-mode))
12 (defun cython-compile ()
13 "Compile the file via Cython."
15 (let ((cy-buffer (current-buffer)))
17 (compile compile-command)
18 (set (make-local-variable 'cython-buffer) cy-buffer)
19 (add-to-list (make-local-variable 'compilation-finish-functions)
20 'cython-compilation-finish)))
23 (defun cython-compilation-finish (buffer how)
24 "Called when Cython compilation finishes."
25 ;; XXX could annotate source here
28 (defvar cython-mode-map
29 (let ((map (make-sparse-keymap)))
30 ;; Will inherit from `python-mode-map' thanks to define-derived-mode.
31 (define-key map "\C-c\C-c" 'cython-compile)
33 "Keymap used in `cython-mode'.")
35 (defvar cython-font-lock-keywords
36 `(;; new keywords in Cython language
37 (,(regexp-opt '("by" "cdef" "cimport" "cpdef" "ctypedef" "enum" "except?"
38 "extern" "gil" "include" "nogil" "property" "public"
39 "readonly" "struct" "union" "DEF" "IF" "ELIF" "ELSE") 'words)
40 1 font-lock-keyword-face)
41 ;; C and Python types (highlight as builtins)
42 (,(regexp-opt '("NULL" "bint" "char" "dict" "double" "float" "int" "list"
43 "long" "object" "Py_ssize_t" "short" "size_t" "void") 'words)
44 1 font-lock-builtin-face)
45 ;; cdef is used for more than functions, so simply highlighting the next
46 ;; word is problematic. struct, enum and property work though.
47 ("\\<\\(?:struct\\|enum\\)[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
49 ("\\<property[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
50 1 font-lock-function-name-face))
51 "Additional font lock keywords for Cython mode.")
53 (define-derived-mode cython-mode python-mode "Cython"
54 "Major mode for Cython development, derived from Python mode.
57 (setcar font-lock-defaults
58 (append python-font-lock-keywords cython-font-lock-keywords))
59 (set (make-local-variable 'compile-command)
60 (concat "cython -a " buffer-file-name))
61 (add-to-list (make-local-variable 'compilation-finish-functions)
62 'cython-compilation-finish)
65 (provide 'cython-mode)