unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How do I highlight word at point?
@ 2008-10-19  2:16 Wei Weng
  2008-10-19  3:02 ` Giorgos Keramidas
                   ` (3 more replies)
  0 siblings, 4 replies; 24+ messages in thread
From: Wei Weng @ 2008-10-19  2:16 UTC (permalink / raw)
  To: help-gnu-emacs

The title pretty much said it. :)


Thanks
Wei


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

* Re: How do I highlight word at point?
  2008-10-19  2:16 How do I highlight word at point? Wei Weng
@ 2008-10-19  3:02 ` Giorgos Keramidas
  2008-10-19 12:09   ` Francis Moreau
                     ` (2 more replies)
  2008-10-19  5:44 ` Xah
                   ` (2 subsequent siblings)
  3 siblings, 3 replies; 24+ messages in thread
From: Giorgos Keramidas @ 2008-10-19  3:02 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Sat, 18 Oct 2008 22:16:10 -0400
> From: Wei Weng < wweng at acedsl.com >
> Subject: How do I highlight word at point?
> Newsgroups: gnu.emacs.help
> Message-ID: <1224382569.209484@nntp.acecape.com>
>
> The title pretty much said it. :)

Please include the full question in the body of the post too in future
posts.  It makes things much easier to read in some environments :-)

I think what you asked may be done by typing `M-b M-@'.

If you have enabled cua-mode you can also use something like `C-<left>
S-C-<right>'.



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

* Re: How do I highlight word at point?
  2008-10-19  2:16 How do I highlight word at point? Wei Weng
  2008-10-19  3:02 ` Giorgos Keramidas
@ 2008-10-19  5:44 ` Xah
  2008-10-19 17:00   ` Nikolaj Schumacher
       [not found]   ` <mailman.1521.1224435664.25473.help-gnu-emacs@gnu.org>
  2008-10-19 23:35 ` Drew Adams
  2008-10-21  3:43 ` htbest2000
  3 siblings, 2 replies; 24+ messages in thread
From: Xah @ 2008-10-19  5:44 UTC (permalink / raw)
  To: help-gnu-emacs

Wei Weng wrote:
«how to highlight word at point»

You can do this:

(defun select-word ()
"Select a word under cursor.
“word” here is considered any alphenumeric sequence with “_” or “-”."
 (interactive)
 (let (b1 b2)
   (skip-chars-backward "-_A-Za-z0-9")
   (setq b1 (point))
   (skip-chars-forward "-_A-Za-z0-9")
   (setq b2 (point))
   (set-mark b1)
   )
 )

(transient-mark-mode t)

(global-set-key (kbd "M-8") 'select-word)

Then, Alt+8 will select the current word.

-----------------

This select-word command will be incorporated into the ergonomic
keymap design in the future. I just haven't decided Alt+8 is the best
spot, but i've been using it for a year. See
http://xahlee.org/emacs/ergonomic_emacs_keybinding.html

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: How do I highlight word at point?
  2008-10-19  3:02 ` Giorgos Keramidas
@ 2008-10-19 12:09   ` Francis Moreau
  2008-10-19 16:19     ` Nikolaj Schumacher
       [not found]   ` <mailman.1504.1224419414.25473.help-gnu-emacs@gnu.org>
  2008-10-29 22:13   ` Francis Moreau
  2 siblings, 1 reply; 24+ messages in thread
From: Francis Moreau @ 2008-10-19 12:09 UTC (permalink / raw)
  To: help-gnu-emacs

Giorgos Keramidas <keramida@ceid.upatras.gr> writes:

> Please include the full question in the body of the post too in future
> posts.  It makes things much easier to read in some environments :-)

eh ? what kind of environment are you using ?

It looks like it's high time to update ;)

>
> I think what you asked may be done by typing `M-b M-@'.
>

M-b is bind to 'backward-word'.

Which function 'M-b M-@' is bind in your "special" environment ?

thanks

Francis





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

* Re: How do I highlight word at point?
       [not found]   ` <mailman.1504.1224419414.25473.help-gnu-emacs@gnu.org>
@ 2008-10-19 13:24     ` Thorsten Bonow
  2008-10-19 15:54       ` Francis Moreau
  2008-10-19 15:14     ` Giorgos Keramidas
  1 sibling, 1 reply; 24+ messages in thread
From: Thorsten Bonow @ 2008-10-19 13:24 UTC (permalink / raw)
  To: help-gnu-emacs

>>>>> "Francis" == Francis Moreau <francis.moro@gmail.com> writes:

    >> I think what you asked may be done by typing `M-b M-@'.

    Francis> M-b is bind to 'backward-word'.

    Francis> Which function 'M-b M-@' is bind in your "special" environment ?

    Francis> thanks

    Francis> Francis

Hi,

he never claimed that "M-b M-@" was bound to a function: "M-b" is indeed bound to
`backward-word'. But then the point is at the beginning of a word and "M-@",
which is bound to `mark-word' will do the trick.

Come on, you could at least have tried it once. Emacs seldom explodes on
keyboard input ;-)

Toto 



-- 
Contact information and PGP key at
http://www.withouthat.org/~toto/contact.html

There are no good lawyers. There may be lady wrestlers and Catholic
universities. There may be military intelligence. But a good lawyer is
a contradiction in terms.

Friedman, Kinky (1993), Greenwich Killing Time. New York (Wings
Books), 12


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

* Re: How do I highlight word at point?
       [not found]   ` <mailman.1504.1224419414.25473.help-gnu-emacs@gnu.org>
  2008-10-19 13:24     ` Thorsten Bonow
@ 2008-10-19 15:14     ` Giorgos Keramidas
  2008-10-19 15:52       ` Francis Moreau
  1 sibling, 1 reply; 24+ messages in thread
From: Giorgos Keramidas @ 2008-10-19 15:14 UTC (permalink / raw)
  To: help-gnu-emacs

On Sun, 19 Oct 2008 14:09:09 +0200, Francis Moreau <francis.moro@gmail.com> wrote:
> Giorgos Keramidas <keramida@ceid.upatras.gr> writes:
>> Please include the full question in the body of the post too in
>> future posts.  It makes things much easier to read in some
>> environments :-)
>
> eh ? what kind of environment are you using ?
> It looks like it's high time to update ;)

It should be easy to find out: Gnus.  It's shown in the User-Agent
header of my posts, and I'm actually quite fond of it.

I wasn't referring to *my* environment, which is mostly `ok' for all my
email and news needs.  There are news readers that go out of their way
to _hide_ the header though.  It's a very common form of courtesy to
spare the reader the trouble of going back and forth, by typing fully
legible, complete sentences in the body of email & news messages.

>> I think what you asked may be done by typing `M-b M-@'.
>
> M-b is bind to 'backward-word'.
>
> Which function 'M-b M-@' is bind in your "special" environment ?

M-b is backward-word, but I should have written a more clear explanation
of what I was aiming for.

Yes, `M-@' is bound to `mark-word'.  The important detail is that the
`mark-word' function selects the part of the word that is _after_ the
current location of the point.

So if the current `point' is already at the start of a word, then `M-@'
will do the right thing.

When the point is in the middle of a word, however, you can move the
point to the start of the current word with `M-b' and then use `M-@' to
mark the full word.




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

* Re: How do I highlight word at point?
  2008-10-19 15:14     ` Giorgos Keramidas
@ 2008-10-19 15:52       ` Francis Moreau
  0 siblings, 0 replies; 24+ messages in thread
From: Francis Moreau @ 2008-10-19 15:52 UTC (permalink / raw)
  To: Giorgos Keramidas; +Cc: help-gnu-emacs

On Sun, Oct 19, 2008 at 5:14 PM, Giorgos Keramidas
<keramida@ceid.upatras.gr> wrote:
> On Sun, 19 Oct 2008 14:09:09 +0200, Francis Moreau <francis.moro@gmail.com> wrote:
>> Giorgos Keramidas <keramida@ceid.upatras.gr> writes:
>>> Please include the full question in the body of the post too in
>>> future posts.  It makes things much easier to read in some
>>> environments :-)
>>
>> eh ? what kind of environment are you using ?
>> It looks like it's high time to update ;)
>
> It should be easy to find out: Gnus.  It's shown in the User-Agent
> header of my posts, and I'm actually quite fond of it.
>

yes, I saw that but you seem to have missed the ';)'

> I wasn't referring to *my* environment, which is mostly `ok' for all my
> email and news needs.  There are news readers that go out of their way
> to _hide_ the header though.

never hear about them.

>>> I think what you asked may be done by typing `M-b M-@'.
>>
>> M-b is bind to 'backward-word'.
>>
>> Which function 'M-b M-@' is bind in your "special" environment ?
>
> M-b is backward-word, but I should have written a more clear explanation
> of what I was aiming for.
>
> Yes, `M-@' is bound to `mark-word'.  The important detail is that the
> `mark-word' function selects the part of the word that is _after_ the
> current location of the point.
>
> So if the current `point' is already at the start of a word, then `M-@'
> will do the right thing.
>
> When the point is in the middle of a word, however, you can move the
> point to the start of the current word with `M-b' and then use `M-@' to
> mark the full word.
>

yes sorry I stupidely did 'C-h k' and try to type your suggestion.

-- 
Francis




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

* Re: How do I highlight word at point?
  2008-10-19 13:24     ` Thorsten Bonow
@ 2008-10-19 15:54       ` Francis Moreau
  0 siblings, 0 replies; 24+ messages in thread
From: Francis Moreau @ 2008-10-19 15:54 UTC (permalink / raw)
  To: Thorsten Bonow; +Cc: help-gnu-emacs

On Sun, Oct 19, 2008 at 3:24 PM, Thorsten Bonow
<thorsten.bonow@post.rwth-aachen.de> wrote:
> Come on, you could at least have tried it once. Emacs seldom explodes on
> keyboard input ;-)
>

Right I should have. But I stupidely only asked for help to emacs about the
whole key binding.

-- 
Francis




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

* Re: How do I highlight word at point?
  2008-10-19 12:09   ` Francis Moreau
@ 2008-10-19 16:19     ` Nikolaj Schumacher
  0 siblings, 0 replies; 24+ messages in thread
From: Nikolaj Schumacher @ 2008-10-19 16:19 UTC (permalink / raw)
  To: Francis Moreau; +Cc: help-gnu-emacs

Francis Moreau <francis.moro@gmail.com> wrote:

> Giorgos Keramidas <keramida@ceid.upatras.gr> writes:
>
>> Please include the full question in the body of the post too in future
>> posts.  It makes things much easier to read in some environments :-)
>
> eh ? what kind of environment are you using ?
>
> It looks like it's high time to update ;)

In Gnus you can just step through unread articles with SPC.  Normally
the subject is pretty unimportant, so it doubles the "work", if you have
to look up and read the subject.  Especially when

>>> The title pretty much said it. :)

has the same amount of characters as the question.  So it didn't save
any time or space to not put the question there.

>> I think what you asked may be done by typing `M-b M-@'.
>>
>
> M-b is bind to 'backward-word'.
>
> Which function 'M-b M-@' is bind in your "special" environment ?

In the /standard/ environment this does `backward-word' then
`mark-word', which puts the region around the current word.


But maybe the OP wanted to highlight occurrences of the word, not move
the region.  I've written code for that here:

http://nschum.de/src/emacs/highlight-symbol/


regards,
Nikolaj Schumacher




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

* Re: How do I highlight word at point?
  2008-10-19  5:44 ` Xah
@ 2008-10-19 17:00   ` Nikolaj Schumacher
       [not found]   ` <mailman.1521.1224435664.25473.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 24+ messages in thread
From: Nikolaj Schumacher @ 2008-10-19 17:00 UTC (permalink / raw)
  To: Xah; +Cc: help-gnu-emacs

Xah <xahlee@gmail.com> wrote:

> (defun select-word ()
> "Select a word under cursor.
> “word” here is considered any alphenumeric sequence with “_” or “-”."
>  (interactive)
>  (let (b1 b2)
>    (skip-chars-backward "-_A-Za-z0-9")
>    (setq b1 (point))
>    (skip-chars-forward "-_A-Za-z0-9")
>    (setq b2 (point))
>    (set-mark b1)
>    )
>  )

Why not use syntactic tables to determine what a word is?  That way it
would work for non-English languages and use less code.

(defun mark-current-word ()
  "Place the region around the word at point."
  (interactive)
  (forward-word 1)
  (mark-word -1))


I think I'll also use this (replacing M-@).  But it should still be able
to grow the region... Let's see:

(defun mark-current-word (arg &optional incremental)
  "Place the region around the word at point.
ARG determines how many following words to include.  When INCREMENTAL is
non-nil, extend the existing region."
  (interactive (list (prefix-numeric-value current-prefix-arg)
                     (or (and transient-mark-mode mark-active)
                         (eq last-command this-command))))
  (and incremental
       (> (* (signum arg) (- (mark) (point))) 0)
       (exchange-point-and-mark))
  (forward-word arg)
  (unless incremental
    (mark-word (- arg))))

(global-set-key "\M-@" 'mark-current-word)

(improvements welcome)


regards,
Nikolaj Schumacher




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

* Re: How do I highlight word at point?
       [not found]   ` <mailman.1521.1224435664.25473.help-gnu-emacs@gnu.org>
@ 2008-10-19 20:10     ` Xah
  2008-10-19 22:16       ` Drew Adams
                         ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: Xah @ 2008-10-19 20:10 UTC (permalink / raw)
  To: help-gnu-emacs

On Oct 19, 10:00 am, Nikolaj Schumacher <m...@nschum.de> wrote:
> Xah <xah...@gmail.com> wrote:
> > (defun select-word ()
> > "Select a word under cursor.
> > “word” here is considered any alphenumeric sequence with “_” or “-”."
> >  (interactive)
> >  (let (b1 b2)
> >    (skip-chars-backward "-_A-Za-z0-9")
> >    (setq b1 (point))
> >    (skip-chars-forward "-_A-Za-z0-9")
> >    (setq b2 (point))
> >    (set-mark b1)
> >    )
> >  )
>
> Why not use syntactic tables to determine what a word is?  That way it
> would work for non-English languages and use less code.
>
> (defun mark-current-word ()
>   "Place the region around the word at point."
>   (interactive)
>   (forward-word 1)
>   (mark-word -1))
>
> I think I'll also use this (replacing M-@).  But it should still be able
> to grow the region... Let's see:
>
> (defun mark-current-word (arg &optional incremental)
>   "Place the region around the word at point.
> ARG determines how many following words to include.  When INCREMENTAL is
> non-nil, extend the existing region."
>   (interactive (list (prefix-numeric-value current-prefix-arg)
>                      (or (and transient-mark-mode mark-active)
>                          (eq last-command this-command))))
>   (and incremental
>        (> (* (signum arg) (- (mark) (point))) 0)
>        (exchange-point-and-mark))
>   (forward-word arg)
>   (unless incremental
>     (mark-word (- arg))))
>
> (global-set-key "\M-@" 'mark-current-word)
>
> (improvements welcome)

Hi Nik, i have some struggling thoughts about this.

here's what i would want to have ideally:

press a key, expand selection to the current word, press again, expand
to the next semantic unit (with respect to the current lang/mode),
press again, expand further.

This idea is borrowed from Mathematica's IDE (aka the Front End).
I have wrote full description of this feature, see:

http://xahlee.org/emacs/syntax_tree_walk.html

I have a draft version for over a year now:

(defun mark-semantic-unit ()
"Select a semantic unit.
Select a word under cursor. “word” here means alphanumeric sequence
plus “_” or “-”.

When this function is run for the first time and there is no active
region,
then it selects the current word (i.e. sequence of alphanumerics plus
hyphen or underscore).
When the function is run again, or if there is a region, it extends
the
selection to the next level of enclosing delimiters."
 (interactive)
 (require 'thingatpt)
(let ((deactivate-mark nil))
(if
    (or (and (eq last-command this-command))
        (and transient-mark-mode mark-active))

    (let ((e1 (region-beginning))  (e2 (region-end)) b1 b2)
      (goto-char (- e1 0))
      (skip-chars-backward "^<>(“{[「«\"‘")
      (setq b1 (point))
      (goto-char (+ e2 0))
      (skip-chars-forward "^<>)”}]」»\"’")
      (setq b2 (point))
      (set-mark b1)
    )

  (cond
   ( (looking-at "\\_<")
    (setq pt (point))
    (push-mark pt nil t)
    (forward-symbol 1)
    )
   (t
    (forward-symbol -1)
    (setq pt (point))
    (push-mark pt nil t)
    (forward-symbol 1)
    )
   )
  )
)
)

this version works when your cursor is inside a matching pair. For
example, if your cursor is at A in:
(something here A and that)

then invoke twice it'll select the whole paren.

However, it doesn't work when the cursor is in a screwed nested
position. For example:

(something here A (and) that)

if your cursor is at A, invoke twice won't get the whole outer paren,
because the code doesn't track nested parens, it only looks for chars
in a dumb way.

Ideally, this mark-semantic-unit should just extend a semantic unit,
where what's considered a semantic unit depends on the language. But
this i imagine would be rather a non-trivial problem. I am not sure
emacs's syntax table system is rich enough to be used for this.

So, taking a step back, i tried to have the code just extending to
outer matching pairs. (effectively a extend-semantic-unit for lisp) I
think now i can make this code work by using the sexp navigation
functions. I should code this soon.

If anyone can implement this, or better yet, write a proper extend-
semantic-unit for langs with C like syntax (C++,java,javascript),
that'd be a killer feature i think.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 24+ messages in thread

* RE: How do I highlight word at point?
  2008-10-19 20:10     ` Xah
@ 2008-10-19 22:16       ` Drew Adams
  2008-10-19 23:58       ` Nikolaj Schumacher
       [not found]       ` <mailman.1544.1224460736.25473.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 24+ messages in thread
From: Drew Adams @ 2008-10-19 22:16 UTC (permalink / raw)
  To: 'Xah', help-gnu-emacs

> here's what i would want to have ideally:
> press a key, expand selection to the current word, press again, expand
> to the next semantic unit (with respect to the current lang/mode),
> press again, expand further.
> 
> This idea is borrowed from Mathematica's IDE (aka the Front End).
> I have wrote full description of this feature, see:
> http://xahlee.org/emacs/syntax_tree_walk.html
> 
> I have a draft version for over a year now:
> 
> (defun mark-semantic-unit ()
> "Select a semantic unit.
> Select a word under cursor. "word" here means alphanumeric sequence
> plus "_" or "-".
> 
> When this function is run for the first time and there is no active
> region, then it selects the current word (i.e. sequence of
> alphanumerics plus hyphen or underscore).
> When the function is run again, or if there is a region, it extends
> the selection to the next level of enclosing delimiters."
>  (interactive)
>  (require 'thingatpt)
> (let ((deactivate-mark nil))
> (if (or (and (eq last-command this-command))
>         (and transient-mark-mode mark-active))
>     (let ((e1 (region-beginning))  (e2 (region-end)) b1 b2)
>       (goto-char (- e1 0))
>       (skip-chars-backward "^<>("{[?<\"'")
>       (setq b1 (point))
>       (goto-char (+ e2 0))
>       (skip-chars-forward "^<>)"}]?>\"'")
>       (setq b2 (point))
>       (set-mark b1))
>   (cond
>    ( (looking-at "\\_<")
>     (setq pt (point))
>     (push-mark pt nil t)
>     (forward-symbol 1))
>    (t
>     (forward-symbol -1)
>     (setq pt (point))
>     (push-mark pt nil t)
>     (forward-symbol 1))))))
> 
> this version works when your cursor is inside a matching pair. For
> example, if your cursor is at A in: (something here A and that)
> then invoke twice it'll select the whole paren.
> 
> However, it doesn't work when the cursor is in a screwed nested
> position. For example: (something here A (and) that)
> if your cursor is at A, invoke twice won't get the whole outer paren,
> because the code doesn't track nested parens, it only looks for chars
> in a dumb way.
> 
> Ideally, this mark-semantic-unit should just extend a semantic unit,
> where what's considered a semantic unit depends on the language. But
> this i imagine would be rather a non-trivial problem. I am not sure
> emacs's syntax table system is rich enough to be used for this.
> 
> So, taking a step back, i tried to have the code just extending to
> outer matching pairs. (effectively a extend-semantic-unit for lisp) I
> think now i can make this code work by using the sexp navigation
> functions. I should code this soon.
> 
> If anyone can implement this, or better yet, write a proper extend-
> semantic-unit for langs with C like syntax (C++,java,javascript),
> that'd be a killer feature i think.

I'm not sure that it would help you toward what you want to do, but you might
look at `thing-cmds.el'. It has some commands for selecting successive things of
different types.
http://www.emacswiki.org/emacs/ThingAtPointCommands.

See also perhaps:
http://www.emacswiki.org/emacs/MarkCommands#mark-a-word-or-thing.






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

* RE: How do I highlight word at point?
  2008-10-19  2:16 How do I highlight word at point? Wei Weng
  2008-10-19  3:02 ` Giorgos Keramidas
  2008-10-19  5:44 ` Xah
@ 2008-10-19 23:35 ` Drew Adams
  2008-10-19 23:39   ` Drew Adams
  2008-10-21  3:43 ` htbest2000
  3 siblings, 1 reply; 24+ messages in thread
From: Drew Adams @ 2008-10-19 23:35 UTC (permalink / raw)
  To: 'Wei Weng', help-gnu-emacs

> The title pretty much said it. :)

Not really. What do you mean by "highlight"? Most replies so far have assumed
that you mean just select the word (region highlighting).

If you mean instead that you want to change the foreground or background color
or something else about the appearance of the word using a text property or an
overlay, then there are many ways to do that. These alternatives might help:

1. Select the current word (or whatever you want to highlight). For example,
command `select-thing-near-point' in `thing-cmds.el' will do that. (I bind that
command to `M-@'.) 

Then hit `C-mouse-2' to get the `Text Properties' menu. Then do one of the
following:

a. Choose anything in the `Face', `Foreground', or `Background' submenus.
   Example: `Other' (or `M-o o') lets you specify a face to use.

b. If you use libraries `facemenu+.el' and `highlight.el', then choose anything
in the `Highlight' submenu. Example: `Highlight Region/Buffer'. (I bind that
command to `C-x C-y'.)

2. If you use `facemenu+.el' and `highlight.el', then you can alternatively use
command `hlt-highlighter' (or `Text Properties > Highlight > Highlighter Pen')
and then drag the mouse like a highlighter pen. (I bind that command to `C-x
mouse-2'.)

There are other commands in these libraries that will also let you highlight a
word (or whatever).

* http://www.emacswiki.org/cgi-bin/wiki/FaceMenuPlus - facemenu+.el
* http://www.emacswiki.org/emacs/HighLight - highlight.el
* http://www.emacswiki.org/emacs/ThingAtPointCommands - thing-cmds.el

See also nodes `Editing Format Info' and `Format Faces' in the Emacs menu - it
describes the vanilla `Text Properties' menu (e.g. 1a, above, but not 1b or 2).

See also http://www.emacswiki.org/emacs/HighlightTemporarily for related
possibilities.

HTH. Next time, please try to be more specific in your request - "highlight word
at point" can mean several different things in Emacs. The more specific you are,
the better the help you will get and the less likely the thread will wander off
topic.






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

* RE: How do I highlight word at point?
  2008-10-19 23:35 ` Drew Adams
@ 2008-10-19 23:39   ` Drew Adams
  0 siblings, 0 replies; 24+ messages in thread
From: Drew Adams @ 2008-10-19 23:39 UTC (permalink / raw)
  To: 'Wei Weng', help-gnu-emacs

I said:

> See also nodes `Editing Format Info' and `Format Faces' in 
> the Emacs menu - it describes the vanilla `Text Properties' menu

I meant:

See also nodes `Editing Format Info' and `Format Faces' in
the Emacs manual - they describe the vanilla `Text Properties' menu





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

* Re: How do I highlight word at point?
  2008-10-19 20:10     ` Xah
  2008-10-19 22:16       ` Drew Adams
@ 2008-10-19 23:58       ` Nikolaj Schumacher
       [not found]       ` <mailman.1544.1224460736.25473.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 24+ messages in thread
From: Nikolaj Schumacher @ 2008-10-19 23:58 UTC (permalink / raw)
  To: Xah; +Cc: help-gnu-emacs

Xah <xahlee@gmail.com> wrote:

> press a key, expand selection to the current word, press again, expand
> to the next semantic unit (with respect to the current lang/mode),
> press again, expand further.

I have a strong interest in such a function, as well.

> However, it doesn't work when the cursor is in a screwed nested
> position. For example:
>
> (something here A (and) that)

That case I can fix.  Here's some code I wrote a while ago...

(defun my-mark-sexp (arg &optional incremental)
  "Mark the sexp surrounding point.
Subsequent calls mark higher levels of sexps."
  (interactive (list (prefix-numeric-value current-prefix-arg)
                     (or (and transient-mark-mode mark-active)
                         (eq last-command this-command))))
  (if incremental
      (progn
        (up-list (- arg))
        (forward-sexp)
        (mark-sexp -1))
    (if (> arg 1)
        (my-mark-sexp (1- arg) t)
      (re-search-forward "\\_>")
      (mark-sexp -1))))

> Ideally, this mark-semantic-unit should just extend a semantic unit,
> where what's considered a semantic unit depends on the language. But
> this i imagine would be rather a non-trivial problem. I am not sure
> emacs's syntax table system is rich enough to be used for this.

As you can see from your own code, character syntax isn't enough.  There
would have to be a real parser involved to detect where statements start
and end.  As far as I know even Semantic doesn't parse any function
bodies, and that's probably the smartest lib we have.  (Luckily lisp is
that easy to parse.)

Maybe there is an adequate heuristic...



regards,
Nikolaj Schumacher




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

* Re: How do I highlight word at point?
       [not found]       ` <mailman.1544.1224460736.25473.help-gnu-emacs@gnu.org>
@ 2008-10-20  3:46         ` Xah
  2008-10-20 12:31           ` Nikolaj Schumacher
       [not found]           ` <mailman.1579.1224505887.25473.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 24+ messages in thread
From: Xah @ 2008-10-20  3:46 UTC (permalink / raw)
  To: help-gnu-emacs

On Oct 19, 4:58 pm, Nikolaj Schumacher <m...@nschum.de> wrote:
> Xah <xah...@gmail.com> wrote:
> > press a key, expand selection to the current word, press again, expand
> > to the next semantic unit (with respect to the current lang/mode),
> > press again, expand further.
>
> I have a strong interest in such a function, as well.
>
> > However, it doesn't work when the cursor is in a screwed nested
> > position. For example:
>
> > (something here A (and) that)
>
> That case I can fix.  Here's some code I wrote a while ago...
>
> (defun my-mark-sexp (arg &optional incremental)
>   "Mark the sexp surrounding point.
> Subsequent calls mark higher levels of sexps."
>   (interactive (list (prefix-numeric-value current-prefix-arg)
>                      (or (and transient-mark-mode mark-active)
>                          (eq last-command this-command))))
>   (if incremental
>       (progn
>         (up-list (- arg))
>         (forward-sexp)
>         (mark-sexp -1))
>     (if (> arg 1)
>         (my-mark-sexp (1- arg) t)
>       (re-search-forward "\\_>")
>       (mark-sexp -1))))

Super.

is it possible to make it work with selecting a string?

suppose your cursor is on the word “thing” below:

([
«  ‹   "some thing here"  ›   »
{ ht}
「 chinese bracket 『more』  」
])

after second press, it'd be nice to select the string.

> > Ideally, this mark-semantic-unit should just extend a semantic unit,
> > where what's considered a semantic unit depends on the language. But
> > this i imagine would be rather a non-trivial problem. I am not sure
> > emacs's syntax table system is rich enough to be used for this.
>
> As you can see from your own code, character syntax isn't enough.  There
> would have to be a real parser involved to detect where statements start
> and end.  As far as I know even Semantic doesn't parse any function
> bodies, and that's probably the smartest lib we have.  (Luckily lisp is
> that easy to parse.)
>
> Maybe there is an adequate heuristic...

“Semantic”? is that a elisp package?

yes, i think heuristics will do sufficient job. C syntax langs (java,
javascript, C, C++, C#, LSL, and even to large extent perl), has
little tell tail signs good enough for a practical solution. typically
just of this form:

import xyz;

f f (type x, type y) {
...;
...;
}

to start with, we can have the cmd such that:
one press → select current word
2nd press → current string if inside string
press again → current line
press again → current content inside {}
press again → next outer braces
press again → whole function def

i think this is not difficult to implement this to the degree it works
correctly 95% of time.

the nxml and js2 packages contains full parser for xml and javascript.
I think eventually lang modes in emacs would have full lexers.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: How do I highlight word at point?
  2008-10-20  3:46         ` Xah
@ 2008-10-20 12:31           ` Nikolaj Schumacher
       [not found]           ` <mailman.1579.1224505887.25473.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 24+ messages in thread
From: Nikolaj Schumacher @ 2008-10-20 12:31 UTC (permalink / raw)
  To: Xah; +Cc: help-gnu-emacs

Xah <xahlee@gmail.com> wrote:

> is it possible to make it work with selecting a string?

Yes.  It seems so, but the code gets very complicated if you try to do
the right thing in all places.  For instance, inside strings, you might
prefer the text-mode syntax table for best results.

I've written a bit more of this and might turn it into a package once it's
more tested.

(defun semnav-up (arg)
  (interactive "p")
  (when (nth 3 (syntax-ppss))
    (if (> arg 0)
        (progn
          (skip-syntax-forward "^\"")
          (goto-char (1+ (point)))
          (decf arg))
      (skip-syntax-backward "^\"")
      (goto-char (1- (point)))
      (incf arg)))
  (up-list arg))

(defun semnav-mark (arg &optional incremental)
  "Mark the symbol surrounding point.
Subsequent calls mark higher levels of sexps."
  (interactive (list (prefix-numeric-value current-prefix-arg)
                     (or (and transient-mark-mode mark-active)
                         (eq last-command this-command))))
  (if incremental
      (progn
        (semnav-up (- arg))
        (forward-sexp)
        (mark-sexp -1))
    (if (> arg 1)
        (semnav-mark (1- arg) t)
      (if (looking-at "\\=\\(\\s_\\|\\sw\\)*\\_>")
          (goto-char (match-end 0))
        (unless (memq (char-before) '(?\) ?\"))
          (forward-sexp)))
      (mark-sexp -1))))



> “Semantic”? is that a elisp package?

Yes.  It is part of CEDET.
http://cedet.sourceforge.net/semantic.shtml




regards,
Nikolaj Schumacher




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

* Re: How do I highlight word at point?
       [not found]           ` <mailman.1579.1224505887.25473.help-gnu-emacs@gnu.org>
@ 2008-10-20 18:43             ` Xah
  2008-10-21  8:57               ` Nikolaj Schumacher
  0 siblings, 1 reply; 24+ messages in thread
From: Xah @ 2008-10-20 18:43 UTC (permalink / raw)
  To: help-gnu-emacs

On Oct 20, 5:31 am, Nikolaj Schumacher <m...@nschum.de> wrote:
> Xah<xah...@gmail.com> wrote:
> > is it possible to make it work with selecting a string?
>
> Yes.  It seems so, but the code gets very complicated if you try to do
> the right thing in all places.  For instance, inside strings, you might
> prefer the text-mode syntax table for best results.
>
> I've written a bit more of this and might turn it into a package once it's
> more tested.
>
> (defun semnav-up (arg)
>   (interactive "p")
>   (when (nth 3 (syntax-ppss))
>     (if (> arg 0)
>         (progn
>           (skip-syntax-forward "^\"")
>           (goto-char (1+ (point)))
>           (decf arg))
>       (skip-syntax-backward "^\"")
>       (goto-char (1- (point)))
>       (incf arg)))
>   (up-list arg))
>
> (defun semnav-mark (arg &optional incremental)
>   "Mark the symbol surrounding point.
> Subsequent calls mark higher levels of sexps."
>   (interactive (list (prefix-numeric-value current-prefix-arg)
>                      (or (and transient-mark-mode mark-active)
>                          (eq last-command this-command))))
>   (if incremental
>       (progn
>         (semnav-up (- arg))
>         (forward-sexp)
>         (mark-sexp -1))
>     (if (> arg 1)
>         (semnav-mark (1- arg) t)
>       (if (looking-at "\\=\\(\\s_\\|\\sw\\)*\\_>")
>           (goto-char (match-end 0))
>         (unless (memq (char-before) '(?\) ?\"))
>           (forward-sexp)))
>       (mark-sexp -1))))

ummm... doesn't seems to work?

suppose i have

( "this that" [more ])

and cursor is on the word “that”.

I invoke this command twice, it selects the whole paren?

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: How do I highlight word at point?
  2008-10-19  2:16 How do I highlight word at point? Wei Weng
                   ` (2 preceding siblings ...)
  2008-10-19 23:35 ` Drew Adams
@ 2008-10-21  3:43 ` htbest2000
  3 siblings, 0 replies; 24+ messages in thread
From: htbest2000 @ 2008-10-21  3:43 UTC (permalink / raw)
  To: help-gnu-emacs

On 10月19日, 上午10时16分, Wei Weng <ww...@acedsl.com> wrote:
> The title pretty much said it. :)
>
> Thanks
> Wei

place the following code into your .emacs file and

; code begin
(defvar my-highlight-current-word-color-index 0 "the color index
increaser")
(defvar my-highlight-current-word-color-list
  (list 'hi-yellow 'hi-pink 'hi-blue 'hi-green 'hi-red)
  "color list")

(defun my-highlight-current-word ()
  "highlight current word"
  (interactive)
  (highlight-regexp (current-word)
		    (nth my-highlight-current-word-color-index
			 my-highlight-current-word-color-list))
  (incf my-highlight-current-word-color-index)
  (when (= my-highlight-current-word-color-index 4)
    (setq my-highlight-current-word-color-index 0)))

(defun my-unhighlight-current-word ()
  "un-highlight current word"
  (interactive)
  (unhighlight-regexp (current-word)))

;ESC-C-f8 to highlight
;ESC-ESC-C-f8 to unhighlight
(global-set-key (quote [27 C-f8]) (quote ht-highlight-current-word))
(global-set-key (quote [27 27 C-f8]) (quote ht-unhighlight-current-
word))
; code end

ESC-C-f8 or M-x my-highlight-current-word to highlight word in cursor,
ESC-ESC-C-f8 or M-x my-unhighlight-current-word to un-highlight

the color will be varied if you repeat highlight operate.


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

* Re: How do I highlight word at point?
  2008-10-20 18:43             ` Xah
@ 2008-10-21  8:57               ` Nikolaj Schumacher
  0 siblings, 0 replies; 24+ messages in thread
From: Nikolaj Schumacher @ 2008-10-21  8:57 UTC (permalink / raw)
  To: Xah; +Cc: help-gnu-emacs

Xah <xahlee@gmail.com> wrote:

> ummm... doesn't seems to work?
>
> suppose i have
>
> ( "this that" [more ])
>
> and cursor is on the word “that”.
>
> I invoke this command twice, it selects the whole paren?

Did you try it in an emacs-lisp buffer?  In other buffers the quotation
mark might not have the right syntax table entry.


regards,
Nikolaj Schumacher




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

* Re: How do I highlight word at point?
  2008-10-19  3:02 ` Giorgos Keramidas
  2008-10-19 12:09   ` Francis Moreau
       [not found]   ` <mailman.1504.1224419414.25473.help-gnu-emacs@gnu.org>
@ 2008-10-29 22:13   ` Francis Moreau
  2008-10-29 22:44     ` Andreas Politz
  2008-10-30 13:57     ` Scott Frazer
  2 siblings, 2 replies; 24+ messages in thread
From: Francis Moreau @ 2008-10-29 22:13 UTC (permalink / raw)
  To: help-gnu-emacs

Giorgos Keramidas <keramida@ceid.upatras.gr> writes:

> I think what you asked may be done by typing `M-b M-@'.

BTW, any ways to make this works for such words:

                            this-is-a-word

thanks

Francis


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

* Re: How do I highlight word at point?
  2008-10-29 22:13   ` Francis Moreau
@ 2008-10-29 22:44     ` Andreas Politz
  2008-10-30 20:22       ` Francis Moreau
  2008-10-30 13:57     ` Scott Frazer
  1 sibling, 1 reply; 24+ messages in thread
From: Andreas Politz @ 2008-10-29 22:44 UTC (permalink / raw)
  To: help-gnu-emacs

Francis Moreau wrote:
> Giorgos Keramidas <keramida@ceid.upatras.gr> writes:
> 
>> I think what you asked may be done by typing `M-b M-@'.
> 
> BTW, any ways to make this works for such words:
> 
>                             this-is-a-word
> 
> thanks
> 
> Francis

That's usually mark-sexp ( C-M-Space ). Have a look at

M-x apropos-command RET ^mark- RET

-ap


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

* Re: How do I highlight word at point?
  2008-10-29 22:13   ` Francis Moreau
  2008-10-29 22:44     ` Andreas Politz
@ 2008-10-30 13:57     ` Scott Frazer
  1 sibling, 0 replies; 24+ messages in thread
From: Scott Frazer @ 2008-10-30 13:57 UTC (permalink / raw)
  To: help-gnu-emacs

Francis Moreau wrote:
> Giorgos Keramidas <keramida@ceid.upatras.gr> writes:
> 
>> I think what you asked may be done by typing `M-b M-@'.
> 
> BTW, any ways to make this works for such words:
> 
>                             this-is-a-word
> 

Here's what I use:

(defun select-symbol-under-point ()
   "Select the symbol under point."
   (interactive)
   (skip-syntax-forward "w_")
   (set-mark (point))
   (skip-syntax-backward "w_")
   (exchange-point-and-mark))


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

* Re: How do I highlight word at point?
  2008-10-29 22:44     ` Andreas Politz
@ 2008-10-30 20:22       ` Francis Moreau
  0 siblings, 0 replies; 24+ messages in thread
From: Francis Moreau @ 2008-10-30 20:22 UTC (permalink / raw)
  To: help-gnu-emacs

Andreas Politz <politza@fh-trier.de> writes:
>
> That's usually mark-sexp ( C-M-Space ). Have a look at
>
> M-x apropos-command RET ^mark- RET

Thanks for the tip !

Francis


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

end of thread, other threads:[~2008-10-30 20:22 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-19  2:16 How do I highlight word at point? Wei Weng
2008-10-19  3:02 ` Giorgos Keramidas
2008-10-19 12:09   ` Francis Moreau
2008-10-19 16:19     ` Nikolaj Schumacher
     [not found]   ` <mailman.1504.1224419414.25473.help-gnu-emacs@gnu.org>
2008-10-19 13:24     ` Thorsten Bonow
2008-10-19 15:54       ` Francis Moreau
2008-10-19 15:14     ` Giorgos Keramidas
2008-10-19 15:52       ` Francis Moreau
2008-10-29 22:13   ` Francis Moreau
2008-10-29 22:44     ` Andreas Politz
2008-10-30 20:22       ` Francis Moreau
2008-10-30 13:57     ` Scott Frazer
2008-10-19  5:44 ` Xah
2008-10-19 17:00   ` Nikolaj Schumacher
     [not found]   ` <mailman.1521.1224435664.25473.help-gnu-emacs@gnu.org>
2008-10-19 20:10     ` Xah
2008-10-19 22:16       ` Drew Adams
2008-10-19 23:58       ` Nikolaj Schumacher
     [not found]       ` <mailman.1544.1224460736.25473.help-gnu-emacs@gnu.org>
2008-10-20  3:46         ` Xah
2008-10-20 12:31           ` Nikolaj Schumacher
     [not found]           ` <mailman.1579.1224505887.25473.help-gnu-emacs@gnu.org>
2008-10-20 18:43             ` Xah
2008-10-21  8:57               ` Nikolaj Schumacher
2008-10-19 23:35 ` Drew Adams
2008-10-19 23:39   ` Drew Adams
2008-10-21  3:43 ` htbest2000

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