cua-gmrk.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. ;;; cua-gmrk.el --- CUA unified global mark support
  2. ;; Copyright (C) 1997-2012 Free Software Foundation, Inc.
  3. ;; Author: Kim F. Storm <storm@cua.dk>
  4. ;; Keywords: keyboard emulations convenience cua mark
  5. ;; Package: cua-base
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (eval-when-compile
  20. (require 'cua-base)
  21. (require 'cua-rect)
  22. )
  23. ;;; Global Marker
  24. ;; Non-nil when global marker is active.
  25. (defvar cua--global-mark-active nil)
  26. ;; Global mark position marker.
  27. (defvar cua--global-mark-marker nil)
  28. ;; Overlay for global mark position.
  29. (defvar cua--global-mark-overlay nil)
  30. ;; Initialize global mark things once...
  31. (defvar cua--global-mark-initialized nil)
  32. ;; Saved configured blink-cursor-interval
  33. (defvar cua--orig-blink-cursor-interval nil)
  34. (defun cua--deactivate-global-mark (&optional msg)
  35. (when cua--global-mark-overlay
  36. (delete-overlay cua--global-mark-overlay)
  37. (setq cua--global-mark-overlay nil))
  38. (if (markerp cua--global-mark-marker)
  39. (move-marker cua--global-mark-marker nil))
  40. (if cua--orig-blink-cursor-interval
  41. (setq blink-cursor-interval cua--orig-blink-cursor-interval
  42. cua--orig-blink-cursor-interval nil))
  43. (setq cua--global-mark-active nil)
  44. (if msg
  45. (message "Global Mark Cleared")))
  46. (defun cua--activate-global-mark (&optional msg)
  47. (if (not (markerp cua--global-mark-marker))
  48. (setq cua--global-mark-marker (make-marker)))
  49. (when (eobp)
  50. (insert " ")
  51. (backward-char 1))
  52. (move-marker cua--global-mark-marker (point))
  53. (if (overlayp cua--global-mark-overlay)
  54. (move-overlay cua--global-mark-overlay (point) (1+ (point)))
  55. (setq cua--global-mark-overlay
  56. (make-overlay (point) (1+ (point))))
  57. (overlay-put cua--global-mark-overlay 'face 'cua-global-mark))
  58. (if (and cua-global-mark-blink-cursor-interval
  59. (not cua--orig-blink-cursor-interval))
  60. (setq cua--orig-blink-cursor-interval blink-cursor-interval
  61. blink-cursor-interval cua-global-mark-blink-cursor-interval))
  62. (setq cua--global-mark-active t)
  63. (if msg
  64. (message "Global Mark Set")))
  65. (defun cua--global-mark-active ()
  66. (if cua--global-mark-active
  67. (or (and (markerp cua--global-mark-marker)
  68. (marker-buffer cua--global-mark-marker))
  69. (and (cua--deactivate-global-mark nil)
  70. nil))))
  71. (defun cua-toggle-global-mark (stay)
  72. "Set or cancel the global marker.
  73. When the global marker is set, CUA cut and copy commands will automatically
  74. insert the deleted or copied text before the global marker, even when the
  75. global marker is in another buffer.
  76. If the global marker isn't set, set the global marker at point in the current
  77. buffer. Otherwise jump to the global marker position and cancel it.
  78. With prefix argument, don't jump to global mark when canceling it."
  79. (interactive "P")
  80. (unless cua--global-mark-initialized
  81. (cua--init-global-mark))
  82. (if (not (cua--global-mark-active))
  83. (if (not buffer-read-only)
  84. (cua--activate-global-mark t)
  85. (ding)
  86. (message "Cannot set global mark in read-only buffer"))
  87. (when (not stay)
  88. (pop-to-buffer (marker-buffer cua--global-mark-marker))
  89. (goto-char cua--global-mark-marker))
  90. (cua--deactivate-global-mark t)))
  91. (defun cua--insert-at-global-mark (str &optional msg)
  92. ;; Insert string at global marker and move marker
  93. (with-current-buffer (marker-buffer cua--global-mark-marker)
  94. (goto-char (marker-position cua--global-mark-marker))
  95. (insert-for-yank str)
  96. (cua--activate-global-mark))
  97. (if msg
  98. (message "%s %d to global mark in %s:%d" msg
  99. (length str)
  100. (buffer-name (marker-buffer cua--global-mark-marker))
  101. (marker-position cua--global-mark-marker))))
  102. (defun cua--delete-at-global-mark (arg &optional msg)
  103. ;; Delete chars at global marker
  104. (with-current-buffer (marker-buffer cua--global-mark-marker)
  105. (goto-char (marker-position cua--global-mark-marker))
  106. (delete-char arg))
  107. (if msg
  108. (message "%s %d chars at global mark in %s:%d" msg arg
  109. (buffer-name (marker-buffer cua--global-mark-marker))
  110. (marker-position cua--global-mark-marker))))
  111. (defun cua-copy-region-to-global-mark (start end)
  112. "Copy region to global mark buffer/position."
  113. (interactive "r")
  114. (if (cua--global-mark-active)
  115. (let ((src-buf (current-buffer)))
  116. (save-excursion
  117. (if (equal (marker-buffer cua--global-mark-marker) src-buf)
  118. (let ((text (cua--filter-buffer-noprops start end)))
  119. (goto-char (marker-position cua--global-mark-marker))
  120. (insert text))
  121. (set-buffer (marker-buffer cua--global-mark-marker))
  122. (goto-char (marker-position cua--global-mark-marker))
  123. (insert-buffer-substring-as-yank src-buf start end))
  124. (cua--activate-global-mark)
  125. (message "Copied %d to global mark in %s:%d"
  126. (abs (- end start))
  127. (buffer-name (marker-buffer cua--global-mark-marker))
  128. (marker-position cua--global-mark-marker))))
  129. (cua--deactivate-global-mark)
  130. (message "No Global Mark")))
  131. (defun cua-cut-region-to-global-mark (start end)
  132. "Move region to global buffer/position."
  133. (interactive "r")
  134. (if (cua--global-mark-active)
  135. (let ((src-buf (current-buffer)))
  136. (save-excursion
  137. (if (equal (marker-buffer cua--global-mark-marker) src-buf)
  138. (if (and (< start (marker-position cua--global-mark-marker))
  139. (< (marker-position cua--global-mark-marker) end))
  140. (message "Can't move region into itself")
  141. (let ((text (cua--filter-buffer-noprops start end))
  142. (p1 (copy-marker start))
  143. (p2 (copy-marker end)))
  144. (goto-char (marker-position cua--global-mark-marker))
  145. (insert text)
  146. (cua--activate-global-mark)
  147. (delete-region (marker-position p1) (marker-position p2))
  148. (move-marker p1 nil)
  149. (move-marker p2 nil)))
  150. (set-buffer (marker-buffer cua--global-mark-marker))
  151. (goto-char (marker-position cua--global-mark-marker))
  152. (insert-buffer-substring src-buf start end)
  153. (message "Moved %d to global mark in %s:%d"
  154. (abs (- end start))
  155. (buffer-name (marker-buffer cua--global-mark-marker))
  156. (marker-position cua--global-mark-marker))
  157. (cua--activate-global-mark)
  158. (set-buffer src-buf)
  159. (delete-region start end))))
  160. (cua--deactivate-global-mark)
  161. (message "No Global Mark")))
  162. (defun cua--copy-rectangle-to-global-mark (as-text)
  163. ;; Copy rectangle to global mark buffer/position.
  164. (if (cua--global-mark-active)
  165. (let ((src-buf (current-buffer))
  166. (text (cua--extract-rectangle)))
  167. (with-current-buffer (marker-buffer cua--global-mark-marker)
  168. (goto-char (marker-position cua--global-mark-marker))
  169. (if as-text
  170. (while text
  171. (insert-for-yank (car text))
  172. (if (setq text (cdr text))
  173. (insert "\n")))
  174. (cua--insert-rectangle text 'auto))
  175. (cua--activate-global-mark)
  176. (message "Copied rectangle to global mark in %s:%d"
  177. (buffer-name (marker-buffer cua--global-mark-marker))
  178. (marker-position cua--global-mark-marker))))
  179. (cua--deactivate-global-mark)
  180. (message "No Global Mark")))
  181. (defun cua--cut-rectangle-to-global-mark (as-text)
  182. ;; Move rectangle to global buffer/position.
  183. (if (cua--global-mark-active)
  184. (let ((src-buf (current-buffer)))
  185. (save-excursion
  186. (if (equal (marker-buffer cua--global-mark-marker) src-buf)
  187. (let ((olist (overlays-at (marker-position cua--global-mark-marker)))
  188. in-rect)
  189. (while olist
  190. (if (eq (overlay-get (car olist) 'face) 'cua-rectangle)
  191. (setq in-rect t olist nil)
  192. (setq olist (cdr olist))))
  193. (if in-rect
  194. (message "Can't move rectangle into itself")
  195. (let ((text (cua--extract-rectangle)))
  196. (cua--delete-rectangle)
  197. (goto-char (marker-position cua--global-mark-marker))
  198. (if as-text
  199. (while text
  200. (insert-for-yank (car text))
  201. (if (setq text (cdr text))
  202. (insert "\n")))
  203. (cua--insert-rectangle text 'auto))
  204. (cua--activate-global-mark))))
  205. (let ((text (cua--extract-rectangle)))
  206. (cua--delete-rectangle)
  207. (set-buffer (marker-buffer cua--global-mark-marker))
  208. (goto-char (marker-position cua--global-mark-marker))
  209. (cua--insert-rectangle text 'auto))
  210. (message "Moved rectangle to global mark in %s:%d"
  211. (buffer-name (marker-buffer cua--global-mark-marker))
  212. (marker-position cua--global-mark-marker))
  213. (cua--activate-global-mark))))
  214. (cua--deactivate-global-mark)
  215. (message "No Global Mark")))
  216. (defun cua-copy-to-global-mark ()
  217. "Copy active region/rectangle to global mark buffer/position."
  218. (interactive)
  219. (setq cua--last-killed-rectangle nil)
  220. (if cua--rectangle
  221. (cua--copy-rectangle-to-global-mark nil)
  222. (let ((start (mark)) (end (point)))
  223. (or (<= start end)
  224. (setq start (prog1 end (setq end start))))
  225. (cua-copy-region-to-global-mark start end))))
  226. (defun cua-copy-next-to-global-mark (n)
  227. "Copy the following N characters in buffer to global mark buffer/position."
  228. (interactive "p")
  229. (setq cua--last-killed-rectangle nil)
  230. (or (eobp)
  231. (let ((p (point)))
  232. (goto-char (+ p n))
  233. (cua-copy-region-to-global-mark p (point)))))
  234. (defun cua-cut-to-global-mark ()
  235. "Move active region/rectangle to global mark buffer/position."
  236. (interactive)
  237. (if buffer-read-only
  238. (cua-copy-to-global-mark)
  239. (setq cua--last-killed-rectangle nil)
  240. (if cua--rectangle
  241. (cua--cut-rectangle-to-global-mark nil)
  242. (let ((start (mark)) (end (point)))
  243. (or (<= start end)
  244. (setq start (prog1 end (setq end start))))
  245. (cua-cut-region-to-global-mark start end)))))
  246. (defun cua-cut-next-to-global-mark (n)
  247. "Move the following N characters in buffer to global mark buffer/position."
  248. (interactive "p")
  249. (setq cua--last-killed-rectangle nil)
  250. (or (eobp)
  251. (let ((p (point)))
  252. (goto-char (+ p n))
  253. (cua-cut-region-to-global-mark p (point)))))
  254. (defun cua-delete-char-at-global-mark (arg)
  255. "Delete character following the global mark position."
  256. (interactive "p")
  257. (cua--delete-at-global-mark arg "Deleted"))
  258. (defun cua-delete-backward-char-at-global-mark (arg)
  259. "Delete character before the global mark position."
  260. (interactive "p")
  261. (cua--delete-at-global-mark (- arg) "Deleted backward"))
  262. (defun cua-insert-char-at-global-mark ()
  263. "Insert the character you type at the global mark position."
  264. (interactive)
  265. (cua--insert-at-global-mark (char-to-string (aref (this-single-command-keys) 0)) "Inserted"))
  266. (defun cua-insert-newline-at-global-mark ()
  267. "Insert a newline at the global mark position."
  268. (interactive)
  269. (cua--insert-at-global-mark "\n"))
  270. (defun cua-indent-to-global-mark-column ()
  271. "Indent current line or rectangle to global mark column."
  272. (interactive "*")
  273. (if (cua--global-mark-active)
  274. (let (col)
  275. (with-current-buffer (marker-buffer cua--global-mark-marker)
  276. (goto-char (marker-position cua--global-mark-marker))
  277. (setq col (current-column)))
  278. (if cua--rectangle
  279. (cua--indent-rectangle nil col t)
  280. (indent-to col))
  281. (if (eq (current-buffer) (marker-buffer cua--global-mark-marker))
  282. (save-excursion
  283. (goto-char (marker-position cua--global-mark-marker))
  284. (move-to-column col)
  285. (move-marker cua--global-mark-marker (point))
  286. (move-overlay cua--global-mark-overlay (point) (1+ (point))))))))
  287. (defun cua-cancel-global-mark ()
  288. "Cancel the global mark."
  289. (interactive)
  290. (if mark-active
  291. (cua-cancel)
  292. (if (cua--global-mark-active)
  293. (cua--deactivate-global-mark t)))
  294. (cua--fallback))
  295. ;;; Post-command hook for global mark.
  296. (defun cua--global-mark-post-command ()
  297. (when (and (cua--global-mark-active) ;; Updates cua--global-mark-active variable
  298. cua-global-mark-keep-visible)
  299. ;; keep global mark position visible
  300. (sit-for 0)
  301. (if (or (not (eq (current-buffer) (marker-buffer cua--global-mark-marker)))
  302. (not (pos-visible-in-window-p (marker-position cua--global-mark-marker))))
  303. (let ((w (selected-window)) (p (point)) h)
  304. ;; The following code is an attempt to keep the global mark visible in
  305. ;; other window -- but it doesn't work.
  306. (switch-to-buffer-other-window (marker-buffer cua--global-mark-marker) t)
  307. (goto-char (marker-position cua--global-mark-marker))
  308. (if (not (pos-visible-in-window-p (marker-position cua--global-mark-marker)))
  309. (recenter (if (> (setq h (- (window-height) 4)) 1) h '(4))))
  310. (select-window w)
  311. (goto-char p)))))
  312. ;;; Initialization
  313. (defun cua--init-global-mark ()
  314. (define-key cua--global-mark-keymap [remap copy-region-as-kill] 'cua-copy-to-global-mark)
  315. (define-key cua--global-mark-keymap [remap kill-ring-save] 'cua-copy-to-global-mark)
  316. (define-key cua--global-mark-keymap [remap kill-region] 'cua-cut-to-global-mark)
  317. (define-key cua--global-mark-keymap [remap yank] 'cua-copy-next-to-global-mark)
  318. (define-key cua--global-mark-keymap [remap keyboard-escape-quit] 'cua-cancel-global-mark)
  319. (define-key cua--global-mark-keymap [remap keyboard-quit] 'cua-cancel-global-mark)
  320. (define-key cua--global-mark-keymap [(control ?d)] 'cua-cut-next-to-global-mark)
  321. (define-key cua--global-mark-keymap [remap delete-backward-char] 'cua-delete-backward-char-at-global-mark)
  322. (define-key cua--global-mark-keymap [remap backward-delete-char] 'cua-delete-backward-char-at-global-mark)
  323. (define-key cua--global-mark-keymap [remap backward-delete-char-untabify] 'cua-delete-backward-char-at-global-mark)
  324. (define-key cua--global-mark-keymap [remap self-insert-command] 'cua-insert-char-at-global-mark)
  325. (define-key cua--global-mark-keymap [remap self-insert-iso] 'cua-insert-char-at-global-mark)
  326. ;; Catch self-inserting characters which are "stolen" by other modes
  327. (define-key cua--global-mark-keymap [t]
  328. '(menu-item "sic" cua-insert-char-at-global-mark :filter cua--self-insert-char-p))
  329. (define-key cua--global-mark-keymap [remap newline] 'cua-insert-newline-at-global-mark)
  330. (define-key cua--global-mark-keymap [remap newline-and-indent] 'cua-insert-newline-at-global-mark)
  331. (define-key cua--global-mark-keymap "\r" 'cua-insert-newline-at-global-mark)
  332. (define-key cua--global-mark-keymap "\t" 'cua-indent-to-global-mark-column)
  333. (setq cua--global-mark-initialized t))
  334. (provide 'cua-gmrk)
  335. ;;; cua-gmrk.el ends here