Last active
October 28, 2017 10:00
-
-
Save frostidaho/bc3a7b1be32f916dc65d to your computer and use it in GitHub Desktop.
Revisions
-
frostidaho revised this gist
Jul 1, 2016 . 1 changed file with 13 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -153,6 +153,11 @@ Plus a bit." (chopped_string (substring dir offset))) (replace-regexp-in-string "^[^~][^/].*?/" "…/" chopped_string))) (defun shorten-buffer (bname max-length) "Show up to `max-length' characters of a buffer name `bname'" (let ((mlen (min (length bname) max-length))) (substring bname 0 mlen))) (defmacro ivy--buffer-make-info-fn(info_symbol real_buf_expr virt_buf_expr) (let ((sym info_symbol) (symfaces)) @@ -175,7 +180,9 @@ Plus a bit." (list (ivy--buffer-make-info-fn 'buffer-name (shorten-buffer buffer ivy-buffer-max-buffer-display-length) (shorten-buffer buffer ivy-buffer-max-buffer-display-length)) (ivy--buffer-make-info-fn 'mode @@ -249,8 +256,10 @@ For example, the cons cell: ) (defvar ivy-buffer-max-dir-display-length 25 "Truncate the display length of a directory to this value.") (defvar ivy-buffer-max-buffer-display-length 50 "Truncate the display length of buffers to this value.") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Code for formatting entries in minibuffer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -288,4 +297,4 @@ For example, the cons cell: :sort nil)))) ) (provide 'ivy_buffer_extend) -
frostidaho revised this gist
Jun 30, 2016 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -271,7 +271,8 @@ For example, the cons cell: ;; Add this action due to a change in how ivy-call handles alists ;; https://github.com/abo-abo/swiper/commit/c009b28337f408fe571b24be7bdb304bbc596a76 (defun ivy--switch-buffer-action-alist (x) (ivy--switch-buffer-action (if (stringp x) x (cdr x)))) (defun ivy-switch-buffer () "Switch to another buffer." @@ -287,4 +288,4 @@ For example, the cons cell: :sort nil)))) ) (provide 'ivy_buffer_extend) -
frostidaho revised this gist
Jun 30, 2016 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -268,6 +268,11 @@ For example, the cons cell: (ivy-detailed-candidates total_bufs ivy-buffer-format ivy--buffer-info-fns) )) ;; Add this action due to a change in how ivy-call handles alists ;; https://github.com/abo-abo/swiper/commit/c009b28337f408fe571b24be7bdb304bbc596a76 (defun ivy--switch-buffer-action-alist (x) (ivy--switch-buffer-action (cdr x))) (defun ivy-switch-buffer () "Switch to another buffer." (interactive) @@ -277,10 +282,9 @@ For example, the cons cell: (let ((this-command 'ivy-switch-buffer)) (ivy-read "Switch to buffer: " (ivy--buffer-display-list) :action #'ivy--switch-buffer-action-alist :keymap ivy-switch-buffer-map :sort nil)))) ) (provide 'ivy_buffer_extend) -
frostidaho revised this gist
Nov 17, 2015 . 1 changed file with 280 additions and 112 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,118 +1,286 @@ (require 'cl) (require 'ivy) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; General-purpose formatting functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun ivy--cands-get-details (format_spec cand_info_fns entries) "Extract symbols from FORMAT_SPEC, and evaluate the corresponding function found in CAND_INFO_FNS for all ENTRIES. For example: (setq my_format_spec '(make-upper-case \" string doesn't count as key \" (add-gmbh . \"%s\"))) (setq my_info_fns (list '(make-upper-case . upcase) '(add-gmbh . (lambda (x) (concat x \" GmbH\"))))) (ivy--cands-get-details my_format_spec my_info_fns '(\"audi\")) --> ((make-upper-case . \"AUDI\") (add-gmbh . \"audi GmbH\"))." (cl-flet* ((calc-symbol-entry (entry info_sym) (let ((info_fn (cdr (assq info_sym cand_info_fns)))) (cons info_sym (funcall info_fn entry)))) (calc-all-symbols-entry (info_symbols entry) (mapcar (apply-partially #'calc-symbol-entry entry) info_symbols)) ) (let ((info_symbols (mapcar (lambda (x) (if (consp x) (car x) x)) (remove-if #'stringp format_spec)))) (mapcar (apply-partially #'calc-all-symbols-entry info_symbols) entries) ))) (defun ivy--format-maxlen-col (cands_info col) "Compute the max length of COL for entries in CANDS_INFO. Return this value as string." (cl-flet* ((get-symstr-len (cand symstr) (length (cond ((stringp symstr) symstr) ((consp symstr) (format (cdr symstr) (cdr (assq (car symstr cand))))) (t (format "%s" (cdr (assq symstr cand))))))) (get-col-len (col cand) (apply '+ (mapcar (apply-partially #'get-symstr-len cand) col))) ) (number-to-string (apply #'max (mapcar (apply-partially #'get-col-len col) cands_info))) )) (defun ivy--format-template-columns (format_spec default_colfrmt cands_info) (let ((columns nil) (col nil) (colfrmt default_colfrmt)) ;; Split the elements of format_spec into columns ;; based on the presence of the element "<col>". ;; Each new "<col>" creates a new column. ;; If an element is a string of the form "%.*?s" ;; use that string in place of default_colfrmt. (dolist (entry format_spec) (if (stringp entry) (cond ((string-equal "<col>" entry) (if col (push (cons colfrmt (nreverse col)) columns)) (setq col nil) (setq colfrmt default_colfrmt)) ((string-match "%.*?s" entry) (setq colfrmt entry)) (t (push entry col))) (push entry col))) (if col (push (cons colfrmt (nreverse col)) columns)) (setq columns (nreverse columns)) ;; Each col in columns now starts with a frmt string ;; e.g., (car col) --> "%10s" or "%-<MAXLEN>.<MAXLEN>s" ;; the rest of each col contains the symbols and strings ;; to be displayed. ;; Iterate over columns and check if the column's format string ;; contains "<MAXLEN>". ;; If it does calculate the maximum length of that column ;; and replace "<MAXLEN>" with that value. (dolist (col columns) (if (string-match "<MAXLEN>" (car col)) (setcar col (replace-regexp-in-string "<MAXLEN>" (ivy--format-maxlen-col cands_info (cdr col)) (car col))))) columns )) (defun ivy--format-make-rows (cands_info template_columns) (cl-flet* ((listcat (x) (apply #'concat x)) (format-symbol (symbol row) (or (cdr (assq symbol row)) "")) (format-element (row elem) (cond ((stringp elem) elem) ((consp elem) (format (cdr elem) (format-symbol (car elem) row))) (t (format-symbol elem row)))) (format-column (template_col row) (format (car template_col) (listcat (mapcar (apply-partially #'format-element row) (cdr template_col))))) (format-row (template_cols row) (listcat (mapcar (lambda (x) (format-column x row)) template_cols))) ) (mapcar (apply-partially #'format-row template_columns) cands_info))) (defun ivy--format-details (cands format_spec info_fns &optional default_colfrmt) "This is the main ivy--format-* function. You shouldn't need to touch the other ones in general" (if (not default_colfrmt) (setq default_colfrmt "%-<MAXLEN>.<MAXLEN>s")) (let* ((cands_info (ivy--cands-get-details format_spec info_fns cands)) (template_columns (ivy--format-template-columns format_spec default_colfrmt cands_info))) (ivy--format-make-rows cands_info template_columns) )) (defun ivy-detailed-candidates (candidates format_spec info_fns &optional default_colfrmt) "Given a list of CANDIDATES calculate the detailed candidates based on FORMAT_SPEC, INFO_FNS, and optionally DEFAULT_COLFRMT. It returns an association list with each element of the form (detailed candidates . candidates) This association list can be given directly to `ivy-read'. If passed to `ivy-read' the 'detailed candidates' will be displayed, while the ivy-read actions will be passed the corresponding candidate. " (let ((cands_detail (ivy--format-details candidates format_spec info_fns default_colfrmt))) (mapcar* #'cons cands_detail candidates))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Code for calculating information about a buffer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun shorten-directory (dir max-length) "Show up to `max-length' characters of a directory name `dir'. Plus a bit." (let* ((offset (max (- (length dir) max-length) 0)) (chopped_string (substring dir offset))) (replace-regexp-in-string "^[^~][^/].*?/" "…/" chopped_string))) (defmacro ivy--buffer-make-info-fn(info_symbol real_buf_expr virt_buf_expr) (let ((sym info_symbol) (symfaces)) `(cons ,sym (lambda (buffer) (setq symfaces (cdr (assq ,sym ivy-buffer-format-faces))) ;; (message "%s faces --> %s" ,sym symfaces) (if (get-buffer buffer) (let ((buffer_object (get-buffer buffer))) (if (car symfaces) (propertize ,real_buf_expr 'face (car symfaces)) ,real_buf_expr)) (if (cdr symfaces) (propertize ,virt_buf_expr 'face (cdr symfaces)) ,virt_buf_expr)))) ) ) (defvar ivy--buffer-info-fns (list (ivy--buffer-make-info-fn 'buffer-name buffer buffer) (ivy--buffer-make-info-fn 'mode (with-current-buffer buffer_object (format-mode-line mode-name)) '"Virtual") (ivy--buffer-make-info-fn 'dir (shorten-directory (abbreviate-file-name (or (with-current-buffer buffer_object default-directory) "")) ivy-buffer-max-dir-display-length) (shorten-directory (abbreviate-file-name (file-name-directory (cdr (assq buffer ivy--virtual-buffers)))) ivy-buffer-max-dir-display-length)) (ivy--buffer-make-info-fn 'process (if (get-buffer-process buffer_object) "º" "") '"") (ivy--buffer-make-info-fn 'file-name (file-name-nondirectory (or (buffer-file-name buffer_object) "")) '"") )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; User customizable display settings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defvar ivy-buffer-format '( buffer-name "<col>" "|" mode process "<col>" "|" dir file-name "<col>" "|" ) "Describe the display format for `ivy-switch-buffer'. It is a list containing elements that are symbols, cons cells, or strings. - symbol: can be any symbol defined in `ivy--buffer-info-fns' e.g., BUFFER-NAME or MODE - cons cell: (symbol . formatting_string) e.g., (BUFFER-NAME . \"[%s]\") will format BUFFER-NAME inside of square brackets - string: Either \"<col>\" which left-aligns all of the following text before the next \"<col>\", or another string which is inserted literally in the current column. " ) (defvar ivy-buffer-format-faces (list '(buffer-name . (font-lock-builtin-face . font-lock-comment-face)) '(dir . (font-lock-warning-face . nil)) '(process . (font-lock-variable-name-face . nil)) '(mode . (font-lock-type-face . nil)) ) "Describe the text faces (coloring, font, etc) of the symbols. It is a list containing cons cells. Eachs cons cell has the format: '(SYMBOL . (Real buffer face . Virtual buffer face)) For example, the cons cell: '(mode . (font-lock-type-face . nil)) Will apply `font-lock-type-face' to the output of mode function for real buffers, and apply nothing to the output for a virtual buffer. " ) (defvar ivy-buffer-max-dir-display-length 25 "Truncate the display length of a directory to this value." ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Code for formatting entries in minibuffer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun ivy--buffer-display-list () (let ((total_bufs (ivy--buffer-list "" ivy-use-virtual-buffers)) (initial_choice (buffer-name (other-buffer (current-buffer)))) (bufs_info) (bufs_frmted_strs) (template_columns)) (push initial_choice total_bufs) (delete-duplicates total_bufs :from-end t) (ivy-detailed-candidates total_bufs ivy-buffer-format ivy--buffer-info-fns) )) (defun ivy-switch-buffer () "Switch to another buffer." (interactive) (progn (if (not ivy-mode) (call-interactively 'switch-to-buffer) (let ((this-command 'ivy-switch-buffer)) (ivy-read "Switch to buffer: " (ivy--buffer-display-list) :action #'ivy--switch-buffer-action :keymap ivy-switch-buffer-map :sort nil)))) ) (provide 'ivy_buffer_extend) -
frostidaho revised this gist
Oct 10, 2015 . 1 changed file with 82 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,28 +1,92 @@ (defcustom ivy-switch-buffer-show-info '("%-16.40s||%-16.40s||%s%-16s" "buffer-name" "mode" "dir" "file-name") "Docstring `ivy-switch-buffer-show-info'." :type 'list) (defun ivy--insert-minibuffer-wrapper (wrapped_fn text) "Wraps ivy--insert-minibuffer by intercepting the text argument and aligning it to columns. The column delimiter is expected to be @@" (funcall wrapped_fn (with-temp-buffer (insert text) (align-regexp (point-min) (point-max) "\\(\\s-?\\)||" 1 0 t) (while (re-search-backward "||" nil t) (replace-match (propertize "| " 'face (face-at-point)) nil t)) (buffer-string)))) (defvar ivy--buffer-info-fns (make-hash-table :test 'equal) "Docstring `ivy--buffer-info-fns'.") (puthash "buffer-name" 'identity ivy--buffer-info-fns) (puthash "mode" (lambda (x) (if (get-buffer x) (with-current-buffer x (format-mode-line mode-name)))) ivy--buffer-info-fns) (puthash "process" (lambda (x) (get-buffer-process (get-buffer x))) ivy--buffer-info-fns) (puthash "dir" (lambda (x) (if (get-buffer x) (with-current-buffer x (abbreviate-file-name default-directory)))) ivy--buffer-info-fns) (puthash "file-name" (lambda (x) (let ((buf (get-buffer x))) (if (and buf (buffer-file-name buf)) (file-name-nondirectory (buffer-file-name buf))))) ivy--buffer-info-fns) (puthash "number-of-lines" (lambda (x) (if (get-buffer x) (with-current-buffer x (int-to-string (count-lines (point-min) (point-max)))))) ivy--buffer-info-fns) (defvar ivy--buffer-info-frmtfns (make-hash-table :test 'equal) "Docstring `ivy--bufer-info-frmtfns'.") (puthash "buffer-name" 'identity ivy--buffer-info-frmtfns) (puthash "process" (lambda (info_str) (if info_str (propertize "º" 'face '(:foreground "green")) "")) ivy--buffer-info-frmtfns) (puthash "dir" (lambda (info_str) (propertize (or info_str "") 'face 'font-lock-variable-name-face)) ivy--buffer-info-frmtfns) (puthash "number-of-lines" (lambda (info_str) (if info_str (concat "Lines: " info_str) "")) ivy--buffer-info-frmtfns) (puthash "mode" (lambda (mode) (if mode (if (string-match "^Dired" mode) (propertize mode 'face 'dired-directory) (propertize mode 'face 'font-lock-type-face)) "Virtual")) ivy--buffer-info-frmtfns) (defun ivy--buffer-get-frmt (buffer key) "Docstring `ivy--buffer-get-frmt'." (funcall (gethash key ivy--buffer-info-frmtfns (lambda (info_str) (or info_str ""))) (funcall (gethash key ivy--buffer-info-fns) buffer))) (defun ivy--switch-buffer-transformer (buffer) "Add some useful information about BUFFER." (let* ((format_string (car ivy-switch-buffer-show-info)) (keys (cdr ivy-switch-buffer-show-info)) (vals (mapcar (apply-partially 'ivy--buffer-get-frmt buffer) keys))) (if (string-equal buffer ivy--current) (propertize (apply 'format format_string vals) 'face 'ivy-current-match) (apply 'format format_string vals)))) (defun ivy-switch-buffer () "Switch to another buffer." @@ -51,3 +115,4 @@ and aligning it to columns. The column delimiter is expected to be @@" (remove-function (symbol-function 'ivy--insert-minibuffer) 'ivy--insert-minibuffer-wrapper)) (remove-function (symbol-function 'ivy--insert-minibuffer) 'ivy--insert-minibuffer-wrapper)) -
frostidaho created this gist
Oct 8, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ (defun ivy--insert-minibuffer-wrapper (old_fun text) "Wraps ivy--insert-minibuffer by intercepting the text argument and aligning it to columns. The column delimiter is expected to be @@" (funcall old_fun (with-temp-buffer (insert text) (align-regexp (point-min) (point-max) "\\(\\s-*\\)@@" 1 1 t) (while (re-search-backward "@@" nil t) (replace-match "| ")) (buffer-string)))) (defun ivy--switch-buffer-transformer (buffer) "Add some useful information about BUFFER." (let ((buf (get-buffer buffer))) (if buf ;if buffer exists (progn (let ((mode (with-current-buffer buffer (format-mode-line mode-name))) (proc (get-buffer-process buf)) (dir (with-current-buffer buffer (abbreviate-file-name default-directory))) (file-name (if (buffer-file-name buf) (abbreviate-file-name (buffer-file-name buf))))) (format "%s@@%s@@%s" buffer mode (or file-name "")))) (format "%s@@Virtual @@" buffer)))) (defun ivy-switch-buffer () "Switch to another buffer." (interactive) (unwind-protect (progn (advice-add 'ivy--insert-minibuffer :around #'ivy--insert-minibuffer-wrapper) (let* ((store ivy-format-function) (ivy-format-function (lambda (cands) (funcall store (with-ivy-window (mapcar #'ivy--switch-buffer-transformer cands))))) (cands obarray) (pred 'commandp) (sort t)) (if (not ivy-mode) (call-interactively 'switch-to-buffer) (let ((this-command 'ivy-switch-buffer)) (ivy-read "Switch to buffer: " 'internal-complete-buffer :preselect (buffer-name (other-buffer (current-buffer))) :action #'ivy--switch-buffer-action :keymap ivy-switch-buffer-map))))) (remove-function (symbol-function 'ivy--insert-minibuffer) 'ivy--insert-minibuffer-wrapper)) (remove-function (symbol-function 'ivy--insert-minibuffer) 'ivy--insert-minibuffer-wrapper))