(defun symbol-under-point () "Returns the symbol that the point is currently on. But only if it is longer than `completion-min-length'." (setq cmpl-saved-syntax (syntax-table)) (unwind-protect (progn (set-syntax-table cmpl-syntax-table) (cond ;; Cursor is on following-char and after preceding-char ((memq (char-syntax (following-char)) '(?w ?_)) (setq cmpl-saved-point (point) cmpl-symbol-start (scan-sexps (1+ cmpl-saved-point) -1) cmpl-symbol-end (scan-sexps cmpl-saved-point 1)) ;; Remove chars to ignore at the start. (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w) (goto-char cmpl-symbol-start) (forward-word 1) (setq cmpl-symbol-start (point)) (goto-char cmpl-saved-point))) ;; Remove chars to ignore at the end. (cond ((= (char-syntax (char-after (1- cmpl-symbol-end))) ?w) (goto-char cmpl-symbol-end) (forward-word -1) (setq cmpl-symbol-end (point)) (goto-char cmpl-saved-point))) ;; Return completion if the length is reasonable. (if (and (<= (cmpl-read-time-eval completion-min-length) (- cmpl-symbol-end cmpl-symbol-start)) (<= (- cmpl-symbol-end cmpl-symbol-start) (cmpl-read-time-eval completion-max-length))) (buffer-substring-no-properties cmpl-symbol-start cmpl-symbol-end))))) (set-syntax-table cmpl-saved-syntax))) (defun symbol-before-point () "Returns a string of the symbol immediately before point. Returns nil if there isn't one longer than `completion-min-length'." ;; This is called when a word separator is typed so it must be FAST ! (setq cmpl-saved-syntax (syntax-table)) (unwind-protect (progn (set-syntax-table cmpl-syntax-table) ;; Cursor is on following-char and after preceding-char (cond ((= (setq cmpl-preceding-syntax (char-syntax (preceding-char))) ?_) ;; Number of chars to ignore at end. (setq cmpl-symbol-end (point) cmpl-symbol-start (scan-sexps cmpl-symbol-end -1)) ;; Remove chars to ignore at the start. (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w) (goto-char cmpl-symbol-start) (forward-word 1) (setq cmpl-symbol-start (point)) (goto-char cmpl-symbol-end))) ;; Return value if long enough. (if (>= cmpl-symbol-end (+ cmpl-symbol-start (cmpl-read-time-eval completion-min-length))) (buffer-substring-no-properties cmpl-symbol-start cmpl-symbol-end))) ((= cmpl-preceding-syntax ?w) ;; chars to ignore at end (setq cmpl-saved-point (point) cmpl-symbol-start (scan-sexps cmpl-saved-point -1)) ;; take off chars. from end (forward-word -1) (setq cmpl-symbol-end (point)) ;; remove chars to ignore at the start (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w) (goto-char cmpl-symbol-start) (forward-word 1) (setq cmpl-symbol-start (point)))) ;; Restore state. (goto-char cmpl-saved-point) ;; Return completion if the length is reasonable (if (and (<= (cmpl-read-time-eval completion-min-length) (- cmpl-symbol-end cmpl-symbol-start)) (<= (- cmpl-symbol-end cmpl-symbol-start) (cmpl-read-time-eval completion-max-length))) (buffer-substring-no-properties cmpl-symbol-start cmpl-symbol-end))))) (set-syntax-table cmpl-saved-syntax))) (defun symbol-before-point-for-complete () ;; "Returns a string of the symbol immediately before point ;; or nil if there isn't one. Like symbol-before-point but doesn't trim the ;; end chars." ;; Cursor is on following-char and after preceding-char (setq cmpl-saved-syntax (syntax-table)) (unwind-protect (progn (set-syntax-table cmpl-syntax-table) (cond ((memq (setq cmpl-preceding-syntax (char-syntax (preceding-char))) '(?_ ?w)) (setq cmpl-symbol-end (point) cmpl-symbol-start (scan-sexps cmpl-symbol-end -1)) ;; Remove chars to ignore at the start. (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w) (goto-char cmpl-symbol-start) (forward-word 1) (setq cmpl-symbol-start (point)) (goto-char cmpl-symbol-end))) ;; Return completion if the length is reasonable. (if (and (<= (cmpl-read-time-eval completion-prefix-min-length) (- cmpl-symbol-end cmpl-symbol-start)) (<= (- cmpl-symbol-end cmpl-symbol-start) (cmpl-read-time-eval completion-max-length))) (buffer-substring-no-properties cmpl-symbol-start cmpl-symbol-end))))) ;; Restore syntax table. (set-syntax-table cmpl-saved-syntax)))