Skip to content

Instantly share code, notes, and snippets.

@vaiorabbit
Created February 21, 2012 16:12
Show Gist options
  • Save vaiorabbit/1877206 to your computer and use it in GitHub Desktop.
Save vaiorabbit/1877206 to your computer and use it in GitHub Desktop.
markdown-aux-insert-unordered-list-item
;; (autoload 'markdown-mode "markdown-mode.el" "Major mode for editing Markdown files" t)
;; 箇条書き入力支援
;; Ref.: Emacs Lisp Idioms (for writing interactive commands) - Get Current Word or Line
;; http://xahlee.org/emacs/elisp_idioms.html
;; Inconsistent Behavior for 'line
;; http://xahlee.blogspot.com/2011/02/emacs-lisp-using-thing-at-point.html
(setq markdown-aux-unordered-list-item-string "* ")
(defun markdown-aux-insert-unordered-list-item ()
"Insert `markdown-aux-unordered-list-item-string'."
(interactive)
(beginning-of-line) ; 行頭へ移動
(let (line-length line-blank)
(setq line-length ; 現在の行の文字数
(buffer-substring-no-properties (line-beginning-position) (line-end-position)))
(setq line-blank (looking-at "^[ \t]*$")) ; 空白文字だけなら t. Ref.: delete-blank-lines (simple.el)
(end-of-line) ; 変なところで改行しないように行末へ移動
(if (or (equal 0 line-length) line-blank) ; 空行、または空白文字だけの行なら
(insert markdown-aux-unordered-list-item-string) ;  すぐに文字列入力
(newline) ; 空行ではないなら改行して
(funcall indent-line-function) ;  インデントしてから
(insert markdown-aux-unordered-list-item-string)) ;  文字列入力
(end-of-line))) ; 行末(==箇条書きの先頭位置)へカーソルを移動
; ordered list 用
(defun markdown-aux-insert-ordered-list-number ()
(interactive)
(let (paragraph-string)
(setq paragraph-string (thing-at-point 'paragraph))
(if paragraph-string
(if (string-match "^[ \t]*\\([0-9]+\\)\.[ \t]+" paragraph-string)
(let (olnum line-length line-blank)
(setq olnum (string-to-number (match-string 1 paragraph-string)))
(setq line-length (buffer-substring-no-properties (line-beginning-position) (line-end-position)))
(setq line-blank (looking-at "^[ \t]*$"))
(end-of-line)
(if (or (equal 0 line-length) line-blank)
(insert (format "%d. " (+ 1 olnum)))
(newline)
(funcall indent-line-function)
(insert (format "%d. " (+ 1 olnum))))
(end-of-line))
(message "not in ordered list paragraph"))
(message "nil"))))
;; (define-key markdown-mode-map (kbd "M-<RET>") 'markdown-aux-insert-unordered-list-item)
;; (define-key markdown-mode-map "\C-c\C-eu" 'markdown-aux-insert-unordered-list-item)
;; (define-key markdown-mode-map "\C-c\C-eo" 'markdown-aux-insert-ordered-list-number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment