DEV Community

Euber Alexandre Barbosa
Euber Alexandre Barbosa

Posted on

GNU Emacs: O bom e velho ido-mode

GNU Emacs é antigo, até mais que muitas cidades do Brasil e com isso tem uma bagagem cheia de pacotes velhos para leléu.

Dentre as várias funções desse velhinho, existem os completadores das várias partes desse editor, uma dessas é o Interactive do things ou ido-mode, que vai um pouco além do modesto completing-read.

ido é um pacote incluído no editor, mas não ativado por padrão, então pode ser ativado sem precisar baixar nada.

Nesse ponto, eventualmente você vai perceber que o Emacs tem vários pacotes alternativos em hibernação a alguns poucos códigos para sua ativação, como:

(require 'ido)
(ido-mode t)
Enter fullscreen mode Exit fullscreen mode

Pronto, ido esta ativado e com isso vai consumir qualquer lista de string, mas provendo uma interface mais limpa e moderna, testa isso no scratch buffer:

(let ((trapalhoes '("mussum" "zacarias" "didi" "dede")))
  (ido-completing-read "Qual seu trapalhão favorito: " trapalhoes t))
Enter fullscreen mode Exit fullscreen mode

ou algo mais apropriado:

  (defun e/ido-load-theme ()
    "Carregue um tema usando Ido."
    (interactive)
    (let* ((themes (mapcar (lambda (x) (symbol-name x)) (custom-available-themes)))
       (theme (ido-completing-read "Choose recent file: " themes nil t)))
      (when theme
    (load-theme (intern theme) nil))))
Enter fullscreen mode Exit fullscreen mode

Bacana não?! haha, mas tem como realçar ainda mais o comportamento de ido com alguns pacotes externos adicionais:

Uma ótima novidade do Emacs 30 é a opção de poder instalar pacotes de forma reproduzível via use-package com a nova prop :vc:


(use-package ido-completing-read+
  :ensure t
  :vc (:url "https://github.com/DarwinAwardWinner/ido-completing-read-plus")
  :config
  (require 'ido-completing-read+)
  (ido-ubiquitous-mode 1))
Enter fullscreen mode Exit fullscreen mode

E por coerência aqui vai minha configuração final:

(use-package ido
  :init
  (setq ido-enable-flex-matching t
    ido-everywhere t)
  (ido-mode 1)
  :custom
  (ido-create-new-buffer 'always
             ido-enable-prefix nil
             ido-enable-regexp t
             ido-decorations (quote ("\n-> " "" "\n " "\n ..." "[" "]" " [No match]" " [Matched]" " [Not readable]" " [Too big]" " [Confirm]"))
             ido-file-extensions-order '(".lisp" ".py" ".org" ".el")
             ido-max-directory-size 100000
             ido-use-filename-at-point t
             ido-enable-dot-prefix t
             ido-use-url-at-point t
             ido-use-filename-at-point 'guess
             ido-use-virtual-buffers t)
  :config
  (defun e/ido-bookmark-jump (bname)
    "Switch to bookmark BNAME interactively using `ido'."
    (interactive
     (list (ido-completing-read "Bookmark: " (bookmark-all-names) nil t)))
    (bookmark-jump bname))

  (defun e/recentf-ido-find-file ()
    "Find a recent file using Ido."
    (interactive)
    (let ((file (ido-completing-read "Choose recent file: " recentf-list nil t)))
      (when file
    (find-file file))))

  (defun e/ido-load-theme ()
    "Load theme using Ido."
    (interactive)
    (let* ((themes (mapcar (lambda (x) (symbol-name x)) (custom-available-themes)))
       (theme (ido-completing-read "Choose recent file: " themes nil t)))
      (when theme
    (load-theme (intern theme) nil))))

  (defun e/mx-ido ()
    "Open Mx in ido-fashioned way."
    (interactive)
    (call-interactively
     (intern
      (ido-completing-read
       "M-x "
       (all-completions "" obarray 'commandp)))))

  (define-key (cdr ido-minor-mode-map-entry) [remap write-file] nil)
  :bind (("C-c r" . 'e/recentf-ido-find-file)
     ("C-x C-f" . 'ido-find-file)))
Enter fullscreen mode Exit fullscreen mode

Recomendação para leitura:

Informações gerais:

Top comments (0)

The discussion has been locked. New comments can't be added.