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

Advice - Programming in Elisp

Below is a mail sent by Eric Schulte to the org-mode mailing list answering a query on how to write elisp for org-mode. I am reproducing it here, since it is useful advice for me. The actual thread is here.


The way that I learned how to program in emacs lisp was mainly using two commands `elisp-index-search' bound to `C-h e' on my system, and most importantly `describe-function' bound to `C-h f'. With `describe-function' you can look at the source code of functions whose behavior you are familiar with, you can then copy portions of the code to your scratch buffer where they can be edited and evaluated with `eval-defun' bound to `C-M-x'. Now with Babel, instead of doing this in the scratch buffer you could do this in emacs-lisp code blocks in an org file, enabling notes and hierarchical organization – it can be nice to have your noodling all collected in one file for later reference.

If you are going to do any serious work with lisp, I would emphatically recommend using paredit-mode, and becoming friends with the Sexp movement functions

C-M-f runs the command paredit-forward C-M-b runs the command paredit-backward C-M-u runs the command backward-up-list C-M-k runs the command kill-sexp C-y runs the command yank

They allow you to manipulate lisp code on the level of logical expressions, the utility of which can not be over stated.

As for working with Org-mode in particular, I'd recommend looking at the documentation and source-code of Org-mode functions with `describe-function', and then looking for how these functions are actually used in the Org-mode code base with `rgrep'.

For a more structured learning experience, I've heard very good things about http://www.gnu.org/software/emacs/emacs-lisp-intro/, although I haven't used it myself.

Hope this helps. Happy Hacking – Eric


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.

Vimperator and Firemacs!

For the uninitiated, vimperator is an emacs plugin to get Firefox working with vim keybindings and firemacs 1 is for emacs keybindings.

I'm obsessed with emacs (as should already be evident) and can't really edit text without it's key-bindings. There are others like me, and hence the firemacs plugin. It's a blessing for my tribe.

Firemacs is great, except for the one great feature that it lacks - being able to jump to links/textareas without using the mouse. I've seen Nishanth do it with ease, using Vimperator. I wanted to add this feature to Firemacs, by copying some code from Vimperator to Firemacs. I directly started looking at the code, but couldn't make head or tail of it. I got a few doubts about how Vimperator works, since I'd never used it. Nishanth asked me to simply install it and try it out.

And then we had our EUREKA! moment. Vimperator and Emacs both enabled together work seamlessly well. Two obsessed people are happy and satisfied! And hopefully many more. :D

Footnotes:

1

Key-bindings are a religious thing, in the world of editors!

Weblogger.el configured!

If you see this post, it means weblogger has been successfully configured in emacs. Emacs Rocks. \\m/

UPDATE [2010-09-02]: I now use org2blog, a small extension to emacs orgmode to post to my blog.

Mid-Term 'Psycho'Analysis

Its been 10 days on my sabbatical. I have done reasonably well during the first week, but now its getting on my nerves. I have realized how addicted I am to chat, IRC and the Internet. I have to soon find some remedies for this.

Well, here is the first step. I approached ELIZA (or Doctor Fill, that's actually the emacs mode) , the Emacs Psychotherapist to help me. An enjoyable chat this one! ;)

ELIZA (Doctor Fill) abbreviated as DF (pun intended, for those who get it)

DF: I am the psychotherapist.  Please, describe your problems.  Each time
you are finished talking, type RET twice.

Hi. Nice meeting you.

DF: How do you do?  What brings you to see me?

<!--more-->I am obviously not fine, otherwise I would not have been here.
I have realized I am addicted to the internet and want you to help
me.

DF: Why do you say that?

I say that because you are a doctor.

DF: Is it because because I am a doctor that you came to me?

Isn't that pretty obvious?

DF: Can you elaborate on that?

Yes, I did come to see you because you are a doctor. Now, I regret it.

DF: Does it bother you that I am a doctor now you regret it?

I don't understand your question.

DF: Earlier you said because I am a doctor?

Yes, because you are a doctor I came to you.

DF: When did you first know that I am a doctor you came to me?

I knew about you, when I first fiddled around with Emacs around an
year ago.

DF: What happened when you first fiddled around with emacs around a year
ago?

I loved it, thoroughly! But for a beginner like me it was difficult to
get along. I stopped using it.

DF: Is the fact that for a beginner like you it was difficult to get along
you stopped using it the real reason?

I think that along with my laziness to stick with the learning was the
real reason

DF: Does the fact that along with your laziness to stick with the learning
was the real reason explain anything else?

It does not explain anything else to me.

DF: Maybe your plans have something to do with this.

What plans? I donot have any plans.

DF: What makes you believe what plans you donot have any plans?

If I had plans, I would have been executing them.

DF: You mentioned that I am a doctor now you regret it?

Yes, because you seemed stupid initially. Now you seem to be making
some sense.

DF: Are you sure I seemed stupid initially now I seem to be making some
sense?

Yes I am pretty sure.

DF: Is it because of your life that you are going through all this?

Obviously. Because I am alive I am going through all this.

DF: Is it because you are alive you are going through all this that you
came to me?

Yes.

DF: I see...  Well, are you sure that this is so?

Yes. I don't think there is emacs in the hell.

DF: I would appreciate it if you would watch your tongue!

Oh! so emacs is in the hell too! is it in the heavan too?

DF: Emacs?  Hah!  Perhaps you could continue.

I thought only mortals used Emacs. Do others use it too?

DF: Have you been positive?

Yes. I always thought Emacs is only for the homo sapiens.

DF: I believe you have inhibitions with emacs.

No. I love emacs.
But it isn't suitable for Gods and Demons

DF: Are you annoyed that it is not suitable for gods and demons?

No

DF: Why not?

I am neither of them. That's why

DF: Please, continue.

I am a human and I can use emacs. Others donot need it. I am not annoyed

DF: Have you been annoyed often?

Ocassionally

DF: Go on, don't be afraid.

I am not. I have been ocassionally annoyed, more often with myself
than others.

DF: What causes you to be annoyed?

Presently, I am annoyed with myself for being addicted to the Internet
and may be for chatting with you too.

DF: Do you really think you are annoyed?

Affirmative

DF: I need a little more detail please.

Yes. I am a little annoyed with myself.

DF: Are you annoyed often?

Ocassionally. Why do you repeat questions?

DF: When did you first know that do me repeat questions?

Long Long ago, when I first used Emacs.

DF: What makes you think long long ago when you first used emacs?

I saw you repeat questions.

DF: Is it because of the people you hang around with that you say you saw
me repeat questions?

Yes.

DF: Yes, hmmm... so, why do you say this is so?

I am leaving. You are being too repititive. Goodbye!

DF: What makes you believe because I am a doctor?

Goodbye!

DF: My secretary will send you a bill.

Bill? You did not help me much.

DF: How do you reconcile problems at school?

Goodbye!

DF: Bye bye.

Bye

DF: See ya.

See ya.

DF: Are you sure that see me?

Yes, when in trouble

DF: What would happen if in trouble?

I will see ya

DF: Earlier you said I am a doctor you came to me?

Yes, now also I say that.

DF: You're being a bit brief, would you please go into detail.

Goodbye!

DF: Ok, talk to you some other time.

bye!

DF: Talk to you later.

On a more serious note, the Internet has become an Integral part of my life. I do sometimes think, a bit too much, but then…. the internet is "The Internet"!

A sabbatical

This post, is a different one in a couple of ways.

Firstly, I posted this from within emacs! I'm using the w3m browser within emacs to post this. I've started using (trying to use) emacs again. Hope to keep at it longer this time.

Secondly, I wish to inform junta about my sabbatical for a couple of weeks. I have decided not to use IM/IRC/SocialNetworking sites for the next couple of weeks. [2008-07-06 Sun 00:00]–[2008-07-19 Sat 23:59] I will, of course, be using mail and will also blog if and when I feel like. This is an experiment triggered by a few ideas from Chaitu1 and my own holiday lifestyle analysis. (Also Chaitu has volunteered to join me.) Hope to make it through and also get some valid, interesting results out of the experiment.


Update: Firemacs add-on for firfox is very useful. I couldn't get a decent browser for emacs that could open gmail, so I'm using Firefox with the Firemacs add-on to compose my mail. Its absolutely amazing. Believe me! [Jul 6, 2008]


Footnotes:

1

Chaitu - Poetic Paradigm

Not so Floppix...

Disambiguation: The original Linux on 2 Floppies is here.

I, post this from a remastered version of Knoppix that we named Floppix!

It all actually began, with me deciding to do a bit of Emacs-ing at home during the winter hols but I don't have a Linux box at home. Live-CD is the way to go! But then Emacs doesn't \\*usually\\* go with Live-CDs. So I decided to get around making my own. :) [Dunnet "Dunnet Game"), an Adventure game in Emacs, played a crucial role in motivating me to go ahead!] The rest as they say is history….

Actually, I tried a debootstrap from my Debian etch but I couldn't manage to get the initrd working. [[http://avudem.blogspot.com "Voodoo"][I ain't this geekish, its only that I am incapable of putting these terms in a simpler language.] After numerous futile attempt, I gave up for the time being and that's when the genius of [Voodoo]] came in, he took a different route. Remastering Knoppix.[Modifying an existing Live-CD of Knoppix]

I won't get into the exact details of how we got about it. There's a lot of online help available for that.

A few highlights, for the curious reader…

  • got rid of the default KDE and replaced it with Fluxbox. [actually Floppix = Fluxbox + Knoppix :P]
  • added emacs, cmucl, slime, octave, linuxdcpp, vlc and others.
  • got rid of other packages which we weren't going to use.
  • changed the boot message
  • changed the default background image.
  • removed the default boot image [ couldn't change it as the image we chose wasn't being properly converted to lss16 format ]
  • tested the CD with Qemu [size of the iso got more than 700MB]
  • removed a few other not so regularly used packages [including wine!]
  • burnt the CD
  • Logged in!

Now, that I've logged in, I realize we managed to quite a decent job. Obviously we weren't perfect. It was the first time and done in quite a hurry. [btw, we have our end-sems going on; I've no exam tomorrow though] I was able to listen to music, connect to the DC, play a game of dunnet and make a blogpost! We also missed a few packages, for instance a screen shot capture program! But it ain't too bad, still a long way to go though!

Just hoping we manage to make it better, as we get better at it! ;)

Here comes FloppixV0.1!


Update [3/1/08]: I never knew that GIMP could capture screenshots! ImageMagick can do that too. I had two programs at hand and thought I had none! Here's a Screenshot of the Floppix Desktop.

floppix_desktop.jpg

Picture Source [for the Background Image] : 9


Update 2 [4/1/08]: There is already a floppy distro of Linux called Floppix. I knew about this only when some one searching for it reached my blog. Guess I should do something about this. [I'll be able to do it only after getting back to campus,though]