`(blog ,garaemon)

ポップカルチャーを摂取して、コードを吐き出す機械

emacsでカーソル位置の数字を上下させる

emacsでカーソル位置の数字を上下させるemacs lispコード. どこかから拾ってきたのかもしれないもの. 意外と便利.

(defun increment-number-at-point ()
  "Increase number at current cursor."
  (interactive)
  (skip-chars-backward "0123456789")
  (or (looking-at "[0123456789]+")
      (error
       "No number at point"))
  (replace-match (number-to-string (1+ (string-to-number (match-string 0))))))

(defun decrement-number-at-point ()
  "Decrease number at current cursor."
  (interactive)
  (skip-chars-backward "0123456789")
  (or (looking-at "[0123456789]+")
      (error
       "No number at point"))
  (replace-match (number-to-string (1- (string-to-number (match-string 0))))))

(global-set-key (kbd "C-c C-+") 'increment-number-at-point)
(global-set-key (kbd "C-c C--") 'decrement-number-at-point)

ambie + TT-BR06が悪くない

ambieは耳をふさがないイヤホンということで周囲の音が聞きやすいということを売りに出されているイヤホンだ.

ambie.co.jp

もともとはイヤホンジャックを備えている有線イヤホンとして販売されていた.

ambie.co.jp

先日Bluetoothによって無線化された新製品が発売された.

ambie.co.jp

しかしこの新製品、いくつかの残念な点がある.

個人的には旧有線ambieにtaotronicsのTT-BR06というBluetooth レシーバをつけて利用している.

TT-BR06 Bluetoothレシーバー

このTT-BR06という製品の素晴らしいところは

  • 連続再生時間が15時間(!)
  • 充電しながら利用できる

という2点である. また、Bluetooth 4.1なのでBluetooth 3.0の無線ambieに比べるとその点においても良い.

micropythonでm5stackをwifiにつなげる

m5stackでmicropythonを使ってwifiを利用するときは, wifisetupを利用すれば簡単に実装できる.

import wifisetup

ただ、再接続などを実現するためにどうしても自前で実装したかったので試してみた.

from m5stack import lcd
import utime
import network


def connect_wifi(target_ssid, target_passwd, timeout_sec=10):
    wlan = network.WLAN(network.STA_IF)  # create station interface
    wlan.active(True)                    # activate the interface
    for net in wlan.scan():
        ssid = net[0].decode()
        if ssid == target_ssid:
            lcd.println('Connecting to ' + ssid)
            wlan.connect(ssid, target_passwd)
            start_date = utime.time()
            while not wlan.isconnected():
                now = utime.time()
                lcd.print('.')
                if now - start_date > timeout_sec:
                    break
                utime.sleep(1)
            if wlan.isconnected():
                lcd.println('Success to connect')
                return True
            else:
                lcd.println('Failed to connect')
                return False
    return False

helm-miniのデフォルト値にカーソルのシンボルが渡ってしまう

helm-miniC-x bに割り振ってバッファ選択に使っているのだが、現在のカーソルがある位置のシンボル(いわゆるthing-at-point)がデフォルトで渡ってしまい、バッファの選択に不自由していた.

どうやら, helm関数の:defaultキーワードを与えないと、thing-at-pointがデフォルトとして 渡ってしまう。:defaultnilだとだめなので、空文字列を渡すようにすればいい.

(defun my-helm-mini ()
  "Customized version of helm-mini in order to disable 'thing-at-point'."
  (interactive)
  (require 'helm-x-files)
  (unless helm-source-buffers-list
    (setq helm-source-buffers-list
          (helm-make-source "Buffers" 'helm-source-buffers)))
  (helm :sources helm-mini-default-sources
        :buffer "*helm mini*"
        :default "" ;; important
        :ff-transformer-show-only-basename nil
        :truncate-lines helm-buffers-truncate-lines))
(define-key global-map (kbd "C-x b")   'my-helm-mini)

emacsでmarkdown-modeのinline code blockに色を付ける

code blockのフォントを変えないようにしたのは良いけど

、色も変わらなくなってしまった。 これはみにくいので色を設定するようにする.

(set-face-attribute 'markdown-code-face nil
                    :inherit 'default)
(set-face-attribute 'markdown-inline-code-face nil
                    :inherit 'default
                    :foreground (face-attribute font-lock-type-face :foreground))

micropythonのPOSTでハマった件

m5stackmicropythonを動かして、spotifyで再生中の曲を表示するものを作っている.

その過程で、micropythonに入っているurequestsライブラリでは, postメソッドを特に指定せずに使った時にContent-Typeが指定されないというのでハマった.

よくあるライブラリではpostを呼び出すと Content-Type: application/x-www-form-urlencoded が指定される. しかしこれが指定されないのでちゃんとContent-Typeを渡してあげる必要がある.

urequests.post(
    'http://example.com/foo',
    data=...,
    headers={
        'Content-Type': 'application/x-www-form-urlencoded',
})

ただ、jsonオプションを利用すると、Content-Type: application/jsonが親切に自動的に指定される.

urequestsを使う場合は ソースに目を通すのが必要そうだ.

emacsで選択範囲or現在行のpythonを評価する

elpyを使っています. elpyやemacs付属のpython-modeでも, pythonインタプリタemacs上で起動することができ、大変便利だ.

elpyの場合はM-x elpy-shell-switch-to-shell, python-modeならM-x run-pythonpythonインタプリタを立ち上げることができる.

emacs lispだと、S式もしくはregionの評価がC-x C-eでできて、とても便利。それと似たような挙動をpythonでも実現したい.

emacsでregionを選択していたらその範囲、選択していなかったら現在行のpythonを評価するようなelisp.

(elpy-enable) ;; enable elpy
(defun elpy-shell-send-region-or-statement ()
  "Send region or statement to python shell."
  (interactive)
  (if (use-region-p)
      (progn
        (elpy-shell-send-region-or-buffer)
        (deactivate-mark))
    (elpy-shell-send-statement)
    ))
(define-key python-mode-map "\C-x\C-E" 'elpy-shell-send-region-or-statement)

ついでに、個人的にはpython shellの立ち上げを以下のようなキーバインドに設定している.

(define-key python-mode-map "\C-cE" 'elpy-shell-switch-to-shell)
(global-set-key "\C-cE" 'elpy-shell-switch-to-shell)