2019-03-02 01:12:48 -05:00
|
|
|
;;; core/autoload/text.el -*- lexical-binding: t; -*-
|
2017-02-03 08:05:47 -05:00
|
|
|
|
2021-05-16 21:22:06 -04:00
|
|
|
;;;###autoload
|
2020-01-03 02:40:23 -05:00
|
|
|
(defvar doom-point-in-comment-functions ()
|
|
|
|
"List of functions to run to determine if point is in a comment.
|
|
|
|
|
|
|
|
Each function takes one argument: the position of the point. Stops on the first
|
|
|
|
function to return non-nil. Used by `doom-point-in-comment-p'.")
|
|
|
|
|
2021-05-16 21:22:06 -04:00
|
|
|
;;;###autoload
|
2020-01-03 02:40:23 -05:00
|
|
|
(defvar doom-point-in-string-functions ()
|
|
|
|
"List of functions to run to determine if point is in a string.
|
|
|
|
|
|
|
|
Each function takes one argument: the position of the point. Stops on the first
|
|
|
|
function to return non-nil. Used by `doom-point-in-string-p'.")
|
|
|
|
|
2018-02-14 05:10:48 -05:00
|
|
|
;;;###autoload
|
2018-09-21 00:52:53 -04:00
|
|
|
(defun doom-surrounded-p (pair &optional inline balanced)
|
2018-02-14 05:10:48 -05:00
|
|
|
"Returns t if point is surrounded by a brace delimiter: {[(
|
|
|
|
|
|
|
|
If INLINE is non-nil, only returns t if braces are on the same line, and
|
|
|
|
whitespace is balanced on either side of the cursor.
|
|
|
|
|
|
|
|
If INLINE is nil, returns t if the opening and closing braces are on adjacent
|
|
|
|
lines, above and below, with only whitespace in between."
|
2018-09-21 00:52:53 -04:00
|
|
|
(when pair
|
2018-02-14 16:18:55 -05:00
|
|
|
(let ((beg (plist-get pair :beg))
|
|
|
|
(end (plist-get pair :end))
|
|
|
|
(pt (point)))
|
|
|
|
(when (and (> pt beg) (< pt end))
|
|
|
|
(when-let* ((cl (plist-get pair :cl))
|
|
|
|
(op (plist-get pair :op)))
|
2018-03-05 23:12:20 -05:00
|
|
|
(and (not (string= op ""))
|
|
|
|
(not (string= cl ""))
|
2018-02-14 16:18:55 -05:00
|
|
|
(let ((nbeg (+ (length op) beg))
|
|
|
|
(nend (- end (length cl))))
|
|
|
|
(let ((content (buffer-substring-no-properties nbeg nend)))
|
|
|
|
(and (string-match-p (format "[ %s]*" (if inline "" "\n")) content)
|
|
|
|
(or (not balanced)
|
|
|
|
(= (- pt nbeg) (- nend pt))))))))))))
|
2018-02-14 05:10:48 -05:00
|
|
|
|
2019-07-27 22:56:49 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom-point-in-comment-p (&optional pos)
|
|
|
|
"Return non-nil if POS is in a comment.
|
|
|
|
POS defaults to the current position."
|
2020-01-03 02:40:23 -05:00
|
|
|
(let ((pos (or pos (point))))
|
2021-05-16 21:22:06 -04:00
|
|
|
(if doom-point-in-comment-functions
|
|
|
|
(run-hook-with-args-until-success 'doom-point-in-comment-functions pos)
|
|
|
|
(nth 4 (syntax-ppss pos)))))
|
2019-07-27 22:56:49 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-point-in-string-p (&optional pos)
|
|
|
|
"Return non-nil if POS is in a string."
|
|
|
|
;; REVIEW Should we cache `syntax-ppss'?
|
2020-01-03 02:40:23 -05:00
|
|
|
(let ((pos (or pos (point))))
|
2021-05-16 21:22:06 -04:00
|
|
|
(if doom-point-in-string-functions
|
|
|
|
(run-hook-with-args-until-success 'doom-point-in-string-functions pos)
|
|
|
|
(nth 3 (syntax-ppss pos)))))
|
2019-07-27 22:56:49 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-point-in-string-or-comment-p (&optional pos)
|
|
|
|
"Return non-nil if POS is in a string or comment."
|
|
|
|
(or (doom-point-in-string-p pos)
|
|
|
|
(doom-point-in-comment-p pos)))
|
|
|
|
|
2020-01-03 02:39:47 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom-region-active-p ()
|
|
|
|
"Return non-nil if selection is active.
|
|
|
|
Detects evil visual mode as well."
|
|
|
|
(declare (side-effect-free t))
|
|
|
|
(or (use-region-p)
|
|
|
|
(and (bound-and-true-p evil-local-mode)
|
|
|
|
(evil-visual-state-p))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-region-beginning ()
|
|
|
|
"Return beginning position of selection.
|
|
|
|
Uses `evil-visual-beginning' if available."
|
|
|
|
(declare (side-effect-free t))
|
2021-05-23 21:48:38 -04:00
|
|
|
(or (and (bound-and-true-p evil-local-mode)
|
|
|
|
(markerp evil-visual-beginning)
|
|
|
|
(marker-position evil-visual-beginning))
|
|
|
|
(region-beginning)))
|
2020-01-03 02:39:47 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-region-end ()
|
|
|
|
"Return end position of selection.
|
|
|
|
Uses `evil-visual-end' if available."
|
|
|
|
(declare (side-effect-free t))
|
|
|
|
(if (bound-and-true-p evil-local-mode)
|
|
|
|
evil-visual-end
|
|
|
|
(region-end)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-thing-at-point-or-region (&optional thing prompt)
|
|
|
|
"Grab the current selection, THING at point, or xref identifier at point.
|
|
|
|
|
|
|
|
Returns THING if it is a string. Otherwise, if nothing is found at point and
|
|
|
|
PROMPT is non-nil, prompt for a string (if PROMPT is a string it'll be used as
|
|
|
|
the prompting string). Returns nil if all else fails.
|
|
|
|
|
|
|
|
NOTE: Don't use THING for grabbing symbol-at-point. The xref fallback is smarter
|
|
|
|
in some cases."
|
|
|
|
(declare (side-effect-free t))
|
|
|
|
(cond ((stringp thing)
|
|
|
|
thing)
|
|
|
|
((doom-region-active-p)
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
(doom-region-beginning)
|
|
|
|
(doom-region-end)))
|
|
|
|
(thing
|
|
|
|
(thing-at-point thing t))
|
2020-06-29 15:12:30 -04:00
|
|
|
((require 'xref nil t)
|
2020-08-31 23:11:33 -04:00
|
|
|
;; Eglot, nox (a fork of eglot), and elpy implementations for
|
|
|
|
;; `xref-backend-identifier-at-point' betray the documented purpose of
|
|
|
|
;; the interface. Eglot/nox return a hardcoded string and elpy prepends
|
|
|
|
;; the line number to the symbol.
|
|
|
|
(if (memq (xref-find-backend) '(eglot elpy nox))
|
2020-06-29 15:12:30 -04:00
|
|
|
(thing-at-point 'symbol t)
|
|
|
|
;; A little smarter than using `symbol-at-point', though in most
|
|
|
|
;; cases, xref ends up using `symbol-at-point' anyway.
|
|
|
|
(xref-backend-identifier-at-point (xref-find-backend))))
|
2020-01-03 02:39:47 -05:00
|
|
|
(prompt
|
|
|
|
(read-string (if (stringp prompt) prompt "")))))
|
|
|
|
|
2018-02-14 05:10:48 -05:00
|
|
|
|
|
|
|
;;
|
2019-10-26 13:57:54 -04:00
|
|
|
;;; Commands
|
2018-02-14 05:10:48 -05:00
|
|
|
|
2020-01-01 16:18:50 -05:00
|
|
|
(defun doom--bol-bot-eot-eol (&optional pos)
|
2020-03-27 03:03:37 -04:00
|
|
|
(save-mark-and-excursion
|
2020-02-02 02:05:20 -05:00
|
|
|
(when pos
|
|
|
|
(goto-char pos))
|
|
|
|
(let* ((bol (if visual-line-mode
|
|
|
|
(save-excursion
|
|
|
|
(beginning-of-visual-line)
|
|
|
|
(point))
|
|
|
|
(line-beginning-position)))
|
|
|
|
(bot (save-excursion
|
|
|
|
(goto-char bol)
|
|
|
|
(skip-chars-forward " \t\r")
|
|
|
|
(point)))
|
|
|
|
(eol (if visual-line-mode
|
|
|
|
(save-excursion (end-of-visual-line) (point))
|
|
|
|
(line-end-position)))
|
|
|
|
(eot (or (save-excursion
|
|
|
|
(if (not comment-use-syntax)
|
|
|
|
(progn
|
|
|
|
(goto-char bol)
|
|
|
|
(when (re-search-forward comment-start-skip eol t)
|
|
|
|
(or (match-end 1) (match-beginning 0))))
|
|
|
|
(goto-char eol)
|
|
|
|
(while (and (doom-point-in-comment-p)
|
|
|
|
(> (point) bol))
|
|
|
|
(backward-char))
|
|
|
|
(skip-chars-backward " " bol)
|
2021-03-10 12:30:26 -05:00
|
|
|
(or (eq (char-after) 32)
|
|
|
|
(eolp)
|
|
|
|
(bolp)
|
|
|
|
(forward-char))
|
2020-02-02 02:05:20 -05:00
|
|
|
(point)))
|
|
|
|
eol)))
|
|
|
|
(list bol bot eot eol))))
|
2020-01-01 16:18:50 -05:00
|
|
|
|
|
|
|
(defvar doom--last-backward-pt nil)
|
2017-02-03 08:05:47 -05:00
|
|
|
;;;###autoload
|
2020-01-01 16:18:50 -05:00
|
|
|
(defun doom/backward-to-bol-or-indent (&optional point)
|
2018-02-01 14:43:28 -05:00
|
|
|
"Jump between the indentation column (first non-whitespace character) and the
|
|
|
|
beginning of the line. The opposite of
|
|
|
|
`doom/forward-to-last-non-comment-or-eol'."
|
2020-03-28 00:41:04 -04:00
|
|
|
(interactive "^d")
|
2020-01-01 16:18:50 -05:00
|
|
|
(let ((pt (or point (point))))
|
2020-02-02 02:05:20 -05:00
|
|
|
(cl-destructuring-bind (bol bot _eot _eol)
|
2020-01-01 16:18:50 -05:00
|
|
|
(doom--bol-bot-eot-eol pt)
|
2019-07-12 18:19:28 +02:00
|
|
|
(cond ((> pt bot)
|
|
|
|
(goto-char bot))
|
|
|
|
((= pt bol)
|
2020-01-12 02:09:01 -05:00
|
|
|
(or (and doom--last-backward-pt
|
|
|
|
(= (line-number-at-pos doom--last-backward-pt)
|
|
|
|
(line-number-at-pos pt)))
|
|
|
|
(setq doom--last-backward-pt nil))
|
2020-01-01 16:18:50 -05:00
|
|
|
(goto-char (or doom--last-backward-pt bot))
|
|
|
|
(setq doom--last-backward-pt nil))
|
2019-07-12 18:19:28 +02:00
|
|
|
((<= pt bot)
|
|
|
|
(setq doom--last-backward-pt pt)
|
|
|
|
(goto-char bol))))))
|
2017-02-03 08:05:47 -05:00
|
|
|
|
2020-01-01 16:18:50 -05:00
|
|
|
(defvar doom--last-forward-pt nil)
|
2017-02-03 08:05:47 -05:00
|
|
|
;;;###autoload
|
2020-01-01 16:18:50 -05:00
|
|
|
(defun doom/forward-to-last-non-comment-or-eol (&optional point)
|
2018-02-01 16:35:55 -05:00
|
|
|
"Jumps between the last non-blank, non-comment character in the line and the
|
|
|
|
true end of the line. The opposite of `doom/backward-to-bol-or-indent'."
|
2020-03-28 00:41:04 -04:00
|
|
|
(interactive "^d")
|
2020-01-01 16:18:50 -05:00
|
|
|
(let ((pt (or point (point))))
|
|
|
|
(cl-destructuring-bind (_bol _bot eot eol)
|
|
|
|
(doom--bol-bot-eot-eol pt)
|
|
|
|
(cond ((< pt eot)
|
|
|
|
(goto-char eot))
|
|
|
|
((= pt eol)
|
|
|
|
(goto-char (or doom--last-forward-pt eot))
|
|
|
|
(setq doom--last-forward-pt nil))
|
|
|
|
((>= pt eot)
|
|
|
|
(setq doom--last-backward-pt pt)
|
|
|
|
(goto-char eol))))))
|
2017-02-03 08:05:47 -05:00
|
|
|
|
2019-12-23 17:20:59 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/backward-kill-to-bol-and-indent ()
|
|
|
|
"Kill line to the first non-blank character. If invoked again afterwards, kill
|
|
|
|
line to beginning of line. Same as `evil-delete-back-to-indentation'."
|
|
|
|
(interactive)
|
|
|
|
(let ((empty-line-p (save-excursion (beginning-of-line)
|
|
|
|
(looking-at-p "[ \t]*$"))))
|
|
|
|
(funcall (if (fboundp 'evil-delete)
|
|
|
|
#'evil-delete
|
|
|
|
#'delete-region)
|
|
|
|
(point-at-bol) (point))
|
|
|
|
(unless empty-line-p
|
|
|
|
(indent-according-to-mode))))
|
|
|
|
|
2020-01-01 16:02:08 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/delete-backward-word (arg)
|
|
|
|
"Like `backward-kill-word', but doesn't affect the kill-ring."
|
|
|
|
(interactive "p")
|
|
|
|
(let (kill-ring)
|
2021-07-25 16:26:47 -04:00
|
|
|
(ignore-errors (backward-kill-word arg))))
|
2020-01-01 16:02:08 -05:00
|
|
|
|
2017-02-03 08:05:47 -05:00
|
|
|
;;;###autoload
|
2017-06-08 11:47:56 +02:00
|
|
|
(defun doom/dumb-indent ()
|
2017-02-03 08:05:47 -05:00
|
|
|
"Inserts a tab character (or spaces x tab-width)."
|
|
|
|
(interactive)
|
|
|
|
(if indent-tabs-mode
|
|
|
|
(insert "\t")
|
|
|
|
(let* ((movement (% (current-column) tab-width))
|
|
|
|
(spaces (if (= 0 movement) tab-width (- tab-width movement))))
|
2017-02-19 18:02:40 -05:00
|
|
|
(insert (make-string spaces ? )))))
|
2017-02-03 08:05:47 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/dumb-dedent ()
|
|
|
|
"Dedents the current line."
|
|
|
|
(interactive)
|
|
|
|
(if indent-tabs-mode
|
2017-04-17 02:17:10 -04:00
|
|
|
(call-interactively #'backward-delete-char)
|
2017-09-25 05:05:55 +02:00
|
|
|
(unless (bolp)
|
|
|
|
(save-excursion
|
|
|
|
(when (> (current-column) (current-indentation))
|
|
|
|
(back-to-indentation))
|
|
|
|
(let ((movement (% (current-column) tab-width)))
|
|
|
|
(delete-char
|
|
|
|
(- (if (= 0 movement)
|
|
|
|
tab-width
|
|
|
|
(- tab-width movement)))))))))
|
2017-02-03 08:05:47 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2018-05-20 12:10:03 +02:00
|
|
|
(defun doom/retab (arg &optional beg end)
|
2018-02-14 05:10:48 -05:00
|
|
|
"Converts tabs-to-spaces or spaces-to-tabs within BEG and END (defaults to
|
|
|
|
buffer start and end, to make indentation consistent. Which it does depends on
|
2018-05-20 12:10:03 +02:00
|
|
|
the value of `indent-tab-mode'.
|
|
|
|
|
|
|
|
If ARG (universal argument) is non-nil, retab the current buffer using the
|
|
|
|
opposite indentation style."
|
2021-02-02 02:41:45 +01:00
|
|
|
(interactive "P\nr")
|
2017-02-03 08:05:47 -05:00
|
|
|
(unless (and beg end)
|
|
|
|
(setq beg (point-min)
|
|
|
|
end (point-max)))
|
2018-05-20 12:10:03 +02:00
|
|
|
(let ((indent-tabs-mode (if arg (not indent-tabs-mode) indent-tabs-mode)))
|
|
|
|
(if indent-tabs-mode
|
|
|
|
(tabify beg end)
|
|
|
|
(untabify beg end))))
|
2017-02-03 08:05:47 -05:00
|
|
|
|
2018-09-01 12:30:34 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/delete-trailing-newlines ()
|
|
|
|
"Trim trailing newlines.
|
|
|
|
|
|
|
|
Respects `require-final-newline'."
|
|
|
|
(interactive)
|
2020-04-08 15:29:29 -04:00
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-max))
|
|
|
|
(delete-blank-lines)))
|
2018-09-01 12:30:34 +02:00
|
|
|
|
2019-02-18 01:58:50 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/dos2unix ()
|
|
|
|
"Convert the current buffer to a Unix file encoding."
|
|
|
|
(interactive)
|
|
|
|
(set-buffer-file-coding-system 'undecided-unix nil))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/unix2dos ()
|
|
|
|
"Convert the current buffer to a DOS file encoding."
|
|
|
|
(interactive)
|
|
|
|
(set-buffer-file-coding-system 'undecided-dos nil))
|
|
|
|
|
2019-07-11 17:21:33 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/toggle-indent-style ()
|
|
|
|
"Switch between tabs and spaces indentation style in the current buffer."
|
|
|
|
(interactive)
|
|
|
|
(setq indent-tabs-mode (not indent-tabs-mode))
|
|
|
|
(message "Indent style changed to %s" (if indent-tabs-mode "tabs" "spaces")))
|
|
|
|
|
2019-07-21 23:31:42 +02:00
|
|
|
(defvar editorconfig-lisp-use-default-indent)
|
2019-07-11 17:21:33 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/set-indent-width (width)
|
2019-08-06 14:50:42 -04:00
|
|
|
"Change the indentation size to WIDTH of the current buffer.
|
|
|
|
|
|
|
|
The effectiveness of this command is significantly improved if you have
|
|
|
|
editorconfig or dtrt-indent installed."
|
2019-07-11 17:21:33 +02:00
|
|
|
(interactive
|
|
|
|
(list (if (integerp current-prefix-arg)
|
|
|
|
current-prefix-arg
|
|
|
|
(read-number "New indent size: "))))
|
2019-07-12 16:55:36 +02:00
|
|
|
(setq tab-width width)
|
|
|
|
(setq-local standard-indent width)
|
2019-07-11 17:21:33 +02:00
|
|
|
(when (boundp 'evil-shift-width)
|
|
|
|
(setq evil-shift-width width))
|
|
|
|
(cond ((require 'editorconfig nil t)
|
2019-07-12 16:47:49 +02:00
|
|
|
(let (editorconfig-lisp-use-default-indent)
|
|
|
|
(editorconfig-set-indentation nil width)))
|
2019-07-11 17:21:33 +02:00
|
|
|
((require 'dtrt-indent nil t)
|
|
|
|
(when-let (var (nth 2 (assq major-mode dtrt-indent-hook-mapping-list)))
|
|
|
|
(doom-log "Updated %s = %d" var width)
|
|
|
|
(set var width))))
|
|
|
|
(message "Changed indentation to %d" width))
|
|
|
|
|
2019-02-18 01:58:50 -05:00
|
|
|
|
2018-02-14 05:10:48 -05:00
|
|
|
;;
|
2019-10-26 13:57:54 -04:00
|
|
|
;;; Hooks
|
2018-02-14 05:10:48 -05:00
|
|
|
|
2017-07-03 03:11:54 +02:00
|
|
|
;;;###autoload
|
2019-07-18 15:27:20 +02:00
|
|
|
(defun doom-enable-delete-trailing-whitespace-h ()
|
2018-08-30 20:13:43 +02:00
|
|
|
"Enables the automatic deletion of trailing whitespaces upon file save.
|
|
|
|
|
2018-08-31 13:56:50 +02:00
|
|
|
i.e. enables `ws-butler-mode' in the current buffer."
|
|
|
|
(ws-butler-mode +1))
|
2018-08-30 20:13:43 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2019-07-18 15:27:20 +02:00
|
|
|
(defun doom-disable-delete-trailing-whitespace-h ()
|
2018-08-30 20:13:43 +02:00
|
|
|
"Disables the automatic deletion of trailing whitespaces upon file save.
|
|
|
|
|
2018-08-31 13:56:50 +02:00
|
|
|
i.e. disables `ws-butler-mode' in the current buffer."
|
|
|
|
(ws-butler-mode -1))
|
2019-05-16 15:57:08 -04:00
|
|
|
|
|
|
|
;;;###autoload
|
2019-07-18 15:27:20 +02:00
|
|
|
(defun doom-enable-show-trailing-whitespace-h ()
|
2019-05-16 15:57:08 -04:00
|
|
|
"Enable `show-trailing-whitespace' in the current buffer."
|
|
|
|
(setq-local show-trailing-whitespace t))
|
|
|
|
|
|
|
|
;;;###autoload
|
2019-07-18 15:27:20 +02:00
|
|
|
(defun doom-disable-show-trailing-whitespace-h ()
|
2019-05-16 15:57:08 -04:00
|
|
|
"Disable `show-trailing-whitespace' in the current buffer."
|
|
|
|
(setq-local show-trailing-whitespace nil))
|