;;; init-prog-modes.el --- Initialize programming-related modes.

;;; Copyright (C) 2001 -- 2011 Nix <nix@esperi.org.uk>.

;; Author: Nix <nix@esperi.org.uk>
;; Created: 2001-07-20
;; Keywords: c lisp local

;; This file is not part of Emacs.

;; This library 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.

;;; Commentary:

;; This file does all the initialization that I need for programming-language-
;; specific stuff.

;;; Requirements:

; c-mode UI improvements.

(require 'cc-styles)                            ; For `c-make-styles-buffer-local'
(require 'cc-cmds)                              ; For `c-toggle-hungry-state'
;(require 'c-font-lock-keywords)                ; Insane colourization
(require 'cl)                                   ; For `pushnew'
(require 'semantic)                             ; For the semantic bovinator.
(require 'imenu)                                ; For imenu.
(require 'latex)
(require 'font-latex)
(require 'hideshow)

;;; Code:

;; Set up cc-mode.

(setq c-style-variables-are-local-p nil)        ; User-modified changes apply to all cc-mode buffers

(defun nix-setup-c-mode ()
 "Set up C, C++ and Java mode.

This is run in the C-mode-common-hook to set up indentation and other
parameters on creation of buffers managed by cc-mode.el for Nix's personal coding style."
 (c-add-style "nix" '((c-basic-offset . 1)      ; I use single-indentation
                      (c-recognize-knr-p . nil)  ; K&R? Blugh. Not usin' *that*.
                      (c-comment-only-line-offset . (0 . 0)) ; Do nil for lone comments
                      (c-block-comment-prefix . "") ; We don't use *-prefixed comments
                      (c-indent-comments-syntactically-p . t) ; Even with no code before them
                      (c-cleanup-list . (space-before-funcall compact-empty-funcall)) ; Make function calls look nice
                      (c-electric-pound-behavior . 'alignleft) ; Snap #s to the first column
                      (defun-prompt-regexp . " ") ; Regexp to find the starting brace of a block
                      (c-offsets-alist . ((defun-open  . +)
                                          (defun-close . 0)
                                          (class-open  . +)
                                          (class-close . +)
                                          (block-open  . +)
                                          (block-close . 0)
                                          (case-label  . +)
                                          (statement-case-intro . +)
                                          (inline-open . 3)
                                          (inline-close . 0)
                                          (extern-lang-open . +)
                                          (extern-lang-close . +)
                                          (access-label . 0)
                                          (substatement-open . +)
                                          (statement-cont . c-lineup-math)
                                          (do-while-closure . 0)
                                          (else-clause . 0))))))

(defun nix-clean-up-common-hook ()
 "Clean up the common hook.

Removes the other style setup functions from the hook, so that they
only get run once, rather than repeatedly."
 (remove-hook 'c-mode-common-hook 'nix-setup-c-mode)
 (remove-hook 'c-mode-common-hook 'nix-clean-up-common-hook))

(defun nix-setup-this-c-mode-buffer ()
 "Set up this buffer for C mode.
Things (like auto-hungry-state setting and style setting) that should not
be removed from the `c-mode-common-hook' after the first call by
`nix-clean-up-c-common-hook', but which should rather take effect
separately for each buffer."
 (if (gawd-personal-code-p)
     (progn
       (c-make-styles-buffer-local t)          ; Don't let this interfere with user styles
       (c-toggle-hungry-state 1)
       (c-set-style "nix"))))

(add-hook 'c-mode-common-hook 'nix-setup-c-mode t)
(add-hook 'c-mode-common-hook 'nix-clean-up-common-hook t)
(add-hook 'c-mode-common-hook 'nix-setup-this-c-mode-buffer t)

(setq gawd-personal-code-regexp (concat "^" (getenv "HOME") "/src/"))

;; Put the comments in the right place, and remember where they are between
;; sessions; remember the window width.

(setq-default fill-column 132                    ; This is the minimum of my window width and work's :)
              comment-column 48)                 ; My standard right-margin comment column

(if (not (memq 'comment-column desktop-locals-to-save))
    (setq-default desktop-locals-to-save (append desktop-locals-to-save '(comment-column)))) ; Save comment columns from buffers

;; Give me information on function parameters &c in Lisp as-I-type
;; (Others might find this annoying, so it's not site-wide.)

(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
(add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
(add-hook 'ielm-mode-hook 'turn-on-eldoc-mode)

;; Some Emacs Lisp code is automatically HTMLized and placed on the web, along
;; with a copy of its original.

(defvar nix-auto-htmlize-alist
  `((,(concat (regexp-quote (expand-file-name "~/")) "/lisp/emacs/personal/.*\\.el$") . "/home/nix/public_html/emacs/personal/")
    ,(concat (regexp-quote (expand-file-name "~/")) "/lisp/emacs/site-wide/.*\\.el$") . "/home/nix/public_html/emacs/site-wide/")
  "An alist stating what files to automatically HTMLize as they are saved.
It maps from regexps matching the names of files, to directories to put the
files in.  A copy of the original file is saved there, too.")

;; Diff hackery.

(setq ediff-auto-refine-limit 100000)

;; Beat AUC-TeX and preview-LaTeX into functioning properly.

(setq TeX-auto-save t
      TeX-parse-self t
      LaTeX-item-indent -6
      TeX-style-private (list (expand-file-name "~/Docs/LaTeX/Styles/auto"))
      TeX-debug-bad-boxes t
      TeX-debug-warnings t
      LaTeX-section-hook '(LaTeX-section-heading
                           LaTeX-section-title
                           LaTeX-section-toc
                           LaTeX-section-section
                           LaTeX-section-label))
(setq-default TeX-master nil)

;; Some keybindings.

(setq gawd-key-bind-alist
      (nconc gawd-key-bind-alist
             `((,hs-minor-mode-map
                ("A-h" . hs-hide-block)
                ("A-s" . 'hs-show-block)
                ("A-H" . 'hs-hide-all)
                ("A-S" . 'hs-show-all)
                ("A-R" . 'hs-show-region)))))

(provide 'init-prog-modes)