Emacs replacing require with autoload - emacs

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)

Related

Configure TAB key to insert spaces in .lisp, .c files, etc. in 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. ]

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)

Define key-chord key with specific mode

How do you define a key-chord key only in specific mode, for example I want to bind a cider repl to a specific key only in clojure-mode or cider-mode. I can only find an example that activates the key globally.
Thanks for your help.
EDIT:
(require 'evil)
(require 'key-chord)
(evil-mode 1)
(key-chord-mode 1)
(key-chord-define evil-insert-state-map "jk" 'evil-normal-state)
(key-chord-define-global "gt" 'other-window)
(key-chord-define clojure-mode-hook "gj" 'cider-jack-in)
;; error : Wrong type argument: keymapp, (rainbow-delimiters-mode)
(provide 'init-evil)
Defining mode-specific key bindings
Here is an example of how to do this:
(define-key clojure-mode-map (kbd "C-c r") 'cider-repl)
... where of course you would have to replace cider-repl with the specific command you want to bind. Note that the quote ' before the command name is required.
To generalize:
(define-key <mode-map> <key-binding> '<command>)
key-chord-specific instructions
You need to change the line where you're trying to set up the clojure-mode-specific key binding to
(add-hook 'clojure-mode-hook
(lambda () (key-chord-define clojure-mode-map "gj" 'cider-jack-in)))
Appendix: Making sure mode-maps are defined before modifying them
In order for modifications to clojure-mode-map to work properly, you have to make sure it is defined when you call define-key as described above.
If you are using the Emacs Package Manager, you are likely to have this line
(package-initialize)
somewhere in your .emacs file (which takes care of loading packages installed via package-install). Make sure you call define-key somewhere below this line.
Alternatively you can add the call to define-key to the hook that is run when clojure-mode is enabled:
(defun clojure-set-up-key-bindings ()
(define-key clojure-mode-map (kbd "C-c r") 'cider-repl)
;; If necessary, add more calls to `define-key' here ...
)
(add-hook 'clojure-mode-hook 'clojure-set-up-key-bindings)

How to customize Emacs key bindings for going to specific line / end of buffer

I want to customize Emacs so that pressing
ESC : n RET
takes me to line number n
and
ESC : $ RET
takes me to the last line. (That's how the vi editor works.)
How can I achieve this inside my Emacs configuration file? Currently I have this in my .emacs:
(global-set-key (kbd "M-9") 'prev-window)
(global-set-key (kbd "M-0") 'other-window)
I don't want to use any of the off-the-shelf solutions (eg. evil) because they are bloated and mess with my existing shortcuts.
Put this in a file and load it (load means execute):
(defun vi-goto-line (arg)
(interactive "sLine:")
(message arg)
(if (string= "$" arg)
(end-of-buffer)
(goto-line (string-to-int arg))
)
)
(global-set-key (kbd "M-:") 'vi-goto-line)
To load it you can use M-xload-file and then enter interactively the path to the file.
Keep in mind that the key combo M-: (which is the same as ESC:) already has a meaning in Emacs, so this now gets cloaked.
Of course you also can load the file from your .emacs by putting (load-file "/path/to/my/file") into the .emacs or put these lines directly into your .emacs file (or any other configuration file which gets loaded)

Emacs: Keyboard shortcut to run ansi-term with a specific shell

I would like to associate a keyboard binding in Emacs (e.g. C-c a) that automatically starts an ansi-term window with a shell that I have pre-specified in my .emacs file (without prompting anything)
For reference, there are two threads in StackOverflow that address similar problems:
Remote ssh connection from within Emacs
Emacs: Default binary to run with M-x ansi-term
but it isn't obvious to me how to combine the ideas in those threads to get an answer to my question.
(global-set-key (kbd "C-c a") '(lambda () (interactive) (ansi-term "/bin/zsh")))
I suggest you to use multi-term. As its name implies, it lets you deal with multiple term using ansi-term.
Here is a small configuration:
(require 'multi-term)
(eval-after-load "multi-term"
'(setq multi-term-program "/bin/bash"
term-unbind-key-list '("C-x"
"C-h"
"M-x"
"C-z")
term-term-name "xterm-256color"))
(global-set-key (kbd "C-c a") 'multi-term-next)
My whole configuration for multi-term is
here
(compilation-shell-minor-mode is really nice).