Showing posts with label elisp. Show all posts
Showing posts with label elisp. Show all posts

2013-06-19

using emacs to manage temporary files during debugging





a.lisp



We always need some temporary files to do editing work.
for example, we got 10000+ line logs to trace bug in it, first thing to do is create a tmp file.

1. To create a tmp file we'd like to
- quick open the files in a directory without naming collision, with short-key `C-; t`
- store the tmp files for future search/grep

;;code
    (defun create-and-open-tmp-file ()
      (interactive)
      (find-file (s-concat
              "~/Documents/hongmin.wang/Google Drive/notes/tmp"
              (format-time-string "%Y%m%d___%H_%M_%S")
              (uuid-string)
              ".temp")))
    (global-set-key (kbd "C-; t") 'create-and-open-tmp-file)

2. To manage versions of tmp file, we like to put it in a git direcotry
   and `C-x v i` to register the tmp file to git
   and `C-x v v` to commit new version of the tmp file to git.

3. sometime, you think the tmp file is no longer an tmp file,
   do a "Save As..." by `C-x C-w`


2012-11-18

[emacs]go to end-of-buffer ignore whitespaces





init.el




;;we always need goto end-of-buffer, so we bind it to C-. for simple.
;;sometimes we need goto end-of-buffer, but ignore whitespaces at end-of-buffer
;;so by give end-of-buffer an advice, it'll switch between these points, when you
;;continue typing C-.
(defadvice end-of-buffer (around move-backward-skip-whitespace)
  "Never use tabs for alignment."
  (let ((point-before-move (point)))
    ad-do-it
    (if (and (eq point-before-move (point)) (eq last-command this-command))
        (progn (search-backward-regexp "[^\n\s-]\\W*")
               (forward-char)))
    ))
(ad-activate 'end-of-buffer)
(define-key my-keys-minor-mode-map (kbd "C-.") 'end-of-buffer)

-->

2012-11-09

[emacs] compare with clipboard like IntelliJ IDEA





comparewithclipboard.el





(defun compare-with-clipboard  ()
  "compare current buffer with contents in clipboard"
  (interactive)  
  (let ((clipboard-buffer (get-buffer-create "*clipbord*"))
        (orig-buffer (current-buffer)))
    (switch-to-buffer clipboard-buffer)
    (delete-region (point-min) (point-max))
    (yank)
    (ediff-buffers clipboard-buffer orig-buffer)))

-->

2012-08-25

Favorite emacs key-bindings


(global-set-key "\M-n"  'next-buffer)
(global-set-key "\M-p"  'previous-buffer)
(global-set-key (kbd "C-c w") (quote copy-word))
(global-set-key (kbd "C-c l") (quote copy-line))
(global-set-key (kbd "C-c p") (quote copy-paragraph))
(global-set-key (kbd "C-c s") (quote thing-copy-string-to-mark))
(global-set-key (kbd "C-c a") (quote thing-copy-parenthesis-to-mark))

(global-set-key "\C-o"  'other-window)
(global-set-key "\M-o"  'other-window)

(defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")
(define-key my-keys-minor-mode-map (kbd "C-,") 'beginning-of-buffer)
(define-key my-keys-minor-mode-map (kbd "C-.") 'end-of-buffer)
(define-minor-mode my-keys-minor-mode
  "A minor mode so that my key settings override annoying major modes."
  t " my-keys" 'my-keys-minor-mode-map)
(my-keys-minor-mode 1)
(global-set-key (kbd "C-M-'") 'er/expand-region)
(define-key global-map (kbd "C-c SPC") 'ace-jump-mode)
(define-key global-map (kbd "C-;") 'ace-jump-char-mode)
(define-key global-map (kbd "C-'") 'ace-jump-line-mode)
(require 'mark-more-like-this)
(global-set-key (kbd "C-<") 'mark-previous-like-this)
(key-chord-define-global "zz" 'mark-previous-like-this)
(global-set-key (kbd "C->") 'mark-next-like-this)
(key-chord-define-global "??" 'mark-next-like-this)
(global-set-key (kbd "C-M-m") 'mark-more-like-this) 
(global-set-key (kbd "C-*") 'mark-all-like-this)
(key-chord-define-global ",," 'mark-all-like-this)
(key-chord-define-global "zk" 'zap-to-char)
(key-chord-define-global ";w" 'kill-region)
;;accumulate-kill
(defun append-kill-line (&optional arg)
  "Append kill-line to current kill buffer, prefix arg kills from beginning of line."
  (interactive "P")
  (append-next-kill)
  (kill-line arg)
)
(define-key global-map "\C-x\C-m" 'append-kill-line)






More on github.

2012-08-17

Enhance htmlize.el , now can export org-link.

Several Days ago, I found a problem when write blog using org-mode.
The 'html-region command in htmlize.el, just generate a CSS style for org-link,  not a real link.

So I went to great stackoverflow.com:
 http://stackoverflow.com/questions/11937649/in-emacs-how-to-export-links-to-a-clickable-link-when-htmlize-emacs-buffer


BACKGROUND
  1. I using great htmlize.el to export my org-mode buffer contents with font hi-lock.
  2. Emacs org-mode has a Link format.
PROBLEM
For Example, here is a org-mode file with contents:
[[http://hunmr.blogspot.com][blog]]
When I Using Htmlize.el to htmlize buffer to HTML contents, The link was missing. produces HTML like:
<span style="hyperlinkFOOBAR">blog</span>
EXPECTED
I expected it produces clickable link like:
<a style="hyperlinkFOOBAR" href="http://hunmr.blogspot.com">blog</a>
QUESTION
EDIT1 The org-export-as-html can export link, but can not create CSS for the Hi-locks.
  • Do you know other ways to to export org-mode links to HTML?
  • To read the real link in org-mode buffer using elisp, how to do that? read text property?



----------------------------------------------------------------------------------------------------------------
@Andreas gave a great hint, so I tried to read the code of org-mode,
after write some elisp function, finally it worked.

The new htmlize.el was shared on github.com now:

https://github.com/whunmr/dotemacs/blob/master/site-lisp/htmlize.el
----------------------------------------------------------------------------------------------------------------


And this the main code:

(defun expand-org-link (&optional buffer)
  "Change [[url][shortname]] to [[url] [shortname]] by adding a space between url and shortname"
  (goto-char (point-min))
  (while (re-search-forward "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
                nil t)
    (let ((url (match-string 1))
      (link-text (match-string 3)))
      (delete-region (match-beginning 0) (match-end 0))
      (insert "[[" url "] [" link-text "]]"))))

(defun shrink-org-link (&optional buffer)
  "Change [[url] [shortname]] to [[url][shortname]], remove the space between url and shortname"
  (goto-char (point-min))
  (while (re-search-forward "\\[\\[\\([^][]+\\)\\] \\(\\[\\([^][]+\\)\\]\\)?\\]"
                nil t)
    (let ((url (match-string 1))
      (link-text (match-string 3)))
      (delete-region (match-beginning 0) (match-end 0))
      (insert "[[" url "][" link-text "]]"))))

(defun transform-org-link ()
  "transform htmlized  to "
  (goto-char (point-min))
  (while (re-search-forward "\\[\\[]+\\)>\\([^][]+\\)\\] \\[\\([^][]+\\)\\]\\]"
                nil t)
    (let ((style (match-string 1))
          (url (match-string 2))
      (link-text (match-string 3)))
      (delete-region (match-beginning 0) (match-end 0))
      (insert "" link-text ""))))