;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                    Some nifty functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun highlight-region (string)
  "Inserts STRING at the beginning of every line in the region.
Called interactively, STRING defaults to '> ' unless a prefix
argument is given."
  (interactive (list (if current-prefix-arg
                         (read-string "String to insert: ")
                       "> ")))
  (save-excursion
    (if (bolp)
        (forward-line -1))
    (save-restriction
      (narrow-to-region (point) (mark))
      (goto-char (point-min))
      (replace-regexp "^" string))))

(defun try-attach ()
  (cond ((string-match "^/mit/\\([^/]+\\)/" buffer-file-name)
         (let ((filsys (substring buffer-file-name
                                  (match-beginning 1)
                                  (match-end 1))))
           (if (not (file-exists-p (concat "/mit/" filsys)))
               (progn
                 (princ (format "Attaching %s..." filsys))
                 (sit-for 0)
                 (call-process "attach" nil nil nil
                               filsys)
                 (if (condition-case ()
                         (insert-file-contents filename t)
                       (file-error nil))
                     (progn (setq error nil)
                            t))))))))

(defun my-check-file-exists (filename)
  (if (file-readable-p filename)
      filename
    nil))

(defun filter-nils (list)
  (if (null list)
      '()
    (if (null (car list))
	(filter-nils (cdr list))
      (cons (car list) (filter-nils (cdr list))))))

(setq mail-setup-hook
      (function (lambda ()
		  ;; Save excursion means that point is not moved
		  ;; by the commands contained inside the form.
		  ;; When this is executed, point is after "To: " if 
		  ;; there is no destination specified, or after the 
		  ;; --text follows this line-- if there is.
		  (save-excursion 
		    (if to
			(forward-line -1)
		      (forward-line 2))
		    (insert
		     (if cc
			 (if (string-match "webmaster@mit\\.edu"
					   cc)
			     "Reply-to: webmaster@mit.edu\n"
			   ""
			   )
		       (if to
			   "Reply-To: nocturne@mit.edu\n"
			 ""))
;;		     "X-URI: <URL:http://www.mit.edu/people/nocturne/homepage.html>\n"
		     )))))


(defun nifty-next-buffer ()
  "Select the next buffer, regardless of which frame it is on."
  (interactive)
  (select-window (next-window (selected-window)
			      t
			      t)))

(defun nifty-prev-buffer ()
  "Select the previous buffer, regardless of which frame it is on."
  (interactive)
  (select-window (previous-window (selected-window)
				  t
				  t)))

(defun my-new-frame ()
  ""
  (interactive)
  (new-frame (list (cons 'name (concat "emacs@" (system-name)))
		   (cons 'minibuffer (minibuffer-window)))))

(defun webmaster ()
  ""
  (interactive)
  (mail)
  (forward-line 1)
   (insert "bcc: webm.discuss@charon.mit.edu\n")
  (forward-line 3)
  (insert "Reply-to: webmaster@mit.edu\n")
  (forward-line -6)
  (end-of-line)
)

(defun ws ()
  ""
  (interactive)
   (goto-char (point-max))
   (insert " - Eric Mumpower\n")
   (insert "  for the MIT SIPB Webmasters\n")
)

(defun query-save-buffers-kill-emacs ()
  "Exit emacs, but only after the user confirms."
  (interactive)
  (if (y-or-n-p "Really exit emacs? ")
      (save-buffers-kill-emacs)))

;;; acevedo, from the emackers meeting
(defun hide-region (begin end)
  (interactive "r")
  (let ((buffer-read-only nil))
    (subst-char-in-region begin end ?\n ?\r))
  (setq selective-display t
	selective-display-ellipses t))

(defun unhide-region (begin end)
  (interactive "r")
  (let ((buffer-read-only nil))
    (subst-char-in-region begin end ?\r ?\n)))

(defun searchlib (file &optional explicit-name) 
  "Find where the Emacs Lisp library FILE is in the current
'`load-path'.  Tries appending \".el\" and \".elc\" unless the file
already ends in one of these suffixes. If a prefix arg is given or
the filename ends in \".elc\" then the filename is searched for
without changes."
  (interactive "sSearch for library: \nP")
  (let* ((path load-path)
	 (file        (if (or explicit-name (string-match ".elc?\\'" file))
			  file
			(concat file ".el")))
	 (try-elc-too (and (string-match ".el\\'" file)
			   (not explicit-name))))
    (let ((test-file)
	  (found))
      (while (and path 
		  (not found))
	(and (car path) 
	     (or (file-exists-p
		  (setq test-file (concat (car path)
					  (if (string-match "/\\'" (car path)) "" "/")
					  file)))
		 (if try-elc-too
		     (file-exists-p
		      (setq test-file (concat (car path)
					      (if (string-match "/\\'" (car path)) "" "/")
					      file
					      "c")))))
	     (setq found test-file))
	(setq path (cdr path)))
      found)))

(defun wherelib (file &optional explicit-name)
  "Find where the Emacs Lisp library FILE is in the current
'`load-path'.  Tries appending \".el\" and \".elc\" unless the file
already ends in one of these suffixes. If a prefix arg is given or
the filename ends in \".elc\" then the filename is searched for
without changes."
  (interactive "sSearch for library: \nP")
  (let ((path (searchlib file explicit-name)))
    (if path 
	(message "Library %s is in %s." file path)
      (message "Library %s is not found." file))
    path))

(defun findlib (file &optional explicit-name)
  "Find where the Emacs Lisp library FILE is in the current
'`load-path'.  Tries appending \".el\" and \".elc\" unless the file
already ends in one of these suffixes. If a prefix arg is given or
the filename ends in \".elc\" then the filename is searched for
without changes. Then find-file the library."
  (interactive "sSearch for Library: \nP")
  (let ((path (wherelib file explicit-name)))	
    (if path 
	(find-file path))))

(defun narrow-to-c-function ()
  ""
  (interactive)
  (save-excursion
    (c-mark-function)
    (narrow-to-region (point) (mark) )))

(defun wc-buffer ()
  ""
  (interactive)
  (save-excursion
    (shell-command-on-region (point-min) (point-max) "wc" nil)))
