unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Elisp code to choose a region covering a word
@ 2010-11-18 10:22 ishi soichi
  2010-11-18 11:45 ` Tassilo Horn
  2010-11-18 11:46 ` Deniz Dogan
  0 siblings, 2 replies; 6+ messages in thread
From: ishi soichi @ 2010-11-18 10:22 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1846 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 12206 bytes --]

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

* Re: Elisp code to choose a region covering a word
  2010-11-18 10:22 Elisp code to choose a region covering a word ishi soichi
@ 2010-11-18 11:45 ` Tassilo Horn
  2010-11-18 12:10   ` Tassilo Horn
  2010-11-18 11:46 ` Deniz Dogan
  1 sibling, 1 reply; 6+ messages in thread
From: Tassilo Horn @ 2010-11-18 11:45 UTC (permalink / raw)
  To: help-gnu-emacs

Hi Soichi,

this should do the trick:

--8<---------------cut here---------------start------------->8---
(defun mark-next-word ()
  (interactive)
  (skip-chars-forward "[:space:]")
  (set-mark (point))
  (forward-word))
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo




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

* Re: Elisp code to choose a region covering a word
  2010-11-18 10:22 Elisp code to choose a region covering a word ishi soichi
  2010-11-18 11:45 ` Tassilo Horn
@ 2010-11-18 11:46 ` Deniz Dogan
  2010-11-19  6:49   ` ishi soichi
  1 sibling, 1 reply; 6+ messages in thread
From: Deniz Dogan @ 2010-11-18 11:46 UTC (permalink / raw)
  To: ishi soichi; +Cc: help-gnu-emacs

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



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

* Re: Elisp code to choose a region covering a word
  2010-11-18 11:45 ` Tassilo Horn
@ 2010-11-18 12:10   ` Tassilo Horn
  2010-11-18 15:31     ` Drew Adams
  0 siblings, 1 reply; 6+ messages in thread
From: Tassilo Horn @ 2010-11-18 12:10 UTC (permalink / raw)
  To: help-gnu-emacs

Tassilo Horn <tassilo@member.fsf.org> writes:

Just one comment:

> (defun mark-next-word ()
>   (interactive)
>   (skip-chars-forward "[:space:]")
>   (set-mark (point))
>   (forward-word))

Most probably, it's better to use (push-mark) instead of (set-mark),
just like Deniz suggested.  That allows jumping back to the former mark
using `C-u C-SPC' (repeatedly).

Bye,
Tassilo




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

* RE: Elisp code to choose a region covering a word
  2010-11-18 12:10   ` Tassilo Horn
@ 2010-11-18 15:31     ` Drew Adams
  0 siblings, 0 replies; 6+ messages in thread
From: Drew Adams @ 2010-11-18 15:31 UTC (permalink / raw)
  To: 'Tassilo Horn', help-gnu-emacs

What would you do it interactively?  Then just do that and create a keyboard
macro. 

Or look up the commands associated with the keys you use and use those commands
to define your command.

Interactively, you would just do this: `C-SPC M-f...'  (This assumes
transient-mark-mode is turned on, which it is now by default.)

`C-SPC' is `set-mark-command'. But as others have pointed out, all you need in
your function is `push-mark'.

`M-f' is `forward-word'.

The mark is deactivated automatically after each command, but you can prevent
that by setting `mark-active' to non-nil.

(defun foo (n)
  "Mark the next N words.  N is the prefix arg."
  (interactive "p")
  (push-mark)
  (forward-word n)
  (setq mark-active  t))




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

* Re: Elisp code to choose a region covering a word
  2010-11-18 11:46 ` Deniz Dogan
@ 2010-11-19  6:49   ` ishi soichi
  0 siblings, 0 replies; 6+ messages in thread
From: ishi soichi @ 2010-11-19  6:49 UTC (permalink / raw)
  To: Deniz Dogan; +Cc: help-gnu-emacs, Tassilo Horn

[-- Attachment #1: Type: text/plain, Size: 425 bytes --]

Wow, thanks for your helps.  I have realized there are many ways to do it.

My plan is to develop an elisp program like,

choose each word with the least possible key-bindings, and by hitting "RET",
implement text-translator.el for Russian for example.
Eventually, the translated words will be automatically listed in an
org-file.

It is a long way to go.


At least the first step did work.  Thanks to you all.

soichi

>
>

[-- Attachment #2: Type: text/html, Size: 839 bytes --]

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

end of thread, other threads:[~2010-11-19  6:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-18 10:22 Elisp code to choose a region covering a word ishi soichi
2010-11-18 11:45 ` Tassilo Horn
2010-11-18 12:10   ` Tassilo Horn
2010-11-18 15:31     ` Drew Adams
2010-11-18 11:46 ` Deniz Dogan
2010-11-19  6:49   ` ishi soichi

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).