DEV Community

Discussion on: Share a code snippet in any programming language and describe what's going on

Collapse
 
jeremyf profile image
Jeremy Friesen
(defun jf/pulse (parg)
  "Pulse the current line.

If PARG (given as universal prefix), pulse between `point' and `mark'."
  (interactive "P")
  (if (car parg)
    (pulsar--pulse nil nil (point) (mark))
  (pulsar-pulse-line)))
(global-set-key (kbd "C-x l") 'jf/pulse)
Enter fullscreen mode Exit fullscreen mode

The above function uses the pulsar.el package to create a visual "pulse" that temporarily highlights either the current line or the region between mark and point.

Point is analogue to the cursor. Mark is analogue to a "temporary" bookmark.

In the above code, the last line (e.g. (global-set-key...)) maps to C-x l (e.g. Ctrl+x then l) the jf/pulse function defined on lines 1 through 8.

By default, if I just type C-x l, I will "pulse" the current line. If I "prefix" the function call by first typing C-u (e.g. Ctrl+u) then type C-x l, I will pulse the region between mark and point.

The anatomy of the above is as follows:

  • Line 1: Name the function jf/pulse and one parameter parg (e.g. prefix arg).
  • Line 2 through 4: This is the doc string, it's available throughout emacs.
  • Line 5: This declares the function as interactive (I can call it from a menu). The "P" indicates that the above parg parameter is a prefix argument.
  • Line 6: If given the parg (e.g. I typed C-u C-x l)...
  • Line 7: ...then pulse the region between point and mark.
  • Line 8: ...else pulse the current line.
  • Line 9: Bind the jf/pulse to the C-x l key combination.