;;; mushify.el
;;; Azundris.
;;; M-x load-file ..., M-x mushify-buffer
;;; very simple thingie that converts the contents of the current buffer
;;; to the format used on MUSHes (converts tabs, spaces, and concat lines).
;;; 04/07/98, 04/27/04, 10/02/06

(defun mushify-buffer ()
  "Convert buffer to MUSH-code."
  (interactive)

  ;; % escape
  (message "Percent-escape...")
  (goto-char 0)
  (while (search-forward "%" nil t)
    (insert "%"))

  ;; \ escape
  (message "Backslash-escape...")
  (goto-char 0)
  (while (search-forward "\\" nil t)
    (insert "\\"))

  ;; brace escape
  (message "Braces-escape...")
  (goto-char 0)
  (while (search-forward-regexp "[\]{}\[]" nil t)
    (goto-char (- (point) 1))
    (insert "%")
    (goto-char (+ (point) 1)))

  ;; space replace
  (message "Space-replace...")
  (goto-char 0)
  (while (search-forward "  " nil t)
    (goto-char (- (point) 2))
    (if (looking-at "      ")
	(progn
	    (setq bc 0)
	      (while (looking-at " ")
		    (delete-char 1)
		        (setq bc (+ bc 1)))
	        (insert (concat "[space(" (number-to-string bc) ")]")))
      (while (looking-at " ")
	(delete-char 1)
	(insert "%b"))))

  ;; tab replace
  (message "Tab-replace...")
  (goto-char 0)
  (while (search-forward-regexp "[\t]" nil t)
    (goto-char (- (point) 1))
    (delete-char 1)
    (insert "%t"))

  ;; unsplit lines
  (message "Unsplit...")
  (goto-char 0)
  (while (< (point) (- (point-max) 1))
    (end-of-line)
    (delete-char 1)
    (insert "%r"))

  (goto-char 0)
  (message nil))

;; ends
