Cosmetics.
[elisp.git] / emacs.el
index 8d4c2f0..70b0552 100644 (file)
--- a/emacs.el
+++ b/emacs.el
@@ -1,4 +1,4 @@
-;; -*-Emacs-Lisp-*-
+;; -*- mode: Emacs-Lisp; mode: rainbow; -*-
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; This program is free software; you can redistribute it and/or         ;;
 ;; Contact <francois@fleuret.org> for comments & bug reports             ;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+;; Ugly hack because 23.2 is not installed properly in my Debian
+
+(when (and (>= emacs-major-version 23)
+           (>= emacs-minor-version 2))
+  (add-to-list 'load-path "/usr/share/emacs/site-lisp/vm/")
+  (add-to-list 'load-path "/usr/share/emacs/site-lisp/bbdb/lisp/")
+  (add-to-list 'load-path "/usr/share/emacs/site-lisp/mailcrypt/")
+  )
+
 ;; It's better to set the preferences in the .Xresources so that the
 ;; window is not first displayed with the wrong options
 
@@ -81,7 +90,7 @@
 (icomplete-mode 1)
 
 ;; (setq highlight-current-line-globally t
-      ;; highlight-current-line-ignore-regexp "Faces\\|Colors\\| \\*Mini\\|\\*media\\|INBOX")
+;; highlight-current-line-ignore-regexp "Faces\\|Colors\\| \\*Mini\\|\\*media\\|INBOX")
 
 ;; (highlight-current-line-minor-mode 1)
 ;; (highlight-current-line-set-bg-color "gray75")
@@ -328,11 +337,11 @@ load-warning buffer in case of failure."
 
 ;; ;; If my own letter icon is here, use it and change its color
 ;; (when (file-exists-p "~/local/share/emacs/letter.xbm")
-  ;; (setq-default display-time-mail-icon
-                ;; (find-image
-                 ;; '((:type xbm
-                          ;; :file "~/local/share/emacs/letter.xbm"
-                          ;; :ascent center)))))
+;; (setq-default display-time-mail-icon
+;; (find-image
+;; '((:type xbm
+;; :file "~/local/share/emacs/letter.xbm"
+;; :ascent center)))))
 
 ;; My funky setting of face colors. Basically, we switch to a sober
 ;; look and darken a bit the colors which need to (because of the
@@ -405,11 +414,11 @@ load-warning buffer in case of failure."
 
 (when window-system
   ;; (setq
-   ;; display-time-use-mail-icon t)
+  ;; display-time-use-mail-icon t)
 
   (ff/configure-faces
    '(
-     (escape-glyph :foreground "gray70" :weight 'bold)
+     (escape-glyph :foreground "#c0c0c0" :weight 'bold)
      (default :background "gray90" :foreground "black")
      (cperl-array-face :background "gray90" :foreground "blue" :weight 'bold)
      (cperl-hash-face :background "gray90" :foreground "purple" :weight 'bold)
@@ -425,7 +434,8 @@ load-warning buffer in case of failure."
      (font-lock-string-face :foreground "dark olive green")
      (font-lock-variable-name-face :foreground "sienna")
      (font-lock-function-name-face :foreground "blue4" :weight 'bold)
-     ;; (font-lock-comment-face :foreground "")
+     ;; (font-lock-comment-delimiter-face :foreground "dark violet")
+     ;; (font-lock-comment-face :foreground "dark violet")
      (flyspell-incorrect-face :foreground "red2")
      (flyspell-duplicate-face :foreground "OrangeRed2")
      (header-line :background "gray65")
@@ -650,10 +660,10 @@ occurrences "
 (setq ps-print-color-p nil
       ;; ps-paper-type 'letter
       ps-paper-type 'a4
-      ps-top-margin (* 1.75 56.692)
-      ps-left-margin 56.692
-      ps-bottom-margin 56.692
-      ps-right-margin 56.692
+      ;; ps-top-margin (* 1.75 56.692)
+      ;; ps-left-margin 56.692
+      ;; ps-bottom-margin 56.692
+      ;; ps-right-margin 56.692
 
       ;; Simple header. Remove that silly frame shadow.
       ps-print-header nil
@@ -672,6 +682,26 @@ occurrences "
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+;; http://blog.tuxicity.se/elisp/emacs/2010/03/26/rename-file-and-buffer-in-emacs.htm
+
+(defun rename-file-and-buffer ()
+  "Renames current buffer and file it is visiting."
+  (interactive)
+  (let ((name (buffer-name))
+        (filename (buffer-file-name)))
+    (if (not (and filename (file-exists-p filename)))
+        (message "Buffer '%s' is not visiting a file!" name)
+      (let ((new-name (read-file-name "New name: " filename)))
+        (cond ((get-buffer new-name)
+               (message "A buffer named '%s' already exists!" new-name))
+              (t
+               (rename-file name new-name 1)
+               (rename-buffer new-name)
+               (set-visited-file-name new-name)
+               (set-buffer-modified-p nil)))))))
+
+(global-set-key (kbd "C-c r") 'rename-file-and-buffer)
+
 (defun ff/non-existing-filename (dir prefix suffix)
   "Returns a filename of the form DIR/PREFIX[.n].SUFFIX whose file does
 not exist"
@@ -918,8 +948,8 @@ printer."
 
                              )
 
- ;;  display-time-format "%b %a %e %H:%M"
- ;;  display-time-mail-face nil
+ ;; display-time-format "%b %a %e %H:%M"
+ ;; display-time-mail-face nil
  )
 
 ;; Show the time, mail and stuff
@@ -1129,7 +1159,7 @@ goback argument, go back where we were."
     "The face to display known mail identities.")
 
   (defface ff/unknown-address-face
-    '((t (:foreground "red3")))
+    '((t (:foreground "red4")))
     "The face to display unknown mail identities.")
 
   (defun ff/explicit-name (email)
@@ -1158,7 +1188,7 @@ ff/known-address-face is used."
                                             (bbdb-record-raw-notes record)))
                                 'ff/known-address-face))
               (error
-               (propertize (or (and data (concat "<" email ">"))
+               (propertize (or (and data (concat "<" net ">"))
                                "*undefined*")
                            'face 'ff/unknown-address-face)
                ))
@@ -1274,12 +1304,15 @@ universal argument starts xfig even if the .fig does not exist"
                   (or (re-search-forward "{\\([^{}]*.\\)eps}" (point-at-eol) t)
                       (re-search-forward "{\\([^{}]*.\\)pdf}" (point-at-eol) t)
                       (re-search-forward "{\\([^{}]*.\\)pdf_t}" (point-at-eol) t)
+                      (re-search-forward "{\\([^{}]*.\\)png}" (point-at-eol) t)
+                      (re-search-forward "{\\([^{}]*.\\)jpg}" (point-at-eol) t)
                       )))
            (and (<= (match-beginning 1) (point))
                 (>= (match-end 1) (- (point) 2))))
 
       (ff/run-eps-edition (match-string-no-properties 1)
                           '(("fig" . "xfig")
+                            ("jpg" . "gimp" )
                             ("png" . "gimp") ("pgm" . "gimp") ("ppm" . "gimp")
                             ("jpg" . "xv"))
                           universal)
@@ -1369,8 +1402,8 @@ universal argument starts xfig even if the .fig does not exist"
   (define-key global-map [(control \?)] 'lookup-dict))
 
 ;; (defun ff/generate-password () (interactive)
-  ;; (let ((c "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"))
-    ;; (nth (random (length c)) c))
+;; (let ((c "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"))
+;; (nth (random (length c)) c))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Automatization of things I do often
@@ -1401,15 +1434,26 @@ universal argument starts xfig even if the .fig does not exist"
 %% \\usepackage{hyperref}
 %% \\usepackage{harvard}
 
+\\setlength{\\parindent}{0cm}
+\\setlength{\\parskip}{12pt}
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %% Sans serif fonts
 %% \\usepackage[T1]{fontenc}
 %% \\usepackage[scaled]{helvet}
 %% \\usepackage[cm]{sfmath}
 %% \\renewcommand{\\ttdefault}{pcr}
 %% \\renewcommand*\\familydefault{\\sfdefault}
-
-\\setlength{\\parindent}{0cm}
-\\setlength{\\parskip}{12pt}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% Draft layout
+%% \\setlength{\\parindent}{1cm}
+%% \\renewcommand{\\baselinestretch}{2.0}
+%% \\setlength{\\oddsidemargin}{-0.6cm}
+%% \\setlength{\\evensidemargin}{0cm}
+%% \\setlength{\\textwidth}{17.5cm}
+%% \\setlength{\\textheight}{23cm}
+%% \\setlength{\\topmargin}{-1.5cm}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 %% \\renewcommand{\\baselinestretch}{1.3}
 
@@ -1462,6 +1506,14 @@ universal argument starts xfig even if the .fig does not exist"
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+(defun ff/remove-ip-header () (interactive)
+  (save-excursion
+    (goto-char (point-min))
+    (when (and (re-search-forward "START_IP_HEADER" nil t)
+               (re-search-forward "END_IP_HEADER" nil t))
+      (message "yep"))
+    ))
+
 (defun ff/add-gpl ()
   "Adds the GPL statements at the beginning of current buffer."
   (interactive)
@@ -1522,6 +1574,24 @@ END_IP_HEADER
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+(defun ff/start-c ()
+  "Adds the header to start a C program."
+  (interactive)
+  ;;   (beginning-of-buffer)
+  (insert
+   "
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv) {
+  exit(EXIT_SUCCESS);
+}
+")
+  (previous-line 2)
+  )
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
 (defun ff/start-c++ ()
   "Adds the header to start a C++ program."
   (interactive)
@@ -1592,8 +1662,8 @@ int main(int argc, char **argv) {
 
 (defun ff/cout-var (arg)
   "Invoked on a line with a list of variables names,
-it inserts a line which displays their values in cout
-(or cerr if the function is invoked with a universal arg)"
+it inserts a line which displays their values in cout, or cerr if
+the function is invoked with a universal arg"
   (interactive "P")
   (let ((line (if arg "cerr" "cout")))
     (goto-char (point-at-bol))
@@ -1637,12 +1707,16 @@ and refilling all the paragraphs."
 "))
   )
 
-(add-hook 'latex-mode-hook (lambda ()
-                             (define-key latex-mode-map
-                               [(meta S)] 'ff/start-slide)
-                             (define-key latex-mode-map
-                               [(control c) (control a)] 'align-current)
-                             ))
+(add-hook
+ 'latex-mode-hook
+ (lambda ()
+   (define-key latex-mode-map [(meta S)] 'ff/start-slide)
+   (define-key latex-mode-map [(control c) (control a)] 'align-current)
+   (define-key latex-mode-map [(control end)] 'tex-close-latex-block)
+   (define-key latex-mode-map [(control tab)] 'ispell-complete-word)
+   ;; (flyspell-mode 1)
+   ;; (reftex-mode 1)
+   ))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
@@ -1762,10 +1836,16 @@ a file in /tmp"
         (previous-line 1)
         )
 
-      (when (string-match "\\.cc$" filename)
+      (when (string-match "\\.c$" filename)
+        (c-mode)
+        (ff/add-copyrights)
+        (ff/start-c))
+
+      (when (string-match "\.\\(cc\\|cpp\\)$" filename)
         (c++-mode)
         (ff/add-copyrights)
-        (let ((headername  (replace-regexp-in-string "\.cc" ".h" filename)))
+        (let ((headername  (replace-regexp-in-string "\\.\\(cc\\|cpp\\)$" ".h"
+                                                     filename)))
           (if (file-exists-p headername)
               (insert (concat "\n#include \"" (file-name-nondirectory headername) "\"\n"))
             (ff/start-c++))
@@ -1840,7 +1920,7 @@ a file in /tmp"
     )
   )
 
-(setq compilation-finish-functions (cons 'ff/restore-windows-if-no-error compilation-finish-functions))
+(add-to-list 'compilation-finish-functions 'ff/restore-windows-if-no-error)
 
 (defun ff/fast-compile ()
   "Compiles without asking anything."
@@ -1944,7 +2024,7 @@ This may be a useful alternative binding for \\[delete-other-windows]
       (while (re-search-forward "[0-9\.]+" nil t)
         (let ((value (string-to-number (buffer-substring (match-beginning 0) (match-end 0)))))
           (delete-region (match-beginning 0) (match-end 0))
-          (insert (format "%0.2f" value)))))))
+          (insert (format "%0.0f" value)))))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Keymaping
@@ -2001,13 +2081,11 @@ This may be a useful alternative binding for \\[delete-other-windows]
 
 ;; Closes the current \begin{}
 
-(add-hook 'latex-mode-hook (lambda () (define-key latex-mode-map [(control end)] 'tex-close-latex-block)))
-
 (when (ff/load-or-alert "longlines")
 
   (setq longlines-show-hard-newlines t
         longlines-auto-wrap t
-        longline-show-effect #(" -- |\n" 0 2 (face escape-glyph))
+        ;; longlines-show-effect #("|\n" 0 2 (face escape-glyph))
         )
 
   ;; (defun ff/auto-longlines ()
@@ -2022,11 +2100,6 @@ This may be a useful alternative binding for \\[delete-other-windows]
 
   )
 
-(add-hook 'latex-mode-hook
-          (lambda ()
-            (define-key latex-mode-map [(control tab)]
-              'ispell-complete-word)))
-
 ;; Meta-/ remaped (completion)
 
 (define-key global-map [(shift right)] 'dabbrev-expand)
@@ -2191,11 +2264,17 @@ next one. With universal argument, kill all killable buffers."
 (when (ff/load-or-alert "media")
 
   (unless window-system
-    (ff/configure-faces '(
-                          (media/mode-string-face :foreground "blue4" :weight 'bold)
-                          (media/current-tune-face :foreground "black" :background "yellow" :weight 'normal)
-                          (media/instant-highlight-face :foreground "black" :background "orange" :weight 'normal)
-                          ))
+    (ff/configure-faces
+     '(
+       (media/mode-string-face
+        :foreground "blue4" :weight 'bold)
+
+       (media/current-tune-face
+        :foreground "black" :background "yellow" :weight 'normal)
+
+       (media/instant-highlight-face
+        :foreground "black" :background "orange" :weight 'normal)
+       ))
     )
 
   (define-key global-map [(meta \\)] 'media)
@@ -2324,6 +2403,30 @@ proposes to visit them."
   (flyspell-mode)
   (when flyspell-mode (flyspell-buffer)))
 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; The fridge!
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(defun ff/move-region-to-fridge () (interactive)
+  "Cut the current region, paste it in a file called ./fridge
+with a time tag, and save this file"
+  (unless (use-region-p) (error "No region selected"))
+  (let ((bn (file-name-nondirectory (buffer-file-name))))
+    (kill-region (region-beginning) (region-end))
+    (with-current-buffer (find-file-noselect "fridge")
+      (goto-char (point-max))
+      (insert "\n"
+              (format-time-string "%Y %b %d %H:%M:%S" (current-time))
+              " (from "
+              bn
+              ")\n\n")
+      (yank)
+      (save-buffer)
+      (message "Region moved to fridge")
+      )
+    )
+  )
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; My own keymap
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -2340,6 +2443,7 @@ proposes to visit them."
 (define-key ff/map [(control g)] 'ff/git-status)
 (define-key ff/map [(control w)] 'server-edit)
 (define-key ff/map [(control d)] 'ff/elisp-debug-on)
+(define-key ff/map "d" 'diary)
 (define-key ff/map [(control \`)] 'ff/bash-new-buffer)
 (define-key ff/map [(control n)] 'enotes/show-all-notes)
 (define-key ff/map [(control s)] 'ff/secure-note-add)
@@ -2351,6 +2455,7 @@ proposes to visit them."
 (define-key ff/map [(control c)] 'calendar)
 ;; (define-key ff/map [(control c)] (lambda () (interactive) (save-excursion (calendar))))
 (define-key ff/map [(control l)] 'goto-line)
+(define-key ff/map "l" 'longlines-mode)
 (define-key ff/map [(control o)] 'selector/quick-pick-recent)
 (define-key ff/map "s" 'selector/quick-move-in-buffer)
 (define-key ff/map "S" 'selector/search-sentence)
@@ -2367,6 +2472,7 @@ proposes to visit them."
   (lambda () (interactive)
     (bookmark-set)
     (bookmark-save)))
+(define-key ff/map "f" 'ff/move-region-to-fridge)
 (define-key ff/map [(control f)] 'ff/flyspell-mode)
 
 (define-key ff/map [?\C-0] 'ff/delete-annoying-windows)
@@ -2377,6 +2483,8 @@ proposes to visit them."
 (define-key ff/map "3" 'ff/twin-horizontal-current-buffer)
 (define-key ff/map [?\C-3] 'ff/twin-horizontal-current-buffer)
 
+(define-key ff/map " " 'delete-trailing-whitespace)
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Hacks so that all keys are functionnal in xterm and through ssh.
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;