Skip to content

Instantly share code, notes, and snippets.

@amno1
Last active January 5, 2025 08:17
Show Gist options
  • Save amno1/06073befabb3d56728afa36dd673e81e to your computer and use it in GitHub Desktop.
Save amno1/06073befabb3d56728afa36dd673e81e to your computer and use it in GitHub Desktop.
Edit an open svg file in a separate buffer and update the view after the save
;;; temp-edit-svg.el --- Edit svg in a temp buffer -*- lexical-binding: t; -*-
;; Copyright (C) 2025 Arthur Miller
;; Author: Arthur Miller <[email protected]>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(defvar-local temp-edit-svg--real-buffer-file-name nil)
(defvar-local temp-edit-svg--working-buffer nil)
(defun save-temp-svg-buffer ()
(interactive)
(write-region (point-min) (point-max)
temp-edit-svg--real-buffer-file-name)
(when (buffer-live-p temp-edit-svg--working-buffer)
(with-current-buffer temp-edit-svg--working-buffer
(revert-buffer t t t))))
(defvar-keymap temp-edit-svg-mode-map
"C-x C-s" #'save-temp-svg-buffer)
(define-minor-mode temp-edit-svg-mode
"Save content of temp-edit-svg buffer.")
;;;###autoload
(defun temp-edit-svg ()
"Edit SVG file in temp buffer"
(interactive)
(let ((svg buffer-file-name)
(buf (current-buffer)))
(unless (or (null svg) (string-suffix-p "svg" (file-name-extension svg)))
(error "Current buffer is not displaying a SVG file."))
(with-current-buffer (get-buffer-create
(generate-new-buffer-name (concat svg "-edit")))
(nxml-mode)
(temp-edit-svg-mode +1)
(setf temp-edit-svg--real-buffer-file-name svg
temp-edit-svg--working-buffer buf)
(insert-file-contents-literally svg)
(switch-to-buffer-other-window (current-buffer)))))
(provide 'temp-edit-svg)
;;; temp-edit-svg.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment