Yukang's Page

My org-mode stuff

2019-08-01

I switched to org-mode from EverNote recently, and the experience imrpoved much for note and journal writing, especially for technical notes. After the whole tool is set appropriately, I am even addicted to writing. During my setting up for org-mode and related tools, I found these code snippets are really handy, so let’s have a share.

Insert Pic from paste

You need to install pngpaste first, then with this elisp function, we can copy the picture from paste very quickly, the picture will store on current directory’s img sub-directory, it will create it if img directory is not exists.

(defun org-insert-image ()
(interactive)
(let* ((path (concat default-directory "img/"))
(image-file (concat
path
(buffer-name)
(format-time-string "_%Y%m%d_%H%M%S.png"))))
(if (not (file-exists-p path))
(mkdir path))
(shell-command (concat "pngpaste " image-file))
(org-insert-link nil (concat "file:" image-file) ""))
;; (org-display-inline-images) ;; show inline picture
)

Using org-ruby for Hexo publishing

I using Hexo for blogging, the default format is markdown in Hexo, so I need to convert org format to markdown format very conveniently, and finally org-ruby solve it. I did some dirty hacks on the codebase, please have a look at this PR, this PR solve three issues.

  • Add title and path attributes in org file, and the ruby script will extract it for dumping markdown file.
  • Fix the fill paragraph problem, I don’t need the blanks which will broken the paragraph layout.
  • Copy the images to proper directory for Hexo, support image size attributes.

Then I added an elisp function for auto publish it after saving file whenever “#MD_TITLE:” is founded in buffer:

(defun org-publish-to-hexo ()
(interactive)
(shell-command (concat
"org-ruby " "--translate " "markdown " "-a "
(buffer-name))))
(defalias 'op 'org-publish-to-hexo)

(defun buffer-contains-substring (string)
(save-excursion
(save-match-data
(goto-char (point-min))
(search-forward string nil t))))

(defun org-auto-publish-save-hook ()
(when (and (eq major-mode 'org-mode)
(buffer-contains-substring "#+MD_TITLE:")
(buffer-contains-substring "#+MD_PATH:"))
(message "publishing to Hexo markdown")
(org-publish-to-hexo)))

(add-hook 'after-save-hook #'org-auto-publish-save-hook)

(defun org-before-save-hook ()
(when (eq major-mode 'org-mode)
(message "saving org-file")
(pangu-spacing-space-current-buffer)
;;(fill-region (point-min) (point-max))
))
(add-hook 'before-save-hook #'org-before-save-hook)

pangu-spacing

This package will add spacing between Chinese word and English word, so I hooked it before save org file:

(require 'pangu-spacing)
(global-pangu-spacing-mode 1)
;;(setq pangu-spacing-real-insert-separtor t)

(defun org-before-save-hook ()
(when (eq major-mode 'org-mode)
(message "saving org-file")
(pangu-spacing-space-current-buffer)
))

(add-hook 'before-save-hook #'org-before-save-hook)

org-capture

And the best thing is org-capture, with this we can write all kinds of tempaltes, for journal writing, I need to generate file according to date and time:

(defun create-code-file ()
(interactive)
(let ((name (concat (format-time-string "%Y_%m_%d_")
(read-string "file-name: "))))
(expand-file-name (format "%s.org" name) "~/Dropbox/org/snippets/")))

(defun gen-date-file ()
"Create an org file in ~/notes/snippets."
(format-time-string "~/Dropbox/org/journals/%Y_%m_%d.org"))

(setq org-capture-templates
'(("t" "Todo" entry (file+datetree "~/Dropbox/org/work.org")
"** TODO %?\n %i\n " :empty-lines 1)
("x" "Task" entry (file+datetree "~/Dropbox/org/work.org")
"** TODO %^{priority|[#A]|[#B]|[#C]} %?\n")
("e" "Task" entry (file+datetree "~/Dropbox/org/life.org")
"** TODO %^{priority|[#A]|[#B]|[#C]} %?\n" :empty-lines 1)
("l" "Todo" entry (file+datetree "~/Dropbox/org/learn.org")
"** TODO %?\nEntered on %U\n %i\n\n " :kill-buffer t :empty-lines 1)
("k" "Todo" entry (file+datetree "~/Dropbox/org/learn.org")
"* TODO %?\n %i\n %f\n %a" :empty-lines 1)
("j" "Journal" entry (file+datetree "~/Dropbox/org/_journal.org" )
"** %?\nEntered on %U\n %i\n" :empty-lines 1)
("J" "Journal" entry (file gen-date-file)
"** %?\nEntered on %U\n %i\n" :empty-lines 1)
("c" "Code snippet" entry (file+headline "~/Dropbox/org/_code.org" "Code")
"** %^{desc}\n#+BEGIN_SRC %^{language|ruby|shell|c|rust|emacs-lisp}\n%?\n#+END_SRC"
:empty-lines 1)
("C" "Notes" entry (file create-code-file)
"** %^{desc}\n#+BEGIN_SRC %^{language|ruby|shell|c|rust|emacs-lisp}\n%?\n#+END_SRC"
:empty-lines 1)
))