Skip to content

Instantly share code, notes, and snippets.

@spacebat
Created October 27, 2014 11:42
Show Gist options
  • Select an option

  • Save spacebat/57f43fa16571ceeb4dd2 to your computer and use it in GitHub Desktop.

Select an option

Save spacebat/57f43fa16571ceeb4dd2 to your computer and use it in GitHub Desktop.
Set lexical-binding to t
(defun lexbind-insert-lexbind-enablement (&optional verbose)
"Attempt to set `lexical-binding' to t in the current buffer's
prop-line."
(macrolet ((with-prop-line (&rest body)
`(save-excursion
(save-restriction
(goto-char 1)
(forward-line 1)
(narrow-to-region 1 (point))
(goto-char (point-min))
,@body))))
(cl-flet ((prop-line-p ()
(with-prop-line
(re-search-forward "-\\*-.+-\\*-" nil t)))
(mesg (&rest args)
(when verbose
(apply 'message args))))
(if (prop-line-p)
(let* ((vars (hack-local-variables-prop-line))
(lexbound (assoc 'lexical-binding vars)))
(mesg "prop-line found")
(if vars
(if lexbound
(if (not (cdr lexbound))
(progn
(mesg "need to substitute prop-line")
(with-prop-line
(when (re-search-forward "\\blexical-binding: *nil\\b" nil t)
(replace-match "lexical-binding: t" nil nil)
t)))
(message "lexical-binding is already non-nil"))
(mesg "need to append lexical-binding: t")
(with-prop-line
(when (re-search-forward "\\(-\\*- +\\)\\(.*?\\)\\(;*\\)\\( +-\\*-\\)" nil t)
(replace-match (if (plusp (length (match-string 2))) "; " "") nil nil nil 3)
(replace-match "lexical-binding: t\\4" nil nil nil 4))))
(message "Malformed prop line")))
(mesg "no prop-line found")
(save-excursion
(goto-char 1)
(insert "-*- lexical-binding: t -*-\n")
(set-mark (point))
(goto-char 1)
(comment-region (point) (mark)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment