In Vim the * key in normal mode searches for the word under the cursor. In GNU Emacs the closest native equivalent would be:
C-s C-w
But that isn't quite the same. It opens up the incremental search mini buffer and copies from the cursor in the current buffer to the end of the word. In Vim you'd search for the whole word, even if you are in the middle of the word when you press *.
I've cooked up a bit of elisp to do something similar:
(defun find-word-under-cursor (arg)
(interactive "p")
(if (looking-at "\\<") () (re-search-backward "\\<" (point-min)))
(isearch-forward))
That trots backwards to the start of the word before firing up isearch. I've bound it to C-+, which is easy to type on my keyboard and similar to *, so when I type C-+ C-w it copies from the start of the word to the search mini-buffer.
However, this still isn't perfect. Ideally it would regexp search for "\<" word "\>" to not show partial matches (searching for the word "bar" shouldn't match "foobar", just "bar" on its own). I tried using search-forward-regexp and concat'ing \<> but this doesn't wrap in the file, doesn't highlight matches and is generally pretty lame. An isearch-* function seems the best bet, but these don't behave well when scripted.
Any ideas? Can anyone offer any improvements to the bit of elisp? Or is there some other way that I've overlooked?
Based on your feedback to my first answer, how about this:
(defun my-isearch-word-at-point ()
(interactive)
(call-interactively 'isearch-forward-regexp))
(defun my-isearch-yank-word-hook ()
(when (equal this-command 'my-isearch-word-at-point)
(let ((string (concat "\\<"
(buffer-substring-no-properties
(progn (skip-syntax-backward "w_") (point))
(progn (skip-syntax-forward "w_") (point)))
"\\>")))
(if (and isearch-case-fold-search
(eq 'not-yanks search-upper-case))
(setq string (downcase string)))
(setq isearch-string string
isearch-message
(concat isearch-message
(mapconcat 'isearch-text-char-description
string ""))
isearch-yank-flag t)
(isearch-search-and-update))))
(add-hook 'isearch-mode-hook 'my-isearch-yank-word-hook)
The highlight symbol emacs extension provides this functionality. In particular, the recommend .emacsrc setup:
(require 'highlight-symbol)
(global-set-key [(control f3)] 'highlight-symbol-at-point)
(global-set-key [f3] 'highlight-symbol-next)
(global-set-key [(shift f3)] 'highlight-symbol-prev)
Allows jumping to the next symbol at the current point (F3), jumping to the previous symbol (Shift+F3) or highlighting symbols matching the one under the cursor (Ctrl+F3). The commands continue to do the right thing if your cursor is mid-word.
Unlike vim's super star, highlighting symbols and jumping between symbols are bound to two different commands. I personally don't mind the separation, but you could bind the two commands under the same keystroke if you wanted to precisely match vim's behaviour.
There are lots of ways to do this:
http://www.emacswiki.org/emacs/SearchAtPoint
scottfrazer's answer works well for me, except for words that end in '_' (or perhaps other non-word characters?). I found that the code for light-symbol mode was using a different regex for word boundary depending on the version of emacs, and that fixed it for me. Here is the modified code:
(defconst my-isearch-rx-start
(if (< emacs-major-version 22)
"\\<"
"\\_<")
"Start-of-symbol regular expression marker.")
(defconst my-isearch-rx-end
(if (< emacs-major-version 22)
"\\>"
"\\_>")
"End-of-symbol regular expression marker.")
(defun my-isearch-word-at-point ()
(interactive)
(call-interactively 'isearch-forward-regexp))
(defun my-isearch-yank-word-hook ()
(when (equal this-command 'my-isearch-word-at-point)
(let ((string (concat my-isearch-rx-start
(buffer-substring-no-properties
(progn (skip-syntax-backward "w_") (point))
(progn (skip-syntax-forward "w_") (point)))
my-isearch-rx-end)))
(if (and isearch-case-fold-search
(eq 'not-yanks search-upper-case))
(setq string (downcase string)))
(setq isearch-string string
isearch-message
(concat isearch-message
(mapconcat 'isearch-text-char-description
string ""))
isearch-yank-flag t)
(isearch-search-and-update))))
(add-hook 'isearch-mode-hook 'my-isearch-yank-word-hook)
How about built in commands M-b C-s C-w (start of word,search,word search)
Mickey of Mastering Emacs blog reintroduced a cool "Smart Scan" lib that gives global bindings of M-n and M-p for navigating symbols under the cursor in the buffer. Doesn't affect search register so it's not a * replacement as is, but a clever and usable alternative to the navigation problem.
I have not tried it but there is some code here called Grep-O-Matic.
With this you should be able to do C-* while in isearch mode.
(define-key isearch-mode-map [?\C-*] 'kmk-isearch-yank-thing)
(defun kmk-isearch-yank-thing ()
"Pull next thing from buffer into search string."
(interactive)
(let ((string (regexp-quote (thing-at-point 'word))))
(setq isearch-string
(concat isearch-string "\\")
isearch-message
(concat isearch-message
(mapconcat 'isearch-text-char-description
string ""))
;; Don't move cursor in reverse search.
isearch-yank-flag t))
(setq isearch-regexp t isearch-word nil isearch-success t isearch-adjusted t)
(isearch-search-and-update))
;Here is my version: Emulates Visual Studio/Windows key bindings
; C-F3 - Start searching the word at the point
; F3 searches forward and Shift F3 goes reverse
(setq my-search-wrap nil)
(defun my-search-func (dir)
(interactive)
(let* ((text (car search-ring)) newpoint)
(when my-search-wrap
(goto-char (if (= dir 1) (point-min) (point-max)))
(setq my-search-wrap nil))
(setq newpoint (search-forward text nil t dir))
(if newpoint
(set-mark (if (= dir 1) (- newpoint (length text))
(+ newpoint (length text))))
(message "Search Failed: %s" text) (ding)
(setq my-search-wrap text))))
(defun my-search-fwd () (interactive) (my-search-func 1))
(defun my-search-bwd () (interactive) (my-search-func -1))
(defun yank-thing-into-search ()
(interactive)
(let ((text (if mark-active
(buffer-substring-no-properties (region-beginning)(region-end))
(or (current-word) ""))))
(when (> (length text) 0) (isearch-update-ring text) (setq my-search-wrap nil)
(my-search-fwd))))
(global-set-key (kbd "") 'my-search-fwd) ; Visual Studio like search keys
(global-set-key (kbd "") 'my-search-bwd)
(global-set-key (kbd "") 'yank-thing-into-search)
Since Emacs 24.4, searching a symbol under the cursor is available with the global key sequence M-s .
Here is more information about this key sequence and the function it invokes obtained using the describe system of Emacs (C-h k M-s .):
M-s . runs the command isearch-forward-symbol-at-point (found in
global-map), which is an interactive compiled Lisp function in
‘isearch.el’.
It is bound to M-s ., <menu-bar> <edit> <search> <i-search>
<isearch-forward-symbol-at-point>.
(isearch-forward-symbol-at-point &optional ARG)
Do incremental search forward for a symbol found near point.
Like ordinary incremental search except that the symbol found at point
is added to the search string initially as a regexp surrounded
by symbol boundary constructs \_< and \_>.
See the command ‘isearch-forward-symbol’ for more information.
With a prefix argument, search for ARGth symbol forward if ARG is
positive, or search for ARGth symbol backward if ARG is negative.
Probably introduced at or before Emacs version 24.4.
Related
This function:
(defun remove-newlines-in-region ()
"Removes all newlines in the region."
(interactive)
(save-restriction
(narrow-to-region (point) (mark))
(goto-char (point-min))
(while (search-forward "\n" nil t) (replace-match "" nil t))))
will remove EVERY newline in a text. This is useful when importing to org creates wrapped text. We want then to unwrap the text per paragraph (not in all buffer, which would create a whole block without distinction between paragraphs).
How would we add the condition that if should only apply to one newlines, and not 2 consecutive newlines? Thanks!
SOLUTION: use UnfillRegion. It does the job!
After searching for the first newline, you can use looking-at-p to see whether the next character is a second newline. So instead of your
(while (search-forward "\n" nil t) (replace-match "" nil t))
you can use
(while (search-forward "\n" nil t)
(unless (looking-at-p "\n") (replace-match "" nil t)))
I am looking for an elisp function that accepts a string and returns the same in title case (i.e., all words capitalized, except for "a", "an", "on", "the", etc.).
I found this script, which requires a marked region.
Only, I need a function that accepts a string variable, so I can use it with replace-regex. I would love to see a version of the above script that can accept either or...
Something like this?
(progn
(defun title-case (input) ""
(let* (
(words (split-string input))
(first (pop words))
(last (car(last words)))
(do-not-capitalize '("the" "of" "from" "and" "yet"))) ; etc
(concat (capitalize first)
" "
(mapconcat (lambda (w)
(if (not(member (downcase w) do-not-capitalize))
(capitalize w)(downcase w)))
(butlast words) " ")
" " (capitalize last))))
(title-case "the presentation of this HEADING OF my own from my keyboard and yet\n"))
I'd say that the script you linked to does a good job at title casing. You can use it as-is.
That leaves us with two more questions:
How can we make it accept a string?
How can we write a function which accepts both a string or a (marked) region?
Working with strings in Emacs is idiomatically done in temporary buffers which are not displayed. You could write a wrapper like this:
(defun title-capitalization-string (s)
(with-temp-buffer
(erase-buffer)
(insert s)
(title-capitalization (point-min)
(point-max))
(buffer-substring-no-properties (point-min)
(point-max))))
Now, for a function which magically does what you mean, consider something like this:
(defun title-capitalization-dwim (&optional arg)
(interactive)
(cond
(arg
(title-capitalization-string arg))
((use-region-p)
(title-capitalization-string
(buffer-substring-no-properties (region-beginning)
(region-end))))
(t
(title-capitalization-string
(buffer-substring-no-properties (point-at-bol)
(point-at-eol))))))
It accepts an optional argument, or an active region or falls back to the text on the current line. Note that this function is not really useful when used interactively, because it doesn't show any effects. Hat tip also to https://www.emacswiki.org/emacs/titlecase.el
License
I put all this code under the Apache License 2.0 and the GPL 2.0 (or later at your option) in addition to the site's default license.
Use M-x
upcase-initials-region is an interactive built-in function in ‘C
source code’.
(upcase-initials-region BEG END)
Upcase the initial of each word in the region. This means that each
word’s first character is converted to either title case or upper
case, and the rest are left unchanged. In programs, give two
arguments, the starting and ending character positions to operate on.
I'm using Spacemacs, trying to write a function to reverse the string selected in evil visual mode. Here's what I get so far:
(defun fengqi/string-reverse (beg end)
(interactive)
(let ((string-to-reverse (buffer-substring-no-properties beg end)))
(message string-to-reverse beg end))
;; (delete-region beg end)
;; (insert (string-reverse string-to-reverse))
)
Obviously it doesn't work. How do I
get the selected region?
replace it with what I want?
I have read Enter Beg End parameters automatically in Evil-Visual-Mode, it's helpful, but I still don't how to do it.
To answer some questions in the comment:
When I was searching for some solutions, in this blog I found the function string-reverse. I tried in *ielm* buffer and it worked.
What I want is a function to reverse the string selected in evil visual mode.
I recently made some progress, and still there's some problem need to tackle.
The text selected in evil visual mode actually don't seem to be different from native emacs selections. What I missed is to pass the right code character to interactive, which should be r.
According to this blog, I now use search-forward and replace-match to replace the string.
And now the function looks like this:
(defun fengqi/string-reverse (beg end)
(interactive "r")
(save-restriction
(narrow-to-region beg end)
(let ((string-to-reverse (buffer-substring-no-properties (point-min) (point-max))))
(message string-to-reverse)
(goto-char (point-min))
(search-forward string-to-reverse)
(replace-match (string-reverse string-to-reverse)))))
But there's still a bug I don't know the reason. When the first character of the selected text is in upper case, this function doesn't work well:
For Abcde I expect the result to be edcbA, but instead I get EdcbA, which I don't see why.
Here is another implemention:
(defun ar-reverse-at-point (&optional beg end)
"Replace a string or region at point by result of ‘reverse’.
Works at any string detected at position, unless
optional BEG as start and
optional END as end are given as arguments or
an active region is set deliberately"
(interactive "*")
(let* ((pps (parse-partial-sexp (point-min) (point)))
;; (save-excursion (cadr (ar-beginning-of-string-atpt)))
(beg (cond (beg)
((use-region-p)
(region-beginning))
((and (nth 8 pps)(nth 3 pps))
(goto-char (nth 8 pps))
(point))))
;; (copy-marker (cdr (ar-end-of-string-atpt)))
(end (cond (end)
((use-region-p)
(copy-marker (region-end)))
((and (nth 8 pps)(nth 3 pps))
(forward-sexp)
(copy-marker (point)))))
(erg (buffer-substring beg end)))
(when (and beg end)
(delete-region beg end)
(insert (reverse erg)))))
I have been learning Common Lisp for a while, there was a question I have met that
how I can implement such a function which allows user to input some words until user input exit.
(actually I want to know what kind of command line interactive function APIs fit such requirement)
e.g.
prompt "please input a word: " in the REPL, then store user inputs into a global my-words , exit when user input "exit".
You specification is a little bit incomplete (e.g. what constitutes a word in your problem? What if the user add multiple words? What if the input is empty?). Here below I am using CL-PPCRE to split the input into different words and add them all at once, because it seems useful in general. In your case you might want to add more error checking.
If you want to interact with the user, you should read and write from and to the *QUERY-IO* stream. Here I'll present a version with a global variables, as you requested, as well as another one without side-effects (apart from input/output).
With a global variable
Define the global variable and initialize it with an empty adjustable array.
I am using an array so that it is easy to add words at the end, but you could also use a queue.
(defvar *my-words* (make-array 10 :fill-pointer 0 :adjustable t))
The following function mutates the global variable:
(defun side-effect-word-repl ()
(loop
(format *query-io* "~&Please input a word: ")
(finish-output *query-io*)
(let ((words (ppcre:split
'(:greedy-repetition 1 nil :whitespace-char-class)
(read-line *query-io*))))
(dolist (w words)
(when (string-equal w "exit") ; ignore case
(return-from side-effect-word-repl))
(vector-push-extend w *my-words*)))))
The LOOP uses the simple syntax where there are only expressions and no loop-specific keywords. I first write the prompt to *QUERY-IO*. The ~& FORMAT directive performs the same operation as FRESH-LINE. As Rainer pointed out in comments, we have to call FINISH-OUTPUT to ensure the message is effectively printed before the user is expected to reply.
Then, I read a whole line from the same bidirectional stream, and split it into a list of words, where a word is a string of non-whitespace characters.
With DOLIST, I iterate over the list and add words into the global array with VECTOR-PUSH-EXTEND. But as soon as I encouter "exit", I terminate the loop; since I rely on STRING-EQUAL, the test is done case-insensitively.
Side-effect free approach
Having a global variable as done above is discouraged. If you only need to have a prompt which returns a list of words, then the following will be enough. Here, I use the PUSH/NREVERSE idiom to built the resulting list of words.
(defun pure-word-repl ()
(let ((result '()))
(loop
(format *query-io* "~&Please input a word: ")
(finish-output *query-io*)
(let ((words (ppcre:split
'(:greedy-repetition 1 nil :whitespace-char-class)
(read-line *query-io*))))
(dolist (w words)
(when (string-equal w "exit")
(return-from pure-word-repl (nreverse result)))
(push w result))))))
Note about words
As jkiiski commented, it might be better to split words at :word-boundary. I tried different combinations and the following result seems satisfying with weird example strings:
(mapcan (lambda (string)
(ppcre:split :word-boundary string))
(ppcre:split
'(:greedy-repetition 1 nil :whitespace-char-class)
"amzldk 'amlzkd d;:azdl azdlk"))
=> ("amzldk" "'" "amlzkd" "d" ";:" "azdl" "azdlk")
I first remove all whitespaces and split the string into a list of strings, which can contain punctuation marks. Then, each string is itself splitted at :word-boundary, and concatenated with MAPCAN to form a list of separate words. However, I can't really guess what your actual needs are, so you should probably define your own SPLIT-INTO-WORDS function to validate and split an input string.
CL-USER 23 > (progn
(format t "~%enter a list of words:~%")
(finish-output)
(setf my-words (read))
(terpri))
enter a list of words:
(foo bar baz)
or
CL-USER 28 > (loop with word = nil
do
(format t "~%enter a word or exit:~%")
(finish-output)
(setf word (read))
(terpri)
until (eql word 'exit)
collect word)
enter a word or exit:
foo
enter a word or exit:
bar
enter a word or exit:
baz
enter a word or exit:
exit
(FOO BAR BAZ)
I would like to center justify a given input string to a given size so that what is produced is a string with padded spaces either side (left and right) of the input string.
The code I have to do this:
(defun center-string (string size)
(let* ((padding (/ (- size (length string)) 2))
(lpad (+ (length string) padding))
(lformat (format "%%%ds" lpad))
(rformat (format "%%%ds" (- size))))
(format rformat (format lformat string))))
And some test cases:
(center-string "KJF" 10)
=> " KJF "
(center-string "KF" 10)
=> " KF "
(center-string "0123456789" 10)
=> "0123456789"
(center-string "0123456789" 5)
=> "0123456789" ; Notice justifcation is ignored as input string too large.
Is there an existing elisp function to do this or a better method?
There's a center-line, which works in a buffer (and uses the buffer's value of fill-column as the line length), so if your goal is to produce a nicely formatted file, you could do something like
(defun insert-centered (x)
(insert "\n" x)
(center-line)
(insert "\n"))
No, there is not an existing emacs lisp routine that does what you want. (the standard search through emacs lisp info and emacs info supports this).