Emacs Learning

Docstring

I think you shouldn’t leave a blank line between the first line of the docstring and the next. I wrote all my elisp functions like that for the last four years :P

Shell

Dired

Q on marked buffers seems to crash Emacs at the end. It has happened five times now (in my website directory).

Emacs Lisp Hacking

Check out testcover or edebug for test coverage.

Dired

wdired: When doing query-replace-regexp on the file names, you have to be careful not to match anything other than the filename. Otherwise, you match some of the permission data or the owner data, which are read only. And since query-replace-skip-read-only is on, query-replace-regexp skips those matches.

Dot Emacs

“Never use load or require” - Effective .emacs. He recommends using autoload and eval-after-load instead.

Magit

Almost all the actions you can carry out in Magit is done through a one-key system that opens up a “command console” window at the bottom, letting you further refine your command before you carry it out. This is a fantastically well-done interface and is probably the killer feature that makes Magit so great. I like it so much that I have copied the underlying interface code for use in some of my own Emacs hackery. It’s really, really nice.

Mastering Emacs

Annoying Reverting of Buffers with Uncommitted Changes

[2018-12-31 Mon] When I edit and save some file and then commit some other file in the repository (using VC Git or Magit), then when I go back to the first buffer, it asks me to revert the buffer because it seems to have changed on disk.

It happens even when the first buffer has disabled magit-auto-revert-mode.

Helm

Looks awesome! I had my reservations, but it’s blowing my mind with the consistent interface to my files, buffers, bookmarks, imenu items, man entries, etc.

Haskell

Get ghc-mode.

Also other cool haskell stuff.

Structured Haskell Mode is pretty sweet. It lets you do paredit-like edits, such as going from

hGetContents =<< openFile [cursor]"dictionary.txt" ReadMode

to

"dictionary.txt"

Scala

Ensime

C-c C-b t runs all tests. But I can do that on every save using ~test or ~testQuick in sbt. Why is this better? Even C-c C-b q, which runs just changed tests, takes around 2 seconds, which is the same as for ~testQuick in sbt. Maybe it’s easier on your CPU.

Found that if I ran ~testQuick in the *sbt* buffer, I couldn’t exit from it. Weird.

What else does Ensime do? It adds semantic highlighting (which I don’t really need), gives you code completion (which might be useful), and some refactoring support (which I will again probably not use). I don’t know if it will be worth it. Let’s see.

Screen Lag in Large Markdown Buffers

The screen definitely lags when I type in a Markdown buffer with ~24k words, but not when I narrow the buffer to just one small section of 34 words.

Could be because I have large paragraphs (which look to Emacs like large lines). Or it could just be the size of the buffer.

(I think a similar effect occurs even in smaller Markdown buffers.)

Markdown + forward-paragraph + leading spaces = Hang

Experiment: Took a new markdown buffer. Wrote “foo”.

Observation: The word count indicator in the mode line was absent when I pressed C-g to stop Emacs hanging.

Hypothesis: Could it be because of wc-mode?

Experiment: You bet!

wc-mode calls count-words-in-paragraph. Is it because of that function? Yup.

It hangs when I say (count-words-in-paragraph) near the line with leading spaces.

It’s mark-paragraph that hangs!

Digging further: it’s forward-paragraph that hangs!

All this because of one observation - the word count stat in the mode-line was missing!

Experiment: The above doesn’t happen with a text-mode buffer with wc-mode turned on.

So, the problem is with forward-paragraph in Markdown.

Experiment: A long sequence of spaces anywhere is a problem.

Ispell

Set your personal dictionary file so that your words are saved.

(setq ispell-personal-dictionary "~/.ispell_pradeep_laptop")

Upgrading from Emacs 24.3 to 25.3

What still doesn’t work:

log-edit-done - doesn’t show diff and fails when I try to commit.

log-edit-show-diff: Diff functionality has not been setup

Wrong type argument: commandp, nil

This is because of the following code in log-edit.el.gz:

(defvar log-edit-callback nil)
(defvar log-edit-diff-function nil)

I’m guessing that Git backend support has somehow not been set up.

[2018-01-04 Thu] Haven’t had any such problems since then. Don’t know why.

Writing your own Elisp Packages

I wrote org-autoclock.el. It’s not yet a package that can be installed via the package manager, but it is a working standalone file.

But I failed to require org and s and f, which the code needed and which worked in my setup because I had already required them. So, naturally, when my friend tried to use org-autoclock, it blew up. Bad.

Lesson: Run your proposed elisp code in an isolated Emacs instance so that you see which dependencies you need.

emacs -Q -l org-autoclock.el

Observation: When Emacs doesn’t know the definition of some function, it just skips it or something.

Opening Files as Root

To manipulate a file (say /etc/apt/sources.list) with sudo permissions:

# ido will provide completion.
C-x C-f RET /sudo:root@localhost:/etc/apt/sources.list/

Calc

Recompute an Expression for Different Values

To calculate the compound interest formula a * (1 + r / 100)^t for different values of r and t and a, do

'a * (1 + r/100)^t =>

The => tells Calc that you want it to recompute that expression every time you set a value to one of its variables.

So, you can do

ss a = 3336
ss t = 10
ss r = 10

to see

1:  a*(r / 100 + 1)^t => 8652.72484689

Change the rate r

ss r = 20

to see

1:  a*(r / 100 + 1)^t => 20655.6327051

Financial functions

h/t: Andrew Hyatt’s post at https://plus.google.com/+AndrewHyatt/posts/YdwsbNmZ4NL

The examples are from the excellent Emacs Calc manual. And, yes, they were right - I suddenly got a better grasp of present value after reading the manual pages (8.6.3 Present Value)

If you start out with a lump sum of $6479 and get 9% interest for 4 years, you would end up with a final amount of $9146. So, the “future value of a lump sum” of $6479 at 9% after 4 years is $9146.

fvl(9%, 4, 6479.44) => 9146.2583471

If get $2000 each year from an investment (and then put it in a bank at 9%) for 4 years, you would have to start out with $6479 to get what that would earn in 4 years from something earning 9% interest. Put differently, the “present value of benefits” of $2000 each year for 4 years at an interest rate of 9% is $6479.

pv(9%, 4, 2000) => 6479.44

If you pay $2000 each year for 4 years and get 9% interest, then your final amount would be $9146. This is probably “future value of installments”.

fv(9%, 4, 2000) => 9146.258

If you take a loan of $6479 right now, you would have to pay $2000 each year so that the lender would get that much at the end of 4 years by saving it from the beginning at 9%. Basically, the present value of your periodic payments is $6479. Use pmt to calculate the periodic payments.

pmt(9%, 4, 6479.44) => 2000.0000759

Number of periodic payments needed to make the present value of $2000 per year equal $6479: use nper.

nper(9%, 2000, 6479) =>  3.99967586385

Rate at which you would have got a present value of $6479 from payments of $2000 for 4 years: use rate.

rate(4, 2000, 6479) =>  0.0900309222001 (9%)

Rate at which you would get $9146 from a lump sum $6479 after 4 years: use ratel.

ratel(4, 9146, 6479) =>  0.0900108081676 (9%)

Net present value of a series of irregular investments when you could have invested them at 9%: use npv. (Here I’m copying the example for 4 equal payments of $2000.)

npv(9%, [2000, 2000, 2000, 2000]) =>  6479.43975411

Rate of interest that would have got you the same final amount as a bunch of irregular payments: use irr.

[ERROR - Don't know why.]
Unable to bracket root: (1e-4 .. 199261560.80331)

http://nullprogram.com/blog/2009/06/23/ - Walks you through useful Calc commands - data formats like hex, complex numbers, HMS; matrix division; plotting graphs with a few keystrokes!; unit conversion.

https://truongnghiem.wordpress.com/common-calculations-in-emacscalc/ - showed me how to automatically recompute an expression by setting different values to a variable.

Created: August 6, 2015
Last modified: September 28, 2019
Status: in-progress notes
Tags: notes, emacs

comments powered by Disqus