Emacs Utility Functions

I’ve ended up migrating a few of my old functions back to my new setup, as well as creating a few newer ones. These are the ones I’ve added recently - maybe they’ll be useful to you, as well.

I’ve been doing a few book reviews recently, and instead of having to manually add in links I just wrote a function to generate a link based on the ASIN I give it. If you end up using something like this, you’ll probably want to change it so that it links to your Amazon Associate’s account, although I won’t complain if you don’t.

(defun insert-amzn-product (asin)
  "Inserts a link(image + text to an Amazon product using the ASIN"
  (interactive "MAsin:")
  (insert
   (concat "<iframe src=\"http://rcm.amazon.com/e/cm?t=randmusiofaso-20"
           "&o=1&p=8&l=as1&asins="
           (replace-regexp-in-string "\\n" "" asin)
           "&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&"
           "bc1=000000&bg1=FFFFFF&f=ifr\" style=\"width:120px;height:240px;\""
           "scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\""
           "frameborder=\"0\"></iframe>")))

I like to be able to know how many words are in a buffer, so I found the following function somewhere online. It just runs ‘wc -w’ as a shell command on the contents of the current buffer, which outputs the number of words.

(defun wc nil
  "Count words in buffer"
  (interactive)
  (shell-command-on-region (point-min) (point-max) "wc -w"))

I sometimes use different editors on the same files, and end up having broken indentation a lot of the time this happen. I wrote iwb (for indent-whole-buffer) to fix indentation problems.

(defun iwb ()
  "Indents the entire buffer"
  (interactive)
  (indent-region (point-min) (point-max) nil))

This function I picked up from Steve Yegge’s blog. It prompts for a directory and then moves the buffer you are editing and the associated file to that directory

(defun move-buffer-file (dir)
  "Moves both current buffer and file it's visiting to DIR."
  (interactive "DNew directory: ")
  (let* ((name (buffer-name))
	 (filename (buffer-file-name))
	 (dir
	  (if (string-match dir "\\(?:/\\|\\\\)$")
	      (substring dir 0 -1) dir))
	 (newname (concat dir "/" name)))
    (if (not filename)
	(message "Buffer '%s' is not visiting a file!" name)
      (progn
	(copy-file filename newname 1)
	(delete-file filename)
	(set-visited-file-name newname)
	(set-buffer-modified-p nil) 	t))))

Tags: ,

6 Responses to “Emacs Utility Functions”

  1. Oz says:

    For counting words or other text elements there is also “M-x count-matches” command. For example, give it a regexp like \w+ or \S-+ and it will return the number of matches.

  2. perusio says:

    The progn in ‘move-buffer-file’ is spurious. There’s an implicity progn in the else clause of an ‘if ‘ special form.

    You can also replace the ‘if’ in the let* that binds dir by ‘when’.

    Suggestions:
    i) Put a link that using JS can display a markup help when entering comments.
    ii) Put a preview button.

  3. Thanks, I was just thinking that I was going to write that Amazon function. I’d use format though…

  4. aaron says:

    M-x how-many RET \w+ RET

    C-x h C-M-\

    M-x rename-file RET RET RET

    I don’t have an equivalent for the Amazon.com command, but you may want to look at Skeleton mode or Abbrev mode to accomplish those kinds of things.

  5. Paul Rodriguez says:

    I find move-buffer-file more useful with Ido, which is a relatively straightforward conversion.

    (defun ido-move-buffer-file ()
      (interactive)
      (let* ((name (buffer-name))
    	 (filename (buffer-file-name))
    	 (dir (ido-read-directory-name "New directory: "))
    	 (newname (concat dir name)))
        (if (not filename)
    	(message "Buffer %s is not visiting a file." name)
          (progn
    	(copy-file filename newname 1)
    	(delete-file filename)
    	(set-visited-file-name newname)
    	(set-buffer-modified-p nil)
    	t))))
    
  6. aaron says:

    WordPress’s mindless HTML stripping system messed my comment up. This should work.

    M-x rename-file RET RET /path/to/dir RET

Leave a Reply