Sunday, September 21, 2014

Force kill buffer

Occasionally I want to kill a buffer without prompt even it is modified or related with a process.
 (defun kill-buffer-no-prompt (&optional BUFFER-OR-NAME)
   "Kill current buffer, even if it has been modified."
   (let* ((buffer (if BUFFER-OR-NAME (get-buffer BUFFER-OR-NAME)
          (process (get-buffer-process buffer)))
     (if process (delete-process process))
     (when buffer
       (switch-to-buffer buffer)
       (set-buffer-modified-p nil)
       (kill-buffer buffer))))

Sunday, August 03, 2014

Emacs to Word

Emacs で書いた文章をワープロに持っていく時、改行を除きたいものです。 標準の M-^ だと改行が単語を割らないとしてスペースでつなぎます。日本語ではそのそのスペースが不要です。以下のように ssk-j-mode が non-nil 時だけ、そのスペースを取り除きます。

(defun delete-indentation-ja()
  "Join current to previous line without whitespace when
`skk-j-mode' is non-nil.  This is supposed to bind to `M-^'."
  (if skk-j-mode
(global-set-key (kbd "M-^") 'delete-indentation-ja)

Sunday, May 18, 2014

A package smartrep to save fifth finger on left hand

To move around windows, I have to type "C-x o" often.  A package "smartrep" reduces a burden on fifth finger on left hand.

(require 'smartrep)
(smartrep-define-key global-map "C-x"
  '(("o" . 'other-window)
    ("k" . '(lambda () (interactive) (kill-buffer (window-buffer))))    ("0" . 'delete-window)
    ("2" . 'split-window-below)
    ("3" . 'split-window-right)
    ("^" . 'enlarge-window)
    ("{" . 'shrink-window-horizontally)
    ("}" . 'enlarge-window-horizontally)))

Friday, April 11, 2014

Mac and Windows friendly setup

 Since all people in our laboratory refuse to use Emacs, I decided to make Emacs behave like other applications on Windows or Mac.

(global-set-key (kbd "s-x") 'kill-region)
(global-set-key (kbd "s-c") 'kill-ring-save)
(global-set-key (kbd "s-v") 'yank)
(global-set-key (kbd "s-z") 'undo)
(global-set-key (kbd "s-a") 'mark-whole-buffer)
(global-set-key (kbd "s-w") 'close-frame-or-kill-buffer)
(global-set-key (kbd "s-n") 'new-frame-with-new-buffer)
(defun new-frame-with-new-buffer ()
  "Create a new frame with a new buffer `untitled-n'."
  (let* ((n-th 1)
         (dir (format-time-string "~/deleteme.d/%Y-%m" (current-time)))
         (region-p (region-active-p)))
    (if region-p (kill-ring-save (region-beginning) (region-end)))
    (switch-to-buffer-other-frame "*scratch*")
    (make-directory dir t)
    (while (file-exists-p (setq file (format "%s/untitled-%d" dir n-th)))
      (setq n-th (+ n-th 1)))
    (find-file file)
    (emacs-lisp-mode) ;; to use (lispxmp)
    (when region-p
    (write-file file))
  (when (fboundp 'w32-send-sys-command)
    (w32-send-sys-command #xf030)
    (w32-send-sys-command #xf120)))
(defvar no-warning-close-frame-or-kill-buffer nil)
(defun close-frame-or-kill-buffer ()
  "Close a frame if there are more than two frames.  If there is
only one frame, kill selected-buffer.  To avoid closing
frame/buffer by mistype of M-w, prompt for an action for the
first time."
  (let* ((close-buffer-p (= 1 (length (visible-frame-list))))
        (name-obj (if close-buffer-p "buffer" "frame")))
    (if (or no-warning-close-frame-or-kill-buffer
            (setq no-warning-close-frame-or-kill-buffer
                  (yes-or-no-p (format "Are you sure you want to close a %s?" name-obj))))
        (if close-buffer-p (kill-this-buffer) (delete-frame)))))

Saturday, April 05, 2014

Specify a dedicated window for other-window in Emacs

There is a way to specify a window to be used by (other-window).  Also, after a buffer poped up on a unexpected window, you can slide it to the right place.

(winner-mode 1)
(defvar window-locked-p nil)

(defun lockon-window ()
  "Set a window for other-window.  To set the target window,
    move point to the window, then call this.  Technically this sets
    all window dedicated but the target window.  Call again to free
   (lambda (win)
     (set-window-dedicated-p win (not window-locked-p))))
  (if window-locked-p
      (message "Windows are free")
    (set-window-dedicated-p (selected-window) nil)
    (message "Window is locked on"))
  (setq window-locked-p (not window-locked-p)))

(defun freeze-window ()
  "This freezes or locks a window where point is.  Call again to unfreeze.
    Technically this toggles window-dedicate-p property of a window."
   (not (window-dedicated-p (selected-window))))
  (if (window-dedicated-p (selected-window))
      (message (format "%s is freezed" (selected-window)))
    (message (format "%s is free" (selected-window)))))

(defun slide-window ()
  "This slides a buffer in a window where point is to
    next-window.  Technically this recalls previous windows-set by
    winnder-undo then shows the buffer to next-window"
  (let ((buf (buffer-name)))
    (set-window-buffer (next-window) buf)
    ;; (switch-to-buffer buf)
    (message "Window is slided")
    (other-window 1)))

Wednesday, March 26, 2014

A price table for orgtbl-mode using Emacs

You don't need Excel for price calculation but Emacs + orgtbl.

| item     | vendor | price | n | cost |
| monitor  | Eizo   | 10000 | 2 |      |
| keyboard | Topre  |  5000 | 3 |      |
| total    |        |       |   |      |
#+TBLFM: $5=$3*$4::@>$>=vsum(@-II..@-I)

LaTeX table from Emacs org-table

To make a scientific LaTeX table using Emacs + yasnippet + org-tbl is very comfortable.  First you have to convert your table into orgtbl somehow.  To convert the orgtbl to a LaTeX table, use following snippet.  See also instruction for org-mode.

#contributor : Tak Kunihiro
#name : a new table
#key : table
#Last modified: Fri Feb 07 16:06:35 JST 2014
# --
% `(turn-on-orgtbl)`
% ${1:Results} ${2:are} shown in Table \\ref{${3:`(file-name-nondirectory (directory-file-name (file-name-directory buffer-file-name)))`/`(file-name-base buffer-file-name)`}}.
%% ---------------
\begin{tabular}{ ${7:lll} }
${4:name} & ${5:value} & ${6:remarks} \\\\
#+ORGTBL: SEND tbl:$3 orgtbl-to-latex :no-escape t
| $4 | $5 | $6 |
|      |       |         |
%% ---------------

Saturday, February 01, 2014

Interaction between bash and dired in Emacs

Switch back and forth between Dired on Emacs and bash on Terminal is a headache. Add a visiting directory onto "~/.bash_history" and refer it by "tail -n 1" will help with following setup.

on ~/.emacs.d/init.el
(require 's)
(add-hook 'dired-mode-hook 'append-pwd-bash-history)
(add-hook 'find-file-hook 'append-pwd-bash-history)
(defun append-pwd-bash-history (&optional DIR)
  "Append a DIR where Dired visited to `~/.bash_history'."
  (setq DIR (or DIR (expand-file-name default-directory)))
  (let ((DIROUT DIR))
    (with-current-buffer (find-file-noselect "~/.bash_history") ;; $HISTFILE
      (if windows-p
          (setq DIROUT (shell-command-to-string (format "cygpath %s" (shell-quote-argument DIR)))))
      (setq DIROUT (replace-regexp-in-string "\\([@ #]\\)" "\\\\\\1" DIROUT)) ; escape
      (unless (string= DIROUT "")
        (goto-char (point-max))
        (insert (format "cd %s\n" (s-chomp DIROUT)))
 on ~/.bashrc
function cdd {
  lastdir=`grep "^cd /[a-zA-Z0-9]" $HOME/.bash_history | tail -1 | awk '{print $2}'`;
  if [ "$lastdir" != "" ] ; then
    cd "$lastdir";
  echo cd `pwd`;