Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

3731 lines (3297 sloc) 147.552 kb
;;; helm.el --- Emacs incremental and narrowing framework
;; Copyright (C) 2007 Tamas Patrovics
;; 2008 ~ 2011 rubikitch <>
;; 2011 ~ 2012 Thierry Volpiatto <>
;; This is a fork of anything.el wrote by Tamas Patrovics.
;; Authors of anything.el: Tamas Patrovics
;; rubikitch <>
;; Thierry Volpiatto <>
;; Author: Thierry Volpiatto <>
;; URL:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <>.
;;; Code:
(require 'cl)
;;; Keymap
(defvar helm-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map)
(define-key map (kbd "<down>") 'helm-next-line)
(define-key map (kbd "<up>") 'helm-previous-line)
(define-key map (kbd "C-n") 'helm-next-line)
(define-key map (kbd "C-p") 'helm-previous-line)
(define-key map (kbd "<prior>") 'helm-previous-page)
(define-key map (kbd "<next>") 'helm-next-page)
(define-key map (kbd "M-v") 'helm-previous-page)
(define-key map (kbd "C-v") 'helm-next-page)
(define-key map (kbd "M-<") 'helm-beginning-of-buffer)
(define-key map (kbd "M->") 'helm-end-of-buffer)
(define-key map (kbd "C-g") 'helm-keyboard-quit)
(define-key map (kbd "<right>") 'helm-next-source)
(define-key map (kbd "<left>") 'helm-previous-source)
(define-key map (kbd "<RET>") 'helm-exit-minibuffer)
(define-key map (kbd "C-i") 'helm-select-action)
(define-key map (kbd "C-z") 'helm-execute-persistent-action)
(define-key map (kbd "C-e") 'helm-select-2nd-action-or-end-of-line)
(define-key map (kbd "C-j") 'helm-select-3rd-action)
(define-key map (kbd "C-o") 'helm-next-source)
(define-key map (kbd "C-l") 'helm-recenter-top-bottom-other-window)
(define-key map (kbd "M-C-l") 'helm-reposition-window-other-window)
(define-key map (kbd "C-M-v") 'helm-scroll-other-window)
(define-key map (kbd "M-<next>") 'helm-scroll-other-window)
(define-key map (kbd "C-M-y") 'helm-scroll-other-window-down)
(define-key map (kbd "C-M-S-v") 'helm-scroll-other-window-down)
(define-key map (kbd "M-<prior>") 'helm-scroll-other-window-down)
(define-key map (kbd "<C-M-down>") 'helm-scroll-other-window)
(define-key map (kbd "<C-M-up>") 'helm-scroll-other-window-down)
(define-key map (kbd "C-SPC") 'helm-toggle-visible-mark)
(define-key map (kbd "M-SPC") 'helm-toggle-visible-mark)
(define-key map (kbd "M-[") 'helm-prev-visible-mark)
(define-key map (kbd "M-]") 'helm-next-visible-mark)
(define-key map (kbd "C-k") 'helm-delete-minibuffer-contents)
(define-key map (kbd "C-x C-f") 'helm-quit-and-find-file)
(define-key map (kbd "M-m") 'helm-toggle-all-marks)
(define-key map (kbd "C-w") 'helm-yank-text-at-point)
(define-key map (kbd "C-M-a") 'helm-show-all-in-this-source-only)
(define-key map (kbd "C-r") 'undefined)
(define-key map (kbd "C-s") 'undefined)
(define-key map (kbd "C-t") 'helm-toggle-resplit-window)
(define-key map (kbd "C-}") 'helm-narrow-window)
(define-key map (kbd "C-{") 'helm-enlarge-window)
(define-key map (kbd "C-c -") 'helm-swap-windows)
(define-key map (kbd "C-c C-d") 'helm-delete-current-selection)
(define-key map (kbd "C-c C-y") 'helm-yank-selection)
(define-key map (kbd "C-c C-k") 'helm-kill-selection-and-quit)
(define-key map (kbd "C-c C-f") 'helm-follow-mode)
(define-key map (kbd "C-c C-u") 'helm-force-update)
(define-key map (kbd "M-p") 'previous-history-element)
(define-key map (kbd "M-n") 'next-history-element)
(define-key map (kbd "C-!") 'helm-toggle-suspend-update)
;; Disable `file-cache-minibuffer-complete'.
(define-key map (kbd "<C-tab>") 'undefined)
;; Debugging command
(define-key map "\C-c\C-x\C-d" 'helm-debug-output)
(define-key map "\C-c\C-x\C-m" 'helm-display-all-visible-marks)
(define-key map "\C-c\C-x\C-b" 'helm-send-bug-report-from-helm)
;; Use `describe-mode' key in `global-map'.
(define-key map [f1] nil) ; Allow to eval keymap without errors.
(dolist (k (where-is-internal 'describe-mode global-map))
(define-key map k 'helm-help))
"Keymap for helm.")
(defgroup helm nil
"Open helm."
:prefix "helm-" :group 'convenience)
(defcustom helm-completion-window-scroll-margin 5
" `scroll-margin' to use for helm completion window.
Which see. Set to 0 to disable."
:group 'helm
:type 'integer)
(defcustom helm-display-source-at-screen-top t
"Display candidates at the top of screen.
This happen when using `helm-next-source' and `helm-previous-source'."
:group 'helm
:type 'boolean)
(defcustom helm-candidate-number-limit 100
"Limit candidate number globally.
Do not show more candidates than this limit from individual sources.
It is usually pointless to show hundreds of matches
when the pattern is empty, because it is much simpler to type a
few characters to narrow down the list of potential candidates.
Set it to nil if you don't want this limit."
:group 'helm
:type 'integer)
(defcustom helm-idle-delay 0.3
"Be idle for this many seconds, before updating in delayed sources.
This is useful for sources involving heavy operations
\(like launching external programs\), so that candidates
from the source are not retrieved unnecessarily if the user keeps typing.
It also can be used to declutter the results helm displays,
so that results from certain sources are not shown with every
character typed, only if the user hesitates a bit.
Be sure to know what you are doing when modifying this."
:group 'helm
:type 'float)
(defcustom helm-input-idle-delay 0.3
"Be idle for this many seconds, before updating.
Unlike `helm-idle-delay', it is also effective for non-delayed sources.
If nil, candidates are collected immediately.
Note: If this value is too low compared to `helm-idle-delay',
you may have duplicated sources when using multiples sources.
Safe value is always >= `helm-idle-delay'.
Default settings are equal value for both.
Be sure to know what you are doing when modifying this."
:group 'helm
:type 'float)
(defcustom helm-samewindow nil
"Use current window to show the candidates.
If t then Helm doesn't pop up a new window."
:group 'helm
:type 'boolean)
(defcustom helm-quick-update nil
"If non-nil, suppress displaying sources which are out of screen at first.
They are treated as delayed sources at this input.
This flag makes `helm' a bit faster with many sources."
:group 'helm
:type 'boolean)
(defcustom helm-candidate-separator
"Candidates separator of `multiline' source."
:group 'helm
:type 'string)
(defcustom helm-save-configuration-functions
'(set-window-configuration . current-window-configuration)
"The functions used to restore/save window or frame configurations.
It is a pair where the car is the function to restore window or frame config,
and the cdr is the function to save the window or frame config.
If you want to save and restore frame configuration, set this variable to
'\(set-frame-configuration . current-frame-configuration\)
NOTE: This may not work properly with own-frame minibuffer settings.
Older version saves/restores frame configuration, but the default is changed now
because flickering can occur in some environment."
:group 'helm
:type 'sexp)
(defcustom helm-persistent-action-use-special-display nil
"If non-nil, use `special-display-function' in persistent action."
:group 'helm
:type 'boolean)
(defcustom helm-scroll-amount nil
"Scroll amount when scrolling other window in an helm session.
It is used by `helm-scroll-other-window'
and `helm-scroll-other-window-down'.
If you prefer scrolling line by line, set this value to 1."
:group 'helm
:type 'integer)
(defcustom helm-display-function 'helm-default-display-buffer
"Function to display *helm* buffer.
It is `helm-default-display-buffer' by default,
which affects `helm-samewindow'."
:group 'helm
:type 'symbol)
(defcustom helm-case-fold-search 'smart
"Add 'smart' option to `case-fold-search'.
When smart is enabled, ignore case in the search strings
if pattern contains no uppercase characters.
Otherwise, with a nil or t value, the behavior is same as
Default value is smart, other possible values are nil and t.
NOTE: This have no effect in asynchronous sources, you will
have to implement a similar feature directly in the process.
See in helm-grep.el how it is implemented."
:group 'helm
:type 'symbol)
(defcustom helm-file-name-case-fold-search
(if (memq system-type
'(cygwin windows-nt ms-dos darwin))
"Local setting of `helm-case-fold-search' for reading filenames.
See `helm-case-fold-search' for more info."
:group 'helm
:type 'symbol)
(defcustom helm-reuse-last-window-split-state nil
"Reuse the last state of window split, vertical or horizontal.
That is when you use `helm-toggle-resplit-window' the next helm session
will reuse the same window scheme than the one of last session."
:group 'helm
:type 'boolean)
(defcustom helm-split-window-preferred-function 'helm-split-window-default-fn
"Default function used for splitting window."
:group 'helm
:type 'function)
(defcustom helm-split-window-default-side 'below
"The default side to display `helm-buffer'.
Must be one acceptable arg for `split-window' SIDE,
that is 'below, 'above, 'left, 'right.
A nil value as same effect as 'below.
NOTE: this have no effect if `helm-split-window-preferred-function' is not
`helm-split-window-default-fn' unless this new function handle this."
:group 'helm
:type 'symbol)
(defcustom helm-split-window-in-side-p nil
"Force splitting inside selected window when non--nil.
NOTE: this have no effect if `helm-split-window-preferred-function' is not
`helm-split-window-default-fn' unless this new function handle this."
:group 'helm
:type 'boolean)
;;; Faces
(defface helm-source-header
'((((background dark))
:background "#22083397778B"
:foreground "white"
:underline t)
(((background light))
:background "#abd7f0"
:foreground "black"
:underline t))
"Face for source header in the helm buffer."
:group 'helm)
(defface helm-visible-mark
'((((min-colors 88) (background dark))
(:background "green1" :foreground "black"))
(((background dark))
(:background "green" :foreground "black"))
(((background light)) :background "#d1f5ea")
(((min-colors 88))
(:background "green1"))
(t (:background "green")))
"Face for visible mark."
:group 'helm)
(defface helm-header
'((t (:inherit header-line)))
"Face for header lines in the helm buffer."
:group 'helm)
(defface helm-candidate-number
'((((background dark)) :background "Yellow" :foreground "black")
(((background light)) :background "#faffb5" :foreground "black"))
"Face for candidate number in mode-line." :group 'helm)
(defface helm-selection
'((((background dark)) :background "ForestGreen" :underline t)
(((background light)) :background "#b5ffd1" :underline t))
"Face for currently selected item in the helm buffer."
:group 'helm)
(defface helm-separator
'((((background dark)) :foreground "red")
(((background light)) :foreground "#ffbfb5"))
"Face for multiline source separator."
:group 'helm)
(defface helm-action
'((t (:underline t)))
"Face for action lines in the helm action buffer."
:group 'helm)
;;; Variables.
(defvar helm-type-attributes nil
"It's a list of \(TYPE ATTRIBUTES ...\).
ATTRIBUTES are the same as attributes for `helm-sources'.
TYPE connects the value to the appropriate sources.
Don't set this directly, use instead `define-helm-type-attribute'.
This allows specifying common attributes for several sources.
For example, sources which provide files can specify
common attributes with a `file' type.")
(defvar helm-source-filter nil
"A list of source names to be displayed.
Other sources won't appear in the search results.
If nil then there is no filtering.
See also `helm-set-source-filter'.")
(defvar helm-action-buffer "*helm action*"
"Buffer showing actions.")
(defvar helm-selection-overlay nil
"Overlay used to highlight the currently selected item.")
(defvar helm-async-processes nil
"List of information about asynchronous processes managed by helm.")
(defvar helm-before-initialize-hook nil
"Run before helm initialization.
This hook is run before init functions in `helm-sources'.")
(defvar helm-after-initialize-hook nil
"Run after helm initialization.
Global variables are initialized and the helm buffer is created.
But the helm buffer has no contents.")
(defvar helm-update-hook nil
"Run after the helm buffer was updated according the new input pattern.
This hook is run at the beginning of buffer.
The first candidate is selected after running this hook.
See also `helm-after-update-hook'.")
(defvar helm-after-update-hook nil
"Run after the helm buffer was updated according the new input pattern.
This is very similar to `helm-update-hook' but selection is not moved.
It is useful to select a particular object instead of the first one.")
(defvar helm-cleanup-hook nil
"Run after helm minibuffer is closed.
IOW this hook is executed BEFORE performing action.")
(defvar helm-select-action-hook nil
"Run when opening the action buffer.")
(defvar helm-before-action-hook nil
"Run before executing action.
Contrarily to `helm-cleanup-hook',
this hook run before helm minibuffer is closed
and before performing action.")
(defvar helm-after-action-hook nil
"Run after executing action.")
(defvar helm-after-persistent-action-hook nil
"Run after executing persistent action.")
(defvar helm-move-selection-before-hook nil
"Run before moving selection in `helm-buffer'.")
(defvar helm-move-selection-after-hook nil
"Run after moving selection in `helm-buffer'.")
(defvar helm-restored-variables
"Variables which are restored after `helm' invocation.")
(defvar helm-saved-selection nil
"Value of the currently selected object when the action list is shown.")
(defvar helm-current-prefix-arg nil
"Record `current-prefix-arg' when exiting minibuffer.")
(defvar helm-saved-action nil
"Saved value of the currently selected action by key.")
;; [TODO] Remove.
(defvar helm-last-sources nil
"OBSOLETE!! Sources of previously invoked `helm'.")
(defvar helm-saved-current-source nil
"Value of the current source when the action list is shown.")
(defvar helm-compiled-sources nil
"Compiled version of `helm-sources'.")
(defvar helm-in-persistent-action nil
"Flag whether in persistent-action or not.")
(defvar helm-last-sources-local nil
"Buffer local value of `helm-sources'.")
(defvar helm-last-buffer nil
"`helm-buffer' of previously `helm' session.")
(defvar helm-execute-action-at-once-if-one nil
"Execute default action and exit when only one candidate is remaining.
It is useful for `helm' applications.")
(defvar helm-quit-if-no-candidate nil
"Quit when there is no candidates when non--nil.
This variable accepts a function, which is executed if no candidate.
It is useful for `helm' applications.")
(defvar helm-source-in-each-line-flag nil
"Non-nil means add helm-source text-property in each candidate.
experimental feature.")
(defvaralias 'helm-debug-variables 'helm-debug-forms)
(defvar helm-debug-forms nil
"Forms to show in `helm-debug-output'.
Otherwise all variables started with `helm-' are shown.
It is useful for debug.")
(defvar helm-debug nil
"If non-nil, write log message into *Helm Log* buffer.
If `debug-on-error' is non-nil, write log message regardless of this variable.
It is disabled by default because *Helm Log* grows quickly.")
;;; Internal Variables
(defvar helm-sources nil)
(defvar helm-delayed-init-executed nil)
(defvar helm-buffer "*helm*"
"Buffer showing completions.")
(defvar helm-current-buffer nil
"Current buffer when `helm' is invoked.")
(defvar helm-buffer-file-name nil
"Variable `buffer-file-name' when `helm' is invoked.")
(defvar helm-candidate-cache nil
"Holds the available candidate withing a single helm invocation.")
(defvar helm-pattern ""
"The input pattern used to update the helm buffer.")
(defvar helm-input ""
"The input typed in the candidates panel.")
(defvar helm-input-local nil
"Internal, store locally `helm-pattern' value for later use in `helm-resume'.")
(defvar helm-source-name nil)
(defvar helm-candidate-buffer-alist nil)
(defvar helm-match-hash (make-hash-table :test 'equal))
(defvar helm-cib-hash (make-hash-table :test 'equal))
(defvar helm-tick-hash (make-hash-table :test 'equal))
(defvar helm-issued-errors nil)
(defvar helm-follow-mode nil)
(defvar helm-let-variables nil)
(defvar helm-split-window-state nil)
(defvar helm-selection-point nil)
(defvar helm-alive-p nil)
(defvar helm-visible-mark-overlays nil)
(defconst helm-default-match-functions
(list (lambda (candidate)
(string-match helm-pattern candidate)))
"Default functions to match candidates according to `helm-pattern'.")
(defvar helm-update-blacklist-regexps '("^" "^ *" "$" "!" " " "\\b"
"\\<" "\\>" "\\<_" "\\>_"))
(defvar helm-suspend-update-flag nil)
;; Utility: logging
(defun helm-log (format-string &rest args)
"Log message if `debug-on-error' or `helm-debug' is non-nil.
Messages are written to the *Helm Log* buffer.
Argument FORMAT-STRING is a string to use with `format'.
Use optional arguments ARGS like in `format'."
(when (or debug-on-error helm-debug)
(with-current-buffer (get-buffer-create "*Helm Log*")
(set (make-local-variable 'inhibit-read-only) t)
(goto-char (point-max))
(insert (let ((tm (current-time)))
(format "%s.%06d (%s) %s\n"
(format-time-string "%H:%M:%S" tm)
(nth 2 tm)
(apply #'format (cons format-string args))))))))
(defmacro helm-log-eval (&rest exprs)
"Write each EXPRS evaluation result to the *Helm Log* buffer."
`(helm-log-eval-internal ',exprs))
(defun helm-log-run-hook (hook)
"Run HOOK like `run-hooks' but write these actions to helm log buffer."
(helm-log "executing %s" hook)
(when (boundp hook)
(helm-log-eval (symbol-value hook))
(helm-log-eval (default-value hook)))
(run-hooks hook)
(helm-log "executed %s" hook))
(defun helm-log-eval-internal (exprs)
"Eval EXPRS and write results to helm log buffer."
(dolist (expr exprs)
(condition-case err
(helm-log "%S = %S" expr (eval expr))
(error (helm-log "%S = ERROR: %S" expr err)))))
(defun helm-log-get-current-function ()
"Get function name calling `helm-log'.
The original idea is from `tramp-debug-message'."
(loop with exclude-func-re = "^helm-\\(?:interpret\\|log\\|.*funcall\\)"
for btn from 1 to 40 ;avoid inf-loop
for btf = (second (backtrace-frame btn))
for fn = (if (symbolp btf) (symbol-name btf) "")
if (and (string-match "^helm" fn)
(not (string-match exclude-func-re fn)))
return fn))
(defun helm-log-error (&rest args)
"Accumulate error messages into `helm-issued-errors'.
ARGS are args given to `format'."
(apply 'helm-log (concat "ERROR: " (car args)) (cdr args))
(let ((msg (apply 'format args)))
(unless (member msg helm-issued-errors)
(add-to-list 'helm-issued-errors msg))))
(defvar helm-debug-root-directory nil
"When non--nil, save helm log to `helm-last-log-file'.
Be aware that if you set that, you will end up with a huge directory
of log files, so use that only for debugging purpose.
See `helm-log-save-maybe' for more info.")
;; Internal
(defvar helm-last-log-file nil
"The name of the last helm session log file.")
(defun helm-log-save-maybe ()
"May be save log buffer to `helm-last-log-file'.
If `helm-debug-root-directory' is non--nil and a valid directory,
a directory named 'helm-debug-<date of today>'
will be created there and the log recorded in a file named
at the date and time of today in this directory."
(when (and (stringp helm-debug-root-directory)
(file-directory-p helm-debug-root-directory)
(or debug-on-error helm-debug))
(let ((logdir (expand-file-name (concat "helm-debug-"
(format-time-string "%Y%m%d"))
(make-directory logdir t)
(with-current-buffer (get-buffer-create "*Helm Log*")
(write-region (point-min) (point-max)
(setq helm-last-log-file
(format-time-string "%Y%m%d-%H%M%S")
nil 'silent)
(defun helm-open-last-log ()
"Open helm log file of last helm session.
If `helm-last-log-file' is nil, switch to \"*Helm Log*\" buffer."
(if helm-last-log-file
(view-file helm-last-log-file)
(switch-to-buffer "*Helm Log*")))
(defun helm-print-error-messages ()
"Print error messages in `helm-issued-errors'."
(message "%s" (mapconcat 'identity (reverse helm-issued-errors) "\n")))
;; Programming Tools
(defmacro helm-aif (test-form then-form &rest else-forms)
"Like `if' but set the result of TEST-FORM in a temprary variable called `it'.
THEN-FORM and ELSE-FORMS are then excuted just like in `if'."
(declare (indent 2) (debug t))
`(let ((it ,test-form))
(if it ,then-form ,@else-forms)))
(defun helm-mklist (obj)
"If OBJ is a list \(but not lambda\), return itself.
Otherwise make a list with one element."
(if (and (listp obj) (not (functionp obj)))
(list obj)))
;; Helm API
(defun helm-buffer-get ()
"Return `helm-action-buffer' if shown otherwise `helm-buffer'."
(if (helm-action-window)
(defun helm-window ()
"Window of `helm-buffer'."
(get-buffer-window (helm-buffer-get) 'visible))
(defun helm-action-window ()
"Window of `helm-action-buffer'."
(get-buffer-window helm-action-buffer 'visible))
(defmacro with-helm-window (&rest body)
"Be sure BODY is excuted in the helm window."
(declare (indent 0) (debug t))
`(with-selected-window (helm-window)
(defmacro with-helm-current-buffer (&rest body)
"Eval BODY inside `helm-current-buffer'."
(declare (indent 0) (debug t))
`(with-current-buffer helm-current-buffer
(defmacro with-helm-restore-variables(&rest body)
"Restore `helm-restored-variables' after executing BODY."
(declare (indent 0) (debug t))
`(let ((orig-vars (mapcar (lambda (v)
(cons v (symbol-value v)))
(unwind-protect (progn ,@body)
(loop for (var . value) in orig-vars
do (set var value))
(helm-log "restore variables"))))
(defmacro with-helm-default-directory (directory &rest body)
(declare (indent 2) (debug t))
`(let ((default-directory (file-name-as-directory ,directory)))
(defun* helm-attr (attribute-name
&optional (src (helm-get-current-source)) compute)
"Get the value of ATTRIBUTE-NAME of SRC.
If SRC is omitted, use current source.
If COMPUTE is non--nil compute value of ATTRIBUTE-NAME
with `helm-interpret-value'."
(helm-aif (assq attribute-name src)
(if compute (helm-interpret-value (cdr it)) (cdr it))))
(defun* helm-attr-defined (attribute-name
&optional (src (helm-get-current-source)))
"Return non-nil if ATTRIBUTE-NAME of SRC is defined.
if SRC is omitted, use current source."
(and (assq attribute-name src) t))
(defun* helm-attrset (attribute-name value
(src (helm-get-current-source)))
"Set the value of ATTRIBUTE-NAME of source SRC to VALUE.
If ATTRIBUTE-NAME doesn't exists in source it is created with value VALUE.
If SRC is omitted, use current source."
(helm-aif (assq attribute-name src)
(setcdr it value)
(setcdr src (cons (cons attribute-name value) (cdr src))))
(defun helm-get-attribute-from-source-type (attribute source)
"Get ATTRIBUTE from type attribute of SOURCE."
(when (assq 'type source)
(assq attribute
(assq (helm-attr 'type source)
(defun helm-get-attribute-from-type (attribute type)
arg TYPE is an existing type defined in `helm-type-attributes'."
(assq attribute (assq type helm-type-attributes)))
(defun helm-get-actions-from-type (source)
"Get actions list from type attribute of SOURCE."
(when (assq 'type source)
(helm-get-attribute-from-source-type 'action source)))
(defun helm-inherit-attribute-from-source (attribute source)
(helm-aif (assq attribute source)
(helm-get-attribute-from-source-type attribute source)))
(defun helm-append-at-nth (seq elm index)
"Append ELM at INDEX in SEQ."
(loop for i in seq
for count from 1 collect i
when (= count index)
if (listp elm) append elm
else collect elm))
(defun helm-add-action-to-source (name fn source &optional index)
"Add new action NAME linked to function FN to SOURCE.
Function FN should be a valid function that take one arg i.e candidate,
argument NAME is a string that will appear in action menu
and SOURCE should be an existing helm source already loaded.
If INDEX is specified, action is added in action list at INDEX,
otherwise it is added at end.
This allow user to add a specific action to an existing source
without modifying source code."
(let ((actions (helm-attr 'action source))
(new-action (list (cons name fn))))
(when (symbolp actions)
(setq actions (list (cons "Default action" actions))))
(helm-attrset 'action
(if index
(helm-append-at-nth actions new-action index)
(append actions new-action))
(defun* helm-add-action-to-source-if (name fn source predicate
&optional (index 4) test-only)
"Add new action NAME linked to function FN to SOURCE.
Action is added only if current candidate match PREDICATE.
This function add an entry in the `action-transformer' attribute
of SOURCE (or create one if not found).
Function PREDICATE should take one arg candidate.
Function FN should be a valid function that take one arg i.e candidate,
argument NAME is a string that will appear in action menu
and SOURCE should be an existing helm source already loaded.
If INDEX is specified, action is added in action list at INDEX.
Value of INDEX should be always >=1, default to 4.
This allow user to add a specific `action-tranformer'
to an existing source without modifying source code.
Add the action \"Byte compile file async\" linked to
function 'async-byte-compile-file to source `helm-c-source-find-files'
only when predicate helm-ff-candidates-lisp-p return non--nil:
\(helm-add-action-to-source-if \"Byte compile file async\"
(let* ((actions (helm-attr 'action source))
(action-transformers (helm-attr 'action-transformer source))
(new-action (list (cons name fn)))
(transformer `(lambda (actions candidate)
(cond ((funcall (quote ,predicate) candidate)
actions (quote ,new-action) ,index))
(t actions)))))
(when (symbolp actions)
(helm-attrset 'action (list (cons "Default action" actions)) source))
(when (symbolp action-transformers)
(setq action-transformers (list action-transformers)))
(if test-only ; debug
(delq nil (append (list transformer) action-transformers))
(helm-attrset 'action-transformer
(delq nil (append (list transformer) action-transformers))
(defun helm-set-source-filter (sources)
"Set the value of `helm-source-filter' to SOURCES and update.
This function sets a filter for helm sources and it may be
called while helm is running. It can be used to toggle
displaying of sources dinamically. For example, additional keys
can be bound into `helm-map' to display only the file-related
results if there are too many matches from other sources and
you're after files only:
Shift+F shows only file results from some sources:
\(define-key helm-map \"F\" 'helm-my-show-files-only)
\(defun helm-my-show-files-only ()
(helm-set-source-filter '(\"File Name History\"
\"Files from Current Directory\")))
Shift+A shows all results:
\(define-key helm-map \"A\" 'helm-my-show-all)
\(defun helm-my-show-all ()
(helm-set-source-filter nil))
The -my- part is added to avoid collisions with
existing Helm function names."
(unless (and (listp sources)
(loop for name in sources always (stringp name)))
(error "Invalid data in `helm-set-source-filter': %S" sources))
(let ((cur-disp-sel (with-current-buffer helm-buffer
(helm-get-selection nil t))))
(setq helm-source-filter sources)
(helm-log-eval helm-source-filter)
(helm-update cur-disp-sel)))
(defun helm-set-sources (sources &optional no-init no-update)
"Set SOURCES during `helm' invocation.
If NO-INIT is non-nil, skip executing init functions of SOURCES.
If NO-UPDATE is non-nil, skip executing `helm-update'."
(with-current-buffer helm-buffer
(setq helm-compiled-sources nil
helm-sources sources
helm-last-sources-local sources)
(helm-log-eval helm-compiled-sources helm-sources))
(unless no-init (helm-funcall-foreach 'init))
(unless no-update (helm-update)))
(defvar helm-compile-source-functions
"Functions to compile elements of `helm-sources' (plug-in).")
(defun helm-get-sources ()
"Return compiled `helm-sources', which is memoized.
- type
`helm-type-attributes' are merged in.
- candidates-buffer
candidates, volatile and match attribute are created."
;; action
;; memoized
;; first time
(setq helm-compiled-sources
helm-sources helm-compile-source-functions))
(helm-log-eval helm-compiled-sources)))))
(defun* helm-get-selection (&optional (buffer nil buffer-s)
"Return the currently selected item or nil.
if BUFFER is nil or unspecified, use helm-buffer as default value.
If FORCE-DISPLAY-PART is non-nil, return the display string.
If FORCE-DISPLAY-PART value is 'withprop the display string is returned
with its properties."
(setq buffer (if (and buffer buffer-s) buffer helm-buffer))
(unless (helm-empty-buffer-p buffer)
(with-current-buffer buffer
(let* ((disp-fn (if (eq force-display-part 'withprop)
(or (and (not force-display-part)
(get-text-property (overlay-start
;; It is needed to return properties of DISP in some case,
;; e.g for `helm-confirm-and-exit-minibuffer',
;; so use `buffer-substring' here when 'withprop is specified.
(let ((disp (funcall
(overlay-start helm-selection-overlay)
(1- (overlay-end helm-selection-overlay))))
(source (helm-get-current-source)))
(helm-aif (and (not force-display-part)
(assoc-default 'display-to-real source))
(helm-funcall-with-source source it disp)
(unless (equal selection "")
(helm-log-eval selection)
(defun helm-get-action ()
"Return the associated action for the selected candidate.
It is a function symbol \(sole action\) or list
of \(action-display . function\)."
(unless (helm-empty-buffer-p (helm-buffer-get))
(helm-aif (helm-attr 'action-transformer)
(helm-get-current-source) it
(helm-attr 'action) (helm-get-selection))
(helm-attr 'action))))
(defun helm-get-current-source ()
"Return the source for the current selection."
(declare (special source))
;; `helm-source-name' let-bounded in some function with value of source.
;; Return source from this function. (e.g `helm-funcall-with-source').
(if (and (boundp 'helm-source-name)
(stringp helm-source-name))
(with-current-buffer (helm-buffer-get)
;; This happen only when `helm-source-in-each-line-flag'
;; is non--nil and there is candidates in buffer.
(get-text-property (point) 'helm-source)
;; Return nil when no--candidates.
(block exit
;; This goto-char shouldn't be necessary, but point is moved to
;; point-min somewhere else which shouldn't happen.
(goto-char (overlay-start helm-selection-overlay))
(let* ((header-pos (or (helm-get-previous-header-pos)
(unless header-pos
(return-from exit nil))
(goto-char header-pos)
(loop for source in (helm-get-sources) thereis
(and (equal (assoc-default 'name source) source-name)
(defun helm-buffer-is-modified (buffer)
"Return non-nil when BUFFER is modified since `helm' was invoked."
(let* ((b (get-buffer buffer))
(key (concat (buffer-name b) "/" (helm-attr 'name)))
(source-tick (or (gethash key helm-tick-hash) 0))
(buffer-tick (buffer-chars-modified-tick b))
(modifiedp (/= source-tick buffer-tick)))
(puthash key buffer-tick helm-tick-hash)
(helm-log-eval buffer modifiedp)
(defun helm-current-buffer-is-modified ()
"Check if `helm-current-buffer' is modified since `helm' was invoked."
(helm-buffer-is-modified helm-current-buffer))
(defvar helm-quit nil)
(defun helm-run-after-quit (function &rest args)
"Perform an action after quitting `helm'.
The action is to call FUNCTION with arguments ARGS."
(setq helm-quit t)
(helm-log-eval function args)
(apply 'run-with-timer 0.1 nil function args)
(defun define-helm-type-attribute (type definition &optional doc)
"Register type attribute of TYPE as DEFINITION with DOC.
DOC is displayed in `helm-type-attributes' docstring.
Use this function is better than setting `helm-type-attributes' directly."
(loop for i in definition do
;; without `ignore-errors', error at emacs22
(ignore-errors (setf i (delete nil i))))
(helm-add-type-attribute type definition)
(and doc (helm-document-type-attribute type doc))
(defvaralias 'helm-attributes 'helm-additional-attributes)
(defvar helm-additional-attributes nil
"List of all `helm' attributes.")
(defun helm-document-attribute (attribute short-doc &optional long-doc)
"Register ATTRIBUTE documentation introduced by plug-in.
SHORT-DOC is displayed beside attribute name.
LONG-DOC is displayed below attribute name and short documentation."
(if long-doc
(setq short-doc (concat "(" short-doc ")"))
(setq long-doc short-doc
short-doc ""))
(add-to-list 'helm-additional-attributes attribute t)
(put attribute 'helm-attrdoc
(concat "- " (symbol-name attribute)
" " short-doc "\n\n" long-doc "\n")))
(put 'helm-document-attribute 'lisp-indent-function 2)
(defun helm-interpret-value (value &optional source)
"Interpret VALUE as variable, function or literal and return it.
If VALUE is a function, call it with no arguments and return the value.
If SOURCE compute VALUE for this source.
If VALUE is a variable, return the value.
If VALUE is a symbol, but it is not a function or a variable, cause an error.
Otherwise, return VALUE itself."
(cond ((and source (functionp value))
(helm-funcall-with-source source value))
((functionp value)
(funcall value))
((and (symbolp value) (boundp value))
(symbol-value value))
((symbolp value)
"helm-interpret-value: Symbol must be a function or a variable"))
;; Core: API helper
(defun* helm-empty-buffer-p (&optional (buffer helm-buffer))
"Check if BUFFER have candidates.
Default value for BUFFER is `helm-buffer'."
(zerop (buffer-size (and buffer (get-buffer buffer)))))
(defun helm-empty-source-p ()
"Check if current source contains candidates.
This happen only in certains cases when e.g the last element
of a source is deleted without updating the source."
(or (helm-empty-buffer-p)
(and (helm-end-of-source-p)
(eq (point-at-bol) (point-at-eol))
(forward-line -1)
(defun helm-let-internal (binding bodyfunc)
"Set BINDING to helm buffer-local variables and Evaluate BODYFUNC.
BINDING is a list of (VARNAME . VALUE) pair.
The BINDING list should be created with `helm-parse-keys' when `helm'
is called.
Each KEYS VARNAME of elements of BINDING will be bound locally
to VALUE by `helm-create-helm-buffer'."
(setq helm-let-variables binding)
(funcall bodyfunc)
(setq helm-let-variables nil)))
;; Core: tools
(defun helm-current-line-contents ()
"Current line string without properties."
(buffer-substring-no-properties (point-at-bol) (point-at-eol)))
(defun helm-funcall-with-source (source functions &rest args)
"Call from SOURCE FUNCTIONS list or single function FUNCTIONS with ARGS.
FUNCTIONS can be a symbol or a list of functions.
Return the result of last function call."
(let ((helm-source-name (assoc-default 'name source))
(funs (if (functionp functions) (list functions) functions)))
(helm-log-eval helm-source-name functions args)
(loop with result for fn in funs
do (setq result (apply fn args)) finally return result)))
(defun helm-funcall-foreach (sym)
"Call the function SYM for each source if any."
(dolist (source (helm-get-sources))
(helm-aif (assoc-default sym source)
(helm-funcall-with-source source it))))
(defun helm-normalize-sources (sources)
"If SOURCES is only one source, make a list of one element."
(cond ((or (and sources
(symbolp sources))
(and (listp sources) (assq 'name sources)))
(list sources))
(t helm-sources)))
(defun helm-approximate-candidate-number (&optional in-current-source)
"Return candidates number in `helm-buffer'.
If IN-CURRENT-SOURCE is provided return number of candidates
in the source where point is."
(with-current-buffer helm-buffer
(if (or (helm-empty-buffer-p)
(if in-current-source
(goto-char (helm-get-previous-header-pos))
(goto-char (point-min)))
(forward-line 1)
(let ((count-multi 1))
(if (helm-pos-multiline-p)
(loop while (and (not (if in-current-source
(forward-line 2)
(or (helm-pos-header-line-p) (eobp)))
(search-forward helm-candidate-separator nil t))
do (incf count-multi)
finally return count-multi))
(loop with ln = 0
while (not (if in-current-source
(or (helm-pos-header-line-p) (eobp))
unless (helm-pos-header-line-p)
do (incf ln)
do (forward-line 1) finally return ln))))))))
(defmacro with-helm-quittable (&rest body)
"If an error occur in execution of BODY, quit helm safely."
(declare (indent 0) (debug t))
`(let (inhibit-quit)
(condition-case v
(progn ,@body)
(quit (setq helm-quit t)
(defun helm-compose (arg-lst func-lst)
"Apply arguments specified in ARG-LST with each function of FUNC-LST.
The result of each function will be the new `car' of ARG-LST.
Each function in FUNC-LST must accept (length ARG-LST) arguments.
This function allows easy sequencing of transformer functions.
Where generally, ARG-LST is '(candidates-list source) and FUNC-LST a
list of transformer functions that take one or two arguments depending
we are using 'filtered-candidate-transformer' or 'candidate-transformer'.
\(helm-compose '((1 2 3 4 5 6 7)
'(\"a\" \"b\" \"c\" \"d\" \"e\"))
'((lambda (candidates source)
(loop for i in candidates
when (oddp i) collect i))
(lambda (candidates source)
(loop for i in candidates collect (1+ i)))))
=>(2 4 6 8)."
(dolist (func func-lst)
(setcar arg-lst (apply func arg-lst)))
(car arg-lst))
(defun helm-composed-funcall-with-source (source funcs &rest args)
"With SOURCE apply `helm-funcall-with-source' with each FUNCS and ARGS.
This is used in transformers to modify candidates list."
(if (functionp funcs)
(apply 'helm-funcall-with-source source funcs args)
(apply 'helm-funcall-with-source source
(lambda (&rest args)
(helm-compose args funcs))
;; Core: entry point
(defconst helm-argument-keys
'(:sources :input :prompt :resume
:preselect :buffer :keymap :default :history))
(defun helm (&rest plist)
"Main function to execute helm sources.
Keywords supported:
:sources :input :prompt :resume :preselect
:buffer :keymap :default :history :allow-nest
Extra keywords are supported and can be added, see below.
PLIST is a list like \(:key1 val1 :key2 val2 ...\) or
\(&optional sources input prompt resume
preselect buffer keymap default history\).
Basic keywords are the following:
Temporary value of `helm-sources'. It also accepts a
symbol, interpreted as a variable of an helm source. It
also accepts an alist representing an helm source, which is
detected by \(assq 'name ANY-SOURCES\)
Temporary value of `helm-pattern', ie. initial input of minibuffer.
Prompt other than \"pattern: \".
If t, Resurrect previously instance of `helm'. Skip the initialization.
If 'noresume, this instance of `helm' cannot be resumed.
Initially selected candidate. Specified by exact candidate or a regexp.
`helm-buffer' instead of *helm*.
`helm-map' for current `helm' session.
A default argument that will be inserted in minibuffer \
with \\<minibuffer-local-map>\\[next-history-element].
When nil of not present `thing-at-point' will be used instead.
By default all minibuffer input is pushed to `minibuffer-history',
if an argument HISTORY is provided, input will be pushed to HISTORY.
History element should be a symbol.
Allow running this helm command within a running helm session.
Of course, conventional arguments are supported, the two are same.
\(helm :sources sources :input input :prompt prompt :resume resume
:preselect preselect :buffer buffer :keymap keymap :default default
:history history\)
\(helm sources input prompt resume preselect buffer keymap default history\)
are the same.
However the use of non keyword args is deprecated and should not be used.
Other keywords are interpreted as local variables of this helm session.
The `helm-' prefix can be omitted. For example,
\(helm :sources 'helm-c-source-buffers-list
:buffer \"*buffers*\" :candidate-number-limit 10\)
means starting helm session with `helm-c-source-buffers'
source in *buffers* buffer and set variable `helm-candidate-number-limit'
to 10 as session local variable."
(let ((fn (cond ((or (and helm-alive-p (plist-get plist :allow-nest))
(and helm-alive-p (memq 'allow-nest plist)))
((keywordp (car plist))
(t #'helm-internal))))
(if (and helm-alive-p (eq fn #'helm))
(error "Error: Trying to run helm within a running helm session")
(if (keywordp (car plist))
(helm-parse-keys plist)
(lambda ()
(apply fn (mapcar #'(lambda (key) (plist-get plist key))
(apply fn plist)))))
(defun helm-parse-keys (keys)
"Parse the KEYS arguments of `helm'.
Return only the keys that are not in `helm-argument-keys'.
It is used to set local variables via `helm-let-internal'.
This allow to add arguments that are not part of `helm-argument-keys',
but are valid helm attributes.
i.e :candidate-number-limit will be bound to `helm-candidate-number-limit'
in source."
;; (helm-parse-keys '(:sources ((name . "test")
;; (candidates . (a b c)))
;; :buffer "toto"
;; :candidate-number-limit 4))
;; ==> ((helm-candidate-number-limit . 4))
(loop for (key value) on keys by #'cddr
for symname = (substring (symbol-name key) 1)
for sym = (intern (if (string-match "^helm-" symname)
(concat "helm-" symname)))
unless (memq key helm-argument-keys)
collect (cons sym value)))
;;; Core: entry point helper
(defun helm-internal (&optional
any-sources any-input
any-prompt any-resume
any-preselect any-buffer
any-keymap any-default any-history)
"The internal helm function called by `helm'.
(catch 'exit ; `exit-minibuffer' use this tag on exit.
(helm-log (concat "[Start session] " (make-string 41 ?+)))
(helm-log-eval any-prompt any-preselect
any-buffer any-keymap any-default)
(let ((old-overridding-local-map overriding-local-map))
(condition-case v
(let (;; `helm-source-name' is non-nil
;; when `helm' is invoked by action, reset it.
(helm-buffer (or any-buffer helm-buffer)))
(helm-initialize any-resume any-input any-sources)
(helm-display-buffer helm-buffer)
(helm-log "show prompt")
any-prompt any-input any-preselect
any-resume any-keymap any-default
(when (and any-history (symbolp any-history))
(prog1 (unless helm-quit
(helm-log (concat "[End session] " (make-string 41 ?-)))))
(helm-log (concat "[End session (quit)] " (make-string 34 ?-)))
(helm-log-eval (setq helm-alive-p nil))
(setq overriding-local-map old-overridding-local-map)
;;; Helm resume
(defun helm-resume (arg)
"Resurrect previously invoked `helm'.
Called with a prefix arg, allow choosing among all existing
helm buffers. i.e choose among various helm sessions."
(interactive "P")
(let (any-buffer)
(if arg
(setq any-buffer (helm-resume-select-buffer))
(setq any-buffer helm-last-buffer))
(assert any-buffer nil
"helm-resume: No helm buffers found to resume")
(setq helm-compiled-sources nil)
:sources (or (buffer-local-value
'helm-last-sources-local (get-buffer any-buffer))
:input (buffer-local-value 'helm-input-local (get-buffer any-buffer))
:resume t
:buffer any-buffer)))
(defun helm-resume-p (any-resume)
"Whether current helm session is resumed or not."
(eq any-resume t))
(defun helm-resume-select-buffer ()
"Select an `helm-buffer' in `helm-buffers' list to resume an helm session.
Return nil if no `helm-buffer' found."
(when helm-buffers
(or (helm :sources '(((name . "Resume helm buffer")
(candidates . helm-buffers)
(action . identity)))
:resume 'noresume
:buffer "*helm resume*")
(defun helm-other-buffer (any-sources any-buffer)
"Simplified interface of `helm' with other `helm-buffer'.
Call `helm' with only ANY-SOURCES and ANY-BUFFER as args."
(helm :sources any-sources :buffer any-buffer))
(defun helm-nest (&rest same-as-helm)
"Allow calling `helm' whithin a running helm session."
(let ((orig-helm-current-buffer helm-current-buffer)
(orig-helm-buffer helm-buffer)
(let (helm-current-position
(helm-buffer (or (getf same-as-helm :buffer)
(nth 5 same-as-helm)
(helm-samewindow t)
(enable-recursive-minibuffers t))
(apply #'helm same-as-helm))
(with-current-buffer orig-helm-buffer
(setq helm-alive-p t) ; Nested session set this to nil on exit.
(setq helm-buffer (current-buffer))
(helm-initialize-overlays helm-buffer)
(helm-mark-current-line t)
(setq helm-last-frame-or-window-configuration
(setq cursor-type t)
(setq helm-current-buffer orig-helm-current-buffer))))))
;;; Initialize
(defvar helm-buffers nil
"All of `helm-buffer' in most recently used order.")
(defun helm-initialize (any-resume any-input any-sources)
"Start initialization of `helm' session.
(helm-log "start initialization: any-resume=%S any-input=%S"
any-resume any-input)
(helm-frame-or-window-configuration 'save)
(setq helm-sources (helm-normalize-sources any-sources))
(helm-log "sources = %S" helm-sources)
(helm-current-position 'save)
(if (helm-resume-p any-resume)
(helm-initialize-overlays (helm-buffer-get))
(setq helm-alive-p t)
(unless (eq any-resume 'noresume)
(helm-recent-push helm-buffer 'helm-buffers)
(setq helm-last-buffer helm-buffer))
(when any-input (setq helm-input any-input helm-pattern any-input))
(and (helm-resume-p any-resume) (helm-funcall-foreach 'resume))
(helm-log "end initialization"))
(defun helm-restore-position-on-quit ()
"Restore position in `helm-current-buffer' when quitting."
(helm-current-position 'restore))
(defun helm-recent-push (elt list-var)
"Add ELT to the value of LIST-VAR as most recently used value."
(let ((m (member elt (symbol-value list-var))))
(and m (set list-var (delq (car m) (symbol-value list-var))))
(push elt (symbol-value list-var))))
;;; Core: Accessors
(defvar helm-current-position nil
"Cons of \(point . window-start\) when `helm' is invoked.
It is needed to restore position in `helm-current-buffer'
when `helm' is keyboard-quitted.")
(defun helm-current-position (save-or-restore)
"Restore or save current position in `helm-current-buffer'.
Argument SAVE-OR-RESTORE is one of save or restore."
(case save-or-restore
(helm-log "Save position at %S" (cons (point) (window-start)))
(setq helm-current-position (cons (point) (window-start))))
(helm-log "Restore position at %S in buffer %s"
(buffer-name (current-buffer)))
(goto-char (car helm-current-position))
;; Fix this position with the NOFORCE arg of `set-window-start'
;; otherwise, if there is some other buffer than `helm-current-buffer'
;; one, position will be lost.
(set-window-start (selected-window) (cdr helm-current-position) t))))
;; Internal.
(defvar helm-last-frame-or-window-configuration nil
"Used to store window or frame configuration when helm start.")
(defun helm-frame-or-window-configuration (save-or-restore)
"Save or restore last frame or window configuration.
Possible value of SAVE-OR-RESTORE are 'save and 'restore.
window or frame configuration is saved/restored according to values of
(helm-log-eval helm-save-configuration-functions)
(case save-or-restore
(save (setq helm-last-frame-or-window-configuration
(funcall (cdr helm-save-configuration-functions))))
(restore (funcall (car helm-save-configuration-functions)
;; Restore frame focus.
(let ((frame
(and (listp helm-last-frame-or-window-configuration)
(caadr helm-last-frame-or-window-configuration))))
;; If `helm-save-configuration-functions' are window functions
;; frame should be nil, use current frame.
(unless (framep frame)
;; This is needed for minibuffer own-frame config
;; when recursive minibuffers are in use.
;; e.g M-: + helm-minibuffer-history.
(setq frame (if (minibufferp helm-current-buffer)
(select-frame-set-input-focus frame)))))
(defun helm-split-window-default-fn (window)
(let (split-width-threshold)
(if (and (fboundp 'window-in-direction)
;; Don't try to split when starting in a minibuffer
;; e.g M-: and try to use helm-show-kill-ring.
(not (minibufferp helm-current-buffer)))
(if (or (one-window-p)
(selected-window) nil helm-split-window-default-side)
;; If more than one window reuse one of them.
(case helm-split-window-default-side
(left (or (window-in-direction 'left)
(window-in-direction 'above)
(above (or (window-in-direction 'above)
(window-in-direction 'left)
(right (or (window-in-direction 'right)
(window-in-direction 'below)
(below (or (window-in-direction 'below)
(window-in-direction 'right)
(t (or (window-next-sibling) (selected-window)))))
(split-window-sensibly window))))
;; Core: Display *helm* buffer
(defun helm-display-buffer (buffer)
"Display BUFFER.
The function used to display `helm-buffer'."
(let (pop-up-frames
(if (and (not helm-samewindow)
(cond ((and (eq helm-split-window-state 'horizontal)
(eq helm-split-window-default-side 'left))
((and (eq helm-split-window-state 'horizontal)
(eq helm-split-window-default-side 'right))
((and (eq helm-split-window-state 'horizontal)
(eq helm-split-window-default-side 'above))
((and (eq helm-split-window-state 'horizontal)
(eq helm-split-window-default-side 'below))
(t helm-split-window-default-side))
(funcall (with-current-buffer buffer helm-display-function) buffer)))
(defun helm-default-display-buffer (buffer)
"Default function to display `helm-buffer' BUFFER.
It use `switch-to-buffer' or `pop-to-buffer' depending of value of
(if helm-samewindow
(switch-to-buffer buffer)
(pop-to-buffer buffer)))
;; Core: initialize
(defun helm-initial-setup ()
"Initialize helm settings and set up the helm buffer."
(helm-log-run-hook 'helm-before-initialize-hook)
(setq helm-current-prefix-arg nil)
(setq helm-suspend-update-flag nil)
(setq helm-delayed-init-executed nil)
(setq helm-current-buffer
(if (minibuffer-window-active-p (minibuffer-window))
;; If minibuffer is active be sure to use it's buffer
;; as `helm-current-buffer'.
(window-buffer (active-minibuffer-window))
(setq helm-buffer-file-name buffer-file-name)
(setq helm-issued-errors nil)
(setq helm-compiled-sources nil)
(setq helm-saved-current-source nil)
(unless (and helm-reuse-last-window-split-state
(if (or (not split-width-threshold)
(and (integerp split-width-threshold)
(>= split-width-threshold (+ (frame-width) 4))))
(setq helm-split-window-state 'vertical)
(setq helm-split-window-state 'horizontal)))
;; Call the init function for sources where appropriate
(helm-funcall-foreach 'init)
(setq helm-pattern "")
(setq helm-input "")
(setq helm-candidate-cache nil)
(setq helm-last-sources helm-sources)
(helm-log-run-hook 'helm-after-initialize-hook))
(defun helm-read-pattern-maybe (any-prompt any-input
any-preselect any-resume any-keymap
any-default any-history)
"Read pattern with prompt ANY-PROMPT and initial input ANY-INPUT.
(if (and (helm-resume-p any-resume) (not (helm-empty-buffer-p)))
(helm-mark-current-line t)
(helm-update any-preselect))
(with-current-buffer (helm-buffer-get)
(let* ((src (helm-get-current-source))
(src-keymap (assoc-default 'keymap src))
(hist (or any-history
;; Needed for resuming.
(assoc-default 'history src)))
(timer nil))
;; Startup with the first keymap found either in current source
;; or helm arg, otherwise use global value of `helm-map'.
;; This map will be used as a `minibuffer-local-map'.
;; Maybe it will be overriden when changing source
;; by `helm-maybe-update-keymap'.
;; Note that helm-map have been made buffer-local
;; in `helm-create-helm-buffer'.
(setq helm-map (or src-keymap any-keymap helm-map))
(helm-log-eval (helm-approximate-candidate-number)
(cond ((and helm-execute-action-at-once-if-one
(= (helm-approximate-candidate-number) 1))
((and helm-quit-if-no-candidate
(= (helm-approximate-candidate-number) 0))
(setq helm-quit t)
(and (functionp helm-quit-if-no-candidate)
(funcall helm-quit-if-no-candidate)))
(let ((tap (or any-default
(thing-at-point 'symbol)))))
#'(lambda ()
(setq timer (run-with-idle-timer
helm-input-idle-delay 'repeat
#'(lambda ()
;; Stop updating when in persistent action
;; or when `helm-suspend-update-flag' is
;; non--nil.
(unless (or helm-in-persistent-action
(read-from-minibuffer (or any-prompt "pattern: ")
any-input helm-map
nil hist tap t))
(when timer (cancel-timer timer) (setq timer nil)))))))))
(defun helm-toggle-suspend-update ()
"Enable or disable update of display in helm.
This can be useful for e.g writing quietly a complex regexp."
(when (setq helm-suspend-update-flag (not helm-suspend-update-flag))
(setq helm-pattern ""))
(message (if helm-suspend-update-flag
"Helm update suspended!"
"Helm update reenabled!")))
(declare-function tramp-read-passwd "tramp" (proc &optional prompt))
(defadvice tramp-read-passwd (around disable-helm-update activate)
;; Suspend update when prompting for a tramp password.
(setq helm-suspend-update-flag t)
(setq helm-suspend-update-flag nil)))
(defun helm-maybe-update-keymap ()
"Handle differents keymaps in multiples sources.
This function is meant to be run in `helm-move-selection-after-hook'.
It will override `helm-map' with the keymap attribute of current source
if some when multiples sources are present."
(let* ((source (helm-get-current-source))
(kmap (and (listp source) ; Check if source is empty.
(assoc-default 'keymap source))))
(when kmap (setq overriding-local-map kmap)))))
(add-hook 'helm-move-selection-after-hook 'helm-maybe-update-keymap)
(defun helm-create-helm-buffer ()
"Create and setup `helm-buffer'."
(with-current-buffer (get-buffer-create helm-buffer)
(helm-log "kill local variables: %S" (buffer-local-variables))
(set (make-local-variable 'inhibit-read-only) t)
(set (make-local-variable 'helm-map) helm-map)
(set (make-local-variable 'helm-last-sources-local) helm-sources)
(set (make-local-variable 'helm-follow-mode) nil)
(set (make-local-variable 'helm-display-function) helm-display-function)
(set (make-local-variable 'helm-selection-point) nil)
(set (make-local-variable 'scroll-margin) helm-completion-window-scroll-margin)
(helm-log-eval helm-display-function helm-let-variables)
(loop for (var . val) in helm-let-variables
do (set (make-local-variable var) val))
(setq cursor-type nil)
(setq mode-name "Helm"))
(helm-initialize-overlays helm-buffer)
(get-buffer helm-buffer))
(defun helm-initialize-overlays (buffer)
"Initialize helm overlays in BUFFER."
(helm-log "overlay setup")
(if helm-selection-overlay
;; make sure the overlay belongs to the helm buffer if
;; it's newly created
(move-overlay helm-selection-overlay (point-min) (point-min)
(get-buffer buffer))
(setq helm-selection-overlay
(make-overlay (point-min) (point-min) (get-buffer buffer)))
(overlay-put helm-selection-overlay 'face 'helm-selection)))
;; Core: clean up
(defun helm-cleanup ()
"Clean up the mess when helm exit or quit."
(helm-log "start cleanup")
(with-current-buffer helm-buffer
;; If we end up for any reasons (error etc...)
;; with an helm-buffer staying around (visible),
;; we will have no cursor in this buffer when switching to it.
(setq cursor-type t)
;; Call burry-buffer whithout arg
;; to be sure helm-buffer is removed from window.
;; Be sure we call this from helm-buffer.
(helm-funcall-foreach 'cleanup))
(helm-log-run-hook 'helm-cleanup-hook)
(helm-frame-or-window-configuration 'restore)
(setq helm-alive-p nil)
;; This is needed in some cases where last input
;; is yielded infinitely in minibuffer after helm session.
(defun helm-clean-up-minibuffer ()
"Remove contents of minibuffer."
(let ((miniwin (minibuffer-window)))
;; Clean only current minibuffer used by helm.
;; i.e The precedent one is active.
(unless (minibuffer-window-active-p miniwin)
(with-current-buffer (window-buffer miniwin)
;; Core: input handling
(defun helm-check-minibuffer-input ()
"Check minibuffer content."
(with-selected-window (or (active-minibuffer-window)
(helm-check-new-input (minibuffer-contents)))))
(defun helm-check-new-input (input)
"Check INPUT string and update the helm buffer if necessary."
(unless (equal input helm-pattern)
(setq helm-pattern input)
(unless (helm-action-window)
(setq helm-input helm-pattern))
(helm-log-eval helm-pattern helm-input)
;; Core: source compiler
(defvar helm-compile-source-functions-default helm-compile-source-functions
"Plug-ins this file provides.")
(defun helm-compile-sources (sources funcs)
"Compile SOURCES with FUNCS.
See `helm-compile-source-functions'.
Helm plug-ins are realized by this function."
(lambda (source)
(loop with source = (if (listp source) source (symbol-value source))
for f in funcs
do (setq source (funcall f source))
finally (return source)))
;; Core: all candidates
(defun helm-process-delayed-init (source)
"Initialize delayed SOURCE."
(let ((name (assoc-default 'name source)))
(unless (member name helm-delayed-init-executed)
(helm-aif (assoc-default 'delayed-init source)
(with-current-buffer helm-current-buffer
(helm-funcall-with-source source it)
(dolist (f (if (functionp it) (list it) it))
(add-to-list 'helm-delayed-init-executed name)))))))
(defun helm-get-candidates (source)
"Retrieve and return the list of candidates from SOURCE."
(helm-process-delayed-init source)
(let* (inhibit-quit
(candidate-fn (assoc-default 'candidates source))
(candidate-proc (assoc-default 'candidates-process source))
(type-error (lambda ()
(error (concat "Candidates must either be a function, "
" a variable or a list: %s")
(candidates (condition-case err
(if candidate-proc
(helm-interpret-value candidate-proc source)
(helm-interpret-value candidate-fn source)))
(error (funcall type-error)))))
(when (and (processp candidates) (not candidate-proc))
(warn "Candidates function `%s' should be called in a `candidates-process' attribute"
(cond ((processp candidates) candidates)
((listp candidates)
(let* ((transformed-lst (while-no-input
candidates source))))
;; `while-no-input' may return t or nil if user enter
;; input or C-g, so return the list or nil but never t.
(and (consp transformed-lst) transformed-lst)))
(t (funcall type-error)))))
(defun helm-get-cached-candidates (source)
"Return the cached value of candidates for SOURCE.
Cache the candidates if there is not yet a cached value."
(let* ((name (assoc-default 'name source))
(candidate-cache (assoc name helm-candidate-cache)))
(cond (candidate-cache
(helm-log "use cached candidates")
(cdr candidate-cache))
(helm-log "calculate candidates")
(let ((candidates (helm-get-candidates source)))
(cond ((processp candidates)
(push (cons candidates
(append source
(list (cons 'item-count 0)
(cons 'incomplete-line ""))))
(set-process-filter candidates 'helm-output-filter)
(setq candidates nil))
((not (assoc 'volatile source))
(setq candidate-cache (cons name candidates))
(push candidate-cache helm-candidate-cache)))
;;; Core: candidate transformers
(defun helm-transform-mapcar (function args)
"`mapcar' for candidate-transformer.
ARGS is (cand1 cand2 ...) or ((disp1 . real1) (disp2 . real2) ...)
\(helm-transform-mapcar 'upcase '(\"foo\" \"bar\"))
=> (\"FOO\" \"BAR\")
\(helm-transform-mapcar 'upcase '((\"1st\" . \"foo\") (\"2nd\" . \"bar\")))
=> ((\"1st\" . \"FOO\") (\"2nd\" . \"BAR\"))
(loop for arg in args
if (consp arg)
collect (cons (car arg) (funcall function (cdr arg)))
collect (funcall function arg)))
(defun helm-process-candidate-transformer (candidates source)
"Execute `candidate-transformer' function(s) on CANDIDATES in SOURCE."
(helm-aif (assoc-default 'candidate-transformer source)
(helm-composed-funcall-with-source source it candidates)
(defun helm-process-filtered-candidate-transformer (candidates source)
"Execute `filtered-candidate-transformer' function(s) on CANDIDATES in SOURCE."
(helm-aif (assoc-default 'filtered-candidate-transformer source)
(helm-composed-funcall-with-source source it candidates source)
(defun helm-process-filtered-candidate-transformer-maybe
(candidates source process-p)
"Execute `filtered-candidate-transformer' function(s) on CANDIDATES in SOURCE.
When PROCESS-P is non-nil execute `filtered-candidate-transformer'
functions if some, otherwise return CANDIDATES."
(if process-p
;; When no filter return CANDIDATES unmodified.
(helm-process-filtered-candidate-transformer candidates source)
(defun helm-process-real-to-display (candidates source)
"Execute real-to-display function on all CANDIDATES of SOURCE."
(helm-aif (assoc-default 'real-to-display source)
(setq candidates (helm-funcall-with-source
source 'mapcar
(lambda (cand_)
(if (consp cand_)
;; override DISPLAY from candidate-transformer
(cons (funcall it (cdr cand_)) (cdr cand_))
(cons (funcall it cand_) cand_)))
(defun helm-transform-candidates (candidates source &optional process-p)
"Transform CANDIDATES of SOURCE according to candidate transformers.
When PROCESS-P is non-nil execute the `filtered-candidate-transformer' functions
otherwise only the `candidate-transformer' functions are processed.
When attribute `real-to-display' is present, execute its function on all maybe
filtered CANDIDATES."
(helm-process-candidate-transformer candidates source) source process-p)
;; Core: narrowing candidates
(defun helm-candidate-number-limit (source)
"Apply candidate-number-limit attribute value.
This overhide variable `helm-candidate-number-limit'.
If \(candidate-number-limit\) is in SOURCE, show all candidates in SOURCE.
If \(candidate-number-limit . 123\) is in SOURCE limit candidate to 123."
(helm-aif (assq 'candidate-number-limit source)
(or (cdr it) 99999999)
(or helm-candidate-number-limit 99999999)))
(defun helm-candidate-get-display (candidate)
"Get searched display part from CANDIDATE.
CANDIDATE is a string, a symbol, or \(DISPLAY . REAL\) cons cell."
(format "%s" (or (car-safe candidate) candidate)))
(defun helm-process-pattern-transformer (pattern source)
"Execute pattern-transformer attribute PATTERN function in SOURCE."
(helm-aif (assoc-default 'pattern-transformer source)
(helm-composed-funcall-with-source source it pattern)
(defun helm-match-functions (source)
(let ((matchfns (or (assoc-default 'match source)
(assoc-default 'match-strict source)
(if (listp matchfns) matchfns (list matchfns))))
(defmacro helm-accumulate-candidates-internal (cand newmatches
hash item-count limit)
"Internal, add CAND into NEWMATCHES.
Argument ITEM-COUNT count the matches.
if ITEM-COUNT reaches LIMIT, exit from inner loop."
`(unless (gethash ,cand ,hash)
(puthash ,cand t ,hash)
(push ,cand ,newmatches)
(incf ,item-count)
(when (= ,item-count ,limit) (return))))
(defun helm-take-first-elements (seq n)
"Return the N first element of SEQ if SEQ is longer than N.
It is used to narrow down list of candidates to `helm-candidate-number-limit'."
(if (> (length seq) n) (subseq seq 0 n) seq))
(defun* helm-set-case-fold-search (&optional (pattern helm-pattern))
"Used to set the value of `case-fold-search' in helm.
Return t or nil depending of value of `helm-case-fold-search'
and `helm-pattern'."
(let ((helm-case-fold-search
(helm-aif (assq 'case-fold-search (helm-get-current-source))
(cdr it)
(case helm-case-fold-search
(smart (let ((case-fold-search nil))
(if (string-match "[A-Z]" pattern) nil t)))
(t helm-case-fold-search))))
(defun helm-match-from-candidates (cands matchfns limit)
(let (matches)
(condition-case nil
(let ((item-count 0)
(case-fold-search (helm-set-case-fold-search))
(clrhash helm-match-hash)
(dolist (match matchfns)
(let (newmatches)
(dolist (candidate cands)
(when (funcall match (helm-candidate-get-display candidate))
candidate newmatches helm-match-hash item-count limit)))
(setq matches (append matches (reverse newmatches)))
;; Don't recompute matches already found by this match function
;; with the next match function.
(setq cands (loop for i in cands unless (member i matches) collect i)))))
(invalid-regexp (setq matches nil)))
(defun helm-compute-matches (source)
"Compute matched results from SOURCE according to its settings."
(condition-case nil
(helm-compute-matches-internal source)
(error nil)))
(defun helm-compute-matches-internal (source)
(let ((matchfns (helm-match-functions source))
(helm-source-name (assoc-default 'name source))
(limit (helm-candidate-number-limit source))
(helm-pattern (helm-process-pattern-transformer
helm-pattern source)))
(if (or (equal helm-pattern "") (equal matchfns '(identity)))
(helm-get-cached-candidates source) limit)
(helm-get-cached-candidates source) matchfns limit))
(defun helm-process-source (source)
"Display matched results from SOURCE according to its settings."
(helm-log-eval (assoc-default 'name source))
(if (assq 'direct-insert-match source) ;experimental
(helm-process-source--direct-insert-match source)
(let ((matches (helm-compute-matches source)))
(when matches
(helm-insert-header-from-source source)
(if (not (assq 'multiline source))
(mapc #'(lambda (m)
(helm-insert-match m 'insert source))
(let ((start (point)) separate)
(dolist (match matches)
(if separate
(setq separate t))
(helm-insert-match match 'insert source))
(put-text-property start (point) 'helm-multiline t)))))))
(defun helm-process-source--direct-insert-match (source)
"[EXPERIMENTAL] Insert candidates from `helm-candidate-buffer' in SOURCE."
(helm-log-eval (assoc-default 'name source))
(let ((helm-source-name (assoc-default 'name source))
(funcall (assoc-default 'candidates source))
(setq content-buf (helm-candidate-buffer))
(unless (helm-empty-buffer-p content-buf)
(helm-insert-header-from-source source)
(insert-buffer-substring content-buf))))
(defun helm-process-delayed-sources (delayed-sources &optional preselect)
"Process helm DELAYED-SOURCES.
Move selection to string or regexp PRESELECT if non--nil.
This function is called in `helm-process-delayed-sources-timer'
when emacs is idle for `helm-idle-delay'."
(helm-log-eval (mapcar (lambda (s)
(assoc-default 'name s))
(with-current-buffer helm-buffer
(goto-char (point-max))
(mapc 'helm-process-source delayed-sources)
(when (and (not (helm-empty-buffer-p))
;; No selection yet.
(= (overlay-start helm-selection-overlay)
(overlay-end helm-selection-overlay)))
(helm-update-move-first-line 'without-hook)))
(when preselect (helm-preselect preselect))
(goto-char (point-min))
(helm-log-run-hook 'helm-update-hook))
(helm-log-run-hook 'helm-after-update-hook))))
;;; Core: helm-update
(defun helm-update (&optional preselect)
"Update candidates list in `helm-buffer' according to `helm-pattern'.
Argument PRESELECT is a string or regexp used to move selection to a particular
place once updating is done. It should be used on single source because search
is done on whole `helm-buffer' and not on current source."
(helm-log "Start updating")
;; When persistent action have been called
;; we have two windows even with `helm-samewindow'.
;; So go back to one window when updating if `helm-samewindow'
;; is non--nil.
(when helm-samewindow (delete-other-windows)))
(with-current-buffer (helm-buffer-get)
(set (make-local-variable 'helm-input-local) helm-pattern)
(let (delayed-sources
(unwind-protect ; Process normal sources and store delayed one's.
(loop for source in (remove-if-not 'helm-update-source-p
if (helm-delayed-source-p source)
collect source into ds
else collect source into ns and do
(helm-process-source source)
finally (setq delayed-sources ds
normal-sources ns))
(mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
(cond ((and preselect delayed-sources normal-sources)
;; Preselection run here when there is
;; normal AND delayed sources.
(helm-log "Update preselect candidate %s" preselect)
(helm-preselect preselect))
(delayed-sources ; Preselection and hooks will run later.
(helm-update-move-first-line 'without-hook))
(t ; No delayed sources, run the hooks now.
(helm-log-run-hook 'helm-after-update-hook)
(when preselect
(helm-log "Update preselect candidate %s" preselect)
(helm-preselect preselect))))
(when delayed-sources
;; Allow giving a value to `delayed' attr from inside source.
;; Retain the biggest value (the slower) found in DELAYED-SOURCES.
(let ((helm-idle-delay (loop with delay = helm-idle-delay
for s in delayed-sources
for d = (assoc-default 'delayed s)
when d do (setq delay (max delay d))
finally return delay)))
;; Be sure helm-idle-delay is >
;; to helm-input-idle-delay
;; otherwise use value of helm-input-idle-delay
;; or 0.1 if == to 0.
(max helm-idle-delay helm-input-idle-delay 0.1) nil
'helm-process-delayed-sources delayed-sources preselect)))
(helm-log "end update")))))
(defun helm-update-source-p (source)
"Wheter SOURCE need updating or not."
(and (or (not helm-source-filter)
(member (assoc-default 'name source) helm-source-filter))
(>= (length helm-pattern)
(helm-aif (assoc 'requires-pattern source)
(or (cdr it) 1)
;; These incomplete regexps hang helm forever
;; so defer update.
(not (member helm-pattern helm-update-blacklist-regexps))))
(defun helm-delayed-source-p (source)
"Wheter SOURCE is a delayed source or not."
(or (assoc 'delayed source)
(and helm-quick-update
(< (window-height (get-buffer-window (current-buffer)))
(line-number-at-pos (point-max))))))
(defun helm-update-move-first-line (&optional without-hook)
"Goto first line of `helm-buffer'."
(goto-char (point-min))
(unless without-hook
(save-excursion (helm-log-run-hook 'helm-update-hook)))
(defun helm-force-update (&optional preselect)
"Force recalculation and update of candidates.
If arg PRESELECT, a candidate to preselect, is provided,
It will be preselected by `helm-update', otherwise the current candidate
will be preselected is available.
If current source has `update' attribute, a function without argument,
call it before update."
(let ((source (helm-get-current-source))
(selection (helm-get-selection nil t)))
(when source
(mapc 'helm-force-update--reinit
(helm-update preselect)
;; If preselect arg exists, `helm-update' should
;; have moved to selection, otherwise do it now.
(unless preselect
(helm-keep-selection (assoc-default 'name source) selection))
(with-helm-window (recenter))))
(defun helm-force-update--reinit (source)
"Reinit SOURCE by calling his update and/or init functions."
(helm-aif (helm-funcall-with-source
source 'helm-candidate-buffer)
(kill-buffer it))
(dolist (attr '(update init))
(helm-aif (assoc-default attr source)
(helm-funcall-with-source source it)))
(helm-remove-candidate-cache source))
(defun helm-keep-selection (source selection)
"Switch to SOURCE and goto SELECTION."
(when (and source selection)
(helm-goto-source source)
(forward-char -1)
(if (search-forward selection nil t)
(forward-line 0)
(goto-char (point-min))
(forward-line 1))
(defun helm-remove-candidate-cache (source)
"Remove SOURCE from `helm-candidate-cache'."
(setq helm-candidate-cache
(delete (assoc (assoc-default 'name source)
(defun helm-insert-match (match insert-function source)
"Insert MATCH into `helm-buffer' with INSERT-FUNCTION for SOURCE.
If MATCH is a list then insert the string intended to appear on the display
and store the real value in a text property."
(let ((start (point-at-bol (point)))
(dispvalue (or (car-safe match) match))
(realvalue (cdr-safe match)))
(setq dispvalue
(cond ((symbolp dispvalue) (symbol-name dispvalue))
((numberp dispvalue) (number-to-string dispvalue))
(t dispvalue)))
(when (stringp dispvalue)
(funcall insert-function dispvalue)
;; Some sources with candidates-in-buffer have already added
;; 'helm-realvalue property when creating candidate buffer.
(unless (get-text-property start 'helm-realvalue)
(and realvalue
(put-text-property start (point-at-eol)
'helm-realvalue realvalue)))
(when helm-source-in-each-line-flag
(put-text-property start (point-at-eol) 'helm-source source))
(funcall insert-function "\n"))))
(defun helm-insert-header-from-source (source)
"Insert SOURCE name in `helm-buffer' header.
Maybe insert by overlay additional info after source name if SOURCE have
header-name attribute."
(let ((name (assoc-default 'name source)))
(helm-aif (assoc-default 'header-name source)
(helm-funcall-with-source source it name)))))
(defun helm-insert-header (name &optional display-string)
"Insert header of source NAME into the helm buffer.
If DISPLAY-STRING is non--nil and a string, display this additional info
after the source name by overlay."
(unless (bobp)
(let ((start (point)))
(insert "\n")
(put-text-property start (point) 'helm-header-separator t)))
(let ((start (point)))
(insert name)
(put-text-property (point-at-bol)
(point-at-eol) 'helm-header t)
(when display-string
(overlay-put (make-overlay (point-at-bol) (point-at-eol))
'display display-string))
(insert "\n")
(put-text-property start (point) 'face 'helm-source-header)))
(defun helm-insert-candidate-separator ()
"Insert separator of candidates into the helm buffer."
(insert (propertize helm-candidate-separator 'face 'helm-separator))
(put-text-property (point-at-bol)
(point-at-eol) 'helm-candidate-separator t)
(insert "\n"))
;;; Core: async process
(defun helm-output-filter (process string)
"The `process-filter' of PROCESS.
It will be used by `set-process-filter' in asynchronous sources.
STRING is the output of PROCESS."
(helm-output-filter-1 (assoc process helm-async-processes) string))
(defun helm-output-filter-1 (process-assoc string)
(helm-log-eval string)
(with-current-buffer helm-buffer
(let ((source (cdr process-assoc)))
(helm-aif (assoc-default 'insertion-marker source)
(goto-char it)
(goto-char (point-max))
(helm-insert-header-from-source source)
(setcdr process-assoc
(append source `((insertion-marker . ,(point-marker))))))
(car process-assoc) string source
(helm-candidate-number-limit source))))
(defun helm-output-filter--process-source (process string source limit)
(dolist (candidate (helm-transform-candidates
(split-string string "\n")
(assoc 'incomplete-line source))
source t))
(if (assq 'multiline source)
(let ((start (point)))
(helm-insert-match candidate 'insert-before-markers source)
(put-text-property start (point) 'helm-multiline t))
(helm-insert-match candidate 'insert-before-markers source))
(incf (cdr (assoc 'item-count source)))
(when (>= (assoc-default 'item-count source) limit)
(helm-kill-async-process process)
(defun helm-output-filter--collect-candidates (lines incomplete-line-info)
"Collect lines in LINES maybe completing the truncated first and last lines."
;; The output of process may come in chunks of any size,
;; so the last line of LINES come truncated, this truncated line is
;; stored in INCOMPLETE-LINE-INFO and will be concated with the first
;; incomplete line of next chunk arriving.
;; INCOMPLETE-LINE-INFO is an attribute of source which is created
;; with an empty string when the source is computed => (incomplete-line . "")
(helm-log-eval (cdr incomplete-line-info))
(butlast ; The last line is the incomplete line, remove it.
(loop for line in lines collect
(if (cdr incomplete-line-info) ; On start it is an empty string.
(concat (cdr incomplete-line-info) line)
(setcdr incomplete-line-info nil))
;; store last incomplete line (last chunk truncated)
;; until new output arrives.
finally do (setcdr incomplete-line-info line))))
(defun helm-output-filter--post-process ()
(helm-log-run-hook 'helm-update-hook)
(helm-aif (get-buffer-window helm-buffer 'visible)
(with-selected-window it
(helm-skip-noncandidate-line 'next)
(helm-display-mode-line (helm-get-current-source))
(defun helm-kill-async-processes ()
"Kill all asynchronous processes registered in `helm-async-processes'."
(while helm-async-processes
(helm-kill-async-process (caar helm-async-processes))
(setq helm-async-processes (cdr helm-async-processes))))
(defun helm-kill-async-process (process)
"Stop output from `helm-output-filter' and kill associated PROCESS."
(set-process-filter process t)
(delete-process process))
;;; Core: action
(defun helm-execute-selection-action ()
"Execute current action and kill the action buffer if present."
(helm-log-run-hook 'helm-before-action-hook)
;; Position can be different when `helm-current-buffer'
;; is splitted, so jump to this position before executing action.
(helm-current-position 'restore)
(helm-aif (get-buffer helm-action-buffer)
(kill-buffer it))
(helm-log-run-hook 'helm-after-action-hook)))
(defun helm-execute-selection-action-1 (&optional
selection action
"Execute ACTION on current SELECTION.
If PRESERVE-SAVED-ACTION is non--nil save action."
(helm-log "executing action")
(setq action (helm-get-default-action
(or action
(if (get-buffer helm-action-buffer)
(helm-get-selection helm-action-buffer)
(let ((source (or helm-saved-current-source
(setq selection (or selection
(and (assoc 'accept-empty source) "")))
(unless preserve-saved-action (setq helm-saved-action nil))
(when (and selection action)
source action
(helm-coerce-selection selection source)))))
(defun helm-coerce-selection (selection source)
"Apply coerce attribute function to SELECTION in SOURCE.
Coerce source with coerce function."
(helm-aif (assoc-default 'coerce source)
(helm-funcall-with-source source it selection)
(defun helm-get-default-action (action)
"Get the first ACTION value of action list in source."
(if (and (listp action) (not (functionp action)))
(cdar action)
(defun helm-select-action ()
"Select an action for the currently selected candidate.
If action buffer is selected, back to the helm buffer."
(helm-log-run-hook 'helm-select-action-hook)
(cond ((get-buffer-window helm-action-buffer 'visible)
(set-window-buffer (get-buffer-window helm-action-buffer)
(kill-buffer helm-action-buffer)
(helm-set-pattern helm-input 'noupdate))
(setq helm-saved-selection (helm-get-selection))
(unless helm-saved-selection
(error "Nothing is selected"))
(setq helm-saved-current-source (helm-get-current-source))
(let ((actions (helm-get-action)))
(if (functionp actions)
(message "Sole action: %s" actions)
(helm-show-action-buffer actions)
;; Make `helm-pattern' differs from the previous value.
(setq helm-pattern 'dummy)
(defun helm-show-action-buffer (actions)
(with-current-buffer (get-buffer-create helm-action-buffer)
(set-window-buffer (get-buffer-window helm-buffer) helm-action-buffer)
(set (make-local-variable 'helm-sources)
`(((name . "Actions")
(candidates . ,actions)
. (lambda (candidates)
(loop for (i . j) in candidates
(cons (propertize i 'face 'helm-action) j))))
(set (make-local-variable 'helm-source-filter) nil)
(set (make-local-variable 'helm-selection-overlay) nil)
(helm-initialize-overlays helm-action-buffer)))
;; Core: selection
(defun helm-move-selection-common (move-func unit direction)
"Move the selection marker to a new position with function MOVE-FUNC.
It is determined by UNIT and DIRECTION."
(unless (or (helm-empty-buffer-p (helm-buffer-get))
(not (helm-window)))
(helm-log-run-hook 'helm-move-selection-before-hook)
(funcall move-func)
(helm-skip-noncandidate-line direction)
(helm-display-source-at-screen-top-maybe unit)
(when (helm-get-previous-header-pos)
(helm-display-mode-line (helm-get-current-source))
(helm-log-run-hook 'helm-move-selection-after-hook))))
(defun helm-display-source-at-screen-top-maybe (unit)
(when (and helm-display-source-at-screen-top (eq unit 'source))
(set-window-start (selected-window)
(save-excursion (forward-line -1) (point)))))
(defun helm-skip-noncandidate-line (direction)
(helm-skip-header-and-separator-line direction)
(and (bobp) (forward-line 1)) ; skip first header.
(and (eobp) (forward-line -1))) ; avoid last empty line.
(defun helm-skip-header-and-separator-line (direction)
"Skip source header or candidate separator when going to next/previous line.
Possible value of DIRECTION are 'next or 'previous."
(while (and (not (bobp))
(or (helm-pos-header-line-p)
(forward-line (if (and (eq direction 'previous)
(not (eq (point-at-bol) (point-min))))
-1 1))))
;; Internal
;; The string to display in mode-line.
(defvar helm-mode-line-string-real nil)
(defun helm-display-mode-line (source)
"Setup mode-line and header-line for `helm-buffer'."
(set (make-local-variable 'helm-mode-line-string)
(helm-interpret-value (or (and (listp source) ; Check if source is empty.
(assoc-default 'mode-line source))
(default-value 'helm-mode-line-string))
;; Setup mode-line.
(if helm-mode-line-string
(setq mode-line-format
'(" " mode-line-buffer-identification " "
(line-number-mode "L%l") " " (helm-follow-mode "(HF) ")
(:eval (helm-show-candidate-number
(when (listp helm-mode-line-string)
(car-safe helm-mode-line-string))))
" " helm-mode-line-string-real " -%-")
(substitute-command-keys (if (listp helm-mode-line-string)
(cadr helm-mode-line-string)
(setq mode-line-format (default-value 'mode-line-format)))
;; Setup header-line.
(let* ((hlstr (helm-interpret-value
(and (listp source)
(assoc-default 'header-line source)) source))
(hlend (make-string (max 0 (- (window-width) (length hlstr))) ? )))
(setq header-line-format
(propertize (concat " " hlstr hlend) 'face 'helm-header))))
(defun helm-show-candidate-number (&optional name)
"Used to display candidate number in mode-line.
You can specify NAME of candidates e.g \"Buffers\" otherwise
it is \"Candidate\(s\)\" by default."
(unless (helm-empty-source-p)
(format "[%s %s]"
(helm-approximate-candidate-number 'in-current-source)
(or name "Candidate(s)"))
'face 'helm-candidate-number)))
(defun helm-previous-line ()
"Move selection to the previous line."
(lambda ()
(if (not (helm-pos-multiline-p))
(forward-line -1) ;double forward-line is meaningful
(forward-line -1) ;because evaluation order is important
(helm-skip-header-and-separator-line 'previous)
(let ((header-pos (helm-get-previous-header-pos))
(separator-pos (helm-get-previous-candidate-separator-pos)))
(when header-pos
(goto-char (if (or (null separator-pos)
(< separator-pos header-pos))
header-pos ; first candidate
(forward-line 1)))))
'line 'previous))
(defun helm-next-line ()
"Move selection to the next line."
(lambda ()
(if (not (helm-pos-multiline-p))
(forward-line 1)
(let ((header-pos (helm-get-next-header-pos))
(separator-pos (helm-get-next-candidate-separator-pos)))
(cond ((and separator-pos
(or (null header-pos) (< separator-pos header-pos)))
(goto-char separator-pos))
(goto-char header-pos))))))
'line 'next))
(defun helm-previous-page ()
"Move selection back with a pageful."
(lambda ()
(condition-case nil
(beginning-of-buffer (goto-char (point-min)))))
'page 'previous))
(defun helm-next-page ()
"Move selection forward with a pageful."
(lambda ()
(condition-case nil
(end-of-buffer (goto-char (point-max)))))
'page 'next))
(defun helm-beginning-of-buffer ()
"Move selection at the top."
(lambda () (goto-char (point-min)))
'edge 'previous))
(defun helm-end-of-buffer ()
"Move selection at the bottom."
(lambda () (goto-char (point-max)))
'edge 'next))
(defun helm-previous-source ()
"Move selection to the previous source."
(lambda ()
(forward-line -1)
(if (bobp)
(goto-char (point-max))
(helm-skip-header-and-separator-line 'previous))
(goto-char (helm-get-previous-header-pos))
(forward-line 1))
'source 'previous))
(defun helm-next-source ()
"Move selection to the next source."
(lambda ()
(goto-char (or (helm-get-next-header-pos) (point-min))))
'source 'next))
(defun helm-goto-source (source-or-name)
"Move the selection to the source SOURCE-OR-NAME."
(lambda ()
(goto-char (point-min))
(let ((name (if (stringp source-or-name) source-or-name
(assoc-default 'name source-or-name))))
(condition-case err
(while (not (string= name (helm-current-line-contents)))
(goto-char (helm-get-next-header-pos)))
(error (message "")))))
'source 'next))
(defun helm-mark-current-line (&optional resumep)
"Move `helm-selection-overlay' to current line.
Note that this is not related with visibles marks, which are used
to mark candidates."
(when resumep
(goto-char helm-selection-point))
helm-selection-overlay (point-at-bol)
(if (helm-pos-multiline-p)
(let ((header-pos (helm-get-next-header-pos))
(separator-pos (helm-get-next-candidate-separator-pos)))
(or (and (null header-pos) separator-pos)
(and header-pos separator-pos (< separator-pos header-pos)
(1+ (point-at-eol))))
(setq helm-selection-point (overlay-start helm-selection-overlay)))
(defun helm-this-command-key ()
(event-basic-type (elt (this-command-keys-vector) 0)))
(defvar helm-exit-status 0
"Flag to inform whether helm have exited or quitted.
Exit with 0 mean helm have exited executing an action.
Exit with 1 mean helm have quitted with \\[keyboard-quit]
It is useful for example to restore a window config if helm abort
in special cases.
See `helm-exit-minibuffer' and `helm-keyboard-quit'.")
(defvar helm-minibuffer-confirm-state nil)
(defun helm-confirm-and-exit-minibuffer ()
"Maybe ask for confirmation when exiting helm.
It is similar to `minibuffer-complete-and-exit' adapted to helm.
If `minibuffer-completion-confirm' value is 'confirm,
send in minibuffer confirm message and exit on next hit.
If `minibuffer-completion-confirm' value is t,
don't exit and send message 'no match'."
(let* ((empty-buffer-p (with-current-buffer helm-buffer
(eq (point-min) (point-max))))
(unknow (and (not empty-buffer-p)
(string= (get-text-property
0 'display (helm-get-selection nil 'withprop))
(cond ((and (or empty-buffer-p unknow)
(eq minibuffer-completion-confirm 'confirm))
(setq helm-minibuffer-confirm-state
(setq minibuffer-completion-confirm nil)
(minibuffer-message " [confirm]"))
((and (or empty-buffer-p unknow)
(eq minibuffer-completion-confirm t))
(minibuffer-message " [No match]"))
(setq helm-minibuffer-confirm-state nil)
(add-hook 'helm-after-update-hook 'helm-confirm-and-exit-hook)
(defun helm-confirm-and-exit-hook ()
"Restore `minibuffer-completion-confirm' when helm update."
(unless (or (eq minibuffer-completion-confirm t)
(not helm-minibuffer-confirm-state))
(setq minibuffer-completion-confirm
(defun helm-exit-minibuffer ()
"Select the current candidate by exiting the minibuffer."
(unless helm-current-prefix-arg
(setq helm-current-prefix-arg current-prefix-arg))
(setq helm-exit-status 0)
(defun helm-keyboard-quit ()
"Quit minibuffer in helm.
If action buffer is displayed, kill it."
(when (get-buffer-window helm-action-buffer 'visible)
(kill-buffer helm-action-buffer))
(setq helm-exit-status 1)
(defun helm-get-next-header-pos ()
"Return the position of the next header from point."
(next-single-property-change (point) 'helm-header))
(defun helm-get-previous-header-pos ()
"Return the position of the previous header from point."
(previous-single-property-change (point) 'helm-header))
(defun helm-pos-multiline-p ()
"Return non-nil if the current position is in the multiline source region."
(get-text-property (point) 'helm-multiline))
(defun helm-get-next-candidate-separator-pos ()
"Return the position of the next candidate separator from point."
(next-single-property-change (point) 'helm-candidate-separator))
(defun helm-get-previous-candidate-separator-pos ()
"Return the position of the previous candidate separator from point."
(previous-single-property-change (point) 'helm-candidate-separator))
(defun helm-pos-header-line-p ()
"Return t if the current line is a header line."
(or (get-text-property (point-at-bol) 'helm-header)
(get-text-property (point-at-bol) 'helm-header-separator)))
(defun helm-pos-candidate-separator-p ()
"Return t if the current line is a candidate separator."
(get-text-property (point-at-bol) 'helm-candidate-separator))
;;; Debugging
(defun helm-debug-output ()
"Show all helm-related variables at this time."
(helm-help-internal " *Helm Debug*" 'helm-debug-output-function))
(defun helm-debug-output-function (&optional vars)
(message "Calculating all helm-related values...")
(insert "If you debug some variables or forms, set `helm-debug-forms'
to a list of forms.\n\n")
(dolist (v (or vars
(apropos-internal "^helm-" 'boundp)))
(insert "** "
(pp-to-string v) "\n"
(pp-to-string (with-current-buffer helm-buffer (eval v))) "\n"))
(message "Calculating all helm-related values...Done"))
;; Core: misc
(defun helm-kill-buffer-hook ()
"Remove tick entry from `helm-tick-hash' when killing a buffer."
(loop for key being the hash-keys in helm-tick-hash
if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
do (remhash key helm-tick-hash)))
(add-hook 'kill-buffer-hook 'helm-kill-buffer-hook)
(defun helm-preselect (candidate-or-regexp)
"Move `helm-selection-overlay' to CANDIDATE-OR-REGEXP on startup."
(when candidate-or-regexp
(goto-char (point-min))
;; go to first candidate of first source
(forward-line 1)
(let ((start (point)))
(or (re-search-forward
(concat "^" (regexp-quote candidate-or-regexp) "$") nil t)
(re-search-forward candidate-or-regexp nil t)
(search-forward candidate-or-regexp nil t)
(goto-char start))))
(defun helm-delete-current-selection ()
"Delete the currently selected item."
(cond ((helm-pos-multiline-p)
(helm-aif (helm-get-next-candidate-separator-pos)
(delete-region (point-at-bol)
(1+ (progn (goto-char it) (point-at-eol))))
;; last candidate
(goto-char (helm-get-previous-candidate-separator-pos))
(delete-region (point-at-bol) (point-max)))
(when (helm-end-of-source-p)
(goto-char (or (helm-get-previous-candidate-separator-pos)
(forward-line 1)))
(delete-region (point-at-bol) (1+ (point-at-eol)))
(when (helm-end-of-source-p)
(let ((headp (save-excursion
(forward-line -1)
(not (helm-pos-header-line-p)))))
(and headp (forward-line -1))))))
(unless (eq (point-at-bol) (point-at-eol))
(defun helm-end-of-source-p ()
"Return non--nil if we are at eob or end of source."
(forward-line 1)
(or (eq (point-at-bol) (point-at-eol))
(defun helm-edit-current-selection-internal (func)
(let ((realvalue (get-text-property (point) 'helm-realvalue)))
(funcall func)
(and realvalue
(put-text-property (point) (point-at-eol)
'helm-realvalue realvalue))
(defmacro helm-edit-current-selection (&rest forms)
"Evaluate FORMS at current selection in the helm buffer.
You can edit the line."
(declare (indent 0) (debug t))
(lambda () ,@forms)))
(defun helm-set-pattern (pattern &optional noupdate)
"Set minibuffer contents to PATTERN.
if optional NOUPDATE is non-nil, helm buffer is not changed."
(with-selected-window (or (active-minibuffer-window) (minibuffer-window))
(insert pattern))
(when noupdate
(setq helm-pattern pattern)))
(defun helm-delete-minibuffer-contents ()
"Same as `delete-minibuffer-contents' but this is a command."
(helm-set-pattern ""))
;;; Plugins
;; Built-in plug-in: type
(defun helm-compile-source--type (source)
(helm-aif (assoc-default 'type source)
(append source (assoc-default it helm-type-attributes) nil)
;; `define-helm-type-attribute' is public API.
(defun helm-add-type-attribute (type definition)
(helm-aif (assq type helm-type-attributes)
(setq helm-type-attributes (delete it helm-type-attributes)))
(push (cons type definition) helm-type-attributes))
(defvar helm-types nil)
(defun helm-document-type-attribute (type doc)
(add-to-list 'helm-types type t)
(put type 'helm-typeattrdoc
(concat "- " (symbol-name type) "\n\n" doc "\n")))
;; Built-in plug-in: dummy
(defun helm-dummy-candidate (candidate source)
"Use `helm-pattern' as CANDIDATE in SOURCE."
;; `source' is defined in filtered-candidate-transformer
(list helm-pattern))
(defun helm-compile-source--dummy (source)
(if (assoc 'dummy source)
(append source
'((candidates "dummy")
(match identity)
(filtered-candidate-transformer . helm-dummy-candidate)
;; Built-in plug-in: candidates-in-buffer
(defun helm-candidates-in-buffer ()
"Get candidates from the candidates buffer according to `helm-pattern'.
BUFFER is `helm-candidate-buffer' by default. Each
candidate must be placed in one line. This function is meant to
be used in candidates-in-buffer or candidates attribute of an
helm source. Especially fast for many (1000+) candidates.
'((name . \"many files\")
(init . (lambda () (with-current-buffer (helm-candidate-buffer 'local)
(search re-search-forward) ; optional
(type . file))
| The new way of making and narrowing candidates: Using buffers |
By default, `helm' makes candidates by evaluating the
candidates function, then narrows them by `string-match' for each
But this way is very slow for many candidates. The new way is
storing all candidates in a buffer and narrowing them by
`re-search-forward'. Search function is customizable by search
attribute. The important point is that buffer processing is MUCH
FASTER than string list processing and is the Emacs way.
The init function writes all candidates to a newly-created
candidate buffer. The candidates buffer is created or specified
by `helm-candidate-buffer'. Candidates are stored in a line.
The candidates function narrows all candidates, IOW creates a
subset of candidates dynamically. It is the task of
`helm-candidates-in-buffer'. As long as
`helm-candidate-buffer' is used,`(candidates-in-buffer)' is
sufficient in most cases.
Note that `(candidates-in-buffer)' is shortcut of three attributes:
(candidates . helm-candidates-in-buffer)
(match identity)
And `(candidates-in-buffer . func)' is shortcut of three attributes:
(candidates . func)
(match identity)
The expansion is performed in `helm-get-sources'.
The candidates-in-buffer attribute implies the volatile attribute.
The volatile attribute is needed because `helm-candidates-in-buffer'
creates candidates dynamically and need to be called everytime
`helm-pattern' changes.
Because `helm-candidates-in-buffer' plays the role of `match' attribute
function, specifying `(match identity)' makes the source slightly faster.
However if source contain match-part attribute, match is computed only
on part of candidate returned by the call of function provided by this attribute.
The function should have one arg, candidate, and return only
a specific part of candidate.
To customize `helm-candidates-in-buffer' behavior, use search,
get-line, match-part and search-from-end attributes.
See also `helm-sources' docstring."
(declare (special source))
(or (assoc-default 'get-line source)
;; use external variable `source'.
(or (assoc-default 'search source)
(if (assoc 'search-from-end source)
(helm-candidate-number-limit source)
(assoc 'search-from-end source)
(helm-attr 'match-part)))
(defun helm-candidates-in-buffer-search-from-start (pattern)
"Search PATTERN with `re-search-forward' with bound and noerror args."
(re-search-forward pattern nil t))
(defun helm-candidates-in-buffer-search-from-end (pattern)
"Search PATTERN with `re-search-backward' with bound and noerror args."
(re-search-backward pattern nil t))
(defun helm-candidates-in-buffer-1 (buffer pattern get-line-fn
search-fns limit search-from-end
;; buffer == nil when candidates buffer does not exist.
(when buffer
(with-current-buffer buffer
(let ((start-point (if search-from-end (point-max) (point-min)))
(endp (if search-from-end #'bobp #'eobp))
(inhibit-point-motion-hooks t))
(goto-char (1- start-point))
(if (string= pattern "")
endp get-line-fn limit search-from-end)
pattern get-line-fn search-fns limit search-from-end
start-point endp match-part-fn))))))
(defun helm-point-is-moved (proc)
"If point is moved after executing PROC, return t, otherwise nil."
(/= (point) (save-excursion (funcall proc) (point))))
(defun helm-search-from-candidate-buffer (pattern get-line-fn search-fns
limit search-from-end
start-point endp match-part-fn)
(let (buffer-read-only
(case-fold-search (helm-set-case-fold-search)))
(lambda ()
(clrhash helm-cib-hash)
(dolist (searcher search-fns)
(goto-char start-point)
(setq newmatches nil)
(loop with item-count = 0
while (funcall searcher pattern)
for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
when (or
;; Always collect when cand is matched by searcher funcs
;; and match-part attr is not present.
(not match-part-fn)
;; If match-part attr is present, collect only if PATTERN
;; match the part of CAND specified by the match-part func.
(helm-search-match-part cand pattern match-part-fn))
do (helm-accumulate-candidates-internal
cand newmatches helm-cib-hash item-count limit)
unless (helm-point-is-moved
(lambda ()
(if search-from-end
(goto-char (1- (point-at-bol)))
(forward-line 1))))
return nil)
(setq matches (append matches (nreverse newmatches))))
(delq nil matches)))))
(defun helm-search-match-part (candidate pattern match-part-fn)
"Match PATTERN only on part of CANDIDATE returned by MATCH-PART-FN."
(let ((part (funcall match-part-fn candidate)))
(if (string-match " " pattern)
(loop for i in (split-string pattern " " t)
always (string-match i part))
(string-match pattern part))))
(defun helm-initial-candidates-from-candidate-buffer (endp
limit search-from-end)
(delq nil (loop with next-line-fn =
(if search-from-end
(lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
until (funcall endp)
for i from 1 to limit
collect (funcall get-line-fn (point-at-bol) (point-at-eol))
do (funcall next-line-fn 1))))
(defun helm-search-from-candidate-buffer-internal (search-fn)
(goto-char (point-min))
(insert "\n")
(goto-char (point-max))
(insert "\n")
(funcall search-fn)
(goto-char (point-min))
(delete-char 1)
(goto-char (1- (point-max)))
(delete-char 1)
(set-buffer-modified-p nil)))
(defun helm-candidate-buffer (&optional create-or-buffer)
"Register and return a buffer containing candidates of current source.
`helm-candidate-buffer' searches buffer-local candidates buffer first,
then global candidates buffer.
Acceptable values of CREATE-OR-BUFFER:
- nil (omit)
Only return the candidates buffer.
- a buffer
Register a buffer as a candidates buffer.
- 'global
Create a new global candidates buffer,
named \" *helm candidates:SOURCE*\".
- other non-nil value
Create a new local candidates buffer,
named \" *helm candidates:SOURCE*HELM-CURRENT-BUFFER\"."
(let* ((global-bname (format " *helm candidates:%s*"
(local-bname (format " *helm candidates:%s*%s"
(buffer-name helm-current-buffer)))
(register-func #'(lambda ()
(setq helm-candidate-buffer-alist
(cons (cons helm-source-name create-or-buffer)
(delete (assoc helm-source-name
(kill-buffers-func #'(lambda ()
(loop for b in (buffer-list)
if (string-match (format "^%s" (regexp-quote global-bname))
(buffer-name b))
do (kill-buffer b))))
(create-func #'(lambda ()
(get-buffer-create (if (eq create-or-buffer 'global)
(font-lock-mode -1))))
(return-func #'(lambda ()
(or (get-buffer local-bname)
(get-buffer global-bname)
(helm-aif (assoc-default helm-source-name
(and (buffer-live-p it) it))))))
(when create-or-buffer
(funcall register-func)
(unless (bufferp create-or-buffer)
(and (eq create-or-buffer 'global) (funcall kill-buffers-func))
(funcall create-func)))
(funcall return-func)))
(defun helm-init-candidates-in-buffer (buffer data)
"Register BUFFER with DATA for an helm candidates-in-buffer session.
Arg BUFFER can be a string, a buffer object (bufferp), or a symbol,
either 'local or 'global which is passed to `helm-candidate-buffer'.
Arg DATA can be either a list or a plain string."
(let ((buf (helm-candidate-buffer
(if (or (stringp buffer)
(bufferp buffer))
(get-buffer-create buffer)
buffer)))) ; a symbol.
(with-current-buffer buf
(if (listp data)
(loop for i in data do (insert (concat i "\n")))
(and (stringp data) (insert data)))))
(defun helm-compile-source--candidates-in-buffer (source)
(helm-aif (assoc 'candidates-in-buffer source)
(append source
`((candidates . ,(or (cdr it) 'helm-candidates-in-buffer))
(volatile) (match identity)))
;; Utility: resplit helm window
(defun helm-toggle-resplit-window ()
"Toggle resplit helm window, vertically or horizontally."
(let ((before-height (window-height)))
(if (= (window-height) before-height) ; initial split was horizontal.
;; Split window vertically with `helm-buffer' placed
;; on the good side according to actual value of
;; `helm-split-window-default-side'.
(cond ((or (eq helm-split-window-default-side 'above)
(eq helm-split-window-default-side 'left))
(selected-window) nil 'above))
(t (split-window-vertically)))
(setq helm-split-window-state 'vertical))
;; Split window vertically, same comment as above.
(setq helm-split-window-state 'horizontal)
(cond ((or (eq helm-split-window-default-side 'left)
(eq helm-split-window-default-side 'above))
(split-window (selected-window) nil 'left))
(t (split-window-horizontally)))))
;; Utility: Resize helm window.
(defun helm-enlarge-window-1 (n)
"Enlarge or narrow helm window.
If N is positive enlarge, if negative narrow."
(unless helm-samewindow
(let ((horizontal-p (eq helm-split-window-state 'horizontal)))
(enlarge-window n horizontal-p)))))
(defun helm-narrow-window ()
"Narrow helm window."
(helm-enlarge-window-1 -1))
(defun helm-enlarge-window ()
"Enlarge helm window."
(helm-enlarge-window-1 1))
(defun helm-swap-windows ()
"Swap window holding `helm-buffer' with other window."
(if (and helm-samewindow (one-window-p t))
(error "Error: Can't swap windows in a single window")
(let* ((w1 (helm-window))
(split-state (eq helm-split-window-state 'horizontal))
(w1size (window-total-size w1 split-state))
(b1 (window-buffer w1)) ; helm-buffer
(s1 (window-start w1))
(cur-frame (window-frame w1))
(w2 (next-window w1 1 cur-frame))
(w2size (window-total-size w2 split-state))
(b2 (window-buffer w2)) ; probably helm-current-buffer
(s2 (window-start w2))
(with-selected-frame (window-frame w1)
(helm-replace-buffer-in-window w1 b1 b2)
(helm-replace-buffer-in-window w2 b2 b1)
(setq resize
(cond ( ;; helm-window is smaller than other window.
(< w1size w2size)
(- (- (max w2size w1size)
(min w2size w1size))))
( ;; helm-window is larger than other window.
(> w1size w2size)
(- (max w2size w1size)
(min w2size w1size)))
( ;; windows have probably same size.
t nil)))
;; Maybe resize the window holding helm-buffer.
(and resize (window-resize w2 resize split-state))
(set-window-start w1 s2 t)
(set-window-start w2 s1 t)))))
(defun helm-replace-buffer-in-window (window buffer1 buffer2)
"Replace BUFFER1 by BUFFER2 in WINDOW registering BUFFER1."
(when (get-buffer-window buffer1)
(unrecord-window-buffer window buffer1)
(set-window-buffer window buffer2)))
;; Utility: select another action by key
(defun helm-select-nth-action (n)
"Select the N nth action for the currently selected candidate."
(setq helm-saved-selection (helm-get-selection))
(unless helm-saved-selection
(error "Nothing is selected"))
(setq helm-saved-action (helm-get-nth-action n (helm-get-action)))
(defun helm-get-nth-action (n action)
(cond ((and (zerop n) (functionp action))
((listp action)
(or (cdr (elt action n))
(error "No such action")))
((and (functionp action) (< 0 n))
(error "Sole action"))
(error "Error in `helm-select-nth-action'"))))
(defun helm-select-2nd-action ()
"Select the 2nd action for the currently selected candidate."
(helm-select-nth-action 1))
(defun helm-select-3rd-action ()
"Select the 3rd action for the currently selected candidate."
(helm-select-nth-action 2))
(defun helm-select-4th-action ()
"Select the 4th action for the currently selected candidate."
(helm-select-nth-action 3))
(defun helm-select-2nd-action-or-end-of-line ()
"Select the 2nd action for the currently selected candidate.
This happen when point is at the end of minibuffer.
Otherwise goto the end of minibuffer."
(if (eolp)
(helm-select-nth-action 1)
;; Utility: Persistent Action
(defmacro with-helm-display-same-window (&rest body)
"Execute BODY in the window used for persistent action.
Make `pop-to-buffer' and `display-buffer' display in the same window."
(declare (indent 0) (debug t))
`(let ((display-buffer-function 'helm-persistent-action-display-buffer))
(defvar helm-persistent-action-display-window nil)
(defun helm-initialize-persistent-action ()
(set (make-local-variable 'helm-persistent-action-display-window) nil))
(defun* helm-execute-persistent-action
(&optional (attr 'persistent-action) split-onewindow)
"Perform the associated action ATTR without quitting helm.
ATTR default is 'persistent-action', but it can be helm else.
In this case you have to add this new attribute to your source.
When `helm-samewindow' or SPLIT-ONEWINDOW are non--nil,
and `helm-buffer' is displayed in only one window,
the helm window is splitted to display
`helm-select-persistent-action-window' in other window
and keep its visibility."
(helm-log "executing persistent-action")
(or split-onewindow (and helm-samewindow (one-window-p t))))
(helm-log-eval (current-buffer))
(let ((helm-in-persistent-action t))
(or (assoc-default attr (helm-get-current-source))
(helm-log-run-hook 'helm-after-persistent-action-hook))
;; A typical case is when a persistent action delete
;; the buffer already displayed in
;; `helm-persistent-action-display-window' and `helm-samewindow'
;; is enabled, we end up with the `helm-buffer'
;; displayed in two windows.
(when (and helm-samewindow
(> (length (window-list)) 1)
(equal (buffer-name
(defun helm-persistent-action-display-window (&optional split-onewindow)
"Return the window that will be used for persistent action.
If SPLIT-ONEWINDOW is non--nil window will be splitted in persistent action."
(setq helm-persistent-action-display-window
(cond ((and (window-live-p helm-persistent-action-display-window)
(not (member helm-persistent-action-display-window
(get-buffer-window-list helm-buffer))))
(split-onewindow (split-window))
((get-buffer-window helm-current-buffer))
(t (next-window (selected-window) 1))))))
(defun helm-select-persistent-action-window (&optional split-onewindow)
"Select the window that will be used for persistent action.
See `helm-persistent-action-display-window' for how to use SPLIT-ONEWINDOW."
(select-window (get-buffer-window (helm-buffer-get)))
(setq minibuffer-scroll-window
(helm-persistent-action-display-window split-onewindow))))
(defun helm-persistent-action-display-buffer (buf &optional not-this-window)
"Make `pop-to-buffer' and `display-buffer' display in the same window.
If `helm-persistent-action-use-special-display' is non-nil and
BUF is to be displayed by `special-display-function', use it.
Otherwise ignores `special-display-buffer-names' and `special-display-regexps'.
Argument NOT-THIS-WINDOW if present will be used as
second argument of `display-buffer'."
(let* ((name (buffer-name buf))
display-buffer-function pop-up-windows pop-up-frames
(unless (and helm-persistent-action-use-special-display
(or (member name
(mapcar (lambda (x) (or (car-safe x) x))
(loop for x in special-display-regexps
thereis (string-match (or (car-safe x) x)
(display-buffer buf not-this-window)))
;; scroll-other-window(-down)? for persistent-action
(defun helm-other-window-base (command &optional scroll-amount)
(setq scroll-amount (unless (eq scroll-amount 'noscroll)
(with-selected-window (helm-persistent-action-display-window)
(funcall command scroll-amount)))
(defun helm-scroll-other-window ()
"Scroll other window (not *Helm* window) upward."
(helm-other-window-base 'scroll-up))
(defun helm-scroll-other-window-down ()
"Scroll other window (not *Helm* window) downward."
(helm-other-window-base 'scroll-down))
(defun helm-recenter-top-bottom-other-window ()
"`recenter-top-bottom' in other window (not *Helm* window)."
(helm-other-window-base 'recenter-top-bottom 'noscroll))
(defun helm-reposition-window-other-window ()
"`helm-reposition-window' in other window (not *Helm* window)."
(helm-other-window-base 'reposition-window 'noscroll))
;; Utility: Visible Mark
(defun helm-clear-visible-mark ()
(with-current-buffer (helm-buffer-get)
(mapc 'delete-overlay helm-visible-mark-overlays)
(set (make-local-variable 'helm-visible-mark-overlays) nil)))
(add-hook 'helm-after-initialize-hook 'helm-clear-visible-mark)
(defvar helm-marked-candidates nil
"Marked candadates. List of \(source . real\) pair.")
(defun helm-this-visible-mark ()
(loop for o in helm-visible-mark-overlays
when (equal (point-at-bol) (overlay-start o))
return o))
(defun helm-delete-visible-mark (overlay)
(setq helm-marked-candidates
(cons (helm-get-current-source) (helm-get-selection))
(delete-overlay overlay)
(setq helm-visible-mark-overlays
(delq overlay helm-visible-mark-overlays)))
(defun helm-make-visible-mark ()
(let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
(overlay-put o 'face 'helm-visible-mark)
(overlay-put o 'source (assoc-default 'name (helm-get-current-source)))
(overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
(overlay-put o 'real (helm-get-selection))
(add-to-list 'helm-visible-mark-overlays o))
(push (cons (helm-get-current-source) (helm-get-selection))
(defun helm-toggle-visible-mark ()
"Toggle helm visible mark at point."
(helm-aif (helm-this-visible-mark)
(helm-delete-visible-mark it)
(defun helm-mark-all ()
"Mark all visible unmarked candidates in current source."
(goto-char (helm-get-previous-header-pos))
(let* ((next-head (helm-get-next-header-pos))
(end (and next-head
(goto-char next-head)
(forward-line -1)
(maxpoint (or end (point-max))))
(while (< (point) maxpoint)
(let* ((prefix (get-text-property (point-at-bol) 'display))
(cand (helm-get-selection))
(bn (and (helm-file-completion-source-p)
(helm-c-basename cand)))
(src (assoc-default 'name (helm-get-current-source))))
(when (and (not (helm-this-visible-mark))
(not (or (string= prefix "[?]")
(string= prefix "[@]"))))
;; Don't mark possibles directories ending with . or ..
;; autosave files/links and non--existent file.
(and (or (helm-file-completion-source-p)
(equal src "Files from Current Directory"))
(or (string-match "^\\.#.*\\|^#.*#$\\|\\.$" bn)
;; We need to test here when not using a transformer
;; that tag prefix (i.e on tramp)
(not (file-exists-p cand))))
(forward-line 1) (end-of-line))))
(message "%s candidates marked" (length helm-marked-candidates))))
(defun helm-unmark-all ()
"Unmark all candidates in all sources of current helm session."
(let ((len (length helm-marked-candidates)))
(setq helm-marked-candidates nil)
(message "%s candidates unmarked" len))))
(defun helm-toggle-all-marks ()
"Toggle all marks.
Mark all visible candidates of current source or unmark all candidates
visible or invisible in all sources of current helm session"
(let ((marked (helm-marked-candidates)))
(if (and (>= (length marked) 1)
(with-helm-window helm-visible-mark-overlays))
(defun helm-display-all-visible-marks ()
"Show all `helm' visible marks strings.
Only useful for debugging."
(lexical-let ((overlays (reverse helm-visible-mark-overlays)))
(lambda ()
(with-output-to-temp-buffer "*helm visible marks*"
(dolist (o overlays) (princ (overlay-get o 'string)))))))))
(defun helm-marked-candidates ()
"Return marked candidates of current source if any.
Otherwise one element list of current selection.
It is analogous to `dired-get-marked-files'."
(with-current-buffer (helm-buffer-get)
(let ((cands
(if helm-marked-candidates
(loop with current-src = (helm-get-current-source)
for (source . real) in (reverse helm-marked-candidates)
when (equal current-src source)
collect (helm-coerce-selection real source))
(list (helm-get-selection)))))
(helm-log-eval cands)
(defun helm-reset-marked-candidates ()
(with-current-buffer (helm-buffer-get)
(set (make-local-variable 'helm-marked-candidates) nil)))
(add-hook 'helm-after-initialize-hook 'helm-reset-marked-candidates)
(defun helm-current-source-name= (name)
(goto-char (helm-get-previous-header-pos))
(equal name (helm-current-line-contents))))
(defun helm-revive-visible-mark ()
"Restore marked candidates when helm update display."
(with-current-buffer helm-buffer
(dolist (o helm-visible-mark-overlays)
(goto-char (point-min))
(while (and (search-forward (overlay-get o 'string) nil t)
(helm-current-source-name= (overlay-get o 'source)))
;; Calculate real value of candidate.
;; It can be nil if candidate have only a display value.
(let ((real (get-text-property (point-at-bol 0) 'helm-realvalue)))
(if real
;; Check if real value of current candidate is the same
;; that the one stored in overlay.
(and (string= (overlay-get o 'real) real)
(move-overlay o (point-at-bol 0) (1+ (point-at-eol 0))))
(move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))))
(add-hook 'helm-update-hook 'helm-revive-visible-mark)
(defun helm-next-point-in-list (curpos points &optional prev)
;; rule out special cases
((null points) curpos)
((and prev (< curpos (car points))) curpos)
((< (car (last points)) curpos)
(if prev (car (last points)) curpos))
(nth (if prev
(loop for pt in points
for i from 0
if (<= curpos pt)
return (1- i))
(loop for pt in points
for i from 0
if (< curpos pt)
return i))
(defun helm-next-visible-mark (&optional prev)
"Move next helm visible mark.
If PREV is non-nil move to precedent."
(goto-char (helm-next-point-in-list
(sort (mapcar 'overlay-start helm-visible-mark-overlays) '<)
(defun helm-prev-visible-mark ()
"Move previous helm visible mark."
(helm-next-visible-mark t))
;; Utility: Selection Paste
(defun helm-yank-selection ()
"Set minibuffer contents to current selection."
(let ((str (helm-get-selection nil t)))
(kill-new str)
(helm-set-pattern str)))
(defun helm-kill-selection-and-quit ()
"Store current selection to kill ring.
You can paste it by typing \\[yank]."
(lambda (sel)
(kill-new sel)
(message "Killed: %s" sel))
(helm-get-selection nil t)))
;;; Follow-mode: Automatical execution of persistent-action
(define-minor-mode helm-follow-mode
"Execute persistent action everytime the cursor is moved when enabled."
:group 'helm
:init-value t
(with-current-buffer helm-buffer
(setq helm-follow-mode (not helm-follow-mode))
(message "helm-follow-mode is %s"
(if helm-follow-mode "enabled" "disabled"))))
(defun helm-follow-execute-persistent-action-maybe ()
"Execute persistent action in mode `helm-follow-mode'.
This happen after `helm-input-idle-delay' secs."
(and (not (get-buffer-window helm-action-buffer 'visible))
(buffer-local-value 'helm-follow-mode
(get-buffer-create helm-buffer))
(sit-for (and helm-input-idle-delay
(max helm-input-idle-delay 0.1)))
;; Bug Report
(defvar helm-maintainer-mail-address "")
(defvar helm-bug-report-salutation
"Describe bug below, using a precise recipe.
When I executed M-x ...
How to send a bug report:
1) Be sure to use the LATEST version of helm.el.
2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
3) Use Lisp version instead of compiled one: (load \"helm.el\")
4) If you got an error, please paste *Backtrace* buffer.
5) Type C-c C-c to send.")
(defvar helm-no-dump-variables
"Variables not to dump in bug report.")
(defun helm-dumped-variables-in-bug-report ()
(let ((hash (make-hash-table)))
(loop for var in (apropos-internal "helm-" 'boundp)
for vname = (symbol-name var)
unless (or (string-match "-map$" vname)
(string-match "^helm-c-source-" vname)
(string-match "-hash$" vname)
(string-match "-face$" vname)
(memq var helm-no-dump-variables))
collect var)))
(defun helm-send-bug-report ()
"Send a bug report of helm.el."
(with-current-buffer (or helm-last-buffer
nil nil
(defun helm-send-bug-report-from-helm ()
"Send a bug report of helm.el in helm session."
(helm-run-after-quit 'helm-send-bug-report))
(provide 'helm)
;; Local Variables:
;; byte-compile-warnings: (not cl-functions obsolete)
;; coding: utf-8
;; indent-tabs-mode: nil
;; End:
;;; helm.el ends here
Jump to Line
Something went wrong with that request. Please try again.