How to reverse a string selected in evil visual mode? - string

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)))))

Related

AutoLsip - Update Command Window Text on Exsisting Line

There's this Whirl function that I found interesting while learning from this lsp file. The problem I'm having is that although (princ "\010") should be removing and replacing the last character shown, my cad application (DraftSight) is simply either ignoring the command, or it doesn't know how to interpret the command. How can I update the text in the command window without going to the next line?
(defun Whirl ()
(if *Whirl#
(setq *Whirl# (1+ *Whirl#))
(setq *Whirl# 1)
);if
(if (>= *Whirl# 5)
(setq *Whirl# 1)
);if
(cond
((= *Whirl# 1)(princ "-"))
((= *Whirl# 2)(princ "\\"))
((= *Whirl# 3)(princ "|"))
((= *Whirl# 4)(princ "/"))
);cond
(princ "\010")
);defun Whirl
Using "\r" will remove everything before it that's on the same line as itself. Therefore,
(princ "Test01 \nTest02 \rTest03")(princ)
will return:
Test01
Test03
This means that the entire line will need to be rewritten instead of just the last character, but that won't be much of a problem for me.

Removing newlines from buffer (useful when importing to ORG to unwrap the text while keeping paragraphs)

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)))

Convert string to title case - Emacs Lisp

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.

Scheme string-ref and char-whitespace? using

(define len (string-length "James ApR23Trb&%25G)(=?vqa"))
(define (divide-string str)
(let (x)
(if (char-whitespace? (string-ref str x))
(substring str (+ 1 x) (- len 1))
(printf "an invalid input!"))
))
(divide-string "James ApR23Trb&%25G)(=?vqa")
I have a string with divided into a blank space. I need to handle two
substring. One is till blank space and the other one is from blank
space. But i could not handle the index of blank space with x.
Any help will be appraciated. Thank you even now.
Try regexp-split:
> (regexp-split #rx"\\s" "James ApR23Trb&%25G)(=?vqa")
'("Jame" " ApR23Trb&%25G)(=?vqa")
Here \\s matches whitespace.
Oops. I mistook the question for a Racket question.
In a Scheme implementation: search for split in the documentation
and see what your implementation of choice has available.

How can I emulate Vim's * search in GNU Emacs?

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.

Resources