Download
(defcustom bracketphobia-modes '(scheme-mode common-lisp-mode emacs-lisp-mode)
"The modes where brackets need to be gone.
See `bracketphobia-auto'."
:group 'applications
:type '(repeat function))
(defun bracketphobia ()
"Replace brackets outside of strings and comments with parens."
(interactive)
(font-lock-set-defaults)
(font-lock-default-fontify-region (point-min)
(point-max)
nil)
(save-excursion
(goto-char (point-min))
(while (bracketphobia-find)
(if (string= "[" (match-string 0))
(replace-match "(")
(replace-match ")")))))
(defun bracketphobia-auto ()
"Query the user whether to remove brackets from this file.
This is a suitable value for `find-file-hook', and will only
consider files in modes mentioned in `bracketphobia-modes'. Also
see `bracketphobie'."
(when (memq major-mode bracketphobia-modes)
(save-excursion
(goto-char (point-min))
(font-lock-set-defaults)
(font-lock-default-fontify-region (point-min)
(point-max)
nil)
(when (and (bracketphobia-find)
(save-window-excursion
(switch-to-buffer (current-buffer))
(y-or-n-p "This file contains brackets. Exterminate them? ")))
(bracketphobia)))))
(defun bracketphobia-find ()
"Find the next bracket.
This sets the match string 0 to the bracket."
(catch 'return
(while (re-search-forward "[][]" nil t)
(let ((face (get-text-property (match-beginning 0)
'face)))
(message "Face: %S" face)
(when (not (or (looking-back "#\\\\[][]")
(if (consp face)
(or (memq 'font-lock-comment-face face)
(memq 'font-lock-string-face face))
(or (eq face 'font-lock-comment-face)
(eq face 'font-lock-string-face)))))
(throw 'return t))))
nil))
(defvar bracketphobia-keywords
'(("\\["
(0 (progn
(compose-region (match-beginning 0) (match-end 0)
"(")
nil)))
("\\]"
(0 (progn
(compose-region (match-beginning 0) (match-end 0)
")")
nil))))
"The keywords used to hide brackets.")
(defun bracketphobia-hide ()
"Show brackets as parens."
(interactive)
(font-lock-add-keywords nil bracketphobia-keywords)
(font-lock-fontify-buffer))
(provide 'bracketphobia)