Skip to content

Commit fb61ce5

Browse files
authored
Add visual-line versions of some MEOW operations/entities (#416)
- `meow-visual-line` as visual-line counterpart to `meow-line` - `visual-line` as a new 'meow-thing - `meow-open-below-visual` as visual-line counterpart to `meow-open-below` - `meow-open-above-visual` as visual-line counterpart to `meow-open-above`
1 parent 9860a7a commit fb61ce5

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

meow-command.el

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,19 @@ This command supports `meow-selection-command-fallback'."
468468
;; (insert "\n"))
469469
(indent-according-to-mode)))
470470

471+
(defun meow-open-above-visual ()
472+
"Open a newline above and switch to INSERT state."
473+
(interactive)
474+
(if meow--temp-normal
475+
(progn
476+
(message "Quit temporary normal mode")
477+
(meow--switch-state 'motion))
478+
(meow--switch-state 'insert)
479+
(goto-char (meow--visual-line-beginning-position))
480+
(save-mark-and-excursion
481+
(newline))
482+
(indent-according-to-mode)))
483+
471484
(defun meow-open-below ()
472485
"Open a newline below and switch to INSERT state."
473486
(interactive)
@@ -479,6 +492,17 @@ This command supports `meow-selection-command-fallback'."
479492
(goto-char (line-end-position))
480493
(meow--execute-kbd-macro "RET")))
481494

495+
(defun meow-open-below-visual ()
496+
"Open a newline below and switch to INSERT state."
497+
(interactive)
498+
(if meow--temp-normal
499+
(progn
500+
(message "Quit temporary normal mode")
501+
(meow--switch-state 'motion))
502+
(meow--switch-state 'insert)
503+
(goto-char (meow--visual-line-end-position))
504+
(meow--execute-kbd-macro "RET")))
505+
482506
(defun meow-change ()
483507
"Kill current selection and switch to INSERT state.
484508
@@ -944,6 +968,87 @@ This command will expand line selection."
944968
(meow--select (> orig-p beg)))
945969
(recenter)))
946970

971+
;; visual line versions
972+
(defun meow--visual-line-beginning-position ()
973+
(save-excursion
974+
(beginning-of-visual-line)
975+
(point)))
976+
977+
(defun meow--visual-line-end-position ()
978+
(save-excursion
979+
(end-of-visual-line)
980+
(point)))
981+
982+
(defun meow--forward-visual-line-1 ()
983+
(let ((orig (point)))
984+
(line-move-visual 1)
985+
(if meow--expanding-p
986+
(progn
987+
(goto-char (meow--visual-line-end-position))
988+
(meow--visual-line-end-position))
989+
(when (< orig (meow--visual-line-beginning-position))
990+
(meow--visual-line-beginning-position)))))
991+
992+
(defun meow--backward-visual-line-1 ()
993+
(line-move-visual -1)
994+
(meow--visual-line-beginning-position))
995+
996+
(defun meow-visual-line (n &optional expand)
997+
"Select the current visual line, eol is not included.
998+
999+
Create selection with type (expand . line).
1000+
For the selection with type (expand . line), expand it by line.
1001+
For the selection with other types, cancel it.
1002+
1003+
Prefix:
1004+
numeric, repeat times.
1005+
"
1006+
(interactive "p")
1007+
(unless (or expand (equal '(expand . line) (meow--selection-type)))
1008+
(meow--cancel-selection))
1009+
(let* ((orig (mark t))
1010+
(n (if (meow--direction-backward-p)
1011+
(- n)
1012+
n))
1013+
(forward (> n 0)))
1014+
(cond
1015+
((region-active-p)
1016+
(let (p)
1017+
(save-mark-and-excursion
1018+
(line-move-visual n)
1019+
(goto-char
1020+
(if forward
1021+
(setq p (meow--visual-line-end-position))
1022+
(setq p (meow--visual-line-beginning-position)))))
1023+
(thread-first
1024+
(meow--make-selection '(expand . line) orig p expand)
1025+
(meow--select))
1026+
(meow--maybe-highlight-num-positions '(meow--backward-visual-line-1 . meow--forward-visual-line-1))))
1027+
(t
1028+
(let ((m (if forward
1029+
(meow--visual-line-beginning-position)
1030+
(meow--visual-line-end-position)))
1031+
(p (save-mark-and-excursion
1032+
(if forward
1033+
(progn
1034+
(line-move-visual (1- n))
1035+
(meow--visual-line-end-position))
1036+
(progn
1037+
(line-move-visual (1+ n))
1038+
(when (meow--empty-line-p)
1039+
(backward-char 1))
1040+
(meow--visual-line-beginning-position))))))
1041+
(thread-first
1042+
(meow--make-selection '(expand . line) m p expand)
1043+
(meow--select))
1044+
(meow--maybe-highlight-num-positions '(meow--backward-visual-line-1 . meow--forward-visual-line-1)))))))
1045+
1046+
(defun meow-visual-line-expand (n)
1047+
"Like `meow-line', but always expand."
1048+
(interactive "p")
1049+
(meow-visual-line n t))
1050+
1051+
9471052
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9481053
;;; BLOCK
9491054
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

meow-thing.el

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ The thing `string' is not available in Emacs 27.'"
8282
(cons (save-mark-and-excursion (back-to-indentation) (point))
8383
(line-end-position)))
8484

85+
(defun meow--inner-of-visual-line ()
86+
(cons (meow--visual-line-beginning-position)
87+
(meow--visual-line-end-position)))
88+
8589
;;; Registry
8690

8791
(defvar meow--thing-registry nil
@@ -314,6 +318,8 @@ PAIR-EXPR contains two string token lists. The tokens in first
314318

315319
(meow-thing-register 'line #'meow--inner-of-line 'line)
316320

321+
(meow-thing-register 'visual-line #'meow--inner-of-visual-line #'meow--inner-of-visual-line)
322+
317323
(defun meow--parse-inner-of-thing-char (ch)
318324
(when-let ((ch-to-thing (assoc ch meow-char-thing-table)))
319325
(meow--parse-range-of-thing (cdr ch-to-thing) t)))

meow-var.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ This will affect how selection is displayed."
118118
(?b . buffer)
119119
(?p . paragraph)
120120
(?l . line)
121+
(?v . visual-line)
121122
(?d . defun)
122123
(?. . sentence))
123124
"Mapping from char to thing."

0 commit comments

Comments
 (0)