Using emacs on Ubuntu 11.10. I want to connect to a SQL Server database using sqsh instead of isql. I added the following to my initi.el
(set 'sql-sybase-program "sqsh")
(set 'sql-ms-program "sqsh")
It recompiles and loads successfully. However, when I use sql-ms and try to connect to the database, I am getting errors because emacs is using lower-case command parameters when it should be using upper-case command parameters. Furthermore, I can successfully connect to the database server using sqsh from the command line. When I try to run things within emacs, I get the following error:
sqsh: -d: Invalid integer expression
Process SQL exited abnormally with code 255
I did a pretty extensive Google search and I can't find much on how to do this (which makes me think it may not be possible). Obviously, I can run sqsh from within a shell, but then I lose the SQL mode integration. I'm not sure what I can / need to do to my init.el file to make this possible.
I think all I really need to do is figure out how to get emacs to send a -D not a -d to sqsh. Apparently isql doesn't care, but sqsh cares deeply about the difference.
As you say, the real answer is to get Emacs to use -D instead of -d. However, as a workaround, running sql-sybase instead of sql-ms seems to work fine for me.
Update: Try this code, it removes the -n option from sql-ms-options and redefines sql-ms-options to use -D instead of -d as the option to select the database:
(setq sql-ms-options (remove "-n" sql-ms-options))
(defun sql-comint-ms (product options)
"Create comint buffer and connect to Microsoft SQL Server."
;; Put all parameters to the program (if defined) in a list and call
;; make-comint.
(let ((params options))
(if (not (string= "" sql-server))
(setq params (append (list "-S" sql-server) params)))
(if (not (string= "" sql-database))
(setq params (append (list "-D" sql-database) params)))
(if (not (string= "" sql-user))
(setq params (append (list "-U" sql-user) params)))
(if (not (string= "" sql-password))
(setq params (append (list "-P" sql-password) params))
(if (string= "" sql-user)
;; if neither user nor password is provided, use system
;; credentials.
(setq params (append (list "-E") params))
;; If -P is passed to ISQL as the last argument without a
;; password, it's considered null.
(setq params (append params (list "-P")))))
(sql-comint product params)))
Related
I'm trying to customize my eshell to intercept python to do two things:
Open a new window and run python if no arguments are given (e.g., $ python)
Run the command "per usual" if arguments are given (e.g., $ python foobar.py)
So far I have something like this
(defun eshell/python (&rest cmd-args)
(if (not cmd-args)
(progn (run-python "python")
(select-window (split-window-below))
(switch-to-buffer "*Python*")
(balance-windows)
nil)
(message "Use '*python' to call python directly")))
I've tried replacing (message ...) with a few different things:
Based on the ouput of eshell-parse-command "python test.py" I tried
(progn (eshell-trap-errors
(eshell-named-command "python" cmd-args)))
but it hits a recursion limit.
Since *python test.py does what I want, I then tried
(progn (eshell-trap-errors
(eshell-named-command (concat "*" "python") cmd-args)))
but that puts the python process in the background and interrupts stdout with the output of my eshell-prompt-function.
Finally, I've fiddled with shell-command but I can't get it to write to the eshell buffer. In particular,
(progn (eshell-trap-errors
(shell-command (mapconcat (lambda(x) x) cmd-args " ")
(get-buffer "*eshell*") (get-buffer "*eshell*"))))
gives me a Text is read only message and moves point to start of the eshell buffer.
Is what I'm looking for possible?
Edit 1
Running without eshell/python defined, I've instead tried to avoid alias problems:
(defun eshell/gvr (&rest cmd-args)
(if (not cmd-args)
(progn (run-python "python")
(select-window (split-window-below))
(switch-to-buffer "*Python*")
(balance-windows)
nil)
(progn (eshell-trap-errors
(eshell-named-command "python" cmd-args)))))
If test.py is
print "Hello World"
x = raw_input("What should I repeat? ")
print x
running gvr test.py in eshell fails when I reply to the prompt because eshell tries to execute the input instead of handing it to python, but running python test.py goes off without a hitch.
How can I get run my own subprocesses in eshell the same way that they happen by default?
I would like to use elisp to execute a privileged command, then use a filter to do further processing when certain output is seen.
It's my understanding, after exhaustive RTFM'ing, that I can:
Set default-directory to a path that begins with "/sudo::/".
Call start-file-process to start a process that will be run under sudo.
Here is a function I wrote that tries to do this:
(defun start-vpn (config-file password-file callback)
"Starts the VPN connection, waits for a DHCP address,
then invokes callback with the address it got."
(let* ((default-directory "/sudo::/tmp")
(buff (get-buffer-create "*openvpn*"))
(proc (start-file-process "openvpn" "*openvpn*"
"/usr/local/sbin/openvpn"
(concat "--config " config-file)
(concat "--auth-user-pass " password-file))))
(set-process-filter proc
(lambda (proc output)
(message "Got output: " output)
(let ((ip (get-vpn-ip-address output)))
(when ip
(callback ip)))))))
When I run this, I see the following output in the *Messages* buffer:
start-vpn
Tramp: Opening connection for root#localhost using sudo...
Tramp: Sending command `exec sudo -u root -s -H -p Password:'
Tramp: Waiting for prompts from remote shell
Tramp: Sending command `exec sudo -u root -s -H -p Password:'
Tramp: Found remote shell prompt on `localhost'
Tramp: Opening connection for root#localhost using sudo...done
(lambda (proc output) (message "Got output: " output) (let ((ip ...)) (if ip (progn ...))))
Got output:
...and no output in the *openvpn* buffer the function creates.
I am not an expert at elisp, so I suspect there is some stupid mistake I am making. I am also really curious about the "(lambda (proc ..." in the *Messages* buffer.
Any advice, criticism, or tips appreciated. Thanks!
First of all, the reason for seeing (lambda ... in the message buffer is that set-process-filter returns the filter function, so therefore start-vpn does too.
Your message call needs to contain a format specifier to actually show the output:
(message "Got output: %s" output)
And (callback ip) won't work for two reasons:
Emacs Lisp has separate namespaces for functions and variables, so (callback ip) will call the function callback (which doesn't exist), while you need (funcall callback ip) to call the function stored in the variable callback.
Once you're past that, since Emacs Lisp uses dynamic scope by default, by the time your lambda function gets called the binding for callback is already gone.
In Emacs 24, you can set lexical-binding to t, and the code above should work. In any case, you can explicitly use the lexical-let macro:
(lexical-let ((callback callback))
(lambda (proc output)
(message "Got output: " output)
(let ((ip (get-vpn-ip-address output)))
(when ip
(funcall callback ip))))
This macro uses black magic to preserve the value of callback inside the lambda function.
I'm trying to start using eshell in place of bash within emacs, but I rely heavily on bash functions that I have written over the years. I'd like to configure eshell to invoke bash whenever a "command not found" condition occurs, in case the command in question is implemented as a bash function.
There is a variable tantalizingly named eshell-alternate-command-hook that sounds like it is made to order, but my lack of elisp skill is interfering with my success I think.
This is my best effort:
(add-hook 'eshell-alternate-command-hook 'invoke-bash t t)
(defun invoke-bash (command args)
(throw 'eshell-replace-command
(list "bash -c" command args)))
But when I test it, it doesn't work:
c:/temp $ lsd
Wrong number of arguments: (lambda (command args) (throw (quote eshell-replace-command) (list "bash -c" command args))), 1
c:/temp $
This is what I eventually came up with:
(defun invoke-bash (command)
(progn
(setq invoke-bash-cmd (concat "bash -c \"" command " " (mapconcat 'identity eshell-last-arguments " ") "\""))
(message invoke-bash-cmd)
(throw 'eshell-replace-command
(eshell-parse-command invoke-bash-cmd))))
I'm not eshell guru, but in the place where this hook is used, I see that it receives only one argument - command, that you trying to execute, so your code could look like
(add-hook 'eshell-alternate-command-hook 'invoke-bash)
(defun invoke-bash (command)
(throw 'eshell-replace-command
(list "bash -c" command)))
but it doesn't work, because you need to return elisp function, not name of command (according to documentation). If you want to run bash, then you need to return string with full path to it, but I hadn't found how to pass additional arguments to bash. Maybe you can find more in corresponding section on Emacs Wiki?
As is asked and answered here. I could use 'lein swank' to run clojure on Aquamacs.
I need to automate running 'lein swank', before running slime/clojure.
Q : Is there a way to this automatically? I mean how can I run the command 'lein swank' automatically when slime/clojure (M-x slime-connect) is called.
Q : If I have to come up with elisp code to run 'lein swank', how can I do that?
Added
Based on Jürgen Hötzel's answer, I modified the elisp as follows.
(defun lein-swank ()
(interactive)
(let ((default-directory (locate-dominating-file default-directory "/Users/smcho/bin/leiningen")))
(when (not default-directory)
(error "Not in a Leiningen project."))
;; you can customize slime-port using .dir-locals.el
(let ((proc (start-process "lein-swank" nil "/Users/smcho/bin/leiningen/bin/lein" "swank" (number-to-string 4005))))
(when proc
(process-put proc :output nil)
(set-process-sentinel proc (lambda (proc event)
(message "%s%s: `%S'"
(process-get proc :output)
proc (replace-regexp-in-string "\n" "" event))))
(set-process-filter proc
(lambda (proc output)
;; record last line of output until connected (possible error message)
(process-put proc :output (concat (process-get proc :output) output))
(when (string-match "Connection opened on" output)
(slime-connect "localhost" 4005)
;; no need to further process output
(set-process-filter proc nil))))
(message "Starting swank server...")))))
But, I got this error.
No project.clj found in this directory. lein-swank: `"exited abnormally with code 1"'.
What I found was that I should change pwd to ~/bin/leiningen to run 'lein swank'. Just put lein binary inside the PATH string doesn't make it run.
I made a gist for this job:
http://gist.github.com/419364
Just use the interactive command "M-x lein-swank", which will spawn the command in the current directory and connect to it.
I made several improvements to lein-swank:
lein-swank-command is now customizable: You can use Leiningen, if its bin directory is not part of your PATH environment.
Added directory as interactive argument: If project.clj can not be found in a dominating location of your current directory, you can specify a location.
I started using sqlplus for emacs. It works fine except one thing - very often I get a message "Buffer ... is not talking to anybody". In a file sqlplus.el there is the following code which verifies (get-buffer-process process-buffer-name). How can I keep alive the sql process?
(defun sqlplus-verify-buffer (connect-string)
(let ((output-buffer-name (sqlplus-get-output-buffer-name connect-string))
(process-buffer-name (sqlplus-get-process-buffer-name connect-string)))
(when (not (get-buffer process-buffer-name))
(sqlplus-shutdown connect-string)
(error "No SQL*Plus session! Use 'M-x sqlplus' to start the SQL*Plus interpreter"))
(unless (get-buffer-process process-buffer-name)
(sqlplus-shutdown connect-string)
(error "Buffer '%s' is not talking to anybody!" output-buffer-name)))
t)
One possible solution is the following
(defadvice sqlplus-verify-buffer (before sqlplus-verify-buffer-and-reconnect activate)
(unless (get-buffer-process (sqlplus-get-process-buffer-name connect-string))
(sqlplus connect-string)))