0

I want to skip blank region under cursor, so I created this regex:

\s+\w translated to emacs regex [:space:]+[:graph:] but it doesnt match what I want. It matches some random space somewhere!

Whats even worse, regex [:space:]\{1,\}[:graph:] which is exactly same fails completelly without giving me anything.

What I want concretelly:

[cursor]_____some code...

I want to get position of the code to be able to jump there with cursor ...

EDIT: CODE

(trying to enhance code of someone else, just to be clear ...)

(setq custom/separators-regexp
      (concat "\\([\-'\"(){};:,.\\/?!@#%&*+=\]\\)\\|\\(\\[\\)\\|"
              "\\(\\]\\)\\|\\(\\s-\\)\\|\\(\\s_\\)\\|\\(\\`\\)\\|"
              "\\(\\'\\)\\|\\(\\^\\)\\|\\(\n\\)"))

(defun custom/position-normalize-forward (saved-pos my-pos)
  "Normalize position after forward move"
  (if (>= (- my-pos saved-pos) 2)
      (progn
                                        ;going to my-pos
        (goto-char my-pos)
        (if (not (eq my-pos (point-max)))
            (progn
                                        ;also backward-to-separator to fix position
              (custom/backward-symbol))))
    (progn
                                        ;going to my-pos in the usual way
      (goto-char my-pos))))

(defun custom/forward-symbol ()
  "Move to the next separator like in the every NORMAL editor"
  (interactive "^")
  (let ((saved-pos (point))
        (my-pos (re-search-forward custom/separators-regexp))
        (my-pos2 (re-search-forward "[:space:]+[:graph:]")))
    (if (> my-pos2 my-pos)
        (progn
          (message "POS %s %s" my-pos2 my-pos)
          (goto-char my-pos2)
          (return)))
    (if (eq my-pos 1)
        (progn
          (goto-char (+ my-pos 1))
          (setf my-pos (re-search-forward custom/separators-regexp))
          (custom/position-normalize-forward saved-pos my-pos))
      (custom/position-normalize-forward saved-pos my-pos))))
7
  • Show your code.
    – Dan
    Nov 29, 2017 at 20:48
  • 1
    Change the regexp-string to "[[:space:]]+[[:graph:]]".
    – Tobias
    Nov 29, 2017 at 21:03
  • 2
    \= (or in a double-quoted string, "\\=") matches the empty string at point. Refer to C-h i g (elisp)Regexp Backslash
    – phils
    Nov 29, 2017 at 22:05
  • 1
    Also, you might find that your monster custom/separators-regexp could be reduced to "\\([]['\"(){};:,.\\/?!@#%&*+=^[:space:]\n-]\\|\\s_\\|\\`\\|‌​\\'\\)", assuming most of the groupings are as redundant as I suspect they are.
    – phils
    Nov 29, 2017 at 23:09
  • 1
    Use looking-at instead of re-search-forward to anchor the search at point.
    – Tobias
    Nov 30, 2017 at 2:31

1 Answer 1

0

First of all, thank you for your help. Emacs is extremelly hard to dive in alone.

Solution with \\= partially worked well (with another checks because regex sometimes jumped somewhere ... even after usage of \\=). It was looking bright but, ... backward regex checking simply does not worked. After I tried to reverse regex and play with it, I simply give up. Emacs regex handling is simply outside of my league.

So I used simple while loop to find blank space ...

This is resulting and working code:

(require 'cl-macs)

(setq custom/separators-regexp
      "\\([]['\"(){};:,.\\/?!@#%&*+=^[:space:]\n-]\\|\\s_\\|\\`\\|‌​‌​\\'\\)")

(defun custom/position-normalize-forward (saved-pos my-pos)
  (if (>= (- my-pos saved-pos) 2)
      (progn
        (goto-char my-pos)
        (if (not (eq my-pos (point-max)))
            (progn
              (custom/backward-symbol))))
    (progn
      (goto-char my-pos))))

(defun custom/forward-symbol ()
  (interactive "^")
  (block custom/forward-symbol
    (let* ((saved-pos (point))
           (my-pos (re-search-forward custom/separators-regexp))
           (my-pos2 (custom/find-blank-space t saved-pos)))
      (if (> my-pos2 my-pos)
          (progn
            (goto-char (- my-pos2 1))
            (return-from custom/forward-symbol)))
      (if (eq my-pos 1)
          (progn
            (goto-char (+ my-pos 1))
            (setf my-pos (re-search-forward custom/separators-regexp))
            (custom/position-normalize-forward saved-pos my-pos))
        (custom/position-normalize-forward saved-pos my-pos)))))


(defun custom/position-normalize-backward (saved-pos my-pos)
  (if (>= (- saved-pos my-pos) 2)
      (progn
        (goto-char my-pos)
        (if (not (eq my-pos 1))
            (progn
              (custom/forward-symbol))))
    (progn
      (goto-char my-pos))))

(defun custom/backward-symbol ()
  (interactive "^")
  (block custom/backward-symbol
    (let* ((saved-pos (point))
           (my-pos (re-search-backward custom/separators-regexp))
           (my-pos2 (custom/find-blank-space nil saved-pos)))
      (if (< my-pos2 my-pos)
          (progn
            (goto-char (+ my-pos2 1))
            (return-from custom/backward-symbol)))
      (if (eq my-pos (point-max))
          (progn
            (goto-char (- my-pos 1))
            (setf my-pos (re-search-backward custom/separators-regexp))
            (custom/position-normalize-backward saved-pos my-pos))
        (custom/position-normalize-backward saved-pos my-pos)))))

(defun custom/find-blank-space (go-forward starting-pos)
  (let ((starting-char (string (char-after starting-pos)))
        (skipped-count 0)
        (increment (if go-forward 1 -1))
        (should-loop t))
    (if (or (equal starting-char " ")
            (equal starting-char "\t")
            (equal starting-char "\n"))
        (progn
          (while should-loop
            (if (or (equal (string (char-after (+ saved-pos skipped-count))) " ")
                    (equal (string (char-after (+ saved-pos skipped-count))) "\t")
                    (equal (string (char-after (+ saved-pos skipped-count))) "\n"))
                (setq skipped-count (+ skipped-count increment))
              (setq should-loop nil)))
          (+ starting-pos skipped-count))
      starting-pos)))

Hope it will help someone.

1
  • Please accept your own answer to mark this question as answered.
    – Tobias
    Dec 30, 2017 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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