elisp - Moving around well defined blocks of text - text

I am quite a noob in elisp and try to practice by solving problems I face using it but this one seems to hard.
I have an enormous buffer which has this structure
texta
str1
textb
str1
textc
str1
<more times>
textA
str2
textB
str2
textC
str2
<same number of occurances of str2 as str1>
Now i want the text above str1 until the previous str1(or the beginning for the first one to be moved to the corresponding occurance of str2 so that the first str1 and the text above it is right above the first occurance of str2
textA
texta
str1
str2
textB
textb
str1
str2
textC
textc
str1
str2
<etc>
Given that elisp is a language that binds emacs together i think it shouldnt be too hard for someone with little more experience than me to come up with some solution...

ok I did it!
(defun move-under (str1 str2)
(interactive)
(save-excursion
(goto-char (point-min))
(set-mark (point-min))
(search-forward str1)
(setq text (buffer-substring (mark) (point)))
(delete-region (mark) (point))
(search-forward str2)
(backward-delete-char (length str2))
(insert text)))
(defun rearrange-for-nonbuffer-usage ()
(interactive)
(while (search-forward "str2" nil t)
(progn (goto-char (point-min))
(move-under "str1" "str2")))))

As OP showed, it's not too hard to write Elisp for this. However, it's instantaneous if you have keyboard macros as a part of your Emacs workflow. Look into it.

Related

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

How to reverse a string selected in evil visual mode?

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

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.

Function which verifies if a string is includes in an other one in Lisp

I am trying to write a function which verifies if a string is included in another one in Lisp but I cannot
For example :
(string-include 'abd 'abbbe) => nil
(string-include 'ghf 'dghfd) => ghf
Here is my function:
(defun string-include (string1 string2)
(cond
((not string1) 0)
((not string2) 0)
((.... (string1) (string2)) (string1 (string-include string1 (cdr string2))))
((string-include string1 (cdr string2)) ) )
Return an index or the substring, not a symbol
In your question, you used this example:
(string-include 'abd 'abbbe) => nil
(string-include 'ghf 'dghfd) => ghf
Assuming that you're returning the symbols nil and ghf, you'll run into an ambiguity if you ever want to check whether a string contains the substring NIL. E.g., with this approach, you'll have:
(string-include 'nil 'vanilla) => nil
Did that return nil because "NIL" is in "VANILLA", because it isn't? It's ambiguous and you can't tell. Instead, you could return the actual string, since the string "NIL" is a true value. Even better, if you return the index of the string, then you find out where in the other string the first string appears. That's the way that the built in function search behaves, for instance.
Directly, using SEARCH
You can implement this in terms of search:
(defun substringp (needle haystack &key (test 'char=))
"Returns the index of the first occurrence of the string designated
by NEEDLE within the string designated by HAYSTACK, or NIL if it does
not occur. Characters within the string are compared by TEST, which
defaults to CHAR= (for case-sensitive comparison)."
(search (string needle)
(string haystack)
:test test))
Note the use of the string function to convert from string designators (characters, strings, and symbols) to the strings that they designate. Remember that with the standard settings, the reader upcases the names of symbols, so the symbol cat designates the string "CAT". Finally, since this returns the result from search, it does double duty for you: it returns the index of the first occurrence if there is an occurrence, and nil otherwise. Remember that everything except nil is a true value (even 0), so you can use the result as a boolean or as an index (as long as you check that it's not nil). Here are some examples:
CL-USER> (substringp "cat" "concatenate")
3
CL-USER> (substringp "dog" "concatenate")
NIL
;; Default upcasing of symbol names means that the
;; result of 'cat is a symbol named "CAT", which is not
;; in "concatenate".
CL-USER> (substringp 'cat "concatenate")
NIL
;; You can test the characters with CHAR-EQUAL, which
;; is case insensitive, in which case "CAT" is in
;; "concatenate".
CL-USER> (substringp 'cat "concatenate" :test 'char-equal)
3
Using recursion
Your code, and the code that uselpa showed in another answer, are more recursive in nature. That in and of itself is not a problem, but recursive string processing in Common Lisp is prone to a few pitfalls. It's inefficient to make lots of new stings by using subseq, so lots of sequence functions in Common Lisp take :start and :end arguments, or in the case of functions that take two sequences, :start1, :end1, :start2, and :end2 arguments. By using these, you can recurse and change the indices into the strings, rather than creating entirely new strings. For instance, string= lets you compare two strings.
;; "toc" is in both "octocat" and "toccata"
CL-USER> (string= "octocat" "toccata" :start1 2 :end1 5 :end2 3)
T
Working with these kinds of functions requires a bit of care to make sure you don't provide any indices that are out of range, but it's not too bad, and you don't end up copying any strings. Here's a version of substringp that accepts these start and end parameters, and uses a local recursive function to do the actual processing.
(defun substringp (string1 string2
&key
(start1 0) (end1 nil)
(start2 0) (end2 nil))
"Returns the index of the first occurence of the substring of
STRING1 bounded by START1 and END1 within the substring of STRING2
bounded by START2 and END2, or NIL if the string does not appear. The
index is a position within STRING2 as a whole."
;; First, compute the actual strings designated by STRING1 and
;; STRING2, and the values for END1 and END2, which default to the
;; length of the respective strings. Also get the length of the
;; substring in STRING1 that we're looking for. This is done just
;; once. The actual recursive portion is handled by the local
;; function %SUBSTRINGP.
(let* ((string1 (string string1))
(string2 (string string2))
(end1 (or end1 (length string1)))
(end2 (or end2 (length string2)))
(len1 (- end1 start1)))
(labels ((%substringp (start2 &aux (end2-curr (+ start2 len1)))
(cond
;; If end2-curr is past end2, then we're done, and
;; the string was not found.
((not (< end2-curr end2)) nil)
;; Otherwise, check whether the substrings match. If
;; they do, return the current start2, which is the
;; index of the substring within string2.
((string= string1 string2
:start1 start1 :end1 end1
:start2 start2 :end2 end2-curr)
start2)
;; If that doesn't match, then recurse, starting one
;; character farther into string2.
(t (%substringp (1+ start2))))))
(%substringp start2))))
Judging by your code, what you are looking for is something like this:
(defun string-include (string1 string2)
(cond
((zerop (length string1)) nil) ; string1 is empty (no need to test it every time)
((> (length string1) (length string2)) nil) ; string1 is longer than string2
((string= string1 (subseq string2 0 (length string1))) string1) ; string2 starts with string1
(t (string-include string1 (subseq string2 1))))) ; otherwise shorten string2 by 1 and start over
This works but it is inefficient and not idiomatic Common Lisp. Just make sure that you actually pass strings and not symbols like in your example:
? (string-include "abd" "abbbe")
NIL
? (string-include "ghf" "dghfd")
"ghf"
Of course, Joshua's answer is the recommended solution.
EDIT
Added a version that works with both symbols and strings (but returns strings anyway). I took the opportunity to include one of Joshua's suggestions:
(defun string-include (string1 string2)
(let* ((string1 (string string1)) (length1 (length string1)))
(if (zerop length1)
nil
(labels ((sub (s)
(cond
((> length1 (length s)) nil)
((string= string1 s :end2 (length string1)) string1)
(t (sub (subseq s 1))))))
(sub (string string2))))))
Testing:
? (string-include "abd" "abbbe")
NIL
? (string-include "ghf" "dghfd")
"ghf"
? (string-include 'abd 'abbbe)
NIL
? (string-include 'ghf 'dghfd)
"GHF"
? (string-include "ghf" '|dghfd|)
"ghf"
? (string-include '|ghf| "dghfd")
"ghf"

Make String from Sequence of Characters

This code does not work as I expected. Could you please explain why?
(defn make-str [s c]
(let [my-str (ref s)]
(dosync (alter my-str str c))))
(defn make-str-from-chars
"make a string from a sequence of characters"
([chars] make-str-from-chars chars "")
([chars result]
(if (== (count chars) 0) result
(recur (drop 1 chars) (make-str result (take 1 chars))))))
Thank you!
This is very slow & incorrect way to create string from seq of characters. The main problem, that changes aren't propagated - ref creates new reference to existing string, but after it exits from function, reference is destroyed.
The correct way to do this is:
(apply str seq)
for example,
user=> (apply str [\1 \2 \3 \4])
"1234"
If you want to make it more effective, then you can use Java's StringBuilder to collect all data in string. (Strings in Java are also immutable)
You pass a sequence with one character in it to your make-str function, not the character itself. Using first instead of take should give you the desired effect.
Also there is no need to use references. In effect your use of them is a gross misuse of them. You already use an accumulator in your function, so you can use str directly.
(defn make-str-from-chars
"make a string from a sequence of characters"
([chars] (make-str-from-chars chars ""))
([chars result]
(if (zero? (count chars))
result
(recur (drop 1 chars) (str result (first chars))))))
Of course count is not very nice in this case, because it always has to walk the whole sequence to figure out its length. So you traverse the input sequence several times unnecessarily. One normally uses seq to identify when a sequence is exhausted. We can also use next instead of drop to save some overhead of creating unnecessary sequence objects. Be sure to capture the return value of seq to avoid overhead of object creations later on. We do this in the if-let.
(defn make-str-from-chars
"make a string from a sequence of characters"
([chars] (make-str-from-chars chars ""))
([chars result]
(if-let [chars (seq chars)]
(recur (next chars) (str result (first chars)))
result)))
Functions like this, which just return the accumulator upon fully consuming its input, cry for reduce.
(defn make-str-from-chars
"make a string from a sequence of characters"
[chars]
(reduce str "" chars))
This is already nice and short, but in this particular case we can do even a little better by using apply. Then str can use the underlying StringBuilder to its full power.
(defn make-str-from-chars
"make a string from a sequence of characters"
[chars]
(apply str chars))
Hope this helps.
You can also use clojure.string/join, as follows:
(require '[clojure.string :as str] )
(assert (= (vec "abcd") [\a \b \c \d] ))
(assert (= (str/join (vec "abcd")) "abcd" ))
There is an alternate form of clojure.string/join which accepts a separator. See:
http://clojuredocs.org/clojure_core/clojure.string/join

Resources