Software Escape Archives

MetaCPAN-on-search.cpan.org update

I’ve uploaded a new version of the GreaseMonkey script I introduced in my last entry (to let you use search.cpan.org for searching but with links to MetaCPAN in the search results).

This version is updated for GreaseMonkey 1.0, and also includes a small link at the top right of a search results page which links to the same search on MetaCPAN:

var query = document.querySelector('input[type=text]').value;
if (query) {
    document.querySelector('.t4 small').insertAdjacentHTML('beforeend',' <a id="goto-metacpan">&#x2192; MetaCPAN</a>');
    var link = document.getElementById('goto-metacpan');
    var href = 'https://metacpan.org/search?q=' + encodeURIComponent(query);
    link.href = href.replace(/%20/g,'+');
    link.style.setProperty('float','right');
    link.style.setProperty('padding-right','.4em');
    link.style.setProperty('color','inherit');
}

Easily installable from userscripts.org. Share and enjoy.

Marrying MetaCPAN to the search.cpan.org search engine

I generally like MetaCPAN better than search.cpan.org, but as fREW mentioned in Using search.cpan.org AND metacpan, the latter’s search result ranking algorithm is far superior to the former’s. So fREW’s idea was to use GreaseMonkey – or actually not GreaseMonkey – to rewrite the links in search.cpan.org search results so that he could use that site for searching, but go to MetaCPAN for everything else. In fact though, he used dotjs and took advantage of the built-in jQuery it ships with. Thus his script does not work with vanilla GreaseMonkey.

Here is a version that does. As a bonus, above and beyond fREW’s version, it also only rewrites the distribution and author links rather than just the module links.

// ==UserScript==
// @name        Marry MetaCPAN to the search.cpan.org search engine
// @namespace   http://plasmasturm.org/
// @include     http://search.cpan.org/search?*
// @version     1
// ==/UserScript==

var _rx = new RegExp('^/~([^/]+)(.*)');
var urx = new RegExp('^https://metacpan.org/module/([^/]+)/$');
var rrx = new RegExp('^https://metacpan.org/module/([^/]+/[^/]+)/$');
var anchors = Array.prototype.slice.call(document.querySelectorAll('a[href^="/~"]'));
anchors.forEach(function(anchor){
    var href = anchor.getAttribute('href');
    href = href.replace(_rx, function (m,p1,p2) { return 'https://metacpan.org/module/' + p1.toUpperCase() + p2 });
    href = href.replace(urx, 'https://metacpan.org/author/$1');
    href = href.replace(rrx, 'https://metacpan.org/release/$1');
    anchor.href = href;
});

And here it is ready to install on userscripts.org. Share and enjoy.

Bash perldoc completion tweaks

Remember when I wrote the following?

Completion in bash is hard-wired to understand trailing slashes as “the user might want to do more completion right after this” – we want :: treated that way instead but there is no way to tell bash.

This is still true, but reading the manpage a little less carelessly reveals that passing -o nospace to the complete command tells bash to simply never append a space, which achieves what is desired without hacks.

While I was in there, I found -o default, which means that if the completion generator returned no results, bash should use its default completion generator instead. This is very useful.

Previously, with “.” in your @INC (as it commonly is), all directories directly inside the current working directory would be included in the completions list as suggested namespaces, because perldoc-complete has no way of knowing which of them contain modules and which don’t. (It would have to recurse, which is too expensive.) But since there is a way to fall back to bash’s default filesystem completion, I have added an extra check that removes not only the home directory from @INC, but the current directory as well.

If you want to complete on documentation in modules somewhere below the current directory, simply type perldoc ./<Tab> etc to use standard path completion. Or perldoc /<Tab> to complete on an absolute path.

Share and enjoy.

Summary

  • No more ugly fake suggestions to force ambiguity
  • No more spurious namespace suggestions outside your home directory
  • You can now complete on paths as well as namespaces by starting the path with / or ./
  • Existing users will have to change their .bashrc line to
    complete -C perldoc-complete -o nospace -o default perldoc

More bash completion help for perldoc

Yesterday’s posting got a fair bit of response. Among other feedback, I had some feature requests from Offer Kaye in my email today.

So now perldoc-complete will complete built-in Perl functions if you’ve typed perldoc -f (try it with eg. perldoc -f ch<Tab>) and it will also complete Perl’s documentation pages – although for uncluttering’s sake, it will hide the list if you’ve merely typed perldoc <Tab>.

Share and enjoy.

A bash completion helper for perldoc

A month ago, Yanick Champoux wrote a note about helpers for browsing the POD in your your local perl install.

His first script is for firing up a browser pointed at a local POD web server, including starting one up if it’s not already running – not that useful to me, since I haven’t found myself actually using these servers very much, because of the console↔browser flipping that they entail. Plain old perldoc on a console just feels faster to juggle.

However, he also includes another script: a completion helper for bash. This allows you to type something like perldoc Cata<tab> and have bash turn it into perldoc Catalyst for you. I used this script for mere hours before I realised it’s exactly the one thing I have always missed in Perl: a way to quickly and efficiently browse my local module library – the thing that all the POD web servers promised to give me, but couldn’t deliver in a convenient enough fashion for me to use routinely.

But as presented, his script has one limitation that annoyed me more than perhaps I should have let it: you have to use your system directory separator (ie. slash on Unixoid systems) en lieu of Perl’s :: package separator. Ie.: you cannot type perldoc Catalyst::Re<tab>, it has to be perldoc Catalyst/Re<tab>.

I thought that should be easy to fix. (Famous last words, I know.) It turned out to be harder than expected due the fact that completion in bash is hard-wired to understand trailing slashes as “the user might want to do more completion right after this” – we want :: treated that way instead but there is no way to tell bash. I also ended up rewriting the script to be more (theoretically) portable, to not require non-core dependencies, and to work on much older perls than just 5.10.0, as Yanick’s code requires. Fastidious as I always am, I also spent quite a bit of effort rewriting the code to make it more beautiful and easy to skim and understand.

So without further ado: share and enjoy.

About Aristotle

user-pic Waxing philosophical