@peccul is peccu

(love peccu '(emacs lisp cat outdoor bicycle mac linux coffee))

helmでwindows.elのウィンドウを切り替える

k8sではなくEmacsの話。

anythingからhelmに切り替えてからもウィンドウ切り替えだけ anything-c-source-windowsを使っていたが helm一本にするために重い腰を上げてhelmのsourceを書いてみた。windowsが古すぎるというかマイナーすぎるのか情報が少ない気がする。

元にした実装はこれ

i-yt.info

最近のEmacs事情に追いつけてないので古すぎると思うけれど、手元の環境では動いている感じ。 helmのホームページで知ったけどstraightって何?って状況。

  • init.elの内容
(require 'helm-windows)
(define-key win:switch-map "=" 'helm-windows)
(require 'helm)
(require 'windows)

(defconst helm-windows-candidate-format " (%%c)%%s %%-%ds [%%s]")
(defconst helm-windows-pick-candidate-pattern "^ (\\(.\\))")

(defun helm-windows-switch-to-window (candidate)
  (when (string-match helm-windows-pick-candidate-pattern candidate)
      (let ((last-command-char (if (= win:base-key ?0)
                                   (string-to-number (match-string 1 candidate))
                                 (string-to-char (match-string 1 candidate))))) ;win:base-key should be ?`
        (call-interactively 'win-switch-to-window))))

;; rewrite from anything-c-source-other-windows
;; http://i-yt.info/?date=20090321#p01
;; https://www.emacswiki.org/emacs/AnythingSources#h5o-54
(defun helm-windows-create-candidates ()
  (win:store-config win:current-config)
  (let ((i 1)
        (candidates (list))
        (form (format helm-windows-candidate-format (+ win:names-maxl 2))))
    (while (< i win:max-configs)
      (add-to-list 'candidates
                   (if (aref win:configs i)
                       (format form
                               (+ win:base-key i) ;number or char
                               (cond              ;window state
                                ((= i win:current-config) "*") ;current window
                                ((= i win:last-config) "+")    ;previous window
                                (t " "))                       ;other
                               (format "%s" (aref win:names-prefix i)) ;window name if it exists
                               (aref win:names i)) ;buffer names
                     (format form (+ win:base-key i) " " "" "")))
      (setq i (1+ i)))
    (sort candidates 'string<)))

(defvar helm-source-windows
  (helm-build-sync-source "helm Windows"
    :candidates 'helm-windows-create-candidates
    :action (helm-make-actions
             "Switch to Window"
             'helm-windows-switch-to-window)))

;;;###autoload
(defun helm-windows ()
  "list windows."
  (interactive)
  (helm :sources '(helm-source-windows)
        :window "*helm windows*"))
(provide 'helm-windows)