;; Selecting text in Emacs;; Shift selection mode;; C-SPC + motion keys;; M-S-2 ;; Xah-Lee - select line with one keystroke
Aliquam erat volutpat. Nunc eleifend leo vitae magna. In id erat non orci commodo lobortis. Proin neque massa, cursus ut, gravida ut, lobortis eget, lacus. Sed diam. Praesent fermentum tempor tellus. Nullam tempus. Mauris ac felis vel velit tristique imperdiet. (Donec at pede.) Etiam vel neque nec dui dignissim bibendum. Vivamus id enim. Phasellus neque orci, porta a, aliquet quis, semper a, massa. Phasellus purus. Pellentesque tristique imperdiet tortor. Nam euismod tellus id erat.
(defunxah-select-line ()
"Select current line. If region is active, extend selection downwardby line. If `visual-line-mode' is on, consider line as visual line.URL `http://xahlee.info/emacs/emacs/modernization_mark-word.html'Version: 2017-11-01 2021-03-19 2023-07-16"
(interactive)
(if (region-active-p)
(if visual-line-mode
(let ((xp1 (point)))
(end-of-visual-line 1)
(when (eq xp1 (point))
(end-of-visual-line 2)))
(progn
(forward-line 1)
(end-of-line)))
(if visual-line-mode
(progn (beginning-of-visual-line)
(push-mark (point) t t)
(end-of-visual-line))
(progn
(push-mark (line-beginning-position) t t)
(end-of-line)))))
(defunxah-select-block ()
"Select the current/next block plus 1 blankline.If region is active, extend selection downward by block.URL `http://xahlee.info/emacs/emacs/modernization_mark-word.html'Version: 2019-12-26 2021-04-04 2021-08-13"
(interactive)
(if (region-active-p)
(re-search-forward "\n[ \t]*\n[ \t]*\n*" nil 1)
(progn
(skip-chars-forward " \n\t")
(when (re-search-backward "\n[ \t]*\n" nil 1)
(goto-char (match-end 0)))
(push-mark (point) t t)
(re-search-forward "\n[ \t]*\n" nil 1))))
;; Here's a multi-purpose command that select different text depending on where cursor is, and if called repeatedly, extend selection.;; This command is currently most useful in lisp code. Or, place cursor on a bracket and select the whole including bracket and text in between. (bracket here includes parenthesis and any type of quotation marks.)
(defunxah-extend-selection ()
"Select the current word, bracket/quote expression, or expand selection. Subsequent calls expands the selection.when there is no selection,• If cursor is on any type of bracket (including parenthesis, quotation mark), select whole bracketed thing including bracket• else, select current word.when there is a selection, the selection extension behavior is still experimental. But when cursor is on a any type of bracket (parenthesis, quote), it extends selection to outer bracket.URL `http://xahlee.info/emacs/emacs/modernization_mark-word.html'Version: 2020-02-04 2023-07-22 2023-07-23"
(interactive)
(if (region-active-p)
(progn
(let ((xrb (region-beginning)) (xre (region-end)))
(goto-char xrb)
(cond
((looking-at "\\s(")
(if (eq (nth 0 (syntax-ppss)) 0)
(progn
;; (message "left bracket, depth 0.")
(end-of-line) ; select current line
(push-mark (line-beginning-position) t t))
(progn
;; (message "left bracket, depth not 0")
(up-list -1 t t)
(mark-sexp))))
((eq xrb (line-beginning-position))
(progn
(goto-char xrb)
(let ((xfirstLineEndPos (line-end-position)))
(cond
((eq xre xfirstLineEndPos)
(progn
;; (message "exactly 1 line. extend to next whole line." )
(forward-line 1)
(end-of-line)))
((< xre xfirstLineEndPos)
(progn
;; (message "less than 1 line. complete the line." )
(end-of-line)))
((> xre xfirstLineEndPos)
(progn
;; (message "beginning of line, but end is greater than 1st end of line" )
(goto-char xre)
(if (eq (point) (line-end-position))
(progn
;; (message "exactly multiple lines" )
(forward-line 1)
(end-of-line))
(progn
;; (message "multiple lines but end is not eol. make it so" )
(goto-char xre)
(end-of-line)))))
(t (error "%s: logic error 42946" real-this-command))))))
((and (> (point) (line-beginning-position)) (<= (point) (line-end-position)))
(progn
;; (message "less than 1 line" )
(end-of-line) ; select current line
(push-mark (line-beginning-position) t t)))
(t
;; (message "last resort" )
nil))))
(progn
(cond
((looking-at "\\s(")
;; (message "left bracket")
(mark-sexp)) ; left bracket
((looking-at "\\s)")
;; (message "right bracket")
(backward-up-list) (mark-sexp))
((looking-at "\\s\"")
;; (message "string quote")
(mark-sexp)) ; string quote;; ((and (eq (point) (line-beginning-position)) (not (looking-at "\n")));; (message "beginning of line and not empty");; (end-of-line);; (push-mark (line-beginning-position) t t))
(
;; (prog2 (backward-char) (looking-at "[-_a-zA-Z0-9]") (forward-char))
(looking-back "[-_a-zA-Z0-9]" (max (- (point) 1) (point-min)))
;; (message "left is word or symbol")
(skip-chars-backward "-_a-zA-Z0-9")
;; (re-search-backward "^\\(\\sw\\|\\s_\\)" nil t)
(push-mark)
(skip-chars-forward "-_a-zA-Z0-9")
(setq mark-active t)
;; (exchange-point-and-mark)
)
((and (looking-at "[:blank:]")
(prog2 (backward-char) (looking-at "[:blank:]") (forward-char)))
;; (message "left and right both space" )
(skip-chars-backward "[:blank:]") (push-mark (point) t t)
(skip-chars-forward "[:blank:]"))
((and (looking-at "\n")
(eq (char-before) 10))
;; (message "left and right both newline")
(skip-chars-forward "\n")
(push-mark (point) t t)
(re-search-forward "\n[ \t]*\n")) ; between blank lines, select next block
(t
;; (message "just mark sexp" )
(mark-sexp)
(exchange-point-and-mark))
;;
))))
(global-set-key (kbd "M-3") 'xah-select-line)
(global-set-key (kbd "M-4") 'xah-select-block)
(global-set-key (kbd "M-5") 'xah-extend-selection)