me, thinking out loud…

Posts tagged ‘code’

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))))
Advertisements

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

2010-08-20 Fri 00:23

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*.

git vs. mercurial

At work, we use hg and to be frank, I haven’t really got the hang of using it well. I’ve recently started using git for my own work and I’m loving it.

I chose git because

  1. github seems to be a nice place to hang around, as compared to bit-bucket.
  2. org-mode uses git and I need to learn to use git, if I intend contribute anything to org-mode.

But after using git for a month or so, I’m totally loving using git. The way it handles merges is awesome. People go gaga over it’s speed.

But for me, it’s the way it handles branches. I can have any number of branches on my machine, while pushing only changes in the master branch, upstream. I can easily merge changes from one branch to another. After putting up a lot of fight, I couldn’t make hg do this. Not as well as git does it!

I’ve now decided to convert my personal repositories to git. This blog post shows how to.

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 "/data/life-in-plain-text/journal.org")
        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 file))
      (save-excursion
        (org-datetree-file-entry-under (current-kill 0) post-date)
        (bookmark-set "org-refile-last-stored")))
    (message "Refiled to %s" 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)

Thanks Sacha

2010-07-30 Fri 06:59

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!

Tag Cloud

%d bloggers like this: