.emacs

  1. ;; user information
  2. (setq user-full-name "Giuseppe Coviello")
  3. (setq user-mail-address "cjg@cruxppc.org")
  4.  
  5. ;; setting load-path
  6. (setq load-path (cons "~/.emacs.d/" load-path))
  7. (setq load-path (cons "~/.emacs.d/color-theme/" load-path))
  8.  
  9. ;; start emacs server
  10. (server-start)
  11.  
  12. ;; interface
  13. ;; console specific interface customization
  14. (defun console-interface()
  15.   (menu-bar-mode nil)
  16.   (color-theme-pok-wob)
  17.   (display-time-mode)
  18.   (require 'battery)
  19.   (display-battery-mode)
  20.   )
  21.  
  22. ;; X specific interface customization
  23. (defun x-interface()
  24.   (setq x-select-enable-clipboard t)
  25.   (tool-bar-mode nil)
  26.   (color-theme-blippblopp)
  27.   (setq frame-title-format "Emacs: %b %+%+ %f")
  28.   (setq icon-title-format "Emacs – %b")
  29.   (pc-bindings-mode)
  30.   (pc-selection-mode)
  31.   (set-scroll-bar-mode 'right)
  32.  
  33.   ;; speedbar
  34.   (autoload 'speedbar-frame-mode "speedbar" "Popup a speedbar frame" t)
  35.   (autoload 'speedbar-get-focus "speedbar" "Jump to speedbar frame" t)
  36.   (define-key-after (lookup-key global-map [menu-bar tools])
  37.     [speedbar] '("Speedbar" . speedbar-frame-mode) [calendar])
  38.   (setq php-speedbar-config 1)
  39.  
  40.   ;; wheel mouse
  41.   (defun up-slightly () (interactive) (scroll-up 5))
  42.   (defun down-slightly () (interactive) (scroll-down 5))
  43.   (global-set-key [mouse-4] 'down-slightly)
  44.   (global-set-key [mouse-5] 'up-slightly)
  45.  
  46.   (defun up-one () (interactive) (scroll-up 1))
  47.   (defun down-one () (interactive) (scroll-down 1))
  48.   (global-set-key [S-mouse-4] 'down-one)
  49.   (global-set-key [S-mouse-5] 'up-one)
  50.  
  51.   (defun up-a-lot () (interactive) (scroll-up))
  52.   (defun down-a-lot () (interactive) (scroll-down))
  53.   (global-set-key [C-mouse-4] 'down-a-lot)
  54.   (global-set-key [C-mouse-5] 'up-a-lot)
  55.  
  56.   ;; avoid mouse pointer
  57.   (require 'avoid)
  58.   (if (display-mouse-p) (mouse-avoidance-mode 'animate))
  59.   )
  60.  
  61. ;; MACOSX specific interface customization
  62. (defun mac-interface()
  63.   (setq mac-allow-anti-aliasing t)
  64.   (x-interface)
  65.   (custom-set-faces
  66.    '(hl-line ((t (:background "lightgray")))))
  67.   )
  68.  
  69. ;; global interface customization
  70. (require 'font-lock)
  71. (require 'color-theme)
  72. (color-theme-initialize)
  73. (setq inhibit-startup-message t)
  74. (fset 'yes-or-no-p 'y-or-n-p)
  75. (setq-default transient-mark-mode t)
  76. (blink-cursor-mode nil)      
  77. (setq track-eol t)    
  78. (line-number-mode t)  
  79. (setq column-number-mode t)
  80. (setq scroll-step 1)      
  81. (setq query-replace-highlight t)  
  82. (setq search-highlight t)        
  83. (show-paren-mode t)              
  84. (global-font-lock-mode t)
  85. (setq font-lock-maximum-decoration t
  86.       font-lock-mode-maximum-decoration t
  87.       font-lock-maximum-size nil)
  88. (setq-default enable-multibyte-characters t)
  89. (setq default-input-method "rfc1345")
  90. (toggle-uniquify-buffer-names)
  91. (setq gdb-many-windows t)
  92. (require 'hl-line)
  93. (global-hl-line-mode)
  94.  
  95. (defun shell-mode-customize()
  96.   "Customize the shell-mode"
  97.   (load-library 'ansi-color)
  98.   (ansi-color-for-comint-mode-on)
  99.   )
  100. (add-hook 'shell-mode-hook 'shell-mode-customize)
  101.  
  102. (if (not window-system)
  103.     (console-interface)
  104.   )
  105. (if (eq window-system 'x)
  106.     (x-interface)
  107.   )
  108. (if (eq window-system 'mac)
  109.     (mac-interface)
  110.   )
  111.  
  112. ;; editing
  113. (require 'completion)
  114. (dynamic-completion-mode)
  115. (require 'generic-x)
  116. (recentf-mode t)
  117. (toggle-save-place-globally)
  118. (set-input-mode nil nil 1)
  119. (set-default 'truncate-lines 1)
  120. (setq-default fill-column 80)      
  121. (setq-default auto-fill-function 'do-auto-fill)                              
  122. (setq next-line-add-newlines nil)
  123. (delete-selection-mode t)
  124. (setq require-final-newline 't)
  125. (require 'htmlize)
  126.  
  127. ;; key bindings
  128. (global-set-key "\C-a" 'mark-whole-buffer)
  129. (global-set-key "\C-z" 'undo)
  130. (global-set-key "\C-cg" 'goto-line)
  131. (global-set-key "\C-b" 'buffer-menu)
  132. (global-set-key "\M-a" 'yic-next-buffer)
  133. (global-set-key "\M-d" 'yic-prev-buffer)
  134. (global-set-key "\M-\S-a" 'previous-multiframe-window)
  135. (global-set-key "\M-\S-d" 'next-multiframe-window)
  136. (global-set-key "\M-s" 'delete-other-windows)
  137. (global-set-key "\M-\S-s" 'kill-buffer-and-window)
  138. (global-set-key "\M-c" 'clipboard-kill-ring-save)
  139. (global-set-key "\M-v" 'clipboard-yank)
  140. (global-set-key "\M-f" 'clipboard-kill-region)
  141. (global-set-key "\M-w" 'set-mark-command)
  142. (global-set-key "\M-q" 'comment-region)
  143. (global-set-key "\M-e" 'uncomment-region)
  144. (global-set-key (kbd "(") 'insert-round-brackets)
  145. (global-set-key (kbd "[") 'insert-square-brackets)
  146. (global-set-key (kbd "{") 'insert-brackets)
  147.  
  148. ;; mode customization
  149. ;; latex
  150. (defun latex-mode-fix ()
  151.   (local-unset-key "\M-s")
  152.   (local-unset-key (kbd "<tab>"))
  153.   (local-set-key "\M-s" 'delete-other-windows)
  154.   (local-set-key (kbd "<tab>") 'indent-or-complete)
  155.   )
  156. (add-hook 'LaTeX-mode-hook 'latex-mode-fix)
  157.  
  158. ;; python
  159. (defun python-mode-fix ()
  160.   (setq py-smart-indentation nil
  161.         py-indent-offset 4
  162.         tab-width 4
  163.         indent-tabs-mode nil
  164.         current-language-environment "UTF-8"
  165.         )
  166.   (global-set-key [f5] 'py-execute-buffer)
  167.   (local-set-key (kbd "<tab>") 'indent-or-complete)
  168.   )
  169. (add-hook 'python-mode-hook 'python-mode-fix)
  170.  
  171. ; crux initscripts and Pkgfile
  172. (add-to-list 'auto-mode-alist '("/etc/rc.d/*" . sh-mode))
  173. (add-to-list 'auto-mode-alist '("Pkgfile" . sh-mode))
  174.  
  175. ;; asm motorola 68k
  176. (add-to-list 'auto-mode-alist '("\\.a68$" . asm-mode))
  177.  
  178. ; C
  179. (defun linux-c-mode ()
  180.   "C mode with adjusted defaults for use with the Linux kernel."
  181.   (interactive)
  182.   (c-mode)
  183.   (setq c-indent-level 8)
  184.   (setq c-brace-imaginary-offset 0)
  185.   (setq c-brace-offset -8)
  186.   (setq c-argdecl-indent 8)
  187.   (setq c-label-offset -8)
  188.   (setq c-continued-statement-offset 8)
  189.   (setq indent-tabs-mode nil)
  190.   (setq tab-width 8))
  191.  
  192. (setq auto-mode-alist (cons '("*\\.[ch]$" . linux-c-mode)
  193.                        auto-mode-alist))
  194. ;; Linux C style
  195. (defconst linux-c-style
  196.   '(
  197.     (c-set-style . "k&r")
  198.     (c-indent-level 8)
  199.     (c-brace-imaginary-offset 0)
  200.     (c-brace-offset -8)
  201.     (c-argdecl-indent 8)
  202.     (c-label-offset -8)
  203.     (c-continued-statement-offset 8)
  204.     (indent-tabs-mode nil)
  205.     (c-tab-width . 8)
  206.     (c-basic-offset . 8)
  207.     (comment-multi-line t)
  208.     )
  209.   "Linux C Style"
  210.   )
  211.  
  212. (defun linux-c-mode-common-hook ()
  213.   (c-add-style "Linux" linux-c-style t)
  214.   (cwarn-mode 1)
  215.   (which-function-mode t)
  216.   (setq which-func-unknown "TOP LEVEL")
  217.   (setq compile-command "make CC=gcc")
  218.   (local-set-key "\C-c\C-c" 'compile)
  219.   (local-set-key "\C-c\C-d" 'gdb)
  220.   (local-set-key "\M-m" 'man-follow)
  221.   (local-set-key (kbd "<tab>") 'indent-or-complete)
  222.   (local-set-key (kbd "(") 'insert-round-brackets)
  223.   (local-set-key (kbd "[") 'insert-square-brackets)
  224.   (local-set-key (kbd "{") 'insert-brackets)
  225.   (require 'compile)
  226.   (unless (file-exists-p "Makefile")
  227.     (set (make-local-variable 'compile-command)
  228.          (let ((file (file-name-nondirectory buffer-file-name)))
  229.            (concat "gcc -g -W -Wall -o "
  230.                    (file-name-sans-extension file) " " file))))
  231.   )
  232. (add-hook 'c-mode-common-hook 'linux-c-mode-common-hook)
  233.  
  234. (add-hook 'c-mode-hook
  235.           (lambda ()
  236.             (font-lock-add-keywords nil
  237.                     '(("\\<\\(FIXME\\):" 1 font-lock-warning-face prepend)
  238.                       ("\\<\\(TODO\\):" 1 font-lock-type-face prepend)))
  239.             )
  240.           )
  241.  
  242. ;; c-sharp
  243. (autoload 'csharp-mode "csharp-mode" "Major mode for editing C# code." t)
  244. (setq auto-mode-alist
  245.       (append '(("\\.cs$" . csharp-mode)) auto-mode-alist))
  246.  
  247. ;; C# style
  248. (defconst csharp-style
  249.   '(
  250.     (c-set-style . "java")
  251.     (c-indent-level 4)
  252.     (c-brace-imaginary-offset 0)
  253.     (c-brace-offset -4)
  254.     (c-argdecl-indent 4)
  255.     (c-label-offset -4)
  256.     (c-continued-statement-offset 4)
  257.     (indent-tabs-mode nil)
  258.     (c-tab-width . 4)
  259.     (c-basic-offset . 4)
  260.     (comment-multi-line t)
  261.     )
  262.   "C# Style"
  263.   )
  264.  
  265. (defun csharp-mode-fix ()
  266.   (interactive)
  267.   (c-add-style "C#" csharp-style t)
  268.   (setq tab-width 4
  269.         current-language-environment "UTF-8"
  270.         )
  271.   (local-set-key (kbd "<tab>") 'indent-or-complete)
  272.   (require 'compile)
  273.   (unless (file-exists-p "Makefile")
  274.     (set (make-local-variable 'compile-command)
  275.          (let ((file (file-name-nondirectory buffer-file-name)))
  276.            (concat "mcs " file))))
  277.  )
  278. (add-hook 'csharp-mode-hook 'csharp-mode-fix)
  279.  
  280. ;; skeletons
  281. (define-skeleton c-skeleton
  282.   "Inserts a C file skeleton into current buffer.
  283.   This only makes sense for empty buffers."\n
  284.   "/* " (buffer-name) " */"\n
  285.   "
  286. /* <project_name> -- <project_description>
  287. *
  288. * Copyright (C) 2006 - 2007
  289. *     "(insert user-full-name)" <"(insert user-mail-address)">
  290. *
  291. * This program is free software; you can redistribute it and/or modify
  292. * it under the terms of the GNU General Public License as published by
  293. * the Free Software Foundation; either version 2 of the License, or
  294. * (at your option) any later version.
  295. *
  296. * This program is distributed in the hope that it will be useful,
  297. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  298. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  299. * GNU General Public License for more details.
  300. *
  301. * You should have received a copy of the GNU General Public License
  302. * along with this program; if not, write to the Free Software
  303. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  304. */
  305. "\n
  306. )
  307.  
  308. (define-skeleton h-skeleton
  309.   "Inserts a C include file skeleton into current buffer.
  310.   This only makes sense for empty buffers."\n
  311.   "/* " (buffer-name) " */"\n
  312.   "
  313. /* <project_name> -- <project_description>
  314. *
  315. * Copyright (C) 2006 - 2007
  316. *     "(insert user-full-name)" <"(insert user-mail-address)">
  317. *
  318. * This program is free software; you can redistribute it and/or modify
  319. * it under the terms of the GNU General Public License as published by
  320. * the Free Software Foundation; either version 2 of the License, or
  321. * (at your option) any later version.
  322. *
  323. * This program is distributed in the hope that it will be useful,
  324. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  325. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  326. * GNU General Public License for more details.
  327. *
  328. * You should have received a copy of the GNU General Public License
  329. * along with this program; if not, write to the Free Software
  330. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  331. */
  332.  
  333. #ifndef _" (upcase (replace-regexp-in-string "\\." "_" (buffer-name))) "
  334. #define _" (upcase (replace-regexp-in-string "\\." "_" (buffer-name))) "
  335.  
  336. #endif /* _" (upcase (replace-regexp-in-string "\\." "_" (buffer-name))) " */
  337. "\n
  338. )
  339.  
  340. (define-skeleton python-skeleton
  341.   "Inserts a Python file skeleton into current buffer.
  342.   This only makes sense for empty buffers."\n
  343. "\"\"\"
  344.    " (buffer-name) "
  345.    
  346.    <project_name> -- <project_description>
  347.  
  348.    Copyright (C) 2006 - 2007
  349.        "(insert user-full-name)" <"(insert user-mail-address)">
  350.  
  351.    This program is free software; you can redistribute it and/or modify
  352.    it under the terms of the GNU General Public License as published by
  353.    the Free Software Foundation; either version 2 of the License, or
  354.    (at your option) any later version.
  355.  
  356.    This program is distributed in the hope that it will be useful,
  357.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  358.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  359.    GNU General Public License for more details.
  360.  
  361.    You should have received a copy of the GNU General Public License
  362.    along with this program; if not, write to the Free Software
  363.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  364. \"\"\"
  365. "\n
  366. )
  367.  
  368. ;; defun(s)()
  369.  
  370. (defun yic-ignore (str)
  371.   (or
  372.    (string-match "\\*Buffer List\\*" str)
  373.    (string-match "\\*scratch\\*" str)
  374.    (string-match "^TAGS" str)
  375.    (string-match "^\\*Messages\\*$" str)
  376.    (string-match "^\\*Completions\\*$" str)
  377.    (string-match "^ " str)
  378.    (memq str              
  379.          (mapcar
  380.           (lambda (x)
  381.             (buffer-name
  382.              (window-buffer
  383.               (frame-selected-window x))))
  384.           (visible-frame-list)))
  385.    ))
  386.  
  387. (defun yic-next (ls)
  388.   "Switch to next buffer in ls skipping unwanted ones."
  389.   (let* ((ptr ls)
  390.          bf bn go
  391.          )
  392.     (while (and ptr (null go))
  393.       (setq bf (car ptr)  bn (buffer-name bf))
  394.       (if (null (yic-ignore bn))        
  395.           (setq go bf)
  396.         (setq ptr (cdr ptr))
  397.         )
  398.       )
  399.     (if go
  400.         (switch-to-buffer go))))
  401.  
  402. (defun yic-prev-buffer ()
  403.   "Switch to previous buffer in current window."
  404.   (interactive)
  405.   (yic-next (reverse (buffer-list))))
  406.  
  407. (defun yic-next-buffer ()
  408.   "Switch to the other buffer (2nd in list-buffer) in current window."
  409.   (interactive)
  410.   (bury-buffer (current-buffer))
  411.   (yic-next (buffer-list)))
  412.  
  413. (defun indent-or-complete ()
  414.   "Complete if point is at end of a word, otherwise indent line."
  415.   (interactive)
  416.   (if (looking-at "\\>")
  417.       (complete-symbol)
  418.     (indent-for-tab-command)))
  419.  
  420. (defun insert-round-brackets ()
  421.   "Insert round brackets"
  422.   (interactive)
  423.   (insert "()")
  424.   (backward-char)
  425.   (print (current-word))
  426. )
  427.  
  428. (defun insert-square-brackets ()
  429.   "Insert square brackets"
  430.   (interactive)
  431.   (insert "[]")
  432.   (backward-char)
  433. )
  434.  
  435. (defun insert-brackets ()
  436.   "Insert brackets"
  437.   (interactive)
  438.   (if (and (not (eq (char-before) nil)) (not (= (char-before) ?\ )))
  439.       (insert " "))
  440.   (insert "{")
  441.   (reindent-then-newline-and-indent)
  442.   (newline)
  443.   (insert "}")
  444.   (indent-according-to-mode)
  445.   (backward-to-indentation)
  446. )
  447.  
  448. (defun todo-list ()
  449.   "Find TODOs"
  450.   (interactive)
  451.   (grep (concat "grep -nH -e 'TODO:' -e *.[ch] "
  452.                 "| awk '{ sub(/\\/\\//, \"\"); sub(/\\/\\*/, \"\"); "
  453.                 "gsub (/:[\\ \\t]*TODO/, \": TODO\"); print }' 1>&2 | true")
  454.         )
  455. )
  456.  
  457. (defun fixme-list ()
  458.   "Find FIXMEs"
  459.   (interactive)
  460.   (grep (concat "grep -nH -e 'FIXME:' *.[ch] "
  461.                 "| awk '{ sub(/\\/\\//, \"\"); sub(/\\/\\*/, \"\"); "
  462.                 "gsub (/:[\\ \\t]*FIXME/, \":\ FIXME\"); print }' 1>&2 | true")
  463.         )
  464. )
  465.  
  466. (defun todo-and-fixme-list ()
  467.   "Find TODOs and FIXMEs"
  468.   (interactive)
  469.   (grep (concat "grep -nH -e 'TODO:' -e 'FIXME:' *.[ch] "
  470.                 "| awk '{ sub(/\\/\\//, \"\"); sub(/\\/\\*/, \"\"); "
  471.                 "gsub (/:[\\ \\t]*TODO/, \": TODO\"); "
  472.                 "gsub (/:[\\ \\t]*FIXME/, \":\ FIXME\"); print }' 1>&2 | true")
  473.         )
  474. )