Xah Talk Show 2024-09-17 Ep586, Emacs Lisp, Rename All Links in Web Root Dir
- emacs lisp, write a command to rename all local links, of the file path cursor is on, in current web root dir.
- Emacs: xah-fix-datetime 🚀
- Emacs: Insert Date Time 🚀
- Xah Emacs Commands
- Elisp: thing-at-point
- Elisp: Read File
- Elisp: Walk Directory, List Files
(defun xah-html-rename-title-all () "Rename all links, for all html files of the file path cursor is on, on current web root dir. Created: 2024-09-17 Version: 2024-09-18" (interactive) (let (xbeg xend (xcurrent-file-path buffer-file-name) xrelativepath xfullpath xlinktext) (let ((xboundary (bounds-of-thing-at-point 'filename))) ;; (cons 411 712) (setq xbeg (car xboundary)) (setq xend (cdr xboundary))) (setq xrelativepath (buffer-substring-no-properties xbeg xend)) (setq xfullpath (expand-file-name xrelativepath (file-name-directory xcurrent-file-path))) (let (xbeg xend) ;; get link text, e.g. <a href="../x.html">something</a> (search-forward ">") (setq xbeg (point)) (search-forward "<") (setq xend (match-beginning 0)) (setq xlinktext (buffer-substring-no-properties xbeg xend))) (let (xfind-str xrep-str xnew-title (xrootdir "c:/Users/xah/web/xahlee_org/") xfilelist) ;; now do find replace for all web root dir ;; get find string, typically like this ;; "...name.html">link text</a> ;; create replace string ;; get the web root dir ;; assume .html files, skip dot dir (setq xnew-title (read-string "New title:")) (setq xfind-str (concat (file-name-nondirectory xrelativepath) "\">" xlinktext "</a>")) (setq xrep-str (concat (file-name-nondirectory xrelativepath) "\">" xnew-title "</a>")) ;; now do find and replace, for all in root dir ;; get list of files in root dir (setq xfilelist (directory-files-recursively xrootdir "\\.html$" nil (lambda (x) (not (string-match-p "/\\." x))))) (mapc (lambda (x) (let (xchanged) (with-temp-buffer (insert-file-contents x) ;; do find replace (goto-char (point-min)) (while (search-forward xfind-str nil t) (replace-match xrep-str t t) (setq xchanged t)) (when xchanged (message "changed, path %s" x)) (when xchanged (write-region (point-min) (point-max) x))))) xfilelist))))
draw a chinese goddess holding a sign that says emacs