0

I have been using Emacs on a NixOS (Linux) machine running GNOME as a Desktop Environment. The hardware interface of this system is an American keyboard. Since I am Brazilian, it was necessary for me to have a Brazilian Portuguese input provided by GNOME.

Now, I have a new Macbook Air M1 which is a macOS machine also built with an American keyboard as hardware. After some hassles, I managed to find a way to generate Brazilian Portuguese characters, such as ç, exactly, in the same way, concerning the keybindings on both the macOS and the NixOS machine. See more details here with my question and answer on the topic to illustrate what I mean.

Things have been working well. Exceptionally for some keys in Emacs. This is especially painful because I am a heavy user of Emacs.

Also, I have been using Emacs on both machines with the same configuration file.

One of my frustrations happens when I am typing things like "/". To generate this in an American keyboard with Brazilian Portuguese input, I need to press the right Option key (or the right Alt key in a non-apple keyboard) and the letter "q" on the keyboard.

(i) This keybinding works perfectly on NixOS outside of Emacs;

(ii) This keybinding works perfectly on NixOS also inside of Emacs with this Emacs init file;

(iii) This keybinding works perfectly on macOS outside of Emacs;

Unfortunately, this keybinding does not work on macOS inside of Emacs - even though the config file is the same!

After executing the describe-binding (C-h k) in Emacs, the mini-buffer asks for a binding: "Press key".

After replying with: "right-alt"-q, I was expecting nothing to be returned, since letters and normal characters such as bar /, vertical bar | are not commands to be described. Unfortunately, I received:

paredit-reindent-defun is an interactive and byte-compiled function defined in paredit.el

This is not an isolated problem. It happens in other situations too. When trying to get the question mark ?.

I am able to generate it in macOS outside of Emacs with Alt-right-w. If I type the same thing in macOS inside of Emacs, the keybinding is associated with a command kill-ring-save and not to the character question mark.

Some people suggested using this approach. It did not work out. Actually, it made several things stop working.

I must also highlight that I have started using a program called Karabiner-elements (before that I was using macOS to change a small subset of key modifiers, such as control<>caps_lock). This is my config file.

Is there a way to solve this?

How can I have the same Emacs in macOS with Portuguese input that I have in NixOS?

4
  • 1
    Those are the normal keybindings for M-q and M-w. I think you need to read the Emacs manual where it talks about how Meta is assigned to a key on OSX. Incidentally, every “normal” key is bound to the command self-insert-command which inserts the character you typed into the buffer.
    – db48x
    Aug 7, 2022 at 3:37
  • 3
    Does this answer your question? How to access symbols in emacs, which are accessed normally by alt key
    – matteol
    Aug 7, 2022 at 7:07
  • @matteol, no. See my edition on the question and my answer. Maybe it did not work out because I am using karabiner-elements on macOS. Aug 23, 2022 at 20:11
  • @db48x, thanks for trying to help. You have helped me multiple times. Unfortunately, even self-insert-command does not work for me to get / and ?. The only way I could achieved was with my hack posted as a solution. Aug 23, 2022 at 20:13

3 Answers 3

2

You must configure your keyboard(s) as follow:

  1. In your init.el file configuration add:
(setq ns-alternate-modifier     'alt
      ns-command-modifier       'meta
      ns-function-modifier      'hyper
      ns-right-command-modifier 'super)
(set-language-environment "UTF-8")  ;; Doom-emacs suggested is enough
  1. Go to System Preferences -> Keyboard -> Input Sources and add the keyboard Portuguese -> Brazilian Portuguese-ABNT2. You should also (must) have the US keyboard, since in programming languages it is easy to type on such keyboard than using many shortcuts. Switching between two keyboards is easy, only Ctrl+Space will toggle to the other keyboard; Emacs allows inserting keys from whatever keyboard is in use. The result in picture below:

enter image description here

If pressing on [ sign on your keyboard (which is symbol ´ ), you can press e to get é (see the brown-marked keys). Note that from OS menu, press the keyboard symbol and select Show keyboard Viewer. It will remain in forefront and will react to/show any keypress

  1. Some of the keys, activated normally with option key will not work, but with a small code we can enable them, e.g. use (global-set-key (kbd "A-q") "/") to have the slash character enabled. Do the same with the other needed option-keys.

In picture is Emacs 28.1 on M1 machine, with evil + doom-modeline + doom-themes.

2
  • 1
    I have International keyboard, but tested also with Portuguese Brazilian and works ok. Just open keyboard viewer and you will see two keys marked with a brown square - press the character in that square and you will see what to press next.
    – Ian
    Aug 7, 2022 at 13:46
  • Thanks for trying to help, @Ian. Unfortunately, your suggestion did not work out for me. I will post how I handled the situation. Aug 23, 2022 at 20:05
0

Unfortunately, the initial solution posted here by @Ian did not work out.

Also, the reference to this other previous question did not solve my problem. Surprisingly, some were even voting to close it...

I am not sure why the recommended material did not work out in my environment. Maybe because of my use of the awesome software called Karabiner Elements. This is my config. I use it to change things like modifiers.

Luckily, I managed to find a way to solve this. It could be seen as a dirty hack. But it works! Check it out:

;; Trying to solve some character due to Brazilian Portuguese input
;; and American Keyboard. Especially the Slash character: "/"
(defun pmd/insert-slash ()
  "Hack because of the problem on macOS/Emacs/Portuguese input/American keyboard."
  (interactive)
  (insert "/"))
(global-set-key (kbd "C-x C-M-q") 'pmd/insert-slash)

(defun pmd/insert-question-mark ()
  "Hack because of the problem on macOS/Emacs/Portuguese input/American keyboard."
  (interactive)
  (insert "?"))
(global-set-key (kbd "C-x C-M-w") 'pmd/insert-question-mark)

Now, I have 2 computers running the same emacs file, both with the American Keyboard, both with Brazilian Portuguese Input, one with NixOS and the other with macOS, and almost everything works equally. Until now, the only difference is the pain to insert / and ?.

2
  • I don't see how this can work without something else that's highly unusual in your Emacs configuration and possibly in your Karabiner configuration (if it was your Karabiner configuration alone, it would affect other applications). If pressing a key sends a sequence starting woth Ctrl-X, that has to be programmed somewhere. I can't find it though. Aug 27, 2022 at 8:20
  • I have looked into Pedro's config link - and while I am not familiar with Karabiner, I see an item referring to Aquamacs, which conflicts and interfere with emacs; also on apple.stackexchange.com the O/P uses emacs-everywhere, so this also could make some problems.
    – Ian
    Aug 28, 2022 at 12:51
0

What I do (though I don't use Karabiner Elements), is that I set my input method to Latin-1 Prefix (but I could have used Spanish or Portuguese Prefix) but the coding system to UTF-8. I've set both of them in my init file in the following way:

Set input method:

  (set-language-environment "Latin-1")
  (add-hook 'text-mode-hook 
    (lambda () "Defaults for text modes"
      ;; flyspell mode to spell check everywhere
      (flyspell-mode 1)
      ;; latin-1-prefix to always have it on
      (activate-input-method "latin-1-prefix")
      ;; Activa el diccionario espanol y hace que se guarde el
      ;; diccionario personal sin preguntar:
      (setq ispell-dictionary "es" ispell-silently-savep t)
      )
    )

Set coding system:

  (setq utf-translate-cjk-mode nil) ; disable CJK coding/encoding (Chinese/Japanese/Korean characters)
  (set-language-environment 'utf-8)
  ;;  (set-keyboard-coding-system 'utf-8-mac) ; For old Carbon emacs on OS X only
  (setq locale-coding-system 'utf-8)
  (set-default-coding-systems 'utf-8)
  (set-terminal-coding-system 'utf-8)
  (set-selection-coding-system
   (if (eq system-type 'windows-nt) 'utf-16-le ;; https://rufflewind.com/2014-07-20/pasting-unicode-in-emacs-on-windows
 'utf-8
 )) 
  (prefer-coding-system 'utf-8)
  (modify-coding-system-alist 'file "\\.sjs\\'" 'shift_jis)
  

It might be overkill, but it works for me on MacOS (Intel MacBookAir 2019). (Disclaimer: I've found this coding somewhere in the web, but forgot to the source. Sorry.)

2
  • 1
    That would be a completely different way to insert characters like ç, which the question clearly indicates is not acceptable. Aug 27, 2022 at 8:13
  • Thanks for trying to help @pilgix, but I do not thing this a good approach! Aug 28, 2022 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.