Cosmetics in the comments and strings.
[elisp.git] / emacs.el
1 ;; -*- mode: Emacs-Lisp; mode: rainbow; -*-
2
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 ;; This program is free software; you can redistribute it and/or         ;;
5 ;; modify it under the terms of the GNU General Public License as        ;;
6 ;; published by the Free Software Foundation; either version 3, or (at   ;;
7 ;; your option) any later version.                                       ;;
8 ;;                                                                       ;;
9 ;; This program is distributed in the hope that it will be useful, but   ;;
10 ;; WITHOUT ANY WARRANTY; without even the implied warranty of            ;;
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      ;;
12 ;; General Public License for more details.                              ;;
13 ;;                                                                       ;;
14 ;; You should have received a copy of the GNU General Public License     ;;
15 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.  ;;
16 ;;                                                                       ;;
17 ;; Written by and Copyright (C) Francois Fleuret                         ;;
18 ;; Contact <francois@fleuret.org> for comments & bug reports             ;;
19 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20
21 ;; It's better to set the preferences in the .Xresources so that the
22 ;; window is not first displayed with the wrong options
23
24 ;; Emacs.menuBar:            off
25 ;; Emacs.verticalScrollBars: off
26 ;; Emacs.toolBar:            off
27 ;; Emacs.internalBorder:     1
28 ;; Emacs.FontBackend: xft
29 ;; Xft.dpi: 96
30 ;; Xft.hinting: true
31 ;; Xft.antialias: true
32 ;; Xft.rgba: rgb
33
34 ;; Give the focus to the emacs window if we are under a windowing
35 ;; system
36
37 (when window-system
38   ;; (x-focus-frame nil)
39   (set-mouse-pixel-position (selected-frame) 4 4))
40
41 ;; Where I keep my own scripts
42
43 (add-to-list 'load-path "~/sources/gpl/elisp")
44 (add-to-list 'load-path "~/sources/elisp")
45 (add-to-list 'load-path "~/local/elisp")
46
47 ;; No, I do not like menus
48 (menu-bar-mode -1)
49
50 ;; Nor fringes
51 (when (functionp 'fringe-mode) (fringe-mode '(0 . 0)))
52 ;; (when (functionp 'fringe-mode) (fringe-mode '(0 . 1)))
53
54 ;; And I do not like scrollbar neither
55 (when (functionp 'scroll-bar-mode) (scroll-bar-mode -1))
56
57 ;; Make all "yes or no" prompts be "y or n" instead
58 (fset 'yes-or-no-p 'y-or-n-p)
59
60 ;; Show the matching parenthesis and do it immediately, we are in a
61 ;; hurry
62 (setq show-paren-delay 0)
63 (show-paren-mode t)
64
65 ;; use colorization for all modes
66 (global-font-lock-mode t)
67
68 (setq font-lock-maximum-decoration 3
69       ;;'((latex-mode . 2) (t . 2))
70       )
71
72 ;; Activate the dynamic completion of buffer names
73 (iswitchb-mode 1)
74
75 ;; Save the minibuffer history
76 (setq savehist-file "~/private/emacs/savehist")
77 (when (functionp 'savehist-mode) (savehist-mode 1))
78
79 ;; And allow minibuffer recursion
80 (setq enable-recursive-minibuffers t)
81 (minibuffer-depth-indicate-mode 1)
82
83 ;; I do not like tooltips
84 (when (functionp 'tooltip-mode) (tooltip-mode nil))
85
86 ;; Activate the dynamic completion in the mini-buffer
87 (icomplete-mode 1)
88
89 ;; (setq highlight-current-line-globally t
90 ;; highlight-current-line-ignore-regexp "Faces\\|Colors\\| \\*Mini\\|\\*media\\|INBOX")
91
92 ;; (highlight-current-line-minor-mode 1)
93 ;; (highlight-current-line-set-bg-color "gray75")
94
95 (defun ff/compile-when-needed (name)
96   "Compiles the given file only if needed. Adds .el if required, and
97 uses `load-path' to find it."
98   (if (not (string-match "\.el$" name))
99       (ff/compile-when-needed (concat name ".el"))
100     (mapc (lambda (dir)
101             (let* ((src (concat dir "/" name)))
102               (when (file-newer-than-file-p src (concat src "c"))
103                 (if (let ((byte-compile-verbose nil))
104                       (condition-case nil
105                           (byte-compile-file src)
106                         (error nil)))
107                     (message (format "Compiled %s" src ))
108                   (message (format "Failed compilation of %s" src))))))
109           load-path)))
110
111 ;; This is useful when using the same .emacs in many places
112
113 (defun ff/load-or-alert (name &optional compile-when-needed)
114   "Tries to load the specified file and insert a warning message in a
115 load-warning buffer in case of failure."
116
117   (when compile-when-needed (ff/compile-when-needed name))
118
119   (if (load name t nil) t
120     (let ((buf (get-buffer-create "*loading warnings*")))
121       (display-buffer buf)
122       (set-buffer buf)
123       (insert (propertize "Warning:" 'face 'font-lock-warning-face) " could not load '" name "'\n")
124       (fit-window-to-buffer (get-buffer-window buf))
125       (set-buffer-modified-p nil))
126     nil))
127
128 ;; This is the default in emacs 22.1 and later
129 ;; (auto-compression-mode 1)
130
131 ;; make emacs use the clipboard so that copy/paste works for other
132 ;; x-programs. I have no clue how all that clipboard thing works.
133
134 ;; (setq x-select-enable-clipboard t)
135 ;; (setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
136 ;; (setq x-select-enable-primary t)
137 ;; (setq x-select-enable-clipboard t)
138 ;; (global-set-key "\C-y" 'clipboard-yank)
139
140
141
142 (setq
143
144  message-log-max 1000
145
146  ;; avoid GC as much as possible
147  gc-cons-threshold 2500000
148
149  ;; no startup message
150  inhibit-startup-screen t
151
152  ;; no message in the scratch buffer
153  initial-scratch-message nil
154
155  ;; do not fill my buffers, you fool
156  next-line-add-newlines nil
157
158  ;; keep the window focused on the messages during compilation
159  compilation-scroll-output t
160
161  ;; Keep the highlight on the compilation error
162  next-error-highlight t
163
164  ;; blink the screen instead of beeping
165  ;; visible-bell t
166
167  ;; take the CR when killing a line
168  kill-whole-line t
169
170  ;; I prefer to move between lines as defined in the buffer, not
171  ;; visually
172  line-move-visual nil
173
174  ;; I comment empty lines, too (does not seem to work, though)
175  comment-empty-lines t
176
177  ;; We want long lines to be truncated instead of displayed on several lines
178  ;; truncate-lines t
179  ;; Show all lines, even if the window is not as large as the frame
180  ;; truncate-partial-width-windows nil
181  ;; truncate-partial-width-windows t
182
183  ;; Do not keep tracks of the autosaved files
184  auto-save-list-file-prefix nil
185
186  ;; Show me empty lines at the end of the buffer
187  default-indicate-empty-lines t
188
189  ;; Show me the region until I do something on it
190  transient-mark-mode t
191
192  ;; Do not color stuff which are clickable when hovering over it
193  mouse-highlight nil
194
195  ;; Don't bother me with questions even if "unsafe" local variables
196  ;; are set
197  enable-local-variables :all
198
199  ;; I have no problem with small windows
200  window-min-height 1
201
202  ;; I am not a fan of develock
203  develock-auto-enable nil
204
205  ;; I do not like women to open windows
206  woman-use-own-frame nil
207
208  ;; I am not that paranoid, contrary to what you think
209  epa-file-cache-passphrase-for-symmetric-encryption t
210  ;; And I like ascii files
211  epa-armor t
212
213  tramp-default-method "ssh"
214
215  ;; I have no problem with files having their own local variables
216  enable-local-eval t
217
218  mail-from-style 'angles
219  browse-url-mozilla-program "firefox"
220  mc-encrypt-for-me t
221  mc-use-default-recipients t
222
223  ;; browse-url-new-window-flag t
224  )
225
226 ;; The backups
227
228 (setq
229  temporary-file-directory "/tmp/"
230  vc-make-backup-files t
231  backup-directory-alist '((".*" . "~/misc/emacs.backups/"))
232  version-control t ;; Use backup files with numbers
233  kept-new-versions 10
234  kept-old-versions 2
235  delete-old-versions t
236  backup-by-copying-when-linked t
237  )
238
239 (setq tramp-backup-directory-alist backup-directory-alist)
240
241 (setq user-emacs-directory "~/misc/emacs.d/")
242
243 (setq
244  abbrev-file-name (concat user-emacs-directory "abbrev_defs")
245  server-auth-dir (concat user-emacs-directory "server/")
246  custom-theme-directory user-emacs-directory
247  )
248
249 ;; Stop this crazy blinking cursor
250 (blink-cursor-mode 0)
251
252 ;; (setq blink-cursor-delay 0.25
253 ;; blink-cursor-interval 0.25)
254
255 ;; (set-terminal-coding-system 'utf-8)
256
257 ;; (unless window-system
258 ;; (xterm-mouse-mode 1)
259 ;;   (if (string= (getenv "TERM") "xterm-256color")
260 ;;       (ff/load-or-alert "xterm-256color" t))
261 ;; )
262
263 (setq-default
264
265  ;; Show white spaces at the end of lines
266  show-trailing-whitespace t
267
268  ;; Do not show the cursor in non-active window
269  cursor-in-non-selected-windows nil
270
271  use-dialog-box nil
272  use-file-dialog nil
273
274  ;; when on a TAB, the cursor has the TAB length
275  x-stretch-cursor t
276
277  ;; This is the default coding system when toggle-input-method is
278  ;; invoked (C-\)
279  default-input-method "latin-1-prefix"
280  ;; do not put tabs when indenting
281  indent-tabs-mode nil
282  ;; And yes, we have a fast display / connection / whatever
283  baud-rate 524288
284  ;; baud-rate 10
285
286  ;; To keep the cursor always visible when it moves (thanks
287  ;; snogglethrop!)
288  redisplay-dont-pause t
289
290  ;; I want to see the keys I type instantaneously
291  echo-keystrokes 0.1
292  )
293
294 ;; Show the column number
295 (column-number-mode 1)
296
297 ;; What modes for what file extentions
298 (add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
299
300 (add-to-list 'auto-mode-alist '("\\.txt\\'" . (lambda()
301                                                 (text-mode)
302                                                 (orgtbl-mode)
303                                                 (auto-fill-mode)
304                                                 (flyspell-mode))))
305
306 (add-hook 'c++-mode-hook 'flyspell-prog-mode)
307 (add-hook 'log-edit-mode-hook 'flyspell-mode)
308
309 ;; I am a power-user
310
311 (put 'narrow-to-region 'disabled nil)
312 (put 'upcase-region 'disabled nil)
313 (put 'downcase-region 'disabled nil)
314 ;; (put 'scroll-left 'disabled nil)
315 ;; (put 'scroll-right 'disabled nil)
316
317 ;; My selector is clearer than that
318 ;; (when (load "ido" t) (ido-mode t))
319
320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
321
322 ;; Makes buffer names more explicit then <2>, <3> etc. when there are
323 ;; several identical filenames
324
325 (when (load "uniquify" t)
326   (setq uniquify-buffer-name-style 'post-forward-angle-brackets))
327
328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329 ;; Appearance
330 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
331
332 (when (boundp 'x-display-name)
333
334   (setq-default
335
336    ;; If the display is :0.0, we make the assumption that we are
337    ;; running the emacs locally, and we do not show the
338    ;; hostname. Otherwise, show @host.
339
340    frame-title-format (concat "emacs" ;;invocation-name
341                               (unless (string= x-display-name ":0.0")
342                                 (concat "@" system-name))
343                               " (%b)")
344
345    ;; Use the same for the icone
346
347    icon-title-format frame-title-format
348    ))
349
350 ;; "tool" bar? Are you kidding?
351 (when (fboundp 'tool-bar-mode) (tool-bar-mode -1))
352
353 ;; ;; If my own letter icon is here, use it and change its color
354 ;; (when (file-exists-p "~/local/share/emacs/letter.xbm")
355 ;; (setq-default display-time-mail-icon
356 ;; (find-image
357 ;; '((:type xbm
358 ;; :file "~/local/share/emacs/letter.xbm"
359 ;; :ascent center)))))
360
361 ;; My funky setting of face colors. Basically, we switch to a sober
362 ;; look and darken a bit the colors which need to (because of the
363 ;; darker background)
364
365 (defun ff/configure-faces (fl)
366   "Set face attributes and create faces when necessary"
367   (mapc (lambda (f)
368           (unless (boundp (car f)) (make-empty-face (car f)))
369           (eval `(set-face-attribute (car f) nil ,@(cdr f))))
370         fl))
371
372 ;; Not the same in xterm (which is gray in my case) and in
373 ;; X-window
374
375 (unless window-system
376   ;;     (xterm-mouse-mode 1)
377   (ff/configure-faces
378    '((italic :underline nil)
379      (info-title-2 :foreground "green")
380      (cperl-array-face :background "gray90" :foreground "blue" :weight 'bold)
381      (cperl-hash-face :background "gray90" :foreground "purple" :weight 'bold)
382      (diff-added :background "gray90" :foreground "green4" :weight 'bold)
383      (diff-removed :background "gray90" :foreground "red2" :weight 'bold)
384      (diff-changed :background "gray90" :foreground "blue" :weight 'bold)
385      (diff-file-header-face :background "white" :foreground "black"
386                             :weight 'bold)
387      (diff-header-face :background "white" :foreground "black")
388      (diff-hunk-header-face :background "white" :foreground "black")
389      (diff-indicator-removed :foreground "red" :weight 'bold)
390      (diff-removed :foreground "red" :weight 'bold)
391      (diff-indicator-added :foreground "blue" :weight 'bold)
392      (diff-added :foreground "blue" :weight 'bold)
393      (font-lock-string-face :foreground "green")
394      (font-lock-variable-name-face :foreground "blue")
395      (font-lock-constant-face :foreground "blue")
396      (font-lock-function-name-face :foreground "blue")
397      (font-lock-preprocessor-face :foreground "green")
398      (font-lock-function-name-face :foreground "cyan")
399      (flyspell-incorrect-face :foreground "red2")
400      (flyspell-duplicate-face :foreground "OrangeRed2")
401      (hl-line :background "white")
402      (sh-heredoc :foreground "black" :background "#fff0f0")
403      (sh-heredoc-face :foreground "black" :background "#fff0f0")
404      (font-lock-keyword-face :foreground "blue")
405      (highlight :background "darkseagreen3")
406      (isearch :background "orange" :foreground "black")
407      (isearch-lazy-highlight-face' :background "yellow" :foreground "black")
408      ;; (display-time-mail-face :background "white")
409      (show-paren-match-face :background "gold" :foreground "black")
410      (show-paren-mismatch-face :background "red" :foreground "black")
411      (trailing-whitespace :background "white")
412      (mode-line :background "cornflowerblue" :foreground "black" :box nil
413                 :inverse-video nil)
414      (header-line :background "cornflowerblue" :foreground "black" :box nil
415                   :inverse-video nil)
416      (mode-line-inactive :background "gray60" :foreground "black" :box nil
417                          :inverse-video nil)
418      (region :background "springgreen2")
419      (ff/date-info-face :foreground "white" :weight 'bold)
420      (ff/mail-alarm-face :foreground "red" :weight 'bold)
421      (gui-button-face :background "green" :foreground "white")
422      (enotes/information-face :foreground "cyan")
423      ))
424   )
425
426 ;; (list-colors-display (mapcar 'car color-name-rgb-alist))
427
428 ;; (ff/configure-faces '((default :background "black" :foreground "gray80")))
429 ;; (ff/configure-faces '((default :background "gray80" :foreground "black")))
430
431 (when window-system
432   ;; (setq
433   ;; display-time-use-mail-icon t)
434
435   (ff/configure-faces
436    '(
437      ;; (escape-glyph :foreground "#c0c0c0" :weight 'bold)
438      (escape-glyph :foreground "green3" :weight 'bold)
439      (default :background "gray90" :foreground "black")
440      (cperl-array-face :background "gray90" :foreground "blue" :weight 'bold)
441      (cperl-hash-face :background "gray90" :foreground "purple" :weight 'bold)
442      (message-cited-text :foreground "red4")
443      (diff-mode :background "gray90" :weight 'bold)
444      (diff-added :background "gray90" :foreground "green4" :weight 'bold)
445      (diff-removed :background "gray90" :foreground "red2" :weight 'bold)
446      (diff-changed :background "gray90" :foreground "blue" :weight 'bold)
447      (diff-file-header :background "white" :foreground "black"
448                        :weight 'bold)
449      (diff-header :background "white" :foreground "black")
450      (diff-hunk-header :background "white" :foreground "black")
451      (font-lock-builtin-face :foreground "deeppink3")
452      (font-lock-string-face :foreground "dark olive green")
453      (font-lock-variable-name-face :foreground "sienna")
454      (font-lock-function-name-face :foreground "blue4" :weight 'bold)
455      ;; (font-lock-comment-delimiter-face :foreground "dark violet")
456      ;; (font-lock-comment-face :foreground "dark violet")
457      (flyspell-incorrect-face :foreground "red2")
458      (flyspell-duplicate-face :foreground "OrangeRed2")
459      (hl-line :background "white")
460      (sh-heredoc :foreground "black" :background "#fff0f0")
461      (sh-heredoc-face :foreground "black" :background "#fff0f0")
462      (header-line :background "gray65")
463      (highlight :background "turquoise")
464      (message-cited-text-face :foreground "firebrick")
465      (isearch :background "yellow" :foreground "black")
466      (isearch-lazy-highlight-face' :background "yellow3" :foreground "black")
467      (region :background "#b8b8e0" :foreground "black")
468      ;; (region :background "plum" :foreground "black")
469      (show-paren-match-face :background "gold" :foreground "black")
470      (show-paren-mismatch-face :background "red" :foreground "black")
471      (trailing-whitespace :background "gray65")
472      (cursor :inverse-video t)
473      (enotes/list-title-face :foreground "blue" :weight 'bold)
474      (mode-line :background "#b0b0ff" :foreground "black" :box nil
475                 :inverse-video nil)
476      (header-line :background "cornflowerblue" :foreground "black" :box nil
477                   :inverse-video nil)
478      (mode-line-inactive :background "#b0b0b0" :foreground "black" :box nil
479                          :inverse-video nil)
480      ;; (fringe :background "black" :foreground "gray90")
481      (fringe :background "gray65")
482      (tex-verbatim :family "courrier")
483      (ff/date-info-face :foreground "white" :weight 'bold)
484      (ff/mail-alarm-face :foreground "white" :background "red2")
485      ;; (alarm-vc-face :foreground "black" :background "yellow" :weight 'normal)
486      ))
487   )
488
489 ;; When we are root, put the modeline in red
490
491 (when (string= (user-real-login-name) "root")
492   (ff/configure-faces
493    '((mode-line :background "red3" :foreground "black" :box nil
494                 :inverse-video nil))
495    ))
496
497 ;; Why should I have to do this?
498 (add-hook 'sh-mode-hook
499           (lambda ()
500             (set-face-attribute 'sh-heredoc nil
501                                 :foreground "#604000"
502                                 :background "white"
503                                 :italic t)
504             (set-face-attribute 'sh-heredoc-face nil
505                                 :foreground "#604000"
506                                 :background "white"
507                                 :italic t)
508             ))
509
510 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
511 ;; Move the window on the buffer without moving the cursor
512 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
513
514 (defun ff/scroll-down ()
515   "Scroll the buffer down one line and keep the cursor at the same location."
516   (interactive)
517   (condition-case nil
518       (scroll-down 1)
519     (error nil)))
520
521 (defun ff/scroll-up ()
522   "Scroll the buffer up one line and keep the cursor at the same location."
523   (interactive)
524   (condition-case nil
525       (scroll-up 1)
526     (error nil)))
527
528 (defun ff/scroll-left ()
529   "Scroll the buffer left one column and keep the cursor at the same location."
530   (interactive)
531   (condition-case nil
532       (scroll-left 2)
533     (error nil)))
534
535 (defun ff/scroll-right ()
536   "Scroll the buffer right one column and keep the cursor at the same location."
537   (interactive)
538   (condition-case nil
539       (scroll-right 2)
540     (error nil)))
541
542 (define-key global-map [(meta up)] 'ff/scroll-down)
543 (define-key global-map [(meta down)] 'ff/scroll-up)
544 (define-key global-map [(meta p)] 'ff/scroll-down)
545 (define-key global-map [(meta n)] 'ff/scroll-up)
546 (define-key global-map [(meta right)] 'ff/scroll-left)
547 (define-key global-map [(meta left)] 'ff/scroll-right)
548
549 (defun ff/delete-trailing-whitespaces-and-indent ()
550   (interactive)
551   (delete-trailing-whitespace)
552   (indent-region (point-min) (point-max) nil))
553
554 (define-key global-map [(control c) (control q)] 'ff/delete-trailing-whitespaces-and-indent)
555
556 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
557 ;; Playing sounds
558 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
559
560 ;; (defun ff/esd-sound (file)
561 ;;   "Plays a sound with the Enlighted sound daemon."
562 ;;   (interactive)
563 ;;   (process-kill-without-query (start-process-shell-command "esdplay"
564 ;;                                                            nil
565 ;;                                                            "esdplay" file)))
566
567 (defun ff/alsa-sound (file)
568   "Plays a sound with ALSA."
569   (interactive)
570   (process-kill-without-query (start-process-shell-command "aplay"
571                                                            nil
572                                                            "aplay" "-q" file)))
573
574 (if (and (boundp 'x-display-name) (string= x-display-name ":0.0"))
575     (defalias 'ff/play-sound-async 'ff/alsa-sound)
576   (defalias 'ff/play-sound-async 'ding))
577
578 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
579 ;; I comment stuff often, let's be efficient. shift + down comments
580 ;; the current line and goes down, and shift + up uncomments the line
581 ;; and goes up (they are not the dual of each other, but moving and
582 ;; then uncommenting would be very counter-intuitive).
583 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
584
585 (defun ff/comment-and-go-down (arg)
586   "Comments and goes down ARG lines."
587   (interactive "p")
588   (condition-case nil
589       (comment-region (point-at-bol) (point-at-eol)) (error nil))
590   (next-line 1)
591   (if (> arg 1) (ff/comment-and-go-down (1- arg))))
592
593 (defun ff/uncomment-and-go-up (arg)
594   "Uncomments and goes up ARG lines."
595   (interactive "p")
596   (condition-case nil
597       (uncomment-region (point-at-bol) (point-at-eol)) (error nil))
598   (next-line -1)
599   (if (> arg 1) (ff/uncomment-and-go-up (1- arg))))
600
601 (define-key global-map [(shift down)] 'ff/comment-and-go-down)
602 (define-key global-map [(shift up)] 'ff/uncomment-and-go-up)
603
604 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
605 ;; Counting various entities in text
606 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
607
608 (defun ff/count-char ()
609   "Prints the number of characters between the first previous \"--\"
610 and the firt next \"--\"."
611   (interactive)
612   (let ((from (save-excursion (re-search-backward "^--$\\|BEGIN_COUNT" nil t)))
613         (to (save-excursion (re-search-forward "^--$\\|END_COUNT" nil t))))
614     (if (and to from) (message "%d character(s)" (- to from 6))
615       (error "Can not find the -- delimiters"))))
616
617 (defun ff/count-words ()
618   "Print number of words between the first previous \"--\" and the
619 firt next \"--\"."
620   (interactive)
621   (let ((from (save-excursion (re-search-backward "^--$" nil t)))
622         (to (save-excursion (re-search-forward "^--$" nil t))))
623     (if (and to from)
624         (save-excursion
625           (goto-char from)
626           (let ((count 0))
627             (while (< (point) to)
628               (re-search-forward "\\w+\\W+")
629               (setq count (1+ count)))
630             (message "%d word(s)" count)))
631       (error "Can not find the -- delimiters"))))
632
633 (defun ff/word-occurences ()
634   "Display in a new buffer the list of words sorted by number of
635 occurrences "
636   (interactive)
637
638   (let ((buf (get-buffer-create "*word counting*"))
639         (map (make-sparse-keymap))
640         (nb (make-hash-table))
641         (st (make-hash-table))
642         (result nil))
643
644     ;; Collects all words in a hash table
645
646     (save-excursion
647       (goto-char (point-min))
648       (while (re-search-forward "\\([\\-a-zA-Z\\\\]+\\)" nil t)
649         (let* ((s (downcase (match-string-no-properties 1)))
650                (k (sxhash s)))
651           (puthash k s st)
652           (puthash k (1+ (gethash k nb 0)) nb))))
653
654     ;; Creates the result buffer
655
656     (define-key map "q" 'kill-this-buffer)
657     (display-buffer buf)
658     (set-buffer buf)
659     (setq show-trailing-whitespace nil)
660     (erase-buffer)
661
662     ;; Builds a list from the hash table
663
664     (maphash
665      (lambda (key value)
666        (setq result (cons (cons value (gethash key st)) result)))
667      nb)
668
669     ;; Sort and display it
670
671     (mapc (lambda (x)
672             (if (and (> (car x) 3)
673                      ;; No leading backslash and at least four characters
674                      (string-match "^[^\\]\\{4,\\}" (cdr x))
675                      )
676                 (insert (number-to-string (car x)) " " (cdr x) "\n")))
677           (sort result (lambda (a b) (> (car a) (car b)))))
678
679     ;; Adjust the window size and stuff
680
681     (fit-window-to-buffer (get-buffer-window buf))
682     (use-local-map map)
683     (set-buffer-modified-p nil))
684   )
685
686 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
687 ;; Printing
688 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
689
690 (load "ps-print")
691
692 (setq ps-print-color-p nil
693       ps-paper-type 'letter
694       ;; ps-paper-type 'a4
695       ;; ps-top-margin (* 1.75 56.692)
696       ;; ps-left-margin 56.692
697       ;; ps-bottom-margin 56.692
698       ;; ps-right-margin 56.692
699
700       ;; Simple header. Remove that silly frame shadow.
701       ps-print-header nil
702       ps-print-header-frame nil
703       ps-header-line-pad 0.3
704       ps-header-font-family 'Courier
705       ps-header-title-font-size '(8.5 . 10)
706       ps-header-font-size '(6 . 7)
707       ps-font-size '(7 . 8)
708       )
709
710 (ps-put 'ps-header-frame-alist 'back-color 1.0)
711 (ps-put 'ps-header-frame-alist 'shadow-color 1.0)
712 (ps-put 'ps-header-frame-alist 'border-color 0.0)
713 (ps-put 'ps-header-frame-alist 'border-width 0.0)
714
715 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
716
717 ;; http://blog.tuxicity.se/elisp/emacs/2010/03/26/rename-file-and-buffer-in-emacs.htm
718
719 (defun rename-file-and-buffer ()
720   "Renames current buffer and file it is visiting."
721   (interactive)
722   (let ((name (buffer-name))
723         (filename (buffer-file-name)))
724     (if (not (and filename (file-exists-p filename)))
725         (message "Buffer '%s' is not visiting a file!" name)
726       (let ((new-name (read-file-name "New name: " filename)))
727         (cond ((get-buffer new-name)
728                (message "A buffer named '%s' already exists!" new-name))
729               (t
730                (rename-file name new-name 1)
731                (rename-buffer new-name)
732                (set-visited-file-name new-name)
733                (set-buffer-modified-p nil)))))))
734
735 (global-set-key (kbd "C-c r") 'rename-file-and-buffer)
736
737 (defun ff/non-existing-filename (dir prefix suffix)
738   "Returns a filename of the form DIR/PREFIX[.n].SUFFIX whose file does
739 not exist"
740   (let ((n 0)
741         (f (concat prefix suffix)))
742     (while (file-exists-p (concat dir "/" f))
743       (setq n (1+ n)
744             f (concat prefix "." (prin1-to-string n) suffix)))
745     f))
746
747 (defun ff/print-buffer-or-region-with-faces (&optional file)
748
749   ;; I am fed up with spell checking highlights
750   (when (and flyspell-mode
751              ;; (or ispell-minor-mode flyspell-mode)
752              (not (y-or-n-p "The spell checking is on, still print ? ")))
753     (error "Printing cancelled, the spell-checking is on"))
754
755   (unless
756       (condition-case nil
757           (ps-print-region-with-faces (region-beginning) (region-end) file)
758         (error nil))
759     (ps-print-buffer-with-faces file)))
760
761 (defun ff/print-to-file (file)
762   "Prints the region if selected or the whole buffer in postscript
763 into FILE."
764   (interactive
765    (list
766     (read-file-name
767      "PS file: " "/tmp/" nil nil
768      (ff/non-existing-filename
769       "/tmp"
770       (replace-regexp-in-string "[^a-zA-Z0-9_.-]" "_" (file-name-nondirectory
771                                                        (buffer-name)))
772       ".ps"))
773     ))
774   (ff/print-buffer-or-region-with-faces file))
775
776 (defun ff/print-to-printer ()
777   "Prints the region if selected or the whole buffer to a postscript
778 printer."
779   (interactive)
780   (message "Printing to '%s'" (getenv "PRINTER"))
781   (ff/print-buffer-or-region-with-faces))
782
783 ;; Can you believe it? There is a "print" key on PC keyboards ...
784
785 (define-key global-map [(print)] 'ff/print-to-file)
786 (define-key global-map [(shift print)] 'ff/print-to-printer)
787
788 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
789 ;; Dealing with the laptop battery
790 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
791
792 (defcustom ff/battery-dir "/sys/class/power_supply/BAT0"
793   "*Where to gather the battery information")
794
795 (defcustom ff/temperature-file "/sys/class/thermal/thermal_zone0/temp"
796   "*Where to gather the thermal information")
797
798 (defun ff/file-first-line (file)
799   (with-temp-buffer
800     (insert-file-contents-literally file)
801     (buffer-substring (point-at-bol) (point-at-eol))))
802
803 (defun ff/battery-percent (prefix)
804   (condition-case nil
805       (/ (* 100 (string-to-number (ff/file-first-line (format "%s/%s_now" ff/battery-dir prefix))))
806          (string-to-number (ff/file-first-line (format "%s/%s_full"  ff/battery-dir prefix))))
807     (error -1))
808   )
809
810 (defun ff/laptop-info-string () (interactive)
811   (condition-case nil
812       (concat
813
814        ;; The temperature
815
816        (let ((temp (/ (string-to-number (ff/file-first-line ff/temperature-file)) 1000)))
817          (if (> temp 50)
818              (concat
819               (let ((s (format "%dC " temp)))
820                 (if (> temp 65) (propertize s 'face
821                                             'font-lock-warning-face)
822                   s))
823               )
824            )
825          )
826
827        ;; The battery
828
829        (let ((battery-status (ff/file-first-line (concat ff/battery-dir "/status"))))
830
831          (cond
832           ((string= battery-status "Full") "L")
833
834           ((string= battery-status "Charging")
835            (format "L%d%%" (max (ff/battery-percent "charge")
836                                 (ff/battery-percent "energy"))))
837
838           ((string= battery-status "Discharging")
839            (let* ((c (max (ff/battery-percent "charge")
840                           (ff/battery-percent "energy")))
841                   (s (format "B%d%%" c)))
842              (if (>= c 20) s (propertize s 'face 'font-lock-warning-face))))
843
844           (t battery-status)
845
846           ))
847
848        )
849
850     (error nil))
851   )
852
853 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
854
855 (defun ff/system-info () (interactive)
856
857   (let ((buf (get-buffer-create "*system info*"))
858         (map (make-sparse-keymap)))
859
860     (define-key map "q" 'kill-this-buffer)
861     (display-buffer buf)
862     (set-buffer buf)
863     (setq show-trailing-whitespace nil)
864     (erase-buffer)
865
866     (let ((highlight nil))
867
868       (mapc (lambda (x)
869               (insert
870                (if (setq highlight (not highlight))
871                    (propertize
872                     (with-temp-buffer (apply 'call-process x)
873                                       (buffer-string))
874                     'face '(:background "#c0c0ff"))
875                  (with-temp-buffer (apply 'call-process x)
876                                    (buffer-string))
877                  ))
878               )
879
880             '(
881               ("hostname" nil t nil "-v")
882               ("acpi" nil t)
883               ("df" nil t nil "-h")
884               ;; ("mount" nil t)
885               ("ifconfig" nil t)
886               ("ssh-add" nil t nil "-l")
887               )))
888
889     (goto-char (point-min))
890     (while (re-search-forward "^$" nil t) (backward-delete-char 1))
891
892     (fit-window-to-buffer (get-buffer-window buf))
893     (use-local-map map)
894     (set-buffer-modified-p nil)
895     ))
896
897 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
898 ;; Make a sound when there is new mail
899 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
900
901 ;; I do not like sounds anymore
902
903 ;; (setq ff/already-boinged-for-mail nil)
904
905 ;; (defun ff/boing-if-new-mail ()
906 ;; (if mail (when (not ff/already-boinged-for-mail)
907 ;; ;; (ff/play-sound-async "~/local/sounds/boing1.wav")
908 ;; ;; (ff/show-unspooled-mails)
909 ;; (setq ff/already-boinged-for-mail t))
910 ;; (setq ff/already-boinged-for-mail nil))
911 ;; )
912
913 ;; (add-hook 'display-time-hook 'ff/boing-if-new-mail)
914
915 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
916 ;; Display time
917 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
918
919 (setq
920
921  display-time-interval 15 ;; Check every 15s
922
923  display-time-string-forms `(
924
925                              ;; (if mail
926                              ;;     (concat " "
927                              ;;             (propertize " mail "
928                              ;;                         'face 'ff/mail-alarm-face)
929                              ;;             " ")
930                              ;;   )
931
932                              (propertize (concat 24-hours ":" minutes
933                                                  " "
934                                                  dayname " "
935                                                  monthname " "
936                                                  day)
937                                          'face 'ff/date-info-face)
938
939                              load
940
941                              ,(if (ff/laptop-info-string)
942                                   '(concat " " (ff/laptop-info-string)))
943
944                              )
945
946  ;; display-time-format "%b %a %e %H:%M"
947  ;; display-time-mail-face nil
948  )
949
950 ;; Show the time, mail and stuff
951 (display-time)
952
953 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
954 ;; Moving through buffers
955 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
956
957 (defun ff/next-buffer ()
958   "Switches to the next buffer in cyclic order."
959   (interactive)
960   (let ((buffer (current-buffer)))
961     (switch-to-buffer (other-buffer buffer))
962     (bury-buffer buffer)))
963
964 (defun ff/prev-buffer ()
965   "Switches to the previous buffer in cyclic order."
966   (interactive)
967   (let ((list (nreverse (buffer-list)))
968         found)
969     (while (and (not found) list)
970       (let ((buffer (car list)))
971         (if (and (not (get-buffer-window buffer))
972                  (not (string-match "\\` " (buffer-name buffer))))
973             (setq found buffer)))
974       (setq list (cdr list)))
975     (switch-to-buffer found)))
976
977 (define-key global-map [?\C-x right] 'ff/next-buffer)
978 (define-key global-map [?\C-x left] 'ff/prev-buffer)
979
980 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
981 ;; There is actually a decent terminal emulator in emacs!
982 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
983
984 (load "term")
985
986 (defun ff/kill-associated-buffer (process str) (interactive)
987   (let ((buffer (process-buffer process)))
988     (kill-buffer buffer))
989   (message "Process finished (%s)" (replace-regexp-in-string "\n$" "" str)))
990
991 (defun ff/kill-associated-buffer-and-delete-windows (process str) (interactive)
992   (let ((buffer (process-buffer process)))
993     (delete-windows-on buffer)
994     (kill-buffer buffer))
995   (message "Process finished (%s)" (replace-regexp-in-string "\n$" "" str)))
996
997 (defun ff/shell-new-buffer (buffername program &rest param)
998   "Start a terminal-emulator in a new buffer with the shell PROGRAM,
999 optionally invoked with the parameters PARAM. The process associated
1000 to the shell can be killed without query."
1001
1002   (interactive)
1003
1004   (let ((n 1)
1005         (bn buffername))
1006
1007     (while (get-buffer (concat "*" bn "*"))
1008       (setq n (1+ n)
1009             bn (format "%s<%d>" buffername n)))
1010
1011     (set-buffer (apply 'make-term (append (list bn program nil) param)))
1012
1013     (setq show-trailing-whitespace nil)
1014     (term-char-mode)
1015     (message "C-c C-k term-char-mode, C-c C-j term-line-mode. \
1016 In line mode: M-p previous line, M-n next line.")
1017
1018     ;; A standard setup of the face above is not enough, I have to
1019     ;; force them here. Since I have a gray90 background, I like
1020     ;; darker colors.
1021
1022     (when window-system
1023       (ff/configure-faces
1024        '((term-green :foreground "green3")
1025          (term-cyan :foreground "cyan3")
1026          (term-default-fg-inv :foreground "gray90" :background "black")
1027          )))
1028
1029     (term-set-escape-char ?\C-x)
1030
1031     ;; I like the shell buffer and windows to be deleted when the
1032     ;; shell process terminates. It's a bit of a mess to acheive this.
1033
1034     (let ((process (get-buffer-process (current-buffer))))
1035       (process-kill-without-query process)
1036       (set-process-sentinel process
1037                             ;; 'ff/kill-associated-buffer-and-delete-windows
1038                             'ff/kill-associated-buffer
1039                             ))
1040
1041     ;; (switch-to-buffer-other-window (concat "*" bn "*"))
1042     (switch-to-buffer (concat "*" bn "*"))
1043     ))
1044
1045 (defcustom ff/default-bash-commands '("ssh")
1046   "*List of commands to be used for completion when invoking a new
1047 bash shell with `ff/bash-new-buffer'.")
1048
1049 (defun ff/bash-new-buffer (universal)
1050   "Starts a bash in a new buffer. When invoked with a universal
1051 argument, asks for a command to execute in that bash shell. The list
1052 of commands in `ff/default-bash-commands' is used for auto-completion"
1053   (interactive "P")
1054
1055   (if universal
1056       (let ((cmd (completing-read
1057                   "Command: "
1058                   (mapcar (lambda (x) (cons x t)) ff/default-bash-commands))))
1059         (ff/shell-new-buffer cmd "/bin/bash" "-c" cmd))
1060
1061     (ff/shell-new-buffer "bash" "/bin/bash")))
1062
1063 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1064 ;; vc stuff for CVS
1065 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1066
1067 (setq ;; Always follow links if the file is under version control
1068  vc-follow-symlinks t
1069  )
1070
1071 (when (load "vc-git" nil t)
1072   (add-to-list 'vc-handled-backends 'GIT))
1073
1074 ;; alarm-vc.el is one of my own scripts, check my web page
1075
1076 (when (ff/load-or-alert "alarm-vc" t)
1077   (setq alarm-vc-mode-exceptions "^VM"))
1078
1079 (when (ff/load-or-alert "git")
1080   (setq git-show-unknown nil)
1081   )
1082
1083 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1084 ;; Makes .sh and others files executable automagically
1085 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1086
1087 ;; Please consider the security-related consequences of using it
1088
1089 ;; (defun ff/make-shell-scripts-executable (&optional filename)
1090 ;; (setq filename (or filename (buffer-name)))
1091 ;; (when (and (string-match "\\.sh$\\|\\.pl$\\|\\.rb" filename)
1092 ;; (not (file-executable-p filename))
1093 ;; )
1094 ;; (set-file-modes filename 493)
1095 ;; (message "Made %s executable" filename)))
1096
1097 ;; (add-hook 'after-save-hook 'ff/make-shell-scripts-executable)
1098
1099 (add-hook 'after-save-hook
1100           'executable-make-buffer-file-executable-if-script-p)
1101
1102 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1103 ;; Cool stuff to navigate in emacs-lisp sources
1104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1105
1106 (load "find-func")
1107
1108 (defun ff/goto-function-definition (&optional goback)
1109   "Go directly to the definition of the function at point. With
1110 goback argument, go back where we were."
1111   (interactive "P")
1112   (if goback
1113       (if (not (and (boundp 'goto-function-history) goto-function-history))
1114           (error "We were nowhere, buddy")
1115         (message "Come back")
1116         (switch-to-buffer (car (car goto-function-history)))
1117         (goto-char (cdr (car goto-function-history)))
1118         (setq goto-function-history (cdr goto-function-history)))
1119
1120     (let ((function (function-called-at-point)))
1121       (when function
1122         (let ((location (find-function-search-for-symbol
1123                          function nil
1124                          (symbol-file function))))
1125           (setq goto-function-history
1126                 (cons (cons (current-buffer) (point))
1127                       (and (boundp 'goto-function-history)
1128                            goto-function-history)))
1129           (pop-to-buffer (car location))
1130           (goto-char (cdr location)))))))
1131
1132 (define-key global-map [(meta g)] 'ff/goto-function-definition)
1133 (define-key global-map [(meta G)] (lambda () (interactive)
1134                                     (ff/goto-function-definition t)))
1135
1136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1137 ;; The big stuff (bbdb, mailcrypt, etc.)
1138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1139
1140 ;; Failsafe version if we can't load bbdb
1141 (defun ff/explicit-name (email) email)
1142
1143 (load "vc-git")
1144
1145 (when (ff/load-or-alert "bbdb")
1146
1147   (setq
1148    ;; Stop asking (if not t or nil, will not ask)
1149    bbdb-offer-save 'never
1150    ;; I hate when bbdb decides to mess up my windows
1151    bbdb-use-pop-up nil
1152    ;; I have no problem with bbdb asking me if the sender email
1153    ;; does not match exactly the address we have in the database
1154    bbdb-quiet-about-name-mismatches 0
1155    ;; I have european friends, too
1156    bbdb-north-american-phone-numbers-p nil
1157    ;; To cycle through all possible addresses
1158    bbdb-complete-name-allow-cycling t
1159    ;; Cycle with full names only, not through all net-addresses alone too
1160    bbdb-dwim-net-address-allow-redundancy t
1161    ;; Do not add new addresses automatically
1162    bbdb-always-add-addresses nil
1163    )
1164
1165   (defface ff/known-address-face
1166     '((t (:foreground "blue2")))
1167     "The face to display known mail identities.")
1168
1169   (defface ff/unknown-address-face
1170     '((t (:foreground "gray50")))
1171     "The face to display unknown mail identities.")
1172
1173   (defun ff/explicit-name (email)
1174     "Returns a string identity for the first address in EMAIL. The
1175 identity is taken from bbdb if possible or from the address itself
1176 with mail-extract-address-components. The suffix \"& al.\" is added if
1177 there are more than one address.
1178
1179 If no bbdb record is found, the name is propertized with the face
1180 ff/unknown-address-face. If a record is found and contains a note
1181 'face, the associated face is used, otherwise
1182 ff/known-address-face is used."
1183
1184     (and email
1185          (let* ((data (mail-extract-address-components email))
1186                 (name (car data))
1187                 (net (cadr data))
1188                 (record (bbdb-search-simple nil net)))
1189
1190            (concat
1191
1192             (condition-case nil
1193                 (propertize (bbdb-record-name record)
1194                             'face
1195                             (or (cdr (assoc 'face
1196                                             (bbdb-record-raw-notes record)))
1197                                 'ff/known-address-face))
1198               (error
1199                (propertize (or (and data (concat "<" net ">"))
1200                                "*undefined*")
1201                            'face 'ff/unknown-address-face)
1202                ))
1203             (if (string-match "," (mail-strip-quoted-names email)) " & al.")
1204             )))
1205     )
1206
1207   (ff/configure-faces '((ff/robot-address-face :foreground "green4")
1208                         (ff/personal-address-face :foreground "dark magenta"
1209                                                   :weight 'bold)
1210                         (ff/important-address-face :foreground "blue2"
1211                                                    ;; :underline t
1212                                                    ;; :background "white"
1213                                                    ;; :foreground "green4"
1214                                                    :weight 'bold
1215                                                    ;; :slant 'italic
1216                                                    )))
1217
1218   )
1219
1220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1221 ;; An encrypted file to put secure stuff (passwords, ...)
1222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1223
1224 (when (ff/load-or-alert "mailcrypt")
1225   (mc-setversion "gpg")
1226   ;; Keep the passphrase for 10min
1227   (setq mc-passwd-timeout 600
1228         ff/secure-note-file "~/private/secure-notes.gpg")
1229   )
1230
1231 (defface ff/secure-date
1232   '((t (:background "gold" :weight bold)))
1233   "The face to display the dates in the modeline.")
1234
1235 (defun ff/secure-note-add () (interactive)
1236   (find-file ff/secure-note-file)
1237
1238   ;; Adds a new entry (i.e. date and a bunch of empty lines)
1239
1240   (goto-char (point-min))
1241   (insert "-- "
1242           (format-time-string "%Y %b %d %H:%M:%S" (current-time))
1243           " ------------------------------------------------\n\n")
1244   (previous-line 1)
1245
1246   ;; Colorizes the dates
1247
1248   (save-excursion
1249     (goto-char (point-min))
1250     (while (re-search-forward
1251             "^-+ [0-9]+ [a-z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+.+$"
1252             nil t)
1253       (add-text-properties
1254        (match-beginning 0) (match-end 0) '(face ff/secure-date))))
1255
1256   (set-buffer-modified-p nil)
1257   (setq buffer-undo-list nil)
1258   )
1259
1260 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1261 ;; Spelling
1262 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1263
1264 (setq ;; For french, aspell is far better than ispell
1265  ispell-program-name "aspell"
1266  ;; To avoid ispell errors in figure filenames, labels, references.
1267  ;;       ispell-tex-skip-alists
1268  ;;       (list
1269  ;;        (append (car ispell-tex-skip-alists)
1270  ;;                '(("\\\\citep"           ispell-tex-arg-end) ;; JMLR
1271  ;;                  ("\\\\cite"            ispell-tex-arg-end)
1272  ;;                  ("\\\\nocite"          ispell-tex-arg-end)
1273  ;;                  ("\\\\includegraphics" ispell-tex-arg-end)
1274  ;;                  ("\\\\author"          ispell-tex-arg-end)
1275  ;;                  ("\\\\ref"             ispell-tex-arg-end)
1276  ;;                  ("\\\\label"           ispell-tex-arg-end)
1277  ;;                  ))
1278  ;;        (cadr ispell-tex-skip-alists))
1279
1280  ;; So that reftex follows the text when moving in the summary
1281  reftex-toc-follow-mode nil
1282  ;; So that reftex visits files to follow
1283  reftex-revisit-to-follow t
1284  )
1285
1286 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1287 ;; Used in a \includegraphics runs xfig with the corresponding .fig
1288 ;; file or gimp with the corresponding bitmap picture
1289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1290
1291 (defun ff/run-eps-edition (prefix rules &optional force)
1292   (if rules
1293       (let ((filename (concat prefix (car (car rules)))))
1294         (if (or force (file-exists-p filename))
1295             (start-process "latex-eps-editor" nil (cdr (car rules)) filename)
1296           (ff/run-eps-edition prefix (cdr rules) force)))
1297     (message "No original file found for %seps" prefix)))
1298
1299 (defcustom ff/xdvi-for-latex-options nil
1300   "*Options to pass to xdvi when invoking `ff/run-viewer'")
1301
1302 (defun ff/run-viewer (universal)
1303
1304   "Starts an editor for the .eps at point (either xfig or gimp,
1305 depending with the original file it can find), or starts xdvi for
1306 the current .tex if no .eps is found at point. When run with a
1307 universal argument starts xfig even if the .fig does not exist"
1308
1309   (interactive "P")
1310
1311   (if (and (save-excursion
1312              (and (re-search-backward "{" (point-at-bol) t)
1313                   (or (re-search-forward "{\\([^{}]*.\\)eps}" (point-at-eol) t)
1314                       (re-search-forward "{\\([^{}]*.\\)pdf}" (point-at-eol) t)
1315                       (re-search-forward "{\\([^{}]*.\\)pdf_t}" (point-at-eol) t)
1316                       (re-search-forward "{\\([^{}]*.\\)png}" (point-at-eol) t)
1317                       (re-search-forward "{\\([^{}]*.\\)jpg}" (point-at-eol) t)
1318                       )))
1319            (and (<= (match-beginning 1) (point))
1320                 (>= (match-end 1) (- (point) 2))))
1321
1322       (ff/run-eps-edition (match-string-no-properties 1)
1323                           '(("fig" . "xfig")
1324                             ("jpg" . "gimp" )
1325                             ("png" . "gimp") ("pgm" . "gimp") ("ppm" . "gimp")
1326                             ("jpg" . "xv"))
1327                           universal)
1328
1329     (if (not (and (buffer-file-name) (string-match "\\(.*\\)\.tex$"
1330                                                    (buffer-file-name))))
1331         (message "Not a latex file!")
1332       (condition-case nil (kill-process xdvi-process) (error nil))
1333       (let ((dvi-name (concat (match-string 1 (buffer-file-name)) ".dvi")))
1334         (if (not (file-exists-p dvi-name)) (error "Can not find %s !" dvi-name)
1335           (message "Starting xdvi with %s" dvi-name)
1336           (setq xdvi-process (apply 'start-process
1337                                     (append '("xdvi-for-latex" nil "xdvi")
1338                                             ff/xdvi-for-latex-options
1339                                             (list dvi-name))))
1340           (process-kill-without-query xdvi-process))))
1341     ))
1342
1343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1344 ;; Tex mode
1345
1346 ;; When working on a tex file with other people, I can just change
1347 ;; ff/tex-command in the -*- part of the file so that I don't mess up
1348 ;; other's people configuration.
1349
1350 (defadvice tex-file (around ff/set-my-own-tex-command () activate)
1351   (let ((tex-command
1352          (or (and (boundp 'ff/tex-command)
1353                   ff/tex-command)
1354              tex-command)))
1355     ad-do-it))
1356
1357 ;; This is a bit hardcore, but really I can't bear the superscripts in
1358 ;; my emacs window and could not find another way to deactivate them.
1359
1360 (load "tex-mode")
1361 (defun tex-font-lock-suscript (pos) ())
1362
1363 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1364 ;; Prevents many errors from beeping and makes the others play a nifty
1365 ;; sound
1366 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1367
1368 (defun ff/ring-bell ()
1369   (unless (memq this-command
1370                 '(isearch-abort
1371                   abort-recursive-edit
1372                   exit-minibuffer
1373                   keyboard-quit
1374                   backward-delete-char-untabify
1375                   delete-backward-char
1376                   minibuffer-complete-and-exit
1377                   previous-line next-line
1378                   backward-char forward-char
1379                   scroll-up scroll-down
1380                   enlarge-window-horizontally shrink-window-horizontally
1381                   enlarge-window shrink-window
1382                   minibuffer-complete
1383                   ))
1384     ;; (message "command [%s]" (prin1-to-string this-command))
1385     ;; (ff/play-sound-async "~/local/sounds/short_la.wav")
1386     ))
1387
1388 (setq ring-bell-function 'ff/ring-bell)
1389
1390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1391 ;; Past the content of the url currently in the kill-ring with
1392 ;; shift-click 2
1393 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1394
1395 (defun ff/insert-url (&optional url)
1396   "Downloads an URL with lynx and inserts it after the point."
1397   (interactive "MUrl: ")
1398   (when url
1399     (message "Inserting %s" url)
1400     (insert (concat "from: " url "\n\n"))
1401     ;; (call-process "lynx" nil t nil "-nolist" "-dump" url))
1402     (call-process "w3m" nil t nil "-dump" url))
1403   )
1404
1405 (define-key global-map [(shift mouse-2)]
1406   (lambda () (interactive) (ff/insert-url (current-kill 0))))
1407
1408 ;; lookup-dict is one of my own scripts, check my web page
1409
1410 (when (ff/load-or-alert "lookup-dict" t)
1411   (define-key global-map [(control \?)] 'lookup-dict))
1412
1413 ;; (defun ff/generate-password () (interactive)
1414 ;; (let ((c "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"))
1415 ;; (nth (random (length c)) c))
1416
1417 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1418 ;; Automatization of things I do often
1419 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1420
1421 (defun ff/snip () (interactive)
1422   (let ((start (condition-case nil (region-beginning) (error (point))))
1423         (end (condition-case nil (region-end) (error (point)))))
1424     (goto-char end)
1425     (insert "---------------------------- snip snip -------------------------------\n")
1426     (goto-char start)
1427     (insert "---------------------------- snip snip -------------------------------\n")
1428     ))
1429
1430 (defun ff/start-latex ()
1431   "Adds all that stuff to start a new LaTeX document."
1432   (interactive)
1433   (goto-char (point-min))
1434   (insert "%% -*- mode: latex; mode: reftex; mode: flyspell; coding: utf-8; tex-command: \"pdflatex.sh\" -*-
1435
1436 \\documentclass[12pt]{article}
1437 \\usepackage[a4paper,top=2.5cm,bottom=2cm,left=1.5cm,right=1.5cm]{geometry}
1438 \\usepackage[utf8]{inputenc}
1439 \\usepackage{amsmath}
1440 \\usepackage{amssymb}
1441 \\usepackage[pdftex]{graphicx}
1442 \\usepackage{microtype}
1443 \\usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue,citecolor=blue]{hyperref}
1444
1445 \\setlength{\\parindent}{0cm}
1446 \\setlength{\\parskip}{12pt}
1447 \\renewcommand{\\baselinestretch}{1.3}
1448
1449 \\def\\argmax{\\operatornamewithlimits{argmax}}
1450 \\def\\argmin{\\operatornamewithlimits{argmin}}
1451
1452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1453 %% Sans serif fonts
1454 %% \\usepackage[T1]{fontenc}
1455 %% \\usepackage[scaled]{helvet}
1456 %% \\usepackage[cm]{sfmath}
1457 %% \\renewcommand{\\ttdefault}{pcr}
1458 %% \\renewcommand*\\familydefault{\\sfdefault}
1459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1460 %% The \\todo command
1461 \\newcounter{nbdrafts}
1462 \\setcounter{nbdrafts}{0}
1463 \\makeatletter
1464 \\newcommand{\\checknbdrafts}{
1465 \\ifnum \\thenbdrafts > 0
1466 \\@latex@warning@no@line{*WARNING* The document contains \\thenbdrafts \\space draft note(s)}
1467 \\fi}
1468 \\newcommand{\\todo}[1]{\\addtocounter{nbdrafts}{1}{\\color{red} #1}}
1469 \\makeatother
1470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1471
1472 \\begin{document}
1473
1474 ")
1475   (save-excursion
1476     (goto-char (point-max))
1477     (insert "
1478
1479 \\end{document}
1480 "))
1481   (latex-mode))
1482
1483 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1484
1485 (defun ff/add-copyrights ()
1486   "Adds two lines for the (C) at the beginning of current buffer."
1487   (interactive)
1488
1489   (let ((comment-style 'plain))
1490
1491     (goto-char (point-min))
1492
1493     ;; If this is a script, put the copyrights after the first line
1494
1495     (when (re-search-forward "^#!" nil t)
1496       (beginning-of-line)
1497       (next-line 1))
1498
1499     (let ((start (point))
1500           (comment-style 'box))
1501       (insert
1502        (concat
1503
1504         "\nSTART_IP_HEADER\n"
1505
1506         (when (boundp 'user-full-name)
1507           (concat "\nWritten by " user-full-name "\n"))
1508
1509         (when (boundp 'user-mail-address)
1510           (concat "Contact <" user-mail-address "> for comments & bug reports\n"))
1511
1512         "\nEND_IP_HEADER\n"
1513         ))
1514
1515       (comment-region start (point)))
1516
1517     ))
1518
1519 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1520
1521 (defun ff/remove-ip-header () (interactive)
1522   (save-excursion
1523     (goto-char (point-min))
1524     (when (and (re-search-forward "START_IP_HEADER" nil t)
1525                (re-search-forward "END_IP_HEADER" nil t))
1526       (message "yep"))
1527     ))
1528
1529 (defun ff/add-gpl ()
1530   "Adds the GPL statements at the beginning of current buffer."
1531   (interactive)
1532   (let ((comment-style 'box)
1533         (gpl
1534          (concat
1535
1536           ;;           "
1537           ;; This program is free software; you can redistribute it and/or
1538           ;; modify it under the terms of the GNU General Public License
1539           ;; version 2 as published by the Free Software Foundation.
1540
1541           ;; This program is distributed in the hope that it will be useful, but
1542           ;; WITHOUT ANY WARRANTY\; without even the implied warranty of
1543           ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1544           ;; General Public License for more details.
1545           ;; "
1546
1547           "
1548 START_IP_HEADER
1549
1550 This program is free software: you can redistribute it and/or modify
1551 it under the terms of the version 3 of the GNU General Public License
1552 as published by the Free Software Foundation.
1553
1554 This program is distributed in the hope that it will be useful, but
1555 WITHOUT ANY WARRANTY; without even the implied warranty of
1556 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1557 General Public License for more details.
1558
1559 You should have received a copy of the GNU General Public License
1560 along with this program. If not, see <http://www.gnu.org/licenses/>.
1561
1562 "
1563           (when (boundp 'user-full-name)
1564             (concat "Written by and Copyright (C) " user-full-name "\n"))
1565
1566           (when (boundp 'user-mail-address)
1567             (concat "Contact <" user-mail-address "> for comments & bug reports\n"))
1568
1569           "
1570 END_IP_HEADER
1571 "
1572
1573           )))
1574
1575     (goto-char (point-min))
1576
1577     ;; If this is a script, put the gpl after the first line
1578     (when (re-search-forward "^#!" nil t)
1579       (beginning-of-line)
1580       (next-line 1))
1581
1582     (let ((start (point)))
1583       (insert gpl)
1584       (comment-region start (point)))
1585     ))
1586
1587 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1588
1589 (defun ff/start-c ()
1590   "Adds the header to start a C program."
1591   (interactive)
1592   ;;   (beginning-of-buffer)
1593   (insert
1594    "
1595 #include <stdio.h>
1596 #include <stdlib.h>
1597
1598 int main(int argc, char **argv) {
1599   exit(EXIT_SUCCESS);
1600 }
1601 ")
1602   (previous-line 2)
1603   )
1604
1605 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1606
1607 (defun ff/start-c++ ()
1608   "Adds the header to start a C++ program."
1609   (interactive)
1610   ;;   (beginning-of-buffer)
1611   (insert
1612    "
1613 #include <iostream>
1614 #include <fstream>
1615 #include <cmath>
1616 #include <stdio.h>
1617 #include <stdlib.h>
1618
1619 using namespace std;
1620
1621 int main(int argc, char **argv) {
1622
1623 }
1624 ")
1625   (previous-line 2)
1626   )
1627
1628 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1629
1630 (defun ff/headerize ()
1631   "Adds the #define HEADER_H, etc."
1632   (interactive)
1633   (let ((flag-name (replace-regexp-in-string
1634                     "[\. \(\)]" "_"
1635                     (upcase (file-name-nondirectory (buffer-file-name))))))
1636     (goto-char (point-max))
1637     (insert "\n#endif\n")
1638     (goto-char (point-min))
1639     (insert (concat "#ifndef " flag-name "\n"))
1640     (insert (concat "#define " flag-name "\n"))
1641     )
1642   )
1643
1644 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1645
1646 (defun ff/start-html ()
1647   "Adds all that stuff to start a new HTML file."
1648   (interactive)
1649   (goto-char (point-min))
1650   (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>
1651 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
1652
1653 <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">
1654
1655 <head>
1656 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
1657 <title></title>
1658 </head>
1659
1660 <body>
1661 ")
1662   (goto-char (point-max))
1663   (insert "
1664 </body>
1665
1666 </html>
1667 ")
1668   (html-mode))
1669
1670 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1671
1672 ;; Insert a line showing all the variables written on the current line
1673 ;; and separated by commas
1674
1675 (defun ff/cout-var (arg)
1676   "Invoked on a line with a list of variables names,
1677 it inserts a line which displays their values in cout, or cerr if
1678 the function is invoked with a universal arg"
1679   (interactive "P")
1680   (let ((line (if arg "cerr" "cout")))
1681     (goto-char (point-at-bol))
1682     ;; Regexp syntax sucks moose balls, honnest. To match '[', just
1683     ;; put it as the first char in the [...] ... This leads to some
1684     ;; obvious things like the following
1685     (while (re-search-forward "\\([][a-zA-Z0-9_.:\(\)]+\\)" (point-at-eol) t)
1686       (setq line
1687             (concat line " << \" "
1688                     (match-string 1) " = \" << " (match-string 1))))
1689     (goto-char (point-at-bol))
1690     (kill-line)
1691     (insert line " << endl\;\n")
1692     (indent-region (point-at-bol 0) (point-at-eol 0) nil)
1693     ))
1694
1695 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1696
1697 (defun ff/clean-article ()
1698   "Cleans up an article by removing the leading blanks on each line
1699 and refilling all the paragraphs."
1700   (interactive)
1701   (let ((fill-column 92))
1702     (goto-char (point-min))
1703     (while (re-search-forward "^\\ +" nil t)
1704       (replace-match "" nil nil))
1705     (fill-individual-paragraphs (point-min) (point-max) t)))
1706
1707 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1708
1709 (defun ff/start-slide ()
1710   (interactive)
1711   (insert "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1712
1713 \\begin{frame}{")
1714
1715   (save-excursion (insert "}{}
1716
1717 \\end{frame}
1718
1719 "))
1720   )
1721
1722 (add-hook
1723  'latex-mode-hook
1724  (lambda ()
1725    (define-key latex-mode-map [(meta S)] 'ff/start-slide)
1726    (define-key latex-mode-map [(control c) (control a)] 'align-current)
1727    (define-key latex-mode-map [(control end)] 'tex-close-latex-block)
1728    (define-key latex-mode-map [(control tab)] 'ispell-complete-word)
1729    ;; Strange that I have to specify that
1730    ;; (setq paragraph-separate "[%      \f]*$")
1731    ;; (setq paragraph-separate
1732    ;; (concat "[%]*\\|[\f%]\\|[ \t]*\\($\\|"
1733    ;; "\\\\[][]\\|"
1734    ;; "\\\\" (regexp-opt (append
1735    ;; (mapcar 'car latex-section-alist)
1736    ;; '("begin" "label" "end" )) t)
1737    ;; "\\>\\|\\\\\\(" (regexp-opt '("item" "bibitem" "newline"
1738    ;; "noindent" "newpage" "footnote"
1739    ;; "marginpar" "parbox" "caption"))
1740    ;; "\\|\\$\\$\\|[a-z]*\\(space\\|skip\\|page[a-z]*\\)"
1741    ;; "\\>\\)[ \t]*\\($\\|%\\)\\)"))
1742    ;; (flyspell-mode 1)
1743    ;; (reftex-mode 1)
1744    ))
1745
1746 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1747
1748 (defun ff/start-test-code ()
1749   (interactive)
1750   (let ((start (point)))
1751     (insert "
1752 { // ******************************* START ***************************
1753 #warning Test code added on "
1754             (format-time-string "%04Y %b %02d %02H:%02M:%02S" (current-time))
1755             "
1756
1757 } // ******************************** END ****************************
1758
1759 ")
1760     (indent-region start (point) nil))
1761   (previous-line 3)
1762   (c-indent-command))
1763
1764 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1765
1766 (defun ff/code-to-html () (interactive)
1767   (save-restriction
1768     (narrow-to-region (region-beginning) (region-end))
1769     (replace-string "\"" "&quot;" nil (point-min) (point-max))
1770     (replace-string " " "&nbsp;" nil (point-min) (point-max))
1771     (replace-string ">" "&gt;" nil (point-min) (point-max))
1772     (replace-string "<" "&lt;" nil (point-min) (point-max))
1773     (replace-string "\e" "^[" nil (point-min) (point-max))
1774     (replace-string "\7f" "^?" nil (point-min) (point-max))
1775     (replace-string "\1f" "^_" nil (point-min) (point-max))
1776     (replace-regexp "$" "<br />" nil (point-min) (point-max))
1777     )
1778   )
1779
1780 (defun ff/downcase-html-tags () (interactive)
1781   (save-excursion
1782     (beginning-of-buffer)
1783     (while (re-search-forward "<\\([^>]+\\)>" nil t)
1784       (downcase-region (match-beginning 1) (match-end 1)))
1785     )
1786   )
1787
1788 ;; If we enter html mode and there is no makefile around, create a
1789 ;; compilation command with tidy (this is cool stuff)
1790
1791 (add-hook 'html-mode-hook
1792           (lambda ()
1793             (unless (or (not (buffer-file-name))
1794                         (file-exists-p "makefile")
1795                         (file-exists-p "Makefile"))
1796               (set (make-local-variable 'compile-command)
1797                    (let ((fn (file-name-nondirectory buffer-file-name)))
1798                      (format "tidy -utf8 %s > /tmp/%s" fn fn))))))
1799
1800 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1801
1802 (defun ff/count-words-region (beginning end)
1803   "Print number of words in the region.
1804 Words are defined as at least one word-constituent character
1805 followed by at least one character that is not a
1806 word-constituent.  The buffer's syntax table determines which
1807 characters these are."
1808
1809   (interactive "r")
1810   (message "Counting words in region ... ")
1811   (save-excursion
1812     (goto-char beginning)
1813     (let ((count 0))
1814       (while (< (point) end)
1815         (re-search-forward "\\w+\\W+")
1816         (setq count (1+ count)))
1817       (cond ((zerop count) (message "The region does NOT have any word."))
1818             ((= 1 count) (message "The region has 1 word."))
1819             (t (message "The region has %d words." count))))))
1820
1821 ;; (add-hook 'html-mode-hook 'flyspell-mode)
1822
1823 (defun ff/tidy-html ()
1824   "Run tidy in on the content of the current buffer, put the result in
1825 a file in /tmp"
1826   (interactive)
1827   (call-process-region (point-min) (point-max)
1828                        "/usr/bin/tidy"
1829                        nil
1830                        (list nil (make-temp-file "/tmp/tidy-html."))))
1831
1832 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1833
1834 ;; Create the adequate embryo of a file if it does not exist
1835
1836 (defun ff/start-file () (interactive)
1837   (let ((filename (buffer-file-name)))
1838     (when filename
1839
1840       (when (string-match "\\.sh$" filename)
1841         (sh-mode)
1842         (insert "#!/bin/bash\n\nset -e\n\n")
1843         (save-excursion
1844           (ff/add-copyrights))
1845         )
1846
1847       (when (string-match "\\.html$" filename)
1848         (html-mode)
1849         (ff/start-html)
1850         (previous-line 4)
1851         )
1852
1853       (when (string-match "\\.h$" filename)
1854         (c++-mode)
1855         (ff/headerize)
1856         (save-excursion
1857           (ff/add-copyrights)
1858           (newline))
1859         (newline)
1860         (newline)
1861         (previous-line 1)
1862         )
1863
1864       (when (string-match "\\.c$" filename)
1865         (c-mode)
1866         (ff/add-copyrights)
1867         (ff/start-c))
1868
1869       (when (string-match "\.\\(cc\\|cpp\\)$" filename)
1870         (c++-mode)
1871         (ff/add-copyrights)
1872         (let ((headername  (replace-regexp-in-string "\\.\\(cc\\|cpp\\)$" ".h"
1873                                                      filename)))
1874           (if (file-exists-p headername)
1875               (insert (concat "\n#include \"" (file-name-nondirectory headername) "\"\n"))
1876             (ff/start-c++))
1877           ))
1878
1879       (when (string-match "\\.tex$" filename)
1880         (latex-mode)
1881         (ff/start-latex)
1882         ))
1883     )
1884   (set-buffer-modified-p nil)
1885   )
1886
1887 (if (>= emacs-major-version 22)
1888     (add-to-list 'find-file-not-found-functions 'ff/start-file)
1889   (add-to-list 'find-file-not-found-hooks 'ff/start-file))
1890
1891 (when (>= emacs-major-version 24)
1892   (define-obsolete-function-alias 'make-local-hook 'ignore "21.1")
1893   (setq send-mail-function 'sendmail-send-it) ;; emacs 24.x stuff
1894
1895   (custom-set-faces
1896    '(diff-added ((default (:background "gray90" :foreground "green4" :weight bold))))
1897    '(diff-removed ((default (:background "gray90" :foreground "red2" :weight bold))))
1898    '(diff-changed ((default (:background "gray90" :foreground "blue" :weight bold))))
1899    )
1900   )
1901
1902 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1903
1904 (define-key global-map [f8] 'ff-find-other-file)
1905 (define-key global-map [(shift f8)] (lambda () (interactive) (ff-find-other-file t)))
1906
1907 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1908 ;; Antiword, htmlize and boxquote
1909 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1910
1911 (autoload 'no-word "no-word")
1912 (add-to-list 'auto-mode-alist '("\\.doc\\'" . no-word))
1913 ;; (add-to-list 'auto-mode-alist '("\\.DOC\\'" . no-word))
1914
1915 (autoload 'htmlize-buffer "htmlize" nil t)
1916
1917 (setq boxquote-top-and-tail "------------------")
1918 (autoload 'boxquote-region "boxquote" nil t)
1919
1920 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1921 ;; The compilation hacks
1922 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1923
1924 ;; If we enter c++ mode and there is no makefile around, we create a
1925 ;; make command on the fly for the specific object file
1926
1927 (add-hook 'c++-mode-hook
1928           (lambda ()
1929             (unless (or (file-exists-p "makefile") (file-exists-p "Makefile"))
1930               (set (make-local-variable 'compile-command)
1931                    (concat
1932                     "make -k "
1933                     (file-name-sans-extension
1934                      (file-name-nondirectory buffer-file-name)))))))
1935
1936 ;; <f1> runs the compilation according to the compile-command (and
1937 ;; thus does not ask any confirmation), shows the compilation buffer
1938 ;; during compilation and delete all windows showing the compilation
1939 ;; buffer if the compilation ends with no error
1940
1941 ;; <shift-f1> asks for a compilation command and runs the compilation
1942 ;; but does not restore the window configuration (i.e. the compilation
1943 ;; buffer's window will still be visible, as usual)
1944
1945 ;; <f2> goes to the next compilation error (as C-x ` does on the
1946 ;; standard configuration)
1947
1948 (defun ff/restore-windows-if-no-error (buffer msg)
1949   "Delete the windows showing the compilation buffer if msg
1950   matches \"^finished\"."
1951
1952   (when (string-match "^finished" msg)
1953     ;;     (delete-windows-on buffer)
1954     (if (boundp 'ff/window-configuration-before-compilation)
1955         (set-window-configuration ff/window-configuration-before-compilation))
1956     )
1957   )
1958
1959 (add-to-list 'compilation-finish-functions 'ff/restore-windows-if-no-error)
1960
1961 (defun ff/fast-compile ()
1962   "Compiles without asking anything."
1963   (interactive)
1964   (let ((compilation-read-command nil))
1965     (setq ff/window-configuration-before-compilation (current-window-configuration))
1966     (compile compile-command)))
1967
1968 (setq compilation-read-command t
1969       compile-command "make -j -k"
1970       compile-history '("make clean" "make DEBUG=yes -j -k" "make -j -k")
1971       )
1972
1973 (defun ff/universal-compile () (interactive)
1974   (funcall (or (cdr (assoc major-mode
1975                            '(
1976                              (latex-mode . tex-file)
1977                              (html-mode . browse-url-of-buffer)
1978                              ;; Here you can add other mode -> compile command
1979                              )))
1980                'ff/fast-compile         ;; And this is the failsafe
1981                )))
1982
1983 (define-key global-map [f1] 'ff/universal-compile)
1984 (define-key global-map [(shift f1)] 'compile)
1985 (define-key global-map [f2] 'next-error)
1986
1987 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1988 ;; Related to mail
1989 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1990
1991 ;; (when (ff/load-or-alert "flyspell-timer" t)
1992 ;;   (add-hook 'flyspell-mode-hook 'flyspell-timer-ensure-idle-timer))
1993
1994 (defun ff/pick-dictionnary () (interactive)
1995   (when (and (boundp 'flyspell-mode) flyspell-mode)
1996     (if (and current-input-method (string-match "latin" current-input-method))
1997         (ispell-change-dictionary "francais")
1998       (ispell-change-dictionary "american"))
1999     ;;     (flyspell-buffer)
2000     )
2001   )
2002
2003 (defadvice toggle-input-method (after ff/switch-dictionnary nil activate)
2004   (ff/pick-dictionnary))
2005
2006 ;; (add-hook 'message-mode-hook 'auto-fill-mode)
2007 ;; (add-hook 'message-mode-hook 'flyspell-mode)
2008
2009 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2010 ;; Delete all windows which are in the same "column", which means
2011 ;; whose xmin and xmax are bounded by the xmin and xmax of the
2012 ;; currently selected column
2013 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2014
2015 ;; This is from emacs23 ! better than my old ff/delete-other-windows-in-column
2016
2017 (unless (fboundp 'delete-other-windows-vertically)
2018
2019   (defun delete-other-windows-vertically (&optional window)
2020     "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2021 This may be a useful alternative binding for \\[delete-other-windows]
2022  if you often split windows horizontally."
2023     (interactive)
2024     (let* ((window (or window (selected-window)))
2025            (edges (window-edges window))
2026            (w window) delenda)
2027       (while (not (eq (setq w (next-window w 1)) window))
2028         (let ((e (window-edges w)))
2029           (when (and (= (car e) (car edges))
2030                      (= (caddr e) (caddr edges)))
2031             (push w delenda))))
2032       (mapc 'delete-window delenda)))
2033   )
2034
2035 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2036 ;; Misc things
2037 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2038
2039 ;; Entropy is cool
2040
2041 (defun ff/entropy (l)
2042   (apply '+
2043          (mapcar
2044           (lambda (x)
2045             (if (= x 0.0) 0.0
2046               (* (- x) (/ (log x) (log 2)))))
2047           l)
2048          )
2049   )
2050
2051 ;; Usefull to deal with results in latex files
2052
2053 (defun ff/round-floats-in-region () (interactive)
2054   (save-restriction
2055     (condition-case nil
2056         (narrow-to-region (region-beginning) (region-end))
2057       (error (thing-at-point 'word)))
2058     (save-excursion
2059       (goto-char (point-min))
2060       (while (re-search-forward "[0-9\.]+" nil t)
2061         (let ((value (string-to-number (buffer-substring (match-beginning 0) (match-end 0)))))
2062           (delete-region (match-beginning 0) (match-end 0))
2063           (insert (format "%0.2f" value)))))))
2064
2065 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2066 ;; Keymaping
2067 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2068
2069 (load "info" nil t)
2070
2071 (define-key global-map [(shift iso-lefttab)] 'ispell-complete-word)
2072 ;; shift-tab going backward is kind of standard
2073 (define-key Info-mode-map [(shift iso-lefttab)] 'Info-prev-reference)
2074
2075 ;; (define-key global-map [(control x) (control a)] 'auto-fill-mode)
2076
2077 ;; Put back my keys, you thief!
2078 (define-key global-map [(home)] 'beginning-of-buffer)
2079 (define-key global-map [(end)] 'end-of-buffer)
2080 ;; (define-key global-map [(insertchar)] 'overwrite-mode)
2081 (define-key global-map [(delete)] 'delete-char)
2082
2083 ;; Cool shortcuts to move to the end / beginning of block keen
2084 (define-key global-map [(control right)] 'forward-sexp)
2085 (define-key global-map [(control left)] 'backward-sexp)
2086
2087 ;; Wheel mouse moves up and down 2 lines (and DO NOT BEEP when we are
2088 ;; out of the buffer)
2089
2090 (define-key global-map [mouse-4]
2091   (lambda () (interactive) (condition-case nil (scroll-down 2) (error nil))))
2092 (define-key global-map [mouse-5]
2093   (lambda () (interactive) (condition-case nil (scroll-up 2) (error nil))))
2094
2095 ;; with shift it goes faster
2096 (define-key global-map [(shift mouse-4)]
2097   (lambda () (interactive) (condition-case nil (scroll-down 50) (error nil))))
2098 (define-key global-map [(shift mouse-5)]
2099   (lambda () (interactive) (condition-case nil (scroll-up 50) (error nil))))
2100
2101 ;; Meta-? shows the properties of the character at point
2102 (define-key global-map [(meta ??)]
2103   (lambda () (interactive)
2104     (message (prin1-to-string (text-properties-at (point))))))
2105
2106 ;; Compiles the latex file in the current buffer
2107
2108 (setq tex-start-commands "\\input")
2109 (define-key global-map [f3] 'tex-file)
2110 (define-key global-map [(shift f3)] 'tex-bibtex-file)
2111
2112 ;; To run xdvi on the dvi associated to the .tex in the current
2113 ;; buffer, and to edit the .fig or bitmap image used to generate the
2114 ;; .eps at point
2115
2116 (define-key global-map [f4] 'ff/run-viewer)
2117
2118 ;; Closes the current \begin{}
2119
2120 (when (ff/load-or-alert "longlines")
2121
2122   (setq longlines-show-hard-newlines t
2123         longlines-auto-wrap t
2124         ;; longlines-show-effect #("|\n" 0 2 (face escape-glyph))
2125         ;; longlines-show-effect #("∴\n" 0 2 (face escape-glyph))
2126         longlines-show-effect #("•\n" 0 2 (face escape-glyph))
2127         ;; longlines-show-effect #("↵\n" 0 2 (face escape-glyph))
2128         )
2129
2130   ;; (defun ff/auto-longlines ()
2131   ;; (when (save-excursion
2132   ;; (goto-char (point-min))
2133   ;; (re-search-forward "^.\\{81,\\}$" nil t))
2134   ;; (longlines-mode)
2135   ;; (message "Switched on the lonlines mode automatically")
2136   ;; ))
2137
2138   ;; (add-hook 'latex-mode-hook 'ff/auto-longlines)
2139
2140   )
2141
2142 ;; Meta-/ remaped (completion)
2143
2144 (define-key global-map [(shift right)] 'dabbrev-expand)
2145 (define-key global-map [(meta =)] 'dabbrev-expand)
2146
2147 ;; Change the current window.
2148
2149 (defun ff/next-same-frame-window () (interactive)
2150   (select-window (next-window (selected-window)
2151                               (> (minibuffer-depth) 0)
2152                               nil)))
2153
2154 (defun ff/previous-same-frame-window () (interactive)
2155   (select-window (previous-window (selected-window)
2156                                   (> (minibuffer-depth) 0)
2157                                   nil)))
2158
2159 (define-key global-map [(shift prior)] 'ff/next-same-frame-window)
2160 (define-key global-map [(shift next)] 'ff/previous-same-frame-window)
2161
2162 (define-key global-map [(control })] 'enlarge-window-horizontally)
2163 (define-key global-map [(control {)] 'shrink-window-horizontally)
2164 (define-key global-map [(control \")] 'enlarge-window)
2165 (define-key global-map [(control :)] 'shrink-window)
2166
2167 ;; (define-key global-map [(control shift prior)] 'next-multiframe-window)
2168 ;; (define-key global-map [(control shift next)] 'previous-multiframe-window)
2169
2170 ;; I have two screens sometime!
2171
2172 (define-key global-map [(meta next)] 'other-frame)
2173 (define-key global-map [(meta prior)] (lambda () (interactive) (other-frame -1)))
2174
2175 (define-key global-map [(shift home)] 'delete-other-windows-vertically)
2176
2177 ;; (define-key global-map [(control +)] 'enlarge-window)
2178 ;; (define-key global-map [(control -)] 'shrink-window)
2179
2180 ;; Goes to next/previous buffer
2181
2182 (define-key global-map [(control prior)] 'ff/next-buffer)
2183 (define-key global-map [(control next)] 'ff/prev-buffer)
2184
2185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2186 ;; If M-. on a symbol, show where it is defined in another window
2187 ;; without giving focus, cycle if repeated.
2188 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2189
2190 (when (ff/load-or-alert "etags")
2191
2192   (defun ff/find-tag-nofocus () (interactive)
2193     "Show in another window the definition of the current tag"
2194     (let ((tag (find-tag-default)))
2195       (display-buffer (find-tag-noselect tag (string= tag last-tag)))
2196       (message "Tag %s" tag)
2197       )
2198     )
2199
2200   (define-key global-map [(meta .)] 'ff/find-tag-nofocus)
2201   )
2202
2203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2204 ;; Destroys the current buffer and its window if it's not the only one
2205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2206
2207 (defcustom ff/kill-this-buffer-and-delete-window-exceptions ""
2208   "*Regexp matching the buffer names which have to be kept when using
2209 `ff/kill-this-buffer-and-delete-window'.")
2210
2211 (defun ff/kill-this-buffer-and-delete-window (universal)
2212   "Unless its name matches
2213 `ff/kill-this-buffer-and-delete-window-exceptions', kills the
2214 current buffer and deletes the current window if it's not the
2215 only one in the frame. If the buffer has to be kept, go to the
2216 next one. With universal argument, kill all killable buffers."
2217   (interactive "P")
2218   (if universal
2219       (let ((nb-killed 0))
2220         (mapc (lambda (x)
2221                 (unless (string-match ff/kill-this-buffer-and-delete-window-exceptions
2222                                       (buffer-name x))
2223                   (kill-buffer x)
2224                   (setq nb-killed (1+ nb-killed))
2225                   ))
2226               (buffer-list))
2227         (message "Killed %d buffer%s" nb-killed (if (> nb-killed 1) "s" "")))
2228     (if (string-match ff/kill-this-buffer-and-delete-window-exceptions (buffer-name))
2229         (ff/next-buffer)
2230       (kill-this-buffer)))
2231   ;; (unless (one-window-p t) (delete-window))
2232   )
2233
2234 (define-key global-map [(control backspace)] 'ff/kill-this-buffer-and-delete-window)
2235 ;; (define-key calc-mode-map [(control backspace)] 'calc-quit)
2236
2237
2238 (setq ff/kill-this-buffer-and-delete-window-exceptions
2239       "^ \\|\\*Messages\\*\\|\\*scratch\\*\\|\\*Group\\*\\|\\*-jabber-\\*\\|\\*-jabber-process-\\*\\|\\*media\\*")
2240
2241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2242 ;; Misc stuff
2243 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2244
2245 (defun ff/elisp-debug-on ()
2246   "Switches `debug-on-error' and `debug-on-quit'."
2247   (interactive)
2248   (if debug-on-error
2249       (setq debug-on-error nil
2250             debug-on-quit nil)
2251     (setq debug-on-error t
2252           debug-on-quit t))
2253   (if debug-on-error
2254       (message "elisp debug on")
2255     (message "elisp debug off")))
2256
2257 (defun ff/create-dummy-buffer (&optional universal) (interactive "P")
2258   (find-file (concat "/tmp/" (ff/non-existing-filename "/tmp/" "dummy" "")))
2259   (text-mode)
2260   (if universal (ff/insert-url (current-kill 0)))
2261   (message "New dummy text-mode buffer"))
2262
2263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2264 ;; Recentf to keep a list of recently visited files. I use it
2265 ;; exclusively with my selector.el
2266 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2267
2268 (load "recentf")
2269
2270 (setq recentf-exclude
2271       (append recentf-exclude
2272               '("enotes$" "secure-notes$" "media-playlists$"
2273                 "bbdb$"
2274                 "svn-commit.tmp$" ".git/COMMIT_EDITMSG$"
2275                 "\.bbl$" "\.aux$" "\.toc$"))
2276       recentf-max-saved-items 1000
2277       recentf-save-file "~/private/emacs/recentf"
2278       )
2279
2280 (when (boundp 'recentf-keep) (add-to-list 'recentf-keep 'file-remote-p))
2281
2282 ;; Removes the link if we add the file itself (I am fed up with
2283 ;; duplicates because of vc-follow-symlinks)
2284
2285 (defadvice recentf-add-file (before ff/remove-links (filename) activate)
2286   ;; If we are adding a filename corresponding to the last link we
2287   ;; have added, remove the latter
2288   (when (and recentf-list
2289              (file-symlink-p (car recentf-list))
2290              (string= filename (file-chase-links filename)))
2291     (setq recentf-list (cdr recentf-list))
2292     ))
2293
2294 (recentf-mode 1)
2295
2296 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2297 ;; My front-end to mplayer
2298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2299
2300 ;; (ff/compile-when-needed "media/mplayer")
2301 ;; (ff/compile-when-needed "media")
2302
2303 (when (ff/load-or-alert "media")
2304
2305   (unless window-system
2306     (ff/configure-faces
2307      '(
2308        (media/mode-string-face
2309         :foreground "blue4" :weight 'bold)
2310
2311        (media/current-tune-face
2312         :foreground "black" :background "yellow" :weight 'normal)
2313
2314        (media/instant-highlight-face
2315         :foreground "black" :background "orange" :weight 'normal)
2316        ))
2317     )
2318
2319   (define-key global-map [(meta \\)] 'media)
2320
2321   (setq media/expert t
2322         media/add-current-song-to-interrupted-when-killing t
2323         media/duration-to-history 30
2324         media/history-size 1000
2325         media/playlist-file "~/private/emacs/media-playlists"
2326         media/mplayer/args '(
2327                              "-framedrop"
2328                              "-zoom"
2329                              "-cache" "512"
2330                              "-subfont-osd-scale" "3"
2331                              ;; "-stop-xscreensaver"
2332                              ;; "-osdlevel" "3"
2333                              )
2334         media/mplayer/timing-request-period 5.0
2335         )
2336   )
2337
2338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2339 ;; A dynamic search
2340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2341
2342 ;; selector.el is one of my own scripts, check my web page
2343
2344 (when (ff/load-or-alert "selector" t)
2345   ;; (define-key global-map [(shift return)] 'selector/quick-move-in-buffer)
2346   (define-key global-map [(control x) (control b)] 'selector/switch-buffer)
2347
2348   (defun ff/visit-debpkg-file (&optional regexp)
2349     "This function lists all the files found with dpkg -S and
2350 proposes to visit them."
2351     (interactive "sPattern: ")
2352
2353     (selector/select
2354
2355      (mapcar
2356       (lambda (s)
2357         (cons (selector/filename-to-string s) s))
2358       (split-string
2359        (shell-command-to-string (concat "dpkg -S " regexp " | awk '{print $2}'"))))
2360
2361      'selector/find-file
2362      "*selector find-file*"
2363      ))
2364   )
2365
2366 (add-hook 'selector/mode-hook (lambda () (setq truncate-lines t)))
2367
2368 (defun ff/selector-insert-record-callback (r)
2369   (bbdb-display-records (list r))
2370   ;; Weird things will happen if you kill the buffer from which you
2371   ;; invoked ff/selector-mail-from-bbdb
2372   (insert (car (elt r 6)))
2373   )
2374
2375 (defun ff/selector-compose-mail-callback (r)
2376   (vm-compose-mail (car (elt r 6)))
2377   )
2378
2379 (defun ff/selector-mail-from-bbdb () (interactive)
2380   (selector/select
2381    (mapcar
2382     (lambda (r) (cons (concat (elt r 0)
2383                               " "
2384                               (elt r 1)
2385                               " ("
2386                               (car (elt r 6))
2387                               ")")
2388                       r))
2389     (bbdb-records))
2390    (if (string= mode-name "Mail")
2391        'ff/selector-insert-record-callback
2392      'ff/selector-compose-mail-callback)
2393    "*bbdb-search*"
2394    )
2395   )
2396
2397 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2398 ;; A function to remove temporary alarm windows
2399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2400
2401 (defcustom ff/annoying-windows-regexp
2402   "\\*Messages\\*\\|\\*compilation\\*\\|\\*tex-shell\\*\\|\\*Help\\*\\|\\*info\\*\\|\\*Apropos\\*\\|\\*BBDB\\*\\|\\*.*-diff\\*"
2403   "The regexp matching the windows to be deleted by `ff/delete-annoying-windows'"
2404   )
2405
2406 (defun ff/delete-annoying-windows ()
2407   "Close all the windows showing buffers whose names match
2408 `ff/annoying-windows-regexp'."
2409   (interactive)
2410   (when ff/annoying-windows-regexp
2411     (mapc (lambda (w)
2412             (when (and (not (one-window-p w))
2413                        (string-match ff/annoying-windows-regexp
2414                                      (buffer-name (window-buffer w))))
2415               (delete-window w)))
2416           (window-list)
2417           )
2418     (message "Removed annoying windows")
2419     )
2420   )
2421
2422 (setq ff/annoying-windows-regexp
2423       (concat ff/annoying-windows-regexp
2424               "\\|\\*unspooled mails\\*\\|\\*enotes alarms\\*\\|\\*system info\\*"))
2425
2426 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2427 ;; Some handy functions
2428 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2429
2430 (defun ff/twin-horizontal-current-buffer () (interactive)
2431   (delete-other-windows)
2432   (split-window-horizontally)
2433   (balance-windows)
2434   )
2435
2436 (defun ff/twin-vertical-current-buffer () (interactive)
2437   (delete-other-windows)
2438   (split-window-vertically)
2439   (balance-windows)
2440   )
2441
2442 (defun ff/flyspell-mode (arg) (interactive "p")
2443   (if flyspell-mode (flyspell-mode -1)
2444     (flyspell-mode 1)
2445     (flyspell-buffer))
2446 )
2447
2448 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2449 ;; The fridge!
2450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2451
2452 (defun ff/move-region-to-fridge () (interactive)
2453   "Cut the current region, paste it in a file called ./fridge
2454 with a time tag, and save this file"
2455   (unless (use-region-p) (error "No region selected"))
2456   (let ((bn (file-name-nondirectory (buffer-file-name))))
2457     (kill-region (region-beginning) (region-end))
2458     (with-current-buffer (find-file-noselect "fridge")
2459       (goto-char (point-max))
2460       (insert "\n")
2461       (insert "######################################################################\n")
2462       (insert "\n"
2463               (format-time-string "%Y %b %d %H:%M:%S" (current-time))
2464               " (from "
2465               bn
2466               ")\n\n")
2467       (yank)
2468       (save-buffer)
2469       (message "Region moved to fridge")
2470       )
2471     )
2472   )
2473
2474 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2475 ;; My own keymap
2476 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2477
2478 (setq ff/map (make-sparse-keymap))
2479 (define-key global-map [(control \`)] ff/map)
2480 (define-key esc-map "`" ff/map)
2481
2482 (defun ff/git-status (&optional dir) (interactive)
2483   (if (buffer-file-name)
2484       (git-status (file-name-directory (buffer-file-name)))
2485     (error "No file attached to this buffer")))
2486
2487 (defun ff/insert-date () (interactive)
2488   (insert (format-time-string "\n * %Y %b %d %H:%M:%S\n\n" (current-time)))
2489   )
2490
2491 (define-key ff/map [(control g)] 'ff/git-status)
2492 (define-key ff/map [(control w)] 'server-edit)
2493 (define-key ff/map [(control d)] 'ff/elisp-debug-on)
2494 ;; (define-key ff/map "d" 'diary)
2495 (define-key ff/map "d" 'ff/insert-date)
2496 (define-key ff/map [(control \`)] 'ff/bash-new-buffer)
2497 (define-key ff/map [(control n)] 'enotes/show-all-notes)
2498 (define-key ff/map [(control s)] 'ff/secure-note-add)
2499 (define-key ff/map [(control t)] 'ff/start-test-code)
2500 (define-key ff/map [(control q)] 'ff/create-dummy-buffer)
2501 (define-key ff/map [(control a)] 'auto-fill-mode)
2502 (define-key ff/map [(control i)] 'ff/system-info)
2503 (define-key ff/map "w" 'ff/word-occurences)
2504 (define-key ff/map [(control c)] 'calendar)
2505 ;; (define-key ff/map [(control c)] (lambda () (interactive) (save-excursion (calendar))))
2506 (define-key ff/map [(control l)] 'goto-line)
2507 (define-key ff/map "l" 'longlines-mode)
2508 (define-key ff/map [(control o)] 'selector/quick-pick-recent)
2509 (define-key ff/map "s" 'selector/quick-move-in-buffer)
2510 (define-key ff/map "S" 'selector/search-sentence)
2511 (define-key ff/map "t" (lambda () (interactive) (find-file "~/private/TODO.txt")))
2512 (define-key ff/map "h" 'ff/tidy-html)
2513 (define-key ff/map "c" 'ff/count-char)
2514 (define-key ff/map [(control p)] 'ff/print-to-file)
2515 (define-key ff/map "P" 'ff/print-to-printer)
2516 (define-key ff/map [(control b)] 'bbdb)
2517 (define-key ff/map "m" 'ff/selector-mail-from-bbdb)
2518 (define-key ff/map [(control m)] 'woman)
2519 (define-key ff/map "b" 'bookmark-jump)
2520 (define-key ff/map [(control =)] 'calc)
2521 (define-key ff/map [(control shift b)]
2522   (lambda () (interactive)
2523     (bookmark-set)
2524     (bookmark-save)))
2525 (define-key ff/map "f" 'ff/move-region-to-fridge)
2526 (define-key ff/map [(control f)] 'ff/flyspell-mode)
2527
2528 (define-key ff/map [?\C-0] 'ff/delete-annoying-windows)
2529 (define-key ff/map "1" 'delete-other-windows)
2530 (define-key ff/map [?\C-1] 'delete-other-windows)
2531 (define-key ff/map "2" 'ff/twin-vertical-current-buffer)
2532 (define-key ff/map [?\C-2] 'ff/twin-vertical-current-buffer)
2533 (define-key ff/map "3" 'ff/twin-horizontal-current-buffer)
2534 (define-key ff/map [?\C-3] 'ff/twin-horizontal-current-buffer)
2535
2536 (define-key ff/map " " 'delete-trailing-whitespace)
2537
2538 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2539 ;; Hacks so that all keys are functionnal in xterm and through ssh.
2540 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2541
2542 (unless window-system
2543
2544   ;; One day I will understand these clipboard business. Until then,
2545   ;; so that it works in xterm (yes), let's use xclip. This is a bit
2546   ;; ugly.
2547
2548   ;; (defun ff/yank-with-xclip (&optional arg)
2549   ;; "Paste the content of the X clipboard with the xclip
2550   ;; command. Without ARG converts some of the '\\uxxxx' characters."
2551   ;; (interactive "P")
2552   ;; (with-temp-buffer
2553   ;; (shell-command "xclip -o" t)
2554   ;; (unless arg
2555   ;; (mapc (lambda (x) (replace-string (concat "\\u" (car x)) (cdr x) nil (point-min) (point-max)))
2556   ;; '(("fffd" . "??")
2557   ;; ("2013" . "-")
2558   ;; ("2014" . "--")
2559   ;; ("2018" . "`")
2560   ;; ("2019" . "'")
2561   ;; ("201c" . "``")
2562   ;; ("201d" . "''")
2563   ;; ("2022" . "*")
2564   ;; ("2026" . "...")
2565   ;; ("20ac" . "EUR")
2566   ;; )))
2567   ;; (kill-ring-save (point-min) (point-max)))
2568
2569   ;; (yank))
2570
2571   ;; (define-key global-map [(meta y)] 'ff/yank-with-xclip)
2572
2573   ;;   (set-terminal-coding-system 'iso-latin-1)
2574   ;; (set-terminal-coding-system 'utf-8)
2575
2576   ;; I have in my .Xressource
2577
2578   ;; XTerm.VT100.translations: #override\n\
2579   ;;   <Btn4Down>,<Btn4Up>:scroll-back(2,line)\n\
2580   ;;   <Btn5Down>,<Btn5Up>:scroll-forw(2,line)\n\
2581   ;;   Ctrl<Btn4Down>,Ctrl<Btn4Up>:scroll-back(1,page)\n\
2582   ;;   Ctrl<Btn5Down>,Ctrl<Btn5Up>:scroll-forw(1,page)\n\
2583   ;;   Shift<Btn4Down>,Shift<Btn4Up>:scroll-back(1,halfpage)\n\
2584   ;;   Shift<Btn5Down>,Shift<Btn5Up>:scroll-forw(1,halfpage)\n\
2585   ;;   Alt<KeyPress>:insert-eight-bit()\n\
2586   ;;   !Shift<Key>BackSpace: string("\7f")\n\
2587   ;;   Ctrl<Key>BackSpace: string("\eOZ")\n\
2588   ;;   Shift<Key>Prior: string("\e[5;2~")\n\
2589   ;;   Shift<Key>Next: string("\e[6;2~")\n\
2590   ;;   Shift Ctrl<Key>]: string("\eO}")\n\
2591   ;;   Shift Ctrl<Key>[: string("\eO{")\n\
2592   ;;   Shift Ctrl<Key>/: string("\eO?")\n\
2593   ;;   Ctrl<Key>/: string("\eO/")\n\
2594   ;;   Shift Ctrl<Key>=: string("\eO+")\n\
2595   ;;   Ctrl<Key>=: string("\eO=")\n\
2596   ;;   Shift Ctrl<Key>;: string("\eO:")\n\
2597   ;;   Ctrl<Key>;: string("\eO;")\n\
2598   ;;   Shift Ctrl<Key>`: string("\eO~")\n\
2599   ;;   Ctrl<Key>`: string("\eO`")\n\
2600   ;;   Shift Ctrl<Key>': string("\eO\\\"")\n\
2601   ;;   Ctrl<Key>': string("\eO'")\n\
2602   ;;   Shift Ctrl<Key>.: string("\eO>")\n\
2603   ;;   Ctrl<Key>.: string("\eO.")\n\
2604   ;;   Shift Ctrl<Key>\\,: string("\eO<")\n\
2605   ;;   Ctrl<Key>\\,: string("\eO,")
2606
2607   (define-key function-key-map "\e[2~" [insert])
2608
2609   (define-key function-key-map "\e[Z" [S-iso-lefttab])
2610
2611   (define-key function-key-map "\e[1;2A" [S-up])
2612   (define-key function-key-map "\e[1;2B" [S-down])
2613   (define-key function-key-map "\e[1;2C" [S-right])
2614   (define-key function-key-map "\e[1;2D" [S-left])
2615   (define-key function-key-map "\e[1;2F" [S-end])
2616   (define-key function-key-map "\e[1;2H" [S-home])
2617
2618   (define-key function-key-map "\e[2;2~" [S-insert])
2619   (define-key function-key-map "\e[5;2~" [S-prior])
2620   (define-key function-key-map "\e[6;2~" [S-next])
2621
2622   (define-key function-key-map "\e[1;2P" [S-f1])
2623   (define-key function-key-map "\e[1;2Q" [S-f2])
2624   (define-key function-key-map "\e[1;2R" [S-f3])
2625   (define-key function-key-map "\e[1;2S" [S-f4])
2626   (define-key function-key-map "\e[15;2~" [S-f5])
2627   (define-key function-key-map "\e[17;2~" [S-f6])
2628   (define-key function-key-map "\e[18;2~" [S-f7])
2629   (define-key function-key-map "\e[19;2~" [S-f8])
2630   (define-key function-key-map "\e[20;2~" [S-f9])
2631   (define-key function-key-map "\e[21;2~" [S-f10])
2632
2633   (define-key function-key-map "\e[1;5A" [C-up])
2634   (define-key function-key-map "\e[1;5B" [C-down])
2635   (define-key function-key-map "\e[1;5C" [C-right])
2636   (define-key function-key-map "\e[1;5D" [C-left])
2637   (define-key function-key-map "\e[1;5F" [C-end])
2638   (define-key function-key-map "\e[1;5H" [C-home])
2639
2640   (define-key function-key-map "\e[2;5~" [C-insert])
2641   (define-key function-key-map "\e[5;5~" [C-prior])
2642   (define-key function-key-map "\e[6;5~" [C-next])
2643
2644   (define-key function-key-map "\e[1;9A" [M-up])
2645   (define-key function-key-map "\e[1;9B" [M-down])
2646   (define-key function-key-map "\e[1;9C" [M-right])
2647   (define-key function-key-map "\e[1;9D" [M-left])
2648   (define-key function-key-map "\e[1;9F" [M-end])
2649   (define-key function-key-map "\e[1;9H" [M-home])
2650
2651   (define-key function-key-map "\e[2;9~" [M-insert])
2652   (define-key function-key-map "\e[5;9~" [M-prior])
2653   (define-key function-key-map "\e[6;9~" [M-next])
2654
2655   ;; The following ones are not standard
2656
2657   (define-key function-key-map "\eO}" (kbd "C-}"))
2658   (define-key function-key-map "\eO{" (kbd "C-{"))
2659   (define-key function-key-map "\eO?" (kbd "C-?"))
2660   (define-key function-key-map "\eO/" (kbd "C-/"))
2661   (define-key function-key-map "\eO:" (kbd "C-:"))
2662   (define-key function-key-map "\eO;" (kbd "C-;"))
2663   (define-key function-key-map "\eO~" (kbd "C-~"))
2664   (define-key function-key-map "\eO`" (kbd "C-\`"))
2665   (define-key function-key-map "\eO\"" (kbd "C-\""))
2666   (define-key function-key-map "\eO|" (kbd "C-|"))
2667   (define-key function-key-map "\eO'" (kbd "C-'"))
2668   (define-key function-key-map "\eO>" (kbd "C->"))
2669   (define-key function-key-map "\eO." (kbd "C-."))
2670   (define-key function-key-map "\eO<" (kbd "C-<"))
2671   (define-key function-key-map "\eO," (kbd "C-,"))
2672   (define-key function-key-map "\eO-" (kbd "C--"))
2673   (define-key function-key-map "\eO=" (kbd "C-="))
2674   (define-key function-key-map "\eO+" (kbd "C-+"))
2675
2676   (define-key function-key-map "\eOZ" [C-backspace])
2677
2678   (define-key minibuffer-local-map "\10" 'previous-history-element)
2679   (define-key minibuffer-local-map "\ e" 'next-history-element)
2680
2681   ;; (define-key global-map [(alt prior)] 'ff/prev-buffer)
2682   ;; (define-key global-map [(alt next)] 'ff/next-buffer)
2683
2684   )
2685
2686 ;; I am fed up with Alt-Backspace in the minibuffer erasing the
2687 ;; content of the kill-ring
2688
2689 (defun ff/backward-delete-word (arg)
2690   "Delete characters forward until encountering the end of a word, but do not put them in the kill ring.
2691 With argument ARG, do this that many times."
2692   (interactive "p")
2693   (delete-region (point) (progn (forward-word (- arg)) (point))))
2694
2695 (define-key minibuffer-local-map
2696   [remap backward-kill-word] 'ff/backward-delete-word)
2697
2698 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2699 ;; Privacy
2700 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2701
2702 ;; Where to save the bookmarks and where is bbdb
2703
2704 (setq bookmark-default-file "~/private/emacs/bmk"
2705       bbdb-file "~/private/bbdb"
2706       custom-file "~/private/emacs/custom")
2707
2708 ;; enotes.el is one of my own scripts, check my web page
2709
2710 (when (ff/load-or-alert "enotes" t)
2711   (setq enotes/file "~/private/enotes"
2712         enotes/show-help nil
2713         enotes/full-display nil
2714         enotes/default-time-fields "9:30")
2715
2716   (enotes/init)
2717   ;; (add-hook 'enotes/alarm-hook
2718   ;;  (lambda () (ff/play-sound-async "~/local/sounds/three_notes2.wav")))
2719   )
2720
2721 ;; (when (ff/load-or-alert "goto-last-change.el")
2722 ;; (define-key global-map [(control x) (control a)] 'goto-last-change))
2723
2724 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2725 ;; My private stuff (email adresses, mail filters, etc.)
2726 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2727
2728 (ff/load-or-alert "~/private/emacs.perso.el" t)
2729
2730 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2731 ;; emacs server
2732 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2733
2734 ;; Runs in server mode, so that emacsclient works
2735 (server-start)
2736
2737 (defun ff/raise-frame-and-give-focus ()
2738   (when window-system
2739     (raise-frame)
2740     (x-focus-frame (selected-frame))
2741     (set-mouse-pixel-position (selected-frame) 4 4)
2742     ))
2743
2744 ;; Raises the window when the server is invoked
2745
2746 (add-hook 'server-switch-hook 'ff/raise-frame-and-give-focus)