Release coccinelle-0.2.0rc1
[bpt/coccinelle.git] / editors / emacs / cocci.el
index fbf1a71..a655cc2 100644 (file)
@@ -2,15 +2,15 @@
 
 ;; Copyright (C) 2006-2007 Yoann Padioleau
 
-;; Please imagine a long and boring gnu-style copyright notice 
+;; Please imagine a long and boring gnu-style copyright notice
 ;; appearing just here.
 
 
 ;; Emacs Lisp Archive Entry
-;; Author: Padioleau Yoann <yoann.padioleau@gmail.com>, 
+;; Author: Padioleau Yoann <yoann.padioleau@gmail.com>,
 ;; Version: 0.2
 ;; Keywords: coccinelle patch refactoring program transformation
-;; URL: http://www.emn.fr/x-info/coccinelle/
+;; URL: http://coccinelle.lip6.fr/
 
 
 ;;; Usage
 ;;
 ;; Add the following lines to your ~/.emacs or equivalent:
 ;;  (load "~/.emacs.d/cocci.el")
-;;  (setq auto-mode-alist 
+;;  (setq auto-mode-alist
 ;;          (cons '("\\.cocci$" . cocci-mode) auto-mode-alist))
-;;  (autoload 'cocci-mode "cocci" 
+;;  (autoload 'cocci-mode "cocci"
 ;;          "Major mode for editing cocci code." t)
 ;;
-;; You can also use cocci-mode to edit the files containing the 
-;; isomorphisms with: 
-;;  (setq auto-mode-alist 
+;; You can also use cocci-mode to edit the files containing the
+;; isomorphisms with:
+;;  (setq auto-mode-alist
 ;;           (cons '("\\.iso$" . cocci-mode) auto-mode-alist))
 ;;
 
-;;; History 
+;;; History
 
+;; 2009-11-05 Nico: Cleanups, Change shortcut % to C-M-% (% is used in Python rule)
 ;; Some cleanups done by Rene Rydhof Hansen
 
-;;; Utilities 
+;;; Utilities
 
-(defun join-sep (sep xs) 
+(defun join-sep (sep xs)
   (mapconcat 'identity xs sep))
 
 
-;;; Variables 
+;;; Variables
 
 (defvar cocci-menu)
 
@@ -90,8 +91,8 @@
 
 ;; can look in lexer_cocci.mll for new identifiers
 
-(defconst cocci-c-keywords-list 
-  '("if" "else" "while" "do" "for" "return" 
+(defconst cocci-c-keywords-list
+  '("if" "else" "while" "do" "for" "return"
     "sizeof"
     "struct" "union"
     "static" "extern" "const" "volatile"
     "switch" "case"
     ))
 
-(defconst cocci-declaration-keywords-list 
+(defconst cocci-declaration-keywords-list
   '("identifier" "type" "parameter" "constant" "expression" "statement"
-    "function" "local" "list" 
-    "fresh" 
+    "function" "local" "list"
+    "fresh"
     "position"
     "idexpression"
 
     "context"
 
-    "typedef" 
+    "typedef"
     "declarer" "iterator"
     "pure"
     ;"error" "words"
-    
-    "char" "short" "int" "float" "double" "long" 
+
+    "char" "short" "int" "float" "double" "long"
     "void"
-    "signed" "unsigned" 
+    "signed" "unsigned"
     ))
 
 (defconst cocci-iso-keywords-list
-  '("Expression" "Statement" "Type" 
+  '("Expression" "Statement" "Type"
     "Declaration" "TopLevel" "ArgExpression"
     ))
 
   '("define" "undef"
     "if" "ifdef" "elif" "else" "endif" "ifndef"
     "include"
-    "error" "pragma" 
-    "file" "line" 
+    "error" "pragma"
+    "file" "line"
     ))
 
-      
-(setq cocci-font-lock-keywords 
+(setq cocci-font-lock-keywords
  `(
 
    ; blink possible errors, when - or + is not in first column
    ; --- +++
 
    ; #cpp
-   ("#\\(include\\) *\\(.*\\)" 
+   ("#\\(include\\) *\\(.*\\)"
     (1 'font-lock-builtin-face)
     (2 'font-lock-string-face)
     )
    ("\"[^\"]*\"" . 'font-lock-string-face)
 
    ; rule header
-   ("@[ \t]*@" . 'cocci-special-face) 
-   ; this rule may seems redundant with the following one, but 
+   ("@[ \t]*@" . 'cocci-special-face)
+   ; this rule may seems redundant with the following one, but
    ; without it, @@ int x; @@ would color the int x with rulename-face.
-   ; by using this rule, we color the @@ and so prevent the 
+   ; by using this rule, we color the @@ and so prevent the
    ; next rule to be applied (cf font-lock semantic when have not the
    ; OVERRIDE flag).
 
-   ("\\(@\\)\\(.*\\)\\(@\\)" 
+   ("\\(@\\)\\(.*\\)\\(@\\)"
     (1 'cocci-special-face)
     (2 'cocci-rulename-face)
     (3 'cocci-special-face)
 
    ;old: does not work, not easy to handle the   rule1, rule2, rule3  list.
    ;   ("@[ \t]*\\(\\(\\w+\\)[ \t,]*\\)*[ \t]*@"
-   ;   ("\\(@\\)[ \t]*\\(\\w+\\)[ \t]*\\(@\\)" 
-   ;   ("\\(@\\)[ \t]*\\(\\w+\\)[ \t]+\\(extends\\)[ \t]+\\(\\w+\\)[ \t]*\\(@\\)" 
-   ;   ("\\(@\\)[ \t]*\\(\\w+\\)[ \t]+\\(depends\\)[ \t]+\\(on\\)[ \t]+\\(\\(\\w+\\)[ ,\t]*\\)+\\(@\\)" 
+   ;   ("\\(@\\)[ \t]*\\(\\w+\\)[ \t]*\\(@\\)"
+   ;   ("\\(@\\)[ \t]*\\(\\w+\\)[ \t]+\\(extends\\)[ \t]+\\(\\w+\\)[ \t]*\\(@\\)"
+   ;   ("\\(@\\)[ \t]*\\(\\w+\\)[ \t]+\\(depends\\)[ \t]+\\(on\\)[ \t]+\\(\\(\\w+\\)[ ,\t]*\\)+\\(@\\)"
 
 
    ; inherited variable, fontifying rulename
       ".*?\\(\\w+\\)\\.")
     (2 'cocci-rulename-face))
 
-   ;rule1.T *a; 
+   ;rule1.T *a;
    ("^\\(\\w+\\)\\."
     (1 'cocci-rulename-face))
-    
 
    ; just for pad, metavariables in maj
    ("\\b[A-Z][0-9]?\\b" . font-lock-variable-name-face)
    ("\\bWHEN[ \t]+!=" . 'font-lock-keyword-face)
    ("\\bwhen[ \t]+=" . 'font-lock-keyword-face)
    ("\\bWHEN[ \t]+=" . 'font-lock-keyword-face)
-   
+
    ; used in iso files
    ("<=>" . 'font-lock-keyword-face)
    ("=>" . 'font-lock-keyword-face)
 
-   (,(concat "\\b\\(" (regexp-opt cocci-iso-keywords-list) "\\)\\b") . 
+   (,(concat "\\b\\(" (regexp-opt cocci-iso-keywords-list) "\\)\\b") .
      'cocci-special-face)
 
    ("\\<[0-9]+\\>" . 'cocci-number-face)
 
-   (,(join-sep "\\|" 
+   (,(join-sep "\\|"
         (list "(" ")" ";" "," "{" "}" "\\[" "\\]")) .  'cocci-punctuation-face)
    ; . ->   * + etc
 
    ; c keywords
-   (,(concat "\\b\\(" (regexp-opt cocci-c-keywords-list) "\\)\\b") . 
+   (,(concat "\\b\\(" (regexp-opt cocci-c-keywords-list) "\\)\\b") .
      'font-lock-keyword-face)
 
    ; cocci declaration keywords
-   (,(concat "\\b\\(" (regexp-opt cocci-declaration-keywords-list) "\\)\\b") . 
+   (,(concat "\\b\\(" (regexp-opt cocci-declaration-keywords-list) "\\)\\b") .
       'font-lock-type-face)
 
    ; cpp directives
 ;  "Expressions to highlight in cocci-mode.")
 
 
-    
 ;; define a mode-specific abbrev table for those who use such things
 (defvar cocci-mode-abbrev-table nil
   "Abbrev table used while in cocci mode.")
   (setq cocci-mode-map (make-sparse-keymap))
   (define-key cocci-mode-map [(meta control *)] 'switch-between-cocci-c)
   (define-key cocci-mode-map "%" 'cocci-replace-modifiers)
-  
+
   ;(define-key cocci-mode-map "\C-c" 'compile)
   )
 
 
-(defvar cocci-mode-syntax-table nil 
+(defvar cocci-mode-syntax-table nil
   "Syntax table used while in cocci mode.")
 (unless cocci-mode-syntax-table
   (setq cocci-mode-syntax-table (make-syntax-table))
 
   ; _ is part of a word.
-  (modify-syntax-entry ?\_ "w"  cocci-mode-syntax-table) 
+  (modify-syntax-entry ?\_ "w"  cocci-mode-syntax-table)
 
   ; change mode for ", bad interaction with font-lock
-  (modify-syntax-entry ?\" "w"  cocci-mode-syntax-table) 
+  (modify-syntax-entry ?\" "w"  cocci-mode-syntax-table)
   )
 
 
 ;;; Code
 
-;; helper functions for the cocci programmer 
+;; helper functions for the cocci programmer
 
 (defun cocci-replace-modifiers (beg end str)
   "TODO"
   (interactive
-   (let ((str (read-string "New modifier string (+, -, space): " 
+   (let ((str (read-string "New modifier string (+, -, space): "
                            nil 'my-history)))
      (list (region-beginning) (region-end) str)))
 
 (defun switch-between-cocci-c ()
   (interactive)
   (let ((target
-         (cond ((string-match ".c$" (buffer-name)) 
+         (cond ((string-match ".c$" (buffer-name))
                 (replace-match ".cocci" t t (buffer-name)))
-               ((string-match ".cocci$" (buffer-name)) 
+               ((string-match ".cocci$" (buffer-name))
                 (replace-match ".c" t t (buffer-name)))
                (t
                 "none"))))
     (if (get-buffer target)
        (switch-to-buffer target)
-      (find-file 
+      (find-file
        (read-file-name "file: " nil nil t target)))))
 
 (eval-after-load "cc-mode"
 
 
 
-               
 (defvar cocci-mode-hook nil
   "Hook called by  `cocci-mode'")
 
@@ -346,7 +343,7 @@ Turning on cocci-mode runs the hook `cocci-mode-hook'."
 )
 
 
-;; Menu 
+;; Menu
 
 (easy-menu-define cocci-menu cocci-mode-map "Cocci menu"
   '("Cocci"