Giter Site home page Giter Site logo

selected.el's Introduction

selected.el http://melpa.org/packages/selected-badge.svg

selected.el provides the selected-minor-mode for Emacs. When selected-minor-mode is active, the keybindings in selected-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.el also provides selected-global-mode, if you want selected-minor-mode in every buffer.

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
  :ensure t
  :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.

You might have other minor-modes which conflict with selected, such as worf, lispy or ryo. In that case you can use (setq selected-minor-mode-override t).

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
  :ensure t
  :commands selected-minor-mode
  :init
  (setq selected-org-mode-map (make-sparse-keymap))
  :bind (:map selected-keymap
              ("q" . selected-off)
              ("u" . upcase-region)
              ("d" . downcase-region)
              ("w" . count-words-region)
              ("m" . apply-macro-to-region-lines)
              :map selected-org-mode-map
              ("t" . org-table-convert-region)))

If the major-mode specific keymap isn’t found, derived from modes will be searched. For instance if you open an org-mode file, don’t have selected-org-mode-map but do have a selected-text-mode-map then selected-text-mode-map will be used.

Installation and setup

selected.el is on MELPA, so the easiest way is to install it from there using M-x package-install. MELPA needs to be in your package-archives:

(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)

If you add (selected-global-mode) to your init-file, or if you add selected-minor-mode to a hook, please do so towards the end of your init-file. This is in order to prioritize the bindings in selected-keymap over other minor-modes.

Recommended resources

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

Feel free to suggest additions to this list!

Changelog

1.01
Added selected-global-mode.
1.02
Derived modes now searches up the hierarchy for selected-<major-mode>-map.

selected.el's People

Contributors

kungsgeten avatar lordpretzel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

selected.el's Issues

selected-keymap doesn't activate if region already active when turning on

Hi, I'm using the technique recommended in the ryo-modal README, to synchronize selected-minor-mode with ryo-modal-mode, something like this:

  (defun cl/ryo-sync-selected-mode ()
    (selected-minor-mode (if ryo-modal-mode 1 -1)))
  (add-hook 'ryo-modal-mode-hook 'cl/ryo-sync-selected-mode)

But one issue I encountered is that if I already have a region active in "insert" mode (ryo-modal off), and then I hit escape (turning ryo-modal on), the selected keymap doesn't activate and I'm stuck with just the ryo-modal bindings (until I toggle mark off and on again). I guess that's because selected-minor-mode hooks into mark activation/deactivation, but those are removed once out of selected and ryo.

Anyway, it seems to be a one-line fix:

--- a/selected.el
+++ b/selected.el
@@ -69,6 +69,7 @@
   :lighter " sel"
   (if selected-minor-mode
       (progn
+        (if mark-active (selected--on))
         (add-hook 'activate-mark-hook #'selected--on)
         (add-hook 'deactivate-mark-hook #'selected-off))
     (remove-hook 'activate-mark-hook #'selected--on)

which of course I can also do in my hook, but it seems to me like it would be the right thing™ for all users of selected…?

Thanks for both these packages – I gave Evil a fair shake but it wasn't for me, so now I'm having a good time rolling my own!

Region is lost after calling a binding

With the following:

(use-package selected
  :commands selected-minor-mode
  :bind (:map selected-keymap
              ("<M-left>" . indent-rigidly-left)
              ("<M-right>" . indent-rigidly-right)))

I wish I could just keep indenting things, but region is lost.

I guess it has something to do with indent-rigidly-left/right. Assuming a ^ in interactive's arguments would fix it, how do I do that from my own files?

(defun indent-rigidly-right (beg end)
  "Indent all lines between BEG and END rightward by one space."
  (interactive "r")
  (indent-rigidly--pop-undo)
  (indent-rigidly
   beg end
   (if (eq (current-bidi-paragraph-direction) 'right-to-left) -1 1)))

no buffer-local hooks are used - selected activates in minibuffer

selected global mode uses selected--global-on-p to determine whether we are in the minibuffer and if yes, does not turn on selected-minor-mode. However, selected-minor-mode does add to global rather then buffer-local hooks. Thus, the selected--on will still be called in the minibuffer activating the keymap.

(define-minor-mode selected-minor-mode
  "If enabled activates the `selected-keymap' when the region is active."
  :lighter " sel"
  (if selected-minor-mode
      (progn
        (if mark-active (selected--on))
        (add-hook 'activate-mark-hook #'selected--on)
        (add-hook 'deactivate-mark-hook #'selected-off))
    (remove-hook 'activate-mark-hook #'selected--on)
    (remove-hook 'deactivate-mark-hook #'selected-off)
    (selected-off)))

Also it would be nice if a list of major modes to ignore could be added, e.g., I use s to activate swiper-at-point, but this ovrerides, s as staging in magit. I'll send a pull request soon

C-g (`keyboard-quit`) does not turn off `selected`

I have a (bad) habit of hitting C-g (keyboard-quit) to cancel my current mark. When selected-minor-mode is active and doing this selected will remain on and active until I hit q (selected-off).

Would it be possible to turn selected off on keyboard-quit?

selected-keymap is void

Hi

when using the use-package install
I have an error

Symbol's value as variable is void: selected-keymap

What can I do to fix this ?

thanks

any chance this can work with evil mode?

Hi

i started playing today with selected.el and it seems really cool, thx for this!

the problem is that im also an evil user and it seems selected wont really launch when i select text in evil mode. is there any chance these two can work together or thats just not technically possible?

thx!

Z

Submit to Melpa Stable

It would be great to have selected on stable.melpa.org. I believe all that's required is to tag a commit with a version number.

Can't override local map with use-package's :bind*

Seeing as I can't reproduce the issue with other packages where I want :bind to override other active maps, I figured I would ask here first. I may just be missing something obvious :-)

In my case, I use C-c l in LSP buffers, but I would like the same keybinding to take over when the selected keymap becomes active. I would expect the following to work:

(setq-default use-package-always-defer t)

(use-package lsp-mode
  :bind
  (:map lsp-mode-map
   ("C-c l" . hydra-lsp/body)))

(use-package selected
  :bind*
  (:map selected-keymap
   ("C-c l" . downcase-region))
  :hook
  (after-init . selected-global-mode))

What am I missing?

THANK YOU

I don't have an issue. I just LOVE this package. Thank you for making it. I'm a big fan of modal navigation (not editing) and used this to create a very fast way to edit the active region and act on it. Fan-tas-tic.

Here's my config:

(use-package selected
  :init (selected-global-mode 1)
  :bind (:map selected-keymap
			  ("q" . selected-off)
			  ("u" . upcase-dwim)
			  ("d" . downcase-dwim)
			  ("w" . kill-ring-save)
			  ("n" . next-line)
			  ("p" . previous-line)
			  ("f" . forward-char)
			  ("b" . backward-char)
			  ("F" . forward-word)
			  ("B" . backward-word)
			  ("a" . beginning-of-visual-line)
			  ("e" . end-of-visual-line)
			  ("{" . backward-paragraph)
			  ("}" . forward-paragraph)
			  ("(" . backward-sentence)
			  (")" . forward-sentence)
			  ("s" . ctrlf-forward-fuzzy)
			  ("r" . ctrlf-backward-fuzzy)
			  ("[" . scroll-down-line)
			  ("]" . scroll-up-line)
			  ("M" . rectangle-mark-mode)
			  ("R" . replace-rectangle)
			  ("x" . exchange-point-and-mark)
			  ))

selected--on: Wrong type argument: consp, nil

Whenever I try to mark a region with S-up/right/down/left, I get the following error:

selected--on: Wrong type argument: consp, nil

This makes the first attempt at marking region fail and forces me to press again up/right/down/left while maintaining Shift. Apparently the error occurs on M-x set-mark-command and when I click somewhere in the buffer (to move point) as well.

Here is my setup:

(use-package selected
  :bind (:map selected-keymap ...)
  :config (selected-global-mode))

Let me know if you need anything else.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.