Is it possible to collapse a function in emacs? - emacs

So, for a lisp homework assignment I have, it has a long defparameter expression that's a large data set. What I'm wondering is, does emacs or SLIME have anything to "collapse" that large defparameter into a single line, like, say, MATLAB does?

Like Bertfred mentioned, hideshow works great, and it comes build in with more recent versions of emacs. To use it simply add the following snippet to your init file:
(add-hook 'prog-mode-hook #'hs-minor-mode)
(global-set-key (kbd "C-c <right>") 'hs-show-block)
(global-set-key (kbd "C-c <left>") 'hs-hide-block)
The first line enables the functionality in any major mode associated with programming. Once there, C-c <left> and C-c <right> should do what you expect - just be mindful of where point is.
https://www.emacswiki.org/emacs/HideShow

There's also a package on Melpa called vimish-fold (or the equivalent evil version of it - evil-vimish-fold).
It is not as "automatic" as hideshow or outline in the sense that you have to select the lines you want to fold, but the advantage is that you can fold any lines. And the folds don't disappear the when you close your file.
You can define your keybindings for creating/deleting folds and for unfolding/refolding folds, and there you go!
(global-set-key (kbd "your-keybinding") 'vimish-fold)
(global-set-key (kbd "your-keybinding") 'vimish-fold-delete)
(global-set-key (kbd "your-keybinding") 'vimish-fold-toggle)

Related

Disable completion pop-up in python-mode shadowing indentation

I updated recently my version of python-mode.el. Since then pressing at end of lines tries completion instead of indentation and pops up a buffer of suggestion.
I would rather have only indentation. I use something else for completion.
How can this be achieved?
By the way, auto indentation grew /less/ smart with the update. What possibly have I broken?
You didn't tell which python-mode. Also assume it's about TAB-key.
WRT python-mode.el comment out the present key-setting and write the desired one.
Like that:
;; (define-key map (kbd "TAB") 'py-indent-or-complete)
(define-key map (kbd "TAB") 'py-indent-line)
You shouldn't need to modify the mode file itself to make this happen. You should be able to do it via python-mode-hook. This works for me (in my ~/.emacs):
(add-hook 'python-mode-hook
(define-key python-mode-map (kbd "TAB") 'py-indent-line))

How to restore anything-like behavior for TAB autocomplete in helm?

A related question was asked here. But the answer is to get used to the new way autocomplete works in helm. I cannot get used to it, here's why.
Say, I want to open a file /home/user/work/f.txt. I do C-x C-f, it takes me to current dir, say /current/dir/. I hit Backspace and notice that autocomplete won't let me delete /. Ok, turn off autocomplete with C-Backspace. Then kill the line C-a C-k and start typing. Notice that autocomplete doesn't work, turn it back on C-Backspace. Normally I would type the part that I know is probably unique, e.g. /hom and hit Tab.
Not here. As soon as I type /ho, autocomplete resolves it to /home/, but since I type fast, I end up with /home/m, and continue typing now meaningless characters until I notice it. Chances are, by that time I got autocompleted into directories that I had no intent of going.
So I have to constantly watch what autocomplete is doing, rather than rely on what I type and only checking suggested completions when I hit Tab.
I also find myself descending into wrong directories due to occasional typo, and then having difficulty going up a level -- evil autocomplete won't let you fix the situation with a couple of Backspaces.
This interaction of autocomplete behavior and the removal of Tab functionality completely upsets my work, so much that I decided to ask this question. I am looking to either:
restore the old functionality
learn how to use autocomplete in a meaningful way, or
configure helm's C-x C-f to behave more like a linux command line
Please help.
Here are some ido tricks if you want to start using it.
Let me know if helm is better, perhaps I'll switch over.
I tried once shortly, but didn't like it.
Basic setup:
This will give you `ido-find-file on C-x C-f.
(ido-mode)
(setq ido-enable-flex-matching t)
Smex setup:
Install from https://github.com/nonsequitur/smex.
(require 'smex)
(global-set-key "\C-t" 'smex)
Switch buffers with ido:
(global-set-key
"η"
(lambda()(interactive)
(when (buffer-file-name)
(save-buffer))
(ido-switch-buffer)))
(global-set-key
(kbd "C-η")
(lambda()(interactive)
(let ((ido-default-buffer-method 'other-window))
(ido-switch-buffer))))
Tricks:
;; 1
(add-hook 'dired-mode-hook
(lambda()
(define-key dired-mode-map "j" 'ido-find-file)))
(add-hook
'ido-setup-hook
(lambda()
;; 2
(define-key ido-file-dir-completion-map "~"
(lambda ()(interactive)
(ido-set-current-directory "~/")
(setq ido-exit 'refresh)
(exit-minibuffer)))
;; 3
(define-key ido-buffer-completion-map "η" 'ido-next-match)
;; 4
(define-key ido-buffer-completion-map (kbd "C-p")
'ido-fallback-command)
;; 5
(define-key ido-completion-map (kbd "C-.") 'smex-find-function)
(define-key ido-completion-map (kbd "C-,") 'smex-describe-function)))
Quick open file from dired.
Move to home directory one key faster (i.e. ~ instead of ~/).
Cycle buffer candidates with the same key that shows the candidates (a la C-TAB in Firefox).
Useful to have a fall back when you want to create a file-less buffer (ido will try
select an existing buffer unless you fall back).
Useful to jump to function definition/documentation.
If you want TAB completion of directories and file names, map helm-execute-persistent-action to the TAB key:
(define-key helm-map (kbd "<tab>") 'helm-execute-persistent-action)
See also the answer to "How can I change emacs helm-find-file default action[...]".

Define key-bindings in emacs

I'd like to map a command in emacs to a key-binding. I want the command Control-l to have the same effect as the command Alt-x goto-line followed by a return (since that command first needs a return to be invoked and then a line number).
I modified the init file as follows:
(define-key (M-x goto-line) '\C-l)
but that didn't work. The error was that define-key was being given more than 1 arguments.
Does anyone know how to reset key-bindings in emacs?
Thanks!
M-g g is the default shortcut for goto-line. You might want to try that.
To redefine C-l use:
(global-set-key (kbd "C-l") 'goto-line)
Easiest way to customize lots of keybindings is to install John Wiegley's bind-key module, which is a part of use-package Lisp package. Solution in your init.el:
(require 'bind-key)
(bind-key "C-l" 'goto-line)
Minor modes keys usually override global keys, so if you don't want such behavior, use function bind-key* instead. The package is on MELPA, if you don't know what is it, quickly learn about Emacs package management (should take you 2 minutes to set up MELPA as your repository).
The main problem with keybindings in Emacs is that minor modes keys often override your custom ones. In vanilla Emacs people workaround by creating a minor mode for your own keybindings. If you really wanna understand how Emacs keys work, read Key Bindings # Emacs Manual and Keymaps # Elisp Manual carefully.
I have set as (global-set-key (kbd "C-x g") 'goto-line). You can use that or (global-set-key (kbd "C-l") 'goto-line). I would personally do not touch the C-l key from its default behavior.
If you must use M-x define-key, use
(define-key global-map (kbd "C-l") 'goto-line). The 1st argument to define-key is a KEYMAP.

Rebind C-space in Emacs

I've tried various version to no avail:
(global-set-key (kbd "C-<space>") 'tempo-complete-tag)
(global-set-key [C-space] 'tempo-complete-tag)
I'm using CUA mode and running Emacs on Ubuntu, version: GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.0)
of 2009-09-27 on crested, modified by Debian
When I run tempo-complete-tag manually it tells me it is bound to C-space but C-space still runs cua-set-mark (or if CUA is disable, set-mark-command).
How can I rebind the C-space shortcut in Emacs to a command I decide?
C-h k (key) will tell you how Emacs refers to a given key (which is "C-SPC" in this instance). (global-set-key (kbd "C-SPC") 'tempo-complete-tag) will do what you want.
I always use the (kbd) function for key-bindings, as it allows you to refer to the key the same way it is typically written everywhere else.
Do keep in mind that C-SPC is a standard set-mark-command binding! Personally I'd pick something different :)
Also keep in mind that "global-set-key" will only do what you want, if your mode doesn't override it. I'm too lazy to load tempo to see if it does indeed override C-SPC, but it might well do so, in which case, you want to put this in your .emacs:
(add-hook 'tempo-mode-hook
(lambda ()
(local-set-key (kbd "C-SPC") 'tempo-complete-tag)
))
Alternative syntax for key binding is via vector:
(global-set-key [?\M-\ ] 'cycle-spacing)
(global-set-key [?\C-\ ] 'tempo-complete-tag)

Assign multiple Emacs keybindings to a single command?

I'm giving ErgoEmacs mode a try to see if I can use Emacs more comfortably. Some of its keybindings are fairly intuitive, but in many cases I don't want to outright replace the defaults.
For example, in the context of ErgoEmacs' navigation shortcut structure, M-h makes sense as a replacement for C-a--but I want to be able to use both, not just M-h. I tried simply duplicating the commands:
;; Move to beginning/ending of line
(defconst ergoemacs-move-beginning-of-line-key (kbd "C-a")) ; original
(defconst ergoemacs-move-end-of-line-key (kbd "C-e")) ; original
(defconst ergoemacs-move-beginning-of-line-key (kbd "M-h")) ; ergoemacs
(defconst ergoemacs-move-end-of-line-key (kbd "M-H")) ; ergoemacs
But Emacs simply overwrites the first keybinding with the second. What's the best way to address this?
To re-post reply from ergo-emacs mailing list:
Xah Lee said:
that's very easy.
in the
ergoemacs-mode.el file, there's this
line (load "ergoemacs-unbind") just
comment it out. That should be all
you need to do. However, note that
ErgoEmacs keybinding defines those
common shortcuts such as Open, Close,
New, Save... with keys Ctrl+o,
Ctrl+w, Ctrl+n, Ctrl+s etc. About 7 of
them or so. So, i think some of these
will hit on emacs traditional
bindings with Ctrl. if you are new to
ErgoEmacs and trying to explore it,
you might just try starting with few
keys. this page might have some
useful info:
http://code.google.com/p/ergoemacs/wiki/adoption
thanks for checking out ErgoEmacs!
Xah ∑ http://xahlee.org/
As it turns out, ErgoEmacs uses two files to define the keybinding. One is the main ergoemacs-mode.el file, and the other is the specific keyboard layout you select (e.g. ergoemacs-layout-us.el). The latter document creates a constant, which the former uses to create the keybinding. So while I thought I was duplicating the keybinding, I was actually changing the constant which was subsequently used for that purpose.
Solution:
In ergomacs-mode.el:
;; Move to beginning/ending of line
(define-key ergoemacs-keymap ergoemacs-move-beginning-of-line-key 'move-beginning-of-line)
(define-key ergoemacs-keymap ergoemacs-move-end-of-line-key 'move-end-of-line)
(define-key ergoemacs-keymap ergoemacs-move-beginning-of-line-key2 'move-beginning-of-line) ; new
(define-key ergoemacs-keymap ergoemacs-move-end-of-line-key2 'move-end-of-line) ; new
In ergoemacs-layout-us.el:
;; Move to beginning/ending of line
(defconst ergoemacs-move-beginning-of-line-key (kbd "M-h"))
(defconst ergoemacs-move-end-of-line-key (kbd "M-H"))
(defconst ergoemacs-move-beginning-of-line-key2 (kbd "C-a")) ; new
(defconst ergoemacs-move-end-of-line-key2 (kbd "C-e")) ; new
Huh? Is having one and only one way for every function some golden principle of ErgoEmacs? Because normal keybinding works exactly the opposite way: you name one key at a time and specify what it should do. If a mode defines a global variable to mean "the key that end-of-line is bound to", then of course there can be only one value, but with the normal binding commands you can bind the same function to as many combinations as you like. In fact, every keybinding I have ever seen used looked either like this
(global-set-key [(meta space)] 'just-one-space)
or like this
(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-c-mode-hook ()
(define-key c-mode-map [(control c) b] 'c-insert-block))
if it's only for a specific mode.