Random Emacs Functions

  • user warning: Table 'nolan.comments' doesn't exist query: SELECT COUNT(*) FROM comments WHERE nid = 208 AND status = 0 in /home/sneakin/web/nolan.eakins.net/includes/database.mysql.inc on line 120.
  • user warning: Table 'nolan.comments' doesn't exist query: SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.picture, u.data, c.score, c.users, c.thread, c.status FROM comments c INNER JOIN users u ON c.uid = u.uid WHERE c.nid = 208 AND c.status = 0 GROUP BY c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, u.picture, c.homepage, u.uid, u.name, u.picture, u.data, c.score, c.users, c.thread, c.status ORDER BY c.thread DESC LIMIT 0, 50 in /home/sneakin/web/nolan.eakins.net/includes/database.mysql.inc on line 120.

I created some functions for Emacs because I really didn't want to do this stuff by hand. I have here four functions: count-occurences, line-empty?, unfill-region, unfill-buffer. You can either read the rest of this post, or click here to get the file.

Here's the code for each:

count-occurences

(defun count-occurences (string)
  "Counts the number of occurences of STRING in the buffer."
  (interactive (list (read-string "Count: ")))
  (let ((point (point))
	(counter 0))
    (beginning-of-buffer)
    (loop with pos = t
	  until (not (search-forward string (point-max) t))
	  do (incf counter))
    (goto-char point)
    (message (format "%d occurences" counter))
    counter))

line-empty?

(defun line-empty? ()
  "Returns true if the current line is empty."
  (eq (line-beginning-position) (line-end-position)))

unfill-region

(defun unfill-region (min max)
  "Joins the lines in the paragraphs inside a
region defined by MIN and MAX. If this is called
interactively then the mark and point are used to
define the region."
  (interactive (list (mark)
		     (point)))
  (if (> min max)
      (let ((temp min))
	(setq min max)
	(setq max temp)))
  (goto-char min)
  (message (format "%d %d" min max))
  (let ((empty nil))
    (message "Unfilling...")
    (loop with end = min
	  always (< end max)
	  do
	  (setq end (line-end-position))
	  (setq empty (line-empty?))
	  (next-line 1)
	  (if (not empty)
	      (if (not (line-empty?))
		  (join-line))))
    (message "Unfilled")))

unfill-buffer

(defun unfill-buffer ()
  "Joins the lines of all the paragraphs of a buffer."
  (interactive)
  (let ((point (point)))
    (unfill-region (point-min) (point-max))
    (goto-char point)))

Ad's by Google