[英]How to copy/paste a region from emacs buffer with line + file reference?
From time to time I see people pasting portions of code with reference to the file name and line number. 我不时会看到人们参考文件名和行号粘贴部分代码。 Something like
就像是
;; ----- line:3391 file: simple.el.gz -----;;;
(if (eq last-command 'kill-region)
(kill-append (filter-buffer-substring beg end) (< end beg))
(kill-new (filter-buffer-substring beg end)))
;; ----- line:3394 --------------------------;;;
This is mainly useful to send comments on code by mail. 这主要用于通过邮件发送代码注释。 I can easily wrap a simple function for myself, but I am sure someone have already done this in a smart and pretty way.
我可以轻松地为自己包装一个简单的功能,但我相信有人已经以聪明而漂亮的方式完成了这项工作。
Thanks. 谢谢。
[EDIT] [编辑]
Because this functionality is needed only occasionally, and only for one copy/paste action, I ended up using an alternative solution to the toggling version proposed by @thisirs. 因为仅偶尔需要此功能,并且仅用于一次复制/粘贴操作,所以我最终使用了@thisirs提出的切换版本的替代解决方案。
(defun kill-with-linenum (beg end)
(interactive "r")
(save-excursion
(goto-char end)
(skip-chars-backward "\n \t")
(setq end (point))
(let* ((chunk (buffer-substring beg end))
(chunk (concat
(format "╭──────── #%-d ─ %s ──\n│ "
(line-number-at-pos beg)
(or (buffer-file-name) (buffer-name))
)
(replace-regexp-in-string "\n" "\n│ " chunk)
(format "\n╰──────── #%-d ─"
(line-number-at-pos end)))))
(kill-new chunk)))
(deactivate-mark))
It is unicode based and produces this output: 它是基于unicode的并产生以下输出:
╭──────── #3557 ─ /usr/share/emacs/24.1.50/lisp/simple.el.gz ──
│ (if (eq this-command t)
│ (setq this-command 'yank))
│ nil)
╰──────── #3559 ─
I have come up with this, using wrapper hook: 我已经想出了这个,使用包装钩:
(defun filter-buffer-substring-add-line (func beg end delete)
(concat
(format ";; line:%5d file: %s\n"
(line-number-at-pos beg)
(or (buffer-file-name) (buffer-name)))
(funcall func beg end delete)
(format "\n;; line:%5d" (line-number-at-pos end))))
(defun kill-add-line-toggle ()
(interactive)
(if (memq 'filter-buffer-substring-add-line
filter-buffer-substring-functions)
(progn
(setq filter-buffer-substring-functions
(delq 'filter-buffer-substring-add-line
filter-buffer-substring-functions))
(message "Add line is off!"))
(push 'filter-buffer-substring-add-line
filter-buffer-substring-functions)
(message "Add line is on!")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.