Goodbye helm, hello ido.

cp-ido contains nearly a week of work, starting from a few lines of
configuration and moving to a wannabe helm-mini implementation -
cp/ido-mini
This commit is contained in:
Kashish Sharma 2016-09-29 23:00:42 +05:30
parent 07636ba167
commit 26a56fb788
3 changed files with 276 additions and 12 deletions

View File

@ -6,7 +6,8 @@
(setq god-exempt-major-modes nil)
(setq god-exempt-predicates nil)
(define-key helm-map (kbd "<escape>") 'god-local-mode)
;; for helm
;; (define-key helm-map (kbd "<escape>") 'god-local-mode)
(defun my-update-cursor ()
(setq cursor-type (if (or god-local-mode buffer-read-only)

262
contrapunctus/cp-ido.el Normal file
View File

@ -0,0 +1,262 @@
(require 'ido)
(setq ido-use-virtual-buffers t
ido-enable-regexp t)
(ido-mode 'buffers)
;; (ido-mode 1)
(ido-everywhere 1)
;; (global-set-key (kbd "C-x C-l") 'ido-switch-buffer)
;; (require 'ido-ubiquitous)
;; (ido-ubiquitous-mode 1)
(cp-set-keys
:keymap ido-common-completion-map
:bindings
`((,(kbd "C-n") ido-next-match)
(,(kbd "C-p") ido-prev-match)))
;; ;; do not enable...
;; (cp-set-keys
;; :keymap ido-completion-map
;; :bindings
;; `((,(kbd "C-n") ido-next-match)
;; (,(kbd "C-p") ido-prev-match)))
(cp-set-keys
:keymap ido-buffer-completion-map
:bindings
`((,(kbd "C-n") ido-next-match)
(,(kbd "C-p") ido-prev-match)
(,(kbd "C-x C-p") ido-toggle-prefix)))
(require 'smex)
(global-set-key (kbd "M-x") 'smex)
;; ;; thanks to wilfredh -
;; ;; https://gist.github.com/Wilfred/31e8e0b24e3820c24850920444dd941d
;; (defun wh/ido-switch-buffer-with-filename ()
;; "Switch to a buffer with ido, including the filename in the prompt."
;; (interactive)
;; (let* ((bufs (buffer-list))
;; (bufs-with-paths
;; (--map (with-current-buffer it
;; (if (buffer-file-name)
;; (format "%s <%s>" (buffer-name) (buffer-file-name))
;; (buffer-name)))
;; bufs))
;; (chosen-index
;; (-elem-index
;; (ido-completing-read
;; "Switch to buffer: " bufs-with-paths)
;; bufs-with-paths)))
;; (switch-to-buffer (nth chosen-index bufs))))
(defun cp-testfn-1 (a)
(+ a a))
(defvar cp-test-function
;; #'cp-testfn-1
(lambda ()
(cp-testfn-1 2)))
(defun cp-testfn-2 ()
(funcall cp-test-function))
(cp-testfn-2)
(defun cp/testfn (pred list)
(let ((el (car list)))
(if (funcall pred el)
(append (cdr list)
(list el))
(cp/testfn pred (cdr list)))))
(defun cp/buffer-names ()
"Return a list of the name of all buffers returned
by (buffer-list)."
(-map 'buffer-name (buffer-list)))
;; (defun cp/buffer-dc-alist ()
;; "Return an alist of buffer names and buffer-display-count
;; values."
;; (-zip (cp/buffer-names)
;; (--map (with-current-buffer it
;; buffer-display-count)
;; (buffer-list))))
;; (defun cp/buffer-dc-alist-sorted ()
;; "Return alist created by `cp/buffer-dc-alist', sorted by
;; buffer-display-count values."
;; (--sort (> (cdr it) (cdr other))
;; (cp/buffer-dc-alist)))
;; (defun cp/buffer-list-dc-sorted ()
;; "Like `buffer-list' but sorted by buffer-display-count
;; values."
;; (-map 'car (cp/buffer-dc-alist-sorted)))
(defun cp/ido-sort-buffer-list ()
"Return a list of buffer names using (buffer-list), processed
the way ido-switch-buffer does."
(cl-flet ((leading-space-p
(el)
(string-match-p "^ " el)))
(let* ((bufs (cp/buffer-names))
;; Remove buffers whose names start with a space
(bufs-main (-remove #'leading-space-p
bufs))
(bufs-space (-filter #'leading-space-p
bufs))
;; Put visible buffers at the end of list, in reverse order of appearance
;; (e.g. (vb1 b1 vb2 b2 ...) becomes (b1 b2 ... vb2 vb1))
(bufs-wins-alist (-zip bufs-main
(-map 'get-buffer-window-list
bufs-main)))
(bufs-with-wins (-filter 'cdr bufs-wins-alist))
(bufs-wo-wins (-remove 'cdr bufs-wins-alist)))
(append
(-map 'car bufs-wo-wins)
(reverse (-map 'car bufs-with-wins))))))
(defun cp/ido-mini-recentf-list ()
"Returns the contents of `recentf-list', with files being
visited by a buffer placed at the end of the list."
(append
(-remove #'get-file-buffer recentf-list)
(-filter #'get-file-buffer recentf-list)))
;; TODO - make all operations on buffer list and recentf-list -
;; sorting, addition of paths, coloring into their own single
;; functions, and the functions user-definable via "-function"
;; variables.
;; TODO - colors!
;; - dired buffers - major-mode
;; - unsaved buffers - buffer-modified-p
;; - buffers modified outside emacs - verify-visited-file-modtime
;; - buffers with deleted files - buffer-file-name -> file-exists-p
;; - and maybe the matched substring in the candidates
;; TODO - clean input history, do not store file paths
;; TODO - C-j to create buffer with the search string as name.
;; add variables to
;; - toggle displaying paths with buffer names
(defvar cp/ido-mini-use-paths nil
"If non-nil, display file paths of the associated files of
buffers, where applicable (see function `cp/ido-mini-add-paths').
Additionally, completion will search for buffer names as well as
their file paths.
Users may also find it useful to set this to nil and to enable
buffer uniquifying via `toggle-uniquify-buffer-names'.")
(defun cp/ido-mini-add-paths (&optional buflist)
"Add paths to a list of buffer names. If BUFLIST is nil or
omitted, use output from cp/ido-sort-buffer-list."
(let ((bufs (if buflist buflist (cp/ido-sort-buffer-list))))
(if cp/ido-mini-use-paths
(--map (with-current-buffer it
(if (buffer-file-name)
(format "%s <%s>" (buffer-name) (buffer-file-name))
(buffer-name)))
(cp/ido-sort-buffer-list))
bufs)))
(defun cp/ido-mini-color-recentf ()
"Color output from cp/ido-mini-recentf-list."
(-map (lambda (el)
(propertize el 'face 'ido-virtual))
;; recentf-list
(cp/ido-mini-recentf-list)))
(defun cp/ido-mini-color-buffer-names ()
""
(-map (lambda (buffer)
(if (buffer-modified-p)
(propertize buffer 'face 'ido-indicator)))))
;; (defun cp/ido-mini-clean)
;; pipeline
;; recentf-list -> sorted recentf -> color
;; \
;; combine -> ido-completing-read -> select from sorted buffer/recentf list
;; /
;; (buffer-list) -> buffer names -> sort -> add paths -> color
;; | | |
;; (cp/buffer-names) | (cp/ido-mini-add-paths)
;; (cp/ido-sort-buffer-list)
(defun cp/ido-mini ()
"A helm-mini replacement using Ido. Switch to a buffer or a
recentf entry with ido. If `cp/ido-mini-use-paths' is non-nil,
search for and display the whole file path instead of just the
file name.
Using ido-vertical in conjunction may be beneficial.
Based off code from wilfredh -
https://gist.github.com/Wilfred/31e8e0b24e3820c24850920444dd941d"
(interactive)
(let*
((bufs (cp/ido-sort-buffer-list))
(bufs-with-paths (cp/ido-mini-add-paths bufs))
(recentf-list-colored (cp/ido-mini-color-recentf))
(bufs-and-recentf (append bufs-with-paths
recentf-list-colored))
(chosen-index
(-elem-index (ido-completing-read
"Switch to buffer: "
bufs-and-recentf
nil nil nil
'ido-buffer-history)
bufs-and-recentf)))
;; is the chosen candidate in the buffer-list or recentf-list?
(if (< chosen-index (length bufs))
(switch-to-buffer (nth chosen-index
bufs))
(find-file (nth (- chosen-index (length bufs))
(cp/ido-mini-recentf-list))))))
(global-set-key (kbd "C-x C-l") 'cp/ido-mini)
(ido-vertical-mode 1)
;; is this being saved by desktop.el and actually being disabled here?
;; (toggle-uniquify-buffer-names)
;; How I'm guessing ido sorts the buffer list -
;; 1. buffers whose names start with a space are not shown in the
;; 'initial' list, and appear only if the search string matches
;; them..
;; 2. if (get-buffer-window-list <buffer>) returns non-nil, the buffer
;; is moved to the end of the list, in the reverse order of how it
;; appears in (buffer-list),
;;
;; e.g. if (buffer-list) has buffers (1 2 3 ...) and 1, 2 and 3 are
;; open in windows, the ido list will be (... 3 2 1)
;; 3. After some 10-12 entries (...yeah, really :\), buffers whose
;; names start with * are shown, sorted alphabetically.
;; - it does not seem to be a fixed number! I've seen it go up to
;; 15. It's as though there's a 'recently used list', after which
;; the "^\*" buffers are shown...
;; 4. "Virtual" buffers (recentf files), if enabled, are shown at the
;; end of the currently-open buffers.
;; - ido-switch-buffer behaviour with recentf is silly - if you
;; have many files with the same name in different directories
;; and search for the file name, you will only see a single file
;; instead, and no path. That's part of what led to this NIH'ing.
;; 5. buffer-display-count actually seems to play no role in all this,
;; contrary to previous suspicion.

23
init.el
View File

@ -336,7 +336,8 @@ not before a link, or with a prefix arg, call
;(define-key global-map (kbd "C-c SPC") 'ace-jump-mode)
(global-set-key (kbd "M-SPC") 'ace-jump-mode)
(load "cp-helm")
;; (load "cp-helm")
(load "cp-ido")
(load "cp-god")
(load "cp-irc")
(load "cp-parens")
@ -426,16 +427,16 @@ not before a link, or with a prefix arg, call
'(diary-entry-marker (quote font-lock-variable-name-face))
'(fci-rule-character-color "#452E2E")
'(gnus-logo-colors (quote ("#0d7b72" "#adadad")))
'(helm-completing-read-handlers-alist
(quote
((describe-function . helm-completing-read-symbols)
(describe-variable . helm-completing-read-symbols)
(debug-on-entry . helm-completing-read-symbols)
(find-function . helm-completing-read-symbols)
(find-tag . helm-completing-read-with-cands-in-buffer)
(ffap-alternate-file)
(tmm-menubar)
(completon-at-point . f))))
;; '(helm-completing-read-handlers-alist
;; (quote
;; ((describe-function . helm-completing-read-symbols)
;; (describe-variable . helm-completing-read-symbols)
;; (debug-on-entry . helm-completing-read-symbols)
;; (find-function . helm-completing-read-symbols)
;; (find-tag . helm-completing-read-with-cands-in-buffer)
;; (ffap-alternate-file)
;; (tmm-menubar)
;; (completon-at-point . f))))
'(highlight-changes-colors (quote ("#FD5FF0" "#AE81FF")))
'(highlight-tail-colors
(quote