`(blog ,garaemon)

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

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)

emacsのmarkdown-modeのcode blockでフォントを変えない

emacsmarkdown-modeはcode blockでフォントが変わるのが嫌だったのでこれを変更しないように設定。

emacを使うような人は、デフォルトで等幅フォントを指定してるんじゃないのかな?

;; Do not change font in code block
(set-face-attribute 'markdown-code-face nil :inherit 'default)

before f:id:garaemon1:20180414155744p:plain

after f:id:garaemon1:20180414155755p:plain

emacsで毎日のメモのためのmarkdownを自動的に作成する

メモをとるのにmarkdownはとても便利.

emacsで作業中のメモをとるためのmarkdownを日別に自動的に作成するemacs lispのコード.

~/daily-notes/の下に日付の入ったファイル名を自動生成する. また、先頭に日付も自動的に挿入するようにしている.

(defvar daily-markdown-memo-directory "~/daily-notes"
  "Directory to save daily markdown memos.")
(defun daily-markdown-memo-create-today-markdown ()
  "Create markdown file for today under DAILY-MARKDOWN-MEMO-DIRECTORY."
  (interactive)
  ;; If there is no memo directory, create it.
  (if (not (file-directory-p daily-markdown-memo-directory))
      (progn
        (message "Creating directory: %s" daily-markdown-memo-directory)
        (make-directory daily-markdown-memo-directory)))
  (let ((today-file-full-path
         (let ((today-file (format-time-string "%Y-%m-%d.md" (current-time))))
           (concat daily-markdown-memo-directory "/" today-file))))
    ;; Verify if the buffer is already opened for today-file-full-path.
    ;; If not,
    (let ((file-buffer (find-file-noselect today-file-full-path)))
      (with-current-buffer file-buffer
        (if (not (file-exists-p today-file-full-path))
            ;; If it is the first time to open the file, insert date and save it automatically.
            (progn
              (insert (format-time-string "# %Y-%m-%d" (current-time)))
              (save-buffer))))))
  )
;; Run daily-markdown-memo-create-today-markdown when emacs is opened.
(add-hook 'after-init-hook '(daily-markdown-memo-create-today-markdown))
;; Run daily-markdown-memo-create-today-markdown every 30 minutes
(run-with-timer 0 (* 30 60) 'daily-markdown-memo-create-today-markdown)

emacsのC-sをhelm-swoopで置き換える

最近emacsの検索のキーバインドであるC-shelm-swoopに置き換えてみている.

helm-swoopに関する説明はこちらが詳しい

emacs.rubikitch.com

C-sのたびにバッファがかちゃかちゃしてうるさい気もするけど、使いこなせれば作業効率が上がりそうな気がする.

僕が使っている設定は以下のような感じ.

(global-set-key (kbd "C-s") 'helm-swoop)
(define-key helm-swoop-map (kbd "C-r") 'helm-previous-line)
(define-key helm-swoop-map (kbd "C-s") 'helm-next-line)
(define-key helm-multi-swoop-map (kbd "C-r") 'helm-previous-line)
(define-key helm-multi-swoop-map (kbd "C-s") 'helm-next-line)
;; Disable pre-input for helm-swoop
(setq helm-swoop-pre-input-function (lambda () nil))