Skip to content

Commit

Permalink
Fix `helm-set-local-variable' to work in an init function when force-…
Browse files Browse the repository at this point in the history
…updating.

* helm.el (helm-set-local-variable): Do it.
* helm-buffers.el (helm-buffers-list--init): New.
  • Loading branch information
thierryvolpiatto committed May 21, 2015
1 parent 71e1a60 commit b28ceca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
34 changes: 16 additions & 18 deletions helm-buffers.el
Expand Up @@ -162,33 +162,31 @@ Only buffer names are fuzzy matched when this is enabled,

(defvar helm-buffers-list-cache nil)
(defvar helm-buffer-max-len-mode nil)

(defun helm-buffers-list--init ()
;; Issue #51 Create the list before `helm-buffer' creation.
(setq helm-buffers-list-cache (funcall (helm-attr 'buffer-list)))
(let ((result (cl-loop for b in helm-buffers-list-cache
maximize (length b) into len-buf
maximize (length (with-current-buffer b
(symbol-name major-mode)))
into len-mode
finally return (cons len-buf len-mode))))
(unless (default-value 'helm-buffer-max-length)
(helm-set-local-variable 'helm-buffer-max-length (car result)))
(unless (default-value 'helm-buffer-max-len-mode)
(helm-set-local-variable 'helm-buffer-max-len-mode (cdr result)))))

(defclass helm-source-buffers (helm-source-sync helm-type-buffer)
((buffer-list
:initarg :buffer-list
:initform #'helm-buffer-list
:custom function
:documentation
" A function with no arguments to create buffer list.")
(init
:initform
(lambda ()
;; Issue #51 Create the list before `helm-buffer' creation.
(setq helm-buffers-list-cache (funcall (helm-attr 'buffer-list)))
(let ((result (cl-loop for b in helm-buffers-list-cache
maximize (length b) into len-buf
maximize (length (with-current-buffer b
(symbol-name major-mode)))
into len-mode
finally return (cons len-buf len-mode))))
(unless (default-value 'helm-buffer-max-length)
(helm-set-local-variable 'helm-buffer-max-length (car result)))
(unless (default-value 'helm-buffer-max-len-mode)
;; If a new buffer is longer that this value
;; this value will be updated
(helm-set-local-variable 'helm-buffer-max-len-mode (cdr result))))))
(init :initform 'helm-buffers-list--init)
(candidates :initform helm-buffers-list-cache)
(matchplugin :initform nil)
;(nohighlight :initform t)
(match :initform 'helm-buffers-match-function)
(persistent-action :initform 'helm-buffers-list-persistent-action)
(resume :initform (lambda ()
Expand Down
21 changes: 15 additions & 6 deletions helm.el
Expand Up @@ -1508,17 +1508,26 @@ Otherwise, return VALUE itself."

(defun helm-set-local-variable (&rest args)
"Bind each pair in ARGS locally to `helm-buffer'.
Use this to set local vars before calling helm.
When used from an init or update function
(i.e when `helm-force-update' is running) the variables are set
using `make-local-variable' within the `helm-buffer'.
Usage: helm-set-local-variable ([VAR VALUE]...)
Just like `setq' except that the vars are not set sequentially.
IOW Don't use VALUE of previous VAR to eval the VALUE of next VAR.
When helm is alive use `make-local-variable' as usual on `helm-buffer'.
IOW Don't use VALUE of previous VAR to set the VALUE of next VAR.
\(fn VAR VALUE ...)"
(setq helm--local-variables
(append (cl-loop for i on args by #'cddr
collect (cons (car i) (cadr i)))
helm--local-variables)))
(if helm-force-updating-p
(with-helm-buffer
(cl-loop for i on args by #'cddr
do (set (make-local-variable (car i)) (cadr i))))
(setq helm--local-variables
(append (cl-loop for i on args by #'cddr
collect (cons (car i) (cadr i)))
helm--local-variables))))

(defun helm-make-actions (&rest args)
"Build an alist with (NAME . ACTION) elements with each pairs in ARGS.
Expand Down

0 comments on commit b28ceca

Please sign in to comment.