0

I would like to write a custom elisp function that searches for a few variants of unicode text under a cursor and highlights their occurrences in the currently open file.

For e.g. if my text is helloम् (it ends with bytes U2350+U2381) the function should search a modified version of the text helloं (i.e. hello+U2306) The unicode bytes are always at the end of the word.

I started with a simple elisp function that would print a message if a text that matches my criteria is found and saving the modified string to be searched into a variable:

(defun c-search ()
  "If word under cursor ends with U2350+U2381, search for word+U2306"
  (interactive)
  (if (string-match-p "म्" (thing-at-point 'word))
  (setq z (replace-regexp-in-string "म्$" "ं" (thing-at-point 'word)))
  (message "Should search  %s" z)))

Nothing gets printed when I evaluate this function. However, the following function that searches and replaces the string works fine:

(defun rrx ()
      (interactive)
      (setq z (replace-regexp-in-string "म्$" "ं" (thing-at-point 'word)))
      (message z))

How can I fix function c-search to perform a search for the string saved in variable z?

1

I would like to write a custom elisp function that searches for a few variants of unicode text under a cursor and highlights their occurrences in the currently open file.

I took this as the task and wrote a function that highlights the variant of the symbol at point. It doesn't search for it, nor replace it. This way you can move freely while the matches are highlighted. Here's the function,

(defun c-search ()
  "If word under cursor ends with U2350+U2381, highlight all
occurrences of word+U2306"
  (interactive)
  (save-excursion
    (let ((bos (re-search-backward "\\_<"))
          (eos (re-search-forward "\\_>")))
      (goto-char bos)
      (when (re-search-forward ".*म्" eos)
        (highlight-regexp (replace-regexp-in-string "म्$" "ं" (match-string-no-properties 0)))))))

Notice it searches within the symbol (delimited by \_< and \_>) at point instead of the word (delimited by \b) at point, because (at least in Emacs Lisp mode) words are splitted at the character म्, so if you search until the end of the word, or if you use (thing-at-point 'word), the search stops short of म्.

hi-lock saves the regexps it highlights in the variable hi-lock-interactive-patterns. The latest regexp it has highlighted is the car of the car of that variable, which means you can access it with (caar hi-lock-interactive-patterns) if you need it. For example, you can unhighlight the latest highlight with this function:

(defun unhighlight-latest-regexp ()
  (interactive)
  (unhighlight-regexp (caar hi-lock-interactive-patterns)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.