Xah Talk Show 2026-02-02 Ep755 emacs lisp write a find text command, but only if occur inside given tags

Video Summary (Generated by AI, Edited by Human.)

The video is an Emacs Lisp coding session by Xah Lee, where he demonstrates how to write a script that finds text within files based on specific conditions (0:30).

Here's a breakdown of the key points:

;; -*- coding: utf-8; lexical-binding: t; -*-

;; 2026-02-02

;; script to find text in all files in a dir
;; with the condition the text only occur
;; between given texts, such as html begin and end tag.

(defvar xah-inputDir nil "input dir to search.")
(setq xah-inputDir "c:/Users/xah/web/xahlee_info/js/")

(defvar xbegin-tag-text nil "text that must occur before.")
(setq xbegin-tag-text "<pre class=\"js\">")

(defvar xend-tag-text nil "text that must occur after.")
(setq xend-tag-text "</pre>")

(defvar xfind-text nil "text that we want to find.")
(setq xfind-text "console")

(defvar xnot-contain-text-p nil "boolean. if t, means only when not contain the text.")
(setq xnot-contain-text-p t)

(defvar xah-filename-regex nil "regex on filename.")
(setq xah-filename-regex ".+\\.html$")

(defvar xah-ignore-dir-regex nil "regex on filename or dir name to ignore.")
(setq xah-ignore-dir-regex "/\\.")

;; s------------------------------

(defun xah-do-file (fPath xoutbuf)
  "Process the file at path FPATH"
  (with-temp-buffer
    (insert-file-contents fPath)
    (goto-char (point-min))
    (when (search-forward xbegin-tag-text nil t)
      (let ((xbegin-text-pos (point)))
        (when (search-forward xend-tag-text nil t)
          (let ((xend-text-pos (point)))
            (goto-char xbegin-text-pos)
            (let ((xcontain-p (search-forward xfind-text xend-text-pos t)))
              (when (if xnot-contain-text-p (not xcontain-p) xcontain-p)
                (print (format "found! at file %s at position %s" fPath (point)) xoutbuf)))))))))

;; s------------------------------

(let ((xoutbuf (get-buffer-create "*xah-find-in-tag-output*")))
  (mapc
   (lambda (x)
     (xah-do-file x xoutbuf))
   (directory-files-recursively
    xah-inputDir
    xah-filename-regex
    nil (lambda (x) (not (string-match-p xah-ignore-dir-regex x)))))
  (pop-to-buffer xoutbuf))