2017-06-08 11:47:56 +02:00
|
|
|
;;; core/autoload/editor.el -*- lexical-binding: t; -*-
|
2017-02-03 08:05:47 -05:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
;; Commands
|
|
|
|
|
2017-02-03 08:05:47 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/backward-to-bol-or-indent ()
|
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'."
|
2017-02-03 08:05:47 -05:00
|
|
|
(interactive)
|
2018-02-01 14:43:28 -05:00
|
|
|
(let ((pos (point))
|
|
|
|
(indent (save-excursion
|
|
|
|
(beginning-of-visual-line)
|
|
|
|
(skip-chars-forward " \t\r")
|
|
|
|
(point))))
|
|
|
|
(cond ((or (> pos indent) (= pos (line-beginning-position)))
|
|
|
|
(goto-char indent))
|
|
|
|
((<= pos indent)
|
|
|
|
(beginning-of-visual-line)))))
|
2017-02-03 08:05:47 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/forward-to-last-non-comment-or-eol ()
|
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'."
|
2017-02-03 08:05:47 -05:00
|
|
|
(interactive)
|
2018-03-05 02:59:21 -05:00
|
|
|
(let ((eol (save-excursion (if visual-line-mode
|
|
|
|
(end-of-visual-line)
|
|
|
|
(end-of-line))
|
|
|
|
(point))))
|
2018-02-01 16:35:55 -05:00
|
|
|
(if (and (sp-point-in-comment) (not (= (point) eol)))
|
|
|
|
(goto-char eol)
|
|
|
|
(let* ((bol (save-excursion (beginning-of-visual-line) (point)))
|
|
|
|
(boc (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 (sp-point-in-comment)
|
|
|
|
(> (point) bol))
|
|
|
|
(backward-char))
|
|
|
|
(skip-chars-backward " " bol)
|
|
|
|
(point)))
|
|
|
|
eol)))
|
|
|
|
(cond ((= boc (point))
|
|
|
|
(goto-char eol))
|
|
|
|
((/= bol boc)
|
|
|
|
(goto-char boc)))))))
|
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
|
|
|
|
(defun doom/backward-kill-to-bol-and-indent ()
|
|
|
|
"Kill line to the first non-blank character. If invoked again
|
2018-02-14 05:10:48 -05:00
|
|
|
afterwards, kill line to beginning of line."
|
2017-02-03 08:05:47 -05:00
|
|
|
(interactive)
|
2017-09-25 05:05:55 +02:00
|
|
|
(let ((empty-line-p (save-excursion (beginning-of-line)
|
|
|
|
(looking-at-p "[ \t]*$"))))
|
2019-02-24 13:58:56 -05:00
|
|
|
(funcall (if (fboundp 'evil-delete)
|
2017-09-25 05:05:55 +02:00
|
|
|
#'evil-delete
|
|
|
|
#'delete-region)
|
2017-02-03 08:05:47 -05:00
|
|
|
(point-at-bol) (point))
|
2017-09-25 05:05:55 +02:00
|
|
|
(unless empty-line-p
|
2017-02-03 08:05:47 -05:00
|
|
|
(indent-according-to-mode))))
|
|
|
|
|
|
|
|
;;;###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."
|
|
|
|
(interactive "Pr")
|
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-01-03 03:32:19 -05:00
|
|
|
(defvar-local doom--buffer-narrowed-origin nil)
|
2017-12-29 22:29:57 -05:00
|
|
|
;;;###autoload
|
2018-02-12 01:44:02 -05:00
|
|
|
(defun doom/clone-and-narrow-buffer (beg end &optional clone-p)
|
2017-12-29 22:29:57 -05:00
|
|
|
"Restrict editing in this buffer to the current region, indirectly. With CLONE-P,
|
|
|
|
clone the buffer and hard-narrow the selection. If mark isn't active, then widen
|
|
|
|
the buffer (if narrowed).
|
|
|
|
|
|
|
|
Inspired from http://demonastery.org/2013/04/emacs-evil-narrow-region/"
|
2018-10-03 11:14:08 -04:00
|
|
|
(interactive "rP")
|
|
|
|
(cond ((or (region-active-p)
|
|
|
|
(and beg end))
|
2017-12-29 22:29:57 -05:00
|
|
|
(deactivate-mark)
|
|
|
|
(when clone-p
|
|
|
|
(let ((old-buf (current-buffer)))
|
|
|
|
(switch-to-buffer (clone-indirect-buffer nil nil))
|
2018-01-03 03:32:19 -05:00
|
|
|
(setq doom--buffer-narrowed-origin old-buf)))
|
2017-12-29 22:29:57 -05:00
|
|
|
(narrow-to-region beg end))
|
2018-01-03 03:32:19 -05:00
|
|
|
(doom--buffer-narrowed-origin
|
2017-12-29 22:29:57 -05:00
|
|
|
(kill-this-buffer)
|
2018-01-03 03:32:19 -05:00
|
|
|
(switch-to-buffer doom--buffer-narrowed-origin)
|
|
|
|
(setq doom--buffer-narrowed-origin nil))
|
2017-12-29 22:29:57 -05:00
|
|
|
(t
|
|
|
|
(widen))))
|
|
|
|
|
2018-09-01 12:30:34 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/delete-trailing-newlines ()
|
|
|
|
"Trim trailing newlines.
|
|
|
|
|
|
|
|
Respects `require-final-newline'."
|
|
|
|
(interactive)
|
|
|
|
(goto-char (point-max))
|
|
|
|
(skip-chars-backward " \t\n\v")
|
|
|
|
(when (looking-at "\n\\(\n\\|\\'\\)")
|
|
|
|
(forward-char 1))
|
|
|
|
(when require-final-newline
|
|
|
|
(unless (bolp)
|
|
|
|
(insert "\n")))
|
|
|
|
(when (looking-at "\n+")
|
|
|
|
(replace-match "")))
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
2018-02-14 05:10:48 -05:00
|
|
|
;;
|
|
|
|
;; Hooks
|
|
|
|
|
2017-07-03 03:11:54 +02:00
|
|
|
;;;###autoload
|
2017-09-25 03:02:13 +02:00
|
|
|
(defun doom|enable-delete-trailing-whitespace ()
|
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
|
|
|
|
(defun doom|disable-delete-trailing-whitespace ()
|
|
|
|
"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))
|