;;; semantic-picolisp.el --- Semantic details for PicoLisp

(require 'semantic)
(require 'semantic-picolisp-by)
(require 'backquote)

(eval-when-compile
  (require 'semantic-format))

;;; Code:

(defcustom-mode-local-semantic-dependency-system-include-path
  picolisp-mode semantic-default-picolisp-path
  '("/opt/picolisp/")
  "Default set of include paths for scheme (guile) code.
This should probably do some sort of search to see what is
actually on the local machine.")

(define-mode-local-override semantic-format-tag-prototype picolisp-mode (tag)
  "Return a prototype for the Emacs Lisp nonterminal TAG."
  (let* ((tok (semantic-tag-class tag))
	 (args (semantic-tag-components tag))
	 )
    (if (eq tok 'function)
	(concat (semantic-tag-name tag) " ("
		(mapconcat (lambda (a) a) args " ")
		")")
      (semantic-format-tag-prototype-default tag))))

(define-mode-local-override semantic-documentation-for-tag picolisp-mode (tag &optional nosnarf)
  "Return the documentation string for TAG.
Optional argument NOSNARF is ignored."
  (let ((d (semantic-tag-docstring tag)))
    (if (and d (> (length d) 0) (= (aref d 0) ?*))
	(substring d 1)
      d)))

(define-mode-local-override semantic-insert-foreign-tag picolisp-mode (tag tagfile)
  "Insert TAG from TAGFILE at point.
Attempts a simple prototype for calling or using TAG."
  (cond ((eq (semantic-tag-class tag) 'function)
	 (insert "(" (semantic-tag-name tag) " )")
	 (forward-char -1))
	(t
	 (insert (semantic-tag-name tag)))))

;; Note: Analyzer from Henry S. Thompson
(define-lex-regex-analyzer semantic-lex-picolisp-symbol
  "Detect and create symbol and keyword tokens."
  "\\(\\sw\\([:]\\|\\sw\\|\\s_\\)+\\)"
  ;; (message (format "symbol: %s" (match-string 0)))
  (semantic-lex-push-token
   (semantic-lex-token
    (or (semantic-lex-keyword-p (match-string 0)) 'symbol)
    (match-beginning 0) (match-end 0))))


(define-lex semantic-picolisp-lexer
  "A simple lexical analyzer that handles simple buffers.
This lexer ignores comments and whitespace, and will return
syntax as specified by the syntax table."
  semantic-lex-ignore-whitespace
  semantic-lex-ignore-newline
  semantic-lex-picolisp-symbol
  semantic-lex-charquote
  semantic-lex-paren-or-list
  semantic-lex-close-paren
  semantic-lex-string
  semantic-lex-ignore-comments
  semantic-lex-punctuation
  semantic-lex-number
  semantic-lex-default-action)

;;;###autoload
(defun semantic-default-picolisp-setup ()
  "Setup hook function for Emacs Lisp files and Semantic."
  (semantic-picolisp-by--install-parser)
  (setq semantic-symbol->name-assoc-list '( (variable . "Variables")
                                            ;;(type     . "Types")
                                            (function . "Functions")
                                            (include  . "Loads")
                                            (package  . "DefineModule"))
        imenu-create-index-function 'semantic-create-imenu-index
        imenu-create-index-function 'semantic-create-imenu-index
        )
  (setq semantic-lex-analyzer #'semantic-picolisp-lexer)
  )

;;;###autoload
(add-hook 'picolisp-mode-hook 'semantic-default-picolisp-setup)

(provide 'semantic-picolisp)

;;; semantic-picolisp.el ends here