Initial Commit
[packages] / xemacs-packages / hyperbole / hactypes.el
1 ;;; hactypes.el --- Default action types for Hyperbole.
2
3 ;; Copyright (C) 1991-1995, 2006 Free Software Foundation, Inc.
4 ;; Developed with support from Motorola Inc.
5
6 ;; Author: Bob Weiner, Brown U.
7 ;; Maintainer: Mats Lidell <matsl@contactor.se>
8 ;; Keywords: extensions, hypermedia
9
10 ;; This file is part of GNU Hyperbole.
11
12 ;; GNU Hyperbole is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 3, or (at
15 ;; your option) any later version.
16
17 ;; GNU Hyperbole is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 ;;;
32 ;;; Other required Elisp libraries
33 ;;;
34
35 (mapcar 'require '(hbut hpath hargs hact hmail))
36
37 ;;;
38 ;;; Standard Hyperbole action types
39 ;;;
40
41 (defact annot-bib (key)
42   "Follows internal ref KEY within an annotated bibliography, delimiters=[]."
43   (interactive "sReference key (no []): ")
44   (let ((opoint (point)))
45     (goto-char 1)
46     (if (re-search-forward
47          (concat "^[*]*[ \t]*\\\[" (ebut:key-to-label key) "\\\]") nil t)
48         (progn
49           (beginning-of-line)
50           (delete-other-windows)
51           (split-window-vertically nil)
52           (goto-char opoint))
53       (beep))
54     (goto-char opoint)))
55
56 (defact completion ()
57   "Inserts completion at point into minibuffer or other window.
58 Unless at end of buffer or if completion has already been inserted, then
59 deletes completions window."
60   (interactive)
61   (if (eobp)
62       (progn (bury-buffer nil)
63              (delete-window))
64     (hargs:completion)))
65
66 (defact eval-elisp (lisp-expr)
67   "Evaluates a Lisp expression LISP-EXPR."
68   (interactive "xLisp to eval: ")
69   (eval lisp-expr))
70
71 (defact exec-kbd-macro (kbd-macro &optional repeat-count)
72   "Executes KBD-MACRO REPEAT-COUNT times.
73 KBD-MACRO may be a string of editor command characters, a function symbol or
74 nil to use the last defined keyboard macro.
75 Optional REPEAT-COUNT nil means execute once, zero means repeat until
76 error."
77   (interactive
78    (let (macro repeat)
79      (setq macro (intern-soft
80                   (hargs:read-match
81                    "Unquoted macro name or nil for last one defined: "
82                    obarray (function
83                             (lambda (sym)
84                               (and (fboundp sym)
85                                    (stringp (hypb:indirect-function sym)))))
86                    nil "nil" 'symbol)))
87      (cond ((fboundp macro))
88            ((null last-kbd-macro)
89             (hypb:error
90               "(exec-kbd-macro): Define a keyboard macro first."))
91            (t (fset 'zzz last-kbd-macro)
92               (setq macro 'zzz)))
93      (save-excursion
94        (let ((standard-output (get-buffer-create "*macro-def*")))
95          (unwind-protect
96              (progn (set-buffer standard-output)
97                     (setq buffer-read-only nil)
98                     (erase-buffer)
99                     (insert-kbd-macro macro)
100                     (goto-char (point-min))
101                     (setq macro (car (cdr (cdr (read (current-buffer)))))))
102            (kill-buffer standard-output))))
103      (fmakunbound 'zzz)
104      (setq repeat (hargs:read "Repeat count: "
105                              (function
106                               (lambda (repeat)
107                                 (or (null repeat)
108                                     (and (integerp repeat) (>= repeat 0)))))
109                              1))
110      (list macro repeat)))
111   (if (interactive-p)
112       nil
113     (or (and kbd-macro (or (stringp kbd-macro)
114                        (and (symbolp kbd-macro) (fboundp kbd-macro))))
115         (hypb:error "(exec-kbd-macro): Bad macro: %s" kbd-macro))
116     (or (null repeat-count) (and (integerp repeat-count) (<= 0 repeat-count))
117         (hypb:error "(exec-kbd-macro): Bad repeat count: %s" repeat-count)))
118   (execute-kbd-macro kbd-macro repeat-count))
119
120 ;;; Support next two actypes on systems which use the 'comint' shell package
121 ;;; rather than Emacs V18 shell.el.
122 ;;;
123 (if (or hyperb:xemacs-p hyperb:emacs19-p)
124     (require 'comint))
125 (and (fboundp 'comint-send-input) (not (fboundp 'shell-send-input))
126      (fset 'shell-send-input 'comint-send-input))
127 (and (fboundp 'comint-kill-output) (not (fboundp 'kill-output-from-shell))
128      (fset 'kill-output-from-shell 'comint-kill-output))
129 (and (fboundp 'comint-show-output) (not (fboundp 'show-output-from-shell))
130      (fset 'show-output-from-shell 'comint-show-output))
131
132 (defact exec-shell-cmd (shell-cmd &optional internal-cmd kill-prev)
133   "Executes a SHELL-CMD string asynchronously.
134 Optional non-nil second argument INTERNAL-CMD means do not display the shell
135 command line executed.  Optional non-nil third argument KILL-PREV means
136 kill last output to shell buffer before executing SHELL-CMD."
137   (interactive
138    (let ((default  (car defaults))
139          (default1 (nth 1 defaults))
140          (default2 (nth 2 defaults)))
141    (list (hargs:read "Shell cmd: "
142                      (function
143                       (lambda (cmd) (not (string= cmd ""))))
144                     default "Enter a shell command." 'string)
145          (y-or-n-p (format "Omit cmd from output (default = %s): "
146                            default1))
147          (y-or-n-p (format "Kill prior cmd's output (default = %s): "
148                            default2)))))
149   (let ((buf-name "*Hypb Shell*")
150         (owind (selected-window)))
151     (unwind-protect
152         (progn
153           (if (not (hpath:ange-ftp-p default-directory))
154               (setq shell-cmd
155                     (concat "cd " default-directory "; " shell-cmd)))
156           (if (not (get-buffer buf-name))
157               (save-excursion
158                 ;; Ensure shell displays in other window unless in the
159                 ;; OO-Browser, then use selected window.
160                 (if (br-in-browser)
161                     nil
162                   (if (= (length (hypb:window-list)) 1)
163                       (split-window-vertically))
164                   (other-window 1))
165                 (if (eq (minibuffer-window) (selected-window))
166                     (other-window 1))
167                 (shell) (rename-buffer buf-name)
168                 (setq last-input-start (point-marker)
169                       last-input-end (point-marker))
170                 (if (fboundp 'comint-kill-output)
171                     (setq comint-last-input-start last-input-start
172                           comint-last-input-end last-input-end)
173                   )))
174           (or (equal (buffer-name (current-buffer)) buf-name)
175               (if (br-in-browser) (switch-to-buffer buf-name)
176                 (pop-to-buffer buf-name)))
177           (goto-char (point-max))
178           (and kill-prev last-input-end
179                (not (equal last-input-start last-input-end))
180                (kill-output-from-shell))
181           (insert shell-cmd)
182           (shell-send-input)
183           (show-output-from-shell)
184           (or internal-cmd (scroll-down 1)))
185       (select-window owind))))
186
187 (defact exec-window-cmd (shell-cmd)
188   "Executes an external window-based SHELL-CMD string asynchronously."
189   (interactive
190    (let ((default  (car defaults)))
191      (list (hargs:read "Shell cmd: "
192                        (function
193                         (lambda (cmd) (not (string= cmd ""))))
194                        default "Enter a shell command." 'string))))
195   (let ((buf-name "*Hypb Shell*")
196         (cmd (if (hpath:ange-ftp-p default-directory)
197                  (concat "(" shell-cmd ") &")
198                (concat "(cd " default-directory "; " shell-cmd ") &")))
199         (msg (format "Executing: %s" shell-cmd))
200         (shell-buf))
201     (message msg)
202     (save-excursion
203       (save-window-excursion
204         (if (not (get-buffer buf-name))
205             (progn (save-excursion
206                      (save-window-excursion
207                        (shell)
208                        (setq shell-buf (current-buffer))))
209                    (message msg)
210                    ;; Wait for shell to startup before sending it input.
211                    (sit-for 1)
212                    (set-buffer shell-buf)
213                    (rename-buffer buf-name)
214                    (setq last-input-start (point-marker)
215                          last-input-end (point-marker))
216                    (if (fboundp 'comint-kill-output)
217                        (setq comint-last-input-start last-input-start
218                              comint-last-input-end last-input-end)
219                      )))
220         (or (equal (buffer-name (current-buffer)) buf-name)
221             (set-buffer buf-name))
222         (goto-char (point-max))
223         (insert cmd)
224         (shell-send-input)))
225     (message msg)))
226
227 (defact hyp-config (&optional out-buf)
228   "Inserts Hyperbole configuration info at end of optional OUT-BUF or current."
229   (hypb:configuration out-buf))
230
231 (defact hyp-request (&optional out-buf)
232   "Inserts Hyperbole mail list request help into optional OUT-BUF or current."
233   (save-excursion
234     (and out-buf (set-buffer out-buf))
235     (goto-char (point-max))
236     (delete-blank-lines) (delete-blank-lines)
237     (insert "Use one of the following formats on your subject line:\n
238 Subject: Subscribe <joe@any.com> (Joe Williams).
239 Subject: Unsubscribe <joe@any.com>.
240
241 To change your address, first unsubscribe by sending an unsubscribe
242 request from your old address.  Then subscribe by sending a subscribe
243 request from your new address.
244
245 Possible mail lists are:
246   hyperbole          - discussion of Hyperbole
247   hyperbole-announce - Hyperbole announcements only\n")))
248
249 (defact hyp-source (buf-str-or-file)
250   "Displays a buffer or file from a line beginning with 'hbut:source-prefix'."
251   (interactive
252    (list (prin1-to-string (get-buffer-create
253                            (read-buffer "Buffer to link to: ")))))
254   (if (stringp buf-str-or-file)
255       (cond ((string-match "\\`#<buffer \"?\\([^ \n\"]+\\)\"?>" buf-str-or-file)
256              (pop-to-buffer (substring buf-str-or-file
257                                        (match-beginning 1) (match-end 1))))
258             (t (hpath:find-other-window buf-str-or-file)))
259     (hypb:error "(hyp-source): Non-string argument: %s" buf-str-or-file)))
260
261 (defact link-to-buffer-tmp (buffer)
262   "Displays a BUFFER in another window.
263 Link is generally only good for current Emacs session.
264 Use 'link-to-file' instead for a permanent link."
265   (interactive "bBuffer to link to: ")
266   (if (or (stringp buffer) (bufferp buffer))
267       (pop-to-buffer buffer)
268     (hypb:error "(link-to-buffer-tmp): Not a current buffer: %s" buffer)))
269
270 (defact link-to-directory (directory)
271   "Displays a DIRECTORY in Dired mode in another window."
272   (interactive "DDirectory to link to: ")
273   (hpath:find-other-window directory))
274
275 (defact link-to-ebut (key-file key)
276   "Performs action given by another button, specified by KEY-FILE and KEY."
277   (interactive
278    (let (but-file but-lbl)
279      (while (cond ((setq but-file
280                          (read-file-name
281                           "File of button to link to: " nil nil t))
282                    (if (string= but-file "")
283                        (progn (beep) t)))
284                   ((not (file-readable-p but-file))
285                    (message "(link-to-ebut): You cannot read '%s'."
286                             but-file)
287                    (beep) (sit-for 3))))
288      (list but-file
289            (progn
290              (find-file-noselect but-file)
291              (while (string= "" (setq but-lbl
292                                       (hargs:read-match
293                                        "Button to link to: "
294                                        (ebut:alist but-file)
295                                        nil nil nil 'ebut)))
296                (beep))
297              (ebut:label-to-key but-lbl)))))
298   (or (interactive-p)
299       (setq key-file (hpath:validate (hpath:substitute-value key-file))))
300   (let ((but (ebut:get key (find-file-noselect key-file))))
301     (if but (hbut:act but)
302       (hypb:error "(link-to-ebut): No button '%s' in '%s'." (ebut:key-to-label key)
303              key-file))))
304
305 (defact link-to-elisp-doc (func-symbol)
306   "Displays documentation for FUNC-SYMBOL."
307   (interactive "aFunction to display doc for: ")
308   (cond ((not (symbolp func-symbol))
309          (hypb:error "(link-to-elisp-doc): '%s' not a symbol."
310                 func-symbol))
311         ((not (fboundp func-symbol))
312          (hypb:error "(link-to-elisp-doc): '%s' not defined as a function."
313                 func-symbol))
314         ((not (documentation func-symbol))
315          (hypb:error "(link-to-elisp-doc): '%s' has no documentation."
316                 func-symbol))
317         ((describe-function func-symbol))))
318
319 (defact link-to-file (path &optional point)
320   "Displays a PATH in another window scrolled to optional POINT.
321 With POINT, buffer is displayed with POINT at the top of the window."
322   (interactive
323    (let ((prev-reading-p hargs:reading-p))
324      (unwind-protect
325          (let* ((default (car defaults))
326                 (hargs:reading-p 'file)
327                 (path (read-file-name "Path to link to: " default default))
328                 (path-buf (get-file-buffer path)))
329            (if path-buf
330                (save-excursion
331                  (set-buffer path-buf)
332                  (setq hargs:reading-p 'character)
333                  (if (y-or-n-p
334                       (format "y = Display at present position (line %d); n = no position: "
335                               (count-lines 1 (point))))
336                      (list path (point))
337                    (list path)))
338              (list path)))
339        (setq hargs:reading-p prev-reading-p))))
340   (and (hpath:find-other-window path)
341        (integerp point)
342        (progn (goto-char (min (point-max) point))
343               (recenter 0))))
344
345 (defact link-to-file-line (path line-num)
346   "Displays a PATH in another window scrolled to LINE-NUM."
347   (interactive "fPath to link to: \nnDisplay at line number: ")
348   (and (setq path (smart-tags-file-path path))
349        (hpath:find-other-window path)
350        (integerp line-num)
351        (progn (widen)
352               (goto-line line-num))))
353
354 (defact link-to-Info-node (node)
355   "Displays an Info NODE in another window.
356 NODE must be a string of the form `(file)nodename'."
357   (interactive "+IInfo (file)nodename to link to: ")
358   (require 'info)
359   (if (and (stringp node) (string-match "^(\\([^\)]+\\))\\(.*\\)" node))
360       (let ((nodename (substring node (match-beginning 2) (match-end 2)))
361             (file (hpath:absolute-to
362                    (substring node (match-beginning 1) (match-end 1))
363                    (if (boundp 'Info-directory-list)
364                        Info-directory-list
365                      Info-directory))))
366         (if (and file (setq file (hpath:substitute-value file)))
367             (let ((wind (get-buffer-window "*info*")))
368               (if wind (select-window wind)
369                 (pop-to-buffer (other-buffer)))
370               (info) (Info-goto-node (concat "(" file ")" nodename)))
371           (hypb:error "(link-to-Info-node): Bad node spec: '%s'" node)))))
372
373 (defact link-to-kcell (file cell-ref)
374   "Displays FILE with kcell given by CELL-REF at the top of the window.
375 See documentation for 'kcell:ref-to-id' for valid cell-ref formats.
376
377 If FILE is nil, the current buffer is used.
378 If CELL-REF is nil, the first cell in the view is shown."
379   (interactive "fKotl file to link to: \n+KKcell to link to: ")
380   (require 'kfile)
381   (cond ((and (stringp cell-ref) (= ?| (aref cell-ref 0)))
382          ;; Activate view spec in current window.
383          (kotl-mode:goto-cell cell-ref))
384         ((if file
385              (hpath:find-other-window file)
386            (pop-to-buffer (current-buffer) t))
387          (if cell-ref
388              (kotl-mode:goto-cell cell-ref)
389            (kotl-mode:beginning-of-buffer))
390          (recenter 0))))
391
392 (defact link-to-mail (mail-msg-id &optional mail-file)
393   "Displays mail msg with MAIL-MSG-ID from optional MAIL-FILE in other window.
394 See documentation for the variable 'hmail:init-function' for information on
395 how to specify a mail reader to use."
396   (interactive "+MMail Msg: ")
397   (if (not (fboundp 'rmail:msg-to-p))
398       (hypb:error "(link-to-mail): Invoke mail reader before trying to follow a mail link.")
399     (if (and (listp mail-msg-id) (null mail-file))
400         (setq mail-file (car (cdr mail-msg-id))
401               mail-msg-id (car mail-msg-id)))
402     (let ((wind (selected-window))
403           (wconfig (current-window-configuration)))
404       (other-window 1)
405       (if (eq wind (selected-window))
406           (progn (split-window-vertically nil) (other-window 1)))
407       ;; Initialize user-specified mail reader if need be.
408       (if (and (symbolp hmail:init-function)
409                (fboundp hmail:init-function)
410                (listp (symbol-function hmail:init-function))
411                (eq 'autoload (car (symbol-function hmail:init-function))))
412           (funcall hmail:init-function))
413       (if (rmail:msg-to-p mail-msg-id mail-file)
414           nil
415         ;; Couldn't find message, restore old window config, report error
416         (set-window-configuration wconfig)
417         (hypb:error "(link-to-mail): No msg '%s' in file \"%s\"."
418                     mail-msg-id mail-file)))))
419
420 (defact link-to-regexp-match (regexp n source &optional buffer-p)
421   "Finds REGEXP's Nth occurrence in SOURCE and displays location at window top.
422 SOURCE is a pathname unless optional BUFFER-P is non-nil, then SOURCE must be
423 a buffer name or buffer.
424 Returns t if found, signals an error if not."
425   (interactive "sRegexp to match: \nnOccurrence number: \nfFile to search: ")
426   (let ((orig-src source))
427     (if buffer-p
428         (if (stringp source)
429             (setq source (get-buffer source)))
430       ;; Source is a pathname.
431       (if (not (stringp source))
432           (hypb:error
433            "(link-to-regexp-match): Source parameter is not a filename: '%s'"
434            orig-src)
435         (setq source (find-file-noselect (hpath:substitute-value source)))))
436     (if (not (bufferp source))
437         (hypb:error
438          "(link-to-regexp-match): Invalid source parameter: '%s'" orig-src)
439       (switch-to-buffer-other-window source)
440       (widen)
441       (goto-char (point-min))
442       (if (re-search-forward regexp nil t n)
443           (progn (beginning-of-line) (recenter 0) t)
444         (hypb:error
445          "(link-to-regexp-match): Pattern not found: '%s'" regexp)))))
446
447 (defact link-to-rfc (rfc-num)
448   "Retrieves and displays an Internet rfc given by RFC-NUM.
449 RFC-NUM may be a string or an integer.  Requires ange-ftp or efs for
450 remote retrievals."
451   (interactive "nRFC number to retrieve: ")
452   (if (or (stringp rfc-num) (integerp rfc-num))
453       (hpath:find-other-window (hpath:rfc rfc-num))))
454
455 (defact link-to-string-match (string n source &optional buffer-p)
456   "Finds STRING's Nth occurrence in SOURCE and displays location at window top.
457 SOURCE is a pathname unless optional BUFFER-P is non-nil, then SOURCE must be
458 a buffer name or buffer.
459 Returns t if found, nil if not."
460   (interactive "sString to match: \nnOccurrence number: \nfFile to search: ")
461   (funcall (actype:action 'link-to-regexp-match)
462            (regexp-quote string) n source buffer-p))
463
464 (defact man-show (topic)
465   "Displays man page on TOPIC, which may be of the form <command>(<section>)."
466   (interactive "sManual topic: ")
467   (manual-entry topic))
468
469 (defact rfc-toc (&optional buf-name opoint)
470   "Computes and displays summary of an Internet rfc in BUF-NAME.
471 Assumes point has already been moved to start of region to summarize.
472 Optional OPOINT is point to return to in BUF-NAME after displaying summary."
473   (interactive)
474   (if buf-name
475       (cond ((get-buffer buf-name)
476              (switch-to-buffer buf-name))
477             ((let ((buf (get-file-buffer buf-name)))
478                (if buf
479                    (progn (switch-to-buffer (setq buf-name buf))
480                           t))))
481             (t (if opoint (goto-char opoint))
482                (hypb:error "(rfc-toc): Invalid buffer name: %s" buf-name))))
483   (let ((sect-regexp "^[ \t]*[1-9][0-9]*\\.[0-9.]*[ \t]+[^ \t\n]"))
484     (occur sect-regexp)
485     (set-buffer "*Occur*")
486     (rename-buffer (format "*%s toc*" buf-name))
487     (re-search-forward "^[ ]*[0-9]+:" nil t)
488     (beginning-of-line)
489     (delete-region (point-min) (point))
490     (insert "Contents of " (buffer-name occur-buffer) ":\n")
491     (set-buffer-modified-p nil)
492     (set-buffer buf-name)
493     (if opoint (goto-char opoint))))
494
495 (defact text-toc (section)
496   "Jumps to the text file SECTION referenced by a table of contents entry at point."
497   (interactive "sGo to section named: ")
498   (if (stringp section)
499       (progn
500         (actypes::link-to-regexp-match
501               (concat "^\\*+[ \t]*" (regexp-quote section))
502               1 (current-buffer) t)
503         (while (and (= (forward-line -1) 0)
504                     (looking-at "[ \t]*[-=][-=]")))
505         (forward-line 1)
506         (recenter 0))))
507
508 (provide 'hactypes)
509
510 ;;; hactypes.el ends here