The
search-forward-regexp
,replace-match
, andmatch-string
functions work together nicely, and makes my job much easier and enjoyable!
I am writing a release notes for the a software updates. Part of the process is to associate the SVN Revision number that relates to important changes, so that others can backtrack and review the code and see what exactly has been implemented.
In Phabricator, the revision number will be render automatically. Clicking them takes me to the exact revision, showing the difference with previous version. But the documentation will be eventually built by Sphinx and hosted on a remote server. So I have to manually add the URL to all the SVN revision number. For example, to replace rS1234 to
[[http://phabricator.domain.co.uk/rS1234][rS1234]]
There are 31 revision number in the whole document. I could do it manually but for the long term benefits, it would be more efficient write a function to process it automatically, maybe others can use it as well.
The first thing I noticed is each SVN revision numbers consist of two letters (rS) and few digits. Because the four digits I don't know beforehand, I have to use regular expression to do the pattern search.
The tricky bit here is to retrieve the values that matched the pattern, because of it is needed to construct the URL that points to the commits, and I also need to replace the it with differnet values.
The procedure can be summarised as:
search-forward-regexp()
to search the pattern "rS[0-9]+", which
means a string that starts with rS with one or more digits.amatch-string()
.replace-match()
, and I use concat()
to combine the IP address with the
revision number.The following is a workable implementation:
Note the last two lines of the function can be simplified as
You can easily adopt the code and make it applicable to your case,
just modify the revision-pattern
and repo-url
variables. But
beware that you should not apply the function to the same buffer more than
once, otherwise you will get something crazy like this:
[[http://10.0.0.11/[[http://10.0.0.11/rS1234][rS1234]]][[[http://10.0.0.11/rS1234][rS1234]]]]
One way to make it better is to have a test before replacing: if the revision number is already associated with a URL, then do nothing. If you have figure out how to do it, please let me know and I've happy to update this post.
My posts published last year showed my frustration with regular expression in Emacs. But now I am looking forward doing more text processing with it, because it will be fun!
The search-forward-regexp
, replace-match
, and match-string
functions work together nicely and make the my job much easier and
enjoyable!
What's your favourite functions in regular expression? Do you have something to recommend?