Skip to content
/ b.el Public

Emacs Lisp utility functions for buffer manipulation

License

Notifications You must be signed in to change notification settings

emacs-php/b.el

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

b.el

Utility functions for buffer manipulation.

Functions

  • b-append (buffer string-or-buffer)
  • b-binary? (buffer)
  • b-blank? (buffer)
  • b-coding-system (buffer)
  • b-duplicate (buffer target-buffer &key start end)
  • b-erase (buffer)
  • b-insert (buffer &rest string-or-buffer)
  • b-prepend (buffer string-or-buffer)
  • b-string (buffer &key start end)
  • b-string-with-property (buffer &key start end)

Documentation and examples

b-append (buffer string-or-buffer)

Insert string-or-buffer to bottom of the buffer.

(let ((x (get-buffer-create "buf-1"))
      (y "yyy"))
  (with-current-buffer x
    (erase-buffer)
    (insert "XXX"))
  (b-append x y)
  (b-string x)) ; => "XXXyyy"

b-binary? (buffer)

(let ((f (find-file-noselect "~/f.png")))
  (b-binary? f)) ; => t

(let ((f (find-file-noselect "~/a.txt")))
  (b-binary? f)) ; => nil

b-blank? (buffer)

(with-temp-buffer
  (b-blank? (current-buffer))) ; => t

(with-temp-buffer
  (insert "yey!")
  (b-blank? (current-buffer))) ; => nil

b-coding-system (buffer)

(let ((f (find-file-noselect "~/a.txt")))
  (b-coding-system f)) ; => utf-8-unix

(let ((f (find-file-noselect "~/f.png")))
  (b-coding-system f)) ; => no-conversion

b-duplicate (buffer target-buffer &key start end)

(with-temp-buffer
  (let ((new-buffer (get-buffer-create "buf-2"))
        (orig (current-buffer)))
    (insert "0123456789")

    (b-duplicate orig new-buffer)
    (b-string new-buffer) ;=> "0123456789"

    (b-erase new-buffer)

    (b-duplicate orig new-buffer)
    (b-string new-buffer :start 6) ;=> "56789"

    (b-erase new-buffer)

    (b-duplicate orig new-buffer)
    (b-string new-buffer :end 6) ;=> "01234"

    (b-erase new-buffer)

    (b-duplicate orig new-buffer)
    (b-string new-buffer :start 4 :end 8) ;=> "3456"

    ))

b-erase (buffer)

(let ((buf (get-buffer-create "buf-3")))
  (with-current-buffer buf
    (insert "ababababa"))
  (b-string buf) ; => "ababababa"

  (b-erase buf)

  (b-string buf) ;=> ""
  )

b-insert (buffer &rest string-or-buffer)

(with-temp-buffer
  (let ((buf (get-buffer-create "buf-4"))
        (tmp (current-buffer)))

    ;; The following idiom is abbreviated to `b-insert'
    (with-current-buffer buf
      (insert "12345"))
    (b-string buf) ; => "12345"

    (b-insert buf "aaaaa")
    (b-string buf) ;=> "12345aaaaa"

    (b-insert buf "67890" "bbbbb")
    (b-string buf) ;=> "12345aaaaa67890bbbbb"

    (b-insert tmp "zzzzz")

    (b-insert buf " | " tmp)
    (b-string buf) ;=> "12345aaaaa67890bbbbb | zzzzz"
    ))

b-prepend (buffer string-or-buffer)

(let ((x (get-buffer-create "buf-1"))
      (y "yyy"))
  (with-current-buffer x
    (erase-buffer)
    (insert "XXX"))
  (b-prepend x y)
  (b-string x)) ; => "yyyXXX"

b-string (buffer &key start end)

(let ((buf (get-buffer-create "buf-4")))

  (b-erase buf)
  (b-string buf) ;=>

  (b-insert buf "nurupo")
  (b-string buf) ; => "nurupo"
  (b-string buf :end 3) ; => "nu"
  (b-string buf :start 3) ; => "rupo"
  (b-string buf :start 3 :end 5) ; => "ru"
  )

b-string-with-properties (buffer &key start end)

(let ((buf (get-buffer-create "buf-5")))
  (b-erase buf)

  (b-insert buf (propertize "Red" 'face 'bold 'foreground "red"))

  (b-string buf) ; => "Red"
  (b-string-with-properties buf) ; => #("Red" 0 3 (foreground "red" face bold))
  )

About

Emacs Lisp utility functions for buffer manipulation

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published