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

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

Re: Mark whole Word


From: Xah Lee
Subject: Re: Mark whole Word
Date: Tue, 9 Dec 2008 13:23:23 -0800 (PST)
User-agent: G2/1.0

over comp.emacs, someone asked a way to select whole word, where mark-
word is somewhat inconvenient. I think this is a frequently desired
operation.

Here's some suggestions i wrote up on this issue, which i myself
wanted and thought about for the past year.

• Suggestions on Emacs's mark-word Command
  http://xahlee.org/emacs/modernization_mark-word.html

plain text version follows. (formatting may be screwed up)

------------------
Xah Lee, 2008-12-09

Emacs has a command to select the current word, mark-word, with
default shortcut of “Alt+@”. Selecting the current word is a
frequently needed operation. However, emacs's mark-word command is
inconvenient. It does not select the whole word. It only select from
the cursor position to the end of the word. For example, if your word
is “transmission”, and your cursor is at the “m”, then it will select
just “mission”. To select the whole word, you need to move the cursor
to the beginning of the word first. Also, mark-word has a feature that
if you repeat the command, then it extend the selection to the next
word to the right.

Here we suggest a improvement. We want a command that select the whole
word. When repeated, it should select the next larger syntactic unit.
In human languages, that would be sentence, then paragraph, then whole
buffer. In computer languages, the sequence would be: current
identifier, current expression, current construct (or line), current
block or defun. Similarly, if the lang is lisp, it simply extend the
selection to the next outer parens. For detailed example of this, see:
A Text Editor Feature: Extend Selection By Semantic Unit.

Here's the code that implements the above idea for lisp (or any simply
nested syntax):

;; by Nikolaj Schumacher, 2008-10-20. Released under GPL.
(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))

;; by Nikolaj Schumacher, 2008-10-20. Released under GPL.
(defun extend-selection (arg &optional incremental)
  "Select the current word.
Subsequent calls expands the selection to larger semantic unit."
  (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)
        (extend-selection (1- arg) t)
      (if (looking-at "\\=\\(\\s_\\|\\sw\\)*\\_>")
          (goto-char (match-end 0))
        (unless (memq (char-before) '(?\) ?\"))
          (forward-sexp)))
      (mark-sexp -1))))

(global-set-key (kbd "M-8") 'extend-selection)

In the above, “Alt+8” is assigned to the command, because selecting
whole word is a commonly needed operation, and “Alt+8” is one key
simpler than “Alt+@” on a standard American layout PC keyboard.

Pressing “Alt+8” will select the current whole word. Press it again
will extend the selection to the next outer parens. The above code
effectively does extend selection to higher level of semantic unit for
lisp or simply nested syntax. It does not work in more complicated
nesting, such as HTML/XML. For the code to work in other langs like
Java, Perl, Python, XML, it'll need some more work.

Another frequently needed operation is to select text inside quotes.
This is especially frequently needed in most languages such as C, C++,
Java, JavaScript, Perl, Python, PHP, HTML/XML. Ideally, the extend-
selection above should do it, by simply invoking the command twice, or
just once when cursor is on the quote, but since the above is limited
to lisp syntax only, so here's a additional command as a workaround to
do the frequently needed operation of selecting text inside quote
pairs.


(defun select-text-in-quote ()
"Select text between the nearest left and right delimiters.
Delimiters are paired characters: ()[]<>«»“”‘’「」, including \"\"."
 (interactive)
 (let (b1 b2)
   (skip-chars-backward "^<>(“{[「«\"‘")
   (setq b1 (point))
   (skip-chars-forward "^<>)”}]」»\"’")
   (setq b2 (point))
   (set-mark b1)
   )
 )

(global-set-key (kbd "M-*") 'select-text-in-quote)


With both of the above defined, pressing “Alt+8” will select whole
word (or extend current selection). Pressing “Alt+Shift+8” (that is:
“Alt+*”) will select the text inside matching quotes.

The above is included in the ergo map: A Ergonomic Keyboard Shortcut
Layout For Emacs.

  Xah
∑ http://xahlee.org/

☄


On Dec 9, 8:07 am, Michael Schuster <schusterSoc...@enertex.de> wrote:
> Hi,
>
> I'm searching for way to mark a word. I know the mark-word function, but
> this marks only the word from the cursor. So if the cursor is at the 'd' in
> the word "Wordmarker" only "dmarker" is marked. I want a function to mark
> the whole word regardingless where in the word the cursor is.
>
> Does anybody know such a function?
> Any help is welcome.
>
> Thanks in advance
> Michael
> --
> Remove the sport from my address to obtain emailwww.enertex.de- Innovative 
> Systemlösungen der Energie- und Elektrotechnik



reply via email to

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