Combining Find and Grep in Emacs

One of the classic Unix idioms is to search all files in a file hierarchy for some regular expression with a find/grep pipeline. For example, to search the lisp directory and all of its subdirectories for file containing the mapcar function, one could use

find ~/lisp -name "*.lisp" -exec grep -H mapcar {} \;

That pipeline finds every .lisp file in the hierarchy and runs grep on it looking for mapcar.

I, at least, don’t do this type of thing very much anymore because there are so many easier ways of doing so, especially from within Emacs.

Nonetheless, it’s still possible to usefully combine find and grep from within Emacs to build a Dired buffer of all the files that contain the matching regex. This tweet by
Mickey tells you how:

There’s a bit more to the command so you should take a look at the on-line documentation if you have a use for it.

Posted in General | Tagged | Leave a comment

Adding To a Keyboard Macro

Wilfred Hughes’ Emacs Command of the Day Twitter feed has a useful tip:

I’ve seen this before and may even have written about it before but I always forget about it. I’m not sure why; it could hardly be simpler: just prefix the start macro command with the universal argument.

In any event, it’s a useful reminder for me and may be for you too.

Posted in General | Tagged | Leave a comment

MELPA Surpasses a Quarter Billion Downloads

From Steve Purcell:

I’ve said this before but it bears repeating: We all owe Purcell our thanks and, if we happen to meet him, a beer. He has, virtually single-handedly, run the most important Emacs package repository. It’s a lot of work that Purcell donates to the our community and more often than not his only feedback is complaints when the rare glitch isn’t resolved quickly enough.

A quarter of a billion downloads is a lot for a community the size of ours and underscores again the tremendous amount of work required to keep things ticking along. There are currently 5,077 packages in MELPA and each one has been curated and validated by Purcell or one of his helpers.

So again, let’s all raise to toast to Purcell and if you happen to be where he is, buy him a round.

Posted in General | Tagged | Leave a comment

Red Meat Friday: SICP in JavaScript

Remember a couple of weeks ago when I declared No. Just no? Well, here’s another, even more emphatic, refusal to accept the apocalypse.

No. Just HELL no!

The thing about SICP is how simple and beautiful the code is and how precisely it demonstrates the concepts under discussion. To pretend that you could do the same with JavaScript is self delusion of the first order.

Yeah yeah, I know. There’s more code written in JavaScript than there are atoms in the universe. There are also more Ford Focuses than Rolls Royces. That doesn’t make Focus the better car or JavaScript the better language. More to the point, JavaScript is a bad fit for SICP.

I have long abhorred MIT’s decision to replace SICP with Lego robots or something but this latest outrage is an act of vandalism. If you want to learn the wisdom in SICP then learn Scheme. The whiners notwithstanding, it’s an easy language to learn. Indeed, 6.001, the MIT course in which SICP was used, didn’t have any time devoted to teaching Scheme. Students just picked it up as they went along.

If we’re going to abandon SICP as a teaching vehicle, we should at least let it rest in peace and not desecrate its corpse.

Posted in General | Tagged | Leave a comment

Automating Your Job

One of my favorite bits of—obviously apocryphal—Lisp folklore is the story of the invention of macros. The TL;DR is a that some senior Lisp developers got tired of writing the same boilerplate code all the time so that started using abbreviations for the pieces of boilerplate and hired a junior developer to translate the abbreviations into normal Lisp. After a while, according to the story, the junior developer realized he could just write some code to do the translations automatically so he added that code to the Lisp compiler and Lisp Macros were born. It worked so well that he soon saw there was no reason for him to oversee things so he retired to the beach while still getting paid. The senior developers just assumed he was still there slaving away at the translations.

I’ve always loved that story so this real life analog really resonated. It’s an apparently true story of an IT specialist working for a law firm who was tasked with handling digital evidence for the firm. That’s probably more involved than you most of you realize but our hero soon came to realize it was not an 8-hour job. Even more, he discovered that he could automate the whole process with some simple scripts,

So when COVID struck and he started working from home, he simply used his scripts to do the work and spent his time playing computer games and hacking on his side projects. There may be some ethical issues here but there may also not be any. After all, the job he was hired to do is getting done and the firm seems happy.

Regardless, I just love the real life example of the Lisp Macro story. I always considered it a delightful fantasy but it has, it seems, come to life.

Posted in General | Tagged | Leave a comment

Corner Quotes

John D. Cook has a useful post on using “corner quotes” (⌜ ⌟) to delimit regular expressions in text. As Cook says, it makes a lot of sense especially when the regular expression begins or ends with a space. Cook’s port deals with using Unicode for the corner quotes but it’s also easy to do in Emacs.

In vanilla Emacs, the way to do it is to specify TOP LEFT CORNER and BOTTOM RIGHT CORNER with insert-char through the Ctrl+x 8 binding. If you have Ivy installed, you can also use the lceil and rfloor entities through ivy-insert-org-entity a handy function that I stole from John Kitchin’s Scimax.

(defun ivy-insert-org-entity ()
  "Insert an org-entity using ivy."
  (interactive)
  (ivy-read "Entity: " (loop for element in (append org-entities org-entities-user)
                             when (not (stringp element))
                             collect
                             (cons 
                              (format "%10s | %s | %s | %s"
                                      (car element) ;name
                                      (nth 1 element) ; latex
                                      (nth 3 element) ; html
                                      (nth 6 element)) ;utf-8
                              element))
            :require-match t
            :action '(1
                      ("u" (lambda (element) (insert (nth 6 (cdr element)))) "utf-8")
                      ("o" (lambda (element) (insert "\\" (cadr element))) "org-entity")
                      ("l" (lambda (element) (insert (nth 1 (cdr element)))) "latex")
                      ("h" (lambda (element) (insert (nth 3 (cdr element)))) "html"))))

The latter method uses left and right floor and ceiling entities (⌈ ⌋) rather than the corner entities but the results are almost the same. At least they are if the regular expression doesn’t contain a floor or ceiling operator.

I usually just set the regular expression in a constant width font but as Cook notes that doesn’t work as well when the regex begins or ends with a space. This seems to me to be a good convention that we should consider adopting.

Posted in General | Tagged , | Leave a comment

The UK Plans an Anti-encryption Campaign

Rolling Stone has an article on a planned UK Campaign against end-to-end encryption. Of course, after the University of Virginia rape story debacle, everything they report should be viewed, um, critically. That said, the story is disturbing.

The Home Office has reportedly hired the M&C Saatchi advertising agency to concoct what can only be termed a disingenuous advertising campaign to discredit end-to-end encryption in general and FaceBook’s plan to implement it in WhatsApp—its messaging app—in particular.

You can read the article to discover some of the stunts they have planned but the whole campaign is based on the usual Four Horsemen of the Infocalypse and especially how that nasty encryption makes it impossible to “protect the children”™.

As always in this type of action, the Home Office is pretending that it’s possible to provide a backdoor key that only the good guys can use. Virtually every cryptography expert says that’s impossible and there’s nothing in the government’s past record in protecting these types of secrets that suggests it will be any different with a backdoor key. One former government security executive described it as “the digital equivalent of alchemy”.

All governments hate end-to-end encrypted messaging apps because it allows citizens to keep their private business private and how can the nannies do their job if they can’t snoop on what you’re saying? They’ll bring up “think of the children”, drugs, terrorism and all the other boogeymen as if encryption were somehow unique in its use by criminals. I hope my cousins in the UK tell them to get lost.

Posted in General | Tagged | Leave a comment

Users Sue PayPal

I love the idea of PayPal: a neutral broker that holds funds involved in online purchases until both sides have fulfilled their obligations. The buyer sends the funds for a purchase to PayPal, which tells the seller it has the money and holds it until the buyer confirms it has received the merchandise. The only wrinkle should be when there are disputes about the merchandise. In that case, PayPal adjudicates the dispute.

That’s the theory and as far as I know, that’s the way it worked in the beginning. But like many Internet sites, PayPal came to believe that they should police their users’ activities. That’s not completely unreasonable. After all, no one wants to be serving as a middle man for, say, drug sales or sex trafficking. But also like many sites, PayPal also prohibited actions that they didn’t like even though they might not be illegal.

Even that would probably have been okay if they simply refused to deal with those people and terminated their accounts for clear violations of a well defined and published terms of service. That’s where the problems begin. PayPal has apparently terminated some accounts, refused to say why, and seized the users’ funds. The amounts are not trivial: in one case it was $172,000.

Some users are fed up and are fighting back. They are suing PayPal and trying to get the suit certified as a class action. If the allegations are true, I hope the suits succeed and that criminal charges are filed. After all, how are these actions not stealing customers’ funds?

One could maintain that even terminating the accounts for legal activities that PayPal doesn’t like should be prohibited. Yes, PayPal is a private corporation but they are also, arguably, a public utility with a duty to serve anyone not breaking the law.

Posted in General | Tagged | Leave a comment

Exporting Variables From Org-mode to LaTeX

Franco Pasut has posted on exporting variables from Org to LaTeX. The TL;DR is that you can define LaTeX macros defining a variable value in #+LATEX_HEADER: entries at the top of the Org file and then interpolate the variables in the text by including \variable_name{} in the body of the document. That’s nice because it makes it easy to change the value of those variables, if needed, in one place and have the results reflected in the whole document.

The problem with this method is that it’s not portable. It’s great if you’re only exporting to PDF but if you want to also export to HTML, say, it won’t work. The proper way of handling the general case is to define the variables at the Org level as described by Mike Hamrick in his excellent video Consistent Technical Documents Using Emacs and Org Mode that I wrote about wrote about previously.

In either case, the idea of defining variables in one place and using their values throughout the document makes a lot sense precisely because it makes it easy to change those values when needed without scanning the entire document. Take a look Hamrick’s video to see how powerful the method can be.

Posted in General | Tagged , | Leave a comment

Source Block Executors

Isa Mert Gurbuz (isamert) has a very interesting post on using Org Babel to interactively deal with Web APIs. Starting with Zweifisch’s ob-http, he queries a site and gets some JSON returned. The ob-http code even allows you to pipe the results through the jq binary to slice and filter the JSON results.

That’s pretty neat but what’s better is he shows us how to make the JSON results “executable”. It’s probably not clear what it means to “execute” JSON but the idea is simple: use can specify source block header parameters to specify filtering or other manipulation. The code to do this is simple and isamert gives an example of such code.
There are several examples in his post so take a look to see how it all works.

But wait! There’s more. JSON is an existing Emacs major mode but it’s possible to extend the idea to custom modes as well. The example isamert uses is for the Couchbase database. There’s no mode for Couchbase, so isamert created one by simply extending sql-mode. Then he wrote a function to take an SQL statement and send it to the Couchbase server. Again, that’s simple because Couchbase provides a command line tool. Finally, he wrote an executor for the Couchbase source block thereby providing an interactive Org-mode based tool to manipulate the database. Here’s an example of the its use (n1ql is the name of the mode):

#+begin_src n1ql :host DB_HOST :username DB_USERNAME :password DB_PASSWORD
  SELECT * FROM SomeTable LIMIT 10;
#+end_src

When he executes this block, the database is queried with the SELECT statement and the results are returned in a #+RESULTS: block as usual.

This is good stuff and has application outside of Web APIs so it’s well worth spending some time to read and understand isamert’s post even if you don’t routinely deal with Web APIs.

Posted in General | Tagged , | Leave a comment