Skip to content
Permalink
Browse files

cleanup embarrassing code and increasing robustness

  note: this will break calls to defjump as it is now written as a
        macro.  To use this new code you must unquote the first two
        arguments to defjump.

* jump.el (jump-remove-unwanted-files): whitespace fixes and adding
  documentation string
  (jump-to-file): now ensuring that directories exist before calling
  findr
  (defjump): rewrote this macro with defmacro rather than as a
  function with a quoted body
  • Loading branch information...
eschulte committed Oct 15, 2010
1 parent 0def544 commit 820bb7d81bf08cee6f2610965ca97e96a1fe561d
Showing with 48 additions and 38 deletions.
  1. +48 −38 jump.el
86 jump.el
@@ -115,10 +115,14 @@ Return the path selected or nil if files was empty."
(if file (find-file (cdr (assoc file files))))))

(defun jump-remove-unwanted-files (files)
(delete-if nil (mapcar (lambda (file-cons)
(unless (string-match
jump-ignore-file-regexp (cdr file-cons)) file-cons))
files)))
"Remove file matching `jump-ignore-file-regexp' from the list
of possible jumps."
(delete-if nil
(mapcar
(lambda (file-cons)
(unless (string-match jump-ignore-file-regexp (cdr file-cons))
file-cons))
files)))

(defun jump-to-file (&optional file)
"Open the file located at file if file ends in a / then look in
@@ -141,9 +145,14 @@ from all matches."
(jump-uniqueify file-cons))
(add-to-list 'file-alist file-cons)
file-cons))
(findr (car file-cons)
(expand-file-name (or (file-name-directory
(cdr file-cons)) "") root)))))))))
(let ((dir (expand-file-name
(or (file-name-directory (cdr file-cons)) "")
root)))
(when (and (file-exists-p dir) (file-directory-p dir))
(findr (car file-cons)
(expand-file-name
(or (file-name-directory
(cdr file-cons)) "") root)))))))))))

(defun jump-to-method (&optional method)
"If `jump-method' returns method in buffer, go to the first
@@ -251,7 +260,7 @@ MAKE to create the target file."
(t (message (format "unrecognized jump-from specification format %s")))))

;;;###autoload
(defun defjump (name specs root &optional doc make method-command)
(defmacro defjump (name specs root &optional doc make method-command)
"Define NAME as a function with behavior determined by SPECS.
SPECS should be a list of cons cells of the form
@@ -277,36 +286,37 @@ MAKE `find-file' will be used to open the path.
Optional argument METHOD-COMMAND overrides the function used to
find the current method which defaults to `which-function'."
(eval
`(defun ,name (&optional create) ,(concat doc "\n\nautomatically created by `defjump'")
(interactive "P")
(let ((root ,(if (functionp root) `(,root) root))
(method-command (quote ,(or method-command 'which-function)))
matches)
(loop ;; try every rule in mappings
for spec in (quote ,(mapcar
(lambda (spec)
(if (stringp (car spec))
;;xemacs did not understand :digit: class
(if (featurep 'xemacs)
(cons (replace-regexp-in-string
"\\\\[0-9]+" "\\\\(.*?\\\\)"
(car spec)) (cdr spec))
(cons (replace-regexp-in-string
"\\\\[[:digit:]]+" "\\\\(.*?\\\\)"
(car spec)) (cdr spec)))
spec))
specs))
;; don't stop until both the front and the back match
;;
;; the back should match if the user is presented with a list
;; of files, or a single file is jumped to
until (and (setf matches (jump-from (car spec)))
(cond
((equal t matches)
(jump-to (cdr spec) nil (if create (quote ,make))))
((consp matches)
(jump-to (cdr spec) matches (if create (quote ,make)))))))))))
`(defun ,name (&optional create)
,(concat doc "\n\nautomatically created by `defjump'")
(interactive "P")
(let ((root ,(if (functionp root) (root) root))
(method-command ,(or method-command 'which-function))
matches)
(loop ;; try every rule in mappings
for spec in (quote ,(mapcar
(lambda (spec)
(if (stringp (car spec))
;;xemacs did not understand :digit: class
(if (featurep 'xemacs)
(cons (replace-regexp-in-string
"\\\\[0-9]+" "\\\\(.*?\\\\)"
(car spec)) (cdr spec))
(cons (replace-regexp-in-string
"\\\\[[:digit:]]+" "\\\\(.*?\\\\)"
(car spec)) (cdr spec)))
spec))
specs))
;; don't stop until both the front and the back match
;;
;; the back should match if the user is presented with a list
;; of files, or a single file is jumped to
until
(and (setf matches (jump-from (car spec)))
(cond
((equal t matches)
(jump-to (cdr spec) nil (when create ,make)))
((consp matches)
(jump-to (cdr spec) matches (when create ,make)))))))))

(provide 'jump)
;;; jump.el ends here

0 comments on commit 820bb7d

Please sign in to comment.
You can’t perform that action at this time.