Next: , Previous: , Up: Introduction  

Q1.4.4: May I see an example of a useful SXEmacs Lisp function?

The following function does a little bit of everything useful. It does something with the prefix argument, it examines the text around the cursor, and it’s interactive so it may be bound to a key. It inserts copies of the current word the cursor is sitting on at the cursor. If you give it a prefix argument: C-u 3 M-x double-word then it will insert 3 copies.

(defun double-word (count)
  "Insert a copy of the current word underneath the cursor"
  (interactive "*p")
  (let (here there string)
      (forward-word -1)
      (setq here (point))
      (forward-word 1)
      (setq there (point))
      (setq string (buffer-substring here there)))
    (while (>= count 1)
      (insert string)
      (decf count))))

The best way to see what is going on here is to let SXEmacs tell you. Put the code into an SXEmacs buffer, and do a C-h f with the cursor sitting just to the right of the function you want explained. Eg. move the cursor to the SPACE between interactive and ‘"*p"’ and hit C-h f to see what the function interactive does. Doing this will tell you that the * requires a writable buffer, and p converts the prefix argument to a number, and interactive allows you to execute the command with M-x.