Skip to main content

C'est la Z

Learning Elisp - Rot13 conclusion

Two more videos to finish up the rot13 project.

The first video covers about prefix arguments. The idea is when calling an Emacs function interactively, you can specify a numeric argument either by typing C-u # where # is a single digit or M-x ## where ## is an integer which can be positive, negative, single or multiple digits. You then type the key for your command or invoke it with its full name M-x. This passes the numeric parameter to the function.

In the function, we add a parameter to the (interactive) form - specifically (interactive "P") which says that the parameter to our function will be that number we passed as a prefix argument or nil if there was no prefix argument.

We use the prefix argument to determine how much to rotate our text. If we don't pass a parameter, we just do a rot13 but if we do, we rotate that amount.

The second video shows how we can use see if there's an active region and if so, run our rotate function on the text in that region. If not, rotate the previous word.

Here are the key Emacs features we instroduce.

FeatureDescription
mark-activea built in variable which is true if you've set the mark
(mark)returns an integer representation of where the mark is set
(point)returns an integer representation of where the cursor is
(buffer-substring-no-properties)returns text from the buffer

Notes from previous videos:

A viewer pointed out that the Emacs if statement can actually have multiple forms to its false part.

For example:

    (if true
        (print "Hello")
      (print "Goodbye")
      )

will print "Hello" if condition is true and "Goodbye" if it's fales.

Now, if we have multiple forms int he false part:

  (if conditiotrue
      (print "Hello")
    (print "Goodbye")
    (print "World")
    )

then while it will still print "Hello" if condition is true, it will print both "Goodbye" and "World" if it is false.

I didn't know that and appreciated the comment so that I could learn something new.

Here's the code and video links.

Code:

Once again, the code is up online:

The rot13 code will be in the file rot13.el.

The videos:

Prefix Arguments:

Video link: https://www.youtube.com/watch?v=5oHpxh0JsBE

Rotating a region:

Video link: https://www.youtube.com/watch?v=YzQ8iG3nZQ8

comments powered by Disqus