editing-defuns.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. ;;; editing-defuns.el --- Basic text editing defuns -*- lexical-binding: t; -*-
  2. (defun open-line-below ()
  3. (interactive)
  4. (end-of-line)
  5. (newline)
  6. (indent-for-tab-command))
  7. (defun open-line-above ()
  8. (interactive)
  9. (beginning-of-line)
  10. (newline)
  11. (forward-line -1)
  12. (indent-for-tab-command))
  13. (defun smart-open-line ()
  14. "Insert an empty line after the current line.
  15. Position the cursor at its beginning, according to the current mode."
  16. (interactive)
  17. (move-end-of-line nil)
  18. (newline-and-indent))
  19. (defun smart-open-line-above ()
  20. "Insert an empty line above the current line.
  21. Position the cursor at it's beginning, according to the current mode."
  22. (interactive)
  23. (move-beginning-of-line nil)
  24. (newline-and-indent)
  25. (forward-line -1)
  26. (indent-according-to-mode))
  27. (defun new-line-in-between ()
  28. (interactive)
  29. (newline)
  30. (save-excursion
  31. (newline)
  32. (indent-for-tab-command))
  33. (indent-for-tab-command))
  34. (defun new-line-dwim ()
  35. (interactive)
  36. (let ((break-open-pair (or (and (looking-back "{" 1) (looking-at "}"))
  37. (and (looking-back ">" 1) (looking-at "<"))
  38. (and (looking-back "(" 1) (looking-at ")"))
  39. (and (looking-back "\\[" 1) (looking-at "\\]")))))
  40. (newline)
  41. (when break-open-pair
  42. (save-excursion
  43. (newline)
  44. (indent-for-tab-command)))
  45. (indent-for-tab-command)))
  46. (defun duplicate-current-line-or-region (arg)
  47. "Duplicates the current line or region ARG times.
  48. If there's no region, the current line will be duplicated."
  49. (interactive "p")
  50. (if (region-active-p)
  51. (let ((beg (region-beginning))
  52. (end (region-end)))
  53. (duplicate-region arg beg end)
  54. (one-shot-keybinding "d" (λ (duplicate-region 1 beg end))))
  55. (duplicate-current-line arg)
  56. (one-shot-keybinding "d" 'duplicate-current-line)))
  57. (defun one-shot-keybinding (key command)
  58. (set-temporary-overlay-map
  59. (let ((map (make-sparse-keymap)))
  60. (define-key map (kbd key) command)
  61. map) t))
  62. (defun replace-region-by (fn)
  63. (let* ((beg (region-beginning))
  64. (end (region-end))
  65. (contents (buffer-substring beg end)))
  66. (delete-region beg end)
  67. (insert (funcall fn contents))))
  68. (defun duplicate-region (&optional num start end)
  69. "Duplicates the region bounded by START and END NUM times.
  70. If no START and END is provided, the current region-beginning and
  71. region-end is used."
  72. (interactive "p")
  73. (save-excursion
  74. (let* ((start (or start (region-beginning)))
  75. (end (or end (region-end)))
  76. (region (buffer-substring start end)))
  77. (goto-char end)
  78. (dotimes (i num)
  79. (insert region)))))
  80. (defun duplicate-current-line (&optional num)
  81. "Duplicate the current line NUM times."
  82. (interactive "p")
  83. (save-excursion
  84. (when (eq (point-at-eol) (point-max))
  85. (goto-char (point-max))
  86. (newline)
  87. (forward-char -1))
  88. (duplicate-region num (point-at-bol) (1+ (point-at-eol)))))
  89. ;; automatically indenting yanked text if in programming-modes
  90. (require 'dash)
  91. (defvar yank-indent-modes '(prog-mode
  92. sgml-mode
  93. js2-mode)
  94. "Modes in which to indent regions that are yanked (or yank-popped)")
  95. (defvar yank-advised-indent-threshold 1000
  96. "Threshold (# chars) over which indentation does not automatically occur.")
  97. (defun yank-advised-indent-function (beg end)
  98. "Do indentation, as long as the region isn't too large."
  99. (if (<= (- end beg) yank-advised-indent-threshold)
  100. (indent-region beg end nil)))
  101. (defadvice yank (after yank-indent activate)
  102. "If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)."
  103. (if (and (not (ad-get-arg 0))
  104. (--any? (derived-mode-p it) yank-indent-modes))
  105. (let ((transient-mark-mode nil))
  106. (yank-advised-indent-function (region-beginning) (region-end)))))
  107. (defadvice yank-pop (after yank-pop-indent activate)
  108. "If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)."
  109. (if (and (not (ad-get-arg 0))
  110. (member major-mode yank-indent-modes))
  111. (let ((transient-mark-mode nil))
  112. (yank-advised-indent-function (region-beginning) (region-end)))))
  113. (defun yank-unindented ()
  114. (interactive)
  115. (yank 1))
  116. ;; toggle quotes
  117. (defun current-quotes-char ()
  118. (nth 3 (syntax-ppss)))
  119. (defalias 'point-is-in-string-p 'current-quotes-char)
  120. (defun move-point-forward-out-of-string ()
  121. (while (point-is-in-string-p) (forward-char)))
  122. (defun move-point-backward-out-of-string ()
  123. (while (point-is-in-string-p) (backward-char)))
  124. (defun alternate-quotes-char ()
  125. (if (eq ?' (current-quotes-char)) ?\" ?'))
  126. (defun toggle-quotes ()
  127. (interactive)
  128. (if (point-is-in-string-p)
  129. (let ((old-quotes (char-to-string (current-quotes-char)))
  130. (new-quotes (char-to-string (alternate-quotes-char)))
  131. (start (make-marker))
  132. (end (make-marker)))
  133. (save-excursion
  134. (move-point-forward-out-of-string)
  135. (backward-delete-char 1)
  136. (set-marker end (point))
  137. (insert new-quotes)
  138. (move-point-backward-out-of-string)
  139. (delete-char 1)
  140. (insert new-quotes)
  141. (set-marker start (point))
  142. (replace-string new-quotes (concat "\\" new-quotes) nil start end)
  143. (replace-string (concat "\\" old-quotes) old-quotes nil start end)))
  144. (error "Point isn't in a string")))
  145. ;; kill region if active, otherwise kill backward word
  146. (defun kill-region-or-backward-word ()
  147. (interactive)
  148. (if (region-active-p)
  149. (kill-region (region-beginning) (region-end))
  150. (backward-kill-word 1)))
  151. (defun kill-to-beginning-of-line ()
  152. (interactive)
  153. (kill-region (save-excursion (beginning-of-line) (point))
  154. (point)))
  155. ;; copy region if active
  156. ;; otherwise copy to end of current line
  157. ;; * with prefix, copy N whole lines
  158. (defun copy-to-end-of-line ()
  159. (interactive)
  160. (kill-ring-save (point)
  161. (line-end-position))
  162. (message "Copied to end of line"))
  163. (defun copy-whole-lines (arg)
  164. "Copy lines (as many as prefix argument) in the kill ring"
  165. (interactive "p")
  166. (kill-ring-save (line-beginning-position)
  167. (line-beginning-position (+ 1 arg)))
  168. (message "%d line%s copied" arg (if (= 1 arg) "" "s")))
  169. (defun copy-line (arg)
  170. "Copy to end of line, or as many lines as prefix argument"
  171. (interactive "P")
  172. (if (null arg)
  173. (copy-to-end-of-line)
  174. (copy-whole-lines (prefix-numeric-value arg))))
  175. (defun save-region-or-current-line (arg)
  176. (interactive "P")
  177. (if (region-active-p)
  178. (kill-ring-save (region-beginning) (region-end))
  179. (copy-line arg)))
  180. (defun kill-and-retry-line ()
  181. "Kill the entire current line and reposition point at indentation"
  182. (interactive)
  183. (back-to-indentation)
  184. (kill-line))
  185. (defun camelize-buffer ()
  186. (interactive)
  187. (goto-char 0)
  188. (ignore-errors
  189. (replace-next-underscore-with-camel 0))
  190. (goto-char 0))
  191. ;; kill all comments in buffer
  192. (defun comment-kill-all ()
  193. (interactive)
  194. (save-excursion
  195. (goto-char (point-min))
  196. (comment-kill (save-excursion
  197. (goto-char (point-max))
  198. (line-number-at-pos)))))
  199. (require 's)
  200. (defun incs (s &optional num)
  201. (let* ((inc (or num 1))
  202. (new-number (number-to-string (+ inc (string-to-number s))))
  203. (zero-padded? (s-starts-with? "0" s)))
  204. (if zero-padded?
  205. (s-pad-left (length s) "0" new-number)
  206. new-number)))
  207. (defun change-number-at-point (arg)
  208. (interactive "p")
  209. (unless (or (looking-at "[0-9]")
  210. (looking-back "[0-9]"))
  211. (error "No number to change at point"))
  212. (save-excursion
  213. (while (looking-back "[0-9]")
  214. (forward-char -1))
  215. (re-search-forward "[0-9]+" nil)
  216. (replace-match (incs (match-string 0) arg) nil nil)))
  217. (defun subtract-number-at-point (arg)
  218. (interactive "p")
  219. (change-number-at-point (- arg)))
  220. (defun replace-next-underscore-with-camel (arg)
  221. (interactive "p")
  222. (if (> arg 0)
  223. (setq arg (1+ arg))) ; 1-based index to get eternal loop with 0
  224. (let ((case-fold-search nil))
  225. (while (not (= arg 1))
  226. (search-forward-regexp "\\b_[a-z]")
  227. (forward-char -2)
  228. (delete-char 1)
  229. (capitalize-word 1)
  230. (setq arg (1- arg)))))
  231. (defun snakeify-current-word ()
  232. (interactive)
  233. (er/mark-word)
  234. (let* ((beg (region-beginning))
  235. (end (region-end))
  236. (current-word (buffer-substring-no-properties beg end))
  237. (snakified (snake-case current-word)))
  238. (replace-string current-word snakified nil beg end)))
  239. (defun transpose-params ()
  240. "Presumes that params are in the form (p, p, p) or {p, p, p} or [p, p, p]"
  241. (interactive)
  242. (let* ((end-of-first (cond
  243. ((looking-at ", ") (point))
  244. ((and (looking-back ",") (looking-at " ")) (- (point) 1))
  245. ((looking-back ", ") (- (point) 2))
  246. (t (error "Place point between params to transpose."))))
  247. (start-of-first (save-excursion
  248. (goto-char end-of-first)
  249. (move-backward-out-of-param)
  250. (point)))
  251. (start-of-last (+ end-of-first 2))
  252. (end-of-last (save-excursion
  253. (goto-char start-of-last)
  254. (move-forward-out-of-param)
  255. (point))))
  256. (transpose-regions start-of-first end-of-first start-of-last end-of-last)))
  257. (defun move-forward-out-of-param ()
  258. (while (not (looking-at ")\\|, \\| ?}\\| ?\\]"))
  259. (cond
  260. ((point-is-in-string-p) (move-point-forward-out-of-string))
  261. ((looking-at "(\\|{\\|\\[") (forward-list))
  262. (t (forward-char)))))
  263. (defun move-backward-out-of-param ()
  264. (while (not (looking-back "(\\|, \\|{ ?\\|\\[ ?"))
  265. (cond
  266. ((point-is-in-string-p) (move-point-backward-out-of-string))
  267. ((looking-back ")\\|}\\|\\]") (backward-list))
  268. (t (backward-char)))))
  269. (autoload 'zap-up-to-char "misc"
  270. "Kill up to, but not including ARGth occurrence of CHAR.")
  271. (defun css-expand-statement ()
  272. (interactive)
  273. (save-excursion
  274. (end-of-line)
  275. (search-backward "{")
  276. (forward-char 1)
  277. (let ((beg (point)))
  278. (newline)
  279. (er/mark-inside-pairs)
  280. (replace-regexp ";" ";\n" nil (region-beginning) (region-end))
  281. (indent-region beg (point)))))
  282. (defun css-contract-statement ()
  283. (interactive)
  284. (end-of-line)
  285. (search-backward "{")
  286. (while (not (looking-at "}"))
  287. (join-line -1))
  288. (back-to-indentation))