help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Elisp code to choose a region covering a word


From: Deniz Dogan
Subject: Re: Elisp code to choose a region covering a word
Date: Thu, 18 Nov 2010 12:46:03 +0100

2010/11/18 ishi soichi <soichi777@gmail.com>:
> I am trying to develop an elisp program that sets a region covering a word
> in text.
> I am a newbie in elisp so please be adviced.
> For example, we have a text like...
> ----------------------------
> In late 2008, it (<=point here!!!)  reduced its interest to 13 percent to
> raise much-needed cash, in a move that many saw as the beginning of a
> distancing of the long-time partners. Ford's stake fell further to 11
> percent when Mazda issued new shares last year.
> ----------------------
> by tying "C-f" (I know it's not a good idea to override "C-f". It's a test),
> the region is set to be
> ----------------------------
> In late 2008, it (Mark here!!!=>) reduced(<=point here!!!) its interest to
> 13 percent to raise much-needed cash, in a move that many saw as the
> beginning of a distancing of the long-time partners. Ford's stake fell
> further to 11 percent when Mazda issued new shares last year.
> ----------------------
> by typing "C-f" again, the region would be
> ----------------------------
> In late 2008, it reduced (Mark here!!!=>)its(<=point here!!!) interest to 13
> percent to raise much-needed cash, in a move that many saw as the beginning
> of a distancing of the long-time partners. Ford's stake fell further to 11
> percent when Mazda issued new shares last year.
> ----------------------
> and so on.  Every time we execute the interactive function, each individual
> word is chosen.  So I tried,
> ;;-------------------------
> (defun word-choice ()
>   (interactive)
>   (let  (foo)
> (re-search-forward "\\w")
> (goto-char (match-beginning 0))
> (setq foo (point-marker))
> (re-search-forward "\\s ")
> (goto-char (match-beginning 0))
> ))
> (define-key global-map "\C-f" 'word-choice)
> ;;---------------------------
> Obviously it does not do the job.  I don't think it sets a mark at all.
>  Maybe the use of "point-marker" is wrong.
> Could anyone help me for it?
> soichi

To put a region around the word following point in the buffer you can
use C-s C-w and then continue hitting C-w to extend the region to
include more words. This is not what you were looking for, but I
figured you may want to know about it anyways.

Here is a naive function I just put together:

(defun select-following-word ()
  (interactive)
  (re-search-forward "\\S-")
  (push-mark)
  (forward-word))

It works by first moving point to the first non-whitespace character
following current point, then pushing the mark and then moving a word
forward. This effectively sets the region around the following word,
but you may want to massage this code a bit to have it work in certain
corner cases.

You may also want to know about `kbd' which could simplify your keybindings:

(global-set-key (kbd "C-f") ;; easier on the eyes
  'select-following-word)

-- 
Deniz Dogan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]