boon/boon-regs.el

82 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)
2016-09-08 21:58:23 +02:00
"Make a region with MRK PNT and CURSOR."
2016-08-20 20:39:08 +02:00
(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)
2016-09-08 21:58:23 +02:00
"Return t if either the point or the mark of REG is nil."
2016-08-29 20:20:19 +02:00
(not (or (boon-reg-point reg) (boon-reg-mark reg))))
(defun boon-regs-from-bounds (bnds)
2016-09-08 21:58:23 +02:00
"Convert BNDS as (mark . point) in to a list of boon regions."
2016-08-30 21:18:36 +02:00
(when bnds
(list (boon-mk-reg (car bnds) (cdr bnds) nil))))
2016-11-06 16:06:44 +01:00
(defun boon-reg-mark (reg) (nth 0 reg))
2015-10-24 22:45:23 +02:00
2016-11-06 16:06:44 +01:00
(defun boon-reg-point (reg) (nth 1 reg))
2016-08-20 20:39:08 +02:00
2016-11-06 16:06:44 +01:00
(defun boon-reg-cursor (reg) (nth 2 reg))
2015-11-09 21:01:47 +01:00
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)
2016-09-08 22:03:25 +02:00
"Put convert markers to numbers in REG."
2016-08-22 21:24:33 +02:00
(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
(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))
2016-09-08 13:48:42 +02:00
(fwd (> pt mk))
2016-09-08 21:58:23 +02:00
(skip-pt (if fwd 'skip-syntax-forward 'skip-syntax-backward))
(skip-mk (if fwd 'skip-syntax-backward 'skip-syntax-forward))
(pt-ext (progn (goto-char pt) (funcall skip-pt "-") (point))))
(boon-mk-reg (if (equal pt-ext pt) (progn (goto-char mk) (funcall skip-mk "-") (point)) mk)
pt-ext
2016-09-08 13:48:42 +02:00
(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-before (r1 r2)
"Return non-nil when R1 occurs before R2."
(< (boon-reg-begin r1) (boon-reg-end r2)))
2016-08-22 21:24:33 +02:00
(defun boon-reg-cur-after (r1 r2)
(declare (obsolete "unused" "July 2017"))
2016-08-22 21:24:33 +02:00
(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