Cool Emacs Hacks

I'm an Emacs-guy, have been for a long time. As much as I use vi
from time-to-time, I never could get into modal editing. Despite
having used GNU Emacs as a programmer for years, I still learn cool
Emacs-fu all the time. Here are a few recent findings:

* Useful Emacs commands, from the Reddit community [0]
* Emacs search and replace across files [1]
* Building regular expressions with Emacs [2]

I do a lot of Perl web development, meaning Perl but also CSS, HTML
and Javascript are in the mix, sometimes all in the same file.
Emacs' MMM-mode [3] works really well if you want to color-highlight
the syntax of disparate languages in the same buffer. Here is the
configuration I use in my .emacs:

(add-to-list 'auto-mode-alist '("\\.tmpl\\'" . html-mode))
(add-to-list 'auto-mode-alist '("\\.[sj]?html?\\'" . html-mode))

;; CSS-Mode
(autoload 'css-mode "css-mode")
(add-to-list 'auto-mode-alist '("\\.css\\'" . css-mode))
(setq cssm-indent-function #'cssm-c-style-indenter)
(setq cssm-indent-level '4)

;; javascript-generic-mode
(require 'generic-x)

;; MMM-Mode
(require 'mmm-auto)
(setq mmm-global-mode 'maybe)

;; Set up an mmm group for fancy html editing
(mmm-add-group
'fancy-html
'(
        (html-perl-embedded
               :submode cperl-mode
               :face mmm-output-submode-face
               :front "\\[perl\\]"
               :back "\\[/perl\\]")
        (html-css-embedded
               :submode css-mode
               :face mmm-declaration-submode-face
               :front "<style\[^>\]*>"
               :back "</style>")
        (html-css-attribute
               :submode css-mode
               :face mmm-declaration-submode-face
               :front "\\bstyle=\\s-*\""
               :back "\"")
        (html-javascript-embedded
               :submode javascript-generic-mode
               :face mmm-code-submode-face
               :front "<script\[^>\]*>"
               :back "</script>")
        (html-javascript-attribute
               :submode javascript-generic-mode
               :face mmm-code-submode-face
               :front "\\bon\\w+=\\s-*\""
               :back "\"")
  )
)

(add-to-list 'mmm-mode-ext-classes-alist '(html-mode nil fancy-html))

(add-hook 'html-mode-hook 'guess-xhtml-hook t)

(defun guess-xhtml-hook ()
 "Guess whether the current buffer is XHTML."
 (when
   (save-excursion
     (search-forward-regexp "<[?]xml\\|//W3C//DTD XHTML" 80 t))
   (html-mode)
   ))


;; xml-mode
(autoload 'nxml-mode "nxml-mode" nil t)
(defalias 'xml-mode 'nxml-mode)

(defalias 'perl-mode 'cperl-mode)

(custom-set-variables
'(cperl-close-paren-offset -4)
'(cperl-continued-statement-offset 4)
'(cperl-indent-level 4)
'(cperl-indent-parens-as-block t)
'(cperl-tab-always-indent t))

(setq-default indent-tabs-mode nil)

(defun perl-eval (beg end)
 "Run selected region as Perl code"
 (interactive "r")
 (shell-command-on-region beg end "perl"))
(global-set-key "\M-\C-p" 'perl-eval)

[0] http://www.reddit.com/r/programming/comments/8lfx7/what_emacs_commands_do_you_use_most_and_find_most
[1] http://atomized.org/2009/05/emacs-nerdery-search-replace-across-files
[2] http://emacs-fu.blogspot.com/2009/05/building-regular-expression.html
[3] http://mmm-mode.sourceforge.net