Emacsist RSS --- If never, make it ever: C-h C-h

selected.el - Keymap for when region is active

7461

这里 查看更多 Emacs 相关推荐文章最新 Emacs 圈的动态. 欢迎关注微信公众账号: Emacsist

Hick 叽喳:

    也没用, 只看大概看了下介绍, 甚至因为时间因素暂时没去认真理解和试用, 这种选择一块以后相应的按键就有特定的功能这种思路感觉挺聪明的, 乃至可以叫个名字 交互式编辑器? 

selected.el provides the selected-minor-mode for Emacs. When selected-minor-mode is active, the keybindings inselected-keymap will be enabled when the region is active. This is useful for commands that operates on the region, which you only want bound to a key when the region is active.

selected-keymap has no default bindings. Bind it yourself:

(require 'selected)
(define-key selected-keymap (kbd "q") #'selected-off)
(define-key selected-keymap (kbd "u") #'upcase-region)
(define-key selected-keymap (kbd "d") #'downcase-region)
(define-key selected-keymap (kbd "w") #'count-words-region)
(define-key selected-keymap (kbd "m") #'apply-macro-to-region-lines)

It is cleaner with use-package:

(use-package selected
  :commands selected-minor-mode
  :bind (:map selected-keymap
              ("q" . selected-off)
              ("u" . upcase-region)
              ("d" . downcase-region)
              ("w" . count-words-region)
              ("m" . apply-macro-to-region-lines)))

Then activate it with M-x selected-minor-mode. It can be a good idea to add selected-minor-mode to the hooks of the major-modes where you want it activated. selected-off is a function which deactivates the keybindings until the next time the region becomes active. This is useful when you want to execute a command on the region, but then disable selected-minor-mode in favour of other commands like multiple-cursors.

Major mode specific bindings

You may want some keybindings to operate on the region, but only if you’re in a certain major-mode. This is possible if you create a keymap named selected-<major-mode>-map (for instance selected-org-mode-map) and add the mode-specific bindings there. Here’s an example, using use-package:

(use-package selected
  :commands selected-minor-mode
  :init
  (setq selected-org-mode-map (make-sparse-keymap))
  :bind (:map selected-keymap
              ("q" . selected-off)
              ("u" . upcase-region)
              ("d" . upcase-region)
              ("w" . count-words-region)
              ("m" . apply-macro-to-region-lines)
              :map selected-org-mode-map
              ("t" . org-table-convert-region)))

Recommended resources

Here’s some functions and packages that are useful with selected.el:

Feel free to suggest additions to this list!

原文出处: Kungsgeten
原文地址: https://github.com/Kungsgeten/selected.el
原文时间: 2016-04-22 17:02
本文地址: http://emacsist.com/10861
整理时间: 2016-04-24 23:19

本文由 Hick 整理,转载请保留以上信息;
COPYLEFTThe articles on this site come from Internet, thanks to all the original authors.
      If anything about COPYRIGHT, or LEFT, please contact Emacsist at gmail dot com .