Skip to content

Commit

Permalink
Merge 8486f82 into f5e6cf4
Browse files Browse the repository at this point in the history
  • Loading branch information
Ergus committed Jun 25, 2019
2 parents f5e6cf4 + 8486f82 commit a2ef354
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 50 deletions.
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -247,7 +247,7 @@ The idea is that if you can mark a thing then you know both where the
thing start and ends. Thus you can not only perform an action on the
entire thing, but also from point to the begining or end of the thing.

Similairly if you have have a pair of commands that move to the
Similarly if you have have a pair of commands that move to the
beginning and end of a thing you can use the two in unison to mark the
entire thing.

Expand Down Expand Up @@ -288,3 +288,13 @@ When a prefix argument is specified before a paired movement command
(begin and end are treated the same) the two commands are used to
establish a region. For instance both <kbd>M-w , f</kbd> and <kbd>M-w . f</kbd> will save the
current word to the kill ring.

## Mode-line Color indicator

A variable `composable-mode-line-color` is a variable defined to
change the mode-line background color when the composable mode is
active. The default value for this variable is "cyan" but it can be
set to any valid color.

If you don't want a color change in the mode-line, then just set this
variable to `nil`.
2 changes: 1 addition & 1 deletion composable-mark.el
Expand Up @@ -27,7 +27,7 @@
;;; Code:

(defun composable-mark-join (arg)
"Mark the whitespace seperating lines.
"Mark the whitespace separating lines.
Between the line above if ARG is negative otherwise below."
(interactive "p")
(forward-line arg)
Expand Down
132 changes: 84 additions & 48 deletions composable.el
Expand Up @@ -68,6 +68,9 @@
"Repeat the last excuted action by repressing the last key."
:type 'boolean)

(defcustom composable-repeat-copy-save-last t
"Keep only the last copied text in the `kill-ring'.")

(defcustom composable-object-cursor 'composable-half-cursor
"Use a custom face for the cursor when in object mode.
This can be either a function or any value accepted by
Expand All @@ -76,11 +79,16 @@ This can be either a function or any value accepted by
(defcustom composable-twice-mark 'composable-mark-line
"Thing to mark when a composable command is called twice successively.")

(defcustom composable-mode-line-color "cyan"
"Color for mode-line background when composable is active."
:type 'color)

(defface easy-kill-selection '((t (:inherit secondary-selection)))
"Faced used to highlight kill candidate.")

(defvar composable--saved-mode-line-color nil)
(defvar composable--command nil)
(defvar composable--skip-first)
(defvar composable--count 0) ;; Count the repeated times
(defvar composable--prefix-arg nil)
(defvar composable--start-point)
(defvar composable--fn-pairs (make-hash-table :test 'equal))
Expand All @@ -93,7 +101,7 @@ The returned function will ask for an object, mark the region it
specifies and call COMMAND on the region."
(lambda (arg)
(interactive "P")
(cond (mark-active
(cond ((region-active-p)
(call-interactively command))
(composable-object-mode
(setq this-command composable-twice-mark)
Expand Down Expand Up @@ -134,8 +142,9 @@ For each function named foo a function name composable-foo is created."
"Call COMMAND if set then go to POINT-MARK marker."
(when (commandp command)
(let ((current-prefix-arg composable--command-prefix))
(call-interactively command))
(goto-char (marker-position point-mark))))
(activate-mark)
(call-interactively command)
(goto-char (marker-position point-mark)))))

(defun composable--repeater (point-marker command object direction)
"Preserve point at POINT-MARKER when doing COMMAND on OBJECT in DIRECTION."
Expand All @@ -147,6 +156,7 @@ For each function named foo a function name composable-foo is created."
(let ((current-prefix-arg direction))
(call-interactively object))
(set-marker point-marker (point))
(setq composable--count (1+ composable--count))
(composable--call-excursion command composable--start-point)))

(defun composable--direction (arg)
Expand All @@ -165,6 +175,12 @@ For each function named foo a function name composable-foo is created."
'(universal-argument digit-argument negative-argument
composable-begin-argument composable-end-argument))

(defun composable--exit ()
"Actions to perform every time composable exits."
(set-marker composable--start-point nil)
(when composable--saved-mode-line-color
(set-face-attribute 'mode-line nil :background composable--saved-mode-line-color)))

(defun composable--activate-repeat (object point-marker)
"Activate repeat map on OBJECT preserving point at POINT-MARKER."
(interactive)
Expand All @@ -175,7 +191,8 @@ For each function named foo a function name composable-foo is created."
t
(lambda ()
(set-marker point-marker nil)
(set-marker composable--start-point nil))))
(composable--exit)))
)

(defun composable--handle-prefix (command pairs)
"Handle prefix arg where the COMMAND is paired in PAIRS."
Expand All @@ -186,12 +203,14 @@ For each function named foo a function name composable-foo is created."
(defun composable--post-command-hook-handler ()
"Called after each command when composable-object-mode is on."
(cond
(composable--skip-first
(setq composable--skip-first nil))
((= composable--count 0)
(setq composable--count 1))
((and (not (member this-command composable--arguments)) ;; detect prefix < 25.1
(not (eq last-command this-command))) ;; in 25.1 prefix args don't change `this-command'
(when composable--prefix-arg (composable--handle-prefix this-command composable--fn-pairs))
(when composable-repeat (composable--activate-repeat this-command (point-marker)))
(when composable--prefix-arg
(composable--handle-prefix this-command composable--fn-pairs))
(when composable-repeat
(composable--activate-repeat this-command (point-marker)))
(composable--call-excursion composable--command composable--start-point)
(composable-object-mode -1))))

Expand Down Expand Up @@ -237,6 +256,10 @@ For each function named foo a function name composable-foo is created."
(lambda (beg end)
(interactive "r")
(let ((o (make-overlay beg end)))
;; Pop kill ring before repeating
(when (and (> composable--count 1)
composable-repeat-copy-save-last)
(pop kill-ring))
(copy-region-as-kill beg end)
(setq composable--overlay o)
(overlay-put o 'priority 999)
Expand All @@ -247,49 +270,60 @@ For each function named foo a function name composable-foo is created."
"Composable mode."
:lighter "Object "
:keymap
'(((kbd "1") . digit-argument)
((kbd "2") . digit-argument)
((kbd "3") . digit-argument)
((kbd "4") . digit-argument)
((kbd "5") . digit-argument)
((kbd "6") . digit-argument)
((kbd "7") . digit-argument)
((kbd "8") . digit-argument)
((kbd "9") . digit-argument)
((kbd "-") . negative-argument)
((kbd ".") . composable-end-argument)
((kbd ",") . composable-begin-argument)
((kbd "a") . move-beginning-of-line)
((kbd "e") . move-end-of-line)
((kbd "f") . forward-word)
((kbd "b") . backward-word)
((kbd "u") . mark-whole-buffer)
((kbd "n") . next-line)
((kbd "p") . previous-line)
((kbd "l") . composable-mark-line)
((kbd "{") . backward-paragraph)
((kbd "}") . forward-paragraph)
((kbd "s") . mark-sexp)
((kbd "w") . composable-mark-word)
((kbd "y") . composable-mark-symbol)
((kbd "h") . mark-paragraph)
((kbd "m") . back-to-indentation)
((kbd "j") . composable-mark-join)
((kbd "o") . composable-mark-up-list)
((kbd "g") . composable-object-mode)
((kbd "C-g") . composable-object-mode))
`(("1" . digit-argument)
("2" . digit-argument)
("3" . digit-argument)
("4" . digit-argument)
("5" . digit-argument)
("6" . digit-argument)
("7" . digit-argument)
("8" . digit-argument)
("9" . digit-argument)
("-" . negative-argument)
("." . composable-end-argument)
("," . composable-begin-argument)
("a" . move-beginning-of-line)
("e" . move-end-of-line)
("f" . forward-word)
("b" . backward-word)
("u" . mark-whole-buffer)
("n" . next-line)
("p" . previous-line)
("l" . composable-mark-line)
("{" . backward-paragraph)
("}" . forward-paragraph)
("s" . mark-sexp)
("w" . composable-mark-word)
("y" . composable-mark-symbol)
("h" . mark-paragraph)
("m" . back-to-indentation)
("j" . composable-mark-join)
("o" . composable-mark-up-list)
("g" . composable-object-mode)
(,(kbd "C-g") . composable-object-mode))
(if composable-object-mode
(progn
(setq composable--saved-cursor cursor-type)
(if (and composable-mode-line-color
(color-supported-p composable-mode-line-color))
(progn (setq composable--saved-mode-line-color (face-attribute 'mode-line :background))
(set-face-attribute 'mode-line nil :background composable-mode-line-color))
(setq composable--saved-mode-line-color nil))
(composable--set-cursor composable-object-cursor)
(if (not mark-active) (push-mark nil t))
(setq composable--start-point (point-marker))
(setq composable--skip-first t)
(add-hook 'post-command-hook 'composable--post-command-hook-handler))
(when (not mark-active)
(push-mark nil t))
(setq composable--start-point (point-marker)
composable--count 0)
(add-hook 'post-command-hook 'composable--post-command-hook-handler)
(message "Composable mode: %s" this-command))
(setq cursor-type composable--saved-cursor)
(remove-hook 'post-command-hook 'composable--post-command-hook-handler)
(setq composable--prefix-arg nil)
(setq composable--command nil)))
(setq composable--command nil)
(when (or (called-interactively-p)
(not composable-repeat))
(composable--exit)
(deactivate-mark))))

;;;###autoload
(define-minor-mode composable-mode
Expand All @@ -306,13 +340,15 @@ For each function named foo a function name composable-foo is created."

(defun composable--deactivate-mark-hook-handler ()
"Leave object mode when the mark is disabled."
(when composable-object-mode (composable-object-mode -1)))
(when composable-object-mode
(composable-object-mode -1)))

(defun composable--set-mark-command-advice (arg)
"Advice for `set-mark-command'.
Activates composable-object-mode unless ARG is non-nil."
(unless (or composable-object-mode arg)
(composable-object-mode)))
(unless (or composable-object-mode
arg)
(composable-object-mode 1)))

;;;###autoload
(define-minor-mode composable-mark-mode
Expand Down

0 comments on commit a2ef354

Please sign in to comment.