PageRenderTime 25ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/my-lisps/edit-misc.el

https://github.com/ryebread/my-emacs
Emacs Lisp | 396 lines | 280 code | 51 blank | 65 comment | 5 complexity | 1ba1ebaa07b1e9a9525f0dbca4c60b99 MD5 | raw file
  1. ;; Copyright (C) 2010 ahei
  2. ;; Author: ahei <ahei0802@gmail.com>
  3. ;; Keywords:
  4. ;; URL: http://code.google.com/p/dea/source/browse/trunk/my-lisps/edit-misc.el
  5. ;; Time-stamp: <2010-09-27 08:26:55 Monday by ryebread>
  6. ;; This file is free software; you can redistribute it and/or
  7. ;; modify it under the terms of the GNU General Public License as
  8. ;; published by the Free Software Foundation; either version 3,
  9. ;; or (at your option) any later version.
  10. ;; This file is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public
  15. ;; License along with GNU Emacs; see the file COPYING. If not,
  16. ;; write to the Free Software Foundation, Inc., 51 Franklin
  17. ;; Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. ;;; Commentary:
  19. ;;; Installation:
  20. ;;
  21. ;; Copy edit-misc.el to your load-path and add to your .emacs:
  22. ;;
  23. ;; (require 'edit-misc)
  24. ;;; History:
  25. ;;
  26. ;; 2010-4-1
  27. ;; * initial version 1.0.
  28. ;;; Code:
  29. ;;;###autoload
  30. (defun backward-kill-word-or-kill-region ()
  31. "`mark-active'时, 剪切选择的区域, 平时向后删除word, 和bash下面一样."
  32. (interactive)
  33. (if (rm-mark-active)
  34. (call-interactively 'rm-kill-region)
  35. (if mark-active
  36. (if cua--rectangle
  37. (progn
  38. (cua-cut-rectangle t)
  39. (cua-cancel))
  40. (call-interactively 'kill-region))
  41. (call-interactively 'backward-kill-word))))
  42. ;;;###autoload
  43. (defun mark-whole-sexp (&optional not-whole)
  44. "Mark whole sexp.
  45. If NOT-WHOLE is non-nil, do not mark whole sexp."
  46. (interactive "P")
  47. (if not-whole
  48. (mark-sexp)
  49. (let ((region (bounds-of-thing-at-point 'sexp)))
  50. (if (not region)
  51. (message "Can not found sexp.")
  52. (goto-char (car region))
  53. (call-interactively 'set-mark-command)
  54. (forward-sexp)))))
  55. ;;;###autoload
  56. (defun kill-whole-sexp (&optional not-whole)
  57. "Kill whole sexp.
  58. If NOT-WHOLE is non-nil, do not kill whole sexp."
  59. (interactive)
  60. (mark-whole-sexp not-whole)
  61. (backward-kill-word-or-kill-region))
  62. ;;;###autoload
  63. (defun copy-sexp (&optional not-whole)
  64. "Copy whole sexp.
  65. If NOT-WHOLE is non-nil, do not copy whole sexp."
  66. (interactive)
  67. (save-excursion
  68. (mark-whole-sexp not-whole)
  69. (if mark-active
  70. (copy-region (region-beginning) (region-end)))))
  71. ;;;###autoload
  72. (defun my-kill-word ()
  73. "删除一个单词, 当光标处于单词中间时也删除整个单词, 这是与`kill-word'的区别"
  74. (interactive)
  75. (wcy-mark-some-thing-at-point)
  76. (backward-kill-word-or-kill-region))
  77. ;;;###autoload
  78. (defun mark-function ()
  79. "Mark function."
  80. (interactive)
  81. (cond
  82. ((or (equal major-mode 'c-mode) (equal major-mode 'c++-mode))
  83. (c-mark-function))
  84. ((or (equal major-mode 'emacs-lisp-mode) (equal major-mode 'lisp-mode) (equal major-mode 'lisp-interaction-mode))
  85. (lisp-mark-function))))
  86. ;;;###autoload
  87. (defmacro def-action-on-function-command (fun-name action action-str)
  88. `(defun ,fun-name ()
  89. ,(concat (capitalize action-str) " function.")
  90. (interactive)
  91. (save-excursion
  92. (mark-function)
  93. (call-interactively ,action))))
  94. ;;;###autoload
  95. (defun comment-function (&optional arg)
  96. "Comment function."
  97. (interactive "P")
  98. (save-excursion
  99. (mark-function)
  100. (comment-region (region-beginning) (region-end) arg)))
  101. ;;;###autoload
  102. (defun kill-whole-paragraph (&optional arg)
  103. "Kill whole paragraph."
  104. (interactive "P")
  105. (if arg
  106. (kill-paragraph nil)
  107. (call-interactively 'mark-paragraph)
  108. (call-interactively 'kill-region)))
  109. ;;;###autoload
  110. (defun copy-whole-paragraph (&optional arg)
  111. "Copy whole paragraph."
  112. (interactive "P")
  113. (save-excursion
  114. (if arg
  115. (progn
  116. (mark-command t)
  117. (forward-paragraph))
  118. (call-interactively 'mark-paragraph))
  119. (call-interactively 'copy-region)))
  120. ;;;###autoload
  121. (defun copy-cur-line ()
  122. "拷贝当前行"
  123. (interactive)
  124. (let ((end (min (point-max) (1+ (line-end-position)))))
  125. (copy-region-as-kill-nomark (line-beginning-position) end)))
  126. ;;;###autoload
  127. (defun copy-lines (&optional number)
  128. "从当前行开始拷贝NUMBER行"
  129. (interactive "p")
  130. (if (null number)
  131. (copy-cur-line)
  132. (let ((lineNo))
  133. (save-excursion
  134. (if (< number 0)
  135. (next-line))
  136. (setq lineNo (line-number-at-pos nil))
  137. (move-beginning-of-line nil)
  138. (set-mark-command nil)
  139. (goto-line (+ number lineNo))
  140. (call-interactively 'copy-region-as-kill-nomark)))))
  141. ;;;###autoload
  142. (defun copy-line-left ()
  143. "拷贝当前行光标后面的文字"
  144. (interactive)
  145. (copy-region-as-kill-nomark (point) (min (1+ (line-end-position)) (point-max))))
  146. ;;;###autoload
  147. (defun smart-copy ()
  148. "智能拷贝, 如果`mark-active'的话, 则`copy-region', 否则`copy-lines'"
  149. (interactive)
  150. (if mark-active (call-interactively 'copy-region) (call-interactively 'copy-lines)))
  151. ;;;###autoload
  152. (defun copy-region-and-paste ()
  153. "拷贝region并且粘贴到region后"
  154. (interactive)
  155. (call-interactively 'copy-region)
  156. (call-interactively 'yank))
  157. ;;;###autoload
  158. (defun which-copy ()
  159. "如果`mark-active'的话, 则`copy-region-and-paste', 否则`copy-line-left'"
  160. (interactive)
  161. (if mark-active (copy-region-and-paste) (copy-line-left)))
  162. ;;;###autoload
  163. (defun insert-cur-line ()
  164. "拷贝当前行并粘贴进当前buffer"
  165. (interactive)
  166. (copy-cur-line)
  167. (forward-line)
  168. (beginning-of-line)
  169. (call-interactively 'yank)
  170. (previous-line)
  171. (end-of-line))
  172. ;;;###autoload
  173. (defun insert-cur-sexp ()
  174. "拷贝当前sexp并粘贴进当前buffer"
  175. (interactive)
  176. (copy-sexp)
  177. (call-interactively 'yank))
  178. ;;;###autoload
  179. (defun copy-sentence ()
  180. "拷贝sentence"
  181. (interactive)
  182. (save-excursion
  183. (call-interactively 'mark-end-of-sentence)
  184. (call-interactively 'copy-region-as-kill-nomark)))
  185. ;; 删除当前光标到行首的字符
  186. ;;;###autoload
  187. (defun del-to-begin (&optional arg)
  188. "Delete characters to line beginning."
  189. (interactive "P")
  190. (if (not arg)
  191. (kill-line 0)
  192. (copy-region-as-kill-nomark (1+ (line-beginning-position)) (point))))
  193. ;;;###autoload
  194. (defun lisp-mark-function (&optional allow-extend)
  195. "`mark-defun'有时候会多mark一个空白行, 这个函数就是解决这个bug的"
  196. (interactive "p")
  197. (mark-defun allow-extend)
  198. (let (next-is-fun)
  199. (save-excursion (forward-line) (setq next-is-fun (looking-at "[ \t]*(defun")))
  200. (if (or (looking-at "$") (and next-is-fun (not (looking-at "[ \t]*(defun"))))
  201. (forward-line))))
  202. ;;;###autoload
  203. (defun case-trans ()
  204. "大小写转换当前字符"
  205. (interactive)
  206. (let* ((ochar (char-after (point))) (char ochar))
  207. (if (and (>= char ?a) (<= char ?z))
  208. (setq char (upcase char))
  209. (setq char (downcase char)))
  210. (if (/= ochar char)
  211. (save-excursion
  212. (delete-char 1)
  213. (insert-char char 1)))))
  214. ;;;###autoload
  215. (defun comment (&optional arg)
  216. "如果`mark-active'的话,就`comment-region',否则注释光标所在行"
  217. (interactive "P")
  218. (if mark-active
  219. (comment-region (region-beginning) (region-end) arg)
  220. (let (fun)
  221. (if arg (setq fun 'uncomment-region) (setq fun 'comment-region))
  222. (funcall fun (line-beginning-position) (line-end-position)))))
  223. ;;;###autoload
  224. (defun uncomment (&optional arg)
  225. "如果`mark-active'的话,就`uncomment-region',否则取消注释光标所在行"
  226. (interactive "P")
  227. (comment (not arg)))
  228. ;;;###autoload
  229. (defun qiang-comment-dwim-line (&optional arg)
  230. "Replacement for the comment-dwim command.
  231. If no region is selected and current line is not blank and we are not at the end of the line,
  232. then comment current line.
  233. Replaces default behaviour of comment-dwim, when it inserts comment at the end of the line."
  234. (interactive "*P")
  235. (comment-normalize-vars)
  236. (if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
  237. (comment-or-uncomment-region (line-beginning-position) (line-end-position))
  238. (comment-dwim arg)))
  239. (global-set-key "\M-;" 'qiang-comment-dwim-line)
  240. ;;;###autoload
  241. (defun mark-invisible-region ()
  242. "Mark invisible region."
  243. (interactive)
  244. (if (not (and last-region-beg last-region-end))
  245. (message "No previous region.")
  246. (goto-char last-region-beg)
  247. (if last-region-is-rect
  248. (if last-region-use-cua
  249. (call-interactively 'cua-set-rectangle-mark)
  250. (call-interactively 'rm-set-mark))
  251. (call-interactively 'set-mark-command))
  252. (goto-char last-region-end)
  253. (if (and last-region-is-rect last-region-use-cua)
  254. (cua--activate-rectangle))))
  255. ;;;###autoload
  256. (defun c-electric-backspace-kill ()
  257. "If `mark-active', run `kill-region', otherwise run `c-electric-backspace'."
  258. (interactive)
  259. (if mark-active
  260. (call-interactively 'kill-region)
  261. (call-interactively 'c-electric-backspace)))
  262. ;;;###autoload
  263. (defun delete-blank-lines-region (beg end)
  264. "Execute `delete-blank-lines' in region."
  265. (interactive "*r")
  266. (save-excursion
  267. (goto-char beg)
  268. (let ((blank-line "^\\s-*$")
  269. (nonblank-line "^.*\\S-.*$")
  270. blank-beg blank-end)
  271. (while (and (< (point) end) (setq blank-beg (search-forward-regexp blank-line end t)))
  272. (save-excursion
  273. (setq blank-end (search-forward-regexp nonblank-line end t)))
  274. (if blank-end
  275. (setq end (- end (- blank-end blank-beg)))
  276. (setq end 0))
  277. (previous-line)
  278. (delete-blank-lines)))))
  279. ;;;###autoload
  280. (defun smart-delete-blank-lines (&optional no-region)
  281. "Smart `delete-blank-lines'.
  282. If NO-REGION is non-nil, always execute `delete-blank-lines',
  283. otherwise, if `mark-active', execute `delete-blank-lines-region',
  284. and execute `delete-blank-lines' if there no mark."
  285. (interactive "P")
  286. (if (or no-region (not mark-active))
  287. (delete-blank-lines)
  288. (call-interactively 'delete-blank-lines-region)))
  289. ;;;###autoload
  290. (defun smart-home (&optional home)
  291. "Goto home.
  292. If HOME is negative, call `beginning-of-line-text',
  293. otherwise call `move-beginning-of-line'."
  294. (interactive "P")
  295. (if (not home)
  296. (let ((old (point)))
  297. (beginning-of-line-text)
  298. (if (= (point) old)
  299. (move-beginning-of-line 1)))
  300. (if (< (prefix-numeric-value home) 0)
  301. (beginning-of-line-text)
  302. (move-beginning-of-line 1))))
  303. ;;;###autoload
  304. (defun smart-kill ()
  305. "If `mark-active', call `kill-region', otherwise call `kill-whole-line'."
  306. (interactive)
  307. (if mark-active
  308. (call-interactively 'kill-region)
  309. (call-interactively 'kill-whole-line)))
  310. ;;;###autoload
  311. (defun smart-indent ()
  312. "If `mark-active', call `indent-region', otherwise indent all buffer."
  313. (interactive)
  314. (save-excursion
  315. (unless mark-active
  316. (call-interactively 'mark-whole-buffer))
  317. (call-interactively 'indent-region)))
  318. ;;;###autoload
  319. (defun fill-paragraph-justify (region)
  320. "Run `fill-paragraph' with argument justify t."
  321. (interactive (list t))
  322. (fill-paragraph 'full region))
  323. ;;;###autoload
  324. (defun open-line-before ()
  325. "Insert a newline before cur line and leave point before it.
  326. If there is a fill prefix and/or a `left-margin', insert them
  327. on the new line if the line would have been blank.
  328. With arg N, insert N newlines."
  329. (interactive)
  330. (move-beginning-of-line 1)
  331. (open-line 1)
  332. (indent-for-tab-command))
  333. ;;;###autoload
  334. (defun open-line-after ()
  335. "Insert a newline after cur line and leave point before it.
  336. If there is a fill prefix and/or a `left-margin', insert them
  337. on the new line if the line would have been blank.
  338. With arg N, insert N newlines."
  339. (interactive)
  340. (move-end-of-line 1)
  341. (open-line 1)
  342. (forward-char)
  343. (indent-for-tab-command))
  344. (provide 'edit-misc)
  345. ;;; edit-misc.el ends here