;; -*- Mode: Emacs-Lisp -*-
;; -*- lisp -*-
;---------------------------------------------------------------------
; $Id: .emacs,v 1.1 2001/10/31 11:20:17 amos-cvs Exp $
; Copyright (C) Jan Borsodi 1998-2001
;
; .emacs file composed with help from various sources by
; Marius Sundbakken <marius@fix.no> &
; Jan "Amos" Borsodi <jb@ez.no>
; Latest versions also contains lots of self-created code
;---------------------------------------------------------------------
; Site lisp files are collect from news:comp.emacs, news:gnu.emacs.sources and
; various web sites.

;; TODO:
;; Lookup word at cursor pos to check if it is a class, if it is open header file.
;; Create a variable with params for a known class
;; Auto update from internet
;; Faster auto include
;; Abstract the project functions to allow it to be used with more than just C++
;; Download external packages from news and web sites, parse them to find downloadable files,
;; then try to parse the files to find instructions for starting them.
;; Select auto insert directory depending on mode and project style.
;; Session management for projects.
;; Change the way the keys are set, find a more consistent way of setting them, also
;; make it possible to revert this changes.
;; Make good comments on all my functions and reorganize them in more files/packages, yikes!
;; Automaticly byte-compile all external packages on startup.
;; Find a good name.

;; Changelog
;; 0.8.1
;; New color mode called GhostDog, was designed to work with white
;;  background and black text desktops. The mode should be better
;;  to work with since it more reflects newspapers, books etc.
;;  It's currently not 100% done.

;; 0.8.0
;; BBDB bindings to Gnus
;; Made it easier to add new elements to the load-path by adding a new function to do the dirty work.
;; All code except some basic loading functions were moved to .emacs-d-startup
;; Inserting and deleting classes in a project is wastly improved, the 
;;  resulting project is sorted to make the list more viewable.
;; Emacs 21 support
;; Uses mouse-wheel from new Emacs instead of site-lisp version
;; Uses blinking cursor mode from new Emacs instead of site-lisp version
;; New site-lisp(and option), clipper, allows one to insert predefined clips in a buffer
;;  Keys are: C-x c c - Create clip, C-x c i - Insert clip, C-x c d - Delete clip
;;        and C-x c e - Edit clip
;; BBDB is now an option, and is loaded in .gnus. This means that people who don't use it(or GNUS)
;;  dont' need to turn it on.
;; New option: IDO, allows for better buffer switching and file finding.
;; Renamed and updated CUA-mode, new filename is cua.el

;; 0.7.8
;; Moved project related functions into .emacs-d-project
;; Moved autoinsert related functions into .emacs-d-autoinsert
;; Some minor code cleanups
;; Added support for namespaces in class generation
;; Added support for simple project management, a list of active projects are read from ~/.projects
;; and will open the project file/dir when selected in the menu (Files/Projects). Also if a Revive
;; buffer is stored it is restored.
;; Project loading can be done by executing project-load-project or pressing M-f6
;; Added INI mode support
;; Improved Tmake mode hightlighting
;; Added toggle source for PHP mode
;; Cursor is place on end of line when browsing minibuffer history
;; 0.7.7
;; Internal changes to make it run on XEmacs (again :), and it seems to work with NT-Emacs too,
;;  unfortunately a lot of things are disabled in XEmacs and NT-Emacs at the moment.
;; ibuffer in options menu
;; Uncomment comments with Alt u
;; M-d to set British dictionary
;; M-a to set American dictionary
;; M-n to set Norwegian dictionary
;; M-p to let flyspell check the buffer
;; The option file is saved to .xemacs-options if XEmacs is used.
;; More auto include classes, including most new Qt classes from 2.2.0
;; Added "Create Custom Files" to project menu, this allows for creating customized autoinsert files
;;  in the project directory amongst other things. Put yourself in a directory and choose 
;;  "Create Custom Files" from the menu, you will then get a directory called .autoinsert which is
;;  a copy of your standard autoinsert directory, edit this files to get custom looks on a specific
;;  project. You will also get a file called .emacs-custom and .emacs-vars, edit these files to suit
;;  your needs.
;; New autoinsert file for PHP coding.
;; Updated PHP mode which can handle "foreach" better.
;; 0.7.6
;; Lots of internal changes to make it run on XEmacs too.
;; 0.7.5
;; More robust initialization, will not fail to init if some packages are missing
;; More flexible autoinsert mechanism, will look for the auto-insert directory in a more dynamic matter
;; dependening on the state.
;; New configuration system, you can now save a global and local config file of how you want
;; emacs too be, you can also specify which packages to start. More options will come later.
;; 0.7.4-2
;; Added newer versions of JDE and PSGML, as well as Speedbar and Semantic.
;; 0.7.4
;; * Menuitem for recently opened files
;; * Rectangular marking with highlight and mouse support
;; * Revive mode, (enhanced desktop mode), saves window properties as well.
;; * History saving, now saves a lot of history buffers on exit and loads them on init
;; * Better scrolling when using page-up and page-down, the cursor now stays in place
;; * Font menu moved to Shift Right Mouse
;; 0.7.3
;; * Highlight current line mode added and updated color styles to set the color
;; * Auto revert mode, automaticly reverts buffers every 5 seconds.
;; * Cursor blinking
;; * Crontab mode
;; 0.7.2
;; * CSS mode included and bound to .css files

;; This emacs config will load these local files automagicly if they are found in
;; the current directory:
;; .emacs-vars          - Override global variables here
;; .emacs-classes	- You can add your own classes here (see .emacs-d-classes for example
;; .emacs-colors	- Override global colors here
;; .emacs-extras	- Add extra code and customization here
;; Since the files loaded are lisp files you can almost anything there but I advice only to do
;; what the file name says.

;; Some very basic variables and functions

(setq initial-frame-alist
      ;; scrollbars, frame width and height
      (list
	'(horizontal-scroll-bars . nil)
	'(vertical-scroll-bars . nil)
	'(background-color . "#456345"))
;;	(nconc (list 'top) default-frame-top)
;;	(nconc (list 'left) default-frame-left)
      )
(set-background-color "#456345")

(defvar site-lisp-path '("~/.site-lisp"
			 "~/site-lisp"
			 "/usr/share/emacs/site-lisp"
			 "/usr/local/share/emacs/site-lisp")
  "List of directories which should be used for appending as a `load-path'
used by `append-custom-load-path'")

(defun append-custom-load-path (name)
  "Appends the custom package NAME the the `load-path' by appending to it
by using all entries found in `site-lisp-path'. If an entry in
`site-lisp-path' does not exist and is not readable it is not used."
  (cond ((stringp name)
	 (mapcar '(lambda (path)
		    (and (file-exists-p path) (file-readable-p path)
			 (setq load-path (nconc (list (concat path "/" name)) load-path ))))
		 site-lisp-path))
	((listp name)
	 (mapcar 'append-custom-load-path
		 name))))

(defun lisp-file-exists (name)
  "Returns t if the emacs lisp file NAME is found in the `load-path'
otherwise nil. The lisp file is searched for with the suffix
.el and .elc"
  (let ((name-str (or (and (stringp name) name)
		      (prin1-to-string name)))
	(lst load-path)
	found)
    (while (and lst (not found))
      (if (or (file-exists-p (concat (car lst) "/" name-str ".el"))
	      (file-exists-p (concat (car lst) "/" name-str ".elc")))
	  (setq found t))
      (setq lst (cdr lst)))
    found))

(defun require-if (name)
  "Checks if the lisp file NAME is available in the `load-path'
\(see `lisp-file-exists'\) if it does it executes `require'
and returns the result otherwise returns nil."
  (and (lisp-file-exists name)
      (require name)))

;; Add .site-lisp and site-lisp in home directory to load path
;; allows people to add emacs files if access to the global site-lisp is not possible
(setq load-path (nconc '( "~/.site-lisp" ) load-path ))
(setq load-path (nconc '( "~/.emacs-defaults/site-lisp" ) load-path ))
(setq load-path (nconc '( "~/site-lisp" ) load-path ))

;; Various extra packages, they are more than one file and thus needs a separate directory
(append-custom-load-path (list "gnus" "semantic" "speedbar" "psgml" "jde/lisp" "mailcrypt" "bbdb" "else"
			       (and (< emacs-major-version 21) '("pre21"))))

(defun load-default-file-module (module)
  "Tries to load the default file \"~/.emacs-d-MODULE\" as a lisp file and execute it.
You'll usually call `load-file-module' instead of this directly."
  (interactive)
  (mapcar '(lambda (path)
	     (let ((def-module (concat path "/.emacs-d-" module)))
	       (if (file-exists-p def-module)
		   (load-file def-module))))
	  '("~/" "~/.emacs-defaults")))

(defun load-local-file-module (module)
  "Tries to load the local file \".emacs-d-MODULE\" as a lisp file and execute it
You'll usually call `load-file-module' instead of this directly."
  (interactive)
  (let ((loc-module (concat ".emacs-" module)))
    (if (file-exists-p loc-module)
	(load-file loc-module))))

(defun load-file-module (module)
  "Tries to load both the default and the local module file
see also `load-default-file-module' and `load-local-file-module'"
  (interactive)
  (load-default-file-module module)
  (load-local-file-module module))

;; Load the emacs type verifier first
(if (require-if 'emacs-type)
    (load-file-module "startup")
  (error "Could not load file emacs-type.el, make sure it is installed in one of your site-lisp dirs."))

