Elisp suppress "Select coding system" when write buffer to file - emacs

I write my function. In this function I insert some data to buffer and then write this buffer to file. Here my code snippet:
(set-buffer my-buffer-name)
(set-language-environment "UTF-8")
(setq make-backup-files nil)
(write-file (expand-file-name my-file-name my-current-dir))
(kill-buffer)
But every time when try to write to file, the elisp prompt:
Select coding system (default raw-text)
How I can set coding system (e.g. utf-8) to not prompt me every time when run my function?
Thanks.

You should set coding-system-for-write, or buffer-file-coding-system
instead of using set-language-environment.
PS. Generally speaking, you should use the lowest-level function possible in
your code (e.g., help for next-line explicitly recommends that one use
forward-line in code instead).
In your case, you might want to use basic-save-buffer or even
write-region instead.
PPS. You should probably use with-current-buffer instead of set-buffer.

Related

The major mode name of emacs-lisp

I want to load emacs init files faster, so I use the 'eval-after-load.
For example, when I load clojure file, I just put
(eval-after-load 'clojure-mode
'do-something)
It works.
But when I try
(eval-after-load 'emacs-lisp-mode
'do-something)
It doesn't work. I wonder to know the right major mode name of emacs-lisp.
Thanks.
Please read the documentation of eval-after-load:
eval-after-load LIBRARY FORM
This function arranges to evaluate form at the end of loading the file LIBRARY, each time LIBRARY is loaded. If LIBRARY is already loaded, it evaluates form right away. Don't forget to quote form!
[…] LIBRARY can also be a feature (i.e., a symbol), in which case form is evaluated at the end of any file where (provide LIBRARY) is called.
You have to pass the name of the file or library, which defines the major mode, as argument.
While some modes are defined in files of the same name (e.g. clojure-mode in clojure-mode.el), many files a different name, especially if the actually define multiple major modes.
emacs-lisp-mode is defined in lisp-mode.el, along with some other modes for Emacs Lisp editing (e.g. lisp-mode as a generic Lisp language mode, or lisp-interaction-mode for *scratch* buffers).
Hence, use (eval-after-load 'lisp-mode …)
Also, you have to give a single sexp as second argument, so you'll likely want to use (eval-after-load 'lisp-mode '(do-something)), to call the function do-something.
If you are using a snapshot build of Emacs, use with-eval-after-load, i.e. (with-eval-after-load 'lisp-mode (do-something)). It allows for more than a single form, and doesn't require quoting.
Just eval with M-: the variable major-mode. It actually is emacs-lisp-mode.
Note that *scratch* is actually in lisp-interaction-mode.
As to what you're trying to do, use (eval-after-load "lisp-mode").
As explained by #lunaryom, the arg passed to eval-after-load is not a function name but a feature name, which is basically a file name. So you need to find the name of the file from which the function is loaded.
We could provide a feature like eval-after-defun, and indeed it might be a good idea to do so. If you'd like such a thing, ask for it via M-x report-emacs-bug.

emacs python-mode: designate which python shell to send commands to

A follow up on this question - if I have two python shells running in emacs, how can I designate which shell commands from each python script buffer is sent to? I suspect it has something to do with python-buffer and python-set-proc but setting these variables to the name of the shell is apparently not the solution.
Edit: actually since I am using python-mode rather than loveshack python, it probably does not have to do with python-buffer and python-set-proc.
You can set a new value to python-buffer.
(defun my-python-set-proc (buffer-name)
(interactive "B")
(setf python-buffer (get-buffer buffer-name))
(python-set-proc))
And then M-xmy-python-set-procRET*Python*RET,
or M-xmy-python-set-procRET*Python*<2>RET.
In any Python buffer, you can use the variable py-which-bufname to control which Python shell your code gets sent to when executing it. The variable is buffer-local, so in order to set it to a custom value you'll need to change it from within the buffer by pressing M-: and entering the following
(setq py-which-bufname "My-Custom-Bufname")
This also makes it easy to quickly create new process buffers: If you set py-which-bufname to a name for which there is no corresponding process buffer, you can just issue C-c ! to quickly create one.
HTH
py-shell-name sets the default, which might be overwritten by command
Should the buffer code contain a shebang specifying pythonVERSION , than this takes precedence over default setting.
You may enforce executing buffer through specific pythonVERSION by calling a command of class py-execute-buffer-pythonVERSION
See menu PyExec, entry Execute buffer ...

Emacs c-mode autoloading failed

I want to load my file named "my-c-setup.el" when the c-mode is loading. So, I'm using the function "autoload".
With my python setup, it works well :
lang.el
(autoload 'python-mode "my-python-setup" "" t)
my-python-setup.el
(require 'python)
; ...
I'm trying to do the same with the c-mode, but i does not work :
lang.el
(autoload 'c-mode "my-c-setup" "" t)
my-c-setup.el
(setq c-basic-offset 4)
; ...
When I try to open a file in c-mode (test.c for example), I have the following error :
File mode specification error: (error "Autoloading failed to define function c-mode")
Autoload is not what you're looking for. What it does is simply load some code the first time it is needed, which is a handy way to extend Emacs' functionality while still keeping the start-up time low.
To solve your problem, we gotta think about what you really want to do: do you simply want some of your code to be loaded at some point, or do you want buffer-local customizations for ever buffer that is in c-mode?
If you simply want Emacs to load your code at start-up, either put your code directly into your .emacs file or use load-file or require instead of autoload:
load-file simply takes a file name, loads the lisp code in that file and evaluates it. So if your code is in a file named "/path/to/my-c-setup.el", you could put the following line in your .emacs, and the code will be loaded on every start-up:
(load-file "/path/to/my-c-setup.el")
Perhaps you don't want to give the absolute path name for every file you load. In that case, you could use the function load-library instead which is similar to load-file but tries to find the given filename in any of the directories stored in the variable load-path:
(add-to-list 'load-path "/path/to")
(load-library "my-c-setup.el")
The advantage is that you have to do the add-to-list part only once, and all subsequent calls to load-library will be able to find code in that directory.
An alternative way is the provide/require mechanism: you can make your .el-file "provide" some feature by putting a (provide 'feature) call in it, e.g.
(provide 'my-c-mode-customizations)
Then put an according (require 'feature) in your .emacs file, and your code will be loaded as well:
(require 'my-c-mode-customizations)
However, if you want your code only be loaded when c-mode is activated on a buffer, the way to achieve that is through Emacs' Hook mechanism:
A hook is a variable where you can
store a function or functions to be
called on a particular occasion by an
existing program.
Most major modes provide a customizable hook variable to which you can add functions that will be called whenever the major mode is invoked. For instance, c-mode provides c-mode-hook. In order for your own customizations to be called whenever c-mode is turned on for a buffer, put them in a function, say, my-c-mode-customizations and add the following line to your .emacs file:
(add-hook 'c-mode-hook 'my-c-mode-customizations)
Of course, you still need autoload for Emacs to actually find the definition of that function.
Lisp's autoload does not call a function when a file is loaded but tells lisp that the function is available and that the given file provides it. Whenever someone calls the (not yet defined) function, the file is loaded.
I think that c-mode is already defined and thus fails to re-register.
Autoload doesn't do what you think it does.
http://www.gnu.org/software/emacs/elisp/html_node/Autoload.html
What you probably want are mode-hooks or eval-after-load.
See eval-after-load vs. mode hook for the difference between the two.

Emacs: How to visit all sql-mode buffers and set the appropriate sql-buffer

My typical usage of sql-mode in emacs is to:
a. open a foo.sql file and begin editing
b. decide I want to run it using the key bindings for sql-send-region
c. fire up my custom (db-connect) function to connect to an appropriate db and create a *SQL* buffer.
However the foo.sql doesn't know about the existence of the *SQL* buffer unless I perform a "m-x sql-mode" in the buffer in order to refresh its environment and detect that such a buffer exists at this point. I would like to embed some code in my custom db-connect function to visit all buffers using sql-mode and update the sql-buffer variable. I am sure several stack overflow members must have done this or something similar before.
Thanks,
SetJmp
A quick look in the sql.el file revealed the command sql-set-sqli-buffer-generally, maybe this is something for you?
Another way you could hand this is to kill the buffer-local variant of sql-buffer by calling kill-local-variable in your major-mode hook. (That way, the effect would be that all SQL buffers would talk to the latest SQL buffer.)
Disclaimer: I don't know anything about SQL or SQL mode, only Emacs in general.
I've implemented this small helper function to filter buffers by their major-mode
(defun buffer-mode (buffer-or-name)
(with-current-buffer buffer-or-name major-mode))
(defun filter-buffers-by-mode (mode)
(delq nil
(mapcar
(lambda (x) (and (eq (buffer-mode x) mode) x))
(buffer-list))))
You can pass 'sql-mode as the argument and you'll get a list of all open sql buffers.

What do I pass to the switch-to-buffer argument in my .emacs file?

I am getting an error in my .emacs file at the following line:
(switch-to-buffer *Completions*)
error: symbols value as variable is void
I did a describe-function on switch-to-buffer and found I CAN pass it a BUFFER (and another optional argument which I do not currently need). What am I doing wrong?
Just a few notes:
a. I also need two similar lines (switch-to-buffer *grep*) and (switch-to-buffer *compilation*) so the simple solution of using (switch-to-completions) won't solve all of my problems.
b. All of the buffers I require are already open, so I don't think that is the problem.
Try
(switch-to-buffer "*Completions*")
You can specify a buffer name, as such:
(switch-to-buffer "*Help*")
From the docs:
Select buffer BUFFER in the current window.
BUFFER may be a buffer or a buffer name.
The implication of what Dewayne said is that you can pass objects returned from things like (buffer-list) to functions, if you're trying to do things programmatically, and don't particularly want to deal with strings as an intermediary.