Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
81 lines (67 sloc) 2.96 KB
(defvar smart-use-extended-syntax nil
"If t the smart symbol functionality will consider extended
syntax in finding matches, if such matches exist.")
(defvar smart-last-symbol-name ""
"Contains the current symbol name.
This is only refreshed when `last-command' does not contain
either `smart-symbol-go-forward' or `smart-symbol-go-backward'")
(make-local-variable 'smart-use-extended-syntax)
(defvar smart-symbol-old-pt nil
"Contains the location of the old point")
(defun smart-symbol-goto (name direction)
"Jumps to the next NAME in DIRECTION in the current buffer.
DIRECTION must be either `forward' or `backward'; no other option
is valid."
;; if `last-command' did not contain
;; `smart-symbol-go-forward/backward' then we assume it's a
;; brand-new command and we re-set the search term.
(unless (memq last-command '(smart-symbol-go-forward
(setq smart-last-symbol-name name))
(setq smart-symbol-old-pt (point))
(message (format "%s scan for symbol \"%s\""
(capitalize (symbol-name direction))
(unless (catch 'done
(while (funcall (cond
((eq direction 'forward) ; forward
((eq direction 'backward) ; backward
(t (error "Invalid direction"))) ; all others
smart-last-symbol-name nil t)
(unless (memq (syntax-ppss-context
(syntax-ppss (point))) '(string comment))
(throw 'done t))))
(goto-char smart-symbol-old-pt)))
(defun smart-symbol-go-forward ()
"Jumps forward to the next symbol at point"
(smart-symbol-goto (smart-symbol-at-pt 'end) 'forward))
(defun smart-symbol-go-backward ()
"Jumps backward to the previous symbol at point"
(smart-symbol-goto (smart-symbol-at-pt 'beginning) 'backward))
(defun smart-symbol-at-pt (&optional dir)
"Returns the symbol at point and moves point to DIR (either `beginning' or `end') of the symbol.
If `smart-use-extended-syntax' is t then that symbol is returned
(with-syntax-table (make-syntax-table)
(if smart-use-extended-syntax
(modify-syntax-entry ?. "w"))
(modify-syntax-entry ?_ "w")
(modify-syntax-entry ?- "w")
;; grab the word and return it
(let ((word (thing-at-point 'word))
(bounds (bounds-of-thing-at-point 'word)))
(if word
((eq dir 'beginning) (goto-char (car bounds)))
((eq dir 'end) (goto-char (cdr bounds)))
(t (error "Invalid direction")))
(error "No symbol found")))))
(global-set-key (kbd "M-s j") 'smart-symbol-go-forward)
(global-set-key (kbd "M-s k") 'smart-symbol-go-backward)
(provide 'arvid-smart-scan)