Edit Hacks - diff

This page discusses some ideas about using the Unix diff(1) command to assist blind programmers in working with white space. See the Editing Hacks page for motivation, etc.

Use Case

Let's say that a blind programmer is tasked with editing some source code. Conveniently, she is using a (mostly) spacing-insensitive language such as Ruby. Also, the local style guide forbids the use of tabs, whose effects on layout can be difficult for a blind person to predict. The following approach will get her well started:

  • Obtain the original file(s).
  • Minimize the white space.
  • Examine the minimized code.
  • Edit and test, as needed.

At this point, we have (putatively :-) working code. However, we still have the problem of incorporating corresponding edits into the original file(s).

Prototype

Here is a plausible (albeit tedious and error-prone) prototype of the required work flow. For simplicity, let's assume that only one file (foo.rb) needs to be edited.

The initial steps are very similar to the ones we took above, but we make a couple of reference copies along the way:

$ git    ... foo.rb ...      # Clone the original file.
$ cp     foo.rb o_foo.rb     # Save a reference copy.
$ chmod  -w o_foo.rb         # Make it read-only.

$ mws    foo.rb              # Minimize white space.
$ cp     foo.rb m_foo.rb     # Save a reference copy.
$ chmod  -w m_foo.rb         # Make it read-only.

$ emacs  o_foo.rb foo.rb     # Examine, edit, & test.

Now, we merge our edits with the original file, producing the proposed new version. Using diff(1), with appropriate options, helps us to characterize our editing results.

$ mv     foo.rb e_foo.rb     # Save for reference.
$ chmod  -w o_foo.rb         # Make it read-only.

$ diff   [me]_foo.rb > d_foo.rb  # List changes.
$ chmod  -w d_foo.rb         # Make it read-only.

$ emacs  [do]_foo.rb foo.rb  # Copy & test edits.
$ diff   o_foo.rb foo.rb     # List final changes.
$ git    ... foo.rb ...      # Commit the edits.

Minimization

The white space minimization command used above (mws) isn't part of Unix. In fact, language-sensitive minimization is a rather tricky problem. However, naive minimization can be performed using a regular expression, replacing each sequence of (two or more) spaces with a single space. In Ruby, this might look like:

line.gsub!(/  +/, ' ')      # Minimize a line.

Production

A production version of this approach could mechanize assorted tasks, making the process faster, more convenient, safer, etc. Most obviously, it could handle assorted low level details (e.g., command syntax, file naming). It should also be able to assist with the copying of edits into a normally formatted version of the file.

It seems likely that this mechanization could be added to Emacs, in the form of Elisp extensions. With a bit of care, most of the extra steps would take place "automagically".


This wiki page is maintained by Rich Morin, an independent consultant specializing in software design, development, and documentation. Please feel free to email comments, inquiries, suggestions, etc!

Topic revision: r11 - 23 Jun 2016, RichMorin
This site is powered by Foswiki Copyright © by the contributing authors. All material on this wiki is the property of the contributing authors.
Foswiki version v2.1.6, Release Foswiki-2.1.6, Plugin API version 2.4
Ideas, requests, problems regarding CFCL Wiki? Send us email