Function: current-word

current-word is a byte-compiled function defined in simple.el.gz.

Signature

(current-word &optional STRICT REALLY-WORD)

Documentation

Return the word at or near point, as a string.

The return value includes no text properties.

If optional arg STRICT is non-nil, return nil unless point is within or adjacent to a word, otherwise look for a word within point's line. If there is no word anywhere on point's line, the value is nil regardless of STRICT.

By default, this function treats as a single word any sequence of characters that have either word or symbol syntax. If optional arg REALLY-WORD is non-nil, only characters of word syntax can constitute a word.

View in manual

Source Code

;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun current-word (&optional strict really-word)
  "Return the word at or near point, as a string.
The return value includes no text properties.

If optional arg STRICT is non-nil, return nil unless point is
within or adjacent to a word, otherwise look for a word within
point's line.  If there is no word anywhere on point's line, the
value is nil regardless of STRICT.

By default, this function treats as a single word any sequence of
characters that have either word or symbol syntax.  If optional
arg REALLY-WORD is non-nil, only characters of word syntax can
constitute a word."
  (save-excursion
    (let* ((oldpoint (point)) (start (point)) (end (point))
	   (syntaxes (if really-word "w" "w_"))
	   (not-syntaxes (concat "^" syntaxes)))
      (skip-syntax-backward syntaxes) (setq start (point))
      (goto-char oldpoint)
      (skip-syntax-forward syntaxes) (setq end (point))
      (when (and (eq start oldpoint) (eq end oldpoint)
		 ;; Point is neither within nor adjacent to a word.
		 (not strict))
	;; Look for preceding word in same line.
	(skip-syntax-backward not-syntaxes (line-beginning-position))
	(if (bolp)
	    ;; No preceding word in same line.
	    ;; Look for following word in same line.
	    (progn
	      (skip-syntax-forward not-syntaxes (line-end-position))
	      (setq start (point))
	      (skip-syntax-forward syntaxes)
	      (setq end (point)))
	  (setq end (point))
	  (skip-syntax-backward syntaxes)
	  (setq start (point))))
      ;; If we found something nonempty, return it as a string.
      (unless (= start end)
	(buffer-substring-no-properties start end)))))