What does "s-[keyname]" refer to in Emacs, and how do I tell Emacs to ignore it? - macos

Background information:
I'm on a Mac, and I've just upgraded to Emacs 23.1 via http://emacsformacosx.com/. There are a few issues, notably the lack of full screen ability.
I've attempted to get around this last issue by installing Megazoomer, which adds a global input manager bound to Cmd-return. This causes the currently forward application to maximise. However, Emacs reports that <s-return> is undefined. I've never seen an s-[key] mentioned before, and Google isn't forthcoming with an answer.
So, two parts:
What does s-[key] mean? This is purely for my satisfaction; and
Can I tell Emacs to ignore this key combination and let the key combination carry through to the system (so that hopefully I can have full screen Emacs back again)?
EDIT: so 1) is resolved, and as to 2) I've got: (global-set-key (kbd "<s-return>") 'ignore), which at least stops the error. However, Emacs still swallows the key combination, which isn't ideal.

It's the Super key, like M- is the Meta key (alt key on a PC keyboard, Command key on your keyboard) and C- is the Control key.
I have of course never actually seen a super key on my keyboard... they are from a long gone era. Wikipedia has an image of this impressive "Space Cadet keyboard" which has all the modifiers you'll ever need:

With plain Emacs 23.1 on a Macbook Pro, I can map the right option key to super by
(setq ns-right-option-modifier 'super)
Your other choice seems to be the function key, which would be ns-function-modifier. However, fn might have other uses, whereas Emacs’ default is to map ns-right-option-modifier to ’left (ie, the same effect as the left option key, which I at any rate need to get the # character!), so the right option key is to some extent redundant.
Left-handers may want to reverse this.

For the question about what the s-[key] means, on ubuntu box it means the Windows® shaped key. What it means on the OSX systems, I do not know.
As for maximizing windows, could you try this?
(It should work, iif OSX runs an X server somewhere underneath it all)
(if (equal (window-system) 'x)
(progn
(defun toggle-fullscreen ()
"Toggles fullscreen"
(interactive)
(x-send-client-message nil 0 nil "_NET_WM_STATE" 32
'(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
(x-send-client-message nil 0 nil "_NET_WM_STATE" 32
'(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0)))
(global-set-key (kbd "C-c C-y") 'x-clipboard-yank)
(global-set-key (kbd "M-RET") 'toggle-fullscreen)))
This little snippet is what I use to toggle fullscreen on my *nix computers. And yanking from X's clipboard is a neat ability to have.
As for how to set keybindings, use global-set-key for mode independent keybindings.
(Add it to your .emacs file if you want it to be permanent.)

(setq ns-command-modifier nil)
That is supposed to do what you want. However, it's having somewhat unpredictable behaviour on machine when I test it, so be warned.

Related

Process "emacs" alt-shift combinations in Iterm2 on Mac OS

I tried many manuals how to deal with alt key in emacs in iTerm, but no of them helped me. The problem is that all suggestions work for alt key, but not for combination of alt+shift.
What do I want:
(global-set-key (kbd "M-S-<left>") 'shrink-window-horizontally)
(global-set-key (kbd "M-S-<right>") 'enlarge-window-horizontally)
(global-set-key (kbd "M-S-<down>") 'shrink-window
(global-set-key (kbd "M-S-<up>") 'enlarge-window)
So, alt+shift+arrow must change the window size. I can easily support this for alt+arrow using this manual:
https://azaleasays.com/2013/07/05/setting-up-mac-os-x-and-iterm2-for-emacs/.
But all this doesn't work for alt+shift.
Also I tried these suggestions. It doesn't work in this particular case:
Making iTerm to translate 'meta-key' in the same way as in other OSes
I don't think it's important, but for pure picture I connect via SSH to instance and work under tmux.
Did anybody try to do this?
Okay, I mostly fixed it.
The main problem is that I couldn't find any description of how to correctly write "Key Mappings" for Iterm2 (googled a lot). After some attempts I could somehow resolve this as a puzzle, but it's really strange to do it like this not having any good documentation. If somebody has a link, please share it.
For people who potentially have this problem I'll write what I could find:
Find the actual code of the key using "showkey -a" command. It usually has 2 values, let's name them X and Y. In this case the code "1A" (arrow up) would have X = 1, Y = A.
Use the following list as reference. I found the codes only by entering numbers and check. No documentation found on this. You can try another numbers if you need another key combinations:
SHIFT – 2
ALT – 3
ALT+SHIFT – 4
CTRL – 5
CTRL+SHIFT – 6
CTRL+ALT – 7
Let's name the previous value with variable Z.
The format for the "Key Mappings" is "[X;ZY" in this case. So, we split the source code to 2 parts, put "[" before and ";Z" inside.
So, CTRL+SHIFT+"Arrow up" would be "[1;6A".
It's not full though. For example, I couldn't understand how to code letters with ALT. For example, CTRL+ALT+z. If somebody knows, please comment also.

How to toggle fullscreen with Emacs as default?

I am using the mac emacs from http://emacsformacosx.com/, and I need to click the maximized icon when I start my emacs.
How can I set the maximized emacs window as default?
start emacs like this
emacs -mm
Ryan McGeary's maxframe.el works well for me on both Aquamacs and Emacs.app. I found it through EmacsWiki: http://www.emacswiki.org/emacs/FullScreen . That page talks about a patched version which is now a 404 page, but the original one at https://github.com/rmm5t/maxframe.el seems to work fine.
Here is a function written and used by me. When you succesivelly press F11, emacs switches in 4 modes:
(defun switch-fullscreen nil
(interactive)
(let* ((modes '(nil fullboth fullwidth fullheight))
(cm (cdr (assoc 'fullscreen (frame-parameters) ) ) )
(next (cadr (member cm modes) ) ) )
(modify-frame-parameters
(selected-frame)
(list (cons 'fullscreen next)))))
(define-key global-map [f11] 'switch-fullscreen)
The short answer is to add the following to your custom-set-variables:-
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
...
'(initial-frame-alist (quote ((fullscreen . maximized))))
...
)
Given below is what I wanted as a solution to the same problem. TL;DR.
I face the same problem but in all applications and not just in Emacs. To this end, I have globally bound the shortcut key cmd-m on my Mac to the Zoom menu option which is usually the menu option for the green maximize button. Emacs however doesn't provide the Zoom menu option which is usually under the Window menu item. So I ended up with the following.
I just coded up the following last night.
;; This defines cmd-m to do the same as clicking the green titlebar button
;; usually meant for the "Window -> Zoom" menu option in Mac apps
(defun zoom () "zoom, same as clicking the green titlebar button in Mac app windows"
(interactive)
(set-frame-parameter
nil 'fullscreen
(pcase (frame-parameter nil 'fullscreen)
(`nil 'fullheight)
(`fullheight 'maximized)
(`fullboth (ding) 'fullboth)
(`fullscreen (ding) 'fullscreen)
(_ nil))))
(global-set-key (kbd "s-m") 'zoom)
This keyboard shortcut in the last line of the code goes well with my global to Mac cmd+m key binding that I described initially. You could customize it to whatever suits you. I am used to pressing cmd-m on launching most apps until it fits screen and Emacs is one of them for me. So I don't bother with the initial-frame-alist setting.
I went on to complete the feature-set I wanted by adding the following code tonight.
;; This defines ctrl-cmd-f to do the same as clicking the toggle-fullscreen titlebar
;; icon usually meant for the "View -> Enter/Exit Full Screen" menu option in
;; Mac apps
(defun toggle-fullscreen() "toggle-fullscreen, same as clicking the
corresponding titlebar icon in the right hand corner of Mac app windows"
(interactive)
(set-frame-parameter
nil 'fullscreen
(pcase (frame-parameter nil 'fullscreen)
(`fullboth nil)
(`fullscreen nil)
(_ 'fullscreen))))
(global-set-key (kbd "C-s-f") 'toggle-fullscreen)
; For some weird reason C-s-f only means right cmd key!
(global-set-key (kbd "<C-s-268632070>") 'toggle-fullscreen)
A couple of notes:-
If you're just learning to use pcase from this code, be careful not to make the same mistake as I did by misreading the backquote as a quote in the docs.
fullscreen is an alias to fullboth and is not a misnomer like the latter is as a term for what it means and hence I have not only handled that case as a value for (frame-parameter nil 'fullscreen) but use that whenever I want to set-frame-parameter to fullboth
HTH
The answer given at https://stackoverflow.com/a/1029065/351716 works for me (with GNU Emacs v24.2.1). To reprise, define the following function in your .emacs file:
(defun x11-maximize-frame ()
"Maximize the current frame (to full screen)"
(interactive)
(x-send-client-message nil 0 nil "_NET_WM_STATE" 32 '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
(x-send-client-message nil 0 nil "_NET_WM_STATE" 32 '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0)))
For convenience, you can bind the command to a key. I use the C-z key, which would otherwise minimize the frame, which I have no need for, but always find annoying when I hit it accidentally:
(global-set-key (kbd "C-z") 'x11-maximize-frame)
As I noted in the comment I added to that answer, using this command repeatedly cycles between the normal frame state and the maximized state, but one little annoyance: in between those two, there's a strange state where the frame is almost but not quite vertically maximized. But that's a minor problem.

Making Aquamacs scrolling more like Emacs

When I used emacs, I used to be able to set the mark and highlight full pages for yanking using C-v, or scroll-up. However, in Aquamacs if I set the mark then hit C-v it loses the mark and stops highlighting. I noticed that in Aquamacs C-v is instead mapped to aquamacs-page-down, so I tried adding the following command to my site file:
(define-key osx-key-mode-map "C-v" 'scroll-up)
and this didn't successfully remap the key. I then tried something similar:
(define-key global-map "\C-v" 'scroll-up)
and still nothing. Aquamacs very stubbornly hangs onto the mapping to aquamacs-page-down. I noticed, however, that there's an additional function, aquamacs-page-down-extend-region, which does exactly what I'm talking about. Its key sequence, however, is , and I have no idea how to input that. I tried "shift-control-v" to no avail.
Has anyone been able to get Aquamacs to scroll pages while maintaining the mark?
I've found a way to get this to work, for posterity's sake.
Paste this into the .emacs file:
;; Enable scrolling to maintain mark if set
(defun scroll-down-maintain-mark ()
(interactive)
(if mark-active
(aquamacs-page-down-extend-region)
(aquamacs-page-down)))
(defun scroll-up-maintain-mark ()
(interactive)
(if mark-active
(aquamacs-page-up-extend-region)
(aquamacs-page-up)))
(define-key global-map "\C-v" #'scroll-down-maintain-mark)
(define-key global-map "\M-v" #'scroll-up-maintain-mark)
hitting C-SPC C-v C-l, the last to recenter screen, seems to show that it does indeed preserve the mark.
and subsequent copy and yank works fine
perhaps this behavior added on newer Aquamacs.

Unable to type braces and square braces in emacs

I'm running Mac OS X and GNU Emacs 22.3.1. I use a swedish keyboard. I am unable to type braces { }, [ ] in emacs. When trying to type braces I get parenthesis. Since I'm quite new to Mac and emacs I need a little help on configuring emacs to get this right.
(setq mac-option-modifier nil
mac-command-modifier 'meta
x-select-enable-clipboard t)
This is what I use for my swedish keyboard. It even works with svorak A5, if you use it :)
You could also try:
(setq mac-option-key-is-meta t)
(setq mac-right-option-modifier nil)
I'm assuming that you're using a graphical emacs, and not just using the OS X bundled version from within Terminal.
To ensure that Emacs responds to keystrokes in the same way as other OS X apps, try the following:
(setq default-input-method "MacOSX")
And in particular, if you want to use the Option key to enter extended characters not on your keyboard (e.g. "Option-c c" => "ç"), use these settings:
(setq mac-command-modifier 'meta)
(setq mac-option-modifier 'none)
(Put these commands in your ~/.emacs or ~/.emacs.d/init.el emacs startup file, and restart Emacs, or just "M-x eval-buffer" while editing the file.)
(setq default-input-method "MacOSX")
(setq mac-command-modifier 'meta
mac-option-modifier nil
mac-allow-anti-aliasing t
mac-command-key-is-meta t)
Try this. You will be able to use the Alt key as a AltGR and for all the old M-x functions you will have to use your command key.
Bind the relevant keyboard shortcuts to anonymous functions that insert those characters, for example add these lines to ~/.emacs for European Portuguese:
(global-set-key "\M-(" (lambda () (interactive) (insert "{")))
(global-set-key "\M-)" (lambda () (interactive) (insert "}")))
(global-set-key "\M-8" (lambda () (interactive) (insert "[")))
(global-set-key "\M-9" (lambda () (interactive) (insert "]")))
Then save ~/.emacs with C-x C-s and reload it with M-x load-file and type ~/.emacs.
One downside is that this does not work in the mini-buffer, and typing "Alt-9" will insert text in the buffer and not the mini-buffer.
Comparison with other solutions: This solution maintains compatibility with other shortcuts using M-. The solutions by #monotux, #sanityinc, and Abdul Bijur V A do work, but they do not maintain that compatibility, e.g. Cmd-Q no longer quits the program and M-x no longer calls the mini-buffer to execute commands.
The solution by #patrikha doesn't suit touch-typing, which requires the same modifier commands on the right and the left side of the keyboard (Command, Alt/Option, Shift, and Control). For example, with this solution doing M-x requires the left thumb on the left Alt key and the left index finger on the S key, instead of the right thumb on the right Alt key. You could (setq mac-left-option-modifier nil), but that might require a change in habits for letters on the right side of the keyboard.
Notes: If you use AquaMacs, the wiki has a work-around in the section "Inputting {}[] etc. on non-English keyboards, or other keys with the Option modifier".
I also add this line to the end of ./emacs to show the matching of brackets and braces: (show-paren-mode).
I would try a Cocoa based emacs ie version 23. For a mac integrated emacs I would try Aquamacs
I had the same issue with a french keyboard. It looks like an Aquamacs issue (Carbon Emacs does not replace { with ()).
The change in emacs above work fine and I could type brackets but I could not use standard shortcuts anymore (Ctrl+C/Ctrl+V for instance).
Aquamacs provides a workaround.
Menu Bar > Options > Option, Command, Meta keys > select ...Meta & French
It worked fine for me. However it may not work for swedish, no swedish keyboard option.
Using Aquamacs:
From the main menu, go to Options - Option, command, meta keys and select "option for composed characters".
The braces and the brackets work as with the standard Mac keyboard.
You don't need to remember those programming like things: Here is the answer. Go to Keyboard preferences, and check the "Show keyboard and character viewer in menu bar". After that, check on the menu bar near the battery meter for the icon and start double clicking any character you want.

Emacs M-x commands for invoking "GUI-style" menus

Question: How could I find out the M-x equivalent commands for doing GUI-based operations in Emacs, in those cases where my Emacs-variant uses OS-specific desktop functionality?
Background: Conventional understanding states that everything in Emacs is a command, and that commands can be invoked via M-x, as long as you know the name of the command. Assuming this statement is correct, what is the way to find the name of the commands used to trigger the "GUI-style" menus in a "desktop" based Emacs variant?
For example, if I were to mouse-select the File menu to open a file, the OS-specific "GUI" style file-open dialog pops up, waiting for my input.
How could I find out the M-x equivalent command for doing the exact same thing?
I thought that describe-key would tell me what I needed to know, but it's indication to use:
M-x menu-find-file-existing
doesn't invoke the "GUI" style file-open dialog. Instead, it uses the Emacs internal non-GUI-OS-neutral variant.
You need to trick Emacs into thinking that the keyboard was not being used, which is not as intuitive as tricking it into thinking that the mouse was used. :)
(defadvice find-file-read-args (around find-file-read-args-always-use-dialog-box act)
"Simulate invoking menu item as if by the mouse; see `use-dialog-box'."
(let ((last-nonmenu-event nil))
ad-do-it))
Tested on Emacs 22.2.1 on WinXP. I believe the paradigm has been around for a while, though, so it should work on older Emacs. No clue if XEmacs works similarly or not.
Wow, I'm glad you asked that. I've been meaning for a while to look it up myself.
C-h k followed by the menu choice will tell you this. Here, for example, is what you get from choosing menu/edit/paste:
<menu-bar> <edit> <paste> runs the command clipboard-yank
which is an interactive compiled Lisp function in `menu-bar.el'.
It is bound to <paste>, <f18>, <menu-bar> <edit> <paste>.
(clipboard-yank)
Insert the clipboard contents, or the last stretch of killed text.
If you want the details, follow the link for menu-bar-el to the LISP source:
(defun menu-find-file-existing ()
"Edit the existing file FILENAME."
(interactive)
(let* ((mustmatch (not (and (fboundp 'x-uses-old-gtk-dialog)
(x-uses-old-gtk-dialog))))
(filename (car (find-file-read-args "Find file: " mustmatch))))
(if mustmatch
(find-file-existing filename)
(find-file filename))))

Resources