Posts Tagged emacs

Using flymake in emacs to validate xml as you type

Motivation

  • Seeing mistakes earlier can make them easier to correct
  • Many programs will simply die with little logging if given broken xml files
  • Remembering to run a validator takes effort

Setup

  • Install rxp (this is available under cygwin on windows, and using apt under most linuxes)
  • Create this perl scripts, make it executable and place it on your path [1] call it xmlparse
    #!/usr/bin/perl
    open(HA, "rxp $ARGV[0] 2>&1|");
    while (){
        /^Error/ && chomp;
        print;
    }
  • Add the following code to your .emacs file
  • (defun flymake-xml-init ()
      (let* ((temp-file (flymake-init-create-temp-buffer-copy
    		     'flymake-create-temp-inplace))
    	 (local-file (file-relative-name
    		      temp-file
    		      (file-name-directory buffer-file-name))))
        (list "xmlparse" (list local-file))))
    
    (add-to-list 'flymake-allowed-file-name-masks
    	     '("\\.xml\\'" flymake-xml-init)) 
    
    (add-to-list 'flymake-err-line-patterns
    	     '("\\(.*\\) at line \\([0-9]+\\) char \\([0-9]+\\) of file://\\(.*\\)"
    	      4 2 3 1))
  • Also, ensure that you have the following command somewhere in your .emacs file
     (add-hook 'find-file-hook 'flymake-find-file-hook)

This should then work. You might like to change the face used to show errors with M-x customize-face flymake-errline. Hovering over an error will show you a description of the error

Caveats

The behavior for unclosed tags is not what I want. The error is reported where the parent tag closes – which can be quite some distance from the opening tag. However at least the message is informative.

There is an existing emacs host, nxhtml, which has some of these things built in together with other editing tools – but it looks a little monolithic.

[1] This ensures that each error is written on only one line. Flymake appears to only be able deal with output in this form.

Add comment February 23, 2009

Emacs version control

I discovered how emacs version control worked today… and was duelly impressed. (Having previously thought that it didn’t work at all well – guess this means I should learn to read manuals).[1]

Regardless, emacs comes with a unified version control front-end which supports back-ends for most version control systems : I believe this includes git, svn and bzr).  This can do most of the things that one wants to do.

One of my only irritations is that one can’t revert individual lines of the diffs that it shows.

Summary for the impatient

Emacs version control is wonderful, I’m dim for not realising this earlier. To make it work do the following:

C-x v = in a file for a diff

To submit:

C-x v d <TOP LEVEL DIR> to get a list of all modifed files, then mark everything (hold down m) and press C-x v v.

If you want to see diffs for individual files press v=.

[1] As an aside, one of the reasons I hadn’t found this out earlier was because I was under the false impression the mode help (C-h m) would show all relevant keybindings… I’m not quite sure why, C-h b is (strangely!) better for this purpose.

3 comments July 30, 2008

Searching multiple buffers emacs

In the past I’ve complained about emacs not being able to search multiple buffers for regular expressions, and I’ve resorted to using the additional package moccur.el. This was pretty good but had the irritating (though understandable) caveat that if you changed the text in one of the searced files then all the links from the output pane became slightly wrong.

However, I take it all back. I’ve just found that the standard FSF emacs has two functions which do what I want to do without these caveats. These allow one to search several buffers simultaneously for a given regexp and maintain links even if context is chaged… I’m not quite sure how I managed to miss this before – since the first thing that one would think to do if you wanted to search multiple strings for buffers would be to type “C-h a o-c-c-u-r-<ENTER>” and then search for multi… but I guess I’m just dim.

Anyway:

Precis for the impatient

In FSF emacs, you can search several buffers for a given string

multi-occur: – Search specified buffers to a string

multi-occur-in-matching-buffers: Allows one to search a subset of one’s open buffers or files specified as a regexp:

Given .* as its first argument this allows one to search all currently open files for a given regular expression.

Given a prefix argument and .* as its first argument this allows one to search all currently open buffers for a regexp.

Also (occur-1 regexp nil buffer-list) can be usefully adapted to search for other things.

2 comments July 27, 2008

Emacs buttons – an introduction for the somewhat impatient

Working out how to make buttons work in emacs took rather too much effort – I suspect I was just being slow. However, I thought I’d write a tiny how-to for anyone else who tries to do this:


Below is a prototypical emacs lisp function call to create a new button in your text between characters beg and end is:

(defun f (button)
    (call-interactively 'find-file))

(make-button beg end 'action 'f 'follow-link t)

Note the following:

  • The properties are defined via a variable number of arguments . It does does not take a list.
  • Removing the follow-link property would cause left mouse-clicks on the link to have no effects
  • f must take a single argument otherwise this will fail.

For more on emacs buttons see:

http://www.gnu.org/software/emacs/manual/html_mono/elisp.html#Buttons

If you want to make your button more anonymous (so that changing the function value associated with f has no deleterious results) you can use the following

(make-button beg end 'action
    (lambda (button) (call-interactively 'find-file))
    'follow-link t)

3 comments May 14, 2008

Sending mail through gmail Using Emacs

Very brief summary for the impatient
Ensure that the gnutls-bin program is installed (

apt-get install gnutls-bin

on ubuntu)

Paste the following into your init file changing username and password as appropriate.

(setq send-mail-function 'smtpmail-send-it)
(setq smtpmail-smtp-server "smtp.gmail.com")
(setq smtpmail-smtp-service 25)
(setq smtpmail-auth-credentials '(("smtp.gmail.com" 25 "USERNAME" "PASSWORD")))
(setq smtpmail-starttls-credentials '(("smtp.gmail.com" 25 nil nil)))

Evaluate this with “M-x eval-buffer”

Done

Longer explanation for the troubled / interested

Emacs can, if set up correctly, send mail through gmails smtp server.

First tell emacs to use smtp to send mail by evaluating:
(setq send-mail-function ’smtpmail-send-it)

(If you don’t do this email will attempt to use sendmail to send mail – on my machine this resulted in mails not being sent despite emacs reporting that they were).

You must then set smtpmail’s authentification details. First run

(setq smtpmail-debug-info t)
(setq smtpmail-debug-verb t)

so as to get as much output as possible.

Then run

(setq smtpmail-smtp-server "smtp.gmail.com")
(setq smtpmail-smtp-service 25)

to tell emacs to use gmail for outgoing mail.

There are (at least) two mechanisms that smtp servers can use to authenticate users.

One method has the user provide a user name and password in plain(ish) text in the smtp connection. This connection can also be encrypted. This encryption mechanism is known as STARTTLS. Gmail uses a plain text authentication method sent over an encrypted connection in this manner.

With the other method, the user passes a certificate to the server which asserts that the user is who they say they are. This method of authentication is, confusingly, also know as STARTTLS.

To set-up authentication within SMTP evaluate

(setq smtpmail-auth-credentials '(("smtp.gmail.com" 25 "USERNAME" "PASSWORD")))

To set up STARTTLS encryption of the connection call

(setq smtpmail-starttls-credentials '(("smtp.gmail.com" 25 nil nil)))

(This variable is also used to set up authentication using STARTTLS).

After you have done this you should be able to send mail from within emacs. To test this run hit
C-x C-m. Write a test message, and then hit C-c C-s, before toggling to a buffer with a name beginning with “*trace of SMTP.” If anything goes wrong this should tell you the approximate reason.

More details

There doesn’t seem to be a way to make emacs prompt for a password when sending mail (though one could probably be hacked-together with half an hours work – after the other 10 hours needed to learn emacs lisp).

You can avoid keeping a password in your initialization file by using a .netrc file.

smtpmail’s documentation can be found here .

If things start not working you might like to use the source – but be careful, some older versions of smtpmail don’t support starttls.

-

3 comments April 27, 2008

Emacs link

http://xahlee.org/emacs/emacs.html
seems to contain quite a lot of information about making emacs keybindings more ergonomic as well as a lot of general emacs tips.

Lots of general emacs tips:

http://steve.yegge.googlepages.com/effective-emacs

Add comment March 15, 2008

Emacs – select entire buffer macro

This is some emacs lisp code that makes -c a select the entire buffer. This is all terribly simple to do (if you spend a while looking at the documentation) – but I’d quite like to be able to cut and paste this without think. This has to pasted into your .emacs file to work.

(fset 'select-buffer
   [?\C-a ?\M-])
(global-set-key "^Ca" (quote select-buffer))

Of course, if you, unlike me, read documentation before deciding to impose your views upon people you might just use the mark-whole-buffer macro instead, which seems to be bound to C-x h by default.

1 comment March 15, 2008

XRefactory

I found something called XRefactory today – a set emacs macros for doing refactoring, and several other clever code related things. The only issue is:

  • The C++ version is proprietary and costs money.
  • The free version (for C and java) doesn’t appear to be open source.

But besides this, it looks quite fun.

Add comment February 12, 2008


Meta

Facets

abstruse Add new tag AOP apt aspect oriented programming assumes knowledge autiobiographical bash scripts bell books clarity code samples configuration console emacs for the benefit of google functional programming graphical design hacks higher-order functions howtos intention revealing programming keyboard links linux note to self opinions parsing patterns philosophising philosophizing programming python random ideas refactoring removing packages short stories succinct svn systems stuff theoretical philosophizing typing work ethic you probably don't want to read this

Archives

Pages

 

November 2009
M T W T F S S
« Aug    
 1
2345678
9101112131415
16171819202122
23242526272829
30