Part 5: better xy moves, defuns, calling defuns, color
Continued from Emacs Lisp programming pt. 4.
To edit Lisp code in its own buffer, create a buffer, giving it a name something like mycode.el
. To make sure Emacs realizes you are editing Lisp, you can type M-x
@lisp-mode@. You should now see (Lisp)
on the status line at the bottom of the screen. Now type away your code. When you’re happy with it, type M-x
@eval-buffer@ to load your code into Emacs. Then create another test buffer in which to run your code (via C-x 5 b
as described at the start of the last examples. When you’re in the test buffer, type M-:
to get a Lisp prompt at the bottom of the window with which you can run a defun by typing the defun’s name in parentheses.
A helper routine to draw at a given screen position
(defun drawxy (x y sprite) (erase-buffer) (dotimes (j y) (newline)) (dotimes (i x) (insert " ")) (insert sprite))
sprite
is the text to draw.
Using the helper function to draw
(defun liner () (dotimes (x 20) (drawxy x x "!") (sit-for 0.1)))
More elaborate drawing, based on Peter’s code
(defun oVeR-sine () (dotimes (y 777) (erase-buffer) (drawxy (round (+ 10 (* 10 (sin (/ y 10.0))))) (round (+ 10 (* 10 (sin (/ y 13.0))))) "--->)") (sit-for 0.1)))
Make a background grid
(defun background (width height) (erase-buffer) (dotimes (i height) (dotimes (j width) (insert ".")) (newline)))
Revised drawxy
to draw on the grid
(defun drawxy2 (x y sprite) (goto-char (point-min)) (forward-line y) (forward-char x) (delete-region (point) (+ (point) (length sprite))) (insert sprite))
Draw two lines on grid at same time
(defun liner2 () (background 80 40) (dotimes (x 20) (drawxy2 x x "&") (drawxy2 (- 20 x) x "*") (sit-for 0.1)))
Using color:
(insert (propertize "foreground color test" 'face '(:foreground "red"))) (insert (propertize "gray background test" 'face '(:background "gray30"))) (insert (propertize "color with hex code" 'face '(:foreground "#33AAFF"))) (insert (propertize "lots of color" 'face '(:foreground "orange" :background "purple")))
To make color work in ielm
, first type M-x font-lock-mode
. You should see a message at the bottom of the screen Font-Lock mode disabled
(if it says enabled
, type M-x font-lock-mode
again). Or just work in a test buffer which you’ve created.
Continued in Emacs Lisp programming pt. 6.