;; Routines to Call dpatch and use it from Emacs.
;; copyright 2003 Junichi Uekawa.

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; readme-debian.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with your Debian installation, in /usr/share/common-licenses/GPL
;; If not, write to the Free Software Foundation, 675 Mass Ave,
;; Cambridge, MA 02139, USA.

(defgroup dpatch nil "dpatch mode"
  :group 'tools
  :prefix "dpatch-")

(defun dpatch-get-debian-dir ()
  "Find the directory with debian/ dir, and get the dir name."
  (let* ((looking-dir (expand-file-name (concat default-directory "."))))
    (while (not (file-accessible-directory-p (concat looking-dir "/debian")))
      (progn 
	(if (string= looking-dir "/")
	    (error "Cannot find debian dir anywhere"))
	(setq looking-dir (expand-file-name (expand-file-name (concat looking-dir "/.."))))))
    looking-dir))

(defun dpatch-list-patch ()
  "Obtain list of patches for this Debian package."
  (interactive)

  (let* ((my-default-directory (concat (dpatch-get-debian-dir) "/"))
	 (dpatch-buffer (concat "*dpatch*" my-default-directory))
	 (dpatch-process (concat "dpatch-process-" my-default-directory)))
    (switch-to-buffer dpatch-buffer)
    (dpatch-list-patch-mode)
    (setq default-directory my-default-directory)
    (kill-region (point-min) (point-max))
    (start-process dpatch-process dpatch-buffer "/usr/bin/dpatch-list-patch")))

(defun dpatch-list-patch-mode ()
  "Mode to view debc output.
\\{dpatch-list-patch-mode-map}"
  (interactive)
  (kill-all-local-variables)
  (setq major-mode 'dpatch-list-patch-mode)
  (setq mode-name "dpatch-list-patch")
  (mapcar 'make-local-variable '(font-lock-defaults))
  (use-local-map dpatch-list-patch-mode-map)
  (set-syntax-table dpatch-list-patch-mode-syntax-table)
  (setq font-lock-defaults 
	'(
					;keywords start here
	  (("^  [^ ].*:$" . font-lock-string-face)
	   ("^[^ ].*:$" . font-lock-keyword-face)
	   ("^    [^ ].*$" . font-lock-comment-face)
	   )
	  nil		;keywords-only
	  nil		;case-fold
	  ()		;syntax-alist
	  ))
  (run-hooks 'dpatch-list-patch-mode-hook)
)

(defvar dpatch-list-patch-mode-map nil "Keymap for dpatch-list-patch mode.")
(defvar dpatch-list-patch-mode-syntax-table nil "Syntax table for dpatch-list-patch mode.")
(if dpatch-list-patch-mode-syntax-table
         ()              ; Do not change the table if it is already set up.
       (setq dpatch-list-patch-mode-syntax-table (make-syntax-table))
       (modify-syntax-entry ?\" ".   " dpatch-list-patch-mode-syntax-table)
       (modify-syntax-entry ?\\ ".   " dpatch-list-patch-mode-syntax-table)
       (modify-syntax-entry ?' "w   " dpatch-list-patch-mode-syntax-table))


(provide 'dpatch)
