;;===> IDL
;; Mmmm colors
(global-font-lock-mode 1)
;;setup good colors for font-lock
(set-face-foreground font-lock-comment-face     "FireBrick")
(set-face-foreground font-lock-string-face      "DarkOrchid")
(set-face-foreground font-lock-keyword-face     "DarkGreen")
(set-face-foreground font-lock-function-name-face       "Navy")
(set-face-foreground font-lock-variable-name-face "Magenta")
(set-face-foreground font-lock-reference-face  "DarkSlateGray")

(autoload 'idlwave-mode "idlwave" "IDLWAVE Mode" t)
(autoload 'idlwave-shell "idlw-shell" "IDLWAVE Shell" t)
(setq auto-mode-alist (cons '(".*/idl/smart/.*\\.pro\\'" . smart-mode)
                                 auto-mode-alist))

; The hook is too late for these things
; Use a period for abbrevs, not backslash... we'll just
; live with the structure member ambiguity
(setq 
 idlwave-abbrev-start-char "."
 ;; Save the finger gymnastics by using the silly Windows key
 ;; instead of C-c C-d
 idlwave-shell-debug-modifiers '(super)
 ;; Old habits die hard.
 idlwave-libinfo-file "~/.idlwave/libinfo.el"
 idlwave-init-rinfo-when-idle-after 10
 ;; No frame splitting please
 idlwave-shell-use-dedicated-frame t
 ;; I like a lot of command history... because hey, you never now
 comint-input-ring-size 1024
 ;; Who likes light turquoise, really?
 my-face (make-face 'my-idlwave-shell-expression-highlight-face)
 ;;      idlwave-shell-mark-stop-line 'arrow
 idlwave-shell-expression-face my-face
 idlwave-shell-output-face     my-face)
(set-face-background my-face "GoldenRod")

;; Sometimes those newly introduced bugs just get you down
(setq idlwave-help-browser-function 'browse-url-generic
      idlwave-help-browser-generic-program "galeonwave")

;; I'll add this little hook to both the idlwave-mode and
;; idlwave-shell-mode hooks, because I'd like the keys in both.  
(defun my-common-idlwave-hook ()
  (local-set-key [C-S-mouse-3] 'idlwave-mouse-context-help)
  (local-set-key [C-kp-subtract] 'idlwave-shell-stack-up)
  (local-set-key [C-kp-add] 'idlwave-shell-stack-down))

(add-hook 'idlwave-mode-hook
          (lambda ()
            ;; To complete structure tags at the Shell prompt and in files!
            (require 'idlw-complete-structtag)
            
            (set (make-local-variable 'minor-mode-alist)
                 (copy-sequence minor-mode-alist))
            (delq (assq 'auto-fill-function minor-mode-alist) minor-mode-alist)
            (delq (assq 'abbrev-mode minor-mode-alist) minor-mode-alist)

            ;(local-set-key [tab] 'indent-relative)
            (local-set-key [return] 'idlwave-newline)       
            (local-set-key [f5] 'idlwave-shell-break-here)
            (local-set-key [f6] 'idlwave-shell-clear-current-bp)
            (local-set-key [f7] 'idlwave-shell-cont)
            (local-set-key [f8] 'idlwave-shell-clear-all-bp)

            (local-unset-key "\M-s")
            ;; If my finger wanders up to Escape
            (local-set-key [?\e?\t] 'idlwave-complete)
            (setq                        ; Set Options Here
             ;; Gotta have that smart-continue-indenting
             idlwave-max-extra-continuation-indent 100
             idlwave-expand-generic-end t
             idlwave-completion-show-classes 10
             idlwave-store-inquired-class t
             idlwave-block-indent 3          ; Proper Indentation settings
             ;; Any self-respecting programmer indents his main block
             idlwave-main-block-indent 2
             idlwave-end-offset -3
             idlwave-continuation-indent 3
             font-lock-maximum-decoration 3

             ;; Ahh, mixed case for nearly everything.  Only upcase keywords.
             idlwave-completion-case '((routine . preserve)
                                       (keyword . upcase)
                                       (class . preserve)
                                       (method . preserve))
             idlwave-shell-automatic-start t
             ;; A little trick to help fill routine descriptions with
             ;; fancy line delimiters (like =*=*=*=*...).     
             paragraph-separate "[ \t\f]*$\\|[ \t]*;+[ \t]*$\\|;+[+=-_*]+$"

             ;; These are in too many classes, so query for them.
             idlwave-query-class '((method-default . nil)
                                   (keyword-default . nil)
                                   ("INIT" . t) 
                                   ("CLEANUP" . t)
                                   ("SETPROPERTY" .t)
                                   ("GETPROPERTY" .t)))
            
            ;; Some personal abbreviations
            (idlwave-define-abbrev "wb" "widget_base()"
                                   (idlwave-keyword-abbrev 1))
            (idlwave-define-abbrev "on" "obj_new()"
                                   (idlwave-keyword-abbrev 1))
            (idlwave-define-abbrev "fn1" "for i=0,n_elements()-1 do"
                                   (idlwave-keyword-abbrev 6))))


(add-hook 'idlwave-shell-mode-hook
          (lambda ()
            ;; A cheat for quick function-only lookup in the shell.
            (local-set-key "\M-\t" 
                           '(lambda ()
                              (interactive)
                              (idlwave-complete 3)))

            (local-set-key [C-kp-begin] 'comint-kill-input)
            (local-set-key [f5] 'idlwave-shell-break-here)
            (local-set-key [f6] 'idlwave-shell-clear-current-bp)
            (local-set-key [f7] 'idlwave-shell-cont)
            (local-set-key [f8] 'idlwave-shell-clear-all-bp)
            (setq 
             ;; The best of both worlds: arrows move up and down, or
             ;; recall history if on the command line.
             idlwave-shell-arrows-do-history 'cmdline
             idlwave-shell-overlay-arrow "=>")
            (define-key (current-local-map) "\C-a" 'comint-bol)
            (define-key (current-local-map) [kp-multiply]  'comint-bol)

            ;; Some custom examines and other bindings    
            (idlwave-shell-define-key-both 
             [s-down-mouse-2] 
             (idlwave-shell-mouse-examine "print, size(___,/DIMENSIONS)"))
            (idlwave-shell-define-key-both 
             [f9] (idlwave-shell-examine "print, size(___,/DIMENSIONS)"))
            (idlwave-shell-define-key-both 
             [f10] (idlwave-shell-examine "print,size(___,/TNAME)"))
            (idlwave-shell-define-key-both 
             [f11] (idlwave-shell-examine "help,___,/STRUCTURE"))
            (idlwave-shell-define-key-both
             [f4] 'idlwave-shell-retall)
            (idlwave-shell-define-key-both
             [f3] (lambda () (interactive)
                    (idlwave-shell-send-command 
                     "__wa=widget_info(/managed) & for i=0,n_elements(__wa)-1 do widget_control,__wa[i], /clear_events" nil 'hide)
                    (idlwave-shell-retall)))
            (local-set-key
              [f5]  
              '(lambda () (interactive) 
                 (insert "n_elements()") (backward-char)))
            (define-key (current-local-map) 
              [f6]  
              '(lambda () (interactive) 
                 (insert "size()") (backward-char)))
            (define-key (current-local-map) 
              [f8]  
              '(lambda () (interactive) 
                 (insert "__define")))))

(add-hook 'idlwave-mode-hook 'my-common-idlwave-hook 'append)
(add-hook 'idlwave-shell-mode-hook 'my-common-idlwave-hook 'append)