Skip to main content
Updates based on comments
Source Link
phils
  • 53k
  • 3
  • 84
  • 127

Is there a way to fix command in a wrapper function?

Sure, you just need to read the start and end and then pass them along with a hard-coded command.

(defun my-sql-send-region-via-FOO (start end)
  "Send a region to the SQL process via shell command FOO."
  (interactive "r")
  (my-sql-send-region-via-shell-command "FOO" start end))

You could also make start and end optional, and have the main function figure them out when they're not passed. I'll leave that as an exercise for the reader...

how can I automatically switch the focus to the sql-interactive-mode buffer after the command is issued?

It looks like sql-send-string supports that directly; try adding (sql-pop-to-buffer-after-send-region t) to the outermost let bindings.


Is there a way to fix command in a wrapper function?

Sure, you just need to read the start and end and then pass them along with a hard-coded command.

(defun my-sql-send-region-via-FOO (start end)
  "Send a region to the SQL process via shell command FOO."
  (interactive "r")
  (my-sql-send-region-via-shell-command "FOO" start end))

You could also make start and end optional, and have the main function figure them out when they're not passed. I'll leave that as an exercise for the reader...

how can I automatically switch the focus to the sql-interactive-mode buffer after the command is issued?

It looks like sql-send-string supports that directly; try adding (sql-pop-to-buffer-after-send-region t) to the outermost let bindings.

added 92 characters in body
Source Link
phils
  • 53k
  • 3
  • 84
  • 127

Entering cat as the shell command ought to give the same outcome as sql-send-region.

Entering cat as the shell command ought to give the same outcome as sql-send-region.

Source Link
phils
  • 53k
  • 3
  • 84
  • 127

If you look at any of the definitions of the sql-send-* commands, you'll see they all hand off to sql-send-string. E.g.:

(defun sql-send-region (start end)
  "Send a region to the SQL process."
  (interactive "r")
  (sql-send-string (buffer-substring-no-properties start end)))

shell-command-on-region is a good place to look for how to obtain a string to pass to sql-send-string. That uses call-process-region behind the scenes, so a simple (will not handle errors) combination of the two could look like this:

(defun my-sql-send-region-via-shell-command (command start end)
  "Send a region to the SQL process via shell COMMAND."
  (interactive
   (list (read-shell-command "Shell command on region: ")
         (region-beginning)
         (region-end)))
  (let* ((buf (current-buffer))
         (sql (with-temp-buffer
                (let ((tmpbuf (current-buffer)))
                  (with-current-buffer buf
                    (call-process-region
                     start end shell-file-name nil tmpbuf
                     nil shell-command-switch command))
                  (buffer-string)))))
    (sql-send-string sql)))

or perhaps this:

(defun my-sql-send-region-via-shell-command (command start end)
  "Send a region to the SQL process via shell COMMAND."
  (interactive
   (list (read-shell-command "Shell command on region: ")
         (region-beginning)
         (region-end)))
  (let ((temp-buffer-show-function
         (lambda (buf)
           (sql-send-string (with-current-buffer buf (buffer-string)))
           (kill-buffer buf))))
    (with-output-to-temp-buffer (generate-new-buffer "sql")
      (call-process-region
       start end shell-file-name nil standard-output
       nil shell-command-switch command))))