gnu-emacs-sources
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

square-braces-as-parens.el 1.8


From: thi
Subject: square-braces-as-parens.el 1.8
Date: Tue, 14 Nov 2000 11:49:24 -0800

ok, things are now fully configurable.

changes:
(sbap-close-everything-magic-char): Make settable via `set-variable'.
(sbap-close-everything-delay-factor): New var, autoloaded, user-settable.
(square-braces-as-parens-close-everything): Use new var.

thi


_____________________________
;;; ID: square-braces-as-parens.el,v 1.8 2000/11/14 19:46:29 ttn Rel
;;;
;;; Copyright (C) 2000 Thien-Thi Nguyen
;;; This file is part of ttn's personal elisp library, released under GNU
;;; GPL with ABSOLUTELY NO WARRANTY.  See the file COPYING for details.

;;; Description: Minor mode to bind parens to square-braces keys, per buffer.

;;;###autoload
(defvar square-braces-as-parens-mode nil
  "If non-nil, \"[\" and \"]\" insert \"(\" and \")\", respectively.")

(defun square-braces-as-parens-insert (n yes no)
  (while (< 0 n)
    (insert (if square-braces-as-parens-mode yes no))
    (setq n (1- n))))

;;;###autoload
(defvar sbap-close-everything-magic-char 32
  "*When closing, if preceding char is this char, delete the char and
close everything (so that the top-level form is closed) one paren at a
time w/ a delay between each paren.  Any input before the closing is
finished cancels the rest of the parens.  A value of nil means never do
close-everything behavior.  Default value is 32 (space).")

;;;###autoload
(defvar sbap-close-everything-delay-factor 0.05
  ;; Thanks go to Dave Pearson for this idea.
  "*Delay factor used when closing everything, larger means slower.
Set to zero for instantaneous closing.  Default value is 0.05.")

;; This function adapted from gnu.emacs.help post
;; <address@hidden> by Kevin Rodgers.
(defun square-braces-as-parens-close-everything ()
  (interactive)
  (let ((last-command-char ?\))
        (depth (car (parse-partial-sexp (save-excursion
                                          (beginning-of-defun)
                                          (point))
                                        (point)))))
      (while (and (not (input-pending-p))
                  (< 0 depth))
        ;; why is this better than using `insert'?  --ttn
        (call-interactively 'self-insert-command)
        (sit-for (* sbap-close-everything-delay-factor
                    (setq depth (1- depth)))))))

;;;###autoload
(defun square-braces-as-parens-mode (&optional arg)
  (interactive "P")
  (setq square-braces-as-parens-mode
        (if (null arg)
            (not square-braces-as-parens-mode)
          (> (prefix-numeric-value arg) 0)))
  (local-set-key "[" #'(lambda (n)
                         (interactive "p")
                         (square-braces-as-parens-insert n "(" "[")))
  (local-set-key "]" #'(lambda (n)
                         (interactive "p")
                         (if (or (not sbap-close-everything-magic-char)
                                 (/= sbap-close-everything-magic-char
                                     (char-before))
                                 (not square-braces-as-parens-mode))
                             (square-braces-as-parens-insert n ")" "]")
                           (delete-char -1)
                           (square-braces-as-parens-close-everything))))
  (unless noninteractive
    (message "Square braces as parens mode: %s"
             (if square-braces-as-parens-mode "on" "off"))))

;; load time action
(mapcar (lambda (var)
            (make-variable-buffer-local var)
            (put var 'permanent-local t))
        '(square-braces-as-parens-mode
          sbap-close-everything-magic-char
          sbap-close-everything-delay-factor))

;; that's it
(provide 'square-braces-as-parens)

;;; square-braces-as-parens.el,v1.8 ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]