Skip to content

Commit

Permalink
From one defuns.el to several.
Browse files Browse the repository at this point in the history
  • Loading branch information
magnars committed Nov 12, 2011
1 parent 30cfbc1 commit c3a86d3
Show file tree
Hide file tree
Showing 7 changed files with 345 additions and 352 deletions.
127 changes: 127 additions & 0 deletions defuns/buffer-defuns.el
@@ -0,0 +1,127 @@
;; Buffer-related defuns

(require 'imenu)

(defun create-scratch-buffer nil
"create a new scratch buffer to work in. (could be *scratch* - *scratchX*)"
(interactive)
(let ((n 0)
bufname)
(while (progn
(setq bufname (concat "*scratch"
(if (= n 0) "" (int-to-string n))
"*"))
(setq n (1+ n))
(get-buffer bufname)))
(switch-to-buffer (get-buffer-create bufname))
(if (= n 1) (lisp-interaction-mode)) ; 1, because n was incremented
))

(defun swap-windows ()
"If you have 2 windows, it swaps them." (interactive) (cond ((not (= (count-windows) 2)) (message "You need exactly 2 windows to do this."))
(t
(let* ((w1 (first (window-list)))
(w2 (second (window-list)))
(b1 (window-buffer w1))
(b2 (window-buffer w2))
(s1 (window-start w1))
(s2 (window-start w2)))
(set-window-buffer w1 b2)
(set-window-buffer w2 b1)
(set-window-start w1 s2)
(set-window-start w2 s1)))))

(defun ido-imenu ()
"Update the imenu index and then use ido to select a symbol to navigate to.
Symbols matching the text at point are put first in the completion list."
(interactive)
(imenu--make-index-alist)
(let ((name-and-pos '())
(symbol-names '()))
(flet ((addsymbols (symbol-list)
(when (listp symbol-list)
(dolist (symbol symbol-list)
(let ((name nil) (position nil))
(cond
((and (listp symbol) (imenu--subalist-p symbol))
(addsymbols symbol))

((listp symbol)
(setq name (car symbol))
(setq position (cdr symbol)))

((stringp symbol)
(setq name symbol)
(setq position (get-text-property 1 'org-imenu-marker symbol))))

(unless (or (null position) (null name))
(add-to-list 'symbol-names name)
(add-to-list 'name-and-pos (cons name position))))))))
(addsymbols imenu--index-alist))
;; If there are matching symbols at point, put them at the beginning of `symbol-names'.
(let ((symbol-at-point (thing-at-point 'symbol)))
(when symbol-at-point
(let* ((regexp (concat (regexp-quote symbol-at-point) "$"))
(matching-symbols (delq nil (mapcar (lambda (symbol)
(if (string-match regexp symbol) symbol))
symbol-names))))
(when matching-symbols
(sort matching-symbols (lambda (a b) (> (length a) (length b))))
(mapc (lambda (symbol) (setq symbol-names (cons symbol (delete symbol symbol-names))))
matching-symbols)))))
(let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
(position (cdr (assoc selected-symbol name-and-pos))))
(goto-char position))))

;;; These belong in coding-hook:

;; We have a number of turn-on-* functions since it's advised that lambda
;; functions not go in hooks. Repeatedly evaling an add-to-list with a
;; hook value will repeatedly add it since there's no way to ensure
;; that a lambda doesn't already exist in the list.

(defun local-column-number-mode ()
(make-local-variable 'column-number-mode)
(column-number-mode t))

(defun local-comment-auto-fill ()
(set (make-local-variable 'comment-auto-fill-only-comments) t)
(auto-fill-mode t))

(defun turn-on-hl-line-mode ()
(if window-system (hl-line-mode t)))

(defun turn-on-save-place-mode ()
(setq save-place t))

(defun turn-on-whitespace ()
(whitespace-mode t))

(defun turn-off-tool-bar ()
(tool-bar-mode -1))

(add-hook 'coding-hook 'local-column-number-mode)
(add-hook 'coding-hook 'local-comment-auto-fill)
(add-hook 'coding-hook 'turn-on-hl-line-mode)

(defun untabify-buffer ()
(interactive)
(untabify (point-min) (point-max)))

(defun indent-buffer ()
(interactive)
(indent-region (point-min) (point-max)))

(defun cleanup-buffer ()
"Perform a bunch of operations on the whitespace content of a buffer."
(interactive)
(indent-buffer)
(untabify-buffer)
(delete-trailing-whitespace))

(defun recentf-ido-find-file ()
"Find a recent file using ido."
(interactive)
(let ((file (ido-completing-read "Choose recent file: " recentf-list nil t)))
(when file
(find-file file))))

0 comments on commit c3a86d3

Please sign in to comment.