cua-gmrk.el 14 KB

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