boon/boon-regs.el

93 lines
3 KiB
EmacsLisp
Raw Normal View History

;;; boon-regs.el --- An Ergonomic Command Mode -*- lexical-binding: t -*-
2015-10-16 14:29:28 +02:00
;;; Commentary:
;;; Code:
2016-08-20 20:39:08 +02:00
(defun boon-mk-reg (mrk pnt &optional cursor)
(list mrk pnt cursor))
2015-10-24 22:45:23 +02:00
2016-08-29 20:20:19 +02:00
(defun boon-reg-nil (reg)
(not (or (boon-reg-point reg) (boon-reg-mark reg))))
(defun boon-reg-from-bounds (bnds)
2016-08-22 21:24:33 +02:00
(boon-mk-reg (car bnds) (cdr bnds) nil))
(defun boon-regs-from-bounds (bnds)
2016-08-30 21:18:36 +02:00
(when bnds
(list (boon-mk-reg (car bnds) (cdr bnds) nil))))
2015-10-24 22:45:23 +02:00
(defun boon-reg-mark (reg)
(car reg))
2016-08-20 20:39:08 +02:00
(defun boon-reg-point (reg)
(cadr reg))
(defun boon-reg-cursor (reg)
(caddr reg))
2015-11-09 21:01:47 +01:00
2015-10-16 14:29:28 +02:00
(defun boon-normalize-reg (reg)
2015-10-24 22:45:23 +02:00
"Normalize the region REG by making sure that mark < point."
2016-08-20 20:39:08 +02:00
(boon-mk-reg (boon-reg-begin reg) (boon-reg-end reg) (boon-reg-cursor reg)))
2015-10-16 14:29:28 +02:00
(defun boon-reg-to-markers (reg)
"Put copy the markers defining REG borders, and return that."
2016-08-20 20:39:08 +02:00
(boon-mk-reg (copy-marker (boon-reg-mark reg)) (copy-marker (boon-reg-point reg)) (boon-reg-cursor reg)))
2015-10-16 14:29:28 +02:00
2016-08-22 21:24:33 +02:00
(defun boon-reg-from-markers (reg)
(boon-mk-reg (marker-position (boon-reg-mark reg)) (marker-position (boon-reg-point reg)) (boon-reg-cursor reg)))
2015-10-16 14:29:28 +02:00
(defun boon-borders (reg how-much)
2015-10-24 22:41:43 +02:00
"Given a normalized region REG, return its borders (as a region list).
The size of the borders is HOW-MUCH."
2016-08-20 20:39:08 +02:00
;; TODO: if the results would touch or overlap, return the input region
(list (boon-mk-reg (boon-reg-end reg) (- (boon-reg-end reg) how-much) (boon-reg-cursor reg))
(boon-mk-reg (boon-reg-begin reg) (+ (boon-reg-begin reg) how-much) (boon-reg-cursor reg))))
2015-11-09 21:01:47 +01:00
2015-12-30 21:37:28 +01:00
;; TODO: also include surrounding blank lines if the other boundary is at bol/eol.
2015-11-09 21:01:47 +01:00
(defun boon-include-surround-spaces (reg)
2016-02-28 23:35:45 +01:00
"Return REG, extended to include spaces around 'boon-reg-point'.
The spaces are searched after 'boon-regpoint' if the region is
directed forward, or or before, if the region is backwards."
2015-11-09 21:01:47 +01:00
(save-excursion
2015-12-30 21:37:28 +01:00
(let* ((mk (boon-reg-mark reg))
(pt (boon-reg-point reg))
(fwd (> pt mk)))
(boon-mk-reg mk
(if fwd
(progn
(goto-char pt)
(skip-syntax-forward "-")
(point))
(progn
(goto-char pt)
(skip-syntax-backward "-")
2016-08-20 20:39:08 +02:00
(point)))
(boon-reg-cursor reg)))))
2015-10-16 14:29:28 +02:00
2015-10-24 22:33:03 +02:00
(defun boon-reg-begin (reg)
2015-10-24 22:41:43 +02:00
"The begining of region REG."
2015-10-24 22:45:23 +02:00
(min (boon-reg-point reg) (boon-reg-mark reg)))
2015-10-24 22:41:43 +02:00
2015-10-24 22:33:03 +02:00
(defun boon-reg-end (reg)
2015-10-24 22:41:43 +02:00
"The end of region REG."
2015-10-24 22:45:23 +02:00
(max (boon-reg-point reg) (boon-reg-mark reg)))
2015-10-24 22:41:43 +02:00
2015-10-16 14:29:28 +02:00
(defun boon-content (reg)
2015-10-24 22:41:43 +02:00
"Given a region REG, return its contents (crop the region by 1)."
2016-08-20 20:39:08 +02:00
(boon-mk-reg (+ (boon-reg-begin reg) 1) (- (boon-reg-end reg) 1) (boon-reg-cursor reg)))
2015-10-16 14:29:28 +02:00
(defun boon-reg-after (r1 r2)
"Return non-nil when R1 occurs after R2."
(> (boon-reg-begin r1) (boon-reg-end r2)))
2016-08-22 21:24:33 +02:00
(defun boon-reg-cur-after (r1 r2)
(or (not (boon-reg-cursor r1))
(and (boon-reg-cursor r2)
(> (overlay-end (boon-reg-cursor r1))
(overlay-end (boon-reg-cursor r2))))))
2015-10-16 14:29:28 +02:00
(provide 'boon-regs)
;;; boon-regs.el ends here