boon/boon-arguments.el

161 lines
6.5 KiB
EmacsLisp
Raw Normal View History

2014-10-19 14:17:46 +02:00
;;; boon --- An Ergonomic Command Mode -*- lexical-binding: t -*-
;;; Commentary:
2014-10-23 09:12:59 +02:00
;; A region list has the following form: ('region (begining . end) (begining . end) ...)
2014-10-19 14:17:46 +02:00
;;; Code:
(require 'boon-core)
2015-03-19 10:50:55 +01:00
(defcustom boon-enclosures
2014-10-19 14:17:46 +02:00
'(
(?$ . ("$" "$"))
(?| . ("|" "|"))
(?@ . ("@" "@"))
(?/ . ("/" "/"))
(?` . ("`" "`"))
(?A . ("" ""))
(?a . ("<" ">"))
(?b . ("[" "]"))
(?c . ("{-" "-}"))
2015-03-19 10:50:55 +01:00
(?d . ("\"" "\"")) ;; double quotes
(?D . ("``" "''")) ;; double quotes
2014-10-19 14:17:46 +02:00
(?f . ("«" "»")) ;; french quotes
(?h . ("#" "#")) ;; hash
(?m . ("`" "'"))
(?p . ("(" ")"))
(?q . ("'" "'"))
(?r . ("{" "}"))
(?o . ("" "")) ;; oxford brackets
(?t . ("~" "~")) ;; tilda
2015-03-19 10:50:55 +01:00
)
"Enclosures to use with the around command."
:type '(alist :key-type character :value-type (list string))
:group 'boon
)
2014-10-19 14:17:46 +02:00
(defun boon-spec-enclosure ()
2015-10-14 13:59:53 +02:00
"Specify an enclosure style. To be used as an argument to interactive."
2014-10-19 14:17:46 +02:00
(let ((c (read-char "Specify the enclosure")))
(cdr (assoc c boon-enclosures))))
2014-10-19 14:17:46 +02:00
2014-10-19 13:59:12 +02:00
(defun boon-select-thing-at-point (thing)
2014-10-23 09:12:59 +02:00
"Return a region list with a single item pointing to the THING at point."
2014-10-19 13:59:12 +02:00
(list 'region (bounds-of-thing-at-point thing)))
(defun boon-select-from-region (select-fun)
2014-10-23 09:12:59 +02:00
"Return a region list with a single item: the region selected after calling SELECT-FUN (interactively)."
2014-10-19 13:59:12 +02:00
(interactive)
(save-excursion
(call-interactively select-fun)
(list 'region (cons (region-beginning) (region-end)))))
(defun boon-select-wim () ;; what i mean
2014-10-23 09:12:59 +02:00
"Return a region list with a single item: either the symbol at point, or, if this fails, the sexp at point."
2014-10-19 13:59:12 +02:00
(interactive)
(let ((bounds (or (bounds-of-thing-at-point 'symbol)
(bounds-of-thing-at-point 'sexp))))
(list 'region bounds)))
2014-10-23 09:12:59 +02:00
2014-10-19 14:17:46 +02:00
(defun boon-jump-over-blanks ()
2014-10-23 09:12:59 +02:00
"Jump over blanks, forward."
2014-10-19 14:17:46 +02:00
(interactive)
(skip-chars-forward "\n\t "))
(defun boon-jump-over-blanks-backward ()
2014-10-23 09:12:59 +02:00
"Jump over blanks, backward."
2014-10-19 14:17:46 +02:00
(interactive)
(skip-chars-backward "\n\t "))
2014-10-19 13:59:12 +02:00
(defun boon-select-justline () (interactive) (list 'region (line-beginning-position) (line-end-position)))
(defun boon-select-line () (interactive) (boon-select-thing-at-point 'line))
(defun boon-select-paragraph () (interactive) (boon-select-thing-at-point 'paragraph))
(defun boon-select-document () (interactive)
(list 'region (cons (point-min) (point-max))))
(defun boon-select-word () (interactive) (boon-select-thing-at-point 'word))
(defun boon-select-sentence () (interactive) (boon-select-thing-at-point 'sentence))
(defun boon-select-symbol () (interactive) (boon-select-thing-at-point 'symbol))
(defun boon-select-list () (interactive) (boon-select-thing-at-point 'list))
(defun boon-select-sexp () (interactive) (boon-select-thing-at-point 'sexp))
(defun boon-select-outside-pairs () (interactive) (boon-select-from-region 'er/mark-outside-pairs))
2015-08-19 11:30:39 +02:00
(defun boon-select-comment () (interactive) (boon-select-from-region 'er/mark-comment))
2014-10-19 13:59:12 +02:00
(defun boon-select-inside-pairs () (interactive) (boon-select-from-region 'er/mark-inside-pairs))
(defun boon-select-outside-quotes () (interactive) (boon-select-from-region 'er/mark-outside-quotes))
(defun boon-select-whitespace () (interactive) (boon-select-thing-at-point 'whitespace))
(defun boon-select-blanks ()
(interactive)
(list 'region (cons
(save-excursion
(boon-jump-over-blanks-backward)
(point))
(save-excursion
(boon-jump-over-blanks)
(point)))))
(defun boon-normalize-reg (reg)
2014-10-23 09:12:59 +02:00
"Normalize the region REG by making sure beginning < end."
2014-10-19 13:59:12 +02:00
(cons (min (cdr reg) (car reg)) (max (cdr reg) (car reg))))
2015-10-14 13:59:53 +02:00
(defun boon-collapse-reg (reg)
"Collapse the region REG by moving the end to the beginning."
(cons (car reg) (cdr reg)))
2014-10-19 13:59:12 +02:00
(defun boon-borders (reg how-much)
2014-10-23 09:12:59 +02:00
"Given a normalized region REG, return its borders, whose size is HOW-MUCH."
2014-10-19 13:59:12 +02:00
(list (cons (cdr reg) (- (cdr reg) how-much))
(cons (car reg) (+ (car reg) how-much))))
2014-10-23 09:12:59 +02:00
2014-10-19 13:59:12 +02:00
(defun boon-content (reg)
2014-10-23 09:12:59 +02:00
"Given a normalized region REG, return its contents (crop the region by 1)."
2014-10-19 13:59:12 +02:00
(cons (+ (car reg) 1) (- (cdr reg) 1)))
(defun boon-select-borders (how-much regs)
2014-10-23 09:12:59 +02:00
"Return the bordering (of size HOW-MUCH) of a region list REGS.
This function is meant to be called interactively."
(interactive (list (prefix-numeric-value current-prefix-arg) (boon-spec-region "select contents")))
2014-10-19 13:59:12 +02:00
(cons 'region (apply 'append (mapcar (lambda (reg) (boon-borders reg how-much)) (mapcar 'boon-normalize-reg regs)))))
(defun boon-select-content (regs)
2014-10-23 09:12:59 +02:00
"Return the contents (of size HOW-MUCH) of a region list REGS.
This function is meant to be called interactively."
(interactive (list (boon-spec-region "select borders")))
2014-10-19 13:59:12 +02:00
(cons 'region (mapcar 'boon-content (mapcar 'boon-normalize-reg regs))))
(defun boon-spec-region (msg)
2014-10-19 14:17:46 +02:00
"Specify a region concisely using the keyboard.
The prompt (as MSG) is displayed. This function actually returns
a list of regions, in the form ((beginning . end) ...)"
(if (use-region-p) (list (cons (region-beginning) (region-end)))
2014-10-20 22:47:55 +02:00
(let (current-prefix-arg
2014-10-19 13:59:12 +02:00
;; this code fiddles with the prefix arg; but if we do
;; not hide our fiddling, the next command will use
;; the prefix arg that we have set.
(orig (point))
(km boon-select-map))
2014-10-20 22:47:55 +02:00
(setq current-prefix-arg 0)
2014-10-19 13:59:12 +02:00
(while (and km (keymapp km))
(let ((last-char (read-char (format "%s %s" msg current-prefix-arg))))
2014-10-19 13:59:12 +02:00
(if (and (>= last-char ?0) (<= last-char ?9))
2014-10-20 22:47:55 +02:00
(setq current-prefix-arg (+ (- last-char ?0) (* 10 current-prefix-arg )))
2014-11-05 13:31:13 +01:00
(setq km (lookup-key km (vector last-char))))))
2014-10-20 22:47:55 +02:00
(when (eq current-prefix-arg 0)
(setq current-prefix-arg nil))
2014-10-19 13:59:12 +02:00
(if km
(let (regs final)
(save-excursion
(setq regs (call-interactively km))
(setq final (point)))
;; (message (format "Reg = %s" regs))
(if (and regs
(listp regs)
(eq (car regs) 'region))
(cdr regs)
(list (cons orig final))))
(error "Unknown region specifier")))))
2014-10-19 14:17:46 +02:00
(provide 'boon-arguments)
;;; boon-arguments.el ends here