;;; my-view.el --- Personal library view  -*- lexical-binding: t -*-

;;; Commentary:

;;; Code:
(defun my-view-python ()
  "Three windows.
On the right side a *Anaconda* buffer with optionally
`virtual-auto-fill-mode' active and a *Python* buffer."
  (interactive)
  (require 'python)

  (unless (get-buffer (concat "*" python-shell-buffer-name "*"))
    (run-python)  ;; cursor is now inside the python buffer.
    (other-window -1))
  
  (delete-other-windows)

  (split-window-horizontally (truncate (* 0.6 (window-body-width))))
  (other-window 1)
  (switch-to-buffer (concat "*" python-shell-buffer-name "*"))

  (split-window-vertically)  ;; both are python buffers now.
  (switch-to-buffer "*Anaconda*")
  (when (fboundp 'virtual-auto-fill-mode) (virtual-auto-fill-mode))
  (other-window -1))

(defun my-view-elisp ()
  "Two windows side-by-side.
On the right side a *Help* buffer with optionally
`virtual-auto-fill-mode' active."
  (interactive)
  (delete-other-windows)

  (split-window-horizontally (truncate (* 0.6 (window-body-width))))
  (other-window 1)
  (switch-to-buffer "*Help*")
  (other-window -1))

(defun my-view-shell ()
  "Two windows side-by-side.
On the right side a *compilation* buffer.
Use `compile' with `sh <foo.sh -flag command>' to run the script."
  ;; TODO: rebind compile to C-c and auto fill sh with filename
  ;; TODO: rebind recompile to ??? to use last compile command
  ;; https://masteringemacs.org/article/compiling-running-scripts-emacs
  ;; TODO: for shell-script buffers:
  ;; ;;; Shut up compile saves
  ;; (setq compilation-ask-about-save nil)
  ;; ;;; Don't save *anything*
  ;; (setq compilation-save-buffers-predicate '(lambda () nil))
  (interactive)
  (delete-other-windows)

  (split-window-horizontally (truncate (* 0.6 (window-body-width))))
  (other-window 1)
  (switch-to-buffer "*compilation*")
  (other-window -1))

(defun my-view-org-pdf ()
  "Two windows side-by-side.
On the right side a DocView buffer displaying the pdf."
  (interactive)
  (delete-other-windows)
  (let ((bufnam (buffer-name))
        (buffilnam buffer-file-name))
    (split-window-horizontally)
    (other-window 1)
    ;;(switch-to-buffer (concat (file-name-sans-extension bufnam) ".pdf"))
    (find-file (concat (file-name-sans-extension buffilnam) ".pdf"))
    (doc-view-fit-height-to-window)
    (doc-view-fit-window-to-page)
    (other-window -1)))

(defun my-view-gnuplot ()
  "Three windows.
On the right side a *Shell* buffer with optionally
`virtual-auto-fill-mode' active and an Image mode buffer."
  (interactive)

  (save-excursion
  (let (output-file-name)  ;; get figure output name
    (goto-char (point-min))
    (when (re-search-forward "set output .*" nil t)
      ;; TODO: search text in between set output '...' then I do not
      ;; need to replace / remove part of the match string.
      (setq output-file-name (match-string 0))
      (setq output-file-name (string-replace "set output " "" output-file-name))
      (setq output-file-name (substring output-file-name 1 -1)))
    ;;(message "%s" output-file-name)
  
    (delete-other-windows)

    (split-window-horizontally (truncate (* 0.6 (window-body-width))))
    (other-window 1)
    (if output-file-name
        ;;(switch-to-buffer output-file-name)
        (find-file output-file-name)
      ;;(switch-to-buffer "*scratch*")
      (switch-to-buffer " *image*"))
    ;;(when (fboundp 'virtual-auto-fill-mode) (virtual-auto-fill-mode))

    (split-window-vertically)  ;; both are shell buffers now.
    (switch-to-buffer "*shell*")
    (shell)
    ;;(when (fboundp 'virtual-auto-fill-mode) (virtual-auto-fill-mode))

    (other-window -1))))

(provide 'my-view)
;;; my-view.el ends here
