boon/boon-main.el

692 lines
23 KiB
EmacsLisp
Raw Normal View History

;;; boon-main.el --- An Ergonomic Command Mode -*- lexical-binding: t -*-
2014-10-19 13:59:12 +02:00
;;; Commentary:
2014-10-19 14:17:46 +02:00
;;; Code:
2014-10-19 13:59:12 +02:00
(require 'boon-core)
2014-10-19 14:17:46 +02:00
(require 'boon-arguments)
2014-10-19 13:59:12 +02:00
(require 'er-basic-expansions)
2015-10-14 22:55:39 +02:00
(require 'multiple-cursors)
2015-10-16 19:29:19 +09:00
(require 'subr-x)
2014-10-19 13:59:12 +02:00
2015-11-11 22:28:20 +01:00
(defcustom boon-hints-enabled 't "Display hints." :group 'boon)
(defun boon-hint (msg)
"Provide MSG as a hint."
(when boon-hints-enabled
(message msg)))
(defmacro boon-with-ordered-region (body)
2014-10-30 20:12:08 +01:00
"Run the BODY, ensuring that the point is before the mark."
2015-11-11 22:28:20 +01:00
`(if (< (point) (mark))
2014-10-19 13:59:12 +02:00
,body
(progn (exchange-point-and-mark) ,body (exchange-point-and-mark))))
2015-10-20 22:37:59 +02:00
(defun boon-drop-cursor (regs)
2015-10-24 21:33:38 +02:00
"Drop a new cursor at the first position given by REGS.
2015-10-22 22:46:27 +02:00
NOTE: Do not run for every cursor."
2015-10-20 22:37:59 +02:00
(interactive (list (boon-spec-region "cursor")))
2015-11-11 19:54:17 +01:00
(when (and mark-active (> (point) (mark)))
(exchange-point-and-mark))
2015-10-20 22:37:59 +02:00
(mc/create-fake-cursor-at-point)
2015-10-30 09:17:00 +01:00
(goto-char (boon-reg-point (car regs)))
(set-marker (mark-marker) (boon-reg-mark (car regs)))
2015-10-20 22:37:59 +02:00
(mc/maybe-multiple-cursors-mode))
2015-10-21 22:31:20 +02:00
(defun boon-move-cursor (regs)
2015-10-24 21:33:38 +02:00
"Move the cursor at the first position given by REGS.
2015-10-22 22:46:27 +02:00
NOTE: Do not run for every cursor."
2015-10-21 22:31:20 +02:00
(interactive (list (boon-spec-region "cursor")))
2015-10-30 09:17:00 +01:00
(goto-char (boon-reg-point (car regs))))
2015-10-21 22:31:20 +02:00
2015-10-22 22:46:27 +02:00
(defun boon-drop-or-extend-mark ()
"Drop a mark; or extend the region to the next full line; or revert to original state."
2014-10-19 13:59:12 +02:00
(interactive)
(if mark-active
(if (and (bolp)
(save-excursion (goto-char (mark)) (bolp))
(not (eq (point) (mark))))
(progn ;; here we have a number of full lines selected, and that number is more than 0
(pop-mark) ;; load the saved position into the mark
(goto-char (mark));; jump there
(deactivate-mark))
(boon-with-ordered-region
2014-10-19 13:59:12 +02:00
(progn ;; here we have at least one non-full line selected. Extend to the full lines.
(beginning-of-line)
(exchange-point-and-mark)
(end-of-line)
(forward-char)
(exchange-point-and-mark))))
(progn
(set-mark (point))
(push-mark) ;; Save the starting position, so we can go back to it.
(call-interactively 'boon-mark-region))))
2015-10-22 22:46:27 +02:00
(defun boon-drop-mark ()
"Drop or deactivate the mark."
(interactive)
2015-10-24 21:33:38 +02:00
(declare (obsolete "Use boon-drop-mark instead" "20151020"))
2015-10-22 22:46:27 +02:00
(if mark-active
(progn
(mc/execute-command-for-all-fake-cursors (lambda () (interactive) (deactivate-mark)))
(deactivate-mark t))
(progn
(call-interactively 'boon-mark-region))))
(defun boon-current-line-indentation ()
"Return the indentation of the curent line."
2014-10-19 13:59:12 +02:00
(save-excursion
(back-to-indentation)
(current-column)))
(defun boon-enclose (enclosure regs)
"Wrap, with the ENCLOSURE the regions given as REGS."
(interactive (list (boon-spec-enclosure) (boon-spec-region "enclose")))
2015-10-15 23:02:57 +02:00
;; (message "boon-enclose regs=%s" regs)
2015-10-30 09:17:00 +01:00
(dolist (reg (mapcar 'boon-reg-to-markers regs))
2015-10-15 23:02:57 +02:00
(save-excursion
2015-10-30 09:17:00 +01:00
(goto-char (boon-reg-end reg))
2015-10-15 23:02:57 +02:00
(insert (cadr enclosure))
2015-10-30 09:17:00 +01:00
(goto-char (boon-reg-begin reg))
2015-10-15 23:02:57 +02:00
(insert (car enclosure)))))
2014-10-19 13:59:12 +02:00
(defun boon-find-char-backward (char)
"Move the cursor backwards, until finding an occurence of the character CHAR."
2014-10-19 13:59:12 +02:00
(interactive "cType the character to find")
(search-backward (make-string 1 char))
(forward-char 1))
(defun boon-find-char-forward (char)
2014-10-19 14:17:46 +02:00
"Find the given character (as CHAR), forwards."
2014-10-19 13:59:12 +02:00
(interactive "cType the character to find")
(search-forward (make-string 1 char))
(backward-char 1))
(defun boon-edge-of-expression (forward)
2014-10-19 14:17:46 +02:00
"Jump to the forward or backward (as FORWARD) limit of the current expression."
2014-10-19 13:59:12 +02:00
(interactive "P")
(let ((orig-point (point)))
(goto-char
(save-excursion
(deactivate-mark)
(if (boon-in-string-p)
(er/mark-inside-quotes) (er/mark-inside-pairs))
(when forward (exchange-point-and-mark))
(point)))
;; make sure we make some progress
(when (eq (point) orig-point)
(forward-char (if forward 1 -1)))))
(defun boon-end-of-expression ()
2014-10-30 20:12:08 +01:00
"Jump to the end of the current expression."
2014-10-19 13:59:12 +02:00
(interactive)
(boon-edge-of-expression 't))
(defun boon-beginning-of-expression ()
2014-10-30 20:12:08 +01:00
"Jump to the beginning of the current expression."
2014-10-19 13:59:12 +02:00
(interactive)
(boon-edge-of-expression nil))
(defun boon-delete-region ()
"Delete the region if it is active."
2014-10-19 13:59:12 +02:00
(when (use-region-p)
(delete-region (region-beginning) (region-end))))
2014-10-19 13:59:12 +02:00
(defun boon-insert-register ()
"Insert register, replacing the region if it is active."
(boon-delete-region)
2014-10-19 13:59:12 +02:00
(call-interactively 'insert-register))
(defun boon-copy-to-register ()
2014-10-30 20:12:08 +01:00
"Copy to register and deactivate mark."
2014-10-19 13:59:12 +02:00
(interactive)
(call-interactively 'copy-to-register)
(deactivate-mark))
(defun boon-splice ()
2015-11-10 22:17:04 +01:00
"Yank, replacing the region if it is active.
When repeated, fix the spacing."
2014-10-19 13:59:12 +02:00
(interactive)
2015-11-22 20:09:33 +01:00
(if (and (eq last-command 'yank) (not (bolp)))
2015-11-10 22:17:04 +01:00
(boon-splice-fix-spaces)
(progn (boon-delete-region)
2015-11-11 22:28:20 +01:00
(yank)
(boon-hint "If spaces are wrong, run boon-splice again."))))
2014-10-19 13:59:12 +02:00
2015-11-09 21:18:43 +01:00
(defun boon-need-space ()
2015-11-10 22:17:04 +01:00
"Is it necessary to insert a space here to separate words or expressions?"
2015-11-18 22:26:05 +01:00
(or
(and (looking-back "\\sw") (looking-at "\\sw"))
(and (looking-back "\\s)") (not (looking-at "\\s)")))
(and (not (looking-back "\\s(")) (looking-at "\\s("))))
2015-11-09 21:18:43 +01:00
2015-11-11 22:28:20 +01:00
(defun boon-fix-a-space ()
"Fix the text to have exactly one space at the point."
(cond ((boon-need-space) (insert " "))
((and (looking-at " ") (looking-back " ") (delete-char 1)))))
2015-11-10 22:17:04 +01:00
(defun boon-splice-fix-spaces ()
"Yank, replacing the region if it is active.
Fix the surroundings so that they become nicely spaced."
(interactive)
(save-excursion
2015-11-11 22:28:20 +01:00
(boon-fix-a-space)
2015-11-10 22:17:04 +01:00
(goto-char (mark))
2015-11-11 22:28:20 +01:00
(boon-fix-a-space)))
2015-11-10 22:17:04 +01:00
2014-10-23 13:37:38 +02:00
(defun boon-line-prefix ()
2015-11-11 22:28:20 +01:00
"Return the text between beginning of line and point."
2014-10-23 13:37:38 +02:00
(buffer-substring-no-properties
2015-11-11 22:28:20 +01:00
(line-beginning-position)
2014-10-23 13:37:38 +02:00
(point)))
2015-11-09 20:29:16 +01:00
(defun boon-line-suffix ()
2015-11-11 22:28:20 +01:00
"Return the text between end of line and point."
2015-11-09 20:29:16 +01:00
(buffer-substring-no-properties
2015-11-11 22:28:20 +01:00
(line-end-position)
2015-11-09 20:29:16 +01:00
(point)))
2014-10-19 13:59:12 +02:00
(defun boon-at-indent-or-more-p ()
2014-10-30 20:12:08 +01:00
"Return non-nil if the point is at the current line indentation; or to the right."
2014-10-19 13:59:12 +02:00
(or (eolp)
(and (not (boon-at-indent-p))
2015-11-09 21:29:34 +01:00
(string-blank-p (boon-line-prefix)))))
2014-10-19 13:59:12 +02:00
(defun boon-at-indent-p ()
2014-10-30 20:12:08 +01:00
"Return non-nil if the point is at the current line indentation."
2014-10-19 13:59:12 +02:00
(eq (save-excursion (back-to-indentation) (point)) (point)))
2015-11-09 20:28:48 +01:00
(defun boon-smarter-upward (count)
2015-11-11 22:28:20 +01:00
"Move upward, to a line with the same level of indentation, or less, COUNT times."
2015-11-09 20:28:48 +01:00
(interactive "p")
2014-10-19 13:59:12 +02:00
(back-to-indentation)
(dotimes (number count)
(previous-logical-line)
(while (boon-at-indent-or-more-p) (previous-logical-line)))
2014-10-19 13:59:12 +02:00
(back-to-indentation))
(defun boon-smarter-downward (count)
2015-11-11 22:28:20 +01:00
"Move downward, to a line with the same level of indentation, or less COUNT times."
(interactive "p")
2014-10-19 13:59:12 +02:00
(back-to-indentation)
(dotimes (number count)
(next-logical-line)
(while (boon-at-indent-or-more-p) (next-logical-line)))
2014-10-19 13:59:12 +02:00
(back-to-indentation))
(defun boon-smarter-backward (count)
2015-11-11 22:28:20 +01:00
"Move backward, over COUNT whole syntactic units."
(interactive "p")
(dotimes (number count)
2014-10-19 13:59:12 +02:00
(boon-jump-over-blanks-backward)
(cond
((boon-looking-at-comment -1)
(forward-comment -1))
((looking-back "\\s\"")
(backward-char)
(er--move-point-backward-out-of-string))
((looking-back "\\s)")
(backward-list))
((looking-back "\\s_") ;; symbol
(skip-syntax-backward "_"))
((looking-back "\\s(")
(backward-char))
((looking-back "\\s!") ;; generic comment delimiter
(skip-syntax-backward "!"))
2015-10-24 21:33:38 +02:00
((looking-back "\\sw")
2015-11-09 20:29:16 +01:00
(if (not (looking-at "\\(\\s-\\|\\s(\\|\\s)\\)"))
2014-10-19 13:59:12 +02:00
(skip-syntax-backward "w")
(skip-syntax-backward "w_")))
(t
(backward-char)))))
2014-10-19 13:59:12 +02:00
(defun boon-smarter-forward (count)
2015-11-11 22:28:20 +01:00
"Move forward, over COUNT whole syntactic unit."
(interactive "p")
(dotimes (number count)
2015-11-09 20:29:16 +01:00
(boon-jump-over-blanks-forward)
2014-10-19 13:59:12 +02:00
(cond
2014-10-23 13:50:16 +02:00
((boon-looking-at-line-comment-start-p)
2014-10-19 13:59:12 +02:00
(end-of-line)
2015-11-09 20:29:16 +01:00
(boon-jump-over-blanks-forward))
2014-10-19 13:59:12 +02:00
((boon-looking-at-comment 1);;
(forward-comment 1))
((looking-at "\\s\"")
(forward-char)
(er--move-point-forward-out-of-string))
((looking-at "\\s(")
(forward-list))
((looking-at "\\s_") ;; symbol
(skip-syntax-forward "_"))
2015-11-09 20:29:16 +01:00
((looking-at "\\s)")
2014-10-19 13:59:12 +02:00
(forward-char))
((looking-at "\\s!") ;; generic comment delimiter
(skip-syntax-forward "!"))
2015-11-09 20:29:16 +01:00
((looking-at "\\sw")
(if (not (looking-at "\\(\\s-\\|\\s(\\|\\s)\\)"))
2014-10-19 13:59:12 +02:00
(skip-syntax-forward "w")
(skip-syntax-forward "w_")))
2015-11-09 20:29:16 +01:00
(t
(forward-char)))))
(defun boon-smarter-forward-spaces (count)
"Move forward, over a whole syntactic unit. Handle spaces cleverly."
(interactive "p")
2015-11-18 22:26:05 +01:00
(declare (obsolete "does not seem very useful" "20151120"))
2015-11-09 20:29:16 +01:00
(dotimes (number count)
(let ((spaces-skipped (not (equal (boon-jump-over-blanks) 0)))
(in-middle nil)
(at-bol (string-blank-p (boon-line-prefix))))
(cond
((boon-looking-at-line-comment-start-p)
(end-of-line)
(forward-char))
((boon-looking-at-comment 1);;
(forward-comment 1))
((looking-at "\\s\"")
(forward-char)
(er--move-point-forward-out-of-string))
((looking-at "\\s(")
(forward-list))
((looking-at "\\s_") ;; symbol
(skip-syntax-forward "_"))
((looking-at "\\s)")
(forward-char)
(setq in-middle 't))
((looking-at "\\s!") ;; generic comment delimiter
(skip-syntax-forward "!"))
((looking-at "\\sw")
(setq in-middle 't)
(if (not (looking-back "\\(\\s-\\|\\s(\\|\\s)\\)"))
(skip-syntax-forward "w")
(skip-syntax-forward "w_")))
(t
(forward-char)
(setq in-middle 't)))
(unless (or spaces-skipped in-middle)
(if at-bol
(skip-chars-forward "\t\n ")
(skip-chars-forward "\t "))))))
(defun boon-smarter-backward-spaces (count)
"Move backward, over a whole syntactic unit. Handles spaces smartly."
(interactive "p")
2015-11-18 22:26:05 +01:00
(declare (obsolete "does not seem very useful" "20151120"))
2015-11-09 20:29:16 +01:00
(dotimes (number count)
(let ((spaces-skipped (not (equal (boon-jump-over-blanks-backward) 0)))
(in-middle nil)
(at-eol (string-blank-p (boon-line-suffix))))
(cond
((boon-looking-at-comment -1)
(forward-comment -1))
((looking-back "\\s\"")
(backward-char)
(er--move-point-backward-out-of-string))
((looking-back "\\s)")
(backward-list))
((looking-back "\\s_") ;; symbol
(skip-syntax-backward "_"))
((looking-back "\\s(")
(backward-char)
(setq in-middle 't))
((looking-back "\\s!") ;; generic comment delimiter
(skip-syntax-backward "!"))
((looking-back "\\sw")
(setq in-middle 't)
(if (not (looking-at "\\(\\s-\\|\\s(\\|\\s)\\)"))
(skip-syntax-backward "w")
(skip-syntax-backward "w_")))
(t
(backward-char)
(setq in-middle 't)))
(unless (or spaces-skipped in-middle)
(if at-eol
(skip-chars-backward "\t\n ")
(skip-chars-backward "\t "))))))
(defun boon-other-side ()
"Move backward, over a whole syntactic unit. Handles spaces smartly."
(cond
((boon-looking-at-comment -1)
(forward-comment -1))
((boon-looking-at-comment 1)
(forward-comment 1))
((looking-back "\\s\"")
(backward-char)
(er--move-point-backward-out-of-string))
((looking-at "\\s\"")
(forward-char)
(er--move-point-forward-out-of-string))
((looking-back "\\s)")
(backward-list))
((looking-at "\\s(")
(forward-list))
((looking-back "\\s_") ;; symbol
(skip-syntax-backward "_"))
((looking-back "\\sw")
(if (not (looking-at "\\(\\s-\\|\\s(\\|\\s)\\)"))
(skip-syntax-backward "w")
(skip-syntax-backward "w_")))))
2014-10-19 14:17:46 +02:00
2014-10-19 13:59:12 +02:00
(defun boon-toggle-character-case ()
2014-10-30 20:12:08 +01:00
"Toggle the case of the character at point."
2014-10-19 13:59:12 +02:00
(interactive)
(let ((case-fold-search nil))
(if (looking-at "[[:upper:]]")
(progn
(downcase-region (point) (+ (point) 1)))
(progn
(upcase-region (point) (+ (point) 1))))))
(defun boon-toggle-case ()
2014-10-30 20:12:08 +01:00
"Toggle the case of the character at point, or cycle the case of the region if it is active."
2014-10-19 13:59:12 +02:00
(interactive)
(if (use-region-p)
(call-interactively 'boon-toggle-region-case)
(boon-toggle-character-case)))
2015-10-24 22:33:03 +02:00
(defun boon-toggle-region-case (regs)
"Cycle regions through 3 capitalizations: UPPER CASE, lower case, Title Case.
Regions are given by REGS.
NOTE: Do not run for every cursor."
(interactive (list (boon-spec-region "toggle-case")))
2014-10-19 13:59:12 +02:00
(let* ((deactivate-mark nil)
(case-fold-search nil)
(cur-state (if (eq last-command this-command)
(get this-command 'state)
(save-excursion
2015-10-24 22:33:03 +02:00
(goto-char (boon-reg-begin (car regs)))
2014-10-19 13:59:12 +02:00
(cond
((looking-at "[[:upper:]][[:upper:]]") 'upcase-region)
((looking-at "[[:upper:]][[:lower:]]") 'capitalize-region)
(t 'downcase-region))))))
(setq cur-state (cdr (assoc cur-state '((downcase-region . capitalize-region)
(capitalize-region . upcase-region)
(upcase-region . downcase-region)
))))
2015-10-24 22:33:03 +02:00
(dolist (reg regs)
(funcall cur-state (boon-reg-begin reg) (boon-reg-end reg)))
2014-10-19 13:59:12 +02:00
(put this-command 'state cur-state)))
(defun boon-toggle-mark ()
2014-10-30 20:12:08 +01:00
"Toggle region activation."
2014-10-19 13:59:12 +02:00
(interactive)
(if mark-active
(deactivate-mark)
(when (eq (point) (mark))
(message "mark placed at point"))
(activate-mark)))
(defun boon-visible-beginning-of-line ()
(interactive)
(beginning-of-line)
(while (and (bound-and-true-p visible-mode) (outline-invisible-p))
(backward-char 1)
(beginning-of-line 1)))
2014-10-19 13:59:12 +02:00
(defun boon-beginning-of-line ()
"Move point to the first non-whitespace character on this line.
If point was already at that position, move point to beginning of
line."
(interactive)
(let ((oldpos (point)))
(back-to-indentation)
2014-10-19 13:59:12 +02:00
(when (= oldpos (point))
(boon-visible-beginning-of-line))))
2014-10-19 13:59:12 +02:00
(defun boon-looking-at-comment (how-many)
2014-10-30 20:12:08 +01:00
"Is the current point looking at HOW-MANY comments? (negative for backwards)?"
2014-10-19 13:59:12 +02:00
(save-excursion
(forward-comment how-many)))
(defun boon-in-string-p ()
2014-10-30 20:12:08 +01:00
"Determine if the point is inside a string."
2014-10-19 13:59:12 +02:00
(nth 3 (syntax-ppss)))
2014-10-23 13:50:16 +02:00
(defun boon-looking-at-line-comment-start-p ()
2014-10-30 20:12:08 +01:00
"Are we looking at a comment-start?"
2014-10-19 13:59:12 +02:00
(interactive)
2015-10-24 21:33:38 +02:00
(and (bound-and-true-p comment-start)
2014-10-19 13:59:12 +02:00
(looking-at comment-start)
(not (boon-in-string-p))))
(defun boon-end-of-line ()
2014-10-30 20:12:08 +01:00
"Intelligently jump to the end of line.
This function toggles between jumping to 1. the last character of code on the
2014-10-19 13:59:12 +02:00
line 2. the last non-blank char on the line 3. the true end of
line."
(interactive)
(let* ((orig (point))
(orig-eol (eolp))
(progress (lambda () (and (not (bolp)) (or orig-eol (> (point) orig))))))
(beginning-of-line)
2014-10-23 13:50:16 +02:00
(while (not (or (boon-looking-at-line-comment-start-p) (eolp)))
2014-10-19 13:59:12 +02:00
(forward-char))
;; we're now at the last non-comment character of the line
(skip-chars-backward "\n\t " (line-beginning-position))
;; we're now at the last non-blank, non-comment character of the line
(unless (funcall progress)
(end-of-line)
(skip-chars-backward "\n\t " (line-beginning-position))
;; we're now at the last non-blank character of the line
(unless (funcall progress)
(end-of-line)))))
(defun boon-open-line-and-insert ()
"Open a new line, indented as much as the current one, and switch to insert mode."
(interactive)
(let ((indent-lvl (boon-current-line-indentation)))
2014-10-19 13:59:12 +02:00
(beginning-of-line)
(open-line 1)
(insert (make-string indent-lvl 32))
(boon-set-insert-state)))
(defun boon-open-next-line-and-insert ()
2014-10-31 20:56:24 +01:00
"Open the line after the current one."
2014-10-19 13:59:12 +02:00
(interactive)
(next-logical-line)
(boon-open-line-and-insert))
(defun boon-open-line ()
2014-10-31 20:56:24 +01:00
"Open the line before the current one."
2014-10-19 13:59:12 +02:00
(interactive)
(save-excursion
(let ((line-prefix (boon-line-prefix)))
;; (message "next-line-prefix is %S" next-line-prefix)
(open-line 1)
2015-06-21 22:43:25 +02:00
(when (string-blank-p line-prefix)
2014-10-19 13:59:12 +02:00
(progn
(forward-char 1)
(insert line-prefix))))))
(defun boon-switch-mark ()
2014-10-30 20:12:08 +01:00
"If mark active, switch point and mark, otherwise pop mark from mark ring."
2014-10-19 13:59:12 +02:00
(interactive)
(if mark-active
(exchange-point-and-mark)
(progn
(goto-char (mark))
(pop-mark))))
(defun boon-switch-mark-quick ()
2014-10-31 20:56:24 +01:00
"Pop the mark ring until we find ourselves on a different line."
2014-10-19 13:59:12 +02:00
(interactive)
(let ((orig-line (line-number-at-pos)))
(while (> 1 (abs (- orig-line (line-number-at-pos))))
(goto-char (mark))
(pop-mark))))
2014-10-30 20:12:08 +01:00
(defun boon-split-line ()
"Split the current line."
(interactive)
(let ((indent-col (min (boon-current-line-indentation) (current-column))))
;; kill the extra spaces
(save-excursion
(delete-region (progn
(skip-chars-forward "\n\t " (line-end-position))
(point))
(progn
(skip-chars-backward "\n\t " (line-beginning-position))
(point))))
2014-10-30 20:12:08 +01:00
(newline)
(insert (make-string indent-col ?\ ))))
2014-10-19 13:59:12 +02:00
(defun boon-newline-dwim ()
2014-10-30 20:12:08 +01:00
"Insert a new line do-what-i-mean style."
2014-10-19 13:59:12 +02:00
(interactive)
2015-06-21 22:43:25 +02:00
(if (and (not (eolp)) (string-blank-p (boon-line-prefix)))
2014-10-19 13:59:12 +02:00
(call-interactively 'boon-open-line)
2014-10-30 20:12:08 +01:00
(boon-split-line)))
2015-10-14 22:55:39 +02:00
(defun boon-lay-multiple-cursors (place-cursor regs)
2015-10-24 21:33:38 +02:00
"Create multiple cursor regions.
This is done by calling PLACE-CURSOR for each element of REGS.
2015-10-14 22:55:39 +02:00
If there is more than one, use mc/create-fake-cursor-at-point."
(mc/remove-fake-cursors)
(dolist (reg (cdr regs))
(funcall place-cursor reg)
(mc/create-fake-cursor-at-point))
(funcall place-cursor (car regs))
2015-10-15 22:00:16 +02:00
(mc/maybe-multiple-cursors-mode))
2015-10-14 22:55:39 +02:00
(defun boon-mark-region (regs)
2015-10-14 22:55:39 +02:00
"Mark the regions REGS."
(interactive (list (boon-spec-region "mark")))
2015-10-14 22:55:39 +02:00
(boon-lay-multiple-cursors (lambda (reg)
2015-10-30 09:17:00 +01:00
(set-mark (boon-reg-mark reg))
(goto-char (boon-reg-point reg))) regs)
2014-10-19 13:59:12 +02:00
(activate-mark))
(defun boon-end-of-region (regs)
"Move the point the end region REGS."
(interactive (list (boon-spec-region "go to end")))
2014-10-19 13:59:12 +02:00
(dolist (reg regs)
2015-10-30 09:17:00 +01:00
(goto-char (boon-reg-end reg))))
2014-10-19 13:59:12 +02:00
(defun boon-beginning-of-region (regs)
"Move the point to the beginning region REGS."
2015-10-14 22:55:39 +02:00
(interactive (list (boon-spec-region "go to beginning")))
2014-10-19 13:59:12 +02:00
(dolist (reg regs)
2015-10-30 09:17:00 +01:00
(goto-char (boon-reg-begin reg))))
2014-10-19 13:59:12 +02:00
(defun boon-take-region (regs)
"Kill the region given as REGS."
(interactive (list (boon-spec-region "take")))
2015-10-15 22:00:16 +02:00
(dolist (reg (mapcar 'boon-reg-to-markers regs))
2015-10-30 09:17:00 +01:00
(kill-region (boon-reg-begin reg) (boon-reg-end reg))))
2014-10-19 13:59:12 +02:00
(defun boon-treasure-region (regs)
2014-10-30 20:12:08 +01:00
"Copy (kill-ring-save) the regions REGS."
(interactive (list (boon-spec-region "treasure")))
2014-10-19 13:59:12 +02:00
(dolist (reg regs)
2015-10-30 09:17:00 +01:00
(kill-ring-save (boon-reg-begin reg) (boon-reg-end reg))))
2014-10-19 13:59:12 +02:00
(defun boon-substitute-region (regs)
2015-10-14 22:55:39 +02:00
"Kill the regions REGS, and switch to insertion mode."
(interactive (list (boon-spec-region "replace")))
2015-10-15 22:00:16 +02:00
(let ((markers (mapcar 'boon-reg-to-markers regs)))
;; use markers so that deleting things does not mess the positions
(boon-take-region regs)
(deactivate-mark t)
(boon-lay-multiple-cursors (lambda (reg)
2015-10-30 09:17:00 +01:00
(goto-char (boon-reg-point reg)))
2015-10-15 22:00:16 +02:00
markers)
(boon-set-insert-state)))
2014-10-19 13:59:12 +02:00
2014-10-28 14:33:55 +01:00
(defun boon-replace-by-character (replacement)
"Replace the character at point, or region if it is active, by the REPLACEMENT character."
2014-10-19 13:59:12 +02:00
(interactive "cType the character to use as a replacement")
(if (use-region-p)
(delete-region (region-beginning) (region-end))
(delete-char 1))
2014-10-19 13:59:12 +02:00
(insert replacement))
(defun boon-quote-character (char)
2014-10-28 14:47:17 +01:00
"Execute the command bound to the character CHAR if boon was not enabled."
2014-10-19 13:59:12 +02:00
(interactive "cThe character to insert or command to execute")
(let ((cmd
2014-11-05 13:31:13 +01:00
(or (and (current-local-map) (lookup-key (current-local-map) (vector char)))
(lookup-key (current-global-map) (vector char)))))
2014-10-19 13:59:12 +02:00
(setq last-command-event char)
(message (format "Executing the command bound to %c" char))
(call-interactively cmd nil [char])))
2014-10-28 14:47:17 +01:00
2014-10-30 20:12:08 +01:00
(defun boon-unhighlight ()
"Pop a highlight regexp."
(interactive)
(when (bound-and-true-p hi-lock-interactive-patterns)
(hi-lock-unface-buffer (car (car hi-lock-interactive-patterns)))))
2014-10-19 13:59:12 +02:00
(defun boon-quit ()
2014-10-28 14:47:17 +01:00
"Exit the current modes we're in until no special state is remaining."
2014-10-19 13:59:12 +02:00
(interactive)
(cond
((use-region-p)
(message "Deactivated region (use ' to reactivate)")
(deactivate-mark))
((bound-and-true-p multiple-cursors-mode)
(message "Exitted from multiple cursors")
(multiple-cursors-mode 0))
((bound-and-true-p hi-lock-interactive-patterns)
(message "Removed highlighting")
(boon-unhighlight))
(t
(keyboard-quit))))
(defun boon-stuff-at-point ()
2015-10-24 21:33:38 +02:00
"Return a meaningful piece of text around at point.
If no such text exists, throw an error."
2014-10-19 13:59:12 +02:00
(interactive)
(if (use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(or (thing-at-point 'symbol)
(error "Nothing relevant at point; move to a symbol or select a region"))))
2015-05-27 00:03:53 +02:00
;; TODO: remove
(require 'skeleton)
(setq skeleton-pair t)
2014-10-19 13:59:12 +02:00
2014-10-23 13:58:55 +02:00
(defun boon-empty-pair-p ()
2014-10-19 13:59:12 +02:00
"Is the point at the middle of an empty pair of matched parens?"
(interactive)
2015-05-27 00:03:53 +02:00
(declare (obsolete "emacs 24.5 electric pair mode is good enough" "20150527"))
2014-10-19 13:59:12 +02:00
(eq (caddr
(assq (preceding-char)
(or skeleton-pair-alist skeleton-pair-default-alist)))
(following-char)))
2014-10-23 13:58:55 +02:00
(defun boon-empty-quotes-p ()
2014-10-19 13:59:12 +02:00
"Is the point in the middle of an empty pair of quotes?"
(interactive)
2015-05-27 00:03:53 +02:00
(declare (obsolete "emacs 24.5 electric pair mode is good enough" "20150527"))
2014-10-19 13:59:12 +02:00
(and (eq (preceding-char) (following-char))
(member (following-char) '(?\" ?\'))))
2014-10-23 13:58:55 +02:00
(defun boon-smart-insert-backspace2 ()
2014-10-19 13:59:12 +02:00
(interactive)
2015-05-27 00:03:53 +02:00
(declare (obsolete "emacs 24.5 electric pair mode is good enough" "20150527"))
2014-10-23 13:58:55 +02:00
(when (or (boon-empty-pair-p) (boon-empty-quotes-p))
2014-10-19 13:59:12 +02:00
(delete-char 1))
(backward-delete-char-untabify 1))
2014-10-23 13:58:55 +02:00
(defun boon-self-insert-quote ()
"Insert doubled quote.
unless: 1. the previous character is a backslash, in which case a
single quote is inserted or 2. the next character is a quote in
which case the cursor simply jumps over it."
2014-10-19 13:59:12 +02:00
(interactive)
2015-05-27 00:03:53 +02:00
(declare (obsolete "emacs 24.5 electric pair mode is good enough" "20150527"))
2014-10-19 13:59:12 +02:00
(cond
((equal (this-command-keys) (make-string 1 (following-char)))
(forward-char 1))
((eq (preceding-char) ?\\)
(self-insert-command 1))
(t
(self-insert-command 2)
(backward-char 1))))
2014-10-30 20:12:08 +01:00
(provide 'boon-main)
;;; boon-main.el ends here
2014-10-19 13:59:12 +02:00