;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                     ;;
;;  do-comments.el                                                     ;;
;;  version 1.3 by rh                                                  ;;
;;                                                                     ;;
;;  Some useful tools for commenting and uncommenting text.            ;;
;;  Works only in combination with a major mode defining the           ;;
;;  comment syntax (e.g. c-mode defines /*...*/).                      ;;
;;                                                                     ;;
;;  To use, put this file into your load-path and                      ;;
;;  add the following line to your .emacs:                             ;;
;;  (load "do-comments")                                               ;;
;;                                                                     ;;
;;  You may also bind keys to the most often used functions, e.g.:     ;;
;;  (global-set-key [f18] 'comment-line)                               ;;
;;  (global-set-key [C-f18] 'uncomment-line)                           ;;
;;  (global-set-key [S-f18] 'structure-line)                           ;;
;;                                                                     ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                     ;;
;;                                                                     ;;
;;  Last Changes:                                                      ;;
;;  -------------                                                      ;;
;;                                                                     ;;
;;  - 25. Aug 03: update for compatibility with EMACS-21.2.1           ;;
;;  - 16. Feb 99: bug fixes                                            ;;
;;  - 26. Aug 98: bug fixes                                            ;;
;;                                                                     ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                     ;;
;; Variables:
;;
(make-local-variable 'comment-start)
(make-local-variable 'comment-end)

(defun comment-line () ""
  (interactive)
  (comment-line-func 1)
  )

(defun uncomment-line () ""
  (interactive)
  (comment-line-func -1)
  )

(defun comment-line-func (arg) "Comment out current line if not empty."
  (if comment-start
      (progn
	(save-excursion
	  (beginning-of-line)
	  (let ((beg (point))) (end-of-line)
	       (setq endp (point))
	       (if (not (string-equal comment-end ""))
		   (indent-to-column 65))
	       (if (> arg 0)
		   (comment-region beg (point))
		   (uncomment-region beg (point)))
	       (goto-char beg)
	       (while (re-search-forward "[\t ]+$" endp t)
		 (replace-match "" nil nil))
	       )
	  )
	(next-line 1)
	)
    (progn (define-comment-syntax) (comment-line))
    )
  )

(defun test () "Comment out current line if not empty."
  (interactive)
  (save-excursion
    (beginning-of-line)
    (let ((beg (point))) (end-of-line)
	 (setq endp (point))
	 (if (not (string-equal comment-end ""))
	     (indent-to-column 65))
	 (goto-char beg)
	 (while (re-search-forward "[\t ]+$" 5000 t)
	   (replace-match "" nil nil))
	 )
    )
  )

(defun uncomment-line-1 ()
  "Comment out current line if not empty (only one layer)."
  (interactive)
  (if comment-start
      (progn (beginning-of-line)
	     (let ((beg (point))) (end-of-line)
		  (if (not (string-equal comment-end ""))
		      (indent-to-column 65))
		  (comment-region beg (point) -1)
		  )
	     (next-line 1)
	     (beginning-of-line)
	     )
    )
  (progn (define-comment-syntax) (uncomment-line-1))
  )

; (defun uncomment-line () "Remove commentation signs from current line."
;   (interactive)
;   (if comment-start
;       (progn (save-excursion
; 	       (beginning-of-line)
; 	       (let ((beg (point)))
; 		 (end-of-line)
; 		 (narrow-to-region beg (point))
; 		 (beginning-of-line)
; 		 (replace-string comment-start "")
; 		 (beginning-of-line)
; 		 (replace-string comment-end "")
; 		 (beginning-of-line)
; 		 (replace-regexp "[ \t]*$" "")
; 		 (beginning-of-line)
; 		 (widen))
; 	       )
; 	     (next-line 1)
; 	     )
;     (progn (define-comment-syntax) (uncomment-line))
;     )
;   )

(defun define-comment-syntax () ""
  (interactive)
  (setq comment-start (read-from-minibuffer "Comment-start: "))
  (setq comment-end (read-from-minibuffer "Comment-end: "))
  )

(defun comment-line-2 ()
  "Kommentarzeichen am Anfang der Zeile und am Cursor"
  (interactive)
  (set-mark-command nil)
  (beginning-of-line)
  (insert comment-start)
  (exchange-dot-and-mark)
  (insert comment-end)
)

(defun structure-line () "Insert structuring line."
  (interactive)
  (beginning-of-line)
  (insert "\n")
  (previous-line 1)
  (insert "----------------------------------------------------------------------")
  (comment-line)
;  (beginning-of-line)
)

