A couple of counting functions

I had a strict character limit of 180 chars for something I was writing. I just wrote a simple function to count characters in a region or a buffer. Another function to count the words.

(defun count-chars ()
  "Count the number of chars in a buffer or region."
  (interactive)
  (let* ((beg (if (region-active-p) (region-beginning) (point-min)))
	 (end (if (region-active-p) (region-end) (point-max))))
	 (message (number-to-string (- end beg)))))
(defun count-words ()
  "Count the number of words in a buffer or region."
  (interactive)
  (let* ((beg (if (region-active-p) (region-beginning) (point-min)))
	 (end (if (region-active-p) (region-end) (point-max)))
	 (count 0))
    (save-excursion
      (goto-char beg)
      (while (< (point) end)
	(forward-word)
	(setq count (1+ count))))
    (message (int-to-string count))))

org/elisp - sorting list of org-links on description

A small utility function that I used to sort the names of people who commented on my blog.

(defun org-get-link-desc-from-list ()
  """ Get link description of a list item containing just links """
  (let* ((item-beg (point))
       (item-end (org-end-of-item))
       (cur-item (buffer-substring-no-properties
		  item-beg item-end)))
    (goto-char item-beg)
    (org-columns-compact-links cur-item)))

sort-lines wasn't good enough for me, since it was sorting alphabetically and it ended up being the sorted order of the urls and not the names. This function, when used with org-sort-entries-or-items gave me what I wanted.

Thanks to benny (on #org-mode), I also learnt the difference between let and let*.

Thanks Sacha

I just wanted to thank Sacha Chua for bearing with my newbie elisp code and trying out org2blog.

Also, thanks for the two patches she sent.

  1. Feature addition - Posting sub-trees instead of whole buffers. I had this feature in mind, and had started working on it after her request, but she was too quick for me! ;)
  2. Cleaning up of the code that uploads images.

I'm loving it!

Refile to date-tree

Useful to refile notes to the journal file, which is a date-tree. org-refile isn't convenient to refile stuff to a date-tree.

(defun my/org-refile-to-journal ()
  "Refile an entry to journal file's date-tree"
  (interactive)
  (require 'org-datetree)
  (let ((journal (expand-file-name "journal.org" org-directory))
	post-date)
    (setq post-date (or (org-entry-get (point) "TIMESTAMP_IA")
			(org-entry-get (point) "TIMESTAMP")))
    (setq post-date (nthcdr 3 (parse-time-string post-date)))
    (setq post-date (list (cadr post-date)
			  (car post-date)
			  (caddr post-date)))
    (org-cut-subtree)
    (with-current-buffer (or (find-buffer-visiting journal)
			     (find-file-noselect journal))
      (save-excursion
	(org-datetree-file-entry-under (current-kill 0) post-date)
	(bookmark-set "org-refile-last-stored")))
    (message "Refiled to %s" journal))
  (setq this-command 'my/org-refile-to-journal))

(defun my/org-agenda-refile-to-journal ()
  "Refile the item at point to journal."
  (interactive)
  (let* ((marker (or (org-get-at-bol 'org-hd-marker)
		     (org-agenda-error)))
	 (buffer (marker-buffer marker))
	 (pos (marker-position marker)))
    (with-current-buffer buffer
      (save-excursion
	(save-restriction
	  (widen)
	  (goto-char marker)
	  (org-remove-subtree-entries-from-agenda)
	  (my/org-refile-to-journal)))))
  (org-agenda-redo))

(org-defkey org-agenda-mode-map (kbd "C-c C-S-w") 'my/org-agenda-refile-to-journal)
(org-defkey org-mode-map (kbd "C-c C-S-w") 'my/org-refile-to-journal)

Enjoy!

Update [2016-06-16] Raam Dev pointed me to an issue and a suggested fix, that I have updated the above code with.