;; -*-Emacs-Lisp-*-

;;GLOS (Graphic Library in Open Source), an ANSI Common Lisp OpenGL subset.
;;Copyright (C) 2000 the GLOS development team (http://glos.sourceforge.net)

;; -fn fixed necessaire sur terminal


;; for beginners:
;; C-a means "press 'Ctrl', then 'a' without releasing 'Ctrl'
;; M "Meta" designates the Left-Alt key.
;; rappel: toutes les fonctions nommées (e.g beginning-of-line) peuvent être invoquées par: M-x beginning-of-line
;; M-x describe-key C-a displays the name of the function invoked by C-a shortcut, i.e beginning-of-line
;; M-x apropos keyword: lists all functions names containing keyword



;;##########################################################
;; Fontes
;; Rem: la commande xlsfonts permet de lister l'ensembles
;; des fontes installées sur le système

;;cf: describe-fonset, completion sur fontes dispo a la commande set-default-font
(set-default-font "-dec-terminal-bold-r-normal-*-*-140-*-*-c-*-iso8859-1")
;;(set-default-font "-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1")
;;***Fin Fontes


;;##########################################################
;; Fichiers de définition
;;
(setq load-path (cons (expand-file-name "~/emacs/") load-path))
;;(add-to-list 'load-path "~/emacs/PACKAGES/slime-0.2")

;;inclusion des fonctions personnelles
;;check pour apprendre les bases d'emacs-lisp
(load-file "~/emacs/Vectra.el")

;;inclusion de gestion langue et accents
(load-file "~/emacs/Langue.el")

;;gestion des mails - newsgroups
;;(load-file "~/emacs/Net.el")

;;gestion des couleurs
(load-file "~/emacs/Colors.el")

;;Gestion du langage SML/NJ
;;(load-file "~/emacs/sml.el")

;;Mode Lisp
(load-file "~/emacs/Lisp.el")

;;Mode Latex
(load-file "~/emacs/Latex.el")

;;***Fin fichiers définition


;;##########################################################
;; Section Mode des fichiers
;; ("\\.java\\'" . java-mode)
(setq vectra-extensions
      (list '("\\.c++\\'"    . c++-mode)
	    '("\\.stk\\'"    . scheme-mode)
	    '("\.ss$"     . scheme-mode)
	    '("\.stklos$" . scheme-mode)
	    '("\.sml$"    . sml-mode)
	    '("\.ML$"     . sml-mode)
	    '("\.SML$"    . sml-mode)
	    '("\.html$"   . html-mode)
	    '("\.htm$"    . html-mode)
	    '("\.shtml$"  . html-mode)
	    '("\.tex$"    . latex-mode)
	    '("\.gps$"    . shell-script-mode)
	    '("\.sh$"     . shell-script-mode)
	    '("^.*\.perso$" . shell-script-mode)
	    '("\.system$" . lisp-mode)
	    '("\.el$"     . emacs-lisp-mode)
	    '("\.lex$"    . text-mode)
	    '("\.lisp"    . lisp-mode)
	    '("\.lsp$"    . lisp-mode)
	    '("\.tcl$"    . tcl-mode)
	    '("\.cl$"     . lisp-mode)
	    '("^.akefile$"  . makefile-mode)
	    ))

(setq auto-mode-alist (append vectra-extensions auto-mode-alist))





;;##########################################################
;; Section Fonctions Evenementielles
;; Configuration de touches/prefs spécifiques à un mode donné

;;------------------------------
;;Mode texte
(add-hook 'text-mode-hook
	  (function (lambda ()
		      ;;(auto-fill-mode)      ;; casser automatiquement lignes trop longues
                      (iso-accents-mode t)
		      (ispell-minor-mode))))  ;; activation de la correction automatique

;;------------------------------		      
;;Mode C
(setq c-mode-hook
      (lambda ()
	(font-lock-mode 1)                                ;; syntax highlighting
	(make-local-variable   'compile-command)          ;; commande apellée par [f9]
	(setq compile-command "make -k")                  ;;
	(c-set-style "GNU")                               ;; mode d'indentation de code (indentation de ligne par tab, de fonction par c-indent-defun ou de buffer)
      	(local-set-key [f4]    'vectra-C-comment-line)    ;; commenter la ligne courante par fonction custom dans Vectra.el (nb: se référer plutot aux fonctions du mode)
	;;	(setq c-auto-newline t)
	))


;;------------------------------
;;Mode C++

;; essai
(defun vtk-link ()
  (setq compile-command link-command)
  (compile)
  (setq compile-command compile-bak))


(setq c++-mode-hook
      (lambda ()
	(font-lock-mode 1)
	;;	(require 'c-font-lock-keywords) marche pas bien
	(c-set-style "GNU")
	(local-set-key [f4]    'vectra-C++-comment-line)
	(make-local-variable   'compile-command)
	(make-local-variable   'link-command)
	(setq compile-command "g++ -c current-buffer")
	(setq compile-bak compile-command)
	(setq link-command "g++ $FILE.o ../Alignment.o $AIRVOL_OBJ -o $FILE $VTK_LINK")  ;; FILE=toto; 
	(global-set-key [f9]     'compile)              ;;launch compilation in a window
	(global-set-key [\C-f9]  'vtk-link)                 ;;launch vtk linking in a window
	;; 	(load-library "cc-cmds")                ;;highlighting C++: pas au point
	;;	(c-toggle-auto-state)                   ;; forces "newline" after ;, {... 
	;;	(c-toggle-hungry-state)                 ;; eats empty lines when erasing one 
	))


;;------------------------------
;;Mode Java
(setq java-mode-hook
      (lambda ()
	(font-lock-mode 1)
	(make-local-variable   'compile-command)
	(local-set-key [f4]    'vectra-C++-comment-line)
	(load-library "cc-cmds")
;;	(c-toggle-auto-state)
;;	(c-toggle-hungry-state)
	))


;;------------------------------
;;Mode Tcl
(setq tcl-mode-hook
      (lambda ()
	(font-lock-mode 1)
	(local-set-key [f4]    'vectra-diese-comment-line)
	))


;;------------------------------
;;Mode Scheme
(setq scheme-mode-hook
      (lambda ()
	(font-lock-mode 1)
	(local-set-key [f4]    'vectra-semicolon-comment)
	))


;;------------------------------
;;Mode Shell (command line)
(setq shell-mode-hook
      (lambda ()
	(font-lock-mode 1)
	(setq truncate-lines t)
	(local-set-key [f4] 'vectra-diese-comment)
	(local-set-key [C-up] 'comint-previous-input)         ;; last command in history
	(local-set-key [M-up] 'comint-backward-matching-input)
	))


;;------------------------------
;;eshell, c'est pourri
(add-hook 'eshell-mode-hook
	  (function (lambda ()
		      (local-set-key [up] 'previous-line)
		      (local-set-key [down] 'next-line))))


;;------------------------------
;;Mode Shell-script (script editing)
(setq sh-mode-hook
      (lambda ()
	(font-lock-mode 1)
	(local-set-key [f4] 'vectra-diese-comment)
	))


;;------------------------------
;;Mode Makefile
(setq makefile-mode-hook
      (lambda ()
	(font-lock-mode 1)
	(local-set-key [f4] 'vectra-diese-comment)
	))


;;------------------------------
;;Mode Emacs-Lisp (.emacs)
(setq emacs-lisp-mode-hook
      (lambda ()
	(font-lock-mode 1)
	(local-set-key [f4] 'vectra-semicolon2-comment)
	(local-set-key [f9] 'eval-current-buffer)
	))


;;------------------------------
;;Mode SGML-HTML
(setq sgml-mode-hook 
      (lambda ()
	(font-lock-mode 1)
	(auto-fill-mode)
	(local-set-key [f4] 'vectra-html-comment)
	))


;;------------------------------
;;Mode SML
;;qd le mode sml est initialise sous emacs
;;sml-mode-hook: a chaque ouverture d'un fichier sml
(setq sml-mode-hook 
      (lambda ()
	(require 'sml-mode)
	(font-lock-mode 1)
	(setq sml-indent-level 2)  
	))


;;------------------------------
;;Mode LaTEX ; ici, latex->auctex, sinon tex suffit aussi

(defun latexs-hook ()
  (font-lock-mode 1)
  ;;lignes "cassees" automatiquement: lourd
  ;;(auto-fill-mode)
  (ispell-minor-mode)
  (setq ispell-dictionnary "francais")
  (local-set-key [f4] 'vectra-percent-comment)
  (local-set-key [C-4] 'vectra-surround-dollar))


(setq latex-mode-hook (lambda () (latexs-hook)))
(setq LaTeX-mode-hook (lambda () (latexs-hook)))  ;;auctex
(setq tex-mode-hook (lambda () (latexs-hook)))


(setq bibtex-mode-hook
      (lambda ()
	(font-lock-mode 1)
	(iso-accents-mode)
	(local-set-key [f4] 'vectra-percent-comment)
	))


;;------------------------------
;;Mode Matlab
(autoload 'matlab-mode "matlab" "Enter Matlab mode." t)
(setq auto-mode-alist (cons '("\\.m$" . matlab-mode) auto-mode-alist))

(defun my-matlab-mode-hook ()
  (setq matlab-indent-function t) ; if you want function bodies indented
  (setq fill-column 76)			; where auto-fill should wrap
  (matlab-mode-hilit)
  (turn-on-auto-fill))

(setq matlab-mode-hook 'my-matlab-mode-hook)
(autoload 'matlab-shell "matlab" "Interactive Matlab mode." t)
(defun my-matlab-shell-mode-hook ()
  '())
(setq matlab-mode-hook 'my-matlab-mode-hook)

;;(add-hook 'latex-mode-hook '(lambda () (font-lock-mode 1)))
;;** Fin Section Fonctions Evenementielles



;;##########################################################
;; Section raccourcis clavier

;; Globals
;;(define-key global-map "\^x\^z" 'suspend-emacs)
;;(define-key global-map "\^z"    'suspend-emacs-old)

;; ---------------------------------
;; Meta
(global-set-key "\M-l"      'goto-line) 
(global-set-key "\M-o"      'other-window)           ;;déplace le curseur d'une fenêtre à l'autre
(global-set-key "\M-j"      'switch-to-buffer)       ;;invoquer un buffer chargé dans la fenêtre courante
(global-set-key "\M-[kp-1]" 'delete-other-windows)   
(global-set-key "\M-[kp-0]" 'delete-window)

(global-set-key "\M-u"      'upcase-region)          ;; notwkin with []
(global-set-key "\M-d"      'downcase-region)

;;par défaut, emacs propose mieux
;;(global-set-key "\M-y"      'rotate-yank-pointer)    ;;pops the stack of lines to yank with C-y

;;(global-set-key "\M-j" 'previous-multiframe-window)


;; ---------------------------------
;; touches de fonctions globales

;;f1 + combinaisons: réservé pour tags

(global-set-key [f2]     'split-window-horizontally) ;;creates another window in splitting current one
(global-set-key [f3]     'split-window-vertically)   ;;

;;f4: réservé pour commentaires

(global-set-key [f5]     'vectra-line-copy)          ;;copy line onto top of stack
(global-set-key [f6]     'yank)                      ;;paste top of selection stack 
                                                     ;;if you need to paste a selection not on top, yank once, then 
                                                     ;;use M-y to dynamically rotate among stack entries
                                                     ;;(before:M-y to pop copied selections stack)
(global-set-key [f7]     'undo)
(global-set-key [f8]     'shell)                     ;;shell bash in emacs (cool): run on window /go to shell window. 
;; M-x n-shell: VERY IMPORTANT!                      ;;open another shell (need to provide number of desired new shells in argt) 
(global-set-key [\C-f8]  'vectra-shell2)             ;;jump to second shell (opened with n-shell)
(global-set-key [\M-f8]  'vectra-shell3)             ;;3rd
;;(global-set-key [\C-f8]  'eshell)                  ;;un peu lourd
;; f9 est réservé pour les compilations
(global-set-key [f10]    'next-error)                ;;navigate through reported compilation errors
;;propose toutes les possibilites
;;(global-set-key [\C-SunF36] 'dabbrev-completion) 
(global-set-key [f11] 'dabbrev-expand)               ;;auto-completion of current word. thesaurus: all opened buffers. don't forget to use TAB to see completion options
                                                     ;;par défaut, la plus recente: appuyer plusieurs fois pour cycler
                                                     ;;'hippie-expand) [SunF36] : un peu lourd 
(global-set-key [\C-f11] 'lisp-complete-symbol)
(global-set-key [f12]    'vectra-indent-hilit-buffer)  ;;re-indente ET colorise le buffer
(global-set-key [\C-f12] 'vectra-hilit-buffer)         ;;re-colorise le buffer


;; ---------------------------------
;; ispell (correcteur orthographique)	
;;(global-set-key [\C-f1] 'ispell-buffer)
;;(global-set-key [\C-f2] 'ispell-region)
;;(global-set-key [\C-f3] 'ispell-word)


;; ---------------------------------
;; tags (need to invoke etags *.c *.h via shell on source files before)
(global-set-key [\f1] 'find-tag)                   ;;jump to symbol definition
(global-set-key [\M-f1] 'find-tag-other-window)    ;;idem, but jump in another window, so as to preserve current
(global-set-key [\C-f1] 'pop-tag-mark)             ;;if find-tag invoked, get back to where you were

(global-set-key [\C-f2] 'vectra-man-on-word)       ;;calls man into an emacs window (very cool)


;; ---------------------------------
;; mouvement
(global-set-key [\M-left]  'backward-sentence)
(global-set-key [\M-right] 'forward-sentence)
(global-set-key [\M-up]    'beginning-of-buffer)
(global-set-key [\M-down]  'end-of-buffer)
;;il y a déjà pas mal de fonctions similaires (beg-of-word, etc) assignées aux touches de direction avec C/M


;; ---------------------------------
;; search
;;C-s search forward
(global-set-key [\M-s] 'search-forward-regexp)
(global-set-key "\C-b" 'search-backward)         ;; for some reason, does not work with [] (std shortcut?)
(global-set-key [\M-b] 'search-backward-regexp)
(global-set-key "\C-q" 'query-replace)


;;***Fin Section Raccourcis clavier



;;##########################################################
;; Section Emacs Global Features


;; disabler lourdeurs graphiques des nouvelles versions d'emacs 
(scroll-bar-mode nil) 
;;(tool-bar-mode nil)
(menu-bar-mode nil) 

;; activer la gestion de la molette
(mouse-wheel-mode 1)  


;;Modes par défaut
(setq default-major-mode 'text-mode)
(setq ispell-dictionnary "english")  ;; langue par défaut du correcteur orthographique (invocable par ispell-buffer, par exple.)
(iso-accents-mode)                   ;; 'e -> é ; l' e -> l'e
(show-paren-mode t)                  ;; appariement visuel des (){}[]
(column-number-mode 1)               ;; affichage du numéro de colonne


(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)



;;##########################################################
;; Hints


;;capitalize: upcase. downcase. M-u, M-d
;;M-q: put line(s) into a proper PARAGRAPH. VERY COOL for text editing 
;;auto-fill-modes that cuts lines without your consent.
;;describe-key, describe-key-briefly: give command name tied to key
;;Ctrl-x r k : Couper un rectangle 
;;Ctrl-x r y : Inserer un rectangle 

;;copy-rectangle-to-register ; insert-register


;;idées:
;;normaliser fonctions de commentaires (utiliser celle du mode, faire gaffe a save path)
;;C-f4 insèrerait un séparateur: comment+ligne avec des étoiles selon le mode
