Category Archives: wickedcoolemacs

On this page:

Emacs and Gnus: zomg, new chapter out the door!

So I _finally_ pulled everything together and got my Gnus chapter out the door. Hooray, hooray, hooray!

Disclaimers: It’s rough, it probably makes a few assumptions about whatever version of Emacs I’m running, it’s probably missing your favorite tips (and I’d love to add them!), and it probably has typos. Meep. But it’s out there!

Hooray, hooray, hooray!

Next step: write about web-browsing in Emacs…

Emacs and PHP tutorial: php-mode

php-mode is responsible for syntax highlighting, indentation, and other major PHP-specific modifications to your editing environment. There are a number of PHP modes available for Emacs. In this project, you’ll learn how to set up the php-mode available from http://sourceforge.net/projects/php-mode/ . At the time of this writing, the current version is 1.4.0 and the maintainer is Aaron Hawley.

Download the latest php-mode.el from http://php-mode.sourceforge.net/ and save it to a directory in your load-path. I like to organize my Emacs Lisp files in a directory called ~/elisp. To add PHP support to your Emacs, add the following lines to your ~/.emacs:

 (add-to-list 'load-path "~/elisp")
 (require 'php-mode)

This configures Emacs to automatically recognize files ending in “.php”, “.phps”, “.php3″, “.php4″, “.phtml”, and “.inc” as PHP files. To associate more extensions with PHP files, add lines like this example to your ~/.emacs:

 (add-to-list 'auto-mode-alist '("\\.module$" . php-mode))
 (add-to-list 'auto-mode-alist '("\\.inc$" . php-mode))
 (add-to-list 'auto-mode-alist '("\\.install$" . php-mode))
 (add-to-list 'auto-mode-alist '("\\.engine$" . php-mode))

This associates php-mode with the extensions used by Drupal, a PHP framework. When you open a file with the specified extension, it should be highlighted according to PHP syntax.

Here are some useful commands:

TAB c-indent-command Indent the current line
M-; comment-dwim Add a line comment, comments or uncomments the currently-selected region, or does other smart comment-related actions
C-c C-f php-search-documentation Search the online PHP manual for the current word
C-c RET php-browse-manual View the online PHP manual
C-c . c-set-style Change coding style
C-M-a, C-M-e c-beginning-of-defun, c-end-of-defun Go to the beginning or end of the current function
C-M-h c-mark-function Select the current function
M-a, M-e c-beginning-of-statement, c-end-of-statement Go to the beginning or end of the current statement

Here are some variables you may wish to customize:

indent-tabs-mode Set this to nil if you want to insert spaces instead of tabs
case-fold-search Set this to t if you want case-insensitive search.
c-basic-offset Set your tab size or number of spaces used as a basis for indentation

You can either customize these variables globally with M-x customize or set them for php-mode. Here’s an example that sets up a buffer with the coding style recommended for Drupal:

 (defun wicked/php-mode-init ()
   "Set some buffer-local variables."
   (setq case-fold-search t) 
   (setq indent-tabs-mode nil)
   (setq fill-column 78)
   (setq c-basic-offset 2)
   (c-set-offset 'arglist-cont 0)
   (c-set-offset 'arglist-intro '+)
   (c-set-offset 'case-label 2)
   (c-set-offset 'arglist-close 0))
 (add-hook 'php-mode-hook 'wicked/php-mode-init)

You can further customize the indentation by moving the point to where the indentation needs improvement and typing C-c C-o (c-set-offset).

To try automatic indentation, press C-j (newline-and-indent). If you like that behavior, you can make it the default in php-mode by adding the following line in ~/.emacs:

(define-key php-mode-map (kbd “RET”) ‘newline-and-indent)

You may also be interested in M-x show-paren-mode, which shows the matching parenthesis, bracket or brace for the character at point. You can enable it automatically by adding the following line to your ~/.emacs:

   (setq show-paren-mode t)

It’s a good idea to separate PHP and HTML code. This is not only better coding practice, but it also makes developing in Emacs much easier. php-mode focuses on PHP-specific behavior and does not have special support for HTML. Emacs has a number of packages that allow you to work with multiple modes like php-mode and html-helper-mode in a single buffer, but they don’t always work, and indentation can be confusing. If you must work with large segments of both PHP and HTML in the same file, check out MultipleModes (http://www.emacswiki.org/cgi-bin/wiki/MultipleModes) for tips.

Things I can do to make progress on my book

  • Switch my development environment to Emacs
  • Put together the existing book chapters I have so far
  • Process the tech reviews I’ve gotten back
  • Work on one outline item
  • Work on a different chapter
  • View and explain a random person’s .emacs file
  • Have regular release schedules
  • Work on the outline
  • Just write
  • Hang out in #emacs
  • Read Emacs-related blogs
  • Read random wiki pages on emacswiki.org
  • Post tidbits
  • Dig through my old Emacs configuration
  • Answer Emacs-related mail
  • Monitor help.gnu.emacs and other Emacs-related newsgroups/mailing lists
  • Learn about a random Emacs symbol
  • Write for 10 minutes
  • Brainstorm ideas
  • Upgrade my packages

Might need to spend more time hanging out with Emacs geeks =)

This is dreadful. I’ve made no progress on my book, and I’ve noticed that it has steadily crept down my list of priorities. I suspect it has a lot to do with the kinds of people I hang out with and the kinds of places I hang out. ;)

I used to hang out in irc.freenode.net#emacs a lot, and I used to frequently check the RecentChanges page of http://www.emacswiki.org. Both were great sources for Emacs questions and answers, and they often inspired me to go and write blog posts sharing what I discovered.

Lately, I’ve been hanging out with Drupal geeks and social networking geeks–hence all the blog posts about Drupal and technology evangelism. This is because of work, and so my blog posts are about things I’m learning at work. My Emacs use is down to reading mail, reading news, and managing my day. I still use it every day, but I’m not doing a lot of development in it. (Hmm, maybe that’s something else I can set up.)

Maybe I should start writing from the front of the book instead – basic Emacs stuff, leading up to more advanced tips…

Emacs Gnus: Filter Spam

(draft for an upcoming book called Wicked Cool Emacs)

Ah, spam, the bane of our Internet lives. There is no completely
reliable way to automatically filter spam. Spam messages that slip
through the filters and perfectly legitimate messages that get
labelled spam are all part of the occupational hazards of using the
Internet.

The fastest way to filter spam is to use an external spam-filtering
program such as Spamassassin or Bogofilter, so your spam can be
filtered in the background and you don’t have to spend time in Emacs
filtering it yourself. In an ideal world, this would be done on the
mail server so that you don’t even need to download unwanted
messages. If your inbox isn’t full of ads for medicine or stocks, your
mail server is probably doing a decent job of filtering the mail for
you.

Server-based mail filtering

As spam filtering isn’t an exact science, you’ll want to find out how
you can check your spam folder for misclassified mail. If you download
your mail through POP, find out if there’s a webmail interface that
will allow you to check if any real mail has slipped into the junk
mail pile. If you’re on IMAP, your mail server might automatically
file spam messages in a different group. Here’s how to add the spam
group to your list of groups:

  1. Type M-x gnus to bring up the group buffer.
  2. Type ^ (gnus-group-enter-server-mode).
  3. Choose the nnimap: entry for your mail server and press RET (gnus-server-read-server).
  4. Find the spam or junk mail group if it exists.
  5. Type u (gnus-browse-unsubscribe-current-group) to toggle the subscription. Subscribed groups will appear in your M-x gnus screen if they contain at least one unread message.

Another alternative is to have all the mail (spam and non-spam)
delivered to your inbox, and then let Gnus be in charge of filing it
into your spam and non-spam groups. If other people manage your mail
server, ask them if you can have your mail processed by the spam
filter but still delivered to your inbox. If you’re administering your
own mail server, set up a spam filtering system such as SpamAssassin
or BogoFilter, then read the documentation of your spam filtering
system to find out how to process the mail.

Spam filtering systems typically add a header such as “X-Spam-Status”
or “X-Bogosity” to messages in order to indicate which messages are
spam or even how spammy they are. To check if your mail server tags
your messages as spam, open one of your messages in Gnus and type C-u
g (gnus-summary-show-article) to view the complete headers and
message. If you find a spam-related header such as X-Spam-Status, you
can use it to split your mail. Add the following to your ~/.gnus:

 (setq spam-use-regex-headers t) ;; (1)
 (setq spam-regex-headers-spam "^X-Spam-Status: Yes")   ;; (2)
 (require 'spam) ;; (3)
 (spam-initialize) ;; (4)

This configures spam.el to detect spam based on message
headers(1). Set spam-regex-headers-spam to a regular expression
matching the header your mail server uses to indicate spam.(2) This
configuration should be done before the spam.el library is loaded(3)
and initialized(4), because spam.el uses the spam-use-* variables to
determine which parts of the spam library to load.

In order to take advantage of this, you’ll also need to add a rule
that splits spam messages into a different group. If you haven’t set
up mail splitting yet, read qthe instructions on setting up fancy mail
splitting in “Project XXX: Organize mail into groups”. Add (:
spam-split) to either nnmail-split-fancy or nnimap-split-fancy,
depending on your configuration. For example, your ~/.gnus may look
like this:

(setq nnmail-split-fancy
'(
;; ... other split rules go here ...
(: spam-split)
;; ... other split rules go here ...
"mail.misc")) ; default mailbox

(draft for an upcoming book called Wicked Cool Emacs, more to come!)

Emacs Gnus: Organize Your Mail

People handle large volumes of mail in different ways. Keeping
everything in one mailbox can quickly become unmanageable because
messages you need to read get lost among messages you don’t need to
read.

You can move mail manually by selecting them in the summary buffer and
typing B m (gnus-summary-move-article). Then type the name of the
group to which you would like to move the message. The group will be
created if it doesn’t exist.

To move multiple messages, mark them with #
(gnus-summary-mark-as-processable) and then type B m
(gnus-summary-move-article). To unmark a message, type M-#
(gnus-summary-unmark-as-processable). To unmark all messages, type M P
U (gnus-summary-unmark-all-processable).

Automatically filing mail

Moving messages by hand is tedious and time-consuming. One way to deal
with this is to set up rules that automatically file mail into
different groups (or folders, as they’re called in other mail
clients). Gnus calls this “splitting” mail, and you can split mail on
IMAP servers as well as mail downloaded from POP3 servers to your
computer.

For example, if you’re using Gnus to read mail from an IMAP server,
you can split your messages by adding this to your ~/.gnus:

 (setq nnimap-split-inbox "INBOX") ;; (1)
 (setq nnimap-split-predicate "UNDELETED") ;; (2)
 (setq nnimap-split-rule
       '(
         ("INBOX.emacs" "^Subject:.*emacs")
         ("INBOX.work" "^To:.*[email protected]")    
         ("INBOX.personal" "^To:.*[email protected]")    
         ("INBOX.errors" "^From:.*\\(mailer.daemon\\|postmaster\\)")   
        )) 

If you use a different inbox, change the value of
nnimap-split-inbox(1). Any messages in the inbox will be split
according to nnimap-split-rule(2), which is a list where each element
is a list containing the group’s name and a regular expression
matching the header of messages that should be filed in the group. In
this example, Gnus will move mail with subjects containing the word
“emacs” to INBOX.emacs, mail directed to [email protected] to the
INBOX.work group, mail directed to [email protected] to the
INBOX.personal group, and mail error messages to INBOX.errors. All
other messages will be stored in INBOX.

If you’re downloading your mail from a POP3 server and storing it in
nnml, add this to your ~/.gnus instead:

 (setq nnmail-split-methods
      '(
        ("mail.emacs" "^Subject:.*emacs")
        ("mail.work" "^To:.*[email protected]")    
        ("mail.personal" "^To:.*[email protected]")    
        ("mail.errors" "^From:.*\\(mailer.daemon\\|postmaster\\)")   
       )) 

All other messages will be stored in mail.misc.

Start M-x gnus again, and your mail will be split into the different
groups.

Where are my groups?

If you don’t see your new groups in the group buffer displayed by M-x
gnus, type A A (gnus-group-list-active) to see all the groups. Go to
the group that you would like to add to the group buffer, then type u
(gnus-group-unsubscribe-current-group) to toggle its subscription. In
this example, INBOX.automated is not subscribed to, but INBOX is.

 U    13: INBOX.automated 
      76: INBOX 

When you type M-x gnus again, you’ll see your subscribed groups if
they have unread messages.

nnimap-split-rule and nnmail-split-methods allow you to filter
interesting or uninteresting mail into different groups based on their
headers. Gnus comes with an even more powerful mail splitting engine.
In fact, Gnus comes with “fancy mail splitting.”

Fancy mail splitting

With fancy mail splitting and some configuration, you can split mail
based on a combination of criteria. You can even manually file a
message and have Gnus automatically file incoming replies in the same
group.

To configure an IMAP connection to use fancy mail splitting, add the
following to your ~/.gnus:

 (setq nnimap-split-inbox "INBOX")
 (setq nnimap-split-predicate "UNDELETED")
 (setq nnmail-split-fancy ;; (1)
       '(|                                ;; (2)
         (: gnus-registry-split-fancy-with-parent) ;; (3)
         ;; splitting rules go here       ;; (4)
         "INBOX"                          ;; (5)
        ))
 (setq nnimap-split-rule 'nnmail-split-fancy)
 (setq nnmail-split-methods 'nnimap-split-fancy) ;; (6)
 (gnus-registry-initialize) ;; (7)

This configures IMAP to use the nnmail-split-fancy function to
determine the group for messages. Note that we’re setting the
nnmail-split-fancy variable here. If you want to process your IMAP
mail separately from your other mail, you can set the
nnimap-split-fancy variable instead. If so, also set nnimap-split-rule
to ‘nnimap-split-fancy. Using nnmail-split-fancy here makes the other
examples easier to understand, though.

The nnmail-split-fancy variable controls the splitting behavior(1). The
“|” symbol means that that the first matching rule is used(2). For
example, if the message being processed is a reply to a message that
Gnus knows about, then the gnus-registry-split-fancy-with-parent
function will return the name of the group, and nnmail-split-fancy
will file the message there(3). You can add other splitting rules as
well(4). If messages don’t match any of these rules, the last rule
specifies that the messages will be filed in INBOX(5). Set
nnmail-split-methods to nnimap-split-fancy as well in order to work
around some assumptions in other parts of the code(6). After that,
initialize the Gnus registry(7), which is responsible for tracking
moved and deleted messages. This allows you to automatically split
replies into the same folders as the original messages.

To configure fancy mail splitting with an nnml backend (suggested
configuration for POP3), add the following to your ~/.gnus instead:

 (gnus-registry-initialize)
 (setq nnmail-split-fancy                 
       '(|                                
         (: gnus-registry-split-fancy-with-parent)
         ;; splitting rules go here       
         "mail.misc"                          ;; (1)
        ))
 (setq nnmail-split-methods 'nnmail-split-fancy)    

This code is similar to the IMAP example, except that the default
mailbox name for nnml is mail.misc(1).

Here’s how the previous rules in nnmail-split-methods would be
translated to nnmail-split-fancy rules for an IMAP configuration:

 (setq nnmail-split-fancy
      '(|
        (: gnus-registry-split-fancy-with-parent)
         ;; splitting rules go here       
        (from mail "INBOX.errors")   ;; (1)
        (any "[email protected]" "INBOX.work")   ;; (2)
        (any "you@personal.example.c[email protected]" "INBOX.personal") ;; 
        ("subject" "emacs" "INBOX.emacs") ;; (3)
        "INBOX"    ;; or "mail.misc" for nnml/POP3
       )) 

The from keyword matches against the “From”, “Sender”, and
“Resent-From” fields, while the mail keyword matches common mail
system addresses(1). The corresponding to keyword matches against
the “To”, “Cc”, “Apparently-To”, “Resent-To” and “Resent-Cc” headers,
while any matches the fields checked by the from and to
keywords(2). You can also compare against the subject
and other headers(3).

You can use logic in splitting rules, too. For example, if you like
reading the jokes on [email protected], but you don’t like
the ones sent by [email protected] (he not only has a bad sense of
humor, but also likes picking on Emacs!), you can use a rule like
this in your nnmail-split-fancy:

         ;; ... other splitting rules go here...
         (any "[email protected]"   ;; (1)
              (| (from "[email protected]" "INBOX.junk") ;; (2)
                 "INBOX.jokes")) ;; (3)
         ;; ... other splitting rules go here

The first rule matches all messages with
[email protected]” in from- or to-related headers.
Matching messages are processed with another split rule, which moves
messages from [email protected] to a separate group(2) and files the
other messages in INBOX.jokes(3). To learn more about creating complex
rules, read the Gnus Info manual for “Fancy Mail Splitting”.