[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, master, updated. 9b36af07eec36e6cf5029070dc23676544a7944f

Micah Anderson micah at riseup.net
Wed May 7 16:20:13 UTC 2008


The following commit has been merged in the master branch:
commit 1e0ea70001b8723ab51f44d1e8404373ba3db89d
Author: Russ Allbery <rra at debian.org>
Date:   Fri May 2 20:37:38 2008 -0700

    Enhancements to syntax highlighting and indentation for Emacs
    
    More correctly handle multiple resources in a block, such as:
    
    type {
        "name":
            attr => foo,
            oattr => bar;
        "other":
            attr => baz;
    }
    
    Handle continued arguments in parentheses, such as define or realize
    arguments.  Try to be a bit more robust about indenting in general.
    
    Add the remaining Puppet language keywords (I think) and improve the
    handling of type references at the top level.  Remove the hack for
    continued include lines and fix it properly.
    
    Remove more non-Puppet code left over from the mode this one was
    based on.  Use a more straightforward method of setting up the font-lock
    keywords and do it the same way for both XEmacs and Emacs since I think
    they can both handle the current method (and other modes agree).

diff --git a/ext/emacs/puppet-mode.el b/ext/emacs/puppet-mode.el
index 56a2b3a..a6d47ee 100644
--- a/ext/emacs/puppet-mode.el
+++ b/ext/emacs/puppet-mode.el
@@ -2,10 +2,11 @@
 ;;; puppet-mode.el
 ;;; 
 ;;; Author: lutter
-;;; Description: A simple mode for editing puppet manifests
+;;; Author: Russ Allbery <rra at stanford.edu>
 ;;;
+;;; Description: A simple mode for editing puppet manifests
 
-(defconst puppet-mode-version "0.1")
+(defconst puppet-mode-version "0.2")
 
 (defvar puppet-mode-abbrev-table nil
   "Abbrev table in use in puppet-mode buffers.")
@@ -68,8 +69,24 @@ of Emacs."
 (defun puppet-comment-line-p ()
   "Return non-nil iff this line is a comment."
   (save-excursion
-    (beginning-of-line)
-    (looking-at (format "\\s-*%s" comment-start))))
+    (save-match-data
+      (beginning-of-line)
+      (looking-at (format "\\s-*%s" comment-start)))))
+
+(defun puppet-block-indent ()
+  "If point is in a block, return the indentation of the first line of that
+block (the line containing the opening brace).  Used to set the indentation
+of the closing brace of a block."
+  (save-excursion
+    (save-match-data
+      (let ((opoint (point))
+            (apoint (search-backward "{" nil t)))
+        (when apoint
+          ;; This is a bit of a hack and doesn't allow for strings.  We really
+          ;; want to parse by sexps at some point.
+          (if (= (puppet-count-matches "}" apoint opoint) 0)
+              (current-indentation)
+            nil))))))
 
 (defun puppet-in-array ()
   "If point is in an array, return the position of the opening '[' of
@@ -99,15 +116,14 @@ of the initial include plus puppet-include-indent."
         (while not-found
           (forward-line -1)
           (cond
-             ((puppet-comment-line-p)
-              (if (bobp)
-                  (setq not-found nil)))
-             ((looking-at "^\\s-*include\\s-+.*,\\s-*$")
-              (setq include-column
-                    (+ (current-indentation) puppet-include-indent))
-              (setq not-found nil))
-             ((not (looking-at ".*,\\s-*$"))
-              (setq not-found nil))))
+           ((bobp)
+            (setq not-found nil))
+           ((looking-at "^\\s-*include\\s-+.*,\\s-*$")
+            (setq include-column
+                  (+ (current-indentation) puppet-include-indent))
+            (setq not-found nil))
+           ((not (looking-at ".*,\\s-*$"))
+            (setq not-found nil))))
         include-column))))
 
 (defun puppet-indent-line ()
@@ -119,6 +135,7 @@ of the initial include plus puppet-include-indent."
     (let ((not-indented t)
           (array-start (puppet-in-array))
           (include-start (puppet-in-include))
+          (block-indent (puppet-block-indent))
           cur-indent)
       (cond
        (array-start
@@ -155,18 +172,12 @@ of the initial include plus puppet-include-indent."
           (setq cur-indent (current-column))))
        (include-start
         (setq cur-indent include-start))
-       ((looking-at "^[^{\n]*}")
-        ;; This line contains the end of a block, but the block does
-        ;; not also begin on this line, so decrease the indentation.
-        (save-excursion
-          (forward-line -1)
-          (if (looking-at "^.*}")
-              (progn
-                (setq cur-indent (- (current-indentation) puppet-indent-level))
-                (setq not-indented nil))
-            (setq cur-indent (- (current-indentation) puppet-indent-level))))
-        (if (< cur-indent 0)     ; We can't indent past the left margin
-            (setq cur-indent 0)))
+       ((looking-at "^\\s-*}\\s-*$")
+        ;; This line contains only a closing brace, so we should indent it
+        ;; matching the indentation of the opening brace of the block.
+        (if block-indent
+            (setq cur-indent block-indent)
+          (setq cur-indent 0)))
        (t
         ;; Otherwise, we did not start on a block-ending-only line.
         (save-excursion
@@ -174,30 +185,136 @@ of the initial include plus puppet-include-indent."
           (while not-indented
             (forward-line -1)
             (cond
+             ;; Comment lines are ignored unless we're at the start of the
+             ;; buffer.
              ((puppet-comment-line-p)
               (if (bobp)
-                  (setq not-indented nil)
-                ;; else ignore the line and continue iterating backwards
-                ))
-             ((looking-at "^.*}") ; indent at the level of the END_ token
+                  (setq not-indented nil)))
+
+             ;; Brace or paren on a line by itself will already be indented to
+             ;; the right level, so we can cheat and stop there.
+             ((looking-at "^\\s-*[\)}]\\s-*")
               (setq cur-indent (current-indentation))
               (setq not-indented nil))
-             ((looking-at "^.*{") ; indent an extra level
+
+             ;; Brace or paren not on a line by itself will be indented one
+             ;; level too much, but don't catch cases where the block is
+             ;; started and closed on the same line.
+             ((looking-at "^[^\({]*[\)}]\\s-*$")
+              (setq cur-indent (- (current-indentation) puppet-indent-level))
+              (setq not-indented nil))
+
+             ;; Indent by one level more than the start of our block.  We lose
+             ;; if there is more than one block opened and closed on the same
+             ;; line but it's still unbalanced; hopefully people don't do that.
+             ((looking-at "^.*{[^}]*$")
+              (setq cur-indent (+ (current-indentation) puppet-indent-level)) 
+              (setq not-indented nil))
+
+             ;; Indent by one level if the line ends with an open paren.
+             ((looking-at "^.*\(\\s-*$")
               (setq cur-indent (+ (current-indentation) puppet-indent-level)) 
               (setq not-indented nil))
-             ((looking-at "^.*;\\s-*$") ; Semicolon ends a nested resource
+
+             ;; Semicolon ends a block for a resource when multiple resources
+             ;; are defined in the same block, but try not to get the case of
+             ;; a complete resource on a single line wrong.
+             ((looking-at "^\\([^'\":\n]\\|\"[^\"]*\"\\|'[^']'\\)**;\\s-*$")
               (setq cur-indent (- (current-indentation) puppet-indent-level))
               (setq not-indented nil))
-             ((looking-at "^.*:\\s-*$") ; indent an extra level after :
+
+             ;; Indent an extra level after : since it introduces a resource.
+             ((looking-at "^.*:\\s-*$")
               (setq cur-indent (+ (current-indentation) puppet-indent-level))
               (setq not-indented nil))
+
+             ;; Start of buffer.
              ((bobp)
-              (setq not-indented nil))
-             )))))
-      (if cur-indent
+              (setq not-indented nil)))))
+
+        ;; If this line contains only a closing paren, we should lose one
+        ;; level of indentation.
+        (if (looking-at "^\\s-*\)\\s-*$")
+            (setq cur-indent (- cur-indent puppet-indent-level)))))
+
+      ;; We've figured out the indentation, so do it.
+      (if (and cur-indent (> cur-indent 0))
           (indent-line-to cur-indent)
         (indent-line-to 0)))))
 
+(defvar puppet-font-lock-syntax-table
+  (let* ((tbl (copy-syntax-table puppet-mode-syntax-table)))
+    (modify-syntax-entry ?_ "w" tbl)
+    tbl))
+
+(defvar puppet-font-lock-keywords
+  (list
+   ;; defines, classes, and nodes
+   '("^\\s *\\(class\\|define\\|node\\)\\s +\\([^( \t\n]+\\)"
+     2 font-lock-function-name-face)
+   ;; inheritence
+   '("\\s +inherits\\s +\\([^( \t\n]+\\)"
+     1 font-lock-function-name-face)
+   ;; include
+   '("\\(^\\|\\s +\\)include\\s +\\(\\([a-zA-Z0-9:_-]+\\(,[ \t\n]*\\)?\\)+\\)"
+     2 font-lock-reference-face)
+   ;; keywords
+   (cons (concat
+          "\\b\\(\\("
+          (mapconcat
+           'identity
+           '("alert"
+             "case"
+             "class"
+             "crit"
+             "debug"
+             "default"
+             "define"
+             "defined"
+             "else"
+             "emerg"
+             "err"
+             "fail"
+             "false"
+             "file"
+             "filebucket"
+             "generate"
+             "if"
+             "import"
+             "include"
+             "info"
+             "inherits"
+             "node"
+             "notice"
+             "realize"
+             "search"
+             "tag"
+             "tagged"
+             "template"
+             "true"
+             "warning"
+             )
+           "\\|")
+          "\\)\\>\\)")
+         1)
+     ;; variables
+     '("\\(^\\|[^_:.@$]\\)\\b\\(true\\|false\\)\\>"
+       2 font-lock-variable-name-face)
+     ;; variables
+     '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
+       1 font-lock-variable-name-face)
+     '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\|:\\)+"
+       0 font-lock-variable-name-face)
+     ;; usage of types
+     '("^\\s *\\([a-zA-Z_-]+\\)\\s +{"
+       1 font-lock-type-face)
+     ;; overrides
+     '("^\\s +\\([a-zA-Z_-]+\\)\\["
+       1 font-lock-type-face)
+     ;; general delimited string
+     '("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
+       (2 font-lock-string-face)))
+  "*Additional expressions to highlight in puppet mode.")
 
 ;;;###autoload
 (defun puppet-mode ()
@@ -222,97 +339,14 @@ The variable puppet-indent-level controls the amount of indentation.
   (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
   (set (make-local-variable 'paragraph-start) "\f\\|[ 	]*$")
   (set (make-local-variable 'paragraph-separate) "[ 	\f]*$")
-  (run-hooks 'puppet-mode-hook))
-
-(cond
- ((featurep 'font-lock)
   (or (boundp 'font-lock-variable-name-face)
       (setq font-lock-variable-name-face font-lock-type-face))
-
-  (setq puppet-font-lock-syntactic-keywords
-        '(
-          ("\\(^\\|[=(,~?:;]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"
-           (4 (7 . ?/))
-           (6 (7 . ?/)))
-          ("^\\(=\\)begin\\(\\s \\|$\\)" 1 (7 . nil))
-          ("^\\(=\\)end\\(\\s \\|$\\)" 1 (7 . nil))))
-
-  (cond ((featurep 'xemacs)
-         (put 'puppet-mode 'font-lock-defaults
-              '((puppet-font-lock-keywords)
-                nil nil nil
-                beginning-of-line
-                (font-lock-syntactic-keywords
-                 . puppet-font-lock-syntactic-keywords))))
-        (t
-         (add-hook 'puppet-mode-hook
-            '(lambda ()
-               (make-local-variable 'font-lock-defaults)
-               (make-local-variable 'font-lock-keywords)
-               (make-local-variable 'font-lock-syntax-table)
-               (make-local-variable 'font-lock-syntactic-keywords)
-               (setq font-lock-defaults '((puppet-font-lock-keywords) nil nil))
-               (setq font-lock-keywords puppet-font-lock-keywords)
-               (setq font-lock-syntax-table puppet-font-lock-syntax-table)
-               (setq font-lock-syntactic-keywords puppet-font-lock-syntactic-keywords)))))
-
-  (defvar puppet-font-lock-syntax-table
-    (let* ((tbl (copy-syntax-table puppet-mode-syntax-table)))
-      (modify-syntax-entry ?_ "w" tbl)
-      tbl))
-
-  (defvar puppet-font-lock-keywords
-    (list
-     ;; defines
-     '("^\\s *\\(define\\|node\\|class\\)\\s +\\([^( \t\n]+\\)"
-       2 font-lock-function-name-face)
-     '("\\s +inherits\\s +\\([^( \t\n]+\\)"
-       1 font-lock-function-name-face)
-     ;; include
-     '("^\\s *include\\s +\\([^( \t\n,]+\\)"
-       1 font-lock-reference-face)
-     ;; hack to catch continued includes
-     '("^\\s *\\([a-zA-Z0-9:_-]+\\),?\\s *$"
-       1 font-lock-reference-face)
-     ;; keywords
-     (cons (concat
-            "\\b\\(\\("
-            (mapconcat
-             'identity
-             '("case"
-               "class"
-               "default"
-               "define"
-               "false"
-               "import"
-               "include"
-               "inherits"
-               "node"
-               "realize"
-               "true"
-               )
-             "\\|")
-            "\\)\\>\\)")
-           1)
-     ;; variables
-     '("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(nil\\|self\\|true\\|false\\)\\>"
-       2 font-lock-variable-name-face)
-     ;; variables
-     '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
-       1 font-lock-variable-name-face)
-     '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
-       0 font-lock-variable-name-face)
-     ;; usage of types
-     '("^\\s +\\([a-zA-Z_-]+\\)\\s +{" 
-       1 font-lock-type-face)
-     ;; overrides
-     '("^\\s +\\([a-zA-Z_-]+\\)\\["
-       1 font-lock-type-face)
-     ;; general delimited string
-     '("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
-       (2 font-lock-string-face))
-     )
-    "*Additional expressions to highlight in puppet mode."))
- )
+  (set (make-local-variable 'font-lock-keywords) puppet-font-lock-keywords)
+  (set (make-local-variable 'font-lock-multiline) t)
+  (set (make-local-variable 'font-lock-defaults)
+       '((puppet-font-lock-keywords) nil nil))
+  (set (make-local-variable 'font-lock-syntax-table)
+       puppet-font-lock-syntax-table)
+  (run-hooks 'puppet-mode-hook))
 
 (provide 'puppet-mode)

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list