[英]Paste text aligned to cursor in emacs
Is there a way to paste a text from clipboard aligned with cursor position:有没有办法从剪贴板粘贴与 cursor position 对齐的文本:
function myFunction() {
console.log('a');
<<PASTING HERE>>
}
By default I get:默认情况下,我得到:
function myFunction() {
console.log('a');
console.log('b');
console.log('c');
console.log('d');
}
But I want to see the last two lines aligned with two spaces too.但我也想看到最后两行与两个空格对齐。 (OF course, I can select text and align it by tab but it's additional action) (当然,我可以使用 select 文本并通过制表符对齐它,但这是附加操作)
Basically you need to do the indent on the freshly pasted code.基本上你需要对新粘贴的代码进行缩进。 I'm using following code for years:我多年来一直在使用以下代码:
(defvar yank-indent-modes '(emacs-lisp-mode lisp-mode
c-mode c++-mode js2-mode
tcl-mode sql-mode
perl-mode cperl-mode
java-mode jde-mode
lisp-interaction-mode
LaTeX-mode TeX-mode
go-mode cuda-mode
scheme-mode clojure-mode)
"Modes in which to indent regions that are yanked (or yank-popped)")
(defadvice yank (after indent-region activate)
"If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)."
(if (member major-mode yank-indent-modes)
(let ((mark-even-if-inactive t))
(indent-region (region-beginning) (region-end) nil))))
This code extends standard yank
command that is bound to Cy
, and if the current mode is in the list of modes defined in the yank-indent-modes
, then it will execute indent-region
on the pasted snippet.此代码扩展了绑定到Cy
的标准yank
命令,如果当前模式在yank-indent-modes
中定义的模式列表中,那么它将在粘贴的代码段上执行indent-region
。
PS You may also need to add the same defadvice
on the yank-pop
command. PS 您可能还需要在yank-pop
命令上添加相同的defadvice
设置。
Implemented the desired solution myself (sorry for the ugly lisp).自己实现了所需的解决方案(对于丑陋的 lisp 感到抱歉)。 The key thing is that pasting text is NOT aligned but simply intended by cursor column spaces as described at the original request关键是粘贴文本没有对齐,而只是由原始请求中描述的 cursor 列空间打算
(defun clipboard-yank-my (&rest args)
""" wrapper: yank with shifting yanked text to current cursor column """
;; wrapping: https://emacs.stackexchange.com/questions/19215/how-to-write-a-transparent-pass-through-function-wrapper#comment55216_19242)
(interactive (advice-eval-interactive-spec
(cadr (interactive-form #'clipboard-yank))))
(setq point1 (point))
(beginning-of-line)
(setq pointStart (point))
(setq currentColumn (- point1 (point)))
;; ORIGINAL
(apply #'clipboard-yank args)
(newline)
;; (print col)
(set-mark-command nil)
(goto-char pointStart)
(indent-rigidly
(region-beginning)
(region-end)
currentColumn)
(goto-char point1)
;; (setq deactivate-mark nil)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.