Man

Development & AI | Alper Akgun

Learning emacs elisp - Practice 4/4

August, 2023

This is a guide to learn by examples. There are two ways I run the commands.

Open a new buffer and run `C-x C-e` after each expression

Using interactive expression evaluation `M-x ielm`

Cursor position and move


;; Return cursor position
(point) ; current point
(point-max) ; return maximum point of the buffer
(point-min) ; return minimum point of the buffer. Not 0 because of narrowing.
(region-beginning)
(region-end)
(line-beginning-position) ; return
(line-end-position) ; return

;; Move cursor
(goto-char 42) ; go to the point.
(forward-char 2) ; go forward n chars. Accepts a negative argument.
(forward-line 3) ; move cursor
(end-of-line) ; go to end of line
(beginning-of-line) ; yes it's what you think
(skip-chars-forward "chars") ; skip given chars.
(skip-chars-backward "chars") ; skip given chars back.
(search-forward "foo") ; search for foo, move cursor there.
(search-backward "foo") ; search backward.
(search-forward-regexp "blah") ; same, but with regexes.
(search-backward-regexp "blah") ; same, but with regexes.
           

Process text

;;; Manipulate
;; Delete
(delete-char 19)
(delete-region 2 11)
(capitalize-region 73 91)
(downcase-region 73 91)
(progn
(goto-char (point-min))
  (while (search-forward "progn" nil t)
     (replace-match "Hello")) ; replace the current region
)

;; buffer strings
(buffer-substring 1 2)
(buffer-string)
(buffer-substring-no-properties
  (line-beginning-position)
  (line-end-position)
  )
(char-before)
(char-after)
(current-word)
(current-word t t) ; not including _
(thing-at-point 'filename) ; Or symbol, 'list, 'sexp, 'defun, 'filename, 'url, 'email, 'uuid, 'word, 'sentence, 'whitespace, 'line, 'number, 'page. 
(bounds-of-thing-at-point)

(save-excursion
  (beginning-of-line)
  (looking-at "X")) ; remember the cursor location

;; Buffers
(buffer-name)
(buffer-file-name)
(current-buffer)
(set-buffer "x") ; switch but does not make visible
(save-buffer) ; save
(kill-buffer "x")
(save-current-buffer)
(generate-new-buffer "MOMO")

(with-current-buffer "x" ;  Temporarily make "x" buffer current.
  ()
)

(with-temp-buffer
  (insert "Hello!"))

(save-restriction
  (narrow-to-region pos1 pos2)
  (delete-char 1)
)
;; Mark and region
(push-mark 1 nil t) ; Set mark to location 1 or if no args, to cursor position
(setq mark-active t)) ; Set mark is active after pushing to current
(region-active-p) ; is there a selection
(use-region-p) ;
(deactivate-mark nil)

;; Cut, copy, paste
(copy-region-as-kill 1 100) ;; push text in buffer positions to kill ring
(kill-region 2020 2033) ;; kill text and push to kill ring
(kill-new "cute cat")
(kill-append "cute cat" nil)
(yank)  ; paste
            

Files and directories


;; Files
(find-file "~/a.txt")
(kill-buffer) ; close the file
(write-file path)
(insert-file-contents "~/a.txt")
(append-to-file start-pos end-pos path)
(rename-file file-name new-name)
(copy-file old-name new-name)
(delete-file file-name)
(file-name-directory full-path)
(file-name-nondirectory full-path)
(file-name-extension file-name)
(file-name-sans-extension file-name)
            

Input


(interactive "sGive me a string") ; make function interactive, first arg as string
(interactive "nprompt_string") ; first arg to a function is number
(interactive "r")
(interactive
 (list
  (read-string "Enter name:")
  (read-number "Enter age:"))) ; ask for a list

(read-file-name "Choose a file")
(read-directory-name "Choose a dir")
(read-string "Say something:")
(read-regexp "Give me a regexp:")
(read-number "Give me a number")
(y-or-n-p) ; ask Yes or no
(let ((choices '("cat" "dog" "dragon" "tiger")))
  (message "%s" (completing-read "Open bookmark:" choices ))))

            

Modules and commands


;; commands
(defun yes ()
  "Insert YES at cursor position."
  (interactive)
  (insert "YES"))

(defun my-command ()
  "70 chars summary

Details:
- parameters.
- return value"
  (interactive)
  (let (var1 var2)
    ;; do something here
    ;; return last expression
    ))

;; Namespaces
(defun module-go ()
  "Go!"
   ...)

(provide 'module)