emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

wrap-search 3.3.5


From: Emanuel Berg
Subject: wrap-search 3.3.5
Date: Thu, 25 Aug 2022 20:28:14 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

;;; wrap-search.el --- wrapped, non-incremental search -*- lexical-binding: t 
-*-
;;
;;; Commentary:
;;
;; Author: Emanuel Berg <incal@dataswamp.org>
;; Created: 2021-05-23 (package)
;; Keywords: matching
;; License: GPL3+
;; URL: https://dataswamp.org/~incal/emacs-init/wrap-search.el
;; Version: 3.3.5
;;
;; The `wrap-search' package offers non-incremental search.
;; It shows hitss - hits and only hits.
;;
;; `wrap-search' searches forward by default, but it wraps
;; and continues up and until the search start position if
;; necessary. This is so one doesn't have to wonder if the
;; target is north or south in the buffer.
;;
;; The philosophy is that `wrap-search' mostly shouldn't be
;; used for searching - that is possible as well, of course,
;; using the same command - rather, it is used to quickly
;; navigate a buffer.
;;
;; To use the package without keyboard shortcuts isn't
;; recommended. Instead, do for example
;;
;;   (global-set-key "\C-s" #'wrap-search)
;;   (global-set-key "\C-r" #'wrap-search-again)
;;
;;; Code:

(defun wrap-string-data-p (str)
  "Return STR if it is a string but not the empty string, \"\"."
  (and (stringp str)
       (not (string= str ""))
       str) )

(let ((prev-str)
      (prev-case)
      (prev-rev)
      (prev-beg)
      (prev-end) )
  (defun wrap-search-again ()
    (interactive)
    (if (wrap-string-data-p prev-str)
        (wrap-search prev-str prev-case prev-rev prev-beg prev-end)
      (message "no search has been done") ))
  (declare-function wrap-search-again nil)

  (defun show-prev-str ()
    (if (wrap-string-data-p prev-str)
        (let*((len   (length prev-str))
              (max   10)
              (short (when (< max len)
                       (substring prev-str 0 max) )))
          (format "[%s] " (if short
                              (format "%s..." short)
                            prev-str) ))
      ""))
  (declare-function show-prev-str nil)

  (defun wrap-search (str &optional case rev beg end)
    "Search for STR.

With CASE the search is case-sensitive.
With REV the search direction is reversed, i.e. north in the buffer from point.
BEG and END, or a region, delimits the search area. (default: whole buffer)

Interactively,
\\[universal-argument] sets CASE;
\\[universal-argument] \\[universal-argument] sets REV; and
\\[universal-argument] \\[universal-argument] \\[universal-argument] sets CASE 
and REV.

Do \\[wrap-search-again] to repeat, with `wrap-search-again'."
    (interactive
     `(,(read-string (format "search: %s" (show-prev-str)))
       ,(or (equal current-prefix-arg '( 4))
            (equal current-prefix-arg '(64)) )
       ,(or (equal current-prefix-arg '(16))
            (equal current-prefix-arg '(64)) )
       ,@(if (use-region-p)
             (list (region-beginning) (region-end))
           (list (point-min) (point-max)) )))
    (if (string= "" str)
        (wrap-search-again)
      (setq prev-str  str)
      (setq prev-case case)
      (setq prev-rev  rev)
      (setq prev-beg  beg)
      (setq prev-end  end)
      (let*((case-fold-search (not case))
            (pos (point))
            (data (if rev (list #'search-backward end beg)
                    (list #'search-forward beg end) ))
            (search-f (car data))
            (search-beg (cadr data))
            (search-end (caddr data)) )
        (if (apply (list search-f str search-end t))
            (message "hit: %s" (point))
          (goto-char search-beg)
          (if (apply (list search-f str (+ pos (if rev 0 (length str))) t))
              (if (= pos (point))
                  (message "this is the one occurrence")
                (message "hit: %s (wrap)" (point)) )
            (goto-char pos)
            (message "no hit") ))) ))
  (declare-function wrap-search nil) )

(provide 'wrap-search)
;;; wrap-search.el ends here

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]