From: "Drew Adams" <drew.adams@oracle.com>
To: "'Stefan Monnier'" <monnier@iro.umontreal.ca>
Cc: 'Emacs-Devel' <emacs-devel@gnu.org>
Subject: RE: FW: commands to select things of different kinds
Date: Sun, 9 Mar 2008 20:30:20 -0800 [thread overview]
Message-ID: <003c01c88267$73875330$0600a8c0@us.oracle.com> (raw)
In-Reply-To: <jwvwsobtfu0.fsf-monnier+emacs@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 2252 bytes --]
> Could you digest it into a single thread of text. The current mess of
> forward and backward quoting is just unreadable.
Everything important is in the original post. I've repeated it below.
RMS asked how it knows what kind of thing to select, to which I repeated
that if the region is not yet active you are prompted with completion for
the type of thing to select (default: sexp).
--- original post ---
From: Drew Adams Sent: Monday, July 16, 2007 12:17 AM
Any interest in these commands? They let you select (that is, mark) various
things at or near point. They are most useful in Transient Mark mode - they
act differently depending on whether the mark is active.
* `mark-thing' selects successive things, starting at point. The mark is put
at the same place that command 'forward-'thing would put it (using the same
prefix argument). If the region is not yet active, then you are prompted
(with completion) for the type of thing to select. The default type is
`sexp'.
* `cycle-thing-region' selects one thing at or near point (just the thing,
not from point through the thing, even if the thing is not exactly at
point). Repeat it to cycle among the thing types - one thing of the current
type is selected at each invocation. The default order of types is `word',
`sexp', `list', `line', `sentence', `paragraph', `page', `defun', `number',
`form'. That order is customizable, and a major mode could also change it to
put the most commonly used types first.
I bind `mark-thing' to `C-M-SPC' as a replacement for `mark-sexp'.
I bind `cycle-thing-region' to `M-@' as a replacement for `mark-word'. (By
default, it does what `mark-word' does without a prefix arg. Unlike,
`mark-thing', however, `cycle-thing-region' does not accept a prefix arg.)
I don't propose these bindings for Emacs generally, but if you use
transient-mark mode you might want to give them a try.
To select successive things in transient-mark mode, you can use
`cycle-thing-region' as an alternative to completion for choosing the thing
type for `mark-thing' - but you need to use `C-x C-x' between the two. That
is, you can use 'M-@ C-x C-x C-M-SPC' to select successive words, 'M-@ M-@
C-x C-x C-M-SPC' for successive symbols, and so on.
The code is attached.
[-- Attachment #2: thing-cmds.el --]
[-- Type: application/octet-stream, Size: 8042 bytes --]
;;; thing-cmds.el --- Commands that use things, as defined by `thingatpt.el'.
;;
;; Filename: thing-cmds.el
;; Description: Commands that use things, as defined by `thingatpt.el'.
;; Author: Drew Adams
;; Maintainer: Drew Adams
;; Copyright (C) 2006-2007, Drew Adams, all rights reserved.
;; Created: Sun Jul 30 16:40:29 2006
;; Version: 20.1
;; Last-Updated: Sun Jul 15 23:58:11 2007 (-25200 Pacific Daylight Time)
;; By: dradams
;; Update #: 41
;; URL: http://www.emacswiki.org/cgi-bin/wiki/thing-cmds.el
;; Keywords: thingatpt, thing, region, selection
;; Compatibility: GNU Emacs 20.x, GNU Emacs 21.x, GNU Emacs 22.x
;;
;; Features that might be required by this library:
;;
;; `thingatpt', `thingatpt+'.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; You can use the commands defined here to select different kinds of
;; text entities ("things"). They are especially useful in
;; combination with Transient Mark mode.
;;
;; Commands defined here:
;;
;; `cycle-thing-region', `mark-thing', `select-thing-near-point',
;; `thing-region'.
;;
;; User options defined here:
;;
;; `thing-types'.
;;
;; Internal variables defined here:
;;
;; `mark-thing-type', `thing-region-index'.
;;
;; Put this in your init file (`~/.emacs'): (require 'thing-cmds)
;;
;; Suggested key bindings (these will replace the standard bindings
;; for `mark-sexp' and `mark-word'):
;;
;; (global-set-key [(control meta ? )] 'mark-thing) ; vs `mark-sexp'
;; (global-set-key [(meta ?@)] 'cycle-thing-region) ; vs `mark-word'
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;; 2007/07/15 dadams
;; Added cycle-thing-region-point.
;; cycle-thing-region: Save point in cycle-thing-region-point and reuse it.
;; 2006/07/30 dadams
;; Created.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 2, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'thingatpt+ nil t) ;; (no error if not found): bounds-of-thing-at-point
(require 'thingatpt) ;; bounds-of-thing-at-point
(when (< emacs-major-version 20) (eval-when-compile (require 'cl))) ;; when, unless
;;;;;;;;;;;;;;;;;;;;;;;;
(defun thing-region (thing)
"Set the region around a THING near the cursor.
You are prompted for the type of thing. Completion is available for
some standard types of thing, but you can enter any type.
The cursor is placed at the end of the region. You can return it to
the original location by using `C-u C-SPC' twice."
(interactive (list (let ((icicle-sort-function nil))
(completing-read "Type of thing: " (mapcar #'list thing-types)
nil nil nil nil "sexp"))))
(let* ((bds (if (fboundp 'bounds-of-thing-nearest-point) ; In `thingatpt+.el'.
(bounds-of-thing-nearest-point (intern thing))
(bounds-of-thing-at-point (intern thing))))
(start (car bds))
(end (cdr bds)))
(cond ((and start end)
(push-mark (point) t) ; Mark position, so can use `C-u C-SPC'.
(goto-char end)
(push-mark start t 'activate)
(setq deactivate-mark nil)
thing) ; Return thing.
(t
(message "No `%s' near point" thing)
(setq deactivate-mark nil)
nil)))) ; Return nil: no thing found.
(defalias 'select-thing-near-point 'cycle-thing-region)
(defun cycle-thing-region ()
"Select a thing near point. Successive uses select different things.
In Transient Mark mode, you can follow this with `\\[mark-thing]' to select
successive things of the same type, but to do that you must first use
`C-x C-x': `\\[cycle-thing-region] C-x C-x \\[mark-thing]'"
(interactive)
(if (eq last-command this-command)
(goto-char cycle-thing-region-point)
(setq thing-region-index 0)
(setq cycle-thing-region-point (point)))
(let* ((thing (elt thing-types thing-region-index))
(success (thing-region thing)))
(setq thing-region-index (1+ thing-region-index))
(when success
(setq mark-thing-type (intern thing)) ; Save it for `mark-thing'.
(message "%s" (capitalize (elt thing-types (1- thing-region-index)))))
(when (>= thing-region-index (length thing-types)) (setq thing-region-index 0))))
(defcustom thing-types '("word" "symbol" "sexp" "list" "line" "sentence"
"paragraph" "page" "defun" "number" "form")
"List of thing types. Used for completion and `cycle-thing-region'.
Each element is a string that names a type of text entity for which
there is a either a corresponding `forward-'thing operation, or
corresponding `beginning-of-'thing and `end-of-'thing operations.
Examples include \"word\", \"sentence\", and \"defun\"."
:type '(repeat string))
(defvar thing-region-index 0 "Index of current thing in `thing-types'.")
(defvar mark-thing-type nil "Current thing type used by `mark-thing'.")
(defvar cycle-thing-region-point nil
"Position of point before `cycle-thing-region'.")
(defun mark-thing (thing &optional arg allow-extend)
"Set the mark ARG THINGs from point.
THING is a string that names a type of thing: \"word\", \"sexp\", and
so on. See option `thing-types' for more examples.
The mark is put at the same place command `forward-'THING would put it
with the same prefix argument.
If the prefix argument (ARG) is negative, then point is put at the end
of the THING, amd mark is put at the beginning of the THING or a
previous THING.
Interactively:
You are prompted for THING. Completion is available for the types of
thing in `thing-types', but you can enter any type.
If `mark-thing' is repeated or if the mark is active (in Transient
Mark mode), then it marks the next ARG THINGs, after the ones already
marked."
(interactive "i\nP\np") ; THING arg is nil (ignored) interactively.
(cond ((and allow-extend (or (and (eq last-command this-command) (mark t))
(and transient-mark-mode mark-active)))
(setq arg (if arg
(prefix-numeric-value arg)
(if (< (mark) (point)) -1 1)))
(set-mark (save-excursion
(goto-char (mark))
(forward-thing mark-thing-type arg)
(point))))
(t
(setq mark-thing-type
(or thing (intern (let ((icicle-sort-function nil))
(completing-read "Type of thing: "
(mapcar #'list thing-types)
nil nil nil nil "sexp")))))
(push-mark (save-excursion
(forward-thing mark-thing-type (prefix-numeric-value arg))
(point))
nil t)))
(setq deactivate-mark nil))
;;;;;;;;;;;;;;;;;;;;;;;;
(provide 'thing-cmds)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; thing-cmds.el ends here
next prev parent reply other threads:[~2008-03-10 4:30 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-03-09 16:45 FW: commands to select things of different kinds Drew Adams
2008-03-10 3:32 ` Stefan Monnier
2008-03-10 4:30 ` Drew Adams [this message]
-- strict thread matches above, loose matches on Subject: below --
2008-04-18 22:06 Drew Adams
2008-04-19 3:42 ` Stefan Monnier
2008-04-19 20:11 ` Juri Linkov
2008-04-19 20:22 ` David Kastrup
2008-04-19 23:18 ` Richard Stallman
2008-04-19 20:35 ` Drew Adams
2008-04-19 22:54 ` Juri Linkov
2008-04-19 23:22 ` Drew Adams
2008-04-20 23:52 ` Juri Linkov
2008-04-21 0:13 ` Drew Adams
2008-04-21 1:03 ` Stefan Monnier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='003c01c88267$73875330$0600a8c0@us.oracle.com' \
--to=drew.adams@oracle.com \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.