@@ -514,9 +514,9 @@ In regular org-mode, tries to view image or executes normal C-c C-c."
514514 nil
515515 (buffer-string ))))
516516 (async-start-process " ob-python-extras-format-process"
517- " uvx "
517+ " ruff "
518518 (apply-partially 'ob-python-extras-format-callback created-temp-file (current-buffer ))
519- " ruff " " format" created-temp-file))))
519+ " format" created-temp-file))))
520520
521521(defun ob-python-extras-format-buffer-callback (temp-file-name src-edit-buffer process-obj )
522522 (with-current-buffer src-edit-buffer
@@ -530,6 +530,15 @@ In regular org-mode, tries to view image or executes normal C-c C-c."
530530 (insert-file-contents temp-file-name nil nil nil t ))
531531 (message " Formatted org-src-block " ))
532532
533+ (defun ob-python-extras-format ()
534+ (interactive )
535+ ; ; check first that the var is set
536+ (if (boundp 'ob-python-extras-formatter )
537+ (cond ((equal ob-python-extras-formatter " black" ) (ob-python-extras-format-black))
538+ ((equal ob-python-extras-formatter " ruff" ) (ob-python-extras-format-ruff))
539+ (t (message " No formatter %s found " ob-python-extras-formatter)))
540+ (message " No formatter set in ob-python-extras-formatter " )))
541+
533542; ; TODO check to make sure that it's a python source block
534543(defun ob-python-extras-format-black ()
535544 (interactive )
@@ -562,17 +571,36 @@ In regular org-mode, tries to view image or executes normal C-c C-c."
562571 created-temp-file))
563572 (message " Nothing to format " ))))
564573
565- ; ; start by using (org-src--contents-area (org-element-at-point))
566- ; ; here's the trick for replacing the contents.
567- ; ; (if (version< emacs-version "27.1")
568- ; ; (progn (delete-region beg end)
569- ; ; (insert (with-current-buffer write-back-buf
570- ; ; (buffer-string))))
571- ; ; (save-restriction
572- ; ; (narrow-to-region beg end)
573- ; ; (org-replace-buffer-contents write-back-buf 0.1 nil)
574- ; ; (goto-char (point-max))))
575- ; ; (when (and expecting-bol (not (bolp))) (insert "\n")))))
574+ (defun ob-python-extras-format-ruff ()
575+ (interactive )
576+ (if (org-src-edit-buffer-p )
577+ (let ((created-temp-file
578+ (make-temp-file
579+ (concat (file-name-as-directory (org-babel-temp-directory)) " ob-python-extras-format-ruff-" )
580+ nil
581+ nil
582+ (buffer-string ))))
583+ (async-start-process " ob-python-extras-format-process"
584+ " ruff"
585+ (apply-partially 'ob-python-extras-format-buffer-callback created-temp-file (current-buffer ))
586+ " format" created-temp-file))
587+ ; ; note that we place this in the else clause
588+ ; ; because org-in-src-block-p does not like running in org-src-edit-buffers
589+ (if (org-in-src-block-p t )
590+ (let* ((created-temp-file
591+ (make-temp-file
592+ (concat (file-name-as-directory (org-babel-temp-directory)) " ob-python-extras-format-ruff-" )
593+ nil
594+ nil
595+ (org-element-property :value (org-element-at-point ))))
596+ (content-area (org-src--contents-area (org-element-at-point )))
597+ (beg (car content-area))
598+ (end (cadr content-area)))
599+ (async-start-process " ob-python-extras-format-process"
600+ " ruff"
601+ (apply-partially 'ob-python-extras-format-src-block-callback created-temp-file (current-buffer ) beg end)
602+ " format" created-temp-file))
603+ (message " Nothing to format " ))))
576604
577605; ;; Load other packages
578606
0 commit comments