Emacs: HTML Rename Source File Path

By Xah Lee. Date: .

This command xah-html-rename-source-file-path lets you easily rename a embeded image link file pname, and also update the link. so that you don't have to rename the file manually and update the link.

(defun xah-html-rename-source-file-path ()
  "Rename HTML source file path.
Place cursor anywhere inside a HTML source local file path, e.g.
<img src=\"img/▮cats.jpg\" >
<a href=\"js_▮canvas_tutorial.html\">
The cursor needs not to be on a path. It can be anywhere inside the start tag.
Call this command, it prompt for a new path/name with one line for dir path and line for file name. The separate lines are for ease of editing.
When done editing, newline characters in path are removed, and comma or space replaced by _. The file name is renamed, and link also updated.

URL `http://ergoemacs.org/emacs/emacs_html_rename_source_file_path.html'

This command is for interactive use only.
Version 2019-10-05 2021-06-17"
  (let* ( ($p0 (point))
          $bounds $p1 $p2 $input $currentDir $oldPath $promptPath $userInputPath
          ($doit-p nil))
        (search-backward "<" )
        (re-search-forward "href=\\|src=" )
        (forward-char ))
      (setq $bounds (xah-get-bounds-of-thing 'filepath))
      (setq $p1 (car $bounds))
      (setq $p2 (cdr $bounds))
      (setq $input (buffer-substring-no-properties $p1 $p2))
      (setq $currentDir (file-name-directory (or (buffer-file-name) default-directory )))
      (setq $oldPath (expand-file-name $input $currentDir))
      (setq $promptPath (concat (file-name-directory $oldPath) "\n" (file-name-nondirectory $oldPath)))
      (setq $userInputPath (read-string "New name: " $promptPath nil $promptPath ))
      (setq $doit-p nil))
    (setq $newPath
           " \\|\n\\|," "_"
           (replace-regexp-in-string "\n" "" $userInputPath)))
    (setq $doit-p
          (if (file-exists-p $newPath)
              (y-or-n-p "File exist. Replace?")
    (message "old path: %s\n new path: %s" $oldPath $newPath)
    (when $doit-p
        (rename-file $oldPath $newPath t)
        (goto-char $p0)
        (delete-region $p1 $p2)
         (file-relative-name $newPath))))))






If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial