Skip to content

Instantly share code, notes, and snippets.

@dotemacs
Created February 21, 2010 12:53
Show Gist options
  • Save dotemacs/310299 to your computer and use it in GitHub Desktop.
Save dotemacs/310299 to your computer and use it in GitHub Desktop.
;; see this gist first http://gist.github.com/285326
(defun sumup()
"Work out the sum for a cua-mode selected column/rectangle
and add it to the kill ring, so that it can be pasted in.
Improves on: http://www.emacswiki.org/emacs/AddNumbers as it
works on the more precise, visually appealing & obvious
cua-rectangle selected area, instead of the 'plain' rectangle
select."
(interactive)
(save-excursion
(let ((sum 0)
(sumup "*sumup*"))
(cua-copy-rectangle-as-text)
(set-buffer (get-buffer-create sumup))
(erase-buffer)
(yank)
(goto-char (point-min))
(while (re-search-forward "[0-9]*\\.?[0-9]+" nil t)
(setq sum (+ sum (string-to-number (match-string 0)))))
(message "Sum: %f" sum)
(kill-new (number-to-string sum))
(kill-buffer sumup))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment