1;;; extraedit.el --- Extra useful edit functions and macros
3;; Copyright (C) 2003 Free Software Foundation, Inc.
5;; Author: Anand B Pillai <abpillai@_remove_me_gmail.com>
6;; Keywords: tools, convenience, matching, lisp
8;; This file is free software; you can redistribute it and/or modify
9;; it under the terms of the GNU General Public License as published by
10;; the Free Software Foundation; either version 2, or (at your option)
13;; This file is distributed in the hope that it will be useful,
14;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;; GNU General Public License for more details.
18;; You should have received a copy of the GNU General Public License
19;; along with GNU Emacs; see the file COPYING. If not, write to
20;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21;; Boston, MA 02111-1307, USA.
25;; Mon Mar 03 15:00:43 2003 - First version.
26;; Jan 17 2005 - Fixed bugs.
27;; July 2005 - Added 2 functions, re-ordered.
33(setq indent-tabs-mode nil)
35(defmacro get-current-line()
37 (buffer-substring (save-excursion (beginning-of-line) (point))
38 (save-excursion (end-of-line) (point))))
40(defmacro line-length()
41 "Length of a line in number of characters"
42 (length (buffer-substring (save-excursion (beginning-of-line) (point))
43 (save-excursion (end-of-line) (point)))))
45;; kill the first word in all lines a buffer
46(defun kill-first-word()
47 "Kill first word in all lines in a buffer"
55(defun kill-first-char()
56 "Kill first character in all lines in a buffer"
64;; insert a word at end of all lines in a buffer
65(defun insert-word-line-end(arg)
66 "Insert a word at end of all lines in a buffer"
67 (interactive "sWord: ")
70 (while (re-search-forward "$")
74;; insert a word at beginning of all lines in a buffer
75(defun insert-word-line-start(arg)
76 "Insert a word at beginning of all lines in a buffer"
77 (interactive "sWord: ")
80 (while (re-search-forward "^")
84;; remove all newlines in a buffer
85(defun remove-newlines()
86 "Remove newlines from a buffer"
90 (while (re-search-forward "\n")
91 (replace-string "\n" "")
94;; remove duplicate lines in a buffer
95(defun remove-duplicate-lines()
96 "Remove duplicate lines in a buffer"
100 ((lines_hash (make-hash-table :test #'equal))
101 (numlines (count-lines 1 (progn (end-of-buffer)(point)))))
103 ;; Make a hash table with key=line
104 ;; and value=the smallest line number that contains a line.
105 (loop for i from numlines downto 1 do
108 (setf line (get-current-line))
109 ;; Want to store the smallest line number for
110 ;; a particular line.
111 (setf (gethash line lines_hash) i)))
112 ;; If a line has a line number not equal to the smallest line, kill it.
113 (loop for i from numlines downto 1 do
116 (setf line (get-current-line))
118 (if (not (equal line ""))
120 (let ((min-line (gethash line lines_hash)))
127;; Comment a line and duplicate it
128(defun line-comment-and-duplicate()
129 "Comment a line and duplicate it."
132 (beg (line-beginning-position))
133 (end (+ 1 (line-end-position))))
134 (copy-region-as-kill beg end)
135 (comment-region beg end)
141;; comment out current line
143 "Comments out current line."
145 (comment-region (+ (current-indentation) (line-beginning-position)) (+ 1 (line-end-position))))
147;; Uncomment current line
148(defun line-uncomment()
149 "Uncomments current line."
151 (uncomment-region (+ (current-indentation) (line-beginning-position)) (+ 1 (line-end-position))))
153;; Uncomment matching lines in a buffer, matching the regular expression
155(defun uncomment-matching( regexp )
156 "Uncomment lines matching regular expressions in a buffer."
157 (interactive "sRegexp: ")
159 (goto-char (point-min))
160 (while (re-search-forward regexp (point-max) t)
163(global-set-key "\C-c\C-u" 'uncomment-matching)
165;; Comment matching lines in a buffer, matching the regular expression
167(defun comment-matching (regexp)
168 "Comment lines matching regular expressions in a buffer."
169 (interactive "sRegexp: ")
171 (goto-char (point-min))
172 (while (re-search-forward regexp (point-max) t)
175(global-set-key "\C-c\C-m" 'uncomment-matching)
177;; Comment a pargraph forward
178(defun para-comment-forward()
179 "Comment a paragraph forward from the current line."
181 (while (not (eq (line-length) 0))
185;; Comment a paragraph backward
186(defun para-comment-backward()
187 "Comment a paragraph backward from the current line."
189 (while (not (eq (line-length) 0))
193;; Comment out a paragraph and duplicate it
194(defun para-comment-and-duplicate()
195 "Comment out a paragraph and duplicate it"
197 (let ((beg (line-beginning-position))
198 (end (save-excursion (forward-paragraph) (point))))
199 (copy-region-as-kill beg end)
200 (para-comment-forward)
203(defun duplicate-paragraph()
204 "Duplicate a paragraph"
206 (let ((beg (line-beginning-position))
207 (end (save-excursion (forward-paragraph) (point))))
208 (copy-region-as-kill beg end)
211;; Delete alternate newlines in a buffer
212(defun alt-newline-yank()
213 "Kill alternate newlines in buffer"
215 (while (not (looking-at "\ \n"))
220(defun copy-region-between-lines(arg1 arg2)
221 """Copy region between two lines in a buffer.
222 The copied region can be pasted using <yank>"""
223 (interactive "nFrom line number: \nnTo line number: ")
225 (error "Invalid arguments"))
228 (setq beg (line-beginning-position))
230 (setq end (line-end-position))
231 (copy-region-as-kill beg end))
232 (message "Copied region to clipboard"))
234;; Other stuff which actually don't belong here!
236;; Add a directory path to the emacs load path
237(defun add-path-to-loadpath( arg )
238 "Adds a directory to the load-path variable of emacs"
239 (interactive "DAdd directory to load path: ")
240 (setq load-path (cons arg load-path))
241 (let* ((files (directory-files arg t)) (len (length files)) f)
244 (if (equal (file-name-extension f) "el")
246 (message "Loading file %s..." f)
249 (setq files (cdr files)))))
251;; kill the first word in all lines a buffer
252;; Set default font to current frame font
253;; and save the setting in user's .emacs file.
254(defun set-current-font()
255"This function sets the current frame font as the default font
256and saves the option in users emacs init file"
258(let ((prevset) (curr-font (face-font 'default)))
259 (set-default-font curr-font)
260 ;;set it in .emacs file
261 (setq font-comment-string ";; default font setting\n")
262 (setq font-string (concat font-comment-string "(set-default-font \"" curr-font "\")\n"))
264 ;; emacs custom file is saved in the variable "user-init-file"
265 (set-buffer (find-file user-init-file))
266 (goto-char (point-min))
267 ;;find all occurences of previous font-setting
268 (while (re-search-forward "set-default-font" (point-max) t)
272 ;;delete the previous font-setting command strings
273 (kill-line)(kill-line)))
274 ;; no previous setting, insert setting at the end
277 (goto-char (point-max)))
278 (insert-string font-string)
279 (save-buffer) (kill-buffer (current-buffer)))))
282;;; extraedit.el ends here