Emacs diary additional functionality

On my another posting about Everyday Text File and emacs LISP Customization, I made a fast way to make a text file every day for diary. Also, there was a way to find information from diaries. However, sometimes, I want to see yesterday’s diary, and previous/next day’s diary of current diary file. To make this, I wrote a bash script and LISP functions.

Bash script that opens previous/next file


fname=$(basename $2)

if [ "$1" == "next" ]
    a=$(ls /diary | grep -C1 $fname | tail -1)
    echo "/diary/"$a
elif [ "$1" == "prev" ]
    b=$(ls /diary | grep -C1 $fname | head -1)
    echo "/diary/"$b

This script gets a file name and return previous file or next file. In this case it returns previous day’s file or next day’s file when I run like this :

np.sh prev diary2015_04_20.md

When there is diary2015_04_21.md file it opens it. If now, it opens a previous file that there is.

LISP functions for emacs

(defun prevfile ()
  (find-file (substring (shell-command-to-string (format "~/sh/np.sh prev %s" (current-buffer)))0 -1) )

(defun nextfile ()
  (find-file (substring (shell-command-to-string (format "~/sh/np.sh next %s" (current-buffer)))0 -1) )

(global-set-key (kbd "C-S-<prior>") 'prevfile)
(global-set-key (kbd "C-S-<next>") 'nextfile)

M-x prevfile takes current buffer name, which is the file name, and open previous file in the diary directory. I set short cuts using global-set-key.
Ctrl+Shift+PageUp opens previous day’s diary
Ctrl+Shift+PageDown opens next day’s diary
and in my case, Ctrl+PageUp/PageDown opens previous/next buffer in emacs.


Markdown format

Recently, I started to use Markdown format as a personal memo format. This is by far the one I was looking for. I always wanted a text format that can be easily formatted as HTML, at the same time, it should be easy to write, and text file itself should be readable. Markdown has all that features and it is already widely being used.
I was thinking about using wiki format. There is local wiki editor and viewer called Zim Desktop Wiki. Emacs also has wiki mode. However, sharing with other devices was painful.

About Markdown

Markdown is developed in 2004 by John Gruber “to write using an easy-to-read, easy-to-write plain text format, and optionally convert it to structurally valid XHTML”


Following links are useful :
* http://en.wikipedia.org/wiki/Markdown
* https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet

Benefit in the local memo file with emacs

To write everyday activities, I was using text files that are everyday generated by emacs (link). It was good, but to share it, I had to copy it to evernote or something.
By using emacs’ Markdown mode, I can see the text with little bit of formatting using color and bold. Also, it gives fast way to insert Markdown formatting keywords.
Now all my text file extension is .md and emacs generates it automatically.

Benefit in mobile sharing with dropbox

When I put the diary directory’s link to dropbox sync directory, the files are automatically synchronized as soon as I edit them.
There is an Android app called “Draft“. It supports Dropbox synchronization for Text and Markdown format. Also it shows formatted Markdown. Of course, it has editing feature.
As a result, when I add or edit a text in the Android Draft app, it is synchronized to my laptop’s diary directory. Now, I do not need evernote. “Draft” looks like this :

Benefit with github

github uses Markdown as readme and all kind of text file. So, if I write my local file using markdown, it can be directly used as github text.

Benefit with wordpress

wordpress supports Markdown. This posting and previous posting was written using Markdown. It is much faster than typing html tag for making list and table. Also, my Markdown memo can be directly posted as a wordpress posting.

Chrome Extension

There is a Markdown Preview Plus chrome extension. If the markdown file is opened by chrome using “file:///home/hosung/diary/diary2015_02_08.md” or drag-drop, it shows generated HTML on the page using CSS for github style or other style. Optionally, the page can reload changed file every 1+ seconds.

Emacs Integration

I wrote a simple LISP script to add into .emacs file. By using it, currently editing file can be quickly opened in chrome by typing M-x chrome or Ctrl-Shift-c.

(defun chrome ()
   (format "google-chrome --activate-existing-profile-browser %s" buffer-file-name ))
(global-set-key (kbd "C-S-c") 'chrome)


How to open a file in existing emacs instance

When the emacs is opened by the command “emacs filename”, it opens new emacs instance.
To avoid this problem, emacs need to run server.

1. start server

M-x server-start

2. command line

emacsclient test.c

Now it opens in the existing emacs.
If there is no emacs instance, nothing happens.

3. Improve
Put this line in .emacs file


Now whenever emacs is started, server is started.

4. Improve more
After the file is opened through emacsclient, when the file is closed(kill-buffer), emacs asks “Buffer still has clients”. The user need to type “yes” to close it. To avoid this, open the file through the command:

emacsclient -n test.c

* In my case files are usually opened through Krusader.
When I push F3, the file is opened through internal lister.
When I push F4, the file is opened through “emacsclient -n filename”.
Just like when I used in Total Commander and UltraEdit.

Konfigurator - Creating Your Own Krusader_035

Everyday Text File and Emacs LISP customization

When I saw this article, I was really glad because I thought that I was the only person who use everyday text file. And I could get some tips that I can try in the future.

Ten Clever Uses for Plain Text Files That Can Increase Your Productivity

When I was a Windows programmer, I used Total Commander and UltraEditor as a file manager and text editor. And I made a simple program that makes a text file as a name of the day’s date. For example, diary09_23_2014.txt. Also, in UltraEditor, there was the functionality to insert date and time to the cursor position. I used this combination for a long time to write my thinking, research results, and piece of code.

When I started to use Linux, I had to find a good file manager and text editor for this purpose. For file manager, I tried several programs including Midnight Commander, Gnome Commander and Krusader. Between them, Krusader was the best for me. It is very stable and has many functions including FTP/SFTP connection.

Screenshot from 2014-11-30 16:19:31

And then, I had to find text editor. I tried many editors: GEdit, Tomboy, Geany, VI, VIM etc. And I decided to use emacs because I knew that emacs is world most customizable editor. At the same time, I knew that it was world most difficult editor to use. So, I spent more than 1 week only to study how to use it. I used a book called “Learning GNU Emacs”. There are still a lot to study.

With emacs, almost everything is possible using LISP script. I haven’t learn LISP yet. However, I could make simple functions using basic LISP syntax and additional bash script for my text diary purpose.

The bash script ~/sh/diary.sh is for making a text file “~/diary/diary11_30_2014.txt” with today’s date, and then put “====Sun Nov 30 15:56:59 EST 2014====” inside the file(end of the file).

_now=$(date +”%m_%d_%Y”)
_now2=$(date +”%a %b %d %H:%M:%S %Z %Y”)
echo “====$_now2====” >> $_file
echo $_file

If I learn LISP more in the future, I could change it to LISP function. However, the benefit of this bash script is that it can be used outside the emacs.

Following is functions in ~/.emacs file.

;;;;;;;;;;;;;;;;open diary file
(defun diary ()
(find-file (substring (shell-command-to-string “~/sh/diary.sh”)0 -1) )
(rename-buffer “diary”)
)(defun diary-search (newnote)
(interactive “sSearch: “)
(format “grep ‘%s’ ~/diary/*.txt” newnote))

(global-set-key (kbd “C-S-d”) ‘diary)

;;;;;;;;;;;;;;;;insert date and time
(defvar current-date-time-format “====%a %b %d %H:%M:%S %Z %Y====”
“Format of date to insert with `insert-current-date-time’ func
See help of `format-time-string’ for possible replacements”)(defun insert-current-date-time ()
“insert the current date and time into current buffer.
Uses `current-date-time-format’ for the formatting the date/time.”
(insert (format-time-string current-date-time-format (current-time)))
(insert “\n”)

(global-set-key “\C-c\C-d” ‘insert-current-date-time)

insert-current-date-time function is modified from this link.

When I push Ctrl-Shift-D, diary() function makes a text file if there is no today’s diary file, and open it with the buffer name “diary”. If there is today’s file, put current date and time at the end of the file and move the cursor to the last position.

Screenshot from 2014-11-30 16:22:09
“M-x diary-search” is for searching text from text diaries inside ~/diary directory using grep shell command. Like below. I can open the file and see the contents by clicking it in the result list.
Screenshot from 2014-11-30 16:23:19

“C-c C-d” is to insert current time to current cursor position while editing diary file.

Screenshot from 2014-11-30 16:29:00

If I make a text file everyday, after several years, there are so many files. So, I merge the files to 1 file per 1 month or 1 year. Because inside the diary, there are year, date and time in every section. Search works the same, so whether it’s day file or month file doesn’t matter.

Also It can be synchronized to evernote, dropbox etc., to search from my phone.

How to connect SeneNET Extream in Linux

In Seneca College, there is very convenient way to access wi-fi network called “SeneNET Extreme“.

It supports Windows, MacOS, iOS, Android, even Blackberry. However, there is no instruction for Linux.
These days, my main OS for laptop is Ubuntu Linux 14.04 LTS; so, I had to use SeneNET in spite of the inconvenience of web authentication. Moreover, in TEL building, SeneNET signal is very very weak. So I had to connect SeneNET Extream on my Galaxy phone and use USB tethering through it. It was ridiculous.

Recently, I could figure out how to connect SeneNET Extreme in Linux. Basically, that is how to connect to WPA2 Enterprise. It works on my Ubuntu machine.

1. Try access ‘SeneNET-Extream’ network whatever method.
It will not work; but it is OK.

2. Open a terminal and run the following commands and there will be SenecaNET-Extream file
cd /etc/NetworkManager/system-connections
sudo nano SenecaNET-Extream #vi/nano/emacs whatever editor

3. Then edit [802-1x] section like this. Edit <> parts. Remove other lines in this section


4. save the file, exit, and test it.

This is all. Simple.