Posted By

kmc on 05/17/07


Tagged


Versions (?)

tidy.el


 / Published in: Lisp
 

  1. ;;;
  2. ;;; Run tidy (http://www.w3.org/People/Raggett/tidy/) on the contents
  3. ;;; of the current buffer. If tidy produces error messages, split the
  4. ;;; buffer's window to show them.
  5. ;;;
  6. ;;; Since the original contents of the buffer are deleted and replaced
  7. ;;; by tidy's output, preserving point is difficult. The best we can
  8. ;;; do is restore point to the same character position that it started
  9. ;;; with.
  10. ;;;
  11. ;;; Kevin Christen
  12. ;;; $Date: 2001/02/23 13:48:57 $
  13. ;;; $Revision: 1.2 $
  14. ;;;
  15. (defun tidy-buffer ()
  16. (interactive)
  17. (let
  18. ((tidy-program "tidy")
  19. (tidy-arguments "-q")
  20. (stderr-file ; Temp file for tidy's stderr
  21. (make-temp-name
  22. (concat temporary-file-directory "tidy-stderr")))
  23. (stderr-buffer-name "*Tidy Output*")
  24. (original-point (point)))
  25.  
  26. ;; Call tidy. `call-process-region' won't allow stdout and stderr
  27. ;; to both be directed to buffers, so put stdout in the current
  28. ;; buffer (replacing its contents) and stderr in a temporary file.
  29. (call-process-region
  30. (point-min)
  31. (point-max)
  32. tidy-program
  33. t
  34. (list (current-buffer) stderr-file)
  35. nil
  36. tidy-arguments)
  37.  
  38. (goto-char original-point) ; Restore point
  39.  
  40. ;; Create a buffer for stderr and read temp file contents into it
  41. (get-buffer-create stderr-buffer-name)
  42. (save-excursion
  43. (set-buffer stderr-buffer-name)
  44. (erase-buffer)
  45. (insert-file-contents stderr-file)
  46. (cond
  47. ((= (point-min) (point-max))
  48. (delete-windows-on (current-buffer))
  49. (kill-buffer (current-buffer)))))
  50.  
  51. ;; Delete temp file
  52. (delete-file stderr-file)
  53.  
  54. ;; Make the error messages magically appear
  55. (if (get-buffer stderr-buffer-name)
  56. (set-window-buffer (split-window) stderr-buffer-name))))

Report this snippet  

You need to login to post a comment.