completion/ivy: flexible column width for +ivy/tasks

This commit is contained in:
Henrik Lissner 2017-05-14 13:49:11 +02:00
parent 7b1687170f
commit 6f6680489a
2 changed files with 51 additions and 35 deletions

View file

@ -89,7 +89,7 @@
+ [ ] twitter + [ ] twitter
+ [ ] present + [ ] present
** 2.0.3 [6/20] ** 2.0.3 [7/20]
+ [ ] lang/org: fix janky visual line motions (~evil-next-visual-line~, etc) + [ ] lang/org: fix janky visual line motions (~evil-next-visual-line~, etc)
+ [ ] lang/org: fix janky cursor positioning when manipulating org-table cells + [ ] lang/org: fix janky cursor positioning when manipulating org-table cells
+ [ ] lang/org: don't move cursor when realigning org tables + [ ] lang/org: don't move cursor when realigning org tables
@ -98,7 +98,6 @@
+ [ ] core-ui: replace ~winner-mode~ (too unreliable) + [ ] core-ui: replace ~winner-mode~ (too unreliable)
+ [ ] Generalize ~doom-visual-bell~ by basing its background off a face + [ ] Generalize ~doom-visual-bell~ by basing its background off a face
+ [ ] ui/doom-modeline: extend ~media-info~ segment for other media + [ ] ui/doom-modeline: extend ~media-info~ segment for other media
+ [ ] completion/ivy: flexible column width for ~+ivy/tasks~
+ [ ] features/evil: extend ~evil-escape~ to ~evil-multiedit-insert-state~ + [ ] features/evil: extend ~evil-escape~ to ~evil-multiedit-insert-state~
+ [ ] ui/doom: fix nav-flash on evil-multiedit or in eshell/term buffers + [ ] ui/doom: fix nav-flash on evil-multiedit or in eshell/term buffers
+ [ ] Write ~describe-setting~ for ~def-setting!~ definitions. + [ ] Write ~describe-setting~ for ~def-setting!~ definitions.
@ -106,6 +105,7 @@
+ [ ] tools/upload: add ~+upload/open-remote-file~ command to open current file + [ ] tools/upload: add ~+upload/open-remote-file~ command to open current file
on the remote (with TRAMP) on the remote (with TRAMP)
+ [ ] tools/regex: PCRE regex editor, maybe ~re-builder~ & ~pcre2el~? + [ ] tools/regex: PCRE regex editor, maybe ~re-builder~ & ~pcre2el~?
+ [X] completion/ivy: flexible column width for ~+ivy/tasks~
+ [X] lang/emacs-lisp: activate flycheck-mode in non-emacs.d files + [X] lang/emacs-lisp: activate flycheck-mode in non-emacs.d files
+ [X] Fix evil normal-mode keybindings in help-mode popups + [X] Fix evil normal-mode keybindings in help-mode popups
+ [X] Fix help-mode links opening new popups #ui + [X] Fix help-mode links opening new popups #ui

View file

@ -76,49 +76,64 @@ limit to buffers in the current workspace."
(string-match-p "\\`[\n[:blank:]]+\\'" it))) (string-match-p "\\`[\n[:blank:]]+\\'" it)))
(cl-remove-duplicates kill-ring :test 'equal)))) (cl-remove-duplicates kill-ring :test 'equal))))
(defun +ivy--tasks-candidates () ;; TODO refactor ivy task candidate functions (messy!)
(defun +ivy--tasks-candidates (tasks)
"Generate a list of task tags (specified by `+ivy-task-tags') for "Generate a list of task tags (specified by `+ivy-task-tags') for
`+ivy/tasks'." `+ivy/tasks'."
(let ((default-directory (if arg default-directory (doom-project-root))) (let* ((max-type-width (seq-max (mapcar #'length (mapcar #'car +ivy-task-tags))))
case-fold-search) (max-desc-width (seq-max (mapcar #'length (mapcar #'cadr tasks))))
(max-width (max 25 (min (- (window-width) (+ max-type-width 1))
(seq-max (mapcar #'length (mapcar #'cadr tasks))))))
(fmt (format "%%-%ds %%-%ds%%s%%s:%%s" max-type-width max-width)))
(mapcar (lambda (task)
(let ((file (nth 2 task))
(line (nth 3 task))
(type (nth 0 task))
(desc (nth 1 task)))
(format fmt
(propertize type 'face (cdr (assoc type +ivy-task-tags)))
(substring desc 0 (min max-width (length desc)))
(propertize " | " 'face 'font-lock-comment-face)
(propertize (abbreviate-file-name file) 'face 'font-lock-keyword-face)
(propertize line 'face 'font-lock-constant-face))))
tasks)))
(defun +ivy--tasks (target)
(let (case-fold-search)
(mapcar (lambda (x) (mapcar (lambda (x)
(save-match-data (save-match-data
(string-match (concat "^\\([^:]+\\):\\([0-9]+\\):.+\\(" (string-match (concat "^\\([^:]+\\):\\([0-9]+\\):.+\\("
(string-join (mapcar #'car +ivy-task-tags) "\\|") (string-join (mapcar #'car +ivy-task-tags) "\\|")
"\\):?\\s-*\\(.+\\)") "\\):?\\s-*\\(.+\\)")
x) x)
(let* (case-fold-search (let ((file (match-string 1 x))
(file (match-string 1 x)) (line (match-string 2 x))
(line (match-string 2 x)) (type (match-string 3 x))
(type (match-string 3 x)) (desc (match-string 4 x)))
(desc (match-string 4 x))) (list type desc file line))))
(format "%-5s %-90s %s:%s" (split-string
(propertize type 'face (cdr (assoc type +ivy-task-tags))) (shell-command-to-string
(substring desc 0 (min 90 (length desc))) (format "rg -H -S --no-heading --line-number %s %s"
(propertize file 'face 'font-lock-keyword-face) (concat " -- "
(propertize line 'face 'font-lock-constant-face))))) (shell-quote-argument
(split-string (shell-command-to-string (concat "\\s("
(format "rg -H -S --no-heading --line-number %s %s" (string-join (mapcar #'car +ivy-task-tags) "|")
(concat " -- " ")([\\s:]|\\([^)]+\\):?)")))
(shell-quote-argument (concat "\\s(" target))
(string-join (mapcar #'car +ivy-task-tags) "|") "\n" t))))
")([\\s:]|\\([^)]+\\):?)")))
(if arg buffer-file-name ".")))
"\n" t))))
(defun +ivy--tasks-open-action (x) (defun +ivy--tasks-open-action (x)
"Jump to the file and line of the current task." "Jump to the file and line of the current task."
(let* ((spec (split-string (substring x 97) ":")) (let ((location (cadr (split-string x " | ")))
(type (car (split-string x " "))) (type (car (split-string x " "))))
(file (car spec)) (destructuring-bind (file line) (split-string location ":")
(line (string-to-number (cadr spec)))) (with-ivy-window
(with-ivy-window (find-file (expand-file-name file (doom-project-root)))
(find-file (expand-file-name file (doom-project-root))) (goto-char (point-min))
(goto-char (point-min)) (forward-line (1- (string-to-number line)))
(forward-line (1- line)) (search-forward type (line-end-position) t)
(search-forward type (line-end-position) t) (backward-char (length type))
(backward-char (length type)) (recenter)))))
(recenter))))
;;;###autoload ;;;###autoload
(defun +ivy/tasks (&optional arg) (defun +ivy/tasks (&optional arg)
@ -129,7 +144,8 @@ search current file. See `+ivy-task-tags' to customize what this searches for."
(if arg (if arg
(concat "in: " (file-relative-name buffer-file-name)) (concat "in: " (file-relative-name buffer-file-name))
"project")) "project"))
(+ivy--tasks-candidates) (+ivy--tasks-candidates
(+ivy--tasks (if arg buffer-file-name (doom-project-root))))
:action #'+ivy--tasks-open-action :action #'+ivy--tasks-open-action
:caller '+ivy/tasks)) :caller '+ivy/tasks))