Configure TAB key to insert spaces in .lisp, .c files, etc. in Emacs - emacs

I am a beginning Emacs user. I am trying to set up Emacs to simply add 2 spaces when I hit TAB. [If possible, I would like to have auto-indentation within Lisp files as well, but if it's not possible to have this coexist with manual tabbing, I would prefer manual tabbing.]
Currently, in "plainly" named files, TAB inserts 2 spaces as expected. However, in files that have an extension .c, .lisp, .java, etc., that Emacs apparently recognizes, pressing TAB currently does nothing.
Here is the full content of my current .emacs:
(add-to-list 'load-path "~")
(setf inhibit-startup-screen t)
(load (expand-file-name "~/.quicklisp/slime-helper.el"))
(setq inferior-lisp-program "sbcl")
(slime-setup '(slime-fancy))
(global-font-lock-mode 0)
(require 'pbcopy)
(turn-on-pbcopy)
(setq-default tab-width 2)
[Currently, auto-indentation is working in .lisp and .emacs files.]
What can I do to make Emacs allow me to TAB manually in any type of file, with or without Lisp auto-indentation for Lisp files? Any info would be much appreciated!

After finding this related question and reading about global-set-key here, I tried adding
(global-set-key (kbd "TAB") 'tab-to-tab-stop)
to my .emacs and that worked!
[ Side note - for some reason, the more standard
(global-set-key (kbd "<tab>") 'tab-to-tab-stop)
did not work. I am on MacOS 13.0. ]

Related

Multiple-cursors in Emacs does not modify all the cursors

I have installed the multiple-cursors package but, I cannot manage to work properly. Only one cursor can be modified, the rest of the cursors do nothing.
I have configured my .emacs file for the multi-cursors package as is shown below:
(require 'multiple-cursors)
(global-set-key (kbd "C-c m c") 'mc/edit-lines)
(global-set-key (kbd "C->") 'mc/mark-next-like-this)
(global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
(global-set-key (kbd "C-c C-<") 'mc/mark-all-like-this)
Can anybody help with this?
Thanks.
My crystal ball tells me that once upon a time the original poster was presented with an interactive question asking him/her whether to perform the same command for all cursors; and, the O.P. answered the question with a "no". Whereupon the Multiple Cursors package added an entry to the mc/lists-file, which has a default location of: (locate-user-emacs-file ".mc-lists.el") -- see https://github.com/magnars/multiple-cursors.el/blob/master/multiple-cursors-core.el#L652
The O.P. should open up the above-mentioned file and see if his/her prior choice should be manually removed. The file contains something like this:
;; This file is automatically generated by the multiple-cursors extension.
;; It keeps track of your preferences for running commands with multiple cursors.
(setq mc/cmds-to-run-for-all
'(
my-custom-function-one
org-self-insert-command
))
(setq mc/cmds-to-run-once
'(
mime-preview-scroll-down-entity
my-custom-function-two
))
(provide '.multiple_cursors)

How can load a .el configure file with a specified mode in emacs

Usually, I put the confugire .el files in src directory for all kinds of languages. Such as Go, the go-conf.el file:
(add-hook 'before-save-hook 'gofmt-before-save)
(add-hook 'go-mode-hook (lambda ()
(local-set-key (kbd "M-.") 'godef-jump)))
(add-hook 'go-mode-hook (lambda ()
(local-set-key (kbd "M-,") 'godef-jump-back)))
(add-to-list 'load-path "/usr/local/go/src/github.com/dougm/goflymake")
(add-hook 'after-init-hook #'global-flycheck-mode)
(require 'flycheck)
(require 'go-autocomplete)
(require 'auto-complete-config)
(ac-config-default)
)
(provide 'go-conf)
Then, in init.el, I write this line
(require 'go-conf)
Although go-conf can be loaded successfully, emacs launches slowly. It is because that emacs loads go-conf whatever files are opened. I can not tolerate it.
It is better that only when Go file is opened, go-conf is loaded.
I modify the init.el as :
(add-hook 'go-mode-hook '(lambda ()
(require 'go-conf)
(go-conf)
))
But it does not work!!
who can help me?
Your code seems to assume that the whole Emacs only has a single buffer and mode, whereas that is not the case. E.g. (add-hook 'before-save-hook 'gofmt-before-save) affects all buffers, whether they're using go-mode or not. Same for (add-hook 'after-init-hook #'global-flycheck-mode). Emacs is designed such that you can start it once and then edit hundreds of different files at the same time in that one Emacs session. So you should probably rewrite your code along the lines of:
(defun my-go-lang-config ()
(add-hook 'before-save-hook #'gofmt-before-save nil 'local)
(local-set-key (kbd "M-.") 'godef-jump)
(local-set-key (kbd "M-,") 'godef-jump-back)
(add-to-list 'load-path "/usr/local/go/src/github.com/dougm/goflymake")
(require 'go-autocomplete))
(add-hook 'go-mode-hook #'my-go-lang-config)
(require 'auto-complete-config)
(ac-config-default)
(global-flycheck-mode 1)
where the last three lines are part of your "generic" config (not specific to support for the Go language), meaning that you want to use flycheck and auto-complete whenever it's available rather than only in go-mode.
Your code to add to the hook doesn't work because the hook is run only after the mode is turned on, and the mode is not defined until the library is loaded. It makes no sense to load the same library in the mode hook.
If Emacs becomes slow after loading some library, it is probably due to that library. Is is slow after loading the library even if you do not turn the mode on?
You can try byte-compiling the library code. That can sometimes make a big difference in performance. You can use M-x byte-compile to compile a given file.
If compiling does not help, and if you do not seen anything suspect in buffer *Messages* (e.g., warnings that seem like they might be pertinent), then consider contacting the library maintainer, reporting the problem and asking for a remedy.
If go-mode itself is already available (most likely loaded on demand via an addition to auto-mode-alist, which is probably taken care of automatically if it was installed as an ELPA package), and you're just looking to load your custom library at the same time, then you can use eval-after-load:
(eval-after-load 'go-mode
'(require 'go-conf))
Make sure that the parent directory for your go-conf.el library is in the load-path, of course, otherwise require won't find it.

Use only two functions from the evil package as keybinding

I want to get the search at point functionality ("*" and "#") in emacs.
So I want to use "*" and "#" from the emacs evil-mode, as this is one of the suggestions. However, I don't want anything else from the evil-mode, just those two functions!
This is my .emacs file:
(package-initialize)
(evil-mode 1) ;; enable evil-mode
(global-set-key (kbd "C-*") 'evil-search-symbol-forward)
(global-set-key (kbd "C-#") 'evil-search-symbol-backward)
Now the keybindings work, but I loaded the whole evil-mode, so it messes up my standard emacs keybindings like "C-y" for yank.
If I don't load the evil-mode in the .emacs file, I get the error:
Symbol's function definition is void: evil-search-symbol-forward
#event_jr's answer mentions highlight-symbols, which is probably a much more lightweight way to do what you want.
However, if you really want to use the evil version, you can (require 'evil) in your .emacs file without turning it on (ie, leave off the (evil-mode 1) statement).
Meanwhile, the functions you want are actually named evil-search-word-forward and evil-search-word-backward, not the ...-symbol-... version you saw on the wiki page (which is probably outdated).
You should use highlight-symbols for symbol jumping and highlighting.

Emacs replacing require with autoload

After profiling my Emacs init file, I saw that many of my modes are taking a long time to load, explaining why I've been having slow start times.
I am trying to use after-load or autoload to decrease the load time but have been unsuccessful in many modes.
For example, I have a mode called multiple-cursors.el that I downloaded manually and placed in my .emacs.d directory. Here is the code I have now:
;; Multiple Cursors
(add-to-list 'load-path "~/.emacs.d/multiple-cursors.el/")
(require 'multiple-cursors)
(global-set-key (kbd "C-c c") 'mc/edit-lines)
(global-set-key (kbd "C-c .") 'mc/mark-next-like-this)
(global-set-key (kbd "C-c ,") 'mc/mark-previous-like-this)
(global-set-key (kbd "C-c /") 'mc/mark-all-like-this)
I tried to replace the require line with (autoload 'multiple-cursors-mode "multiple-cursors.el" "Multiple cursors mode") but that did not work.
This format of the autoload seems to work well only with Melpa-installed packages. How can I do the equivalent for manually installed packages, such as the example above?
You need to write autoloads for the functions that you are actually calling through the key bindings (i.e. mc/edit-lines, mc/mark-next-like-this, mc/mark-previous-like-this and mc/mark-all-like-this), since that's how the loading of the file is triggered. The autoloads need to refer to the files where the respective functions are defined, which is mc-edit-lines for mc/edit-lines, and mc-mark-more for the others.
So after setting the load path and binding the keys, add something like this:
(autoload 'mc/edit-lines "mc-edit-lines" "" t)
(autoload 'mc/mark-next-like-this "mc-mark-more" "" t)
(autoload 'mc/mark-previous-like-this "mc-mark-more" "" t)
(autoload 'mc/mark-all-like-this "mc-mark-more" "" t)

Naive question on how to type-annotated my ocaml prog. in emacs

I heard we can annotate ocaml prog. by their types. An older thread in the forum suggested using ocaml mode of
http://cristal.inria.fr/~remy/poly/emacs/index.html
I have been using Tuareg mode, in which it suggested using "c-c c-t" to retrieve types, cf. this piece of codes in tuareg.el
(when tuareg-with-caml-mode-p
;; Trigger caml-types
(define-key map [?\C-c ?\C-t] 'caml-types-show-type)
;; To prevent misbehavior in case of error during exploration.
(define-key map [(control mouse-2)] 'caml-types-mouse-ignore)
(define-key map [(control down-mouse-2)] 'caml-types-explore)
I got "c-c c-t" undefined although everything seems to be well configured.
Here is the .emacs file
(setq auto-mode-alist
(cons '("\\.ml[iyl]?$" . caml-mode) auto-mode-alist))
(autoload 'caml-mode "ocaml"
"Major mode for editing Caml code." t)
(autoload 'camldebug "camldebug"
"Call the camldebugger on FILE" t)
;; adjust paths for emacs source code
(add-to-list 'load-path "~/my-emacs-config/caml-mode")
;; adjust paths for emacs ocaml info sources
(require 'info)
(add-to-list 'Info-directory-list "~/my-emacs-config/caml-mode")
Here is the files in caml-mode (which contains ocaml.el)
bash-3.2$ ls ~/my-emacs-config/caml-mode/
caml-compat.el caml-emacs.el caml-font.el caml-help.el caml-hilit.el caml-types.el caml.el camldebug.el inf-caml.el ocaml.el
I did the following
--write an factorial func. in ocaml, called "annot.ml"
let rec f n =
if n = 1 then 0 else n * f(n-1)
--ocamlc -annot annot.ml
--open annot.ml by emacs and press "c-c c-t" while the cursor is under "n"
I got in the minibuffer of emacs
c-c c-t undefined
Conclusion, I still cannot retrieve types. Why??? Thank you for your ideas.
More info: when I try M-x caml-[tab] I get the following list, which does not contain caml-types-show-types
Possible completions are:
caml-mode camldebug
camldebug-backtrace camldebug-break
camldebug-close camldebug-complete
camldebug-delete camldebug-display-frame
camldebug-down camldebug-finish
camldebug-goto camldebug-kill
camldebug-last camldebug-mode
camldebug-next camldebug-open
camldebug-print camldebug-refresh
camldebug-reverse camldebug-run
camldebug-step camldebug-up
You're autoloading caml-mode from ocaml.el or ocaml.elc. But there is no such file! The official Caml mode is in a file called caml.el, and Tuareg mode is in a file called tuareg.el. This explains why opening your .ml file doesn't put you in Ocaml mode and doesn't load the Caml support. Change your autoload to either this to use the official mode
(autoload 'caml-mode "caml"
"Major mode for editing Caml code." t)
or this to use Tuareg mode
(autoload 'caml-mode "tuareg"
"Major mode for editing Caml code." t)