Skip to content

Enter Puppet top scope symbols with Emacs Abbrev mode

Puppet manages variables in different scopes. Global variables and all facts are defined in top scope. To make this explicit, these variables and facts should be accessed using the top scope notation. So in a Puppet manifest the hostname fact should for example be written as $::hostname. Here is a piece of Emacs code that automatically adds the double-colon namespace separator to variable and fact names.

The code uses abbrev-mode to call a function whenever one of the given variable or fact names is completed. This function then checks for the double-colon and adds it if necessary.

Get started by including the following code to your Emacs setup.

(defun stm-puppet-top-scope-symbol ()
  "Ensure the Puppet symbol before point is referenced using top scope."
      (when (looking-back "${?\\([a-z][a-z0-9_]*\\)"
        (goto-char (match-beginning 1))
        (insert "::")))))

 (lambda ()
   ;; Define abbrevs for Puppet top scope symbols that should be
   ;; written using the '$::var' format. Single strings don't
   ;; change and only get the '::' prefix. Cons cells define
   ;; an abbrev that expands the string of the car to the string
   ;; of the cdr.
   (dolist (abbrev '("architecture" "domain" "fqdn" "hostname"
                     "interfaces" "ipaddress" "is_virtual"
                     "virtual" "lsbdistcodename"
                     "lsbdistdescription" "lsbdistid"
                     "lsbdistrelease" "lsbmajdistrelease"
                     "lsbminordistrelease" "operatingsystem"
                     "operatingsystemrelease" "os" "osfamily"
                     ("osmr" . "operatingsystemmajrelease")
                     ("osr" . "operatingsystemrelease")))
     (let ((abbrevname (if (consp abbrev) (car abbrev) abbrev))
           (expansion (if (consp abbrev) (cdr abbrev) abbrev)))
       (define-abbrev puppet-mode-abbrev-table
         abbrevname expansion #'stm-puppet-top-scope-symbol
         :system t)))
   (abbrev-mode 1)))

The code adds abbrevs for some popular facts. Expansion happens when the next typed char is not a word constituent character. In this case the function stm-puppet-top-scope-symbol checks for the double-colon format. The abbrevs are mostly expanded literally, so they don't really change the text.

There are two cases of real shortcuts (the intrinsic purpose of abbrev-mode) defined by using a cons of the name and the expansion. Typing $osr will actually expand to $::operatingsystemrelease. See the following illustration for a recording of an example session.


No Trackbacks


Display comments as Linear | Threaded

No comments

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
Textile-formatting allowed
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.

Form options

Submitted comments will be subject to moderation before being displayed.