unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [ELPA] New package: shift-select
@ 2019-04-16  7:51 Jen-Chieh Shen
  2019-04-16 12:50 ` Clément Pit-Claudel
  2019-04-16 22:43 ` Stefan Monnier
  0 siblings, 2 replies; 3+ messages in thread
From: Jen-Chieh Shen @ 2019-04-16  7:51 UTC (permalink / raw)
  To: emacs-devel


[-- Attachment #1.1: Type: text/plain, Size: 602 bytes --]

Shift select text like how other text editor does.

I notice there is `shift-select-mode` built-in to Emacs, but this does not
work the same as other text editor. And somehow act weird and mess up the
select region. This package should have the user use the shift select text
the same way as other text editor.

<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.
www.avg.com
<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

[-- Attachment #1.2: Type: text/html, Size: 1497 bytes --]

[-- Attachment #2: shift-select.el --]
[-- Type: application/octet-stream, Size: 3757 bytes --]

;;; shift-select.el --- Shift select text like how other text editor does.  -*- lexical-binding: t; -*-

;; Copyright (C) 2019  Shen, Jen-Chieh
;; Created date 2019-04-16 11:41:51

;; Author: Shen, Jen-Chieh <jcs090218@gmail.com>
;; Description: Shift select text like how other text editor does.
;; Keyword: region select shift
;; Version: 0.0.1
;; Package-Requires: ((emacs "24.3"))
;; URL: https://github.com/jcs090218/shift-select

;; This file is NOT part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Shift select text like how other text editor does.
;;

;;; Code:

(defgroup shift-select nil
  "Shift select mode."
  :prefix "shift-select-"
  :group 'tool
  :link '(url-link :tag "Repository" "https://github.com/jcs090218/shift-select"))


(defvar-local shift-select-start-pt -1
  "Shift select record point.")

(defvar-local shift-select-active nil
  "Shift select active.")

(defvar-local shift-select-shift-pressed nil
  "Check if shift is pressed.")


(defun shift-select-pre-command-hook ()
  "Shift select pre command hook."
  (setq-local shift-select-shift-pressed
              (ignore-errors
                (and (stringp (symbol-name last-command-event))
                     (string-match-p "S-" (symbol-name last-command-event)))))
  (when (or this-command-keys-shift-translated
            shift-select-shift-pressed)
    (setq-local shift-select-start-pt (point))))

(defun shift-select-post-command-hook ()
  "Shift select post command hook."
  (if (or this-command-keys-shift-translated
          shift-select-shift-pressed)
      (let ((cur-pt (point)))
        ;; User moves the cursor when holding shift key.
        (unless (= cur-pt shift-select-start-pt)
          (unless shift-select-active
            (push-mark shift-select-start-pt)
            (goto-char cur-pt)
            (setq-local mark-active t)
            (setq-local shift-select-active t))))
    (when (and mark-active
               shift-select-active)
      (pop-mark))
    (setq-local shift-select-active nil)))


(defun shift-select-enable ()
  "Enable `shift-select' in current buffer."
  (add-hook 'pre-command-hook 'shift-select-pre-command-hook nil t)
  (add-hook 'post-command-hook 'shift-select-post-command-hook nil t))

(defun shift-select-disable ()
  "Disable `shift-select-mode' in current buffer."
  (remove-hook 'pre-command-hook 'shift-select-pre-command-hook t)
  (remove-hook 'post-command-hook 'shift-select-post-command-hook t))


;;;###autoload
(define-minor-mode shift-select-minor-mode
  "Minor mode 'shift-select-mode'."
  :lighter " ShiSel"
  :group shift-select
  (if shift-select-minor-mode
      (shift-select-enable)
    (shift-select-disable)))

(defun shift-select-turn-on-shift-select-mode ()
  "Turn on the 'shift-select-mode'."
  (shift-select-minor-mode 1))

;;;###autoload
(define-globalized-minor-mode global-shift-select-mode
  shift-select-minor-mode shift-select-turn-on-shift-select-mode
  :require 'shift-select)


(provide 'shift-select)
;;; shift-select.el ends here

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [ELPA] New package: shift-select
  2019-04-16  7:51 [ELPA] New package: shift-select Jen-Chieh Shen
@ 2019-04-16 12:50 ` Clément Pit-Claudel
  2019-04-16 22:43 ` Stefan Monnier
  1 sibling, 0 replies; 3+ messages in thread
From: Clément Pit-Claudel @ 2019-04-16 12:50 UTC (permalink / raw)
  To: Jen-Chieh Shen, emacs-devel

On 2019-04-16 03:51, Jen-Chieh Shen wrote:
> I notice there is `shift-select-mode` built-in to Emacs, but this does not work the same as other text editor.

What's the problem with the way shift-selection works in Emacs?



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [ELPA] New package: shift-select
  2019-04-16  7:51 [ELPA] New package: shift-select Jen-Chieh Shen
  2019-04-16 12:50 ` Clément Pit-Claudel
@ 2019-04-16 22:43 ` Stefan Monnier
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Monnier @ 2019-04-16 22:43 UTC (permalink / raw)
  To: emacs-devel

> (defun shift-select-pre-command-hook ()
>   "Shift select pre command hook."
>   (setq-local shift-select-shift-pressed

You already declared the var with `defvar-local` so you don't need
`setq-local` here (or alternatively, you can use plain `defvar` at
top-level and `setq-local` here).

>               (ignore-errors
>                 (and (stringp (symbol-name last-command-event))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This either errors or returns t but never returns nil, sop you can just
remove it.  Tho I think what you meant to write was (symbolp
last-command-event), after which you should remove the `ignore-errors`
which would otherwise just hide programming errors.

>                      (string-match-p "S-" (symbol-name last-command-event)))))
>   (when (or this-command-keys-shift-translated
>             shift-select-shift-pressed)

When is this shift-select-shift-pressed needed?  It seems like this
might be working around a bug somewhere in the sense that
this-command-keys-shift-translated should have been non-nil already.

> (defun shift-select-post-command-hook ()
>   "Shift select post command hook."
>   (if (or this-command-keys-shift-translated
>           shift-select-shift-pressed)
>       (let ((cur-pt (point)))
>         ;; User moves the cursor when holding shift key.
>         (unless (= cur-pt shift-select-start-pt)

IIUC this is trying to notice when the command was a motion command.
Those should call `handle-shift-selection` (e.g. via the `^` char in
their interactive spec string) to announce that they're motion commands.
So IIUC this code tries to compensate for commands which fail to call
`handle-shift-selection`.

It's probably a good heuristic, but it would be good to report those
commands so we can fix them.  Using a heuristic like this one is OK for
an optional package, but for the core functionality we want something
that doesn't occasionally misfire (e.g. cur-pt can be different from
shift-select-start-pt because we switched to a different buffer, or
because text was inserted/deleted before point).

I don't see anything in this code which handles shift-selection
differently from the default code, so I'm not sure what is meant by
"Shift select text like how other text editor does".

Instead it seems to try and work around bugs in code which was (still)
not (yet) adjusted to cooperate with the "new" shift-selection
introduced in Emacs-23.  Maybe you could change the Commentary
accordingly (or otherwise clarifies in which way the behavior is
supposed to differ from the default shift-select-mode)?


        Stefan




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-04-16 22:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-16  7:51 [ELPA] New package: shift-select Jen-Chieh Shen
2019-04-16 12:50 ` Clément Pit-Claudel
2019-04-16 22:43 ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).