【问题标题】:Shift-selection and emacs 24移位选择和 emacs 24
【发布时间】:2012-06-11 22:19:16
【问题描述】:

我使用 emacs 24 (OSX) 并且在换档选择方面遇到问题。 如果我用鼠标选择一个区域,文本会突出显示并自动保存到杀伤环上(我可以立即拉出它)。 使用移位选择,文本会突出显示,但我必须手动保存文本 (M-w) 才能拉出它。 有没有什么方法可以让 shift-selection 突出显示文本并将其保存到 kill ring 上?

【问题讨论】:

    标签: macos emacs dot-emacs emacs24


    【解决方案1】:

    默认情况下,用鼠标选择文本不会在杀伤环上放置任何东西(它会将其复制到 X 剪贴板)。

    您可以通过多种方式自定义 Emacs 与剪贴板的交互方式。见:

    C-hig (emacs) Cut and Paste RET

    有没有什么方法可以让 shift-selection 突出显示文本并将其保存到 kill ring 上?

    我不确定是否有任何方法可以检测到您已停止选择内容,但您或许可以建议 handle-shift-selection 设置一个临时的 post-command-hook 以运行自定义函数来将该区域放入剪贴板(并移除 post-command-hook)。

    编辑:等等,你所描述的正是我在 Ubuntu 上的 Emacs 24.1 中发生的事情。

    如果我在 Emacs 中移动选择一些文本,然后在任何应用程序中单击鼠标中键,我会粘贴选定的文本。

    在运行emacs -Q时尝试测试

    编辑 2:啊,但你说的不是鼠标,对吧?

    这个怎么样?

    (defvar my-shift-selection-in-progress nil)
    (make-variable-buffer-local 'my-shift-selection-in-progress)
    
    (defadvice handle-shift-selection
      (before my-shift-selection-to-kill-ring-advice)
      "Automatically copy shift-selected text to the kill ring.
    
    If `interprogram-cut-function' is non-nil, also save the text for a window
    system cut and paste.
    
    See `my-shift-selection-to-kill-ring'."
      (when (and shift-select-mode
                 this-command-keys-shift-translated
                 (not my-shift-selection-in-progress))
        (add-hook 'post-command-hook 'my-shift-selection-to-kill-ring nil t)))
    
    (ad-activate 'handle-shift-selection)
    
    (defun my-shift-selection-to-kill-ring ()
      "Post-command callback for my-shift-selection-to-kill-ring-advice."
      (if this-command-keys-shift-translated
          (kill-new (filter-buffer-substring (region-beginning) (region-end))
                    my-shift-selection-in-progress)
        (remove-hook 'post-command-hook 'my-shift-selection-to-kill-ring t))
      (setq my-shift-selection-in-progress this-command-keys-shift-translated))
    

    【讨论】:

    • 当您选择更多文本时,kill-new 不会用字符串填充 kill-ring 吗?
    • 不是my-shift-selection-in-progress
    • 我明白了。随着区域的增长,不会将整个区域连接起来吗?另外,如果我将选择扩展到最小点怎么办?
    • 不,这是一个 replace 参数,而不是 append;没有串联。如果您不相信,请尝试一下。欢迎提交错误报告,但我没有发现此版本有任何问题(除了模糊地想知道我是否真的应该在 post-command 函数中再次重新测试 shift-select-mode)。
    • 诚然,随着区域的增长追加/前置会(大概)更有效;但是只要区域缩小,您仍然需要更换,因此最坏的情况保持不变。不过,性能看起来完全不错,而且我怀疑在真正大的区域上进行移位选择并不会真正发生太多。
    【解决方案2】:

    “从班次选择自动终止”的问题是确定班次选择何时结束(此时您最终可以执行终止)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-07
      • 2013-01-18
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2011-07-21
      • 1970-01-01
      相关资源
      最近更新 更多