Thursday, April 26, 2007

Map on lines in Emacs


;;;--------------------------------------------------------------------
(defun u:map-lines (beg end func)

"Call given FUNC in the form (LAMBDA (LBEG LEND LSTR LCOUNT)...)
for every line in the region. By default just print them."

(interactive "r\nXMap Function ((LAMBDA (LSTR LBEG LEND LCOUNT) ...)): ")

(let ((count 1)
(inhibit-field-text-motion t))

(when (stringp func)
(setq func (car (read (eval (concat "(" func ")"))))))

(unless func
(setq func #'(lambda (s b e c) (princ (format "%4d:%s" c s)))))

(save-excursion
(save-match-data
(goto-char beg)

(let (lbeg
lend
lstr
(delta (- (point-max) end)))

(while (< (point) (- (point-max) delta))

(setq lbeg (line-beginning-position 1))
(setq lend (line-beginning-position 2))
(setq lstr (buffer-substring-no-properties lbeg lend))

(funcall func lstr lbeg lend count)

(goto-char lbeg) ; line can be changed
(forward-line 1)

(incf count)))))))


;;--------------------------------------------------------------------
(defun u:remove-duplicates (beg end)
(interactive "r")

(let (prev-line)
(u:map-lines beg end
#'(lambda (line b e)

(when (and prev-line
(string= prev-line line))

(delete-region b e))

(setq prev-line line)))))


;;--------------------------------------------------------------------
(defun u:num-lines (beg end start-num)
(interactive "r\np")

(when (not start-num) (setq start-num 0))
(u:map-lines beg
end
#'(lambda (s b e n)
(delete-region b e)
(insert (format "%d: %s" (+ n start-num -1) s)) )))

Wednesday, April 25, 2007

Loop on lines in emacs (version 1)


(defmacro do-lines (args &rest body)
(let ((line (gensym "line-"))
(beg (gensym "beg-"))
(end (gensym "end-")))

`(let (,@args)
(save-excursion
(save-match-data
(goto-char (point-min))
(while (< (point) (point-max))
(setq ,beg (point))
(forward-line 1)
(setq ,end (point))
(setq ,line (buffer-substring-no-properties ,beg ,end))

(setq ,(car args) ,line)
(when ',(cdr args) (setq ,(cadr args) ,beg))
(when ',(cddr args) (setq ,(caddr args) ,end))

(progn ,@body)))))))

(let (lines)
(do-lines (l b e)
(push (cons l e) lines))
(reverse lines))

Tuesday, April 17, 2007

How to open file in some applicationl with AppleScript

The one way is:

set the_application to path to application "TextEdit"
tell application "Finder"
open the_file using the_application
end tell


but the best seems to be:

set f to choose file of type "TEXT"
tell application "Finder"
set a to application file id "com.apple.TextEdit"
open f using a
end tell

Monday, April 9, 2007

EMACS HOOKS FOR SWITCHING HEADER TO PROPER MODE


;; C-HOOK FOR SWITCHING HEADER TO PROPER MODE
(defun u:objc-h-match ()
(and (string-match "\\.[hH]$" (buffer-file-name))
(string-match "^\\(@\\|#import\\)" (buffer-string))))

(defun u:c++-h-match ()
(and (string-match "\\.[hH]$" (buffer-file-name))
(string-match "^\s*\\(class\s*.*\\(\s*:\s*public\\|protected\\|private\\)?\\)\\|\\(public\\|protected\\|private\\):\\|\\\\|\\"
(buffer-string))))

(push '(u:objc-h-match . objc-mode) magic-mode-alist)
(push '(u:c++-h-match . c++-mode) magic-mode-alist)