unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* wrap-search 2.1.4
@ 2022-06-02  1:30 Emanuel Berg
  0 siblings, 0 replies; only message in thread
From: Emanuel Berg @ 2022-06-02  1:30 UTC (permalink / raw)
  To: help-gnu-emacs

New version of this program that I used more times (I think)
than every other program I ever wrote, combined. (It says its
from 2021-05-23, that refers to the package tho, actually it's
10~15yo.)

In 2.1.4, it does regions as well.

Will there ever be another version? I don't know, maybe 3D
search is a feature that's lacking?

;;; wrap-search.el --- wrapped, non-incremental search -*- lexical-binding: t -*-
;;
;;; Commentary:
;;
;; Author: Emanuel Berg (incal) <moasenwood@zoho.eu>
;; Created: 2021-05-23
;; Keywords: matching
;; License: GPL3+
;; Package-Requires: ((cl-lib "1.0"))
;; URL: https://dataswamp.org/~incal/emacs-init/wrap-search.el
;; Version: 2.1.4
;;
;; 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 - which is possible as well, of course,
;; using the same commands - 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:

(require 'cl-lib)

(defun wrap-search-again ()
  "Repeat the previous `wrap-search'.

Do a new `wrap-search' with \\[wrap-search]"
  (interactive)
  (let ((cmd (cl-find-if (lambda (c)
                           (and (eq (car c) #'wrap-search)
                                (not (string= (nth 1 c) "")) ))
                         command-history) ))
    (if cmd
        (eval cmd)
      (message "no previous search") )))

(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] once sets CASE
\\[universal-argument] twice sets REV
\\[universal-argument] thrice sets CASE and REV.

Do \\[wrap-search-again] to repeat, with `wrap-search-again'."
  (interactive
   (list (read-string "search: [repeat] ")
         (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)
             (region-beginning)
           (point-min) )
         (if (use-region-p)
             (region-end)
           (point-max) )))
  (if (string= "" str)
      (wrap-search-again)
    (let*((case-fold-search (not case))
          (pos (point))
          (search-f (if rev
                        #'search-backward
                      #'search-forward) )
          (search-beg (if rev
                          end
                        beg) )
          (search-end (if rev
                          beg
                        end) ))
      (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") )))))

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

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




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-02  1:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-02  1:30 wrap-search 2.1.4 Emanuel Berg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).