;; -*- emacs-lisp -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; $Id: devel.el,v 1.25 2006/03/06 12:07:06 ole Exp $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* Development initialization
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* Shell
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun my-shell-mode-hook ()
  (setq indent-tabs-mode nil
        tab-width 2))
(add-hook 'sh-mode-hook 'my-shell-mode-hook)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* Perl
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun my-cperl-hook ()
  (setq cperl-indent-level 4)
  (setq cperl-continued-statement-offset 4)
  (setq cperl-brace-offset -4)
  (setq cperl-label-offset -4)
)
(add-hook 'cperl-mode-hook 'my-cperl-hook)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* Emacs Code Browser
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(load "/usr/share/emacs/site-lisp/cedet/common/cedet")


;; (add-to-list 'load-path (expand-file-name "ecb/" emacs-packages-dir))
(require 'ecb)

(defun switch-to-eshell ()
  "Open a new window with a eshell or jump to an open eshell window.
Takes in account that a possibly running ecb might display the shell
in a compile window."
  (interactive)
  (let ((shellbuf (get-buffer "*eshell*")))
    (if (buffer-live-p shellbuf)
        (if (eq (current-buffer) shellbuf)
            (delete-window)
          (if ecb-compile-window
              (switch-to-buffer shellbuf)
            (switch-to-buffer-other-window shellbuf))
          (end-of-buffer))
      (unless ecb-compile-window
        (split-window-vertically))
      (eshell)
      )))

(defun switch-to-shell ()
  "Open a new window with a shell or jump to an open shell window.
Takes in account that a possibly running ecb might display the shell
in a compile window."
  (interactive)
  (let ((shellbuf (get-buffer "*bash*")))
    (if (buffer-live-p shellbuf)
        (if (eq (current-buffer) shellbuf)
            (delete-window)
          (if ecb-compile-window
              (switch-to-buffer shellbuf)
            (switch-to-buffer-other-window shellbuf))
          (end-of-buffer))
      (unless ecb-compile-window
        (split-window-vertically))
      (ansi-term "/bin/bash" "bash")
      )))

(defun switch-to-compilation ()
  "Open or hide the compilation window and put cursor into it."
  (interactive)
  (if (and ecb-minor-mode ecb-compile-window)
      (if (window-live-p ecb-compile-window)
          (if (eq (window-buffer ecb-compile-window) (current-buffer))
              (ecb-toggle-compile-window-height)
            (ecb-goto-window-compilation))
        (ecb-toggle-compile-window)
        (ecb-goto-window-compilation))
    (let ((buf (get-buffer "*compilation*")))
      (if (buffer-live-p buf)
          (if (eq (current-buffer) buf)
              (delete-window)
            (switch-to-buffer-other-window buf))
        (switch-to-buffer-other-window "*compilation*")))))

(defun ecb-hide-compile-window (buf msg)
  "Hide the compilation window and display message"
  (save-excursion
    (set-buffer buf)
    (if (or (string-match ".*exited abnormally.*" msg) 
            (string-match ".*BUILD FAILED.*" (buffer-string)))
        ;;there were errors, so jump to the first error
        (if jde-compile-jump-to-first-error (next-error))
      ;;no errors, make the compilation window go away in a few seconds
      (if (and ecb-minor-mode ecb-compile-window-height)
          (ecb-toggle-compile-window)
        (lexical-let ((compile-buffer buf))
          (run-at-time "1 sec" nil 'jde-compile-kill-buffer compile-buffer)
          (message "No errors.")))
      (message "No errors."))))

(defun delete-other-windows-hide-compile-window (&optional window)
  "hide ecb compile window also when deleting other windows"
  (interactive)
  (ecb-goto-window-edit-last)
  (delete-other-windows window)
  (when (and ecb-minor-mode ecb-compile-window-height)
    (ecb-toggle-compile-window -1))
  (ecb-goto-window-edit1))

(global-set-key [(insert)] 'ecb-redraw-layout)
(global-set-key [(kp-insert)] 'ecb-redraw-layout)
(global-set-key [(kp-0)] 'ecb-redraw-layout)

(define-key goto-keymap [?s] 'switch-to-eshell)
(define-key goto-keymap [?t] 'switch-to-shell)
(define-key goto-keymap [?c] 'switch-to-compilation)
(define-key goto-keymap [?d] 'ecb-minor-mode)

(define-key ecb-mode-map [(control c) ?1] 'ecb-goto-window-edit1)
(define-key ecb-mode-map [(control c) ?2] 'ecb-goto-window-edit2)

(defun ecb-dir-popup-ant (node target)
  "Run ant in directory with target"
  (require 'jde-ant)
  (let* ((dir (tree-node-get-data node))
         (project-file (jde-ant-find-build-file
                        (if (file-directory-p dir) dir
                          (file-name-directory dir)))))
    (jde-ant-build project-file target)))

(tree-buffer-defpopup-command ecb-dir-popup-ant-build
  "Build project in directory"
  (ecb-dir-popup-ant node "build"))

(tree-buffer-defpopup-command ecb-dir-popup-ant-rebuild
  "Build project in directory"
  (ecb-dir-popup-ant node "clean build"))

(tree-buffer-defpopup-command ecb-dir-popup-ant-clean
  "Clean project in directory"
  (ecb-dir-popup-ant node "clean"))

(tree-buffer-defpopup-command ecb-dir-popup-ant-test 
  "Test project in directory"
  (ecb-dir-popup-ant node "test"))

(tree-buffer-defpopup-command ecb-dir-popup-ant-site
  "Build site for project in directory"
  (ecb-dir-popup-ant node "site"))

(tree-buffer-defpopup-command ecb-dir-popup-ant-javadoc
  "Build javadoc for project in directory"
  (ecb-dir-popup-ant node "javadoc"))

(tree-buffer-defpopup-command ecb-dir-popup-cvs-status-local
  "Check status of directory \(and below) in pcl-cvs mode."
  (ecb-dir-run-cvs-op node 'cvs-status '("-l -v")))

(tree-buffer-defpopup-command ecb-dir-popup-svn-status
  "Check status of directory \(and below) in psvn mode."
  (let ((dir (tree-node->data node)))
    (svn-status dir)))

(setq ecb-directories-menu-user-extension-function
      (lambda ()
        '(("Ant/Maven"
           (ecb-dir-popup-ant-build "Build")
           (ecb-dir-popup-ant-rebuild "Rebuild")
           (ecb-dir-popup-ant-build-tests "Build Tests")
           (ecb-dir-popup-ant-clean "Clean")
           (ecb-dir-popup-ant-test "Run All Tests")
           (ecb-dir-popup-ant-site "Build Site")
           (ecb-dir-popup-ant-javadoc "Build Javadoc"))
          ("CVS"
           (ecb-dir-popup-cvs-status-local "Directory Status")
           (ecb-dir-popup-cvs-status "Subtree Status")
           (ecb-dir-popup-cvs-examine "Examine Subtree")
           (ecb-dir-popup-cvs-update "Update Subtree"))
          ("SVN"
           (ecb-dir-popup-svn-status "Subtree Status")))))


(tree-buffer-defpopup-command ecb-file-popup-create-interface
  "Generate a interface source file"
  (let ((node-data=file (tree-node-get-data node)))
    (jde-gen-interface-buffer)))

(tree-buffer-defpopup-command ecb-file-popup-ant-test 
  "Test single file"
  (let* ((class-name (file-name-sans-extension (file-name-nondirectory (tree-node-get-data node))))
         (test-name  (concat class-name (if (not (string-match "Test" class-name)) "Test"))))
    (ecb-dir-popup-ant node
                       (concat "-Dtestmatch='" test-name "' test:match"))))

(defun ecb-file-popup-function ()
  "Return a pop menu list for the ecb sources window"
  '((ecb-file-popup-ant-test "Run Test")
    ("---")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* Compilation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'compile)

(setq-default compile-command "make ")
(setq compile-auto-highlight t)
(setq compilation-scroll-output t)
(setq compilation-window-height 8)

(global-set-key [(f10)] 'next-error) ;; C-x `
(global-set-key [(control f10)] 'previous-error)

(defun my-compilation-mode-hook ()
  "Function to run when entering compilation mode"
  (setq buffer-read-only nil)
  (setq truncate-partial-width-windows nil
        truncate-lines nil))

(add-hook 'compilation-mode-hook 'my-compilation-mode-hook)

;; (add-to-list 'compilation-error-regexp-alist
;;              '("^\\s-*\\[[^]]*\\]\\s-*[a-zA-Z]?:?\\(.+\\):\\([0-9]+\\):" 1 2))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* Changelog
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(global-set-key [(control x) (control a)] 
                (lambda () 
                  (interactive) 
                  (add-change-log-entry nil (expand-file-name "ChangeLog") t nil)))

(eval-after-load "add-log"
  '(define-key change-log-mode-map[(control c)(control c)]
     (lambda ()
       (interactive)
       (save-buffer)
       (kill-buffer-and-window))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* ASCII Table
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'ascii-table nil t)

(defun ole-ascii-table ()
  "Display an ASCII table in a small buffer"
  (interactive)
  (let ((i 32)
        (buffer (get-buffer "*ascii-table*")))
    (if buffer
        nil
      (setq buffer (create-file-buffer "*ascii-table*"))
      (set-buffer buffer)
      (while (< i 128)
        (insert (concat " " (char-to-string i)))
        (setq i (+ i 1)))
      (beginning-of-buffer)
      (fill-paragraph t))
    (switch-to-buffer-other-window buffer)
    (shrink-window-if-larger-than-buffer)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* RFCView
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(setq  get-rfc-local-rfc-directory (expand-file-name "~/doc/rfc/")
       get-rfc-open-in-new-frame nil)
 
(autoload 'rfcview-mode "rfcview" nil t)
(setq auto-mode-alist (cons '("/rfc[0-9]+\\.txt" . rfcview-mode)
                            auto-mode-alist))

(defun rfc (num)
  "Show RFC NUM in a buffer."
  (interactive "nRFC (0 for index): ")
  (let ((url (if (zerop num)
                 "http://www.ietf.org/iesg/1rfc_index.txt"
               (format "http://www.ietf.org/rfc/rfc%i.txt" num)))
        (buf (get-buffer-create "*RFC*")))
    (with-current-buffer buf
      (let ((inhibit-read-only t))
        (delete-region (point-min) (point-max))
        (let ((proc (start-process "wget" buf "wget" "-q" "-O" "-" url)))
          (set-process-sentinel proc 'rfc-sentinel))
        (message "Getting RFC %i..." num)))))

(defun rfc-sentinel (proc event)
  "Sentinel for `rfc'."
  (with-current-buffer (process-buffer proc)
    (goto-char (point-min))
    (view-mode 1)
    (when (fboundp 'rfcview-mode)
      (rfcview-mode)))
  (display-buffer (process-buffer proc)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* SQL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; sql-mode
(add-to-list 'auto-mode-alist '("\\.sql$" . sql-mode))

;; (require 'sql-complete)

;; (setq sql-oracle-data-dictionary
;;       "select '(\"'||table_name||'\" \"'||column_name||'\")' from all_tab_cols where owner = 'ONEUP_OK' order by table_name;")
(setq sql-electric-stuff 'semicolon)