{"id":123,"date":"2009-02-06T11:25:30","date_gmt":"2009-02-06T16:25:30","guid":{"rendered":"http:\/\/hoolihan.net\/blog-tim\/?p=123"},"modified":"2009-06-29T12:08:33","modified_gmt":"2009-06-29T17:08:33","slug":"wc-like-unix-command-in-emacs-lisp","status":"publish","type":"post","link":"http:\/\/hoolihan.net\/blog-tim\/2009\/02\/06\/wc-like-unix-command-in-emacs-lisp\/","title":{"rendered":"&#8220;wc&#8221;-like Unix Command in Emacs Lisp"},"content":{"rendered":"<p>Inspired by reading <a href=\"http:\/\/steve-yegge.blogspot.com\/2007\/02\/my-save-excursion.html\">a Steve Yegge blog post<\/a>.<\/p>\n<pre>\r\n(require 'cl)\r\n\r\n(defsubst wc-whitespace-char? ()\r\n  \"is current character whitespace?\"\r\n  (not (looking-at \"[A-Za-z0-9]\")))\r\n\r\n(defun wc-buffer (b)\r\n  \"count words in a buffer\"\r\n  (interactive \"bBuffer to Analyze \")\r\n  (let ((word-count 0) (in-word nil) \r\n        (char-count 0) (line-count 1))\r\n    (save-excursion\r\n      (set-buffer b)\r\n      (beginning-of-buffer)\r\n      (while (not (eobp))\r\n        (if in-word\r\n            (if (wc-whitespace-char?)\r\n                (setq in-word nil))\r\n          (if (not (wc-whitespace-char?))\r\n              (progn\r\n                (setq in-word t)\r\n                (incf word-count))))\r\n        (incf char-count)\r\n        (if (looking-at \"[\\r\\n]\") (incf line-count))\r\n        (forward-char 1))\r\n      (message \"%d chars %d words %d lines\" \r\n               char-count word-count line-count))))\r\n\r\n(defun wc ()\r\n  \"count words in current buffer\"\r\n  (interactive)\r\n  (wc-buffer (buffer-name)))\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Inspired by reading a Steve Yegge blog post. (require &#8216;cl) (defsubst wc-whitespace-char? () &#8220;is current character whitespace?&#8221; (not (looking-at &#8220;[A-Za-z0-9]&#8221;))) (defun wc-buffer (b) &#8220;count words in a buffer&#8221; (interactive &#8220;bBuffer to Analyze &#8220;) (let ((word-count 0) (in-word nil) (char-count 0) (line-count 1)) (save-excursion (set-buffer b) (beginning-of-buffer) (while (not (eobp)) (if in-word (if (wc-whitespace-char?) (setq in-word [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59,18],"tags":[22,69],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-lisp","category-programming","tag-emacs","tag-linkedin"],"_links":{"self":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":0,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"wp:attachment":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}