2022年9月19日月曜日

Emacsの補完で使ってるcompanyの見直し?(202209)

company について前回晒してから、はや 1 年。この間に lsp を導入したり、ivy から consult へも変更したりしてるので、 再確認の機会になればと晒してみる。

主な変更は、 company-backends の設定を、対象モード側ではなく、 company のフックで統一したこと。だけだった。

環境は、 Debian の 5.10.140-1 Bullesye で、emacs は 27.1 。 org-mode でのメモ書きと python が主用途で変らず。

(leaf company :ensure t :require t
  :url "https://github.com/company-mode/company-mode"
  :doc "http://company-mode.github.io"
  :blackout t
  :hook ((text-mode-hook python-mode-hook emacs-lisp-mode-hook)
         ;; backends は ここで設定するのがよさそう
         (plantuml-mode-hook . (lambda ()
                                 (set (make-local-variable 'company-backends)
                                      '(company-yasnippet company-dabbrev))))
         (emacs-lisp-mode-hook . (lambda ()
                                   (set (make-local-variable 'company-backends)
                                        '(company-semantic company-files company-elisp company-dabbrev-code))))
         (org-mode-hook . (lambda ()
                            (set (make-local-variable 'company-backends)
                                 '(company-ispell company-dabbrev company-yasnippet company-files))))
         (rst-mode-hook . (lambda ()
                            (set (make-local-variable 'company-backends)
                                 '(company-ispell company-dabbrev company-yasnippet company-files))))
         (python-mode-hook . (lambda ()
                               (set (make-local-variable 'company-backends)
                                     '(company-jedi company-dabbrev-code company-ispell company-yasnippet))))
         (html-mode-hook . (lambda ()
                             (set (make-local-variable 'company-backends)
                                  '(company-capf company-web-html company-dabbrev-code company-yasnippet company-files company-ispell))))
         (nxml-mode-hook . (lambda ()
                             (set (make-local-variable 'company-backends)
                                  '(company-capf company-nxml company-dabbrev-code company-yasnippet company-files company-ispell))))
         (css-mode-hook . (lambda ()
                            (set (make-local-variable 'company-backends)
                                 '(company-capf company-css company-dabbrev-code company-yasnippet company-files company-ispell))))
         ;; company-gtags
         )
  :bind (
         ;; ("<tab>" . company-indent-or-complete-common)
         (company-active-map  ;; enabled during an active completion
          ("M-p")
          ("M-n")
          ("<tab>" . company-quit)
          ("C-n" . company-select-next)
          ("C-p" . company-select-previous)
          ("C-o" . company-other-backend)
          ;; lsp-mode で時々うまく機能しないことがある
          ("C-v" . company-next-page)
          ("M-v" . company-previous-page)
          ("C-s" . company-filter-candidates))
         (company-search-map  ;; incrementally searching the completion candidates
          ("<tab>" . company-quit)
          ("C-p" . company-select-previous)
          ("C-n" . company-select-next)
          ("C-v" . company-next-page)
          ("M-v" . company-previous-page))
         )
  :config
  (setq auto-complete-mode nil) ;; required by jedi-2019
  (setq company-idle-delay 0.1) ;; defailt 0.5
  (setq company-show-numbers nil)
  (setq company-selection-wrap-around t)  ;; 候補の最後の次は先頭に戻る
  (setq-default company-minimum-prefix-length 1) ;; original 3

  ;; (setq completion-ignore-case t)      ;; c-source

  (setq company-dabbrev-downcase nil)
  ;; (setq company-dabbrev-other-buffers nil) ;; 少しでも反応よくしたい
  (setq company-dabbrev-other-buffers t) ;; If t, all buffers with major modes 間違えていた(221019)
  (setq company-dabbrev-code-other-buffers 'code)  ;; 間違えていた(221019)
  (setq company-dabbrev-code-everywhere t)     ;;  include comments and strings.
  ;; (setq company-dabbrev-ignore-buffers "\\`\\'")

  ;; (setq company-eclim-auto-save nil)   ;; for eclipse
  ;; (setq company-tooltip-align-annotations nil)  ;; default nil
  (setq company-tooltip-limit 8)
  (setq company-require-match 'never)    ;; If enabled, disallow non-matching input.

  (defun company-quit nil
    "Insert any selected completion and quit completing.
     https://gist.github.com/rswgnu/85ca5c69bb26551f3f27500855893dbe#file-rsw-company-config-el"
    (interactive)
    (when (and company-selection-changed company--manual-action
               (boundp 'company-tng--overlay)
               company-tng--overlay)
      (company--insert-candidate
       (nth company-selection company-candidates)))
    (company-cancel))

  :defer-config
  ;; (leaf company-gtags :ensure nil :require t)
  ;; (setq company-gtags-insert-arguments t)  ;; original t

  ;; for web

  (leaf company-web :ensure t :disabled t
    :url "https://github.com/osv/company-web"
    :doc "Company-web support integration with emmet-mode and emmet-preview and
        add some advices to make C-g and RET keys work properly."
    :init
    (leaf web-completion-data :ensure t :require t)
    )

  (leaf company-jedi :ensure t
    :after (python-mode)
    :init
    (leaf python-environment :ensure t :require t
      :doc "Required by: jedi-core"
      )
    (leaf jedi-core :ensure t :require t
      ;; :custom (
      ;;   (jedi:environment-root        . "~/.pyenv/versions/3.9.10/bin/python3")
      ;; )
      :commands (jedi:setup)
      ;; :config
      ;; (setq jedi:server-command (list (executable-find "jediepcserver")))
      )  ;; ends jedi-core

   :config
    (setq jedi:tooltip-method nil)  ;; pos-tip and/or popup, nil is minibuffer
    (setq-local jedi:complete-on-dot t)
    (setq-local jedi:use-shortcuts nil) ;; M-. and M-,

    )  ;; ends company-jedi. _____

  ;; start a specific backend.
  ;; (setq company-begin-backend 'company-ispell)

  (leaf company-prescient :ensure t :require t :disabled nil
    :url "https://github.com/raxod502/prescient.el"
    :config
    (company-prescient-mode t))

  ;; skip to the next matching backend in the backend list.
  ;;  [2021-05-07] (setq company-other-backend '(company-eclim company-bbdd company-cmake company-files company-oddmuse))

  ;; lsp-ui-doc があるので、company-quickhelp を不要とする。

  )

なんか、冗長になっているのではと思ってしまう。 もっと、シンプルにならないものかと思うが、ハードル高そう。 どこかに良きサンプルはないだろうか。

vim も、未だ使っている。が、その出番は少なくなってるように思う。 最近設定をいじった記憶がないと、思っていたが、 vim-tiny と neovim を aliase で使い分けるようしてた。

emacs を使う時は、少し構えているのかもしれない。

まあ、なんとなく使えているので、よしとしよう。

2022年9月18日日曜日

Emacs の lsp の設定、なう(202209)

前回さらしてから、はや1年。で、再度晒してみる。誰も興味ないだろうけど。

  • 環境は、Debian(bullseye)+emacs(27.1)。consult + company。 consult になってます。まだ馴染んでません。 embark の恩恵にもあずかってません。頭がついていけない。
  • lsp は、 python, shell, html(追加) で使用。
  • lsp は lsp-mode で、python のクライアントは、lsp-pylsp。 shell は bash-language-server。 html は vscode-langservers-extracted。
  • python のクライアントは整理されたようだ。 pyrigh は、python で完結しないらしいので、私は避けている。
  • flymake ではなく、flycheck。設定は lsp まかせ。
  • 補完は、lsp 経由?の company になってる。ispell も含められてる。 ただ、バックエンドの変更が、時々うまくいかないことがある。
  • フォールディングは、origami だが、あいかわらず使えこなせない。 narrowing で間に合っているかんじ。
  • lsp-ui-peak は、一応設定してあるというレベルで、これも変らず。
  • 新しく追加したのは、sideline-mode と treemacs の2つ。
    • lsp-sideline 見易くなったと思う。
    • lsp-treemacs 画面が広くなったので、試している最中。

最近は、なべると1日1時間ぐらいしか、触ってない。 だからか、変更は少ない。

;; lsp-mode

(leaf sideline :ensure t :require t)
;; 表示を見やすくすため追加。いいかんじ。

(leaf lsp-mode :ensure t
  :url "https://github.com/emacs-lsp/lsp-mode"
  :hook (
         (python-mode-hook . lsp-deferred)
         (rust-mode-hook . lsp-deferred)    ;; rust-analyzer
         (sh-mode-hook . lsp-deferred)      ;;  yarn global add bash-language-server
         (html-mode . lsp-deferred)         ;;  yarn global add vscode-langservers-extracted
         (css-mode . lsp-deferred)         ;;
         ;; (haskell-mode . lsp-deferred)      ;;
         ;; To defer LSP server startup (and DidOpen notifications) until the buffer is visible
         ;; you can use lsp-deferred instead of lsp:
         (lsp-mode-hook . lsp-enable-which-key-integration) ;; Adds to `which-key-mode'
         (lsp-mode-hook . (lambda () (ggtags-mode -1))) ;; global との共存は止めてみる [2021-06-12]
         (lsp-mode-hook . sideline-mode)
         )
  :custom (
           ;; (add-to-list 'lsp-disabled-clients 'pyright)
           (lsp-log-io             . nil)  ;; print all messages to and from the language server to ~*lsp-log*~.
           ;; (lsp-print-performance  . nil) ;; removed in Relese 8.0
           (lsp-server-trace       . nil)  ;; Request trace mode on the language server.
           ;; general
           (lsp-auto-guess-root . t)  ;; original nil
           ;;   Automatically guess the project root using projectile/project.
           ;;   Do *not* use this setting unless you are familiar with ‘lsp-mode’
           ;;   internals and you are sure that all of your projects are
           ;;   following ‘projectile’/‘project.el’ conventions.
           (lsp-message-project-root-warning . t)

           (lsp-auto-configure . t)  ;;
           ;;   "Auto configure `lsp-mode' main features.
           ;; When set to t `lsp-mode' will auto-configure
           ;;   completion, code-actions, breadcrumb, flycheck, flymake, imenu, symbol highlighting,
           ;;   lenses, links, and so on.

           (lsp-headerline-breadcrumb-enable . t)
           ;; (lsp-headerline-breadcrumb-segments . '(project file symbols))
           ;;
           ;; (lsp-document-sync-method . 'lsp-sync-incremental)  ;; original nil  'inclemental は変更された
           ;; (lsp-document-sync-method . 2)
           ;;
           (lsp-eldoc-render-all . nil)
           ;; ミニバッファでの関数情報    nil: シグニチャだけ,   t: doc-string 本体を表示する
           ;;
           (lsp-idle-delay . 0.5)
           (lsp-response-timeout     . 5)
           (lsp-enable-snippet       . nil)     ;; original t ; yasnippet を直接使っている。
           ;; (lsp-enable-folding       . t)     ;; original t
           (lsp-enable-indentation   . t)   ;; あちこちで設定しているみたいで、気持ち悪い。
           ;;
           (lsp-completion-enable . t)  ;; original t
           (lsp-completion-provider . :capf)  ;;
           ;;
           ;; (lsp-completion-provider . :none)
           ;; (lsp-prefer-capf . t)  ;;  use ‘lsp-completion-provider’ instead.
           ;; (lsp-completion-no-cache . t)  ;;
           ;; (lsp-completion-enable-additional-text-edit  . t)
           (lsp-completion-show-detail . nil)
           ;; (lsp-completion-show-kind . nil)

           ;; (lsp-diagnostics-provider . :auto) ;; default :auto
           (lsp-diagnostics-provider . :flycheck)
           ;; Safe renamed lsp-diagnose to lsp-doctor(Relese 8.0)
           ;;
           ;; (lsp-inhibit-message . t)
           ;; (create-lockfiles . nil)
           (lsp-file-watch-threshold . nil)
           ;;
           (lsp-enable-symbol-highlighting . t)
           (lsp-signature-render-documentation . nil)
           )
  :commands (lsp lsp-deferred)
  ;; :commands (lsp)
  :config
  ;; .venv, .mypy_cache などを watch 対象から外す
  ;;    https://qiita.com/slotport/items/0e9cb2a875fe85bc1735
  (dolist (dir '(
                 "[/\\\\]\\.venv$"
                 "[/\\\\]\\.mypy_cache$"
                 "[/\\\\]__pycache__$"
                 "[/\\\\]_archives$"
                 "[/\\\\]_logs$"
                 "[/\\\\]\\.local\\'"
                 "[/\\\\]\\.cache\\'"
                 ))
    (push dir lsp-file-watch-ignored))
  ;;
  (leaf lsp-ui :ensure t
    :custom
    ;; lsp-ui-doc
    (lsp-ui-doc-enable . t)            ;; original t
    (lsp-ui-doc-header . nil)          ;; original nil
    ;; (lsp-ui-doc-use-childframe . t)
    (lsp-ui-doc-delay  . 1.5)
    (lsp-ui-doc-include-signature . t)   ;; original nil
    (lsp-ui-doc-position . 'bottom)    ;; top bottom at-point
    ;; (lsp-ui-doc-alignment . 'frame)
    ;;   "How to align the doc.
    ;;    only takes effect when `lsp-ui-doc-position' is 'top or 'bottom."
    ;; (lsp-ui-doc-border . "orange")
    ;; (lsp-ui-doc-border (face-foreground 'default))
    ;; (lsp-ui-doc-use-childframe . t)      ;; require 26
    ;;(lsp-ui-doc-max-width . 70)
    ;;(lsp-ui-doc-max-height . 20)
    (lsp-ui-doc-use-webkit . nil)
    (lsp-eldoc-enable-hover . nil)  ;; mini-buffer への表示

    ;; disable cursor hover (keep mouse hover)
    (lsp-ui-doc-show-with-cursor . t)
    ;; disable mouse hover (keep cursor hover)
    (lsp-ui-doc-show-with-mouse . nil)

    ;; lsp-ui-flycheck  ;; autoload ?
    (lsp-ui-flycheck-enable . t)

    (lsp-lens-enable . t)

    ;; lsp-ui-sideline
    ;;    Show informations of the symbols on the current line.
    ;;    It also show flycheck diagnostics and LSP code actions
    ;;
    (lsp-ui-sideline-update-mode . 'line)  ;; line or point
    (lsp-ui-sideline-enable . t)
    ;; (lsp-ui-sideline-show-symbol . t)
    ;; (lsp-ui-sideline-show-code-actions . nil) ;; default nil
    ;; (lsp-ui-sideline-code-actions-prefix . "")
    ;; (lsp-ui-sideline-show-hover . nil)  ;; default nil
    (lsp-ui-sideline-show-diagnostics . t)
    (lsp-ui-sideline-ignore-duplicate . t)
    (lsp-ui-sideline-delay . 0.5)
    ;;
    ;; lsp-ui-imenu
    ;;    "q"                       kill
    ;;    "r"                       refresh
    ;;    "<right>"                 next kind
    ;;    "<left>"                  prev kind
    ;;    "M-RET",  "<M-return>"    visit
    ;;    "RET",    "<return>"      view
    ;;
    (lsp-ui-imenu-enable . t)
    (lsp-ui-imenu-kind-position . 'top)
    ;;     (setq lsp-ui-imenu-colors `(,(face-foreground 'font-lock-keyword-face)
    ;;                                 ,(face-foreground 'font-lock-string-face)
    ;;                                 ,(face-foreground 'font-lock-constant-face)
    ;;                                 ,(face-foreground 'font-lock-variable-name-face)))
    ;;
    ;; lsp-ui-peek
    ;;   like Visual studio peak function.
    (lsp-ui-peek-enable . t)
    (lsp-ui-peek-show-directory . t)
    (lsp-ui-peek-always-show . t)
    ;; (lsp-ui-peek-list-width . 60)
    ;; (lsp-ui-peek-peek-height . 20)
    (lsp-ui-peek-fontify . 'on-demand) ;; never, on-demand, or always

    )

   ;; sideline-mode 対応
   (leaf sideline-flycheck :ensure t :require t)
   (leaf sideline-lsp :ensure t :require t
    :init
    :config
    (setq sideline-backends-right '(sideline-lsp sideline-flycheck))
   )

  :defer-config

  ;; consult 対応
  (leaf consult-lsp :ensure t :require t
    :url "https://github.com/gagbo/consult-lsp"
    :doc ""
    :after consult
    )

  ;; うまく使えていない。
  (leaf lsp-origami :ensure t :require t :disabled nil
    :url "https://github.com/emacs-lsp/lsp-origami"
    :doc "folding range の設定をorigami にやらせるということみたい。これは不要か。"
    :doc "python-mode は hideshow を require. うまく共存できる?"
    ;; :hook lsp-after-open-hook
    ;; :after origami-minor-mode
    :commands lsp-origami-try-enable
    )

  ;; L570 にして画面が少し広くなったので、使ってみている。 lsp-ui-imenu の強化版?
  ;; カスタマイズは今後の課題。ブタに真珠の状態。
  (leaf lsp-treemacs :ensure t :require t
    :doc "LSP treemacs"
    :tag "languages" "emacs>=26.1"
    :added "2022-06-24"
    :url "https://github.com/emacs-lsp/lsp-treemacs"
    :emacs>= 26.1
    :ensure t
    :after treemacs lsp-mode
    )

  )

惰性で使ってて、何かを調べてカスタマイズしよう、とかがない昨今である。

L570になって、画面が広くなったし、 メモリも十分だし、 反応が遅いという印象も少ないので、まあ、いいかな。

最近、python-mode が、時々アップデート(v6.3.1 20220907)されている。 アップデートの中身は理解できないけど、 私が python-mode 自体を使っているという意識が希薄なので、 一度、使い方を調べた方がよいのかもと思っている。 (移動や narrow に必要になる ブロックとかの判定がメインかなあ)

まあ、なんとか emacs を使い続けているので、よしとしましょう。

2022年9月2日金曜日

Sphinx で markdown のファイルを include する方法で躓く

Python のプロジェクトで sphinx の autodoc を使おうと努力?していて、 プロジェクトの文書は docs ディレクトリに入れている。

プロジェクトトップの README.md というファイルに 概要を記載することにしているのだが、 こいつを sphinx の文書に include させたくなったのだが、 markdown を変換してくれない、という問題が発生した。 extension には myst_parser を加えている。

REASME.rst にするとか、docs ディレクトリにコピーして使うとかで問題ないのだが、 markdown の方が市民権が強いみたいだし、釈然としない。で、調べてみると、 以下の記事をみつけた。

プロジェクトトップのファイルを autodoc で直接利用する方法について、 昔から話はあるようだが、 markdown ファイルの include には、課題が残っていたようだ。

解決方法は、以下のふたつ。

  1. markdown を reStructured に変換する m2r2 という extension を追加して、 mdinclude というディレクティブを使う。

    .. mdinclude:: ../README.md
    
  1. extension に myst_parser 追加して、 include 時に parser を指定する。ただし、新しいバージョンでないとだめ。

    .. include:: ../README.md
       :parser: myst_parser.sphinx_
    

両方とも、markdown ファイルを include してくれたので、よしとしよう。

しかし、肝心なのは中身なのに、それがまだまだ。 python は、gpx がらみがほとんどで、自転車封印2.5年越えで、 モチベーションが既に維持できてない。

Emacs の lsp の設定、なう(202310)

前回さらしてから、さらに1年。そう、3年めになる。 が、今回は一段と自信がない。 環境は、 Debian GNU/Linux 12 (bookworm) + emacs(29.1)。consult + company。 embark は未だに使ってない。 用途は、メモ と ...