Show Keys In Emacs Keymap Value - emacs

When I query the current value of the keymap, eg with M-: (current-local-map), it shows me something along these lines:
Value:
(keymap
(S-mouse-2 . muse-follow-name-at-mouse-other-window)
(mouse-2 . muse-follow-name-at-mouse)
(33554445 . muse-follow-name-at-point-other-window)
(S-return . muse-follow-name-at-point-other-window)
(13 . muse-follow-name-at-point)
(return . muse-follow-name-at-point)
keymap
(67108924 . muse-decrease-list-item-indentation)
(67108926 . muse-increase-list-item-indentation)
(M-return . muse-insert-list-item)
(33554441 . muse-previous-reference)
(S-iso-lefttab . muse-previous-reference)
(S-tab . muse-previous-reference)
(S-mouse-2 . muse-follow-name-at-mouse-other-window)
(mouse-2 . muse-follow-name-at-mouse)
(33554445 . muse-follow-name-at-point-other-window)
(9 . muse-next-reference)
(tab . muse-next-reference)
(3 keymap
(19 . muse-search)
(2 . muse-find-backlinks)
(tab . muse-insert-thing)
(9 . muse-insert-thing)
(16 . muse-project-publish)
(6 . muse-project-find-file)
(61 . muse-what-changed)
(22 . muse-browse-result)
(27 keymap
(20 . muse-publish-this-file))
(33554452 . muse-publish-this-file)
(20 . muse-project-publish-this-file)
(12 . font-lock-mode)
(5 . muse-edit-link-at-point)
(1 . muse-index))
keymap
(27 keymap
(9 . ispell-complete-word)))
I want instead of numbers see something more meaningful like (control ?c) return for example. How do I do that ?

Did you know the command C-h m (describe-mode), which usually shows a description of the current major and minor modes, usually with their keymaps?
(substitute-command-keys "\\{foo-map}") is what describe-mode uses internally to generate the user-friendly description of the keymap foo-map.
If you'd like to have something closer to the keymap structure, the API is documented in "Classifying Events" in the Emacs Lisp manual. For example,
    (event-modifiers 33554445) ==> (shift control)
    (format "%c" (event-basic-type 33554445)) ==> "m"

This is what you need: library help-fns+.el.
http://www.emacswiki.org/emacs/HelpPlus
http://www.emacswiki.org/emacs/download/help-fns%2b.el
That gives you, as C-h M-k, command describe-keymap, which gives you a human-readable description of any keymap bound to a variable. E.g.:
C-h M-k dired-mode-map
dired-mode-map
--------------
For more information check the manuals.
Local keymap for `dired-mode' buffers.
key binding
--- -------
e .. f dired-find-file
RET dired-find-file
C-o dired-display-file
C-t Prefix Command
ESC Prefix Command
SPC dired-next-line
! dired-do-shell-command
# dired-flag-auto-save-files
$ dired-hide-subdir
% Prefix Command
& dired-do-async-shell-command
* Prefix Command
+ dired-create-directory
- negative-argument
. dired-clean-directory
0 .. 9 digit-argument
: Prefix Command
dired-next-dirline
? dired-summary
A dired-do-search
B dired-do-byte-compile
C dired-do-copy
D dired-do-delete
G dired-do-chgrp
H dired-do-hardlink
L dired-do-load
M dired-do-chmod
O dired-do-chown
P dired-do-print
Q dired-do-query-replace-regexp
R dired-do-rename
S dired-do-symlink
T dired-do-touch
U dired-unmark-all-marks
X dired-do-shell-command
Z dired-do-compress
^ dired-up-directory
a dired-find-alternate-file
d dired-flag-file-deletion
g revert-buffer
h describe-mode
i dired-maybe-insert-subdir
j dired-goto-file
k dired-do-kill-lines
l dired-do-redisplay
m dired-mark
n dired-next-line
o dired-find-file-other-window
p dired-previous-line
q quit-window
s dired-sort-toggle-or-edit
t dired-toggle-marks
u dired-unmark
v dired-view-file
w dired-copy-filename-as-kill
x dired-do-flagged-delete
y dired-show-file-type
z kill-this-buffer
~ dired-flag-backup-files
DEL dired-unmark-backward
mouse-face
dired-mouse-find-file-other-window
??
C-t C-t image-dired-dired-toggle-marked-thumbs
C-t . image-dired-display-thumb
C-t a image-dired-display-thumbs-append
C-t c image-dired-dired-comment-files
C-t d image-dired-display-thumbs
C-t e image-dired-dired-edit-comment-and-tags
C-t f image-dired-mark-tagged-files
C-t i image-dired-dired-display-image
C-t j image-dired-jump-thumbnail-buffer
C-t r image-dired-delete-tag
C-t t image-dired-tag-files
C-t x image-dired-dired-display-external
C-M-d dired-tree-down
C-M-n dired-next-subdir
C-M-p dired-prev-subdir
C-M-u dired-tree-up
M-$ dired-hide-all
M-= dired-backup-diff
M-s Prefix Command
M-{ dired-prev-marked-file
M-} dired-next-marked-file
M-DEL dired-unmark-all-files
M-s a Prefix Command
M-s f Prefix Command
% & dired-flag-garbage-files
% C dired-do-copy-regexp
% H dired-do-hardlink-regexp
% R dired-do-rename-regexp
% S dired-do-symlink-regexp
% d dired-flag-files-regexp
% g dired-mark-files-containing-regexp
% l dired-downcase
% m dired-mark-files-regexp
% r dired-do-rename-regexp
% u dired-upcase
* C-n dired-next-marked-file
* C-p dired-prev-marked-file
* ! dired-unmark-all-marks
* % dired-mark-files-regexp
* * dired-mark-executables
* / dired-mark-directories
* ? dired-unmark-all-files
* # dired-mark-symlinks
* c dired-change-marks
* m dired-mark
* s dired-mark-subdir-files
* t dired-toggle-marks
* u dired-unmark
* DEL dired-unmark-backward
: d epa-dired-do-decrypt
: e epa-dired-do-encrypt
: s epa-dired-do-sign
: v epa-dired-do-verify
dired-undo
dired-next-line
dired-previous-line
dired-toggle-read-only
dired-undo
M-s f C-s dired-isearch-filenames
M-s f ESC Prefix Command
M-s a C-s dired-do-isearch
M-s a ESC Prefix Command
M-s f C-M-s dired-isearch-filenames-regexp
M-s a C-M-s dired-do-isearch-regexp

I just wrote the following based on Gilles' answer, so I figured I'd post it.
I see that Drew's describe-keymap has a very similar basis, but also covers various other use-cases, so offhand I'm inclined to suggest using that one as a more complete solution; but FWIW:
(defun my-describe-keymap (keymap)
"Describe a keymap using `substitute-command-keys'."
(interactive
(list (completing-read
"Keymap: " (let (maps)
(mapatoms (lambda (sym)
(and (boundp sym)
(keymapp (symbol-value sym))
(push sym maps))))
maps)
nil t)))
(with-output-to-temp-buffer (format "*keymap: %s*" keymap)
(princ (format "%s\n\n" keymap))
(princ (substitute-command-keys (format "\\{%s}" keymap)))
(with-current-buffer standard-output ;; temp buffer
(setq help-xref-stack-item (list #'my-describe-keymap keymap)))))

Here's a poor-man's implementation what describe-bindings (C-h b) gives you, but restricted to a particular keymap. The formatting isn't great, but should get you started:
(defun describe-keymap (keymap &optional prefix)
(pop-to-buffer "Keymap description")
(erase-buffer)
(describe-keymap-impl keymap prefix))
(defun describe-keymap-impl (keymap &optional prefix)
"show a description of keymap"
(let (keymaps-to-process)
(mapc (lambda (partofkeymap)
(when prefix
(insert (concat prefix " ")))
(insert (key-description (list partofkeymap)))
(insert " ")
(cond ((atom partofkeymap))
((listp (cdr partofkeymap))
(insert "prefix-key")
(if (eq 'keymap (cadr partofkeymap))
(setq keymaps-to-process (cons partofkeymap keymaps-to-process))))
(t
(insert (symbol-name (cdr partofkeymap)))))
(insert "\n"))
(if (and (symbolp keymap) (boundp keymap))
(symbol-value keymap)
keymap))
(while keymaps-to-process
(insert "\n\n")
(describe-keymap-impl (cddar keymaps-to-process) (concat prefix (key-description (list (caar keymaps-to-process)))))
(setq keymaps-to-process (cdr keymaps-to-process))))
nil)
I tested by evaluating both:
(describe-keymap emacs-lisp-mode-map)
(describe-keymap 'emacs-lisp-mode-map)

For a more modern approach, use the which-key-show-full-keymap command from the which-key package. It will ask you for a keymap and then show you all the bindings from that keymap, exactly like it would if you press a prefix key (if which-key-mode is enabled). That is, in the minibuffer, nice and clean.
From the documentation:
which-key-show-full-keymap is an interactive autoloaded compiled Lisp
function in ‘which-key.el’.
(which-key-show-full-keymap KEYMAP)
Show all bindings in KEYMAP using which-key. KEYMAP is selected
interactively from all available keymaps.

You can also use helpful-variable function from helpful package available in Melpa.
Here is sample output:
projectile-command-map is a variable defined in projectile.el.
Value
! projectile-run-shell-command-in-root
& projectile-run-async-shell-command-in-root
4 C-o projectile-display-buffer
4 D projectile-dired-other-window
4 a projectile-find-other-file-other-window
....
vs. standard output:
projectile-command-map is a variable defined in ‘projectile.el’.
Its value is shown below.
Keymap for Projectile commands after ‘projectile-keymap-prefix’.
This variable may be risky if used as a file-local variable.
Value:
(keymap
(33 . projectile-run-shell-command-in-root)
(38 . projectile-run-async-shell-command-in-root)
(53 keymap
(68 . projectile-dired-other-window)
(15 . projectile-display-buffer)
...

Related

How to make describe-function "C-h f" case-insensitive by default

When searching for a function whose name I partly remember, I use C-h f to call describe-function, enter *part-of-function-name, and hit TAB. But I now realize that this search is not case-insensitive.
For example:
C-h f info TAB
lists all functions starting with info, but doesn't include those starting with Info, whereas
C-h f Info TAB
lists all functions starting with Info, but doesn't include those starting with info.
Another example:
C-h f *nfc TAB
gives me *nfc [No match], whereas
C-h f *NFC TAB
gives me ucs-normalize-HFS-NFC-region.
How can I make describe-function case-insensitive by default, using some configuration in my init.el file?
Add a binding of completion-ignore-case to t in the interactive spec of the command. This has the advantages that (a) it affects only describe-function (C-h f) and (b) you can easily toggle it on/off (as with any Emacs advice).
(defadvice describe-function (before ignore-case activate)
"Make it case-insensitive."
(interactive
(let ((completion-ignore-case t) ; <============= ADDED BINDING
(fn (function-called-at-point))
(enable-recursive-minibuffers t)
val)
(setq val (completing-read
(if fn
(format "Describe function (default %s): " fn)
"Describe function: ")
obarray 'fboundp t nil nil (and fn (symbol-name fn))))
(list (if (equal val "") fn (intern val))))))
(setq completion-ignore-case t) should do what you want, tho globally rather than only for C-h f.
Here's a custom function that I use in place of describe-function.
It uses ido completion.
(defvar functions-cache nil)
;;;###autoload
(defun refresh-functions-cache ()
(interactive)
(setq functions-cache nil)
(mapatoms (lambda (symbol)
(when (fboundp symbol)
(push (symbol-name symbol) functions-cache))))
(setq functions-cache (sort functions-cache #'string<)))
;;;###autoload
(defun describe-function-ex (function)
"Display the full documentation of FUNCTION (a symbol)."
(interactive
(let ((fn (function-called-at-point))
(enable-recursive-minibuffers t)
val)
(unless functions-cache
(refresh-functions-cache))
(setq val (ido-completing-read
(if fn
(format "Describe function (default %s): " fn)
"Describe function: ")
functions-cache
nil t nil nil
(and fn (symbol-name fn))))
(list (if (equal val "")
fn (intern val)))))
(if (null function)
(message "You didn't specify a function")
(help-setup-xref (list #'describe-function function)
(called-interactively-p 'interactive))
(save-excursion
(with-help-window (help-buffer)
(prin1 function)
(princ " is ")
(describe-function-1 function)
(with-current-buffer standard-output
;; Return the text we displayed.
(buffer-string))))))

Is is possible to bind a key plus a variable in Emacs?

I have buffers named *terminal<1>*, *terminal<2>*, *terminal<3>*, etc. Is there a way to bind a key combination that will take an argument for the number? That is, I want to bind C-c 1 to switch to *terminal<1>* and C-c 2 to switch to *terminal<2>*, and so on. If I can't do this directly, is it possible to do metaprogramming in Elisp that will define all the functions for me?
In this suggestion, the interactive switch-to-terminal will take either a prefix argument C-u 2 for example, or prompt the user.
The macro then makes setting up the key bindings a little easier.
Finally I bind C-c 1 to C-c 4 to switch to *terminal<1>* to *terminal<4>*.
(defun switch-to-terminal (buf-num)
(interactive "NNumber of buffer to visist: ")
(let* ((buf-name (format "*terminal<%d>*" buf-num))
(buf (get-buffer buf-name)))
(unless buf
(error "No buffer %s" buf-name))
(switch-to-buffer buf)))
(defmacro bind-switch-to-terminal (num)
`(global-set-key
,(kbd (format "C-c %d" num))
(lambda ()
(interactive)
(switch-to-terminal ,num))))
(bind-switch-to-terminal 1)
(bind-switch-to-terminal 2)
(bind-switch-to-terminal 3)
(bind-switch-to-terminal 4)
This change uses the same switch-to-terminal function, but replaces bind-switch-to-terminal with a function. The lexical-let* allows for the creation of a closure to create unique terminal switching functions, the dotimes loop then binds for C-c 1 to C-c 9.
(defun bind-switch-to-terminal (num)
(lexical-let* ((buf-num num)
(switch-func
(lambda ()
(interactive)
(switch-to-terminal buf-num))))
(global-set-key
(kbd (format "C-c %d" buf-num))
switch-func)))
(dotimes (num 9)
(bind-switch-to-terminal (1+ num)))
You can bind keys as usual:
(global-set-key (kbd "C-c 1") (lambda ()
(interactive)
(switch-to-buffer "*terminal<1>*")))
To create all the shortcuts from 1 to 9 we would use macros.
edit: This buggy version may put you on tracks. I give up :(
(defmacro gototerminal (count)
`(global-set-key (kbd ,(concat "C-c " (number-to-string count)))
;; with the comma I want to evaluate what is inside concat
(lambda () (interactive)
(switch-to-buffer (concat "*terminal<" ,count ">*"))))
)
(progn (setq count 1)
(while (< count 10)
(gototerminal count)
(setq count (1+ count))
))
ps: an elisp debugger is edebug. Set it with C-u C-M-x
I would write a function that calls interactive with an n argument which indicates that the function reads a number from the mini buffer:
(defun test (x)
(interactive "nNumber of buffer to visit: ")
(message (concat "received number: " (number-to-string x))))
Binding this to a key will let you enter a number in the mini buffer.
Another way is to use a numerical argument:
(defun test (x)
(interactive "P")
(message (concat "received number: " (number-to-string x))))
Say you bind this function to C-c c, you can then pass it the number 2 as an argument by pressing C-u 2 C-c c.
If you avoid the usage of an existing prefix key like C-c you can trigger a command with one keypress e.g. F9.
This command can have a single key as input.
Example:
(defun test (k)
(interactive "K")
(message "Pressed key: %d" (- (aref k 0) ?0)))
(local-set-key [f9] 'test)

Shortcut for inserting environments in `org-mode`

I'm using org-mode for organizing myself (very useful so far!). However, it is kind of annoying writting
#+begin_comment
...
#+end_comment
each time I'd like to insert an environment.
Question
Is there a shortcut to insert the #+begin_ and #+end_ for a given environment?
In the same way C-c C-o comment RET would insert
\begin{comment}
\end{comment}
in latex-mode.
Org has a facility called "Easy templates": http://orgmode.org/manual/Easy-Templates.html
A template for comment is missing but you can add it with:
(add-to-list 'org-structure-template-alist '("C" "#+begin_comment\n?\n#+end_comment"))
And use it by typing <C followed by TAB.
Alternatively, you could use yasnippet.
Now the corresponding template section is called Structure Template and the insertion sequence is invoked by C-c C-,. I didn't (require 'org-tempo) which is described to support insertion keys like <s TAB.
The comment environment is already defined in org-structure-template-alist. So the comment would be inserted by
C-c C-, C
It's still possible to add a user defined sequence by, for example,
C-c C-, [TAB|RET|SPC] src python :results output :session
delivering
#+begin_src python :results output :session
#+end_src
(emacs 25.2.2, org-mode 9.2)
You could have a look at "org-auctex-keys.el", a minor mode which I created to offer AUCTeX key bindings within Org documents.
In this case, you'd use C-c C-e to insert an environment (prompt to enter the environment name), as what AUCTeX does.
If you're interested, check it out at https://github.com/fniessen/org-auctex-key-bindings.
Not as elegant as the answer of Michael Markert but maybe more expandable.
1) You can select a region and put the block around it or you can just put the block at point.
2) Keyword expansion and history.
3) Keystrokes: C-c b
The command could be further expanded. E.g., for the src block the various switches like -n -r and export to files could be supported.
(defun list-major-modes ()
"Returns list of potential major mode names (without the final -mode).
Note, that this is guess work."
(interactive)
(let (l)
(mapatoms #'(lambda (f) (and
(commandp f)
(string-match "-mode$" (symbol-name f))
;; auto-loaded
(or (and (autoloadp (symbol-function f))
(let ((doc (documentation f)))
(when doc
(and
(let ((docSplit (help-split-fundoc doc f)))
(and docSplit ;; car is argument list
(null (cdr (read (car docSplit)))))) ;; major mode starters have no arguments
(if (string-match "[mM]inor" doc) ;; If the doc contains "minor"...
(string-match "[mM]ajor" doc) ;; it should also contain "major".
t) ;; else we cannot decide therefrom
))))
(null (help-function-arglist f)))
(setq l (cons (substring (symbol-name f) 0 -5) l)))))
(when (called-interactively-p 'any)
(with-current-buffer (get-buffer-create "*Major Modes*")
(clear-buffer-delete)
(let ((standard-output (current-buffer)))
(display-completion-list l)
(display-buffer (current-buffer)))))
l))
(defvar org-insert-block-hist nil
"History for command `org-insert-block'")
(defvar org-insert-block-hist/src:major nil
"History for major mode in org src blocks.")
(defvar org-insert-block-list (append org-protecting-blocks
'("comment" ""))
"List of block types offered as completion for command `org-insert-block'")
;; block_src switches: -n () -r (references) -l "((%s))" (label format) -k (keep labels)
(defvar org-insert-block-list-specials
"Assoc list of Commands for reading additional specification of org-blocks.")
(setq org-insert-block-list-specials
'(("src" . (concat " " (completing-read "Major mode:"
(list-major-modes)
nil nil
(car org-insert-block-hist/src:major)
'(org-insert-block-hist/src:major . 1)
)))))
(defun org-insert-block (bl &optional b e attributes)
"Put region between b and e into org-block of kind bl.
If b or e is nil then put org-block limiters around point.
The string attributes is inserted behind the string #+begin_... "
(interactive
(let ((usereg (use-region-p))
(blKind (completing-read "Input block kind (tab: completion, uparrow: history):"
org-insert-block-list nil nil (car org-insert-block-hist) '(org-insert-block-hist . 1))))
(list
blKind
(when usereg (region-beginning))
(when usereg (region-end))
(let ((spec (assoc blKind org-insert-block-list-specials)))
(when spec (eval (cdr spec)))
))))
(let ((begBlock (concat "\n#+begin_" bl attributes "\n"))
(endBlock (concat "\n#+end_" bl "\n")))
(if (and b e)
(save-restriction
(narrow-to-region b e)
(goto-char (point-min))
(insert begBlock)
(goto-char (point-max))
(insert endBlock)
(indent-region (point-min) (point-max)))
(let ((p (point)))
(insert endBlock)
(goto-char p)
(insert begBlock))
)))
(add-hook 'org-mode-hook '(lambda ()
(local-set-key (kbd "C-c b") 'org-insert-block)))

Swap parentheses and square brackets in Emacs Paredit

How can I define a command in paredit mode that swaps parentheses and square brackets?
So the task is to turn this, for example:
(blah
(a (b)
c))
into this:
(blah
[a (b)
c])
With paredit mode, move to start of the expression (a ..) and then:
C-M-SPC [ <right> M-s
Without paredit, but still wanting to maintain balanced parens during transitions, move to a and then press C-M-SPC multiple times until error and then (assuming that CUA mode is on):
C-x <timeout> <right> <backspace> <backspace> [ ] <left> C-v
Well that is complex, so let's stick with paredit mode version, and try to make a command out of it. Keyboard Macro Editor tells you the names of commands being used, so you would be able to come up with at least the following code:
(defun my-switch-to-square ()
"Change (..) to [..]."
(interactive)
(mark-sexp --)
(paredit-open-square --)
(right-char --)
(paredit-splice-sexp --))
-- indicates part of code we have not yet decided. After you read documentation of each function in the code, you learn what arguments to pass, and that there is no need to call mark-sexp. After rewriting docstring and adding a call to left-char, the code you end up with would be:
(defun my-switch-to-square ()
"Change |(..) to |[..]. | is point position."
(interactive)
(paredit-open-square 1)
(right-char 1)
(paredit-splice-sexp)
(left-char 1))
The following code does what you request. You can bind swap-parens to whatever key binding you want.
(defvar swap-paren-pairs '("()" "[]"))
(defun swap-parens-at-points (b e)
(let ((open-char (buffer-substring b (+ b 1)))
(paren-pair-list (append swap-paren-pairs swap-paren-pairs)))
(while paren-pair-list
(if (eq (aref open-char 0) (aref (car paren-pair-list) 0))
(save-excursion
(setq to-replace (cadr paren-pair-list))
(goto-char b)
(delete-char 1)
(insert (aref to-replace 0))
(goto-char (- e 1))
(delete-char 1)
(insert (aref to-replace 1))
(setq paren-pair-list nil))
(setq paren-pair-list (cdr paren-pair-list))))))
(defun swap-parens ()
(interactive)
(cond ((looking-at "\\s(")
(swap-parens-at-points (point) (save-excursion (forward-sexp) (point))))
((and (> (point) 1) (save-excursion (forward-char -1) (looking-at "\\s)")))
(swap-parens-at-points (save-excursion (forward-sexp -1) (point)) (point)))
((message "Not at a paren"))))
I'm not sure what you mean by "define command"? You can do it like this:
|(foo bar)
"|" is point.
;; Keyboard Macro Editor. Press C-c C-c to finish; press C-x k RET to cancel.
;; Original keys: C-u [ C-f <M-up> C-b
Command: last-kbd-macro
Key: none
Macro:
C-u [ ;; paredit-open-square
C-f ;; forward-char
<M-up> ;; paredit-splice-sexp-killing-backward
C-b ;; backward-char
Although to be honest, this type of usage scenario is more common for VIM trickery. I've never actually used this IRL.
bzr branch lp:s-x-emacs-werkstatt
will deliver a library inspired by paredit, but delivering more detailed commands
M-x ar-bracket-parentized-atpt RET
puts brackets around parenteses at point
look inside thing-at-point-utils.el and the other files what else is provided

Setting Emacs to Split Buffers Side-by-Side

A lot of Emacs functions automatically split the screen. However, they all do so such that the windows are one on top of the other. Is there any way to make them split such that they are side-by-side by default instead?
(setq split-height-threshold nil)
(setq split-width-threshold 0)
GNU Emacs Lisp Reference Manual: Choosing Window Options
Two solutions here, use any one you like:
A: Vertically(left/right) by default:
(setq split-height-threshold nil)
(setq split-width-threshold 0)
B: Automatically split window vertically(left/right) if current window is wide enough
(defun display-new-buffer (buffer force-other-window)
"If BUFFER is visible, select it.
If it's not visible and there's only one window, split the
current window and select BUFFER in the new window. If the
current window (before the split) is more than 100 columns wide,
split horizontally(left/right), else split vertically(up/down).
If the current buffer contains more than one window, select
BUFFER in the least recently used window.
This function returns the window which holds BUFFER.
FORCE-OTHER-WINDOW is ignored."
(or (get-buffer-window buffer)
(if (one-window-p)
(let ((new-win
(if (> (window-width) 100)
(split-window-horizontally)
(split-window-vertically))))
(set-window-buffer new-win buffer)
new-win)
(let ((new-win (get-lru-window)))
(set-window-buffer new-win buffer)
new-win))))
;; use display-buffer-alist instead of display-buffer-function if the following line won't work
(setq display-buffer-function 'display-new-buffer)
Put any one in you .emacs/init.el file.
You can change the "100" to the value you like, depending on you screen.
If you got two windows in one frame, and you want to change the layout from vertical to horizontal or vice verse, here is a solution:
(defun toggle-window-split ()
(interactive)
(if (= (count-windows) 2)
(let* ((this-win-buffer (window-buffer))
(next-win-buffer (window-buffer (next-window)))
(this-win-edges (window-edges (selected-window)))
(next-win-edges (window-edges (next-window)))
(this-win-2nd
(not (and (<= (car this-win-edges)
(car next-win-edges))
(<= (cadr this-win-edges)
(cadr next-win-edges)))))
(splitter
(if (= (car this-win-edges)
(car (window-edges (next-window))))
'split-window-horizontally
'split-window-vertically)))
(delete-other-windows)
(let ((first-win (selected-window)))
(funcall splitter)
(if this-win-2nd (other-window 1))
(set-window-buffer (selected-window) this-win-buffer)
(set-window-buffer (next-window) next-win-buffer)
(select-window first-win)
(if this-win-2nd (other-window 1))))))
;; C-x 4 t 'toggle-window-split
(define-key ctl-x-4-map "t" 'toggle-window-split)
Put it in your .emacs/init.el file, Use C-x 4 t to toggle the layout of your windows.
(setq split-height-threshold 0) (setq split-width-threshold 0)
is what i had to use to get the desired behaviour (no horizontal splitting)
Sometimes we need change between Horizontal and Vertical according current display and our requirement (more lines or more columns).
I recommand the great ToggleWindowSplit, And I bind key to "C-c y"
http://www.emacswiki.org/emacs/ToggleWindowSplit
the simple answer of setting 2 variables to nil and 0 didn't work for me, so I wrote 2 simple functions: one just splits the window into NX vertical buffers and opens files named (for example) file.1 file.2 ... file.NX in each and another one does the same think, except does it in 2D (NY rows by NX columns for opening files f.1 f.2 ... f.[NX*NY]). To install, add this code to .emacs:
(defun grid-files-h (nx wx pfx)
"Using dotimes, split the window into NX side-by-side buffers of width WX and load files starting with prefix PFX and ending in numbers 1 through NX"
(let (ox fn k) ; ox is not used, but fn is used to store the filename, and k to store the index string
(dotimes (x (- nx 1) ox) ; go through buffers, x goes from 0 to nx-2 and ox is not used here
; (print x)
(setq k (number-to-string (+ x 1) ) ) ; k is a string that goes from "1" to "nx-1"
; (print k)
(setq fn (concat pfx k) ) ; fn is filename - concatenate prefix with k
; (print fn)
(find-file fn) ; open the filename in current buffer
(split-window-horizontally wx) ; split window (current buffer gets wx-columns)
(other-window 1) ; switch to the next (right) buffer
)
(setq k (number-to-string nx )) ; last (rightmost) buffer gets the "nx" file
(setq fn (concat pfx k) ) ; fn = "pfx"+"nx"
(find-file fn ) ; open fn
(other-window 1) ; go back to the first buffer
)
)
(defun grid-files-sq (ny wy nx wx pfx)
"Using dotimes, split the window into NX columns of width WX and NY rows of height WY and load files starting with prefix PFX and ending in numbers 1 through NX*NY"
(let (oy ox fn k)
(dotimes (y ny oy) ; go through rows, y goes from 0 to ny-1 and oy is not used here
(split-window-vertically wy) ; create this row
(dotimes (x (- nx 1) ox) ; go through columns, x goes from 0 to nx-2 and ox is not used here
(setq k (number-to-string (+ 1 (+ x (* y nx) ) ) ) ) ; k must convert 2 indecies (x,y) into one linear one (like sub2ind in matlab)
(setq fn (concat pfx k) ) ; filename
(find-file fn ) ; open
(split-window-horizontally wx) ; create this column in this row (this "cell")
(other-window 1) ; go to the next buffer on the right
)
(setq k (number-to-string (+ nx (* y nx) ) ) ) ; rightmost buffer in this row needs a file too
(setq fn (concat pfx k) ) ; filename
(find-file fn ) ; open
(other-window 1) ; go to next row (one buffer down)
)
)
)
and then to use the vertical one, I go to *scratch* (C-x b *scratch* RET,C-x 1), type in (grid-files-h 3 20 "file.") then C-x C-e, or if you want to test out the square qrid one, C-x 1, type in (grid-files-sq 2 15 3 20 "f.") and then C-x C-e and you should see something like
This probably can be done better/more efficiently, but it's a start and it does what I need it to do (display a bunch of sequentially named small files). Feel free to improve or reuse.
I use multiple frames (OSX windows) in emacs regularly for different projects. Here's how I setup a few frames initially split to a left and right window.
(defun make-maximized-split-frame (name)
(let (( f (make-frame (list (cons 'name name))) ))
(maximize-frame f)
(split-window (frame-root-window f) nil t)
))
(make-maximized-split-frame "DocRaptor")
(make-maximized-split-frame "Gauges")
(make-maximized-split-frame "Instrumental")
The direct answer is to press C-c 3.
It's not clear from the question if you want a permanent setting change, but I found this question looking for this answer and didn't find it. (The answer has actually been sitting in a comment for the last 11 years)